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.StringType;
25 import org.openhab.core.thing.ThingStatus;
26 import org.snmp4j.PDU;
27 import org.snmp4j.event.ResponseEvent;
28 import org.snmp4j.smi.IpAddress;
29 import org.snmp4j.smi.OID;
30 import org.snmp4j.smi.OctetString;
31 import org.snmp4j.smi.VariableBinding;
34 * Tests cases for {@link SnmpTargetHandler}.
36 * @author Jan N. Klug - Initial contribution
38 public class StringChannelTest extends AbstractSnmpTargetHandlerTest {
41 public void testCommandsAreProperlyHandledByStringChannel() throws IOException {
42 VariableBinding variable;
44 variable = handleCommandNumberStringChannel(SnmpBindingConstants.CHANNEL_TYPE_UID_STRING, SnmpDatatype.STRING,
45 new StringType(TEST_STRING), true);
46 assertEquals(new OID(TEST_OID), variable.getOid());
47 assertTrue(variable.getVariable() instanceof OctetString);
48 assertEquals(TEST_STRING, ((OctetString) variable.getVariable()).toString());
50 variable = handleCommandNumberStringChannel(SnmpBindingConstants.CHANNEL_TYPE_UID_STRING,
51 SnmpDatatype.IPADDRESS, new StringType(TEST_STRING), false);
54 variable = handleCommandNumberStringChannel(SnmpBindingConstants.CHANNEL_TYPE_UID_STRING,
55 SnmpDatatype.IPADDRESS, new DecimalType(-5), false);
58 variable = handleCommandNumberStringChannel(SnmpBindingConstants.CHANNEL_TYPE_UID_STRING,
59 SnmpDatatype.HEXSTRING, new StringType("AA bf 11"), true);
60 assertEquals(new OID(TEST_OID), variable.getOid());
61 assertTrue(variable.getVariable() instanceof OctetString);
62 assertEquals("aa bf 11", ((OctetString) variable.getVariable()).toHexString(' '));
64 variable = handleCommandNumberStringChannel(SnmpBindingConstants.CHANNEL_TYPE_UID_STRING,
65 SnmpDatatype.IPADDRESS, new StringType(TEST_ADDRESS), true);
66 assertEquals(new OID(TEST_OID), variable.getOid());
67 assertTrue(variable.getVariable() instanceof IpAddress);
68 assertEquals(TEST_ADDRESS, ((IpAddress) variable.getVariable()).toString());
72 public void testStringChannelsProperlyUpdatingOnHexString() throws IOException {
73 setup(SnmpBindingConstants.CHANNEL_TYPE_UID_STRING, SnmpChannelMode.READ, SnmpDatatype.HEXSTRING);
74 PDU responsePDU = new PDU(PDU.RESPONSE, Collections
75 .singletonList(new VariableBinding(new OID(TEST_OID), OctetString.fromHexStringPairs("aa11bb"))));
76 ResponseEvent event = new ResponseEvent("test", null, null, responsePDU, null);
77 thingHandler.onResponse(event);
78 verify(thingHandlerCallback, atLeast(1)).stateUpdated(eq(CHANNEL_UID), eq(new StringType("aa 11 bb")));
79 verifyStatus(ThingStatus.ONLINE);