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.snmp4j.PDU;
27 import org.snmp4j.Snmp;
28 import org.snmp4j.event.ResponseEvent;
29 import org.snmp4j.smi.Counter64;
30 import org.snmp4j.smi.Integer32;
31 import org.snmp4j.smi.OID;
32 import org.snmp4j.smi.OctetString;
33 import org.snmp4j.smi.UnsignedInteger32;
34 import org.snmp4j.smi.VariableBinding;
37 * Tests cases for {@link SnmpTargetHandler}.
39 * @author Jan N. Klug - Initial contribution
41 public class SnmpTargetHandlerTest extends AbstractSnmpTargetHandlerTest {
44 public void testChannelsProperlyRefreshing() throws IOException {
45 refresh(SnmpChannelMode.READ, true);
46 refresh(SnmpChannelMode.READ_WRITE, true);
47 refresh(SnmpChannelMode.WRITE, false);
48 refresh(SnmpChannelMode.TRAP, false);
52 public void testChannelsProperlyUpdate() throws IOException {
53 onResponseNumberStringChannel(SnmpChannelMode.READ, true);
54 onResponseNumberStringChannel(SnmpChannelMode.READ_WRITE, true);
55 onResponseNumberStringChannel(SnmpChannelMode.WRITE, false);
56 onResponseNumberStringChannel(SnmpChannelMode.TRAP, false);
57 assertEquals(OnOffType.ON, onResponseSwitchChannel(SnmpChannelMode.READ, SnmpDatatype.STRING, "on", "off",
58 new OctetString("on"), true));
59 assertEquals(OnOffType.OFF, onResponseSwitchChannel(SnmpChannelMode.READ_WRITE, SnmpDatatype.INT32, "1", "2",
60 new Integer32(2), true));
61 assertNull(onResponseSwitchChannel(SnmpChannelMode.WRITE, SnmpDatatype.STRING, "on", "off",
62 new OctetString("on"), false));
64 onResponseSwitchChannel(SnmpChannelMode.TRAP, SnmpDatatype.INT32, "1", "2", new Integer32(2), false));
68 public void testCommandsAreProperlyHandledByNumberChannel() throws IOException {
69 VariableBinding variable;
70 variable = handleCommandNumberStringChannel(SnmpBindingConstants.CHANNEL_TYPE_UID_NUMBER, SnmpDatatype.INT32,
71 new DecimalType(-5), true);
72 assertEquals(new OID(TEST_OID), variable.getOid());
73 assertTrue(variable.getVariable() instanceof Integer32);
74 assertEquals(-5, ((Integer32) variable.getVariable()).toInt());
76 variable = handleCommandNumberStringChannel(SnmpBindingConstants.CHANNEL_TYPE_UID_NUMBER, SnmpDatatype.UINT32,
77 new DecimalType(10000), true);
78 assertEquals(new OID(TEST_OID), variable.getOid());
79 assertTrue(variable.getVariable() instanceof UnsignedInteger32);
80 assertEquals(10000, ((UnsignedInteger32) variable.getVariable()).toInt());
82 variable = handleCommandNumberStringChannel(SnmpBindingConstants.CHANNEL_TYPE_UID_NUMBER,
83 SnmpDatatype.COUNTER64, new DecimalType(10000), true);
84 assertEquals(new OID(TEST_OID), variable.getOid());
85 assertTrue(variable.getVariable() instanceof Counter64);
86 assertEquals(10000, ((Counter64) variable.getVariable()).toInt());
88 variable = handleCommandNumberStringChannel(SnmpBindingConstants.CHANNEL_TYPE_UID_NUMBER, SnmpDatatype.FLOAT,
89 new DecimalType("12.4"), true);
90 assertEquals(new OID(TEST_OID), variable.getOid());
91 assertTrue(variable.getVariable() instanceof OctetString);
92 assertEquals("12.4", variable.getVariable().toString());
94 variable = handleCommandNumberStringChannel(SnmpBindingConstants.CHANNEL_TYPE_UID_NUMBER, SnmpDatatype.INT32,
95 new StringType(TEST_STRING), false);
100 public void testNumberChannelsProperlyUpdatingFloatValue() throws IOException {
101 setup(SnmpBindingConstants.CHANNEL_TYPE_UID_NUMBER, SnmpChannelMode.READ, SnmpDatatype.FLOAT);
102 PDU responsePDU = new PDU(PDU.RESPONSE,
103 Collections.singletonList(new VariableBinding(new OID(TEST_OID), new OctetString("12.4"))));
104 ResponseEvent event = new ResponseEvent("test", null, null, responsePDU, null);
105 thingHandler.onResponse(event);
106 verify(thingHandlerCallback, atLeast(1)).stateUpdated(eq(CHANNEL_UID), eq(new DecimalType("12.4")));
110 public void testCancelingAsyncRequest() {
111 setup(SnmpBindingConstants.CHANNEL_TYPE_UID_NUMBER, SnmpChannelMode.READ, SnmpDatatype.FLOAT);
112 PDU responsePDU = new PDU(PDU.RESPONSE,
113 Collections.singletonList(new VariableBinding(new OID(TEST_OID), new OctetString("12.4"))));
115 SnmpMock source = new SnmpMock();
117 ResponseEvent event = new ResponseEvent(source, null, null, responsePDU, null);
119 thingHandler.onResponse(event);
120 assertEquals(1, source.cancelCallCounter);
123 class SnmpMock extends Snmp {
124 public int cancelCallCounter = 0;
127 public void cancel(PDU request, org.snmp4j.event.ResponseListener listener) {