]> git.basschouten.com Git - openhab-addons.git/blob
1561ed99eeaf500c325b0cf7b8f6077740db865b
[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.DS1923;
26 import org.openhab.core.library.types.DecimalType;
27 import org.openhab.core.library.types.OnOffType;
28 import org.openhab.core.library.types.QuantityType;
29
30 /**
31  * Tests cases for {@link DS1923}.
32  *
33  * @author Jan N. Klug - Initial contribution
34  * @author Michał Wójcik - Adapted to DS1923
35  */
36 @NonNullByDefault
37 public class DS1923Test extends DeviceTestParent<DS1923> {
38     @BeforeEach
39     public void setupMocks() {
40         setupMocks(THING_TYPE_MS_TX, DS1923.class);
41
42         addChannel(CHANNEL_TEMPERATURE, "Number:Temperature");
43         addChannel(CHANNEL_HUMIDITY, "Number:Dimensionless");
44         addChannel(CHANNEL_ABSOLUTE_HUMIDITY, "Number:Density");
45         addChannel(CHANNEL_DEWPOINT, "Number:Temperature");
46     }
47
48     @Test
49     public void temperatureChannel() throws OwException {
50         final DS1923 testDevice = instantiateDevice();
51         final InOrder inOrder = Mockito.inOrder(mockThingHandler, mockBridgeHandler);
52
53         Mockito.when(mockBridgeHandler.checkPresence(testSensorId)).thenReturn(OnOffType.ON);
54         Mockito.when(mockBridgeHandler.readDecimalType(eq(testSensorId), any())).thenReturn(new DecimalType(10.0));
55
56         testDevice.enableChannel(CHANNEL_TEMPERATURE);
57         testDevice.configureChannels();
58         testDevice.refresh(mockBridgeHandler, true);
59
60         inOrder.verify(mockBridgeHandler).readDecimalType(eq(testSensorId), any());
61         inOrder.verify(mockThingHandler).postUpdate(eq(CHANNEL_TEMPERATURE), eq(new QuantityType<>("10.0 °C")));
62
63         inOrder.verifyNoMoreInteractions();
64     }
65
66     @Test
67     public void humidityChannel() throws OwException {
68         final DS1923 testDevice = instantiateDevice();
69         final InOrder inOrder = Mockito.inOrder(mockThingHandler, mockBridgeHandler);
70
71         Mockito.when(mockBridgeHandler.checkPresence(testSensorId)).thenReturn(OnOffType.ON);
72         Mockito.when(mockBridgeHandler.readDecimalType(eq(testSensorId), any())).thenReturn(new DecimalType(10.0));
73
74         testDevice.enableChannel(CHANNEL_HUMIDITY);
75         testDevice.enableChannel(CHANNEL_ABSOLUTE_HUMIDITY);
76         testDevice.enableChannel(CHANNEL_DEWPOINT);
77         testDevice.configureChannels();
78         testDevice.refresh(mockBridgeHandler, true);
79
80         inOrder.verify(mockBridgeHandler, times(2)).readDecimalType(eq(testSensorId), any());
81         inOrder.verify(mockThingHandler).postUpdate(eq(CHANNEL_HUMIDITY), eq(new QuantityType<>("10.0 %")));
82         inOrder.verify(mockThingHandler).postUpdate(eq(CHANNEL_ABSOLUTE_HUMIDITY),
83                 eq(new QuantityType<>("0.9381970824113001000 g/m³")));
84         inOrder.verify(mockThingHandler).postUpdate(eq(CHANNEL_DEWPOINT),
85                 eq(new QuantityType<>("-20.31395053870025 °C")));
86
87         inOrder.verifyNoMoreInteractions();
88     }
89 }