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