]> git.basschouten.com Git - openhab-addons.git/blob
a9b392613ce9264cf8de023163b2c655db94170b
[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.netatmo.internal.discovery;
14
15 import static org.junit.jupiter.api.Assertions.assertEquals;
16 import static org.mockito.Mockito.*;
17
18 import java.util.ArrayList;
19 import java.util.Collections;
20 import java.util.List;
21 import java.util.Optional;
22
23 import org.eclipse.jdt.annotation.NonNullByDefault;
24 import org.junit.jupiter.api.BeforeEach;
25 import org.junit.jupiter.api.Test;
26 import org.junit.jupiter.api.extension.ExtendWith;
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.netatmo.internal.handler.NetatmoBridgeHandler;
31 import org.openhab.core.config.discovery.DiscoveryResult;
32 import org.openhab.core.i18n.LocaleProvider;
33 import org.openhab.core.i18n.TranslationProvider;
34 import org.openhab.core.thing.Bridge;
35 import org.openhab.core.thing.ThingUID;
36
37 import io.swagger.client.model.NAMain;
38 import io.swagger.client.model.NAStationDataBody;
39 import io.swagger.client.model.NAStationModule;
40
41 /**
42  * @author Sven Strohschein - Initial contribution
43  */
44 @ExtendWith(MockitoExtension.class)
45 @MockitoSettings(strictness = Strictness.WARN)
46 public class NetatmoModuleDiscoveryServiceTest {
47
48     private NetatmoModuleDiscoveryServiceAccessible service;
49     private NetatmoBridgeHandler bridgeHandlerSpy;
50
51     @BeforeEach
52     public void before() {
53         Bridge bridgeMock = mock(Bridge.class);
54         when(bridgeMock.getUID()).thenReturn(new ThingUID("netatmo", "bridge"));
55
56         bridgeHandlerSpy = spy(new NetatmoBridgeHandler(bridgeMock, null));
57
58         LocaleProvider localeProviderMock = mock(LocaleProvider.class);
59         TranslationProvider translationProvider = mock(TranslationProvider.class);
60
61         service = new NetatmoModuleDiscoveryServiceAccessible(bridgeHandlerSpy, localeProviderMock,
62                 translationProvider);
63     }
64
65     @Test
66     public void testStartScanNothingActivated() {
67         service.startScan();
68
69         assertEquals(0, service.getDiscoveredThings().size());
70     }
71
72     @Test
73     public void testStartScanDiscoverWeatherStationNoStationsBody() {
74         activateDiscoveryWeatherStation();
75
76         service.startScan();
77
78         assertEquals(0, service.getDiscoveredThings().size());
79     }
80
81     @Test
82     public void testStartScanDiscoverWeatherStationNoStations() {
83         activateDiscoveryWeatherStation();
84
85         when(bridgeHandlerSpy.getStationsDataBody(null)).thenReturn(Optional.of(new NAStationDataBody()));
86         service.startScan();
87
88         assertEquals(0, service.getDiscoveredThings().size());
89     }
90
91     @Test
92     public void testStartScanDiscoverWeatherStationNoStationName() {
93         recordStationBody(createStation());
94
95         service.startScan();
96
97         List<DiscoveryResult> discoveredThings = service.getDiscoveredThings();
98         assertEquals(1, discoveredThings.size());
99         // Expected is only the type name, because a station name isn't available
100         assertEquals("NAMain", discoveredThings.get(0).getLabel());
101     }
102
103     @Test
104     public void testStartScanDiscoverWeatherStation() {
105         NAMain station = createStation();
106         station.setStationName("Neu Wulmstorf");
107
108         recordStationBody(station);
109
110         service.startScan();
111
112         List<DiscoveryResult> discoveredThings = service.getDiscoveredThings();
113         assertEquals(1, discoveredThings.size());
114         // Expected is the type name + station name, because both are available
115         // and the station name contains only the city name by default which wouldn't be sufficient.
116         assertEquals("NAMain Neu Wulmstorf", discoveredThings.get(0).getLabel());
117     }
118
119     @Test
120     public void testStartScanDiscoverWeatherStationNoStationNameFavorite() {
121         NAMain station = createStation();
122         station.setFavorite(true);
123
124         recordStationBody(station);
125
126         service.startScan();
127
128         List<DiscoveryResult> discoveredThings = service.getDiscoveredThings();
129         assertEquals(1, discoveredThings.size());
130         // Expected is "(favorite)" within the label to make clear that it is favorite station
131         // (and not the station of the user)
132         assertEquals("NAMain (favorite)", discoveredThings.get(0).getLabel());
133     }
134
135     @Test
136     public void testStartScanDiscoverWeatherStationFavorite() {
137         NAMain station = createStation();
138         station.setStationName("Neu Wulmstorf");
139         station.setFavorite(true);
140
141         recordStationBody(station);
142
143         service.startScan();
144
145         List<DiscoveryResult> discoveredThings = service.getDiscoveredThings();
146         assertEquals(1, discoveredThings.size());
147         // Expected is "(favorite)" within the label to make clear that it is favorite station
148         // (and not the station of the user)
149         assertEquals("NAMain Neu Wulmstorf (favorite)", discoveredThings.get(0).getLabel());
150     }
151
152     @Test
153     public void testStartScanDiscoverWeatherStationModuleNoModuleName() {
154         NAMain station = createStation(createModule());
155         station.setStationName("Neu Wulmstorf");
156
157         recordStationBody(station);
158
159         service.startScan();
160
161         List<DiscoveryResult> discoveredThings = service.getDiscoveredThings();
162         assertEquals(2, discoveredThings.size());
163         assertEquals("NAMain Neu Wulmstorf", discoveredThings.get(0).getLabel());
164         // Expected is the type name + station name to make clear that the module belongs to the station.
165         // The module name isn't available, therefore the type name of the module is used.
166         assertEquals("NAModule1 Neu Wulmstorf", discoveredThings.get(1).getLabel());
167     }
168
169     @Test
170     public void testStartScanDiscoverWeatherStationModule() {
171         NAStationModule module = createModule();
172         module.setModuleName("Outdoor-Module");
173
174         NAMain station = createStation(module);
175         station.setStationName("Neu Wulmstorf");
176
177         recordStationBody(station);
178
179         service.startScan();
180
181         List<DiscoveryResult> discoveredThings = service.getDiscoveredThings();
182         assertEquals(2, discoveredThings.size());
183         assertEquals("NAMain Neu Wulmstorf", discoveredThings.get(0).getLabel());
184         // Expected is the module name + station name to make clear that the module belongs to the station.
185         // Because an explicit module name is available, the module type name isn't required.
186         assertEquals("Outdoor-Module Neu Wulmstorf", discoveredThings.get(1).getLabel());
187     }
188
189     @Test
190     public void testStartScanDiscoverWeatherStationModuleNoModuleNameFavorite() {
191         NAMain station = createStation(createModule());
192         station.setStationName("Neu Wulmstorf");
193         station.setFavorite(true);
194
195         recordStationBody(station);
196
197         service.startScan();
198
199         List<DiscoveryResult> discoveredThings = service.getDiscoveredThings();
200         assertEquals(2, discoveredThings.size());
201         assertEquals("NAMain Neu Wulmstorf (favorite)", discoveredThings.get(0).getLabel());
202         // Expected is "(favorite)" within the label to make clear that it is favorite station
203         // (and not the station of the user)
204         assertEquals("NAModule1 Neu Wulmstorf (favorite)", discoveredThings.get(1).getLabel());
205     }
206
207     @Test
208     public void testStartScanDiscoverWeatherStationModuleFavorite() {
209         NAStationModule module = createModule();
210         module.setModuleName("Outdoor-Module");
211
212         NAMain station = createStation(module);
213         station.setStationName("Neu Wulmstorf");
214         station.setFavorite(true);
215
216         recordStationBody(station);
217
218         service.startScan();
219
220         List<DiscoveryResult> discoveredThings = service.getDiscoveredThings();
221         assertEquals(2, discoveredThings.size());
222         assertEquals("NAMain Neu Wulmstorf (favorite)", discoveredThings.get(0).getLabel());
223         // Expected is "(favorite)" within the label to make clear that it is favorite station
224         // (and not the station of the user)
225         assertEquals("Outdoor-Module Neu Wulmstorf (favorite)", discoveredThings.get(1).getLabel());
226     }
227
228     private void recordStationBody(NAMain station) {
229         activateDiscoveryWeatherStation();
230
231         NAStationDataBody stationsBody = new NAStationDataBody();
232         stationsBody.setDevices(Collections.singletonList(station));
233
234         when(bridgeHandlerSpy.getStationsDataBody(null)).thenReturn(Optional.of(stationsBody));
235     }
236
237     private void activateDiscoveryWeatherStation() {
238         bridgeHandlerSpy.configuration.readStation = true;
239     }
240
241     private static NAMain createStation() {
242         NAMain station = new NAMain();
243         station.setId("01:00:00:00:00:aa");
244         station.setType("NAMain");
245         return station;
246     }
247
248     private static NAMain createStation(NAStationModule module) {
249         NAMain station = createStation();
250         station.setModules(Collections.singletonList(module));
251         return station;
252     }
253
254     private static NAStationModule createModule() {
255         NAStationModule module = new NAStationModule();
256         module.setId("01:00:00:00:01:aa");
257         module.setType("NAModule1");
258         return module;
259     }
260
261     @NonNullByDefault
262     private static class NetatmoModuleDiscoveryServiceAccessible extends NetatmoModuleDiscoveryService {
263
264         private final List<DiscoveryResult> discoveredThings;
265
266         private NetatmoModuleDiscoveryServiceAccessible(NetatmoBridgeHandler netatmoBridgeHandler,
267                 LocaleProvider localeProvider, TranslationProvider translationProvider) {
268             super(netatmoBridgeHandler, localeProvider, translationProvider);
269             discoveredThings = new ArrayList<>();
270         }
271
272         @Override
273         protected void thingDiscovered(DiscoveryResult discoveryResult) {
274             super.thingDiscovered(discoveryResult);
275             discoveredThings.add(discoveryResult);
276         }
277
278         private List<DiscoveryResult> getDiscoveredThings() {
279             return discoveredThings;
280         }
281     }
282 }