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