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.Collections;
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,
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);
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);
98 public void testSwitchChannelsProperlyUpdatingHexValue() throws IOException {
99 setup(SnmpBindingConstants.CHANNEL_TYPE_UID_SWITCH, SnmpChannelMode.READ, SnmpDatatype.HEXSTRING, "AA bb 11",
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);
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);
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);
130 public void testSwitchChannelSendsConfiguredExceptionValue() throws IOException {
131 setup(SnmpBindingConstants.CHANNEL_TYPE_UID_SWITCH, SnmpChannelMode.READ, SnmpDatatype.COUNTER64, "0", "12223",
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);
140 private void verifyResponse(State expectedState) {
141 verify(thingHandlerCallback, atLeast(1)).stateUpdated(eq(CHANNEL_UID), eq(expectedState));
142 verifyStatus(ThingStatus.ONLINE);