2 * Copyright (c) 2010-2021 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.netatmo.internal.discovery;
15 import static org.junit.jupiter.api.Assertions.assertEquals;
16 import static org.mockito.Mockito.*;
18 import java.util.ArrayList;
19 import java.util.Collections;
20 import java.util.List;
21 import java.util.Optional;
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;
37 import io.swagger.client.model.NAMain;
38 import io.swagger.client.model.NAStationDataBody;
39 import io.swagger.client.model.NAStationModule;
42 * @author Sven Strohschein - Initial contribution
44 @ExtendWith(MockitoExtension.class)
45 @MockitoSettings(strictness = Strictness.WARN)
46 public class NetatmoModuleDiscoveryServiceTest {
48 private NetatmoModuleDiscoveryServiceAccessible service;
49 private NetatmoBridgeHandler bridgeHandlerSpy;
52 public void before() {
53 Bridge bridgeMock = mock(Bridge.class);
54 when(bridgeMock.getUID()).thenReturn(new ThingUID("netatmo", "bridge"));
56 bridgeHandlerSpy = spy(new NetatmoBridgeHandler(bridgeMock, null));
58 LocaleProvider localeProviderMock = mock(LocaleProvider.class);
59 TranslationProvider translationProvider = mock(TranslationProvider.class);
61 service = new NetatmoModuleDiscoveryServiceAccessible(bridgeHandlerSpy, localeProviderMock,
66 public void testStartScanNothingActivated() {
69 assertEquals(0, service.getDiscoveredThings().size());
73 public void testStartScanDiscoverWeatherStationNoStationsBody() {
74 activateDiscoveryWeatherStation();
78 assertEquals(0, service.getDiscoveredThings().size());
82 public void testStartScanDiscoverWeatherStationNoStations() {
83 activateDiscoveryWeatherStation();
85 when(bridgeHandlerSpy.getStationsDataBody(null)).thenReturn(Optional.of(new NAStationDataBody()));
88 assertEquals(0, service.getDiscoveredThings().size());
92 public void testStartScanDiscoverWeatherStationNoStationName() {
93 recordStationBody(createStation());
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());
104 public void testStartScanDiscoverWeatherStation() {
105 NAMain station = createStation();
106 station.setStationName("Neu Wulmstorf");
108 recordStationBody(station);
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());
120 public void testStartScanDiscoverWeatherStationNoStationNameFavorite() {
121 NAMain station = createStation();
122 station.setFavorite(true);
124 recordStationBody(station);
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());
136 public void testStartScanDiscoverWeatherStationFavorite() {
137 NAMain station = createStation();
138 station.setStationName("Neu Wulmstorf");
139 station.setFavorite(true);
141 recordStationBody(station);
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());
153 public void testStartScanDiscoverWeatherStationModuleNoModuleName() {
154 NAMain station = createStation(createModule());
155 station.setStationName("Neu Wulmstorf");
157 recordStationBody(station);
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());
170 public void testStartScanDiscoverWeatherStationModule() {
171 NAStationModule module = createModule();
172 module.setModuleName("Outdoor-Module");
174 NAMain station = createStation(module);
175 station.setStationName("Neu Wulmstorf");
177 recordStationBody(station);
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());
190 public void testStartScanDiscoverWeatherStationModuleNoModuleNameFavorite() {
191 NAMain station = createStation(createModule());
192 station.setStationName("Neu Wulmstorf");
193 station.setFavorite(true);
195 recordStationBody(station);
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());
208 public void testStartScanDiscoverWeatherStationModuleFavorite() {
209 NAStationModule module = createModule();
210 module.setModuleName("Outdoor-Module");
212 NAMain station = createStation(module);
213 station.setStationName("Neu Wulmstorf");
214 station.setFavorite(true);
216 recordStationBody(station);
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());
228 private void recordStationBody(NAMain station) {
229 activateDiscoveryWeatherStation();
231 NAStationDataBody stationsBody = new NAStationDataBody();
232 stationsBody.setDevices(Collections.singletonList(station));
234 when(bridgeHandlerSpy.getStationsDataBody(null)).thenReturn(Optional.of(stationsBody));
237 private void activateDiscoveryWeatherStation() {
238 bridgeHandlerSpy.configuration.readStation = true;
241 private static NAMain createStation() {
242 NAMain station = new NAMain();
243 station.setId("01:00:00:00:00:aa");
244 station.setType("NAMain");
248 private static NAMain createStation(NAStationModule module) {
249 NAMain station = createStation();
250 station.setModules(Collections.singletonList(module));
254 private static NAStationModule createModule() {
255 NAStationModule module = new NAStationModule();
256 module.setId("01:00:00:00:01:aa");
257 module.setType("NAModule1");
262 private static class NetatmoModuleDiscoveryServiceAccessible extends NetatmoModuleDiscoveryService {
264 private final List<DiscoveryResult> discoveredThings;
266 private NetatmoModuleDiscoveryServiceAccessible(NetatmoBridgeHandler netatmoBridgeHandler,
267 LocaleProvider localeProvider, TranslationProvider translationProvider) {
268 super(netatmoBridgeHandler, localeProvider, translationProvider);
269 discoveredThings = new ArrayList<>();
273 protected void thingDiscovered(DiscoveryResult discoveryResult) {
274 super.thingDiscovered(discoveryResult);
275 discoveredThings.add(discoveryResult);
278 private List<DiscoveryResult> getDiscoveredThings() {
279 return discoveredThings;