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.eq;
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.DecimalType;
24 import org.openhab.core.library.types.OnOffType;
25 import org.openhab.core.library.types.StringType;
26 import org.openhab.core.thing.ThingStatus;
27 import org.snmp4j.PDU;
28 import org.snmp4j.Snmp;
29 import org.snmp4j.event.ResponseEvent;
30 import org.snmp4j.smi.Counter64;
31 import org.snmp4j.smi.Integer32;
32 import org.snmp4j.smi.OID;
33 import org.snmp4j.smi.OctetString;
34 import org.snmp4j.smi.UnsignedInteger32;
35 import org.snmp4j.smi.VariableBinding;
38 * Tests cases for {@link SnmpTargetHandler}.
40 * @author Jan N. Klug - Initial contribution
42 public class SnmpTargetHandlerTest extends AbstractSnmpTargetHandlerTest {
45 public void testChannelsProperlyRefreshing() throws IOException {
46 refresh(SnmpChannelMode.READ, true);
47 refresh(SnmpChannelMode.READ_WRITE, true);
48 refresh(SnmpChannelMode.WRITE, false);
49 refresh(SnmpChannelMode.TRAP, false);
53 public void testChannelsProperlyUpdate() throws IOException {
54 onResponseNumberStringChannel(SnmpChannelMode.READ, true);
55 onResponseNumberStringChannel(SnmpChannelMode.READ_WRITE, true);
56 onResponseNumberStringChannel(SnmpChannelMode.WRITE, false);
57 onResponseNumberStringChannel(SnmpChannelMode.TRAP, false);
58 assertEquals(OnOffType.ON, onResponseSwitchChannel(SnmpChannelMode.READ, SnmpDatatype.STRING, "on", "off",
59 new OctetString("on"), true));
60 assertEquals(OnOffType.OFF, onResponseSwitchChannel(SnmpChannelMode.READ_WRITE, SnmpDatatype.INT32, "1", "2",
61 new Integer32(2), true));
62 assertNull(onResponseSwitchChannel(SnmpChannelMode.WRITE, SnmpDatatype.STRING, "on", "off",
63 new OctetString("on"), false));
65 onResponseSwitchChannel(SnmpChannelMode.TRAP, SnmpDatatype.INT32, "1", "2", new Integer32(2), false));
66 verifyStatus(ThingStatus.ONLINE);
70 public void testCommandsAreProperlyHandledByNumberChannel() throws IOException {
71 VariableBinding variable;
72 variable = handleCommandNumberStringChannel(SnmpBindingConstants.CHANNEL_TYPE_UID_NUMBER, SnmpDatatype.INT32,
73 new DecimalType(-5), true);
74 assertEquals(new OID(TEST_OID), variable.getOid());
75 assertTrue(variable.getVariable() instanceof Integer32);
76 assertEquals(-5, ((Integer32) variable.getVariable()).toInt());
78 variable = handleCommandNumberStringChannel(SnmpBindingConstants.CHANNEL_TYPE_UID_NUMBER, SnmpDatatype.UINT32,
79 new DecimalType(10000), true);
80 assertEquals(new OID(TEST_OID), variable.getOid());
81 assertTrue(variable.getVariable() instanceof UnsignedInteger32);
82 assertEquals(10000, ((UnsignedInteger32) variable.getVariable()).toInt());
84 variable = handleCommandNumberStringChannel(SnmpBindingConstants.CHANNEL_TYPE_UID_NUMBER,
85 SnmpDatatype.COUNTER64, new DecimalType(10000), true);
86 assertEquals(new OID(TEST_OID), variable.getOid());
87 assertTrue(variable.getVariable() instanceof Counter64);
88 assertEquals(10000, ((Counter64) variable.getVariable()).toInt());
90 variable = handleCommandNumberStringChannel(SnmpBindingConstants.CHANNEL_TYPE_UID_NUMBER, SnmpDatatype.FLOAT,
91 new DecimalType("12.4"), true);
92 assertEquals(new OID(TEST_OID), variable.getOid());
93 assertTrue(variable.getVariable() instanceof OctetString);
94 assertEquals("12.4", variable.getVariable().toString());
96 variable = handleCommandNumberStringChannel(SnmpBindingConstants.CHANNEL_TYPE_UID_NUMBER, SnmpDatatype.INT32,
97 new StringType(TEST_STRING), false);
102 public void testNumberChannelsProperlyUpdatingFloatValue() throws IOException {
103 setup(SnmpBindingConstants.CHANNEL_TYPE_UID_NUMBER, SnmpChannelMode.READ, SnmpDatatype.FLOAT);
104 PDU responsePDU = new PDU(PDU.RESPONSE,
105 Collections.singletonList(new VariableBinding(new OID(TEST_OID), new OctetString("12.4"))));
106 ResponseEvent event = new ResponseEvent("test", null, null, responsePDU, null);
107 thingHandler.onResponse(event);
108 verify(thingHandlerCallback, atLeast(1)).stateUpdated(eq(CHANNEL_UID), eq(new DecimalType("12.4")));
109 verifyStatus(ThingStatus.ONLINE);
113 public void testCancelingAsyncRequest() {
114 setup(SnmpBindingConstants.CHANNEL_TYPE_UID_NUMBER, SnmpChannelMode.READ, SnmpDatatype.FLOAT);
115 PDU responsePDU = new PDU(PDU.RESPONSE,
116 Collections.singletonList(new VariableBinding(new OID(TEST_OID), new OctetString("12.4"))));
118 SnmpMock source = new SnmpMock();
120 ResponseEvent event = new ResponseEvent(source, null, null, responsePDU, null);
122 thingHandler.onResponse(event);
123 assertEquals(1, source.cancelCallCounter);
124 verifyStatus(ThingStatus.ONLINE);
127 class SnmpMock extends Snmp {
128 public int cancelCallCounter = 0;
131 public void cancel(PDU request, org.snmp4j.event.ResponseListener listener) {