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