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 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
41 @ExtendWith(MockitoExtension.class)
42 @MockitoSettings(strictness = Strictness.LENIENT)
43 public class PushoverActionsTest {
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";
51 private final ThingActions thingActionsStub = new ThingActions() {
53 public void setThingHandler(ThingHandler handler) {
57 public @Nullable ThingHandler getThingHandler() {
62 private @NonNullByDefault({}) @Mock PushoverAccountHandler mockPushoverAccountHandler;
63 private @NonNullByDefault({}) PushoverActions pushoverThingActions;
67 pushoverThingActions = new PushoverActions();
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);
78 public void testSendMessageThingActionsIsNotPushoverThingActions() {
79 assertThrows(ClassCastException.class, () -> PushoverActions.sendMessage(thingActionsStub, MESSAGE, TITLE));
83 public void testSendMessageThingHandlerIsNull() {
84 assertThrows(RuntimeException.class, () -> PushoverActions.sendMessage(pushoverThingActions, MESSAGE, TITLE));
88 public void testSendMessageWithoutTitle() {
89 pushoverThingActions.setThingHandler(mockPushoverAccountHandler);
90 boolean sent = PushoverActions.sendMessage(pushoverThingActions, MESSAGE, null);
91 assertThat(sent, is(true));
95 public void testSendMessage() {
96 pushoverThingActions.setThingHandler(mockPushoverAccountHandler);
97 boolean sent = PushoverActions.sendMessage(pushoverThingActions, MESSAGE, TITLE);
98 assertThat(sent, is(true));
103 public void testSendURLMessageThingActionsIsNotPushoverThingActions() {
104 assertThrows(ClassCastException.class,
105 () -> PushoverActions.sendURLMessage(thingActionsStub, MESSAGE, TITLE, URL, URL_TITLE));
109 public void testSendURLMessageThingHandlerIsNull() {
110 assertThrows(RuntimeException.class,
111 () -> PushoverActions.sendURLMessage(pushoverThingActions, MESSAGE, TITLE, URL, URL_TITLE));
115 public void testSendURLMessageWithoutTitle() {
116 pushoverThingActions.setThingHandler(mockPushoverAccountHandler);
117 boolean sent = PushoverActions.sendURLMessage(pushoverThingActions, MESSAGE, null, URL, URL_TITLE);
118 assertThat(sent, is(true));
122 public void testSendURLMessageWithoutURLTitle() {
123 pushoverThingActions.setThingHandler(mockPushoverAccountHandler);
124 boolean sent = PushoverActions.sendURLMessage(pushoverThingActions, MESSAGE, TITLE, URL, null);
125 assertThat(sent, is(true));
129 public void testSendURLMessage() {
130 pushoverThingActions.setThingHandler(mockPushoverAccountHandler);
131 boolean sent = PushoverActions.sendURLMessage(pushoverThingActions, MESSAGE, TITLE, URL, URL_TITLE);
132 assertThat(sent, is(true));
135 // sendPriorityMessage
137 public void testSendPriorityMessageThingActionsIsNotPushoverThingActions() {
138 assertThrows(ClassCastException.class, () -> PushoverActions.sendPriorityMessage(thingActionsStub, MESSAGE,
139 TITLE, PushoverMessageBuilder.EMERGENCY_PRIORITY));
143 public void testSendPriorityMessageThingHandlerIsNull() {
144 assertThrows(RuntimeException.class, () -> PushoverActions.sendPriorityMessage(pushoverThingActions, MESSAGE,
145 TITLE, PushoverMessageBuilder.EMERGENCY_PRIORITY));
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));
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));
164 // cancelPriorityMessage
166 public void testCancelPriorityMessageThingActionsIsNotPushoverThingActions() {
167 assertThrows(ClassCastException.class, () -> PushoverActions.cancelPriorityMessage(thingActionsStub, RECEIPT));
171 public void testCancelPriorityMessageThingHandlerIsNull() {
172 assertThrows(RuntimeException.class,
173 () -> PushoverActions.cancelPriorityMessage(pushoverThingActions, RECEIPT));
177 public void testCancelPriorityMessageWithValidReceipt() {
178 pushoverThingActions.setThingHandler(mockPushoverAccountHandler);
179 boolean cancelled = PushoverActions.cancelPriorityMessage(pushoverThingActions, RECEIPT);
180 assertThat(cancelled, is(true));
184 public void testCancelPriorityMessageWithInvalidReceipt() {
185 pushoverThingActions.setThingHandler(mockPushoverAccountHandler);
186 boolean cancelled = PushoverActions.cancelPriorityMessage(pushoverThingActions, "invalid");
187 assertThat(cancelled, is(false));