]> git.basschouten.com Git - openhab-addons.git/blob
3227eeda316e41166d55fee5818f6a76b08db330
[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
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.OnOffType;
24 import org.openhab.core.thing.ThingStatus;
25 import org.openhab.core.types.State;
26 import org.openhab.core.types.UnDefType;
27 import org.snmp4j.PDU;
28 import org.snmp4j.event.ResponseEvent;
29 import org.snmp4j.smi.Counter64;
30 import org.snmp4j.smi.Integer32;
31 import org.snmp4j.smi.Null;
32 import org.snmp4j.smi.OID;
33 import org.snmp4j.smi.OctetString;
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 SwitchChannelTest extends AbstractSnmpTargetHandlerTest {
42
43     @Test
44     public void testCommandsAreProperlyHandledBySwitchChannel() throws IOException {
45         VariableBinding variable;
46
47         variable = handleCommandSwitchChannel(SnmpDatatype.STRING, OnOffType.ON, "on", "off", true);
48         assertEquals(new OID(TEST_OID), variable.getOid());
49         assertTrue(variable.getVariable() instanceof OctetString);
50         assertEquals("on", ((OctetString) variable.getVariable()).toString());
51
52         variable = handleCommandSwitchChannel(SnmpDatatype.STRING, OnOffType.OFF, "on", "off", true);
53         assertEquals(new OID(TEST_OID), variable.getOid());
54         assertTrue(variable.getVariable() instanceof OctetString);
55         assertEquals("off", ((OctetString) variable.getVariable()).toString());
56
57         variable = handleCommandSwitchChannel(SnmpDatatype.STRING, OnOffType.OFF, "on", null, false);
58         assertNull(variable);
59     }
60
61     @Test
62     public void testSwitchChannelsProperlyUpdatingOnValue() throws IOException {
63         setup(SnmpBindingConstants.CHANNEL_TYPE_UID_SWITCH, SnmpChannelMode.READ, SnmpDatatype.STRING, "on", "off");
64         PDU responsePDU = new PDU(PDU.RESPONSE,
65                 Collections.singletonList(new VariableBinding(new OID(TEST_OID), new OctetString("on"))));
66         ResponseEvent event = new ResponseEvent("test", null, null, responsePDU, null);
67         thingHandler.onResponse(event);
68         verifyResponse(OnOffType.ON);
69     }
70
71     @Test
72     public void testSwitchChannelsProperlyUpdatingOffValue() throws IOException {
73         setup(SnmpBindingConstants.CHANNEL_TYPE_UID_SWITCH, SnmpChannelMode.READ, SnmpDatatype.INT32, "0", "3");
74         PDU responsePDU = new PDU(PDU.RESPONSE,
75                 Collections.singletonList(new VariableBinding(new OID(TEST_OID), new Integer32(3))));
76         ResponseEvent event = new ResponseEvent("test", null, null, responsePDU, null);
77         thingHandler.onResponse(event);
78         verifyResponse(OnOffType.OFF);
79     }
80
81     @Test
82     public void testSwitchChannelsProperlyUpdatingHexValue() throws IOException {
83         setup(SnmpBindingConstants.CHANNEL_TYPE_UID_SWITCH, SnmpChannelMode.READ, SnmpDatatype.HEXSTRING, "AA bb 11",
84                 "cc ba 1d");
85         PDU responsePDU = new PDU(PDU.RESPONSE, Collections
86                 .singletonList(new VariableBinding(new OID(TEST_OID), OctetString.fromHexStringPairs("aabb11"))));
87         ResponseEvent event = new ResponseEvent("test", null, null, responsePDU, null);
88         thingHandler.onResponse(event);
89         verifyResponse(OnOffType.ON);
90     }
91
92     @Test
93     public void testSwitchChannelsIgnoresArbitraryValue() throws IOException {
94         setup(SnmpBindingConstants.CHANNEL_TYPE_UID_SWITCH, SnmpChannelMode.READ, SnmpDatatype.COUNTER64, "0", "12223");
95         PDU responsePDU = new PDU(PDU.RESPONSE,
96                 Collections.singletonList(new VariableBinding(new OID(TEST_OID), new Counter64(17))));
97         ResponseEvent event = new ResponseEvent("test", null, null, responsePDU, null);
98         thingHandler.onResponse(event);
99         verify(thingHandlerCallback, never()).stateUpdated(eq(CHANNEL_UID), any());
100         verifyStatus(ThingStatus.ONLINE);
101     }
102
103     @Test
104     public void testSwitchChannelSendsUndefExceptionValue() throws IOException {
105         setup(SnmpBindingConstants.CHANNEL_TYPE_UID_SWITCH, SnmpChannelMode.READ, SnmpDatatype.COUNTER64, "0", "12223");
106         PDU responsePDU = new PDU(PDU.RESPONSE,
107                 Collections.singletonList(new VariableBinding(new OID(TEST_OID), Null.noSuchInstance)));
108         ResponseEvent event = new ResponseEvent("test", null, null, responsePDU, null);
109         thingHandler.onResponse(event);
110         verifyResponse(UnDefType.UNDEF);
111     }
112
113     @Test
114     public void testSwitchChannelSendsConfiguredExceptionValue() throws IOException {
115         setup(SnmpBindingConstants.CHANNEL_TYPE_UID_SWITCH, SnmpChannelMode.READ, SnmpDatatype.COUNTER64, "0", "12223",
116                 "OFF");
117         PDU responsePDU = new PDU(PDU.RESPONSE,
118                 Collections.singletonList(new VariableBinding(new OID(TEST_OID), Null.noSuchInstance)));
119         ResponseEvent event = new ResponseEvent("test", null, null, responsePDU, null);
120         thingHandler.onResponse(event);
121         verifyResponse(OnOffType.OFF);
122     }
123
124     private void verifyResponse(State expectedState) {
125         verify(thingHandlerCallback, atLeast(1)).stateUpdated(eq(CHANNEL_UID), eq(expectedState));
126         verifyStatus(ThingStatus.ONLINE);
127     }
128 }