2 * Copyright (c) 2010-2022 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.mielecloud.internal.webservice;
15 import static org.mockito.ArgumentMatchers.any;
16 import static org.mockito.Mockito.*;
18 import java.util.Optional;
19 import java.util.concurrent.ScheduledExecutorService;
20 import java.util.concurrent.ScheduledFuture;
22 import org.eclipse.jdt.annotation.NonNullByDefault;
23 import org.eclipse.jdt.annotation.Nullable;
24 import org.junit.jupiter.api.Test;
25 import org.mockito.ArgumentMatchers;
26 import org.mockito.invocation.InvocationOnMock;
27 import org.mockito.stubbing.Answer;
28 import org.openhab.binding.mielecloud.internal.util.MockUtil;
29 import org.openhab.binding.mielecloud.internal.webservice.api.DeviceState;
30 import org.openhab.binding.mielecloud.internal.webservice.api.json.StateType;
31 import org.openhab.binding.mielecloud.internal.webservice.exception.AuthorizationFailedException;
32 import org.openhab.binding.mielecloud.internal.webservice.exception.MieleWebserviceException;
33 import org.openhab.binding.mielecloud.internal.webservice.exception.TooManyRequestsException;
36 * @author Björn Lange - Initial Contribution
39 public class ActionStateFetcherTest {
40 private ScheduledExecutorService mockImmediatelyExecutingExecutorService() {
41 ScheduledExecutorService scheduler = mock(ScheduledExecutorService.class);
42 when(scheduler.submit(ArgumentMatchers.<Runnable> any()))
43 .thenAnswer(new Answer<@Nullable ScheduledFuture<?>>() {
46 public ScheduledFuture<?> answer(@Nullable InvocationOnMock invocation) throws Throwable {
47 ((Runnable) MockUtil.requireNonNull(invocation).getArgument(0)).run();
55 public void testFetchActionsIsInvokedWhenInitialDeviceStateIsSet() {
57 ScheduledExecutorService scheduler = mockImmediatelyExecutingExecutorService();
59 MieleWebservice webservice = mock(MieleWebservice.class);
60 DeviceState deviceState = mock(DeviceState.class);
61 DeviceState newDeviceState = mock(DeviceState.class);
62 ActionStateFetcher actionsfetcher = new ActionStateFetcher(() -> webservice, scheduler);
64 when(deviceState.getStateType()).thenReturn(Optional.of(StateType.RUNNING));
65 when(newDeviceState.getStateType()).thenReturn(Optional.of(StateType.END_PROGRAMMED));
68 actionsfetcher.onDeviceStateUpdated(deviceState);
71 verify(webservice).fetchActions(any());
75 public void testFetchActionsIsInvokedOnStateTransition() {
77 ScheduledExecutorService scheduler = mockImmediatelyExecutingExecutorService();
79 MieleWebservice webservice = mock(MieleWebservice.class);
80 DeviceState deviceState = mock(DeviceState.class);
81 DeviceState newDeviceState = mock(DeviceState.class);
82 ActionStateFetcher actionsfetcher = new ActionStateFetcher(() -> webservice, scheduler);
84 when(deviceState.getStateType()).thenReturn(Optional.of(StateType.RUNNING));
85 when(newDeviceState.getStateType()).thenReturn(Optional.of(StateType.END_PROGRAMMED));
87 actionsfetcher.onDeviceStateUpdated(deviceState);
90 actionsfetcher.onDeviceStateUpdated(newDeviceState);
93 verify(webservice, times(2)).fetchActions(any());
97 public void testFetchActionsIsNotInvokedWhenNoStateTransitionOccurrs() {
99 ScheduledExecutorService scheduler = mockImmediatelyExecutingExecutorService();
101 MieleWebservice webservice = mock(MieleWebservice.class);
102 DeviceState deviceState = mock(DeviceState.class);
103 DeviceState newDeviceState = mock(DeviceState.class);
104 ActionStateFetcher actionsfetcher = new ActionStateFetcher(() -> webservice, scheduler);
106 when(deviceState.getStateType()).thenReturn(Optional.of(StateType.RUNNING));
107 when(newDeviceState.getStateType()).thenReturn(Optional.of(StateType.RUNNING));
109 actionsfetcher.onDeviceStateUpdated(deviceState);
112 actionsfetcher.onDeviceStateUpdated(newDeviceState);
115 verify(webservice, times(1)).fetchActions(any());
119 public void whenFetchActionsFailsWithAMieleWebserviceExceptionThenNoExceptionIsThrown() {
121 ScheduledExecutorService scheduler = mockImmediatelyExecutingExecutorService();
123 MieleWebservice webservice = mock(MieleWebservice.class);
124 doThrow(new MieleWebserviceException("It went wrong", ConnectionError.REQUEST_EXECUTION_FAILED))
125 .when(webservice).fetchActions(any());
127 DeviceState deviceState = mock(DeviceState.class);
128 when(deviceState.getStateType()).thenReturn(Optional.of(StateType.RUNNING));
130 ActionStateFetcher actionsfetcher = new ActionStateFetcher(() -> webservice, scheduler);
133 actionsfetcher.onDeviceStateUpdated(deviceState);
136 verify(webservice, times(1)).fetchActions(any());
140 public void whenFetchActionsFailsWithAnAuthorizationFailedExceptionThenNoExceptionIsThrown() {
142 ScheduledExecutorService scheduler = mockImmediatelyExecutingExecutorService();
144 MieleWebservice webservice = mock(MieleWebservice.class);
145 doThrow(new AuthorizationFailedException("Authorization failed")).when(webservice).fetchActions(any());
147 DeviceState deviceState = mock(DeviceState.class);
148 when(deviceState.getStateType()).thenReturn(Optional.of(StateType.RUNNING));
150 ActionStateFetcher actionsfetcher = new ActionStateFetcher(() -> webservice, scheduler);
153 actionsfetcher.onDeviceStateUpdated(deviceState);
156 verify(webservice, times(1)).fetchActions(any());
160 public void whenFetchActionsFailsWithATooManyRequestsExceptionThenNoExceptionIsThrown() {
162 ScheduledExecutorService scheduler = mockImmediatelyExecutingExecutorService();
164 MieleWebservice webservice = mock(MieleWebservice.class);
165 doThrow(new TooManyRequestsException("Too many requests", null)).when(webservice).fetchActions(any());
167 DeviceState deviceState = mock(DeviceState.class);
168 when(deviceState.getStateType()).thenReturn(Optional.of(StateType.RUNNING));
170 ActionStateFetcher actionsfetcher = new ActionStateFetcher(() -> webservice, scheduler);
173 actionsfetcher.onDeviceStateUpdated(deviceState);
176 verify(webservice, times(1)).fetchActions(any());