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