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