]> git.basschouten.com Git - openhab-addons.git/blob
1eba0fabc922916569e7e77dac472703e1135e82
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 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 @ExtendWith(MockitoExtension.class)
41 @MockitoSettings(strictness = Strictness.WARN)
42 public class PushoverActionsTest {
43
44     private static final String MESSAGE = "My Message";
45     private static final String TITLE = "My Title";
46     private static final String URL = "https://www.test.com";
47     private static final String URL_TITLE = "Some Link";
48     private static final String RECEIPT = "12345";
49
50     @NonNullByDefault
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 @Mock PushoverAccountHandler mockPushoverAccountHandler;
63
64     private PushoverActions pushoverThingActions;
65
66     @BeforeEach
67     public void setUp() {
68         pushoverThingActions = new PushoverActions();
69
70         when(mockPushoverAccountHandler.getDefaultPushoverMessageBuilder(any()))
71                 .thenReturn(PushoverMessageBuilder.getInstance("key", "user"));
72         when(mockPushoverAccountHandler.sendMessage(any())).thenReturn(Boolean.TRUE);
73         when(mockPushoverAccountHandler.sendPriorityMessage(any())).thenReturn(RECEIPT);
74         when(mockPushoverAccountHandler.cancelPriorityMessage(RECEIPT)).thenReturn(Boolean.TRUE);
75     }
76
77     // sendMessage
78     @Test
79     public void testSendMessageThingActionsIsNotPushoverThingActions() {
80         assertThrows(ClassCastException.class, () -> PushoverActions.sendMessage(thingActionsStub, MESSAGE, TITLE));
81     }
82
83     @Test
84     public void testSendMessageThingHandlerIsNull() {
85         assertThrows(RuntimeException.class, () -> PushoverActions.sendMessage(pushoverThingActions, MESSAGE, TITLE));
86     }
87
88     @Test
89     public void testSendMessageWithoutTitle() {
90         pushoverThingActions.setThingHandler(mockPushoverAccountHandler);
91         boolean sent = PushoverActions.sendMessage(pushoverThingActions, MESSAGE, null);
92         assertThat(sent, is(true));
93     }
94
95     @Test
96     public void testSendMessage() {
97         pushoverThingActions.setThingHandler(mockPushoverAccountHandler);
98         boolean sent = PushoverActions.sendMessage(pushoverThingActions, MESSAGE, TITLE);
99         assertThat(sent, is(true));
100     }
101
102     // sendURLMessage
103     @Test
104     public void testSendURLMessageThingActionsIsNotPushoverThingActions() {
105         assertThrows(ClassCastException.class,
106                 () -> PushoverActions.sendURLMessage(thingActionsStub, MESSAGE, TITLE, URL, URL_TITLE));
107     }
108
109     @Test
110     public void testSendURLMessageThingHandlerIsNull() {
111         assertThrows(RuntimeException.class,
112                 () -> PushoverActions.sendURLMessage(pushoverThingActions, MESSAGE, TITLE, URL, URL_TITLE));
113     }
114
115     @Test
116     public void testSendURLMessageWithoutTitle() {
117         pushoverThingActions.setThingHandler(mockPushoverAccountHandler);
118         boolean sent = PushoverActions.sendURLMessage(pushoverThingActions, MESSAGE, null, URL, URL_TITLE);
119         assertThat(sent, is(true));
120     }
121
122     @Test
123     public void testSendURLMessageWithoutURLTitle() {
124         pushoverThingActions.setThingHandler(mockPushoverAccountHandler);
125         boolean sent = PushoverActions.sendURLMessage(pushoverThingActions, MESSAGE, TITLE, URL, null);
126         assertThat(sent, is(true));
127     }
128
129     @Test
130     public void testSendURLMessage() {
131         pushoverThingActions.setThingHandler(mockPushoverAccountHandler);
132         boolean sent = PushoverActions.sendURLMessage(pushoverThingActions, MESSAGE, TITLE, URL, URL_TITLE);
133         assertThat(sent, is(true));
134     }
135
136     // sendPriorityMessage
137     @Test
138     public void testSendPriorityMessageThingActionsIsNotPushoverThingActions() {
139         assertThrows(ClassCastException.class, () -> PushoverActions.sendPriorityMessage(thingActionsStub, MESSAGE,
140                 TITLE, PushoverMessageBuilder.EMERGENCY_PRIORITY));
141     }
142
143     @Test
144     public void testSendPriorityMessageThingHandlerIsNull() {
145         assertThrows(RuntimeException.class, () -> PushoverActions.sendPriorityMessage(pushoverThingActions, MESSAGE,
146                 TITLE, PushoverMessageBuilder.EMERGENCY_PRIORITY));
147     }
148
149     @Test
150     public void testSendPriorityMessageWithoutTitle() {
151         pushoverThingActions.setThingHandler(mockPushoverAccountHandler);
152         String receipt = PushoverActions.sendPriorityMessage(pushoverThingActions, MESSAGE, null,
153                 PushoverMessageBuilder.EMERGENCY_PRIORITY);
154         assertThat(receipt, is(RECEIPT));
155     }
156
157     @Test
158     public void testSendPriorityMessage() {
159         pushoverThingActions.setThingHandler(mockPushoverAccountHandler);
160         String receipt = PushoverActions.sendPriorityMessage(pushoverThingActions, MESSAGE, TITLE,
161                 PushoverMessageBuilder.EMERGENCY_PRIORITY);
162         assertThat(receipt, is(RECEIPT));
163     }
164
165     // cancelPriorityMessage
166     @Test
167     public void testCancelPriorityMessageThingActionsIsNotPushoverThingActions() {
168         assertThrows(ClassCastException.class, () -> PushoverActions.cancelPriorityMessage(thingActionsStub, RECEIPT));
169     }
170
171     @Test
172     public void testCancelPriorityMessageThingHandlerIsNull() {
173         assertThrows(RuntimeException.class,
174                 () -> PushoverActions.cancelPriorityMessage(pushoverThingActions, RECEIPT));
175     }
176
177     @Test
178     public void testCancelPriorityMessageWithValidReceipt() {
179         pushoverThingActions.setThingHandler(mockPushoverAccountHandler);
180         boolean cancelled = PushoverActions.cancelPriorityMessage(pushoverThingActions, RECEIPT);
181         assertThat(cancelled, is(true));
182     }
183
184     @Test
185     public void testCancelPriorityMessageWithInvalidReceipt() {
186         pushoverThingActions.setThingHandler(mockPushoverAccountHandler);
187         boolean cancelled = PushoverActions.cancelPriorityMessage(pushoverThingActions, "invalid");
188         assertThat(cancelled, is(false));
189     }
190 }