]> git.basschouten.com Git - openhab-addons.git/blob
d8136e2428e7ef28db68c3592daff918c711ae85
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.snmp.internal;
14
15 import static org.junit.jupiter.api.Assertions.*;
16 import static org.mockito.ArgumentMatchers.eq;
17 import static org.mockito.Mockito.*;
18
19 import java.io.IOException;
20 import java.util.Collections;
21
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;
32
33 /**
34  * Tests cases for {@link SnmpTargetHandler}.
35  *
36  * @author Jan N. Klug - Initial contribution
37  */
38 public class StringChannelTest extends AbstractSnmpTargetHandlerTest {
39
40     @Test
41     public void testCommandsAreProperlyHandledByStringChannel() throws IOException {
42         VariableBinding variable;
43
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());
49
50         variable = handleCommandNumberStringChannel(SnmpBindingConstants.CHANNEL_TYPE_UID_STRING,
51                 SnmpDatatype.IPADDRESS, new StringType(TEST_STRING), false);
52         assertNull(variable);
53
54         variable = handleCommandNumberStringChannel(SnmpBindingConstants.CHANNEL_TYPE_UID_STRING,
55                 SnmpDatatype.IPADDRESS, new DecimalType(-5), false);
56         assertNull(variable);
57
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(' '));
63
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());
69     }
70
71     @Test
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);
80     }
81 }