]> git.basschouten.com Git - openhab-addons.git/blob
c2c2063e2bcb337585e51564c1cd15b9ef5dcab7
[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.List;
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, List.of(new VariableBinding(new OID(TEST_OID), new OctetString("on"))));
81         ResponseEvent event = new ResponseEvent("test", null, null, responsePDU, null);
82         thingHandler.onResponse(event);
83         verifyResponse(OnOffType.ON);
84     }
85
86     @Test
87     public void testSwitchChannelsProperlyUpdatingOffValue() throws IOException {
88         setup(SnmpBindingConstants.CHANNEL_TYPE_UID_SWITCH, SnmpChannelMode.READ, SnmpDatatype.INT32, "0", "3");
89         PDU responsePDU = new PDU(PDU.RESPONSE, List.of(new VariableBinding(new OID(TEST_OID), new Integer32(3))));
90         ResponseEvent event = new ResponseEvent("test", null, null, responsePDU, null);
91         thingHandler.onResponse(event);
92         verifyResponse(OnOffType.OFF);
93     }
94
95     @Test
96     public void testSwitchChannelsProperlyUpdatingHexValue() throws IOException {
97         setup(SnmpBindingConstants.CHANNEL_TYPE_UID_SWITCH, SnmpChannelMode.READ, SnmpDatatype.HEXSTRING, "AA bb 11",
98                 "cc ba 1d");
99         PDU responsePDU = new PDU(PDU.RESPONSE,
100                 List.of(new VariableBinding(new OID(TEST_OID), OctetString.fromHexStringPairs("aabb11"))));
101         ResponseEvent event = new ResponseEvent("test", null, null, responsePDU, null);
102         thingHandler.onResponse(event);
103         verifyResponse(OnOffType.ON);
104     }
105
106     @Test
107     public void testSwitchChannelsIgnoresArbitraryValue() throws IOException {
108         setup(SnmpBindingConstants.CHANNEL_TYPE_UID_SWITCH, SnmpChannelMode.READ, SnmpDatatype.COUNTER64, "0", "12223");
109         PDU responsePDU = new PDU(PDU.RESPONSE, List.of(new VariableBinding(new OID(TEST_OID), new Counter64(17))));
110         ResponseEvent event = new ResponseEvent("test", null, null, responsePDU, null);
111         thingHandler.onResponse(event);
112         verify(thingHandlerCallback, never()).stateUpdated(eq(CHANNEL_UID), any());
113         verifyStatus(ThingStatus.ONLINE);
114     }
115
116     @Test
117     public void testSwitchChannelSendsUndefExceptionValue() throws IOException {
118         setup(SnmpBindingConstants.CHANNEL_TYPE_UID_SWITCH, SnmpChannelMode.READ, SnmpDatatype.COUNTER64, "0", "12223");
119         PDU responsePDU = new PDU(PDU.RESPONSE, List.of(new VariableBinding(new OID(TEST_OID), Null.noSuchInstance)));
120         ResponseEvent event = new ResponseEvent("test", null, null, responsePDU, null);
121         thingHandler.onResponse(event);
122         verifyResponse(UnDefType.UNDEF);
123     }
124
125     @Test
126     public void testSwitchChannelSendsConfiguredExceptionValue() throws IOException {
127         setup(SnmpBindingConstants.CHANNEL_TYPE_UID_SWITCH, SnmpChannelMode.READ, SnmpDatatype.COUNTER64, "0", "12223",
128                 "OFF");
129         PDU responsePDU = new PDU(PDU.RESPONSE, List.of(new VariableBinding(new OID(TEST_OID), Null.noSuchInstance)));
130         ResponseEvent event = new ResponseEvent("test", null, null, responsePDU, null);
131         thingHandler.onResponse(event);
132         verifyResponse(OnOffType.OFF);
133     }
134
135     private void verifyResponse(State expectedState) {
136         verify(thingHandlerCallback, atLeast(1)).stateUpdated(eq(CHANNEL_UID), eq(expectedState));
137         verifyStatus(ThingStatus.ONLINE);
138     }
139 }