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