]> git.basschouten.com Git - openhab-addons.git/blob
5e1c81111e350d40449f35a186455d0fec233698
[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.pushsafer.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.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;
35
36 /**
37  * Unit tests for {@link PushsaferActions}.
38  *
39  * @author Kevin Siml - Initial contribution, forked from Christoph Weitkamp
40  */
41 @ExtendWith(MockitoExtension.class)
42 @MockitoSettings(strictness = Strictness.LENIENT)
43 public class PushsaferActionsTest {
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     @NonNullByDefault
52     private final ThingActions thingActionsStub = new ThingActions() {
53         @Override
54         public void setThingHandler(ThingHandler handler) {
55         }
56
57         @Override
58         public @Nullable ThingHandler getThingHandler() {
59             return null;
60         }
61     };
62
63     private @Mock PushsaferAccountHandler mockPushsaferAccountHandler;
64
65     private PushsaferActions pushsaferThingActions;
66
67     @BeforeEach
68     public void setUp() throws PushsaferConfigurationException {
69         pushsaferThingActions = new PushsaferActions();
70
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);
75     }
76
77     // sendPushsaferMessage
78     @Test
79     public void testSendMessageThingActionsIsNotPushsaferThingActions() {
80         assertThrows(ClassCastException.class,
81                 () -> PushsaferActions.sendPushsaferMessage(thingActionsStub, MESSAGE, TITLE));
82     }
83
84     @Test
85     public void testSendMessageThingHandlerIsNull() {
86         assertThrows(RuntimeException.class,
87                 () -> PushsaferActions.sendPushsaferMessage(pushsaferThingActions, MESSAGE, TITLE));
88     }
89
90     @Test
91     public void testSendMessageWithoutTitle() {
92         pushsaferThingActions.setThingHandler(mockPushsaferAccountHandler);
93         boolean sent = PushsaferActions.sendPushsaferMessage(pushsaferThingActions, MESSAGE, null);
94         assertThat(sent, is(true));
95     }
96
97     @Test
98     public void testSendMessage() {
99         pushsaferThingActions.setThingHandler(mockPushsaferAccountHandler);
100         boolean sent = PushsaferActions.sendPushsaferMessage(pushsaferThingActions, MESSAGE, TITLE);
101         assertThat(sent, is(true));
102     }
103
104     // sendPushsaferURLMessage
105     @Test
106     public void testSendURLMessageThingActionsIsNotPushsaferThingActions() {
107         assertThrows(ClassCastException.class,
108                 () -> PushsaferActions.sendPushsaferURLMessage(thingActionsStub, MESSAGE, TITLE, URL, URL_TITLE));
109     }
110
111     @Test
112     public void testSendURLMessageThingHandlerIsNull() {
113         assertThrows(RuntimeException.class,
114                 () -> PushsaferActions.sendPushsaferURLMessage(pushsaferThingActions, MESSAGE, TITLE, URL, URL_TITLE));
115     }
116
117     @Test
118     public void testSendURLMessageWithoutTitle() {
119         pushsaferThingActions.setThingHandler(mockPushsaferAccountHandler);
120         boolean sent = PushsaferActions.sendPushsaferURLMessage(pushsaferThingActions, MESSAGE, null, URL, URL_TITLE);
121         assertThat(sent, is(true));
122     }
123
124     @Test
125     public void testSendURLMessageWithoutURLTitle() {
126         pushsaferThingActions.setThingHandler(mockPushsaferAccountHandler);
127         boolean sent = PushsaferActions.sendPushsaferURLMessage(pushsaferThingActions, MESSAGE, TITLE, URL, null);
128         assertThat(sent, is(true));
129     }
130
131     @Test
132     public void testSendURLMessage() {
133         pushsaferThingActions.setThingHandler(mockPushsaferAccountHandler);
134         boolean sent = PushsaferActions.sendPushsaferURLMessage(pushsaferThingActions, MESSAGE, TITLE, URL, URL_TITLE);
135         assertThat(sent, is(true));
136     }
137
138     // sendPushsaferPriorityMessage
139     @Test
140     public void testSendPriorityMessageThingActionsIsNotPushsaferThingActions() {
141         assertThrows(ClassCastException.class, () -> PushsaferActions.sendPushsaferPriorityMessage(thingActionsStub,
142                 MESSAGE, TITLE, PushsaferMessageBuilder.EMERGENCY_PRIORITY));
143     }
144
145     @Test
146     public void testSendPriorityMessageThingHandlerIsNull() {
147         assertThrows(RuntimeException.class, () -> PushsaferActions.sendPushsaferPriorityMessage(pushsaferThingActions,
148                 MESSAGE, TITLE, PushsaferMessageBuilder.EMERGENCY_PRIORITY));
149     }
150
151     @Test
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));
157     }
158
159     @Test
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));
165     }
166 }