]> git.basschouten.com Git - openhab-addons.git/blob
4b0e29ccc3c09e07639419b6478dfedfe6237d36
[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.*;
17 import static org.mockito.Mockito.*;
18 import static org.openhab.binding.snmp.internal.SnmpBindingConstants.THING_TYPE_TARGET;
19
20 import java.io.IOException;
21 import java.util.HashMap;
22 import java.util.List;
23 import java.util.Map;
24 import java.util.Vector;
25
26 import org.eclipse.jdt.annotation.NonNullByDefault;
27 import org.eclipse.jdt.annotation.Nullable;
28 import org.junit.jupiter.api.AfterEach;
29 import org.mockito.ArgumentCaptor;
30 import org.mockito.Mock;
31 import org.mockito.MockitoAnnotations;
32 import org.openhab.binding.snmp.internal.types.SnmpChannelMode;
33 import org.openhab.binding.snmp.internal.types.SnmpDatatype;
34 import org.openhab.core.config.core.Configuration;
35 import org.openhab.core.library.types.StringType;
36 import org.openhab.core.test.java.JavaTest;
37 import org.openhab.core.thing.Channel;
38 import org.openhab.core.thing.ChannelUID;
39 import org.openhab.core.thing.Thing;
40 import org.openhab.core.thing.ThingStatus;
41 import org.openhab.core.thing.ThingUID;
42 import org.openhab.core.thing.binding.ThingHandlerCallback;
43 import org.openhab.core.thing.binding.builder.ChannelBuilder;
44 import org.openhab.core.thing.binding.builder.ThingBuilder;
45 import org.openhab.core.thing.type.ChannelTypeUID;
46 import org.openhab.core.types.Command;
47 import org.openhab.core.types.State;
48 import org.snmp4j.PDU;
49 import org.snmp4j.event.ResponseEvent;
50 import org.snmp4j.smi.OID;
51 import org.snmp4j.smi.OctetString;
52 import org.snmp4j.smi.Variable;
53 import org.snmp4j.smi.VariableBinding;
54
55 /**
56  * Tests cases for {@link SnmpTargetHandler}.
57  *
58  * @author Jan N. Klug - Initial contribution
59  */
60 @NonNullByDefault
61 public abstract class AbstractSnmpTargetHandlerTest extends JavaTest {
62     protected static final ThingUID THING_UID = new ThingUID(THING_TYPE_TARGET, "testthing");
63     protected static final ChannelUID CHANNEL_UID = new ChannelUID(THING_UID, "testchannel");
64     protected static final String TEST_OID = "1.2.3.4";
65     protected static final String TEST_ADDRESS = "192.168.0.1";
66     protected static final String TEST_STRING = "foo.";
67
68     protected @Mock @NonNullByDefault({}) SnmpServiceImpl snmpService;
69     protected @Mock @NonNullByDefault({}) ThingHandlerCallback thingHandlerCallback;
70
71     protected @NonNullByDefault({}) Thing thing;
72     protected @NonNullByDefault({}) SnmpTargetHandler thingHandler;
73     private @NonNullByDefault({}) AutoCloseable mocks;
74
75     @AfterEach
76     public void after() throws Exception {
77         mocks.close();
78     }
79
80     protected @Nullable VariableBinding handleCommandSwitchChannel(SnmpDatatype datatype, Command command,
81             String onValue, @Nullable String offValue, boolean refresh) throws IOException {
82         setup(SnmpBindingConstants.CHANNEL_TYPE_UID_SWITCH, SnmpChannelMode.WRITE, datatype, onValue, offValue);
83         thingHandler.handleCommand(CHANNEL_UID, command);
84
85         if (refresh) {
86             ArgumentCaptor<PDU> pduCaptor = ArgumentCaptor.forClass(PDU.class);
87             verify(snmpService, times(1)).send(pduCaptor.capture(), any(), eq(null), eq(thingHandler));
88             return pduCaptor.getValue().getVariableBindings().stream().findFirst().orElse(null);
89         } else {
90             verify(snmpService, never()).send(any(), any(), eq(null), eq(thingHandler));
91             return null;
92         }
93     }
94
95     protected @Nullable VariableBinding handleCommandNumberStringChannel(ChannelTypeUID channelTypeUID,
96             SnmpDatatype datatype, Command command, boolean refresh) throws IOException {
97         return handleCommandNumberStringChannel(channelTypeUID, datatype, null, command, refresh);
98     }
99
100     protected @Nullable VariableBinding handleCommandNumberStringChannel(ChannelTypeUID channelTypeUID,
101             SnmpDatatype datatype, @Nullable String unit, Command command, boolean refresh) throws IOException {
102         setup(channelTypeUID, SnmpChannelMode.WRITE, datatype, null, null, null, unit);
103         thingHandler.handleCommand(CHANNEL_UID, command);
104
105         if (refresh) {
106             ArgumentCaptor<PDU> pduCaptor = ArgumentCaptor.forClass(PDU.class);
107             verify(snmpService, times(1)).send(pduCaptor.capture(), any(), eq(null), eq(thingHandler));
108             return pduCaptor.getValue().getVariableBindings().stream().findFirst().orElse(null);
109         } else {
110             verify(snmpService, never()).send(any(), any(), eq(null), eq(thingHandler));
111             return null;
112         }
113     }
114
115     protected void onResponseNumberStringChannel(SnmpChannelMode channelMode, boolean refresh) {
116         setup(SnmpBindingConstants.CHANNEL_TYPE_UID_STRING, channelMode);
117
118         PDU responsePDU = new PDU(PDU.RESPONSE,
119                 List.of(new VariableBinding(new OID(TEST_OID), new OctetString(TEST_STRING))));
120         ResponseEvent event = new ResponseEvent("test", null, null, responsePDU, null);
121
122         thingHandler.onResponse(event);
123
124         if (refresh) {
125             verify(thingHandlerCallback, atLeast(1)).stateUpdated(eq(CHANNEL_UID), eq(new StringType(TEST_STRING)));
126         } else {
127             verify(thingHandlerCallback, never()).stateUpdated(any(), any());
128         }
129     }
130
131     protected @Nullable State onResponseSwitchChannel(SnmpChannelMode channelMode, SnmpDatatype datatype,
132             String onValue, String offValue, Variable value, boolean refresh) {
133         setup(SnmpBindingConstants.CHANNEL_TYPE_UID_SWITCH, channelMode, datatype, onValue, offValue);
134
135         PDU responsePDU = new PDU(PDU.RESPONSE, List.of(new VariableBinding(new OID(TEST_OID), value)));
136         ResponseEvent event = new ResponseEvent("test", null, null, responsePDU, null);
137
138         thingHandler.onResponse(event);
139
140         if (refresh) {
141             ArgumentCaptor<State> stateCaptor = ArgumentCaptor.forClass(State.class);
142             verify(thingHandlerCallback, atLeast(1)).stateUpdated(eq(CHANNEL_UID), stateCaptor.capture());
143             return stateCaptor.getValue();
144         } else {
145             verify(thingHandlerCallback, never()).stateUpdated(any(), any());
146             return null;
147         }
148     }
149
150     protected void refresh(SnmpChannelMode channelMode, boolean refresh) throws IOException {
151         setup(SnmpBindingConstants.CHANNEL_TYPE_UID_STRING, channelMode);
152
153         verifyStatus(ThingStatus.UNKNOWN);
154         verify(snmpService).addCommandResponder(any());
155
156         if (refresh) {
157             ArgumentCaptor<PDU> pduCaptor = ArgumentCaptor.forClass(PDU.class);
158             verify(snmpService, timeout(500).atLeast(1)).send(pduCaptor.capture(), any(), eq(null), eq(thingHandler));
159             Vector<? extends VariableBinding> variables = pduCaptor.getValue().getVariableBindings();
160             assertTrue(variables.stream().filter(v -> v.getOid().toDottedString().equals(TEST_OID)).findFirst()
161                     .isPresent());
162         } else {
163             verify(snmpService, never()).send(any(), any(), eq(null), eq(thingHandler));
164         }
165     }
166
167     protected void setup(ChannelTypeUID channelTypeUID, SnmpChannelMode channelMode) {
168         setup(channelTypeUID, channelMode, null);
169     }
170
171     protected void setup(ChannelTypeUID channelTypeUID, SnmpChannelMode channelMode, @Nullable SnmpDatatype datatype) {
172         setup(channelTypeUID, channelMode, datatype, null, null);
173     }
174
175     protected void setup(ChannelTypeUID channelTypeUID, SnmpChannelMode channelMode, @Nullable SnmpDatatype datatype,
176             @Nullable String onValue, @Nullable String offValue) {
177         setup(channelTypeUID, channelMode, datatype, onValue, offValue, null);
178     }
179
180     protected void setup(ChannelTypeUID channelTypeUID, SnmpChannelMode channelMode, @Nullable SnmpDatatype datatype,
181             @Nullable String onValue, @Nullable String offValue, @Nullable String exceptionValue) {
182         setup(channelTypeUID, channelMode, datatype, onValue, offValue, exceptionValue, null);
183     }
184
185     protected void setup(ChannelTypeUID channelTypeUID, SnmpChannelMode channelMode, @Nullable SnmpDatatype datatype,
186             @Nullable String onValue, @Nullable String offValue, @Nullable String exceptionValue,
187             @Nullable String unit) {
188         Map<String, Object> channelConfig = new HashMap<>();
189         Map<String, Object> thingConfig = new HashMap<>();
190         mocks = MockitoAnnotations.openMocks(this);
191
192         thingConfig.put("hostname", "localhost");
193
194         ThingBuilder thingBuilder = ThingBuilder.create(THING_TYPE_TARGET, THING_UID).withLabel("Test thing")
195                 .withConfiguration(new Configuration(thingConfig));
196
197         String itemType = SnmpBindingConstants.CHANNEL_TYPE_UID_NUMBER.equals(channelTypeUID) ? "Number" : "String";
198         channelConfig.put("oid", TEST_OID);
199         channelConfig.put("mode", channelMode.name());
200         if (datatype != null) {
201             channelConfig.put("datatype", datatype.name());
202         }
203         if (onValue != null) {
204             channelConfig.put("onvalue", onValue);
205         }
206         if (offValue != null) {
207             channelConfig.put("offvalue", offValue);
208         }
209         if (exceptionValue != null) {
210             channelConfig.put("exceptionValue", exceptionValue);
211         }
212         if (unit != null) {
213             channelConfig.put("unit", unit);
214         }
215         Channel channel = ChannelBuilder.create(CHANNEL_UID, itemType).withType(channelTypeUID)
216                 .withConfiguration(new Configuration(channelConfig)).build();
217         thingBuilder.withChannel(channel);
218
219         thing = thingBuilder.build();
220         thingHandler = new SnmpTargetHandler(thing, snmpService);
221
222         thingHandler.getThing().setHandler(thingHandler);
223         thingHandler.setCallback(thingHandlerCallback);
224
225         doAnswer(answer -> {
226             ((Thing) answer.getArgument(0)).setStatusInfo(answer.getArgument(1));
227             return null;
228         }).when(thingHandlerCallback).statusUpdated(any(), any());
229
230         thingHandler.initialize();
231
232         verifyStatus(ThingStatus.UNKNOWN);
233     }
234
235     protected void verifyStatus(ThingStatus status) {
236         waitForAssert(() -> assertEquals(status, thingHandler.getThing().getStatusInfo().getStatus()));
237     }
238 }