2 * Copyright (c) 2010-2023 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.modbus.e3dc.internal.handler;
15 import static org.mockito.Mockito.*;
17 import java.util.HashMap;
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.junit.jupiter.api.Test;
21 import org.mockito.ArgumentMatchers;
22 import org.openhab.core.config.core.Configuration;
23 import org.openhab.core.io.transport.modbus.AsyncModbusFailure;
24 import org.openhab.core.io.transport.modbus.AsyncModbusReadResult;
25 import org.openhab.core.io.transport.modbus.ModbusReadRequestBlueprint;
26 import org.openhab.core.io.transport.modbus.ModbusRegisterArray;
27 import org.openhab.core.thing.Bridge;
28 import org.openhab.core.thing.Thing;
29 import org.openhab.core.thing.ThingStatus;
30 import org.openhab.core.thing.ThingStatusDetail;
31 import org.openhab.core.thing.ThingStatusInfo;
32 import org.openhab.core.thing.ThingUID;
33 import org.openhab.core.thing.binding.ThingHandlerCallback;
36 * The {@link E3DCHandlerStateTest} Test State handling of Handler if different results occurs
38 * @author Bernd Weymann - Initial contribution
41 public class E3DCHandlerStateTest {
42 ThingStatusInfo unknownStatus = new ThingStatusInfo(ThingStatus.UNKNOWN, ThingStatusDetail.NONE, null);
43 ThingStatusInfo onlineStatus = new ThingStatusInfo(ThingStatus.ONLINE, ThingStatusDetail.NONE, null);
44 ThingStatusInfo offlineStatus = new ThingStatusInfo(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR,
45 E3DCThingHandler.DATA_READ_ERROR);
48 public void testStatusChain() {
49 Bridge bridge = mock(Bridge.class);
50 ThingUID uid = new ThingUID("modbus", "e3dc", "powerplant");
51 when(bridge.getUID()).thenReturn(uid);
52 ThingHandlerCallback callback = mock(ThingHandlerCallback.class);
53 E3DCThingHandler handler = new E3DCThingHandler(bridge);
54 handler.setCallback(callback);
56 HashMap<String, Object> map = new HashMap<String, Object>();
57 map.put("refresh", 2000);
58 Configuration config = new Configuration(map);
59 when(bridge.getConfiguration()).thenReturn(config);
61 verify(callback).statusUpdated(ArgumentMatchers.eq((Thing) bridge), ArgumentMatchers.eq(unknownStatus));
62 // Initializing is ongoing - now simulate info and data callback
64 handler.handleInfoResult(getInfoResult());
65 verify(callback).statusUpdated(ArgumentMatchers.eq((Thing) bridge), ArgumentMatchers.eq(unknownStatus));
66 handler.handleDataResult(getDataResult());
67 verify(callback, times(1)).statusUpdated(ArgumentMatchers.eq((Thing) bridge),
68 ArgumentMatchers.eq(onlineStatus));
71 // call it a few times - ensure at the end of the test that "ONLINE" status is raised only 2 times and not all
73 handler.handleDataResult(getDataResult());
74 handler.handleDataResult(getDataResult());
75 handler.handleDataResult(getDataResult());
77 // simulate one wrong data result
78 handler.handleDataFailure(getFailResult());
79 verify(callback).statusUpdated(ArgumentMatchers.eq((Thing) bridge), ArgumentMatchers.eq(offlineStatus));
82 handler.handleDataResult(getDataResult());
83 verify(callback, times(2)).statusUpdated(ArgumentMatchers.eq((Thing) bridge),
84 ArgumentMatchers.eq(onlineStatus));
87 private AsyncModbusReadResult getInfoResult() {
88 byte[] infoBlockBytes = new byte[] { -29, -36, 1, 2, 0, -120, 69, 51, 47, 68, 67, 32, 71, 109, 98, 72, 0, 0, 0,
89 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 83, 49, 48, 32, 69, 32, 65, 73, 79, 0, 0, 0, 0,
90 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 85, 78, 73, 78, 73, 84, 73, 65, 76, 73, 90, 69,
91 68, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 83, 49, 48, 95, 50, 48, 50, 48, 95, 48, 52,
92 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
93 ModbusReadRequestBlueprint readRequest = mock(ModbusReadRequestBlueprint.class);
94 return new AsyncModbusReadResult(readRequest, new ModbusRegisterArray(infoBlockBytes));
97 private AsyncModbusReadResult getDataResult() {
98 byte[] dataBlockBytes = new byte[] { 0, -14, 0, 0, -2, -47, -1, -1, 2, 47, 0, 0, 0, 14, 0, 0, 0, 0, 0, 0, 0, 0,
99 0, 0, 0, 0, 0, 0, 99, 99, 0, 99, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
100 1, 125, 2, 21, 0, 0, 0, 27, 0, 26, 0, 0, 0, 103, 0, -117, 0, 0 };
101 ModbusReadRequestBlueprint readRequest = mock(ModbusReadRequestBlueprint.class);
102 return new AsyncModbusReadResult(readRequest, new ModbusRegisterArray(dataBlockBytes));
105 private AsyncModbusFailure<ModbusReadRequestBlueprint> getFailResult() {
106 ModbusReadRequestBlueprint readRequest = mock(ModbusReadRequestBlueprint.class);
107 return new AsyncModbusFailure<ModbusReadRequestBlueprint>(readRequest, new Exception("Something failed!"));