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