2 * Copyright (c) 2010-2020 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.onewire.device;
15 import static org.mockito.ArgumentMatchers.*;
16 import static org.mockito.Mockito.times;
17 import static org.openhab.binding.onewire.internal.OwBindingConstants.*;
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;
33 * Tests cases for {@link DS2438}.
35 * @author Jan N. Klug - Initial contribution
38 public class DS2438Test extends DeviceTestParent<DS2438> {
41 public void setupMocks() {
42 setupMocks(THING_TYPE_MS_TX, DS2438.class);
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");
55 public void temperatureChannel() {
56 final DS2438 testDevice = instantiateDevice();
57 final InOrder inOrder = Mockito.inOrder(mockThingHandler, mockBridgeHandler);
60 Mockito.when(mockBridgeHandler.checkPresence(testSensorId)).thenReturn(OnOffType.ON);
61 Mockito.when(mockBridgeHandler.readDecimalType(eq(testSensorId), any())).thenReturn(new DecimalType(10.0));
63 testDevice.enableChannel(CHANNEL_TEMPERATURE);
64 testDevice.configureChannels();
65 inOrder.verify(mockThingHandler).getThing();
66 testDevice.refresh(mockBridgeHandler, true);
68 inOrder.verify(mockBridgeHandler).readDecimalType(eq(testSensorId), any());
69 inOrder.verify(mockThingHandler).postUpdate(eq(CHANNEL_TEMPERATURE), eq(new QuantityType<>("10.0 °C")));
71 inOrder.verifyNoMoreInteractions();
72 } catch (OwException e) {
73 Assert.fail("caught unexpected OwException");
78 public void humidityChannel() {
79 final DS2438 testDevice = instantiateDevice();
80 final InOrder inOrder = Mockito.inOrder(mockThingHandler, mockBridgeHandler);
83 Mockito.when(mockBridgeHandler.checkPresence(testSensorId)).thenReturn(OnOffType.ON);
84 Mockito.when(mockBridgeHandler.readDecimalType(eq(testSensorId), any())).thenReturn(new DecimalType(10.0));
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);
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")));
100 inOrder.verifyNoMoreInteractions();
101 } catch (OwException e) {
102 Assert.fail("caught unexpected OwException");
107 public void voltageChannel() {
108 final DS2438 testDevice = instantiateDevice();
109 final InOrder inOrder = Mockito.inOrder(mockThingHandler, mockBridgeHandler);
112 Mockito.when(mockBridgeHandler.checkPresence(testSensorId)).thenReturn(OnOffType.ON);
113 Mockito.when(mockBridgeHandler.readDecimalType(eq(testSensorId), any())).thenReturn(new DecimalType(2.0));
115 testDevice.enableChannel(CHANNEL_VOLTAGE);
116 testDevice.configureChannels();
117 inOrder.verify(mockThingHandler).getThing();
118 testDevice.refresh(mockBridgeHandler, true);
120 inOrder.verify(mockBridgeHandler).readDecimalType(eq(testSensorId), any());
121 inOrder.verify(mockThingHandler).postUpdate(eq(CHANNEL_VOLTAGE), eq(new QuantityType<>("2.0 V")));
123 inOrder.verifyNoMoreInteractions();
124 } catch (OwException e) {
125 Assert.fail("caught unexpected OwException");
130 public void currentChannel() {
131 final DS2438 testDevice = instantiateDevice();
132 final InOrder inOrder = Mockito.inOrder(mockThingHandler, mockBridgeHandler);
135 Mockito.when(mockBridgeHandler.checkPresence(testSensorId)).thenReturn(OnOffType.ON);
137 Mockito.when(mockBridgeHandler.readDecimalType(eq(testSensorId), any())).thenReturn(new DecimalType(2.0));
139 testDevice.enableChannel(CHANNEL_CURRENT);
140 testDevice.configureChannels();
141 inOrder.verify(mockThingHandler).getThing();
142 testDevice.refresh(mockBridgeHandler, true);
144 inOrder.verify(mockBridgeHandler).readDecimalType(eq(testSensorId), any());
145 inOrder.verify(mockThingHandler).postUpdate(eq(CHANNEL_CURRENT), eq(new QuantityType<>("2.0 mA")));
147 inOrder.verifyNoMoreInteractions();
148 } catch (OwException e) {
149 Assert.fail("caught unexpected OwException");
154 public void lightChannel() {
155 final DS2438 testDevice = instantiateDevice();
156 final InOrder inOrder = Mockito.inOrder(mockThingHandler, mockBridgeHandler);
159 Mockito.when(mockBridgeHandler.checkPresence(testSensorId)).thenReturn(OnOffType.ON);
160 Mockito.when(mockBridgeHandler.readDecimalType(eq(testSensorId), any())).thenReturn(new DecimalType(0.1));
162 testDevice.enableChannel(CHANNEL_LIGHT);
163 testDevice.configureChannels();
164 inOrder.verify(mockThingHandler).getThing();
165 testDevice.setLightSensorType(LightSensorType.ELABNET_V1);
166 testDevice.refresh(mockBridgeHandler, true);
168 inOrder.verify(mockBridgeHandler).readDecimalType(eq(testSensorId), any());
169 inOrder.verify(mockThingHandler).postUpdate(eq(CHANNEL_LIGHT), eq(new QuantityType<>("97442 lx")));
171 testDevice.setLightSensorType(LightSensorType.ELABNET_V2);
172 testDevice.refresh(mockBridgeHandler, true);
174 inOrder.verify(mockBridgeHandler).readDecimalType(eq(testSensorId), any());
175 inOrder.verify(mockThingHandler).postUpdate(eq(CHANNEL_LIGHT), eq(new QuantityType<>("134 lx")));
177 inOrder.verifyNoMoreInteractions();
178 } catch (OwException e) {
179 Assert.fail("caught unexpected OwException");
184 public void supplyVoltageChannel() {
185 final DS2438 testDevice = instantiateDevice();
186 final InOrder inOrder = Mockito.inOrder(mockThingHandler, mockBridgeHandler);
189 Mockito.when(mockBridgeHandler.checkPresence(testSensorId)).thenReturn(OnOffType.ON);
190 Mockito.when(mockBridgeHandler.readDecimalType(eq(testSensorId), any())).thenReturn(new DecimalType(2.0));
192 testDevice.enableChannel(CHANNEL_SUPPLYVOLTAGE);
193 testDevice.configureChannels();
194 inOrder.verify(mockThingHandler).getThing();
195 testDevice.refresh(mockBridgeHandler, true);
197 inOrder.verify(mockBridgeHandler).readDecimalType(eq(testSensorId), any());
198 inOrder.verify(mockThingHandler).postUpdate(eq(CHANNEL_SUPPLYVOLTAGE), eq(new QuantityType<>("2.0 V")));
200 inOrder.verifyNoMoreInteractions();
201 } catch (OwException e) {
202 Assert.fail("caught unexpected OwException");
207 public void noChannel() {
208 final DS2438 testDevice = instantiateDevice();
209 final InOrder inOrder = Mockito.inOrder(mockThingHandler, mockBridgeHandler);
212 Mockito.when(mockBridgeHandler.checkPresence(testSensorId)).thenReturn(OnOffType.ON);
213 Mockito.when(mockBridgeHandler.readDecimalType(eq(testSensorId), any())).thenReturn(new DecimalType(2.0));
215 testDevice.configureChannels();
216 inOrder.verify(mockThingHandler).getThing();
217 testDevice.refresh(mockBridgeHandler, true);
219 inOrder.verifyNoMoreInteractions();
220 } catch (OwException e) {
221 Assert.fail("caught unexpected OwException");