2 * Copyright (c) 2010-2024 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.snmp.internal;
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;
20 import java.io.IOException;
21 import java.util.HashMap;
22 import java.util.List;
24 import java.util.Vector;
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;
56 * Tests cases for {@link SnmpTargetHandler}.
58 * @author Jan N. Klug - Initial contribution
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.";
68 protected @Mock @NonNullByDefault({}) SnmpServiceImpl snmpService;
69 protected @Mock @NonNullByDefault({}) ThingHandlerCallback thingHandlerCallback;
71 protected @NonNullByDefault({}) Thing thing;
72 protected @NonNullByDefault({}) SnmpTargetHandler thingHandler;
73 private @NonNullByDefault({}) AutoCloseable mocks;
76 public void after() throws Exception {
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);
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);
90 verify(snmpService, never()).send(any(), any(), eq(null), eq(thingHandler));
95 protected @Nullable VariableBinding handleCommandNumberStringChannel(ChannelTypeUID channelTypeUID,
96 SnmpDatatype datatype, Command command, boolean refresh) throws IOException {
97 return handleCommandNumberStringChannel(channelTypeUID, datatype, null, command, refresh);
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);
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);
110 verify(snmpService, never()).send(any(), any(), eq(null), eq(thingHandler));
115 protected void onResponseNumberStringChannel(SnmpChannelMode channelMode, boolean refresh) {
116 setup(SnmpBindingConstants.CHANNEL_TYPE_UID_STRING, channelMode);
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);
122 thingHandler.onResponse(event);
125 verify(thingHandlerCallback, atLeast(1)).stateUpdated(eq(CHANNEL_UID), eq(new StringType(TEST_STRING)));
127 verify(thingHandlerCallback, never()).stateUpdated(any(), any());
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);
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);
138 thingHandler.onResponse(event);
141 ArgumentCaptor<State> stateCaptor = ArgumentCaptor.forClass(State.class);
142 verify(thingHandlerCallback, atLeast(1)).stateUpdated(eq(CHANNEL_UID), stateCaptor.capture());
143 return stateCaptor.getValue();
145 verify(thingHandlerCallback, never()).stateUpdated(any(), any());
150 protected void refresh(SnmpChannelMode channelMode, boolean refresh) throws IOException {
151 setup(SnmpBindingConstants.CHANNEL_TYPE_UID_STRING, channelMode);
153 verifyStatus(ThingStatus.UNKNOWN);
154 verify(snmpService).addCommandResponder(any());
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()
163 verify(snmpService, never()).send(any(), any(), eq(null), eq(thingHandler));
167 protected void setup(ChannelTypeUID channelTypeUID, SnmpChannelMode channelMode) {
168 setup(channelTypeUID, channelMode, null);
171 protected void setup(ChannelTypeUID channelTypeUID, SnmpChannelMode channelMode, @Nullable SnmpDatatype datatype) {
172 setup(channelTypeUID, channelMode, datatype, null, null);
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);
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);
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);
192 thingConfig.put("hostname", "localhost");
194 ThingBuilder thingBuilder = ThingBuilder.create(THING_TYPE_TARGET, THING_UID).withLabel("Test thing")
195 .withConfiguration(new Configuration(thingConfig));
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());
203 if (onValue != null) {
204 channelConfig.put("onvalue", onValue);
206 if (offValue != null) {
207 channelConfig.put("offvalue", offValue);
209 if (exceptionValue != null) {
210 channelConfig.put("exceptionValue", exceptionValue);
213 channelConfig.put("unit", unit);
215 Channel channel = ChannelBuilder.create(CHANNEL_UID, itemType).withType(channelTypeUID)
216 .withConfiguration(new Configuration(channelConfig)).build();
217 thingBuilder.withChannel(channel);
219 thing = thingBuilder.build();
220 thingHandler = new SnmpTargetHandler(thing, snmpService);
222 thingHandler.getThing().setHandler(thingHandler);
223 thingHandler.setCallback(thingHandlerCallback);
226 ((Thing) answer.getArgument(0)).setStatusInfo(answer.getArgument(1));
228 }).when(thingHandlerCallback).statusUpdated(any(), any());
230 thingHandler.initialize();
232 verifyStatus(ThingStatus.UNKNOWN);
235 protected void verifyStatus(ThingStatus status) {
236 waitForAssert(() -> assertEquals(status, thingHandler.getThing().getStatusInfo().getStatus()));