]> git.basschouten.com Git - openhab-addons.git/blob
0ae04d1446978c9ea491c1a4998f209d01594df5
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 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.snmp.internal;
14
15 import static org.junit.jupiter.api.Assertions.*;
16 import static org.mockito.ArgumentMatchers.*;
17 import static org.mockito.Mockito.*;
18
19 import java.io.IOException;
20 import java.util.Collections;
21
22 import org.eclipse.jdt.annotation.NonNullByDefault;
23 import org.junit.jupiter.api.Test;
24 import org.openhab.binding.snmp.internal.types.SnmpChannelMode;
25 import org.openhab.binding.snmp.internal.types.SnmpDatatype;
26 import org.openhab.core.library.types.OnOffType;
27 import org.openhab.core.thing.ThingStatus;
28 import org.openhab.core.types.State;
29 import org.openhab.core.types.UnDefType;
30 import org.snmp4j.PDU;
31 import org.snmp4j.event.ResponseEvent;
32 import org.snmp4j.smi.Counter64;
33 import org.snmp4j.smi.Integer32;
34 import org.snmp4j.smi.Null;
35 import org.snmp4j.smi.OID;
36 import org.snmp4j.smi.OctetString;
37 import org.snmp4j.smi.VariableBinding;
38
39 /**
40  * Tests cases for {@link SnmpTargetHandler}.
41  *
42  * @author Jan N. Klug - Initial contribution
43  */
44 @NonNullByDefault
45 public class SwitchChannelTest extends AbstractSnmpTargetHandlerTest {
46
47     @Test
48     public void testCommandsAreProperlyHandledBySwitchChannel() throws IOException {
49         VariableBinding variable;
50
51         variable = handleCommandSwitchChannel(SnmpDatatype.STRING, OnOffType.ON, "on", "off", true);
52
53         if (variable == null) {
54             fail("'variable' is null");
55             return;
56         }
57
58         assertEquals(new OID(TEST_OID), variable.getOid());
59         assertTrue(variable.getVariable() instanceof OctetString);
60         assertEquals("on", ((OctetString) variable.getVariable()).toString());
61
62         variable = handleCommandSwitchChannel(SnmpDatatype.STRING, OnOffType.OFF, "on", "off", true);
63
64         if (variable == null) {
65             fail("'variable' is null");
66             return;
67         }
68
69         assertEquals(new OID(TEST_OID), variable.getOid());
70         assertTrue(variable.getVariable() instanceof OctetString);
71         assertEquals("off", ((OctetString) variable.getVariable()).toString());
72
73         variable = handleCommandSwitchChannel(SnmpDatatype.STRING, OnOffType.OFF, "on", null, false);
74         assertNull(variable);
75     }
76
77     @Test
78     public void testSwitchChannelsProperlyUpdatingOnValue() throws IOException {
79         setup(SnmpBindingConstants.CHANNEL_TYPE_UID_SWITCH, SnmpChannelMode.READ, SnmpDatatype.STRING, "on", "off");
80         PDU responsePDU = new PDU(PDU.RESPONSE,
81                 Collections.singletonList(new VariableBinding(new OID(TEST_OID), new OctetString("on"))));
82         ResponseEvent event = new ResponseEvent("test", null, null, responsePDU, null);
83         thingHandler.onResponse(event);
84         verifyResponse(OnOffType.ON);
85     }
86
87     @Test
88     public void testSwitchChannelsProperlyUpdatingOffValue() throws IOException {
89         setup(SnmpBindingConstants.CHANNEL_TYPE_UID_SWITCH, SnmpChannelMode.READ, SnmpDatatype.INT32, "0", "3");
90         PDU responsePDU = new PDU(PDU.RESPONSE,
91                 Collections.singletonList(new VariableBinding(new OID(TEST_OID), new Integer32(3))));
92         ResponseEvent event = new ResponseEvent("test", null, null, responsePDU, null);
93         thingHandler.onResponse(event);
94         verifyResponse(OnOffType.OFF);
95     }
96
97     @Test
98     public void testSwitchChannelsProperlyUpdatingHexValue() throws IOException {
99         setup(SnmpBindingConstants.CHANNEL_TYPE_UID_SWITCH, SnmpChannelMode.READ, SnmpDatatype.HEXSTRING, "AA bb 11",
100                 "cc ba 1d");
101         PDU responsePDU = new PDU(PDU.RESPONSE, Collections
102                 .singletonList(new VariableBinding(new OID(TEST_OID), OctetString.fromHexStringPairs("aabb11"))));
103         ResponseEvent event = new ResponseEvent("test", null, null, responsePDU, null);
104         thingHandler.onResponse(event);
105         verifyResponse(OnOffType.ON);
106     }
107
108     @Test
109     public void testSwitchChannelsIgnoresArbitraryValue() throws IOException {
110         setup(SnmpBindingConstants.CHANNEL_TYPE_UID_SWITCH, SnmpChannelMode.READ, SnmpDatatype.COUNTER64, "0", "12223");
111         PDU responsePDU = new PDU(PDU.RESPONSE,
112                 Collections.singletonList(new VariableBinding(new OID(TEST_OID), new Counter64(17))));
113         ResponseEvent event = new ResponseEvent("test", null, null, responsePDU, null);
114         thingHandler.onResponse(event);
115         verify(thingHandlerCallback, never()).stateUpdated(eq(CHANNEL_UID), any());
116         verifyStatus(ThingStatus.ONLINE);
117     }
118
119     @Test
120     public void testSwitchChannelSendsUndefExceptionValue() throws IOException {
121         setup(SnmpBindingConstants.CHANNEL_TYPE_UID_SWITCH, SnmpChannelMode.READ, SnmpDatatype.COUNTER64, "0", "12223");
122         PDU responsePDU = new PDU(PDU.RESPONSE,
123                 Collections.singletonList(new VariableBinding(new OID(TEST_OID), Null.noSuchInstance)));
124         ResponseEvent event = new ResponseEvent("test", null, null, responsePDU, null);
125         thingHandler.onResponse(event);
126         verifyResponse(UnDefType.UNDEF);
127     }
128
129     @Test
130     public void testSwitchChannelSendsConfiguredExceptionValue() throws IOException {
131         setup(SnmpBindingConstants.CHANNEL_TYPE_UID_SWITCH, SnmpChannelMode.READ, SnmpDatatype.COUNTER64, "0", "12223",
132                 "OFF");
133         PDU responsePDU = new PDU(PDU.RESPONSE,
134                 Collections.singletonList(new VariableBinding(new OID(TEST_OID), Null.noSuchInstance)));
135         ResponseEvent event = new ResponseEvent("test", null, null, responsePDU, null);
136         thingHandler.onResponse(event);
137         verifyResponse(OnOffType.OFF);
138     }
139
140     private void verifyResponse(State expectedState) {
141         verify(thingHandlerCallback, atLeast(1)).stateUpdated(eq(CHANNEL_UID), eq(expectedState));
142         verifyStatus(ThingStatus.ONLINE);
143     }
144 }