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.snmp.internal;
15 import static org.junit.jupiter.api.Assertions.*;
16 import static org.mockito.ArgumentMatchers.*;
17 import static org.mockito.Mockito.*;
19 import java.io.IOException;
20 import java.util.List;
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;
40 * Tests cases for {@link SnmpTargetHandler}.
42 * @author Jan N. Klug - Initial contribution
45 public class SwitchChannelTest extends AbstractSnmpTargetHandlerTest {
48 public void testCommandsAreProperlyHandledBySwitchChannel() throws IOException {
49 VariableBinding variable;
51 variable = handleCommandSwitchChannel(SnmpDatatype.STRING, OnOffType.ON, "on", "off", true);
53 if (variable == null) {
54 fail("'variable' is null");
58 assertEquals(new OID(TEST_OID), variable.getOid());
59 assertTrue(variable.getVariable() instanceof OctetString);
60 assertEquals("on", ((OctetString) variable.getVariable()).toString());
62 variable = handleCommandSwitchChannel(SnmpDatatype.STRING, OnOffType.OFF, "on", "off", true);
64 if (variable == null) {
65 fail("'variable' is null");
69 assertEquals(new OID(TEST_OID), variable.getOid());
70 assertTrue(variable.getVariable() instanceof OctetString);
71 assertEquals("off", ((OctetString) variable.getVariable()).toString());
73 variable = handleCommandSwitchChannel(SnmpDatatype.STRING, OnOffType.OFF, "on", null, false);
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);
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);
96 public void testSwitchChannelsProperlyUpdatingHexValue() throws IOException {
97 setup(SnmpBindingConstants.CHANNEL_TYPE_UID_SWITCH, SnmpChannelMode.READ, SnmpDatatype.HEXSTRING, "AA bb 11",
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);
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);
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);
126 public void testSwitchChannelSendsConfiguredExceptionValue() throws IOException {
127 setup(SnmpBindingConstants.CHANNEL_TYPE_UID_SWITCH, SnmpChannelMode.READ, SnmpDatatype.COUNTER64, "0", "12223",
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);
135 private void verifyResponse(State expectedState) {
136 verify(thingHandlerCallback, atLeast(1)).stateUpdated(eq(CHANNEL_UID), eq(expectedState));
137 verifyStatus(ThingStatus.ONLINE);