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