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