]> git.basschouten.com Git - openhab-addons.git/blob
0c7a8d5ddbe93353abd24fd029e37d2516640f0c
[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.List;
21
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;
35
36 /**
37  * Tests cases for {@link SnmpTargetHandler}.
38  *
39  * @author Jan N. Klug - Initial contribution
40  */
41 @NonNullByDefault
42 public class StringChannelTest extends AbstractSnmpTargetHandlerTest {
43
44     @Test
45     public void testCommandsAreProperlyHandledByStringChannel() throws IOException {
46         VariableBinding variable;
47
48         variable = handleCommandNumberStringChannel(SnmpBindingConstants.CHANNEL_TYPE_UID_STRING, SnmpDatatype.STRING,
49                 new StringType(TEST_STRING), true);
50
51         if (variable == null) {
52             fail("'variable' is null");
53             return;
54         }
55
56         assertEquals(new OID(TEST_OID), variable.getOid());
57         assertTrue(variable.getVariable() instanceof OctetString);
58         assertEquals(TEST_STRING, ((OctetString) variable.getVariable()).toString());
59
60         variable = handleCommandNumberStringChannel(SnmpBindingConstants.CHANNEL_TYPE_UID_STRING,
61                 SnmpDatatype.IPADDRESS, new StringType(TEST_STRING), false);
62         assertNull(variable);
63
64         variable = handleCommandNumberStringChannel(SnmpBindingConstants.CHANNEL_TYPE_UID_STRING,
65                 SnmpDatatype.IPADDRESS, new DecimalType(-5), false);
66         assertNull(variable);
67
68         variable = handleCommandNumberStringChannel(SnmpBindingConstants.CHANNEL_TYPE_UID_STRING,
69                 SnmpDatatype.HEXSTRING, new StringType("AA bf 11"), true);
70
71         if (variable == null) {
72             fail("'variable' is null");
73             return;
74         }
75
76         assertEquals(new OID(TEST_OID), variable.getOid());
77         assertTrue(variable.getVariable() instanceof OctetString);
78         assertEquals("aa bf 11", ((OctetString) variable.getVariable()).toHexString(' '));
79
80         variable = handleCommandNumberStringChannel(SnmpBindingConstants.CHANNEL_TYPE_UID_STRING,
81                 SnmpDatatype.IPADDRESS, new StringType(TEST_ADDRESS), true);
82
83         if (variable == null) {
84             fail("'variable' is null");
85             return;
86         }
87
88         assertEquals(new OID(TEST_OID), variable.getOid());
89         assertTrue(variable.getVariable() instanceof IpAddress);
90         assertEquals(TEST_ADDRESS, ((IpAddress) variable.getVariable()).toString());
91     }
92
93     @Test
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);
102     }
103 }