]> git.basschouten.com Git - openhab-addons.git/blob
630d75826bdbada26fb60ba4618ac188cb814ba8
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2024 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.junit.jupiter.api.Assertions.assertNotNull;
16 import static org.mockito.ArgumentMatchers.any;
17 import static org.mockito.Mockito.*;
18 import static org.openhab.binding.mielecloud.internal.util.MockUtil.mockDevice;
19
20 import java.util.Arrays;
21 import java.util.HashSet;
22 import java.util.Objects;
23 import java.util.concurrent.ExecutionException;
24 import java.util.concurrent.TimeoutException;
25
26 import org.eclipse.jdt.annotation.NonNullByDefault;
27 import org.eclipse.jdt.annotation.Nullable;
28 import org.junit.jupiter.api.BeforeEach;
29 import org.junit.jupiter.api.Test;
30 import org.openhab.binding.mielecloud.internal.webservice.api.ActionsState;
31 import org.openhab.binding.mielecloud.internal.webservice.api.DeviceState;
32 import org.openhab.binding.mielecloud.internal.webservice.api.json.Actions;
33 import org.openhab.binding.mielecloud.internal.webservice.api.json.Device;
34 import org.openhab.binding.mielecloud.internal.webservice.api.json.DeviceCollection;
35
36 /**
37  * @author Björn Lange - Initial contribution
38  */
39 @NonNullByDefault
40 public class DeviceStateDispatcherTest {
41     private static final String FIRST_DEVICE_IDENTIFIER = "000124430016";
42     private static final String SECOND_DEVICE_IDENTIFIER = "000124430017";
43     private static final String UNKNOWN_DEVICE_IDENTIFIER = "100124430016";
44
45     @Nullable
46     private Device firstDevice;
47     @Nullable
48     private Device secondDevice;
49     @Nullable
50     private DeviceCollection devices;
51
52     private Device getFirstDevice() {
53         assertNotNull(firstDevice);
54         return Objects.requireNonNull(firstDevice);
55     }
56
57     private Device getSecondDevice() {
58         assertNotNull(secondDevice);
59         return Objects.requireNonNull(secondDevice);
60     }
61
62     private DeviceCollection getDevices() {
63         assertNotNull(devices);
64         return Objects.requireNonNull(devices);
65     }
66
67     @BeforeEach
68     public void setUp() {
69         firstDevice = mockDevice(FIRST_DEVICE_IDENTIFIER);
70         secondDevice = mockDevice(SECOND_DEVICE_IDENTIFIER);
71
72         devices = mock(DeviceCollection.class);
73         when(getDevices().getDeviceIdentifiers())
74                 .thenReturn(new HashSet<String>(Arrays.asList(FIRST_DEVICE_IDENTIFIER, SECOND_DEVICE_IDENTIFIER)));
75         when(getDevices().getDevice(FIRST_DEVICE_IDENTIFIER)).thenReturn(getFirstDevice());
76         when(getDevices().getDevice(SECOND_DEVICE_IDENTIFIER)).thenReturn(getSecondDevice());
77     }
78
79     @Test
80     public void testAddListenerDispatchesStateUpdatesToPassedListenerForCachedDevices()
81             throws InterruptedException, TimeoutException, ExecutionException {
82         // given:
83         DeviceStateListener listener = mock(DeviceStateListener.class);
84
85         DeviceStateDispatcher dispatcher = new DeviceStateDispatcher();
86         dispatcher.dispatchDeviceStateUpdates(getDevices());
87
88         // when:
89         dispatcher.addListener(listener);
90
91         // then:
92         verify(listener).onDeviceStateUpdated(new DeviceState(FIRST_DEVICE_IDENTIFIER, firstDevice));
93         verify(listener).onDeviceStateUpdated(new DeviceState(SECOND_DEVICE_IDENTIFIER, secondDevice));
94         verifyNoMoreInteractions(listener);
95     }
96
97     @Test
98     public void testDeviceStateUpdatesAreNotDispatchedToRemovedListeners() {
99         // given:
100         DeviceStateListener listener = mock(DeviceStateListener.class);
101
102         DeviceStateDispatcher dispatcher = new DeviceStateDispatcher();
103         dispatcher.addListener(listener);
104
105         // when:
106         dispatcher.removeListener(listener);
107         dispatcher.dispatchDeviceStateUpdates(getDevices());
108
109         // then:
110         verifyNoMoreInteractions(listener);
111     }
112
113     @Test
114     public void testClearCachePreventsDeviceStateUpdateDispatchingOnListenerRegistration() {
115         // given:
116         DeviceStateListener listener = mock(DeviceStateListener.class);
117
118         DeviceStateDispatcher dispatcher = new DeviceStateDispatcher();
119         dispatcher.dispatchDeviceStateUpdates(getDevices());
120
121         // when:
122         dispatcher.clearCache();
123         dispatcher.addListener(listener);
124
125         // then:
126         verifyNoMoreInteractions(listener);
127     }
128
129     @Test
130     public void testDeviceStateUpdatesAreDispatchedToSubscribedListeners() {
131         // given:
132         DeviceStateListener listener = mock(DeviceStateListener.class);
133
134         DeviceStateDispatcher dispatcher = new DeviceStateDispatcher();
135         dispatcher.addListener(listener);
136
137         // when:
138         dispatcher.dispatchDeviceStateUpdates(getDevices());
139
140         // then:
141         verify(listener).onDeviceStateUpdated(new DeviceState(FIRST_DEVICE_IDENTIFIER, firstDevice));
142         verify(listener).onDeviceStateUpdated(new DeviceState(SECOND_DEVICE_IDENTIFIER, secondDevice));
143         verifyNoMoreInteractions(listener);
144     }
145
146     @Test
147     public void testRemovalEventsAreDispatchedToSubscribedListeners()
148             throws InterruptedException, TimeoutException, ExecutionException {
149         // given:
150         DeviceStateListener listener = mock(DeviceStateListener.class);
151
152         Device deviceWithUnknownIdentifier = mockDevice(UNKNOWN_DEVICE_IDENTIFIER);
153         DeviceCollection devicesWithUnknownDevice = mock(DeviceCollection.class);
154         when(devicesWithUnknownDevice.getDeviceIdentifiers())
155                 .thenReturn(new HashSet<String>(Arrays.asList(UNKNOWN_DEVICE_IDENTIFIER)));
156         when(devicesWithUnknownDevice.getDevice(UNKNOWN_DEVICE_IDENTIFIER)).thenReturn(deviceWithUnknownIdentifier);
157
158         DeviceStateDispatcher dispatcher = new DeviceStateDispatcher();
159         dispatcher.dispatchDeviceStateUpdates(devicesWithUnknownDevice);
160         dispatcher.clearCache();
161         dispatcher.addListener(listener);
162
163         // when:
164         dispatcher.dispatchDeviceStateUpdates(getDevices());
165
166         // then:
167         verify(listener).onDeviceRemoved(UNKNOWN_DEVICE_IDENTIFIER);
168         verify(listener, times(2)).onDeviceStateUpdated(any());
169         verifyNoMoreInteractions(listener);
170     }
171
172     @Test
173     public void testRemovalEventsAreDispatchedToSubscribedListenersMatchingAllDeviceIds()
174             throws InterruptedException, TimeoutException, ExecutionException {
175         // given:
176         DeviceStateListener listener = mock(DeviceStateListener.class);
177
178         Device deviceWithUnknownIdentifier = mockDevice(UNKNOWN_DEVICE_IDENTIFIER);
179         DeviceCollection devicesWithUnknownDevice = mock(DeviceCollection.class);
180         when(devicesWithUnknownDevice.getDeviceIdentifiers())
181                 .thenReturn(new HashSet<String>(Arrays.asList(UNKNOWN_DEVICE_IDENTIFIER)));
182         when(devicesWithUnknownDevice.getDevice(UNKNOWN_DEVICE_IDENTIFIER)).thenReturn(deviceWithUnknownIdentifier);
183
184         DeviceCollection emptyDevices = mock(DeviceCollection.class);
185         when(emptyDevices.getDeviceIdentifiers()).thenReturn(new HashSet<String>());
186
187         DeviceStateDispatcher dispatcher = new DeviceStateDispatcher();
188         dispatcher.dispatchDeviceStateUpdates(devicesWithUnknownDevice);
189         dispatcher.clearCache();
190         dispatcher.addListener(listener);
191
192         // when:
193         dispatcher.dispatchDeviceStateUpdates(emptyDevices);
194
195         // then:
196         verify(listener).onDeviceRemoved(UNKNOWN_DEVICE_IDENTIFIER);
197         verifyNoMoreInteractions(listener);
198     }
199
200     @Test
201     public void testDeviceEventDispatchingForSubscribedListenersWithAnyDeviceIdFilter()
202             throws InterruptedException, TimeoutException, ExecutionException {
203         // given:
204         DeviceStateListener listener = mock(DeviceStateListener.class);
205
206         DeviceStateDispatcher dispatcher = new DeviceStateDispatcher();
207         dispatcher.addListener(listener);
208
209         // when:
210         dispatcher.dispatchDeviceStateUpdates(getDevices());
211
212         // then:
213         verify(listener).onDeviceStateUpdated(new DeviceState(FIRST_DEVICE_IDENTIFIER, firstDevice));
214         verify(listener).onDeviceStateUpdated(new DeviceState(SECOND_DEVICE_IDENTIFIER, secondDevice));
215         verifyNoMoreInteractions(listener);
216     }
217
218     @Test
219     public void testActionsEventDispatchingForSubscribedListeners()
220             throws InterruptedException, TimeoutException, ExecutionException {
221         // given:
222         DeviceStateListener listener = mock(DeviceStateListener.class);
223         Actions actions = mock(Actions.class);
224
225         DeviceStateDispatcher dispatcher = new DeviceStateDispatcher();
226         dispatcher.addListener(listener);
227
228         // when:
229         dispatcher.dispatchActionStateUpdates(FIRST_DEVICE_IDENTIFIER, actions);
230
231         // then:
232         verify(listener).onProcessActionUpdated(new ActionsState(FIRST_DEVICE_IDENTIFIER, actions));
233         verifyNoMoreInteractions(listener);
234     }
235
236     @Test
237     public void testDeviceStateDispatcherDispatchesDeviceStatesAndActions() {
238         // given:
239         DeviceStateListener listener = mock(DeviceStateListener.class);
240         Actions actions = mock(Actions.class);
241
242         DeviceStateDispatcher dispatcher = new DeviceStateDispatcher();
243         dispatcher.addListener(listener);
244
245         dispatcher.dispatchDeviceStateUpdates(getDevices());
246         dispatcher.dispatchActionStateUpdates(FIRST_DEVICE_IDENTIFIER, actions);
247
248         // when:
249         dispatcher.dispatchDeviceState(FIRST_DEVICE_IDENTIFIER);
250
251         // then:
252         verify(listener, times(2)).onDeviceStateUpdated(new DeviceState(FIRST_DEVICE_IDENTIFIER, firstDevice));
253         verify(listener).onDeviceStateUpdated(new DeviceState(SECOND_DEVICE_IDENTIFIER, secondDevice));
254         verify(listener).onProcessActionUpdated(new ActionsState(FIRST_DEVICE_IDENTIFIER, actions));
255         verifyNoMoreInteractions(listener);
256     }
257 }