2 * Copyright (c) 2010-2020 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.Assert.*;
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.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;
35 import io.swagger.client.model.NAMain;
36 import io.swagger.client.model.NAStationDataBody;
37 import io.swagger.client.model.NAStationModule;
40 * @author Sven Strohschein - Initial contribution
42 @RunWith(MockitoJUnitRunner.class)
43 public class NetatmoModuleDiscoveryServiceTest {
45 private NetatmoModuleDiscoveryServiceAccessible service;
46 private NetatmoBridgeHandler bridgeHandlerSpy;
49 public void before() {
50 Bridge bridgeMock = mock(Bridge.class);
51 when(bridgeMock.getUID()).thenReturn(new ThingUID("netatmo", "bridge"));
53 bridgeHandlerSpy = spy(new NetatmoBridgeHandler(bridgeMock, null));
55 LocaleProvider localeProviderMock = mock(LocaleProvider.class);
56 TranslationProvider translationProvider = mock(TranslationProvider.class);
58 service = new NetatmoModuleDiscoveryServiceAccessible(bridgeHandlerSpy, localeProviderMock,
63 public void testStartScanNothingActivated() {
66 assertEquals(0, service.getDiscoveredThings().size());
70 public void testStartScanDiscoverWeatherStationNoStationsBody() {
71 activateDiscoveryWeatherStation();
75 assertEquals(0, service.getDiscoveredThings().size());
79 public void testStartScanDiscoverWeatherStationNoStations() {
80 activateDiscoveryWeatherStation();
82 when(bridgeHandlerSpy.getStationsDataBody(null)).thenReturn(Optional.of(new NAStationDataBody()));
85 assertEquals(0, service.getDiscoveredThings().size());
89 public void testStartScanDiscoverWeatherStationNoStationName() {
90 recordStationBody(createStation());
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());
101 public void testStartScanDiscoverWeatherStation() {
102 NAMain station = createStation();
103 station.setStationName("Neu Wulmstorf");
105 recordStationBody(station);
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());
117 public void testStartScanDiscoverWeatherStationNoStationNameFavorite() {
118 NAMain station = createStation();
119 station.setFavorite(true);
121 recordStationBody(station);
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());
133 public void testStartScanDiscoverWeatherStationFavorite() {
134 NAMain station = createStation();
135 station.setStationName("Neu Wulmstorf");
136 station.setFavorite(true);
138 recordStationBody(station);
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());
150 public void testStartScanDiscoverWeatherStationModuleNoModuleName() {
151 NAMain station = createStation(createModule());
152 station.setStationName("Neu Wulmstorf");
154 recordStationBody(station);
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());
167 public void testStartScanDiscoverWeatherStationModule() {
168 NAStationModule module = createModule();
169 module.setModuleName("Outdoor-Module");
171 NAMain station = createStation(module);
172 station.setStationName("Neu Wulmstorf");
174 recordStationBody(station);
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());
187 public void testStartScanDiscoverWeatherStationModuleNoModuleNameFavorite() {
188 NAMain station = createStation(createModule());
189 station.setStationName("Neu Wulmstorf");
190 station.setFavorite(true);
192 recordStationBody(station);
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());
205 public void testStartScanDiscoverWeatherStationModuleFavorite() {
206 NAStationModule module = createModule();
207 module.setModuleName("Outdoor-Module");
209 NAMain station = createStation(module);
210 station.setStationName("Neu Wulmstorf");
211 station.setFavorite(true);
213 recordStationBody(station);
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());
225 private void recordStationBody(NAMain station) {
226 activateDiscoveryWeatherStation();
228 NAStationDataBody stationsBody = new NAStationDataBody();
229 stationsBody.setDevices(Collections.singletonList(station));
231 when(bridgeHandlerSpy.getStationsDataBody(null)).thenReturn(Optional.of(stationsBody));
234 private void activateDiscoveryWeatherStation() {
235 bridgeHandlerSpy.configuration.readStation = true;
238 private static NAMain createStation() {
239 NAMain station = new NAMain();
240 station.setId("01:00:00:00:00:aa");
241 station.setType("NAMain");
245 private static NAMain createStation(NAStationModule module) {
246 NAMain station = createStation();
247 station.setModules(Collections.singletonList(module));
251 private static NAStationModule createModule() {
252 NAStationModule module = new NAStationModule();
253 module.setId("01:00:00:00:01:aa");
254 module.setType("NAModule1");
259 private static class NetatmoModuleDiscoveryServiceAccessible extends NetatmoModuleDiscoveryService {
261 private final List<DiscoveryResult> discoveredThings;
263 private NetatmoModuleDiscoveryServiceAccessible(NetatmoBridgeHandler netatmoBridgeHandler,
264 LocaleProvider localeProvider, TranslationProvider translationProvider) {
265 super(netatmoBridgeHandler, localeProvider, translationProvider);
266 discoveredThings = new ArrayList<>();
270 protected void thingDiscovered(DiscoveryResult discoveryResult) {
271 super.thingDiscovered(discoveryResult);
272 discoveredThings.add(discoveryResult);
275 private List<DiscoveryResult> getDiscoveredThings() {
276 return discoveredThings;