]> git.basschouten.com Git - openhab-addons.git/blob
ce8910e12da43e9a2cfab313d30bfa42799110cd
[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.openwebnet.handler;
14
15 import static org.junit.jupiter.api.Assertions.assertEquals;
16 import static org.mockito.Mockito.mock;
17
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19 import org.eclipse.jdt.annotation.Nullable;
20 import org.junit.jupiter.api.Test;
21 import org.openhab.core.thing.Bridge;
22 import org.openwebnet4j.message.BaseOpenMessage;
23 import org.openwebnet4j.message.FrameException;
24 import org.openwebnet4j.message.Where;
25 import org.openwebnet4j.message.WhereEnergyManagement;
26 import org.openwebnet4j.message.WhereLightAutom;
27 import org.openwebnet4j.message.WhereThermo;
28 import org.openwebnet4j.message.WhereZigBee;
29 import org.openwebnet4j.message.Who;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32
33 /**
34  * Test class for {@link OpenWebNetBridgeHandler#ownID} and ThingID calculation using {@link OpenWebNetBridgeHandler}
35  * methods: normalizeWhere(), ownIdFromWhoWhere(), ownIdFromMessage(), thingIdFromWhere()
36  *
37  * @author Massimo Valla - Initial contribution
38  * @author Andrea Conte - Energy management
39  */
40 @NonNullByDefault
41 public class OwnIdTest {
42
43     private final Logger logger = LoggerFactory.getLogger(OwnIdTest.class);
44
45  // @formatter:off
46     /**
47      *
48      *                                      deviceWhere
49      *                                      (DevAddrParam)
50      * TYPE                 WHERE           = normalizeWhere()  ownId           ThingID
51      * ---------------------------------------------------------------------------------
52      * Zigbee Switch        789309801#9     789309800h9         1.789309800h9   789309800h9
53      * Zigbee Switch_2u u1  789301201#9     789301200h9         1.789309800h9   789309800h9
54      * Zigbee Switch_2u u2  789301202#9     789301200h9         1.789309800h9   789309800h9
55      * BUS Switch           51              51                  1.51            51
56      * BUS Local Bus        25#4#01         25h4h01             1.25h4h01       25h4h01
57      * BUS Autom            93              93                  2.93            93
58      * BUS Thermo           #1 or 1         1                   4.1             1
59      * BUS Thermo actuator  1#2             1                   4.1             1
60      * BUS TempSensor       500             500                 4.500           500
61      * BUS Energy           51              51                  18.51           51
62      * -INACTIVE- BUS CEN              51              51                  15.51           51
63      * -INACTIVE- BUS CEN+             212             212                 25.212          212
64      * -INACTIVE- BUS DryContact       399             399                 25.399          399
65      *
66      */
67 // @formatter:on
68
69     public enum TEST {
70         // @formatter:off
71         zb_switch(new WhereZigBee("789309801#9"), Who.fromValue(1), "*1*1*789309801#9##", "789309800h9", "1.789309800h9", "789309800h9"),
72         zb_switch_2u_1(new WhereZigBee("789301201#9"), Who.fromValue(1), "*1*1*789301201#9##", "789301200h9", "1.789301200h9", "789301200h9"),
73         zb_switch_2u_2(new WhereZigBee("789301202#9"), Who.fromValue(1), "*1*1*789301202#9##", "789301200h9", "1.789301200h9", "789301200h9"),
74         bus_switch(new WhereLightAutom("51"), Who.fromValue(1), "*1*1*51##", "51", "1.51", "51"),
75         bus_localbus(new WhereLightAutom("25#4#01"), Who.fromValue(1), "*1*1*25#4#01##", "25h4h01", "1.25h4h01", "25h4h01"),
76         bus_autom(new WhereLightAutom("93"), Who.fromValue(2), "*2*0*93##", "93", "2.93", "93"),
77         bus_thermo_via_cu(new WhereThermo("#1"), Who.fromValue(4),"*#4*#1*0*0020##" ,"1", "4.1", "1"),
78         bus_thermo(new WhereThermo("1"), Who.fromValue(4),"*#4*1*0*0020##" , "1", "4.1", "1"),
79         bus_thermo_act(new WhereThermo("1#2"), Who.fromValue(4),"*#4*1#2*20*0##" ,"1", "4.1", "1"),
80         bus_tempSensor(new WhereThermo("500"), Who.fromValue(4), "*#4*500*15*1*0020*0001##", "500", "4.500", "500"),
81         bus_energy(new WhereEnergyManagement("51"), Who.fromValue(18), "*#18*51*113##", "51", "18.51", "51");
82
83         // @formatter:on
84
85         private final Logger logger = LoggerFactory.getLogger(TEST.class);
86
87         public final Where where;
88         public final Who who;
89         public final @Nullable BaseOpenMessage msg;
90         public final String norm, ownId, thingId;
91
92         private TEST(Where where, Who who, String msg, String norm, String ownId, String thingId) {
93             this.where = where;
94             this.who = who;
95             BaseOpenMessage bmsg = null;
96             try {
97                 bmsg = (BaseOpenMessage) BaseOpenMessage.parse(msg);
98             } catch (FrameException e) {
99                 logger.warn("something is wrong in the test table ({}). ownIdFromMessage test will be skipped",
100                         e.getMessage());
101             }
102             this.msg = bmsg;
103             this.norm = norm;
104             this.ownId = ownId;
105             this.thingId = thingId;
106         }
107     }
108
109     @Test
110     public void testOwnId() {
111         Bridge mockBridge = mock(Bridge.class);
112         OpenWebNetBridgeHandler brH = new OpenWebNetBridgeHandler(mockBridge);
113         BaseOpenMessage bmsg;
114         for (int i = 0; i < TEST.values().length; i++) {
115             TEST test = TEST.values()[i];
116             logger.info("testing where={}", test.where);
117             assertEquals(test.norm, brH.normalizeWhere(test.where));
118             assertEquals(test.ownId, brH.ownIdFromWhoWhere(test.who, test.where));
119             bmsg = test.msg;
120             if (bmsg != null) {
121                 assertEquals(test.ownId, brH.ownIdFromMessage(bmsg));
122             }
123             assertEquals(test.thingId, brH.thingIdFromWhere(test.where));
124         }
125     }
126 }