2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
7 * This program and the accompanying materials are made available under the
8 * terms of the Eclipse Public License 2.0 which is available at
9 * http://www.eclipse.org/legal/epl-2.0
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.pushsafer.internal.actions;
15 import static org.hamcrest.CoreMatchers.is;
16 import static org.hamcrest.MatcherAssert.assertThat;
17 import static org.junit.jupiter.api.Assertions.assertThrows;
18 import static org.mockito.ArgumentMatchers.any;
19 import static org.mockito.Mockito.when;
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22 import org.eclipse.jdt.annotation.Nullable;
23 import org.junit.jupiter.api.BeforeEach;
24 import org.junit.jupiter.api.Test;
25 import org.junit.jupiter.api.extension.ExtendWith;
26 import org.mockito.Mock;
27 import org.mockito.junit.jupiter.MockitoExtension;
28 import org.mockito.junit.jupiter.MockitoSettings;
29 import org.mockito.quality.Strictness;
30 import org.openhab.binding.pushsafer.internal.connection.PushsaferConfigurationException;
31 import org.openhab.binding.pushsafer.internal.connection.PushsaferMessageBuilder;
32 import org.openhab.binding.pushsafer.internal.handler.PushsaferAccountHandler;
33 import org.openhab.core.thing.binding.ThingActions;
34 import org.openhab.core.thing.binding.ThingHandler;
37 * Unit tests for {@link PushsaferActions}.
39 * @author Kevin Siml - Initial contribution, forked from Christoph Weitkamp
41 @ExtendWith(MockitoExtension.class)
42 @MockitoSettings(strictness = Strictness.LENIENT)
43 public class PushsaferActionsTest {
45 private static final String MESSAGE = "My Message";
46 private static final String TITLE = "My Title";
47 private static final String URL = "https://www.test.com";
48 private static final String URL_TITLE = "Some Link";
49 private static final String RECEIPT = "12345";
52 private final ThingActions thingActionsStub = new ThingActions() {
54 public void setThingHandler(ThingHandler handler) {
58 public @Nullable ThingHandler getThingHandler() {
63 private @Mock PushsaferAccountHandler mockPushsaferAccountHandler;
65 private PushsaferActions pushsaferThingActions;
68 public void setUp() throws PushsaferConfigurationException {
69 pushsaferThingActions = new PushsaferActions();
71 when(mockPushsaferAccountHandler.getDefaultPushsaferMessageBuilder(any()))
72 .thenReturn(PushsaferMessageBuilder.getInstance("key", "user"));
73 when(mockPushsaferAccountHandler.sendPushsaferMessage(any())).thenReturn(Boolean.TRUE);
74 when(mockPushsaferAccountHandler.sendPushsaferPriorityMessage(any())).thenReturn(RECEIPT);
77 // sendPushsaferMessage
79 public void testSendMessageThingActionsIsNotPushsaferThingActions() {
80 assertThrows(ClassCastException.class,
81 () -> PushsaferActions.sendPushsaferMessage(thingActionsStub, MESSAGE, TITLE));
85 public void testSendMessageThingHandlerIsNull() {
86 assertThrows(RuntimeException.class,
87 () -> PushsaferActions.sendPushsaferMessage(pushsaferThingActions, MESSAGE, TITLE));
91 public void testSendMessageWithoutTitle() {
92 pushsaferThingActions.setThingHandler(mockPushsaferAccountHandler);
93 boolean sent = PushsaferActions.sendPushsaferMessage(pushsaferThingActions, MESSAGE, null);
94 assertThat(sent, is(true));
98 public void testSendMessage() {
99 pushsaferThingActions.setThingHandler(mockPushsaferAccountHandler);
100 boolean sent = PushsaferActions.sendPushsaferMessage(pushsaferThingActions, MESSAGE, TITLE);
101 assertThat(sent, is(true));
104 // sendPushsaferURLMessage
106 public void testSendURLMessageThingActionsIsNotPushsaferThingActions() {
107 assertThrows(ClassCastException.class,
108 () -> PushsaferActions.sendPushsaferURLMessage(thingActionsStub, MESSAGE, TITLE, URL, URL_TITLE));
112 public void testSendURLMessageThingHandlerIsNull() {
113 assertThrows(RuntimeException.class,
114 () -> PushsaferActions.sendPushsaferURLMessage(pushsaferThingActions, MESSAGE, TITLE, URL, URL_TITLE));
118 public void testSendURLMessageWithoutTitle() {
119 pushsaferThingActions.setThingHandler(mockPushsaferAccountHandler);
120 boolean sent = PushsaferActions.sendPushsaferURLMessage(pushsaferThingActions, MESSAGE, null, URL, URL_TITLE);
121 assertThat(sent, is(true));
125 public void testSendURLMessageWithoutURLTitle() {
126 pushsaferThingActions.setThingHandler(mockPushsaferAccountHandler);
127 boolean sent = PushsaferActions.sendPushsaferURLMessage(pushsaferThingActions, MESSAGE, TITLE, URL, null);
128 assertThat(sent, is(true));
132 public void testSendURLMessage() {
133 pushsaferThingActions.setThingHandler(mockPushsaferAccountHandler);
134 boolean sent = PushsaferActions.sendPushsaferURLMessage(pushsaferThingActions, MESSAGE, TITLE, URL, URL_TITLE);
135 assertThat(sent, is(true));
138 // sendPushsaferPriorityMessage
140 public void testSendPriorityMessageThingActionsIsNotPushsaferThingActions() {
141 assertThrows(ClassCastException.class, () -> PushsaferActions.sendPushsaferPriorityMessage(thingActionsStub,
142 MESSAGE, TITLE, PushsaferMessageBuilder.EMERGENCY_PRIORITY));
146 public void testSendPriorityMessageThingHandlerIsNull() {
147 assertThrows(RuntimeException.class, () -> PushsaferActions.sendPushsaferPriorityMessage(pushsaferThingActions,
148 MESSAGE, TITLE, PushsaferMessageBuilder.EMERGENCY_PRIORITY));
152 public void testSendPriorityMessageWithoutTitle() {
153 pushsaferThingActions.setThingHandler(mockPushsaferAccountHandler);
154 String receipt = PushsaferActions.sendPushsaferPriorityMessage(pushsaferThingActions, MESSAGE, null,
155 PushsaferMessageBuilder.EMERGENCY_PRIORITY);
156 assertThat(receipt, is(RECEIPT));
160 public void testSendPriorityMessage() {
161 pushsaferThingActions.setThingHandler(mockPushsaferAccountHandler);
162 String receipt = PushsaferActions.sendPushsaferPriorityMessage(pushsaferThingActions, MESSAGE, TITLE,
163 PushsaferMessageBuilder.EMERGENCY_PRIORITY);
164 assertThat(receipt, is(RECEIPT));