]> git.basschouten.com Git - openhab-addons.git/blob
2e772d589bc7669702722b91614cdd5bc703648d
[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.OnOffType;
25 import org.openhab.core.library.types.StringType;
26 import org.snmp4j.PDU;
27 import org.snmp4j.event.ResponseEvent;
28 import org.snmp4j.smi.Counter64;
29 import org.snmp4j.smi.Integer32;
30 import org.snmp4j.smi.OID;
31 import org.snmp4j.smi.OctetString;
32 import org.snmp4j.smi.UnsignedInteger32;
33 import org.snmp4j.smi.VariableBinding;
34
35 /**
36  * Tests cases for {@link SnmpTargetHandler}.
37  *
38  * @author Jan N. Klug - Initial contribution
39  */
40 public class SnmpTargetHandlerTest extends AbstractSnmpTargetHandlerTest {
41
42     @Test
43     public void testChannelsProperlyRefreshing() throws IOException {
44         refresh(SnmpChannelMode.READ, true);
45         refresh(SnmpChannelMode.READ_WRITE, true);
46         refresh(SnmpChannelMode.WRITE, false);
47         refresh(SnmpChannelMode.TRAP, false);
48     }
49
50     @Test
51     public void testChannelsProperlyUpdate() throws IOException {
52         onResponseNumberStringChannel(SnmpChannelMode.READ, true);
53         onResponseNumberStringChannel(SnmpChannelMode.READ_WRITE, true);
54         onResponseNumberStringChannel(SnmpChannelMode.WRITE, false);
55         onResponseNumberStringChannel(SnmpChannelMode.TRAP, false);
56         assertEquals(OnOffType.ON, onResponseSwitchChannel(SnmpChannelMode.READ, SnmpDatatype.STRING, "on", "off",
57                 new OctetString("on"), true));
58         assertEquals(OnOffType.OFF, onResponseSwitchChannel(SnmpChannelMode.READ_WRITE, SnmpDatatype.INT32, "1", "2",
59                 new Integer32(2), true));
60         assertNull(onResponseSwitchChannel(SnmpChannelMode.WRITE, SnmpDatatype.STRING, "on", "off",
61                 new OctetString("on"), false));
62         assertNull(
63                 onResponseSwitchChannel(SnmpChannelMode.TRAP, SnmpDatatype.INT32, "1", "2", new Integer32(2), false));
64     }
65
66     @Test
67     public void testCommandsAreProperlyHandledByNumberChannel() throws IOException {
68         VariableBinding variable;
69         variable = handleCommandNumberStringChannel(SnmpBindingConstants.CHANNEL_TYPE_UID_NUMBER, SnmpDatatype.INT32,
70                 new DecimalType(-5), true);
71         assertEquals(new OID(TEST_OID), variable.getOid());
72         assertTrue(variable.getVariable() instanceof Integer32);
73         assertEquals(-5, ((Integer32) variable.getVariable()).toInt());
74
75         variable = handleCommandNumberStringChannel(SnmpBindingConstants.CHANNEL_TYPE_UID_NUMBER, SnmpDatatype.UINT32,
76                 new DecimalType(10000), true);
77         assertEquals(new OID(TEST_OID), variable.getOid());
78         assertTrue(variable.getVariable() instanceof UnsignedInteger32);
79         assertEquals(10000, ((UnsignedInteger32) variable.getVariable()).toInt());
80
81         variable = handleCommandNumberStringChannel(SnmpBindingConstants.CHANNEL_TYPE_UID_NUMBER,
82                 SnmpDatatype.COUNTER64, new DecimalType(10000), true);
83         assertEquals(new OID(TEST_OID), variable.getOid());
84         assertTrue(variable.getVariable() instanceof Counter64);
85         assertEquals(10000, ((Counter64) variable.getVariable()).toInt());
86
87         variable = handleCommandNumberStringChannel(SnmpBindingConstants.CHANNEL_TYPE_UID_NUMBER, SnmpDatatype.FLOAT,
88                 new DecimalType("12.4"), true);
89         assertEquals(new OID(TEST_OID), variable.getOid());
90         assertTrue(variable.getVariable() instanceof OctetString);
91         assertEquals("12.4", variable.getVariable().toString());
92
93         variable = handleCommandNumberStringChannel(SnmpBindingConstants.CHANNEL_TYPE_UID_NUMBER, SnmpDatatype.INT32,
94                 new StringType(TEST_STRING), false);
95         assertNull(variable);
96     }
97
98     @Test
99     public void testNumberChannelsProperlyUpdatingFloatValue() throws IOException {
100         setup(SnmpBindingConstants.CHANNEL_TYPE_UID_NUMBER, SnmpChannelMode.READ, SnmpDatatype.FLOAT);
101         PDU responsePDU = new PDU(PDU.RESPONSE,
102                 Collections.singletonList(new VariableBinding(new OID(TEST_OID), new OctetString("12.4"))));
103         ResponseEvent event = new ResponseEvent("test", null, null, responsePDU, null);
104         thingHandler.onResponse(event);
105         verify(thingHandlerCallback, atLeast(1)).stateUpdated(eq(CHANNEL_UID), eq(new DecimalType("12.4")));
106     }
107 }