2 * Copyright (c) 2010-2020 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.types.UnDefType;
25 import org.snmp4j.PDU;
26 import org.snmp4j.event.ResponseEvent;
27 import org.snmp4j.smi.Counter64;
28 import org.snmp4j.smi.Integer32;
29 import org.snmp4j.smi.Null;
30 import org.snmp4j.smi.OID;
31 import org.snmp4j.smi.OctetString;
32 import org.snmp4j.smi.VariableBinding;
35 * Tests cases for {@link SnmpTargetHandler}.
37 * @author Jan N. Klug - Initial contribution
39 public class SwitchChannelTest extends AbstractSnmpTargetHandlerTest {
42 public void testCommandsAreProperlyHandledBySwitchChannel() throws IOException {
43 VariableBinding variable;
45 variable = handleCommandSwitchChannel(SnmpDatatype.STRING, OnOffType.ON, "on", "off", true);
46 assertEquals(new OID(TEST_OID), variable.getOid());
47 assertTrue(variable.getVariable() instanceof OctetString);
48 assertEquals("on", ((OctetString) variable.getVariable()).toString());
50 variable = handleCommandSwitchChannel(SnmpDatatype.STRING, OnOffType.OFF, "on", "off", true);
51 assertEquals(new OID(TEST_OID), variable.getOid());
52 assertTrue(variable.getVariable() instanceof OctetString);
53 assertEquals("off", ((OctetString) variable.getVariable()).toString());
55 variable = handleCommandSwitchChannel(SnmpDatatype.STRING, OnOffType.OFF, "on", null, false);
60 public void testSwitchChannelsProperlyUpdatingOnValue() throws IOException {
61 setup(SnmpBindingConstants.CHANNEL_TYPE_UID_SWITCH, SnmpChannelMode.READ, SnmpDatatype.STRING, "on", "off");
62 PDU responsePDU = new PDU(PDU.RESPONSE,
63 Collections.singletonList(new VariableBinding(new OID(TEST_OID), new OctetString("on"))));
64 ResponseEvent event = new ResponseEvent("test", null, null, responsePDU, null);
65 thingHandler.onResponse(event);
66 verify(thingHandlerCallback, atLeast(1)).stateUpdated(eq(CHANNEL_UID), eq(OnOffType.ON));
70 public void testSwitchChannelsProperlyUpdatingOffValue() throws IOException {
71 setup(SnmpBindingConstants.CHANNEL_TYPE_UID_SWITCH, SnmpChannelMode.READ, SnmpDatatype.INT32, "0", "3");
72 PDU responsePDU = new PDU(PDU.RESPONSE,
73 Collections.singletonList(new VariableBinding(new OID(TEST_OID), new Integer32(3))));
74 ResponseEvent event = new ResponseEvent("test", null, null, responsePDU, null);
75 thingHandler.onResponse(event);
76 verify(thingHandlerCallback, atLeast(1)).stateUpdated(eq(CHANNEL_UID), eq(OnOffType.OFF));
80 public void testSwitchChannelsProperlyUpdatingHexValue() throws IOException {
81 setup(SnmpBindingConstants.CHANNEL_TYPE_UID_SWITCH, SnmpChannelMode.READ, SnmpDatatype.HEXSTRING, "AA bb 11",
83 PDU responsePDU = new PDU(PDU.RESPONSE, Collections
84 .singletonList(new VariableBinding(new OID(TEST_OID), OctetString.fromHexStringPairs("aabb11"))));
85 ResponseEvent event = new ResponseEvent("test", null, null, responsePDU, null);
86 thingHandler.onResponse(event);
87 verify(thingHandlerCallback, atLeast(1)).stateUpdated(eq(CHANNEL_UID), eq(OnOffType.ON));
91 public void testSwitchChannelsIgnoresArbitraryValue() throws IOException {
92 setup(SnmpBindingConstants.CHANNEL_TYPE_UID_SWITCH, SnmpChannelMode.READ, SnmpDatatype.COUNTER64, "0", "12223");
93 PDU responsePDU = new PDU(PDU.RESPONSE,
94 Collections.singletonList(new VariableBinding(new OID(TEST_OID), new Counter64(17))));
95 ResponseEvent event = new ResponseEvent("test", null, null, responsePDU, null);
96 thingHandler.onResponse(event);
97 verify(thingHandlerCallback, never()).stateUpdated(eq(CHANNEL_UID), any());
101 public void testSwitchChannelSendsUndefExceptionValue() throws IOException {
102 setup(SnmpBindingConstants.CHANNEL_TYPE_UID_SWITCH, SnmpChannelMode.READ, SnmpDatatype.COUNTER64, "0", "12223");
103 PDU responsePDU = new PDU(PDU.RESPONSE,
104 Collections.singletonList(new VariableBinding(new OID(TEST_OID), Null.noSuchInstance)));
105 ResponseEvent event = new ResponseEvent("test", null, null, responsePDU, null);
106 thingHandler.onResponse(event);
107 verify(thingHandlerCallback, atLeast(1)).stateUpdated(eq(CHANNEL_UID), eq(UnDefType.UNDEF));
111 public void testSwitchChannelSendsConfiguredExceptionValue() throws IOException {
112 setup(SnmpBindingConstants.CHANNEL_TYPE_UID_SWITCH, SnmpChannelMode.READ, SnmpDatatype.COUNTER64, "0", "12223",
114 PDU responsePDU = new PDU(PDU.RESPONSE,
115 Collections.singletonList(new VariableBinding(new OID(TEST_OID), Null.noSuchInstance)));
116 ResponseEvent event = new ResponseEvent("test", null, null, responsePDU, null);
117 thingHandler.onResponse(event);
118 verify(thingHandlerCallback, atLeast(1)).stateUpdated(eq(CHANNEL_UID), eq(OnOffType.OFF));