]> git.basschouten.com Git - openhab-addons.git/blob
04c6b1302d5542a657176c73a9491b9838b46958
[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.onewire.device;
14
15 import static org.mockito.ArgumentMatchers.*;
16 import static org.mockito.Mockito.times;
17 import static org.openhab.binding.onewire.internal.OwBindingConstants.*;
18
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.junit.jupiter.api.BeforeEach;
21 import org.junit.jupiter.api.Test;
22 import org.mockito.InOrder;
23 import org.mockito.Mockito;
24 import org.openhab.binding.onewire.internal.OwException;
25 import org.openhab.binding.onewire.internal.device.DS2438;
26 import org.openhab.binding.onewire.internal.device.DS2438.LightSensorType;
27 import org.openhab.core.library.types.DecimalType;
28 import org.openhab.core.library.types.OnOffType;
29 import org.openhab.core.library.types.QuantityType;
30
31 /**
32  * Tests cases for {@link DS2438}.
33  *
34  * @author Jan N. Klug - Initial contribution
35  */
36 @NonNullByDefault
37 public class DS2438Test extends DeviceTestParent<DS2438> {
38
39     @BeforeEach
40     public void setupMocks() {
41         setupMocks(THING_TYPE_MS_TX, DS2438.class);
42
43         addChannel(CHANNEL_TEMPERATURE, "Number:Temperature");
44         addChannel(CHANNEL_HUMIDITY, "Number:Dimensionless");
45         addChannel(CHANNEL_ABSOLUTE_HUMIDITY, "Number:Density");
46         addChannel(CHANNEL_DEWPOINT, "Number:Temperature");
47         addChannel(CHANNEL_VOLTAGE, "Number:Voltage");
48         addChannel(CHANNEL_CURRENT, "Number:Current");
49         addChannel(CHANNEL_LIGHT, "Number:Illuminance");
50         addChannel(CHANNEL_SUPPLYVOLTAGE, "Number:Voltage");
51     }
52
53     @Test
54     public void temperatureChannel() throws OwException {
55         final DS2438 testDevice = instantiateDevice();
56         final InOrder inOrder = Mockito.inOrder(mockThingHandler, mockBridgeHandler);
57
58         Mockito.when(mockBridgeHandler.checkPresence(testSensorId)).thenReturn(OnOffType.ON);
59         Mockito.when(mockBridgeHandler.readDecimalType(eq(testSensorId), any())).thenReturn(new DecimalType(10.0));
60
61         testDevice.enableChannel(CHANNEL_TEMPERATURE);
62         testDevice.configureChannels();
63         inOrder.verify(mockThingHandler).getThing();
64         testDevice.refresh(mockBridgeHandler, true);
65
66         inOrder.verify(mockBridgeHandler).readDecimalType(eq(testSensorId), any());
67         inOrder.verify(mockThingHandler).postUpdate(eq(CHANNEL_TEMPERATURE), eq(new QuantityType<>("10.0 °C")));
68
69         inOrder.verifyNoMoreInteractions();
70     }
71
72     @Test
73     public void humidityChannel() throws OwException {
74         final DS2438 testDevice = instantiateDevice();
75         final InOrder inOrder = Mockito.inOrder(mockThingHandler, mockBridgeHandler);
76
77         Mockito.when(mockBridgeHandler.checkPresence(testSensorId)).thenReturn(OnOffType.ON);
78         Mockito.when(mockBridgeHandler.readDecimalType(eq(testSensorId), any())).thenReturn(new DecimalType(10.0));
79
80         testDevice.enableChannel(CHANNEL_HUMIDITY);
81         testDevice.enableChannel(CHANNEL_ABSOLUTE_HUMIDITY);
82         testDevice.enableChannel(CHANNEL_DEWPOINT);
83         testDevice.configureChannels();
84         inOrder.verify(mockThingHandler).getThing();
85         testDevice.refresh(mockBridgeHandler, true);
86
87         inOrder.verify(mockBridgeHandler, times(2)).readDecimalType(eq(testSensorId), any());
88         inOrder.verify(mockThingHandler).postUpdate(eq(CHANNEL_HUMIDITY), eq(new QuantityType<>("10.0 %")));
89         inOrder.verify(mockThingHandler).postUpdate(eq(CHANNEL_ABSOLUTE_HUMIDITY),
90                 eq(new QuantityType<>("0.9381970824113001000 g/m³")));
91         inOrder.verify(mockThingHandler).postUpdate(eq(CHANNEL_DEWPOINT),
92                 eq(new QuantityType<>("-20.31395053870025 °C")));
93
94         inOrder.verifyNoMoreInteractions();
95     }
96
97     @Test
98     public void voltageChannel() throws OwException {
99         final DS2438 testDevice = instantiateDevice();
100         final InOrder inOrder = Mockito.inOrder(mockThingHandler, mockBridgeHandler);
101
102         Mockito.when(mockBridgeHandler.checkPresence(testSensorId)).thenReturn(OnOffType.ON);
103         Mockito.when(mockBridgeHandler.readDecimalType(eq(testSensorId), any())).thenReturn(new DecimalType(2.0));
104
105         testDevice.enableChannel(CHANNEL_VOLTAGE);
106         testDevice.configureChannels();
107         inOrder.verify(mockThingHandler).getThing();
108         testDevice.refresh(mockBridgeHandler, true);
109
110         inOrder.verify(mockBridgeHandler).readDecimalType(eq(testSensorId), any());
111         inOrder.verify(mockThingHandler).postUpdate(eq(CHANNEL_VOLTAGE), eq(new QuantityType<>("2.0 V")));
112
113         inOrder.verifyNoMoreInteractions();
114     }
115
116     @Test
117     public void currentChannel() throws OwException {
118         final DS2438 testDevice = instantiateDevice();
119         final InOrder inOrder = Mockito.inOrder(mockThingHandler, mockBridgeHandler);
120
121         Mockito.when(mockBridgeHandler.checkPresence(testSensorId)).thenReturn(OnOffType.ON);
122
123         Mockito.when(mockBridgeHandler.readDecimalType(eq(testSensorId), any())).thenReturn(new DecimalType(2.0));
124
125         testDevice.enableChannel(CHANNEL_CURRENT);
126         testDevice.configureChannels();
127         inOrder.verify(mockThingHandler).getThing();
128         testDevice.refresh(mockBridgeHandler, true);
129
130         inOrder.verify(mockBridgeHandler).readDecimalType(eq(testSensorId), any());
131         inOrder.verify(mockThingHandler).postUpdate(eq(CHANNEL_CURRENT), eq(new QuantityType<>("2.0 mA")));
132
133         inOrder.verifyNoMoreInteractions();
134     }
135
136     @Test
137     public void lightChannel() throws OwException {
138         final DS2438 testDevice = instantiateDevice();
139         final InOrder inOrder = Mockito.inOrder(mockThingHandler, mockBridgeHandler);
140
141         Mockito.when(mockBridgeHandler.checkPresence(testSensorId)).thenReturn(OnOffType.ON);
142         Mockito.when(mockBridgeHandler.readDecimalType(eq(testSensorId), any())).thenReturn(new DecimalType(0.1));
143
144         testDevice.enableChannel(CHANNEL_LIGHT);
145         testDevice.configureChannels();
146         inOrder.verify(mockThingHandler).getThing();
147         testDevice.setLightSensorType(LightSensorType.ELABNET_V1);
148         testDevice.refresh(mockBridgeHandler, true);
149
150         inOrder.verify(mockBridgeHandler).readDecimalType(eq(testSensorId), any());
151         inOrder.verify(mockThingHandler).postUpdate(eq(CHANNEL_LIGHT), eq(new QuantityType<>("97442 lx")));
152
153         testDevice.setLightSensorType(LightSensorType.ELABNET_V2);
154         testDevice.refresh(mockBridgeHandler, true);
155
156         inOrder.verify(mockBridgeHandler).readDecimalType(eq(testSensorId), any());
157         inOrder.verify(mockThingHandler).postUpdate(eq(CHANNEL_LIGHT), eq(new QuantityType<>("134 lx")));
158
159         inOrder.verifyNoMoreInteractions();
160     }
161
162     @Test
163     public void supplyVoltageChannel() throws OwException {
164         final DS2438 testDevice = instantiateDevice();
165         final InOrder inOrder = Mockito.inOrder(mockThingHandler, mockBridgeHandler);
166
167         Mockito.when(mockBridgeHandler.checkPresence(testSensorId)).thenReturn(OnOffType.ON);
168         Mockito.when(mockBridgeHandler.readDecimalType(eq(testSensorId), any())).thenReturn(new DecimalType(2.0));
169
170         testDevice.enableChannel(CHANNEL_SUPPLYVOLTAGE);
171         testDevice.configureChannels();
172         inOrder.verify(mockThingHandler).getThing();
173         testDevice.refresh(mockBridgeHandler, true);
174
175         inOrder.verify(mockBridgeHandler).readDecimalType(eq(testSensorId), any());
176         inOrder.verify(mockThingHandler).postUpdate(eq(CHANNEL_SUPPLYVOLTAGE), eq(new QuantityType<>("2.0 V")));
177
178         inOrder.verifyNoMoreInteractions();
179     }
180
181     @Test
182     public void noChannel() throws OwException {
183         final DS2438 testDevice = instantiateDevice();
184         final InOrder inOrder = Mockito.inOrder(mockThingHandler, mockBridgeHandler);
185
186         Mockito.when(mockBridgeHandler.checkPresence(testSensorId)).thenReturn(OnOffType.ON);
187         Mockito.when(mockBridgeHandler.readDecimalType(eq(testSensorId), any())).thenReturn(new DecimalType(2.0));
188
189         testDevice.configureChannels();
190         inOrder.verify(mockThingHandler).getThing();
191         testDevice.refresh(mockBridgeHandler, true);
192
193         inOrder.verifyNoMoreInteractions();
194     }
195 }