2 * Copyright (c) 2010-2021 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 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;
36 * Unit tests for {@link PushoverActions}.
38 * @author Christoph Weitkamp - Initial contribution
40 @ExtendWith(MockitoExtension.class)
41 @MockitoSettings(strictness = Strictness.WARN)
42 public class PushoverActionsTest {
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";
51 private final ThingActions thingActionsStub = new ThingActions() {
53 public void setThingHandler(ThingHandler handler) {
57 public @Nullable ThingHandler getThingHandler() {
62 private @Mock PushoverAccountHandler mockPushoverAccountHandler;
64 private PushoverActions pushoverThingActions;
68 pushoverThingActions = new PushoverActions();
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);
79 public void testSendMessageThingActionsIsNotPushoverThingActions() {
80 assertThrows(ClassCastException.class, () -> PushoverActions.sendMessage(thingActionsStub, MESSAGE, TITLE));
84 public void testSendMessageThingHandlerIsNull() {
85 assertThrows(RuntimeException.class, () -> PushoverActions.sendMessage(pushoverThingActions, MESSAGE, TITLE));
89 public void testSendMessageWithoutTitle() {
90 pushoverThingActions.setThingHandler(mockPushoverAccountHandler);
91 boolean sent = PushoverActions.sendMessage(pushoverThingActions, MESSAGE, null);
92 assertThat(sent, is(true));
96 public void testSendMessage() {
97 pushoverThingActions.setThingHandler(mockPushoverAccountHandler);
98 boolean sent = PushoverActions.sendMessage(pushoverThingActions, MESSAGE, TITLE);
99 assertThat(sent, is(true));
104 public void testSendURLMessageThingActionsIsNotPushoverThingActions() {
105 assertThrows(ClassCastException.class,
106 () -> PushoverActions.sendURLMessage(thingActionsStub, MESSAGE, TITLE, URL, URL_TITLE));
110 public void testSendURLMessageThingHandlerIsNull() {
111 assertThrows(RuntimeException.class,
112 () -> PushoverActions.sendURLMessage(pushoverThingActions, MESSAGE, TITLE, URL, URL_TITLE));
116 public void testSendURLMessageWithoutTitle() {
117 pushoverThingActions.setThingHandler(mockPushoverAccountHandler);
118 boolean sent = PushoverActions.sendURLMessage(pushoverThingActions, MESSAGE, null, URL, URL_TITLE);
119 assertThat(sent, is(true));
123 public void testSendURLMessageWithoutURLTitle() {
124 pushoverThingActions.setThingHandler(mockPushoverAccountHandler);
125 boolean sent = PushoverActions.sendURLMessage(pushoverThingActions, MESSAGE, TITLE, URL, null);
126 assertThat(sent, is(true));
130 public void testSendURLMessage() {
131 pushoverThingActions.setThingHandler(mockPushoverAccountHandler);
132 boolean sent = PushoverActions.sendURLMessage(pushoverThingActions, MESSAGE, TITLE, URL, URL_TITLE);
133 assertThat(sent, is(true));
136 // sendPriorityMessage
138 public void testSendPriorityMessageThingActionsIsNotPushoverThingActions() {
139 assertThrows(ClassCastException.class, () -> PushoverActions.sendPriorityMessage(thingActionsStub, MESSAGE,
140 TITLE, PushoverMessageBuilder.EMERGENCY_PRIORITY));
144 public void testSendPriorityMessageThingHandlerIsNull() {
145 assertThrows(RuntimeException.class, () -> PushoverActions.sendPriorityMessage(pushoverThingActions, MESSAGE,
146 TITLE, PushoverMessageBuilder.EMERGENCY_PRIORITY));
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));
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));
165 // cancelPriorityMessage
167 public void testCancelPriorityMessageThingActionsIsNotPushoverThingActions() {
168 assertThrows(ClassCastException.class, () -> PushoverActions.cancelPriorityMessage(thingActionsStub, RECEIPT));
172 public void testCancelPriorityMessageThingHandlerIsNull() {
173 assertThrows(RuntimeException.class,
174 () -> PushoverActions.cancelPriorityMessage(pushoverThingActions, RECEIPT));
178 public void testCancelPriorityMessageWithValidReceipt() {
179 pushoverThingActions.setThingHandler(mockPushoverAccountHandler);
180 boolean cancelled = PushoverActions.cancelPriorityMessage(pushoverThingActions, RECEIPT);
181 assertThat(cancelled, is(true));
185 public void testCancelPriorityMessageWithInvalidReceipt() {
186 pushoverThingActions.setThingHandler(mockPushoverAccountHandler);
187 boolean cancelled = PushoverActions.cancelPriorityMessage(pushoverThingActions, "invalid");
188 assertThat(cancelled, is(false));