]> git.basschouten.com Git - openhab-addons.git/blob
3edc7684e048d283142a49a34a998134e7146394
[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 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.pushover.internal.connection.PushoverMessageBuilder;
31 import org.openhab.binding.pushover.internal.handler.PushoverAccountHandler;
32 import org.openhab.core.thing.binding.ThingActions;
33 import org.openhab.core.thing.binding.ThingHandler;
34
35 /**
36  * Unit tests for {@link PushoverActions}.
37  *
38  * @author Christoph Weitkamp - Initial contribution
39  */
40 @NonNullByDefault
41 @ExtendWith(MockitoExtension.class)
42 @MockitoSettings(strictness = Strictness.LENIENT)
43 public class PushoverActionsTest {
44
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";
50
51     private final ThingActions thingActionsStub = new ThingActions() {
52         @Override
53         public void setThingHandler(ThingHandler handler) {
54         }
55
56         @Override
57         public @Nullable ThingHandler getThingHandler() {
58             return null;
59         }
60     };
61
62     private @NonNullByDefault({}) @Mock PushoverAccountHandler mockPushoverAccountHandler;
63     private @NonNullByDefault({}) PushoverActions pushoverThingActions;
64
65     @BeforeEach
66     public void setUp() {
67         pushoverThingActions = new PushoverActions();
68
69         when(mockPushoverAccountHandler.getDefaultPushoverMessageBuilder(any()))
70                 .thenReturn(PushoverMessageBuilder.getInstance("key", "user"));
71         when(mockPushoverAccountHandler.sendMessage(any())).thenReturn(Boolean.TRUE);
72         when(mockPushoverAccountHandler.sendPriorityMessage(any())).thenReturn(RECEIPT);
73         when(mockPushoverAccountHandler.cancelPriorityMessage(RECEIPT)).thenReturn(Boolean.TRUE);
74     }
75
76     // sendMessage
77     @Test
78     public void testSendMessageThingActionsIsNotPushoverThingActions() {
79         assertThrows(ClassCastException.class, () -> PushoverActions.sendMessage(thingActionsStub, MESSAGE, TITLE));
80     }
81
82     @Test
83     public void testSendMessageThingHandlerIsNull() {
84         assertThrows(RuntimeException.class, () -> PushoverActions.sendMessage(pushoverThingActions, MESSAGE, TITLE));
85     }
86
87     @Test
88     public void testSendMessageWithoutTitle() {
89         pushoverThingActions.setThingHandler(mockPushoverAccountHandler);
90         boolean sent = PushoverActions.sendMessage(pushoverThingActions, MESSAGE, null);
91         assertThat(sent, is(true));
92     }
93
94     @Test
95     public void testSendMessage() {
96         pushoverThingActions.setThingHandler(mockPushoverAccountHandler);
97         boolean sent = PushoverActions.sendMessage(pushoverThingActions, MESSAGE, TITLE);
98         assertThat(sent, is(true));
99     }
100
101     // sendURLMessage
102     @Test
103     public void testSendURLMessageThingActionsIsNotPushoverThingActions() {
104         assertThrows(ClassCastException.class,
105                 () -> PushoverActions.sendURLMessage(thingActionsStub, MESSAGE, TITLE, URL, URL_TITLE));
106     }
107
108     @Test
109     public void testSendURLMessageThingHandlerIsNull() {
110         assertThrows(RuntimeException.class,
111                 () -> PushoverActions.sendURLMessage(pushoverThingActions, MESSAGE, TITLE, URL, URL_TITLE));
112     }
113
114     @Test
115     public void testSendURLMessageWithoutTitle() {
116         pushoverThingActions.setThingHandler(mockPushoverAccountHandler);
117         boolean sent = PushoverActions.sendURLMessage(pushoverThingActions, MESSAGE, null, URL, URL_TITLE);
118         assertThat(sent, is(true));
119     }
120
121     @Test
122     public void testSendURLMessageWithoutURLTitle() {
123         pushoverThingActions.setThingHandler(mockPushoverAccountHandler);
124         boolean sent = PushoverActions.sendURLMessage(pushoverThingActions, MESSAGE, TITLE, URL, null);
125         assertThat(sent, is(true));
126     }
127
128     @Test
129     public void testSendURLMessage() {
130         pushoverThingActions.setThingHandler(mockPushoverAccountHandler);
131         boolean sent = PushoverActions.sendURLMessage(pushoverThingActions, MESSAGE, TITLE, URL, URL_TITLE);
132         assertThat(sent, is(true));
133     }
134
135     // sendPriorityMessage
136     @Test
137     public void testSendPriorityMessageThingActionsIsNotPushoverThingActions() {
138         assertThrows(ClassCastException.class, () -> PushoverActions.sendPriorityMessage(thingActionsStub, MESSAGE,
139                 TITLE, PushoverMessageBuilder.EMERGENCY_PRIORITY));
140     }
141
142     @Test
143     public void testSendPriorityMessageThingHandlerIsNull() {
144         assertThrows(RuntimeException.class, () -> PushoverActions.sendPriorityMessage(pushoverThingActions, MESSAGE,
145                 TITLE, PushoverMessageBuilder.EMERGENCY_PRIORITY));
146     }
147
148     @Test
149     public void testSendPriorityMessageWithoutTitle() {
150         pushoverThingActions.setThingHandler(mockPushoverAccountHandler);
151         String receipt = PushoverActions.sendPriorityMessage(pushoverThingActions, MESSAGE, null,
152                 PushoverMessageBuilder.EMERGENCY_PRIORITY);
153         assertThat(receipt, is(RECEIPT));
154     }
155
156     @Test
157     public void testSendPriorityMessage() {
158         pushoverThingActions.setThingHandler(mockPushoverAccountHandler);
159         String receipt = PushoverActions.sendPriorityMessage(pushoverThingActions, MESSAGE, TITLE,
160                 PushoverMessageBuilder.EMERGENCY_PRIORITY);
161         assertThat(receipt, is(RECEIPT));
162     }
163
164     // cancelPriorityMessage
165     @Test
166     public void testCancelPriorityMessageThingActionsIsNotPushoverThingActions() {
167         assertThrows(ClassCastException.class, () -> PushoverActions.cancelPriorityMessage(thingActionsStub, RECEIPT));
168     }
169
170     @Test
171     public void testCancelPriorityMessageThingHandlerIsNull() {
172         assertThrows(RuntimeException.class,
173                 () -> PushoverActions.cancelPriorityMessage(pushoverThingActions, RECEIPT));
174     }
175
176     @Test
177     public void testCancelPriorityMessageWithValidReceipt() {
178         pushoverThingActions.setThingHandler(mockPushoverAccountHandler);
179         boolean cancelled = PushoverActions.cancelPriorityMessage(pushoverThingActions, RECEIPT);
180         assertThat(cancelled, is(true));
181     }
182
183     @Test
184     public void testCancelPriorityMessageWithInvalidReceipt() {
185         pushoverThingActions.setThingHandler(mockPushoverAccountHandler);
186         boolean cancelled = PushoverActions.cancelPriorityMessage(pushoverThingActions, "invalid");
187         assertThat(cancelled, is(false));
188     }
189 }