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.pushover.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 java.time.Duration;
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;
38 * Unit tests for {@link PushoverActions}.
40 * @author Christoph Weitkamp - Initial contribution
43 @ExtendWith(MockitoExtension.class)
44 @MockitoSettings(strictness = Strictness.LENIENT)
45 public class PushoverActionsTest {
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);
54 private final ThingActions thingActionsStub = new ThingActions() {
56 public void setThingHandler(ThingHandler handler) {
60 public @Nullable ThingHandler getThingHandler() {
65 private @NonNullByDefault({}) @Mock PushoverAccountHandler mockPushoverAccountHandler;
66 private @NonNullByDefault({}) PushoverActions pushoverThingActions;
70 pushoverThingActions = new PushoverActions();
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);
81 public void testSendMessageThingActionsIsNotPushoverThingActions() {
82 assertThrows(ClassCastException.class, () -> PushoverActions.sendMessage(thingActionsStub, MESSAGE, TITLE));
86 public void testSendMessageThingHandlerIsNull() {
87 assertThrows(RuntimeException.class, () -> PushoverActions.sendMessage(pushoverThingActions, MESSAGE, TITLE));
91 public void testSendMessage() {
92 pushoverThingActions.setThingHandler(mockPushoverAccountHandler);
93 boolean sent = PushoverActions.sendMessage(pushoverThingActions, MESSAGE);
94 assertThat(sent, is(true));
98 public void testSendMessageWithTitle() {
99 pushoverThingActions.setThingHandler(mockPushoverAccountHandler);
100 boolean sent = PushoverActions.sendMessage(pushoverThingActions, MESSAGE, TITLE);
101 assertThat(sent, is(true));
105 public void testSendMessageWithTitleAndTTL() {
106 pushoverThingActions.setThingHandler(mockPushoverAccountHandler);
107 boolean sent = PushoverActions.sendMessage(pushoverThingActions, MESSAGE, TITLE, TTL);
108 assertThat(sent, is(true));
113 public void testSendURLMessageThingActionsIsNotPushoverThingActions() {
114 assertThrows(ClassCastException.class,
115 () -> PushoverActions.sendURLMessage(thingActionsStub, MESSAGE, TITLE, URL, URL_TITLE));
119 public void testSendURLMessageThingHandlerIsNull() {
120 assertThrows(RuntimeException.class,
121 () -> PushoverActions.sendURLMessage(pushoverThingActions, MESSAGE, TITLE, URL, URL_TITLE));
125 public void testSendURLMessageWithoutTitle() {
126 pushoverThingActions.setThingHandler(mockPushoverAccountHandler);
127 boolean sent = PushoverActions.sendURLMessage(pushoverThingActions, MESSAGE, null, URL, URL_TITLE);
128 assertThat(sent, is(true));
132 public void testSendURLMessageWithoutURLTitle() {
133 pushoverThingActions.setThingHandler(mockPushoverAccountHandler);
134 boolean sent = PushoverActions.sendURLMessage(pushoverThingActions, MESSAGE, TITLE, URL, null);
135 assertThat(sent, is(true));
139 public void testSendURLMessage() {
140 pushoverThingActions.setThingHandler(mockPushoverAccountHandler);
141 boolean sent = PushoverActions.sendURLMessage(pushoverThingActions, MESSAGE, TITLE, URL, URL_TITLE);
142 assertThat(sent, is(true));
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));
152 // sendPriorityMessage
154 public void testSendPriorityMessageThingActionsIsNotPushoverThingActions() {
155 assertThrows(ClassCastException.class, () -> PushoverActions.sendPriorityMessage(thingActionsStub, MESSAGE,
156 TITLE, PushoverMessageBuilder.EMERGENCY_PRIORITY));
160 public void testSendPriorityMessageThingHandlerIsNull() {
161 assertThrows(RuntimeException.class, () -> PushoverActions.sendPriorityMessage(pushoverThingActions, MESSAGE,
162 TITLE, PushoverMessageBuilder.EMERGENCY_PRIORITY));
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));
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));
181 // cancelPriorityMessage
183 public void testCancelPriorityMessageThingActionsIsNotPushoverThingActions() {
184 assertThrows(ClassCastException.class, () -> PushoverActions.cancelPriorityMessage(thingActionsStub, RECEIPT));
188 public void testCancelPriorityMessageThingHandlerIsNull() {
189 assertThrows(RuntimeException.class,
190 () -> PushoverActions.cancelPriorityMessage(pushoverThingActions, RECEIPT));
194 public void testCancelPriorityMessageWithValidReceipt() {
195 pushoverThingActions.setThingHandler(mockPushoverAccountHandler);
196 boolean cancelled = PushoverActions.cancelPriorityMessage(pushoverThingActions, RECEIPT);
197 assertThat(cancelled, is(true));
201 public void testCancelPriorityMessageWithInvalidReceipt() {
202 pushoverThingActions.setThingHandler(mockPushoverAccountHandler);
203 boolean cancelled = PushoverActions.cancelPriorityMessage(pushoverThingActions, "invalid");
204 assertThat(cancelled, is(false));