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.dsmr.internal.discovery;
15 import static org.junit.jupiter.api.Assertions.*;
16 import static org.mockito.Mockito.when;
17 import static org.openhab.binding.dsmr.internal.meter.DSMRMeterType.*;
19 import java.util.Collections;
20 import java.util.EnumSet;
21 import java.util.Iterator;
22 import java.util.List;
23 import java.util.concurrent.atomic.AtomicBoolean;
24 import java.util.concurrent.atomic.AtomicReference;
25 import java.util.stream.Collectors;
27 import org.junit.jupiter.api.Test;
28 import org.junit.jupiter.api.extension.ExtendWith;
29 import org.mockito.Answers;
30 import org.mockito.Mock;
31 import org.mockito.junit.jupiter.MockitoExtension;
32 import org.openhab.binding.dsmr.internal.TelegramReaderUtil;
33 import org.openhab.binding.dsmr.internal.device.p1telegram.P1Telegram;
34 import org.openhab.binding.dsmr.internal.device.p1telegram.P1Telegram.TelegramState;
35 import org.openhab.binding.dsmr.internal.handler.DSMRBridgeHandler;
36 import org.openhab.binding.dsmr.internal.handler.DSMRMeterHandler;
37 import org.openhab.binding.dsmr.internal.meter.DSMRMeterDescriptor;
38 import org.openhab.binding.dsmr.internal.meter.DSMRMeterType;
39 import org.openhab.core.thing.Thing;
40 import org.openhab.core.thing.ThingUID;
43 * Test class for {@link DSMRMeterDiscoveryService}.
45 * @author Hilbrand Bouwkamp - Initial contribution
47 @ExtendWith(MockitoExtension.class)
48 public class DSMRMeterDiscoveryServiceTest {
50 private static final String EXPECTED_CONFIGURED_TELEGRAM = "dsmr_50";
51 private static final String UNREGISTERED_METER_TELEGRAM = "unregistered_meter";
53 private @Mock(answer = Answers.RETURNS_DEEP_STUBS) DSMRBridgeHandler bridge;
54 private @Mock Thing thing;
55 private @Mock DSMRMeterHandler meterHandler;
58 * Test if discovery reports when the user has incorrectly configured the binding with the wrong meter types.
59 * Some meters are a subset of other meters so it won't generates errors in usage, but some values will not be
60 * available to the user with the subset meter.
63 public void testInvalidConfiguredMeters() {
64 P1Telegram expected = TelegramReaderUtil.readTelegram(EXPECTED_CONFIGURED_TELEGRAM, TelegramState.OK);
65 AtomicReference<List<DSMRMeterType>> invalidConfiguredRef = new AtomicReference<>();
66 AtomicReference<List<DSMRMeterType>> unconfiguredRef = new AtomicReference<>();
67 DSMRMeterDiscoveryService service = new DSMRMeterDiscoveryService(bridge) {
69 protected void reportConfigurationValidationResults(List<DSMRMeterType> invalidConfigured,
70 List<DSMRMeterType> unconfiguredMeters) {
71 super.reportConfigurationValidationResults(invalidConfigured, unconfiguredMeters);
72 invalidConfiguredRef.set(invalidConfigured);
73 unconfiguredRef.set(unconfiguredMeters);
77 // Mock the invalid configuration by reading a telegram that is valid for a meter that is a subset of the
79 List<DSMRMeterDescriptor> invalidConfiguredMeterDescriptors = EnumSet.of(DEVICE_V5, ELECTRICITY_V4_2, M3_V5_0)
80 .stream().map(mt -> new DSMRMeterDescriptor(mt, 0)).collect(Collectors.toList());
81 List<Thing> things = invalidConfiguredMeterDescriptors.stream().map(m -> thing).collect(Collectors.toList());
82 AtomicReference<Iterator<DSMRMeterDescriptor>> detectMetersRef = new AtomicReference<>();
83 when((meterHandler).getMeterDescriptor()).then(a -> {
84 if (detectMetersRef.get() == null || !detectMetersRef.get().hasNext()) {
85 detectMetersRef.set(invalidConfiguredMeterDescriptors.iterator());
87 return detectMetersRef.get().next();
89 when(thing.getHandler()).thenReturn(meterHandler);
90 when(bridge.getThing().getUID()).thenReturn(new ThingUID("dsmr:dsmrBridge:22e5393c"));
91 when(bridge.getThing().getThings()).thenReturn(things);
93 service.telegramReceived(expected);
94 assertNotNull(invalidConfiguredRef.get(), "Should have invalid configured meters");
95 assertTrue(invalidConfiguredRef.get().contains(DSMRMeterType.ELECTRICITY_V4_2),
96 "Should have found specific invalid meter");
97 assertNotNull(unconfiguredRef.get(), "Should have undetected meters");
98 assertTrue(unconfiguredRef.get().contains(DSMRMeterType.ELECTRICITY_V5_0),
99 "Should have found specific undetected meter");
103 * Test if discovery correctly reports if a meter was detected that has not been registered with the energy
104 * provider. This meter doesn't report all values in telegram and therefore is not recognized as a specific
105 * meter. But reports with an empty equipment identifier.
108 public void testUnregisteredMeters() {
109 P1Telegram telegram = TelegramReaderUtil.readTelegram(UNREGISTERED_METER_TELEGRAM, TelegramState.OK);
110 AtomicBoolean unregisteredMeter = new AtomicBoolean(false);
111 DSMRMeterDiscoveryService service = new DSMRMeterDiscoveryService(bridge) {
113 protected void reportUnregisteredMeters() {
114 super.reportUnregisteredMeters();
115 unregisteredMeter.set(true);
118 when(bridge.getThing().getUID()).thenReturn(new ThingUID("dsmr:dsmrBridge:22e5393c"));
119 when(bridge.getThing().getThings()).thenReturn(Collections.emptyList());
121 service.telegramReceived(telegram);
122 assertTrue(unregisteredMeter.get(), "Should have found an unregistered meter");