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.List;
22 import org.eclipse.jdt.annotation.NonNullByDefault;
23 import org.junit.jupiter.api.Test;
24 import org.openhab.binding.snmp.internal.types.SnmpChannelMode;
25 import org.openhab.binding.snmp.internal.types.SnmpDatatype;
26 import org.openhab.core.library.types.DecimalType;
27 import org.openhab.core.library.types.StringType;
28 import org.openhab.core.thing.ThingStatus;
29 import org.snmp4j.PDU;
30 import org.snmp4j.event.ResponseEvent;
31 import org.snmp4j.smi.IpAddress;
32 import org.snmp4j.smi.OID;
33 import org.snmp4j.smi.OctetString;
34 import org.snmp4j.smi.VariableBinding;
37 * Tests cases for {@link SnmpTargetHandler}.
39 * @author Jan N. Klug - Initial contribution
42 public class StringChannelTest extends AbstractSnmpTargetHandlerTest {
45 public void testCommandsAreProperlyHandledByStringChannel() throws IOException {
46 VariableBinding variable;
48 variable = handleCommandNumberStringChannel(SnmpBindingConstants.CHANNEL_TYPE_UID_STRING, SnmpDatatype.STRING,
49 new StringType(TEST_STRING), true);
51 if (variable == null) {
52 fail("'variable' is null");
56 assertEquals(new OID(TEST_OID), variable.getOid());
57 assertTrue(variable.getVariable() instanceof OctetString);
58 assertEquals(TEST_STRING, ((OctetString) variable.getVariable()).toString());
60 variable = handleCommandNumberStringChannel(SnmpBindingConstants.CHANNEL_TYPE_UID_STRING,
61 SnmpDatatype.IPADDRESS, new StringType(TEST_STRING), false);
64 variable = handleCommandNumberStringChannel(SnmpBindingConstants.CHANNEL_TYPE_UID_STRING,
65 SnmpDatatype.IPADDRESS, new DecimalType(-5), false);
68 variable = handleCommandNumberStringChannel(SnmpBindingConstants.CHANNEL_TYPE_UID_STRING,
69 SnmpDatatype.HEXSTRING, new StringType("AA bf 11"), true);
71 if (variable == null) {
72 fail("'variable' is null");
76 assertEquals(new OID(TEST_OID), variable.getOid());
77 assertTrue(variable.getVariable() instanceof OctetString);
78 assertEquals("aa bf 11", ((OctetString) variable.getVariable()).toHexString(' '));
80 variable = handleCommandNumberStringChannel(SnmpBindingConstants.CHANNEL_TYPE_UID_STRING,
81 SnmpDatatype.IPADDRESS, new StringType(TEST_ADDRESS), true);
83 if (variable == null) {
84 fail("'variable' is null");
88 assertEquals(new OID(TEST_OID), variable.getOid());
89 assertTrue(variable.getVariable() instanceof IpAddress);
90 assertEquals(TEST_ADDRESS, ((IpAddress) variable.getVariable()).toString());
94 public void testStringChannelsProperlyUpdatingOnHexString() throws IOException {
95 setup(SnmpBindingConstants.CHANNEL_TYPE_UID_STRING, SnmpChannelMode.READ, SnmpDatatype.HEXSTRING);
96 PDU responsePDU = new PDU(PDU.RESPONSE,
97 List.of(new VariableBinding(new OID(TEST_OID), OctetString.fromHexStringPairs("aa11bb"))));
98 ResponseEvent event = new ResponseEvent("test", null, null, responsePDU, null);
99 thingHandler.onResponse(event);
100 verify(thingHandlerCallback, atLeast(1)).stateUpdated(eq(CHANNEL_UID), eq(new StringType("aa 11 bb")));
101 verifyStatus(ThingStatus.ONLINE);