]> git.basschouten.com Git - openhab-addons.git/blob
a140c9c473f0418261c95eeccf556cf5cb8b50da
[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.dsmr.internal.discovery;
14
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.*;
18
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;
26
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;
41
42 /**
43  * Test class for {@link DSMRMeterDiscoveryService}.
44  *
45  * @author Hilbrand Bouwkamp - Initial contribution
46  */
47 @ExtendWith(MockitoExtension.class)
48 public class DSMRMeterDiscoveryServiceTest {
49
50     private static final String EXPECTED_CONFIGURED_TELEGRAM = "dsmr_50";
51     private static final String UNREGISTERED_METER_TELEGRAM = "unregistered_meter";
52
53     private @Mock(answer = Answers.RETURNS_DEEP_STUBS) DSMRBridgeHandler bridge;
54     private @Mock Thing thing;
55     private @Mock DSMRMeterHandler meterHandler;
56
57     /**
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.
61      */
62     @Test
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) {
68             @Override
69             protected void reportConfigurationValidationResults(List<DSMRMeterType> invalidConfigured,
70                     List<DSMRMeterType> unconfiguredMeters) {
71                 super.reportConfigurationValidationResults(invalidConfigured, unconfiguredMeters);
72                 invalidConfiguredRef.set(invalidConfigured);
73                 unconfiguredRef.set(unconfiguredMeters);
74             }
75         };
76
77         // Mock the invalid configuration by reading a telegram that is valid for a meter that is a subset of the
78         // expected meter.
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());
86             }
87             return detectMetersRef.get().next();
88         });
89         when(thing.getHandler()).thenReturn(meterHandler);
90         when(bridge.getThing().getUID()).thenReturn(new ThingUID("dsmr:dsmrBridge:22e5393c"));
91         when(bridge.getThing().getThings()).thenReturn(things);
92
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");
100     }
101
102     /**
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.
106      */
107     @Test
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) {
112             @Override
113             protected void reportUnregisteredMeters() {
114                 super.reportUnregisteredMeters();
115                 unregisteredMeter.set(true);
116             }
117         };
118         when(bridge.getThing().getUID()).thenReturn(new ThingUID("dsmr:dsmrBridge:22e5393c"));
119         when(bridge.getThing().getThings()).thenReturn(Collections.emptyList());
120
121         service.telegramReceived(telegram);
122         assertTrue(unregisteredMeter.get(), "Should have found an unregistered meter");
123     }
124 }