]> git.basschouten.com Git - openhab-addons.git/blob
e3c8385fec8b49f27c7ef3d34e2ca6d80192f6b6
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.pushover.internal.actions;
14
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;
20
21 import java.time.Duration;
22
23 import org.eclipse.jdt.annotation.NonNullByDefault;
24 import org.eclipse.jdt.annotation.Nullable;
25 import org.junit.jupiter.api.BeforeEach;
26 import org.junit.jupiter.api.Test;
27 import org.junit.jupiter.api.extension.ExtendWith;
28 import org.mockito.Mock;
29 import org.mockito.junit.jupiter.MockitoExtension;
30 import org.mockito.junit.jupiter.MockitoSettings;
31 import org.mockito.quality.Strictness;
32 import org.openhab.binding.pushover.internal.connection.PushoverMessageBuilder;
33 import org.openhab.binding.pushover.internal.handler.PushoverAccountHandler;
34 import org.openhab.core.thing.binding.ThingActions;
35 import org.openhab.core.thing.binding.ThingHandler;
36
37 /**
38  * Unit tests for {@link PushoverActions}.
39  *
40  * @author Christoph Weitkamp - Initial contribution
41  */
42 @NonNullByDefault
43 @ExtendWith(MockitoExtension.class)
44 @MockitoSettings(strictness = Strictness.LENIENT)
45 public class PushoverActionsTest {
46
47     private static final String MESSAGE = "My Message";
48     private static final String TITLE = "My Title";
49     private static final String URL = "https://www.test.com";
50     private static final String URL_TITLE = "Some Link";
51     private static final String RECEIPT = "12345";
52     private static final Duration TTL = Duration.ofSeconds(15);
53
54     private final ThingActions thingActionsStub = new ThingActions() {
55         @Override
56         public void setThingHandler(ThingHandler handler) {
57         }
58
59         @Override
60         public @Nullable ThingHandler getThingHandler() {
61             return null;
62         }
63     };
64
65     private @NonNullByDefault({}) @Mock PushoverAccountHandler mockPushoverAccountHandler;
66     private @NonNullByDefault({}) PushoverActions pushoverThingActions;
67
68     @BeforeEach
69     public void setUp() {
70         pushoverThingActions = new PushoverActions();
71
72         when(mockPushoverAccountHandler.getDefaultPushoverMessageBuilder(any()))
73                 .thenReturn(PushoverMessageBuilder.getInstance("key", "user"));
74         when(mockPushoverAccountHandler.sendMessage(any())).thenReturn(Boolean.TRUE);
75         when(mockPushoverAccountHandler.sendPriorityMessage(any())).thenReturn(RECEIPT);
76         when(mockPushoverAccountHandler.cancelPriorityMessage(RECEIPT)).thenReturn(Boolean.TRUE);
77     }
78
79     // sendMessage
80     @Test
81     public void testSendMessageThingActionsIsNotPushoverThingActions() {
82         assertThrows(ClassCastException.class, () -> PushoverActions.sendMessage(thingActionsStub, MESSAGE, TITLE));
83     }
84
85     @Test
86     public void testSendMessageThingHandlerIsNull() {
87         assertThrows(RuntimeException.class, () -> PushoverActions.sendMessage(pushoverThingActions, MESSAGE, TITLE));
88     }
89
90     @Test
91     public void testSendMessage() {
92         pushoverThingActions.setThingHandler(mockPushoverAccountHandler);
93         boolean sent = PushoverActions.sendMessage(pushoverThingActions, MESSAGE);
94         assertThat(sent, is(true));
95     }
96
97     @Test
98     public void testSendMessageWithTitle() {
99         pushoverThingActions.setThingHandler(mockPushoverAccountHandler);
100         boolean sent = PushoverActions.sendMessage(pushoverThingActions, MESSAGE, TITLE);
101         assertThat(sent, is(true));
102     }
103
104     @Test
105     public void testSendMessageWithTitleAndTTL() {
106         pushoverThingActions.setThingHandler(mockPushoverAccountHandler);
107         boolean sent = PushoverActions.sendMessage(pushoverThingActions, MESSAGE, TITLE, TTL);
108         assertThat(sent, is(true));
109     }
110
111     // sendURLMessage
112     @Test
113     public void testSendURLMessageThingActionsIsNotPushoverThingActions() {
114         assertThrows(ClassCastException.class,
115                 () -> PushoverActions.sendURLMessage(thingActionsStub, MESSAGE, TITLE, URL, URL_TITLE));
116     }
117
118     @Test
119     public void testSendURLMessageThingHandlerIsNull() {
120         assertThrows(RuntimeException.class,
121                 () -> PushoverActions.sendURLMessage(pushoverThingActions, MESSAGE, TITLE, URL, URL_TITLE));
122     }
123
124     @Test
125     public void testSendURLMessageWithoutTitle() {
126         pushoverThingActions.setThingHandler(mockPushoverAccountHandler);
127         boolean sent = PushoverActions.sendURLMessage(pushoverThingActions, MESSAGE, null, URL, URL_TITLE);
128         assertThat(sent, is(true));
129     }
130
131     @Test
132     public void testSendURLMessageWithoutURLTitle() {
133         pushoverThingActions.setThingHandler(mockPushoverAccountHandler);
134         boolean sent = PushoverActions.sendURLMessage(pushoverThingActions, MESSAGE, TITLE, URL, null);
135         assertThat(sent, is(true));
136     }
137
138     @Test
139     public void testSendURLMessage() {
140         pushoverThingActions.setThingHandler(mockPushoverAccountHandler);
141         boolean sent = PushoverActions.sendURLMessage(pushoverThingActions, MESSAGE, TITLE, URL, URL_TITLE);
142         assertThat(sent, is(true));
143     }
144
145     @Test
146     public void testSendURLMessageWithTTL() {
147         pushoverThingActions.setThingHandler(mockPushoverAccountHandler);
148         boolean sent = PushoverActions.sendURLMessage(pushoverThingActions, MESSAGE, TITLE, URL, URL_TITLE, TTL);
149         assertThat(sent, is(true));
150     }
151
152     // sendPriorityMessage
153     @Test
154     public void testSendPriorityMessageThingActionsIsNotPushoverThingActions() {
155         assertThrows(ClassCastException.class, () -> PushoverActions.sendPriorityMessage(thingActionsStub, MESSAGE,
156                 TITLE, PushoverMessageBuilder.EMERGENCY_PRIORITY));
157     }
158
159     @Test
160     public void testSendPriorityMessageThingHandlerIsNull() {
161         assertThrows(RuntimeException.class, () -> PushoverActions.sendPriorityMessage(pushoverThingActions, MESSAGE,
162                 TITLE, PushoverMessageBuilder.EMERGENCY_PRIORITY));
163     }
164
165     @Test
166     public void testSendPriorityMessageWithoutTitle() {
167         pushoverThingActions.setThingHandler(mockPushoverAccountHandler);
168         String receipt = PushoverActions.sendPriorityMessage(pushoverThingActions, MESSAGE, null,
169                 PushoverMessageBuilder.EMERGENCY_PRIORITY);
170         assertThat(receipt, is(RECEIPT));
171     }
172
173     @Test
174     public void testSendPriorityMessage() {
175         pushoverThingActions.setThingHandler(mockPushoverAccountHandler);
176         String receipt = PushoverActions.sendPriorityMessage(pushoverThingActions, MESSAGE, TITLE,
177                 PushoverMessageBuilder.EMERGENCY_PRIORITY);
178         assertThat(receipt, is(RECEIPT));
179     }
180
181     // cancelPriorityMessage
182     @Test
183     public void testCancelPriorityMessageThingActionsIsNotPushoverThingActions() {
184         assertThrows(ClassCastException.class, () -> PushoverActions.cancelPriorityMessage(thingActionsStub, RECEIPT));
185     }
186
187     @Test
188     public void testCancelPriorityMessageThingHandlerIsNull() {
189         assertThrows(RuntimeException.class,
190                 () -> PushoverActions.cancelPriorityMessage(pushoverThingActions, RECEIPT));
191     }
192
193     @Test
194     public void testCancelPriorityMessageWithValidReceipt() {
195         pushoverThingActions.setThingHandler(mockPushoverAccountHandler);
196         boolean cancelled = PushoverActions.cancelPriorityMessage(pushoverThingActions, RECEIPT);
197         assertThat(cancelled, is(true));
198     }
199
200     @Test
201     public void testCancelPriorityMessageWithInvalidReceipt() {
202         pushoverThingActions.setThingHandler(mockPushoverAccountHandler);
203         boolean cancelled = PushoverActions.cancelPriorityMessage(pushoverThingActions, "invalid");
204         assertThat(cancelled, is(false));
205     }
206 }