]> git.basschouten.com Git - openhab-addons.git/blob
5f5cb7efef2956a753ca85987987b5bdfa6a27ba
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2024 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.modbus.e3dc.internal.handler;
14
15 import static org.mockito.Mockito.*;
16
17 import java.util.HashMap;
18
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;
34
35 /**
36  * The {@link E3DCHandlerStateTest} Test State handling of Handler if different results occurs
37  *
38  * @author Bernd Weymann - Initial contribution
39  */
40 @NonNullByDefault
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);
46
47     @Test
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);
55
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);
60         handler.initialize();
61         verify(callback).statusUpdated(ArgumentMatchers.eq((Thing) bridge), ArgumentMatchers.eq(unknownStatus));
62         // Initializing is ongoing - now simulate info and data callback
63
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));
69         // we are ONLINE!
70
71         // call it a few times - ensure at the end of the test that "ONLINE" status is raised only 2 times and not all
72         // the time
73         handler.handleDataResult(getDataResult());
74         handler.handleDataResult(getDataResult());
75         handler.handleDataResult(getDataResult());
76
77         // simulate one wrong data result
78         handler.handleDataFailure(getFailResult());
79         verify(callback).statusUpdated(ArgumentMatchers.eq((Thing) bridge), ArgumentMatchers.eq(offlineStatus));
80
81         // // go online again
82         handler.handleDataResult(getDataResult());
83         verify(callback, times(2)).statusUpdated(ArgumentMatchers.eq((Thing) bridge),
84                 ArgumentMatchers.eq(onlineStatus));
85     }
86
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));
95     }
96
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));
103     }
104
105     private AsyncModbusFailure<ModbusReadRequestBlueprint> getFailResult() {
106         ModbusReadRequestBlueprint readRequest = mock(ModbusReadRequestBlueprint.class);
107         return new AsyncModbusFailure<ModbusReadRequestBlueprint>(readRequest, new Exception("Something failed!"));
108     }
109 }