]> git.basschouten.com Git - openhab-addons.git/blob
cf71cb50a2429a1454513e2e0718daf6562d26f7
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 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.mielecloud.internal.webservice;
14
15 import static org.mockito.ArgumentMatchers.any;
16 import static org.mockito.Mockito.*;
17
18 import java.util.Optional;
19 import java.util.concurrent.ScheduledExecutorService;
20 import java.util.concurrent.ScheduledFuture;
21
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;
34
35 /**
36  * @author Björn Lange - Initial Contribution
37  */
38 @NonNullByDefault
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<?>>() {
44                     @Override
45                     @Nullable
46                     public ScheduledFuture<?> answer(@Nullable InvocationOnMock invocation) throws Throwable {
47                         ((Runnable) MockUtil.requireNonNull(invocation).getArgument(0)).run();
48                         return null;
49                     }
50                 });
51         return scheduler;
52     }
53
54     @Test
55     public void testFetchActionsIsInvokedWhenInitialDeviceStateIsSet() {
56         // given:
57         ScheduledExecutorService scheduler = mockImmediatelyExecutingExecutorService();
58
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);
63
64         when(deviceState.getStateType()).thenReturn(Optional.of(StateType.RUNNING));
65         when(newDeviceState.getStateType()).thenReturn(Optional.of(StateType.END_PROGRAMMED));
66
67         // when:
68         actionsfetcher.onDeviceStateUpdated(deviceState);
69
70         // then:
71         verify(webservice).fetchActions(any());
72     }
73
74     @Test
75     public void testFetchActionsIsInvokedOnStateTransition() {
76         // given:
77         ScheduledExecutorService scheduler = mockImmediatelyExecutingExecutorService();
78
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);
83
84         when(deviceState.getStateType()).thenReturn(Optional.of(StateType.RUNNING));
85         when(newDeviceState.getStateType()).thenReturn(Optional.of(StateType.END_PROGRAMMED));
86
87         actionsfetcher.onDeviceStateUpdated(deviceState);
88
89         // when:
90         actionsfetcher.onDeviceStateUpdated(newDeviceState);
91
92         // then:
93         verify(webservice, times(2)).fetchActions(any());
94     }
95
96     @Test
97     public void testFetchActionsIsNotInvokedWhenNoStateTransitionOccurrs() {
98         // given:
99         ScheduledExecutorService scheduler = mockImmediatelyExecutingExecutorService();
100
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);
105
106         when(deviceState.getStateType()).thenReturn(Optional.of(StateType.RUNNING));
107         when(newDeviceState.getStateType()).thenReturn(Optional.of(StateType.RUNNING));
108
109         actionsfetcher.onDeviceStateUpdated(deviceState);
110
111         // when:
112         actionsfetcher.onDeviceStateUpdated(newDeviceState);
113
114         // then:
115         verify(webservice, times(1)).fetchActions(any());
116     }
117
118     @Test
119     public void whenFetchActionsFailsWithAMieleWebserviceExceptionThenNoExceptionIsThrown() {
120         // given:
121         ScheduledExecutorService scheduler = mockImmediatelyExecutingExecutorService();
122
123         MieleWebservice webservice = mock(MieleWebservice.class);
124         doThrow(new MieleWebserviceException("It went wrong", ConnectionError.REQUEST_EXECUTION_FAILED))
125                 .when(webservice).fetchActions(any());
126
127         DeviceState deviceState = mock(DeviceState.class);
128         when(deviceState.getStateType()).thenReturn(Optional.of(StateType.RUNNING));
129
130         ActionStateFetcher actionsfetcher = new ActionStateFetcher(() -> webservice, scheduler);
131
132         // when:
133         actionsfetcher.onDeviceStateUpdated(deviceState);
134
135         // then:
136         verify(webservice, times(1)).fetchActions(any());
137     }
138
139     @Test
140     public void whenFetchActionsFailsWithAnAuthorizationFailedExceptionThenNoExceptionIsThrown() {
141         // given:
142         ScheduledExecutorService scheduler = mockImmediatelyExecutingExecutorService();
143
144         MieleWebservice webservice = mock(MieleWebservice.class);
145         doThrow(new AuthorizationFailedException("Authorization failed")).when(webservice).fetchActions(any());
146
147         DeviceState deviceState = mock(DeviceState.class);
148         when(deviceState.getStateType()).thenReturn(Optional.of(StateType.RUNNING));
149
150         ActionStateFetcher actionsfetcher = new ActionStateFetcher(() -> webservice, scheduler);
151
152         // when:
153         actionsfetcher.onDeviceStateUpdated(deviceState);
154
155         // then:
156         verify(webservice, times(1)).fetchActions(any());
157     }
158
159     @Test
160     public void whenFetchActionsFailsWithATooManyRequestsExceptionThenNoExceptionIsThrown() {
161         // given:
162         ScheduledExecutorService scheduler = mockImmediatelyExecutingExecutorService();
163
164         MieleWebservice webservice = mock(MieleWebservice.class);
165         doThrow(new TooManyRequestsException("Too many requests", null)).when(webservice).fetchActions(any());
166
167         DeviceState deviceState = mock(DeviceState.class);
168         when(deviceState.getStateType()).thenReturn(Optional.of(StateType.RUNNING));
169
170         ActionStateFetcher actionsfetcher = new ActionStateFetcher(() -> webservice, scheduler);
171
172         // when:
173         actionsfetcher.onDeviceStateUpdated(deviceState);
174
175         // then:
176         verify(webservice, times(1)).fetchActions(any());
177     }
178 }