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.eclipse.jdt.annotation.NonNullByDefault;
23 import org.eclipse.jdt.annotation.Nullable;
24 import org.junit.jupiter.api.Test;
25 import org.openhab.binding.snmp.internal.types.SnmpChannelMode;
26 import org.openhab.binding.snmp.internal.types.SnmpDatatype;
27 import org.openhab.core.library.types.DecimalType;
28 import org.openhab.core.library.types.OnOffType;
29 import org.openhab.core.library.types.QuantityType;
30 import org.openhab.core.library.types.StringType;
31 import org.openhab.core.thing.ThingStatus;
32 import org.snmp4j.PDU;
33 import org.snmp4j.Snmp;
34 import org.snmp4j.event.ResponseEvent;
35 import org.snmp4j.smi.Counter64;
36 import org.snmp4j.smi.Integer32;
37 import org.snmp4j.smi.OID;
38 import org.snmp4j.smi.OctetString;
39 import org.snmp4j.smi.UnsignedInteger32;
40 import org.snmp4j.smi.VariableBinding;
43 * Tests cases for {@link SnmpTargetHandler}.
45 * @author Jan N. Klug - Initial contribution
48 public class SnmpTargetHandlerTest extends AbstractSnmpTargetHandlerTest {
51 public void testChannelsProperlyRefreshing() throws IOException {
52 refresh(SnmpChannelMode.READ, true);
53 refresh(SnmpChannelMode.READ_WRITE, true);
54 refresh(SnmpChannelMode.WRITE, false);
55 refresh(SnmpChannelMode.TRAP, false);
59 public void testChannelsProperlyUpdate() {
60 onResponseNumberStringChannel(SnmpChannelMode.READ, true);
61 onResponseNumberStringChannel(SnmpChannelMode.READ_WRITE, true);
62 onResponseNumberStringChannel(SnmpChannelMode.WRITE, false);
63 onResponseNumberStringChannel(SnmpChannelMode.TRAP, false);
64 assertEquals(OnOffType.ON, onResponseSwitchChannel(SnmpChannelMode.READ, SnmpDatatype.STRING, "on", "off",
65 new OctetString("on"), true));
66 assertEquals(OnOffType.OFF, onResponseSwitchChannel(SnmpChannelMode.READ_WRITE, SnmpDatatype.INT32, "1", "2",
67 new Integer32(2), true));
68 assertNull(onResponseSwitchChannel(SnmpChannelMode.WRITE, SnmpDatatype.STRING, "on", "off",
69 new OctetString("on"), false));
71 onResponseSwitchChannel(SnmpChannelMode.TRAP, SnmpDatatype.INT32, "1", "2", new Integer32(2), false));
72 verifyStatus(ThingStatus.ONLINE);
76 public void testCommandsAreProperlyHandledByNumberChannel() throws IOException {
77 VariableBinding variable;
78 variable = handleCommandNumberStringChannel(SnmpBindingConstants.CHANNEL_TYPE_UID_NUMBER, SnmpDatatype.INT32,
79 new DecimalType(-5), true);
81 if (variable == null) {
82 fail("'variable' is null");
86 assertEquals(new OID(TEST_OID), variable.getOid());
87 assertTrue(variable.getVariable() instanceof Integer32);
88 assertEquals(-5, ((Integer32) variable.getVariable()).toInt());
90 variable = handleCommandNumberStringChannel(SnmpBindingConstants.CHANNEL_TYPE_UID_NUMBER, SnmpDatatype.UINT32,
91 new DecimalType(10000), true);
93 if (variable == null) {
94 fail("'variable' is null");
98 assertEquals(new OID(TEST_OID), variable.getOid());
99 assertTrue(variable.getVariable() instanceof UnsignedInteger32);
100 assertEquals(10000, ((UnsignedInteger32) variable.getVariable()).toInt());
102 variable = handleCommandNumberStringChannel(SnmpBindingConstants.CHANNEL_TYPE_UID_NUMBER,
103 SnmpDatatype.COUNTER64, new DecimalType(10000), true);
105 if (variable == null) {
106 fail("'variable' is null");
110 assertEquals(new OID(TEST_OID), variable.getOid());
111 assertTrue(variable.getVariable() instanceof Counter64);
112 assertEquals(10000, ((Counter64) variable.getVariable()).toInt());
114 variable = handleCommandNumberStringChannel(SnmpBindingConstants.CHANNEL_TYPE_UID_NUMBER, SnmpDatatype.FLOAT,
115 new DecimalType("12.4"), true);
117 if (variable == null) {
118 fail("'variable' is null");
122 assertEquals(new OID(TEST_OID), variable.getOid());
123 assertTrue(variable.getVariable() instanceof OctetString);
124 assertEquals("12.4", variable.getVariable().toString());
126 variable = handleCommandNumberStringChannel(SnmpBindingConstants.CHANNEL_TYPE_UID_NUMBER, SnmpDatatype.FLOAT,
127 "°C", new QuantityType<>("50 °F"), true);
129 if (variable == null) {
130 fail("'variable' is null");
134 assertEquals(new OID(TEST_OID), variable.getOid());
135 assertTrue(variable.getVariable() instanceof OctetString);
136 assertEquals("10.00", variable.getVariable().toString().substring(0, 5));
138 variable = handleCommandNumberStringChannel(SnmpBindingConstants.CHANNEL_TYPE_UID_NUMBER, SnmpDatatype.INT32,
139 null, new StringType(TEST_STRING), false);
140 assertNull(variable);
144 public void testNumberChannelsProperlyUpdatingFloatValue() throws IOException {
145 setup(SnmpBindingConstants.CHANNEL_TYPE_UID_NUMBER, SnmpChannelMode.READ, SnmpDatatype.FLOAT);
146 PDU responsePDU = new PDU(PDU.RESPONSE,
147 Collections.singletonList(new VariableBinding(new OID(TEST_OID), new OctetString("12.4"))));
148 ResponseEvent event = new ResponseEvent("test", null, null, responsePDU, null);
149 thingHandler.onResponse(event);
150 verify(thingHandlerCallback, atLeast(1)).stateUpdated(eq(CHANNEL_UID), eq(new DecimalType("12.4")));
151 verifyStatus(ThingStatus.ONLINE);
155 public void testCancelingAsyncRequest() {
156 setup(SnmpBindingConstants.CHANNEL_TYPE_UID_NUMBER, SnmpChannelMode.READ, SnmpDatatype.FLOAT);
157 PDU responsePDU = new PDU(PDU.RESPONSE,
158 Collections.singletonList(new VariableBinding(new OID(TEST_OID), new OctetString("12.4"))));
160 SnmpMock source = new SnmpMock();
162 ResponseEvent event = new ResponseEvent(source, null, null, responsePDU, null);
164 thingHandler.onResponse(event);
165 assertEquals(1, source.cancelCallCounter);
166 verifyStatus(ThingStatus.ONLINE);
169 static class SnmpMock extends Snmp {
170 public int cancelCallCounter = 0;
173 public void cancel(@Nullable PDU request, org.snmp4j.event.@Nullable ResponseListener listener) {