]> git.basschouten.com Git - openhab-addons.git/blob
de213f54530cb3503f793078104c1719d5c22055
[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.EDS006x;
27 import org.openhab.binding.onewire.internal.device.OwSensorType;
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 EDS006x}.
34  *
35  * @author Jan N. Klug - Initial contribution
36  */
37 @NonNullByDefault
38 public class EDS006xTest extends DeviceTestParent<EDS006x> {
39
40     @Before
41     public void setupMocks() {
42         setupMocks(THING_TYPE_EDS_ENV, EDS006x.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_LIGHT, "Number:Illuminance");
49         addChannel(CHANNEL_PRESSURE, "Number:Pressure");
50     }
51
52     @Test
53     public void temperatureChannel() {
54         final EDS006x testDevice = instantiateDevice(OwSensorType.EDS0068);
55         final InOrder inOrder = Mockito.inOrder(mockThingHandler, mockBridgeHandler);
56
57         try {
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             testDevice.refresh(mockBridgeHandler, true);
64
65             inOrder.verify(mockBridgeHandler).readDecimalType(eq(testSensorId), any());
66             inOrder.verify(mockThingHandler).postUpdate(eq(CHANNEL_TEMPERATURE), eq(new QuantityType<>("10.0 °C")));
67
68             inOrder.verifyNoMoreInteractions();
69         } catch (OwException e) {
70             Assert.fail("caught unexpected OwException");
71         }
72     }
73
74     @Test
75     public void humidityChannel() {
76         final EDS006x testDevice = instantiateDevice(OwSensorType.EDS0068);
77         final InOrder inOrder = Mockito.inOrder(mockThingHandler, mockBridgeHandler);
78
79         try {
80             Mockito.when(mockBridgeHandler.checkPresence(testSensorId)).thenReturn(OnOffType.ON);
81             Mockito.when(mockBridgeHandler.readDecimalType(eq(testSensorId), any())).thenReturn(new DecimalType(10.0));
82
83             testDevice.enableChannel(CHANNEL_HUMIDITY);
84             testDevice.enableChannel(CHANNEL_ABSOLUTE_HUMIDITY);
85             testDevice.enableChannel(CHANNEL_DEWPOINT);
86             testDevice.configureChannels();
87             testDevice.refresh(mockBridgeHandler, true);
88
89             inOrder.verify(mockBridgeHandler, times(2)).readDecimalType(eq(testSensorId), any());
90             inOrder.verify(mockThingHandler).postUpdate(eq(CHANNEL_HUMIDITY), eq(new QuantityType<>("10.0 %")));
91             inOrder.verify(mockThingHandler).postUpdate(eq(CHANNEL_ABSOLUTE_HUMIDITY),
92                     eq(new QuantityType<>("0.9381970824113001000 g/m³")));
93             inOrder.verify(mockThingHandler).postUpdate(eq(CHANNEL_DEWPOINT),
94                     eq(new QuantityType<>("-20.31395053870025 °C")));
95
96             inOrder.verifyNoMoreInteractions();
97         } catch (OwException e) {
98             Assert.fail("caught unexpected OwException");
99         }
100     }
101
102     @Test
103     public void pressureChannel() {
104         final EDS006x testDevice = instantiateDevice(OwSensorType.EDS0068);
105         final InOrder inOrder = Mockito.inOrder(mockThingHandler, mockBridgeHandler);
106
107         try {
108             Mockito.when(mockBridgeHandler.checkPresence(testSensorId)).thenReturn(OnOffType.ON);
109             Mockito.when(mockBridgeHandler.readDecimalType(eq(testSensorId), any())).thenReturn(new DecimalType(2.0));
110
111             testDevice.enableChannel(CHANNEL_PRESSURE);
112             testDevice.configureChannels();
113             testDevice.refresh(mockBridgeHandler, true);
114
115             inOrder.verify(mockBridgeHandler).readDecimalType(eq(testSensorId), any());
116             inOrder.verify(mockThingHandler).postUpdate(eq(CHANNEL_PRESSURE), eq(new QuantityType<>("2.0 mbar")));
117
118             inOrder.verifyNoMoreInteractions();
119         } catch (OwException e) {
120             Assert.fail("caught unexpected OwException");
121         }
122     }
123
124     @Test
125     public void lightChannel() {
126         final EDS006x testDevice = instantiateDevice(OwSensorType.EDS0068);
127         final InOrder inOrder = Mockito.inOrder(mockThingHandler, mockBridgeHandler);
128
129         try {
130             Mockito.when(mockBridgeHandler.checkPresence(testSensorId)).thenReturn(OnOffType.ON);
131             Mockito.when(mockBridgeHandler.readDecimalType(eq(testSensorId), any())).thenReturn(new DecimalType(100));
132
133             testDevice.enableChannel(CHANNEL_LIGHT);
134             testDevice.configureChannels();
135             testDevice.refresh(mockBridgeHandler, true);
136
137             inOrder.verify(mockBridgeHandler).readDecimalType(eq(testSensorId), any());
138             inOrder.verify(mockThingHandler).postUpdate(eq(CHANNEL_LIGHT), eq(new QuantityType<>("100 lx")));
139
140             inOrder.verifyNoMoreInteractions();
141         } catch (OwException e) {
142             Assert.fail("caught unexpected OwException");
143         }
144     }
145
146     @Test
147     public void noChannel() {
148         final EDS006x testDevice = instantiateDevice(OwSensorType.EDS0068);
149         final InOrder inOrder = Mockito.inOrder(mockThingHandler, mockBridgeHandler);
150
151         try {
152             Mockito.when(mockBridgeHandler.checkPresence(testSensorId)).thenReturn(OnOffType.ON);
153             Mockito.when(mockBridgeHandler.readDecimalType(eq(testSensorId), any())).thenReturn(new DecimalType(2.0));
154
155             testDevice.configureChannels();
156             testDevice.refresh(mockBridgeHandler, true);
157
158             inOrder.verifyNoMoreInteractions();
159         } catch (OwException e) {
160             Assert.fail("caught unexpected OwException");
161         }
162     }
163 }