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