]> git.basschouten.com Git - openhab-addons.git/blob
e9544bf04ade4457409b7e10f90439b24ef7409e
[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.assertEquals;
16 import static org.mockito.ArgumentMatchers.eq;
17 import static org.mockito.Mockito.atLeast;
18 import static org.mockito.Mockito.verify;
19
20 import java.util.List;
21
22 import org.eclipse.jdt.annotation.NonNullByDefault;
23 import org.junit.jupiter.api.Test;
24 import org.mockito.ArgumentCaptor;
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.QuantityType;
29 import org.openhab.core.library.unit.SIUnits;
30 import org.openhab.core.thing.ThingStatus;
31 import org.snmp4j.PDU;
32 import org.snmp4j.event.ResponseEvent;
33 import org.snmp4j.smi.Counter64;
34 import org.snmp4j.smi.OID;
35 import org.snmp4j.smi.Opaque;
36 import org.snmp4j.smi.VariableBinding;
37
38 /**
39  * Tests cases for {@link SnmpTargetHandler}.
40  *
41  * @author Jan N. Klug - Initial contribution
42  */
43 @NonNullByDefault
44 public class NumberChannelTest extends AbstractSnmpTargetHandlerTest {
45
46     @Test
47     public void testNumberChannelsProperlyUpdatingOnOpaque() {
48         setup(SnmpBindingConstants.CHANNEL_TYPE_UID_NUMBER, SnmpChannelMode.READ, SnmpDatatype.FLOAT);
49         PDU responsePDU = new PDU(PDU.RESPONSE, List.of(new VariableBinding(new OID(TEST_OID),
50                 new Opaque(new byte[] { (byte) 0x9f, 0x78, 0x04, 0x41, 0x5b, 0x33, 0x33 }))));
51         ResponseEvent event = new ResponseEvent("test", null, null, responsePDU, null);
52         thingHandler.onResponse(event);
53         final ArgumentCaptor<DecimalType> captor = ArgumentCaptor.forClass(DecimalType.class);
54         verify(thingHandlerCallback, atLeast(1)).stateUpdated(eq(CHANNEL_UID), captor.capture());
55         assertEquals(13.7, captor.getValue().doubleValue(), 0.001);
56         verifyStatus(ThingStatus.ONLINE);
57     }
58
59     @Test
60     public void testNumberChannelsProperlyUpdatingOnInteger() {
61         setup(SnmpBindingConstants.CHANNEL_TYPE_UID_NUMBER, SnmpChannelMode.READ, SnmpDatatype.COUNTER64);
62         PDU responsePDU = new PDU(PDU.RESPONSE,
63                 List.of(new VariableBinding(new OID(TEST_OID), new Counter64(1234567891333L))));
64         ResponseEvent event = new ResponseEvent("test", null, null, responsePDU, null);
65         thingHandler.onResponse(event);
66         verify(thingHandlerCallback, atLeast(1)).stateUpdated(eq(CHANNEL_UID), eq(new DecimalType(1234567891333L)));
67         verifyStatus(ThingStatus.ONLINE);
68     }
69
70     @Test
71     public void testNumberChannelsProperlyUpdatingOnQuantityType() {
72         setup(SnmpBindingConstants.CHANNEL_TYPE_UID_NUMBER, SnmpChannelMode.READ, SnmpDatatype.FLOAT, null, null, null,
73                 "°C");
74         PDU responsePDU = new PDU(PDU.RESPONSE, List.of(new VariableBinding(new OID(TEST_OID),
75                 new Opaque(new byte[] { (byte) 0x9f, 0x78, 0x04, 0x41, 0x5b, 0x33, 0x33 }))));
76         ResponseEvent event = new ResponseEvent("test", null, null, responsePDU, null);
77         thingHandler.onResponse(event);
78         final ArgumentCaptor<QuantityType<?>> captor = ArgumentCaptor.forClass(QuantityType.class);
79         verify(thingHandlerCallback, atLeast(1)).stateUpdated(eq(CHANNEL_UID), captor.capture());
80         assertEquals(13.7, captor.getValue().doubleValue(), 0.001);
81         assertEquals(SIUnits.CELSIUS, captor.getValue().getUnit());
82         verifyStatus(ThingStatus.ONLINE);
83     }
84 }