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