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.junit.jupiter.api.Test;
23 import org.openhab.core.library.types.OnOffType;
24 import org.openhab.core.thing.ThingStatus;
25 import org.openhab.core.types.State;
26 import org.openhab.core.types.UnDefType;
27 import org.snmp4j.PDU;
28 import org.snmp4j.event.ResponseEvent;
29 import org.snmp4j.smi.Counter64;
30 import org.snmp4j.smi.Integer32;
31 import org.snmp4j.smi.Null;
32 import org.snmp4j.smi.OID;
33 import org.snmp4j.smi.OctetString;
34 import org.snmp4j.smi.VariableBinding;
37 * Tests cases for {@link SnmpTargetHandler}.
39 * @author Jan N. Klug - Initial contribution
41 public class SwitchChannelTest extends AbstractSnmpTargetHandlerTest {
44 public void testCommandsAreProperlyHandledBySwitchChannel() throws IOException {
45 VariableBinding variable;
47 variable = handleCommandSwitchChannel(SnmpDatatype.STRING, OnOffType.ON, "on", "off", true);
48 assertEquals(new OID(TEST_OID), variable.getOid());
49 assertTrue(variable.getVariable() instanceof OctetString);
50 assertEquals("on", ((OctetString) variable.getVariable()).toString());
52 variable = handleCommandSwitchChannel(SnmpDatatype.STRING, OnOffType.OFF, "on", "off", true);
53 assertEquals(new OID(TEST_OID), variable.getOid());
54 assertTrue(variable.getVariable() instanceof OctetString);
55 assertEquals("off", ((OctetString) variable.getVariable()).toString());
57 variable = handleCommandSwitchChannel(SnmpDatatype.STRING, OnOffType.OFF, "on", null, false);
62 public void testSwitchChannelsProperlyUpdatingOnValue() throws IOException {
63 setup(SnmpBindingConstants.CHANNEL_TYPE_UID_SWITCH, SnmpChannelMode.READ, SnmpDatatype.STRING, "on", "off");
64 PDU responsePDU = new PDU(PDU.RESPONSE,
65 Collections.singletonList(new VariableBinding(new OID(TEST_OID), new OctetString("on"))));
66 ResponseEvent event = new ResponseEvent("test", null, null, responsePDU, null);
67 thingHandler.onResponse(event);
68 verifyResponse(OnOffType.ON);
72 public void testSwitchChannelsProperlyUpdatingOffValue() throws IOException {
73 setup(SnmpBindingConstants.CHANNEL_TYPE_UID_SWITCH, SnmpChannelMode.READ, SnmpDatatype.INT32, "0", "3");
74 PDU responsePDU = new PDU(PDU.RESPONSE,
75 Collections.singletonList(new VariableBinding(new OID(TEST_OID), new Integer32(3))));
76 ResponseEvent event = new ResponseEvent("test", null, null, responsePDU, null);
77 thingHandler.onResponse(event);
78 verifyResponse(OnOffType.OFF);
82 public void testSwitchChannelsProperlyUpdatingHexValue() throws IOException {
83 setup(SnmpBindingConstants.CHANNEL_TYPE_UID_SWITCH, SnmpChannelMode.READ, SnmpDatatype.HEXSTRING, "AA bb 11",
85 PDU responsePDU = new PDU(PDU.RESPONSE, Collections
86 .singletonList(new VariableBinding(new OID(TEST_OID), OctetString.fromHexStringPairs("aabb11"))));
87 ResponseEvent event = new ResponseEvent("test", null, null, responsePDU, null);
88 thingHandler.onResponse(event);
89 verifyResponse(OnOffType.ON);
93 public void testSwitchChannelsIgnoresArbitraryValue() throws IOException {
94 setup(SnmpBindingConstants.CHANNEL_TYPE_UID_SWITCH, SnmpChannelMode.READ, SnmpDatatype.COUNTER64, "0", "12223");
95 PDU responsePDU = new PDU(PDU.RESPONSE,
96 Collections.singletonList(new VariableBinding(new OID(TEST_OID), new Counter64(17))));
97 ResponseEvent event = new ResponseEvent("test", null, null, responsePDU, null);
98 thingHandler.onResponse(event);
99 verify(thingHandlerCallback, never()).stateUpdated(eq(CHANNEL_UID), any());
100 verifyStatus(ThingStatus.ONLINE);
104 public void testSwitchChannelSendsUndefExceptionValue() throws IOException {
105 setup(SnmpBindingConstants.CHANNEL_TYPE_UID_SWITCH, SnmpChannelMode.READ, SnmpDatatype.COUNTER64, "0", "12223");
106 PDU responsePDU = new PDU(PDU.RESPONSE,
107 Collections.singletonList(new VariableBinding(new OID(TEST_OID), Null.noSuchInstance)));
108 ResponseEvent event = new ResponseEvent("test", null, null, responsePDU, null);
109 thingHandler.onResponse(event);
110 verifyResponse(UnDefType.UNDEF);
114 public void testSwitchChannelSendsConfiguredExceptionValue() throws IOException {
115 setup(SnmpBindingConstants.CHANNEL_TYPE_UID_SWITCH, SnmpChannelMode.READ, SnmpDatatype.COUNTER64, "0", "12223",
117 PDU responsePDU = new PDU(PDU.RESPONSE,
118 Collections.singletonList(new VariableBinding(new OID(TEST_OID), Null.noSuchInstance)));
119 ResponseEvent event = new ResponseEvent("test", null, null, responsePDU, null);
120 thingHandler.onResponse(event);
121 verifyResponse(OnOffType.OFF);
124 private void verifyResponse(State expectedState) {
125 verify(thingHandlerCallback, atLeast(1)).stateUpdated(eq(CHANNEL_UID), eq(expectedState));
126 verifyStatus(ThingStatus.ONLINE);