]> git.basschouten.com Git - openhab-addons.git/blob
39324dea9ddee0677198e59c565d40fce98cb3b7
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2020 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.Assert.*;
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.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;
31
32 /**
33  * Tests cases for {@link SnmpTargetHandler}.
34  *
35  * @author Jan N. Klug - Initial contribution
36  */
37 public class StringChannelTest extends AbstractSnmpTargetHandlerTest {
38
39     @Test
40     public void testCommandsAreProperlyHandledByStringChannel() throws IOException {
41         VariableBinding variable;
42
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());
48
49         variable = handleCommandNumberStringChannel(SnmpBindingConstants.CHANNEL_TYPE_UID_STRING,
50                 SnmpDatatype.IPADDRESS, new StringType(TEST_STRING), false);
51         assertNull(variable);
52
53         variable = handleCommandNumberStringChannel(SnmpBindingConstants.CHANNEL_TYPE_UID_STRING,
54                 SnmpDatatype.IPADDRESS, new DecimalType(-5), false);
55         assertNull(variable);
56
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(' '));
62
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());
68     }
69
70     @Test
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")));
78     }
79 }