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.openwebnet.internal.handler;
15 import static org.junit.jupiter.api.Assertions.assertEquals;
16 import static org.mockito.Mockito.mock;
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.WhereAlarm;
26 import org.openwebnet4j.message.WhereAuxiliary;
27 import org.openwebnet4j.message.WhereCEN;
28 import org.openwebnet4j.message.WhereEnergyManagement;
29 import org.openwebnet4j.message.WhereLightAutom;
30 import org.openwebnet4j.message.WhereThermo;
31 import org.openwebnet4j.message.WhereZigBee;
32 import org.openwebnet4j.message.Who;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
37 * Test class for {@link OpenWebNetBridgeHandler#ownID} and ThingID calculation
38 * using {@link OpenWebNetBridgeHandler} methods: normalizeWhere(),
39 * ownIdFromWhoWhere(), ownIdFromMessage(), thingIdFromWhere()
41 * @author Massimo Valla - Initial contribution, updates
42 * @author Andrea Conte - Energy management
43 * @author Giovanni Fabiani - Auxiliary message support
46 public class OwnIdTest {
48 private final Logger logger = LoggerFactory.getLogger(OwnIdTest.class);
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
70 * BUS Scenario 05 05 0.05 05
71 * BUS Alarm Zone #2 or 2 2 5.2 2
72 * BUS Alarm silent 0 0 5.0 0
73 * BUS Alarm system null 0 5.0 0
79 // msg, who, where, normW, ownId, thingId
80 zb_switch("*1*1*789309801#9##", Who.fromValue(1), new WhereZigBee("789309801#9"), "789309800h9", "1.789309800h9", "789309800h9"),
81 zb_switch_2u_1("*1*1*789301201#9##", Who.fromValue(1), new WhereZigBee("789301201#9"), "789301200h9", "1.789301200h9", "789301200h9"),
82 zb_switch_2u_2("*1*1*789301202#9##", Who.fromValue(1), new WhereZigBee("789301202#9"), "789301200h9", "1.789301200h9", "789301200h9"),
83 bus_switch("*1*1*51##", Who.fromValue(1), new WhereLightAutom("51"),"51", "1.51", "51"),
84 bus_localbus("*1*1*25#4#01##", Who.fromValue(1), new WhereLightAutom("25#4#01"), "25h4h01", "1.25h4h01", "25h4h01"),
85 bus_autom("*2*0*93##",Who.fromValue(2), new WhereLightAutom("93"), "93", "2.93", "93"),
86 bus_thermo_via_cu("*#4*#1*0*0020##", Who.fromValue(4), new WhereThermo("#1"), "1", "4.1", "1"),
87 bus_thermo("*#4*1*0*0020##", Who.fromValue(4), new WhereThermo("1"), "1", "4.1", "1"),
88 bus_thermo_act("*#4*1#2*20*0##", Who.fromValue(4), new WhereThermo("1#2") ,"1", "4.1", "1"),
89 bus_tempSensor("*#4*500*15*1*0020*0001##",Who.fromValue(4), new WhereThermo("500"), "500", "4.500", "500"),
90 bus_energy("*#18*51*113##", Who.fromValue(18), new WhereEnergyManagement("51"), "51", "18.51", "51"),
91 bus_cen("*15*31*51##", Who.fromValue(15), new WhereCEN("51"), "51", "15.51", "51"),
92 bus_cen_plus("*25*21#31*212##", Who.fromValue(25), new WhereCEN("212"), "212", "25.212", "212"),
93 bus_drycontact("*25*32#1*399##", Who.fromValue(25), new WhereCEN("399"), "399", "25.399", "399"),
94 bus_aux( "*9*1*4##", Who.fromValue(9), new WhereAuxiliary("4"),"4","9.4","4"),
95 bus_scenario( "*0*2*05##", Who.fromValue(0), new WhereLightAutom("05"), "05","0.05","05"),
96 bus_alarm_zh("*#5*#2##", Who.fromValue(5), new WhereAlarm("#2"), "2", "5.2", "2"),
97 bus_alarm_silent("*5*2*0##", Who.fromValue(5), new WhereAlarm("0"), "0", "5.0", "0"),
98 bus_alarm_system( "*5*7*##", Who.fromValue(5),new WhereAlarm("0"), "0", "5.0", "0");
102 private final Logger logger = LoggerFactory.getLogger(TEST.class);
104 public final Where where;
105 public final Who who;
106 public final @Nullable BaseOpenMessage msg;
107 public final String norm, ownId, thingId;
109 private TEST(String msg, Who who, Where where, String norm, String ownId, String thingId) {
112 BaseOpenMessage bmsg = null;
114 bmsg = (BaseOpenMessage) BaseOpenMessage.parse(msg);
115 } catch (FrameException e) {
116 logger.warn("something is wrong in the test table ({}). ownIdFromMessage test will be skipped",
122 this.thingId = thingId;
127 public void testOwnId() {
128 Bridge mockBridge = mock(Bridge.class);
129 OpenWebNetBridgeHandler brH = new OpenWebNetBridgeHandler(mockBridge);
130 BaseOpenMessage bmsg;
131 for (int i = 0; i < TEST.values().length; i++) {
132 TEST test = TEST.values()[i];
133 logger.info("testing {} (who={} where={})", test.msg, test.who, test.where);
134 assertEquals(test.norm, brH.normalizeWhere(test.where));
135 assertEquals(test.ownId, brH.ownIdFromWhoWhere(test.who, test.where));
138 assertEquals(test.ownId, brH.ownIdFromMessage(bmsg));
140 assertEquals(test.thingId, brH.thingIdFromWhere(test.where));