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