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.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.QuantityType;
26 import org.openhab.core.library.types.StringType;
27 import org.openhab.core.library.unit.SIUnits;
28 import org.openhab.core.thing.ThingStatus;
29 import org.snmp4j.PDU;
30 import org.snmp4j.Snmp;
31 import org.snmp4j.event.ResponseEvent;
32 import org.snmp4j.smi.Counter64;
33 import org.snmp4j.smi.Integer32;
34 import org.snmp4j.smi.OID;
35 import org.snmp4j.smi.OctetString;
36 import org.snmp4j.smi.UnsignedInteger32;
37 import org.snmp4j.smi.VariableBinding;
40 * Tests cases for {@link SnmpTargetHandler}.
42 * @author Jan N. Klug - Initial contribution
44 public class SnmpTargetHandlerTest extends AbstractSnmpTargetHandlerTest {
47 public void testChannelsProperlyRefreshing() throws IOException {
48 refresh(SnmpChannelMode.READ, true);
49 refresh(SnmpChannelMode.READ_WRITE, true);
50 refresh(SnmpChannelMode.WRITE, false);
51 refresh(SnmpChannelMode.TRAP, false);
55 public void testChannelsProperlyUpdate() throws IOException {
56 onResponseNumberStringChannel(SnmpChannelMode.READ, true);
57 onResponseNumberStringChannel(SnmpChannelMode.READ_WRITE, true);
58 onResponseNumberStringChannel(SnmpChannelMode.WRITE, false);
59 onResponseNumberStringChannel(SnmpChannelMode.TRAP, false);
60 assertEquals(OnOffType.ON, onResponseSwitchChannel(SnmpChannelMode.READ, SnmpDatatype.STRING, "on", "off",
61 new OctetString("on"), true));
62 assertEquals(OnOffType.OFF, onResponseSwitchChannel(SnmpChannelMode.READ_WRITE, SnmpDatatype.INT32, "1", "2",
63 new Integer32(2), true));
64 assertNull(onResponseSwitchChannel(SnmpChannelMode.WRITE, SnmpDatatype.STRING, "on", "off",
65 new OctetString("on"), false));
67 onResponseSwitchChannel(SnmpChannelMode.TRAP, SnmpDatatype.INT32, "1", "2", new Integer32(2), false));
68 verifyStatus(ThingStatus.ONLINE);
72 public void testCommandsAreProperlyHandledByNumberChannel() throws IOException {
73 VariableBinding variable;
74 variable = handleCommandNumberStringChannel(SnmpBindingConstants.CHANNEL_TYPE_UID_NUMBER, SnmpDatatype.INT32,
75 new DecimalType(-5), true);
76 assertEquals(new OID(TEST_OID), variable.getOid());
77 assertTrue(variable.getVariable() instanceof Integer32);
78 assertEquals(-5, ((Integer32) variable.getVariable()).toInt());
80 variable = handleCommandNumberStringChannel(SnmpBindingConstants.CHANNEL_TYPE_UID_NUMBER, SnmpDatatype.UINT32,
81 new DecimalType(10000), true);
82 assertEquals(new OID(TEST_OID), variable.getOid());
83 assertTrue(variable.getVariable() instanceof UnsignedInteger32);
84 assertEquals(10000, ((UnsignedInteger32) variable.getVariable()).toInt());
86 variable = handleCommandNumberStringChannel(SnmpBindingConstants.CHANNEL_TYPE_UID_NUMBER,
87 SnmpDatatype.COUNTER64, new DecimalType(10000), true);
88 assertEquals(new OID(TEST_OID), variable.getOid());
89 assertTrue(variable.getVariable() instanceof Counter64);
90 assertEquals(10000, ((Counter64) variable.getVariable()).toInt());
92 variable = handleCommandNumberStringChannel(SnmpBindingConstants.CHANNEL_TYPE_UID_NUMBER, SnmpDatatype.FLOAT,
93 new DecimalType("12.4"), true);
94 assertEquals(new OID(TEST_OID), variable.getOid());
95 assertTrue(variable.getVariable() instanceof OctetString);
96 assertEquals("12.4", variable.getVariable().toString());
98 variable = handleCommandNumberStringChannel(SnmpBindingConstants.CHANNEL_TYPE_UID_NUMBER, SnmpDatatype.INT32,
99 new StringType(TEST_STRING), false);
100 assertNull(variable);
104 public void testNumberChannelsProperlyUpdatingFloatValue() throws IOException {
105 setup(SnmpBindingConstants.CHANNEL_TYPE_UID_NUMBER, SnmpChannelMode.READ, SnmpDatatype.FLOAT);
106 PDU responsePDU = new PDU(PDU.RESPONSE,
107 Collections.singletonList(new VariableBinding(new OID(TEST_OID), new OctetString("12.4"))));
108 ResponseEvent event = new ResponseEvent("test", null, null, responsePDU, null);
109 thingHandler.onResponse(event);
110 verify(thingHandlerCallback, atLeast(1)).stateUpdated(eq(CHANNEL_UID), eq(new DecimalType("12.4")));
111 verifyStatus(ThingStatus.ONLINE);
115 public void testNumberChannelsProperlyHandlingUnits() throws IOException {
116 setup(SnmpBindingConstants.CHANNEL_TYPE_UID_NUMBER, SnmpChannelMode.READ, SnmpDatatype.FLOAT, null, null, null,
118 PDU responsePDU = new PDU(PDU.RESPONSE,
119 Collections.singletonList(new VariableBinding(new OID(TEST_OID), new OctetString("12.4"))));
120 ResponseEvent event = new ResponseEvent("test", null, null, responsePDU, null);
121 thingHandler.onResponse(event);
122 verify(thingHandlerCallback, atLeast(1)).stateUpdated(eq(CHANNEL_UID),
123 eq(new QuantityType<>(12.4, SIUnits.CELSIUS)));
124 verifyStatus(ThingStatus.ONLINE);
128 public void testCancelingAsyncRequest() {
129 setup(SnmpBindingConstants.CHANNEL_TYPE_UID_NUMBER, SnmpChannelMode.READ, SnmpDatatype.FLOAT);
130 PDU responsePDU = new PDU(PDU.RESPONSE,
131 Collections.singletonList(new VariableBinding(new OID(TEST_OID), new OctetString("12.4"))));
133 SnmpMock source = new SnmpMock();
135 ResponseEvent event = new ResponseEvent(source, null, null, responsePDU, null);
137 thingHandler.onResponse(event);
138 assertEquals(1, source.cancelCallCounter);
139 verifyStatus(ThingStatus.ONLINE);
142 class SnmpMock extends Snmp {
143 public int cancelCallCounter = 0;
146 public void cancel(PDU request, org.snmp4j.event.ResponseListener listener) {