]> git.basschouten.com Git - openhab-addons.git/blob
12b51081ff75d58531d780cb5b587c4d04f40a92
[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.DS1923;
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 DS1923}.
33  *
34  * @author Jan N. Klug - Initial contribution
35  * @author Michał Wójcik - Adapted to DS1923
36  */
37 @NonNullByDefault
38 public class DS1923Test extends DeviceTestParent<DS1923> {
39     @Before
40     public void setupMocks() {
41         setupMocks(THING_TYPE_MS_TX, DS1923.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     }
48
49     @Test
50     public void temperatureChannel() {
51         final DS1923 testDevice = instantiateDevice();
52         final InOrder inOrder = Mockito.inOrder(mockThingHandler, mockBridgeHandler);
53
54         try {
55             Mockito.when(mockBridgeHandler.checkPresence(testSensorId)).thenReturn(OnOffType.ON);
56             Mockito.when(mockBridgeHandler.readDecimalType(eq(testSensorId), any())).thenReturn(new DecimalType(10.0));
57
58             testDevice.enableChannel(CHANNEL_TEMPERATURE);
59             testDevice.configureChannels();
60             testDevice.refresh(mockBridgeHandler, true);
61
62             inOrder.verify(mockBridgeHandler).readDecimalType(eq(testSensorId), any());
63             inOrder.verify(mockThingHandler).postUpdate(eq(CHANNEL_TEMPERATURE), eq(new QuantityType<>("10.0 °C")));
64
65             inOrder.verifyNoMoreInteractions();
66         } catch (OwException e) {
67             Assert.fail("caught unexpected OwException");
68         }
69     }
70
71     @Test
72     public void humidityChannel() {
73         final DS1923 testDevice = instantiateDevice();
74         final InOrder inOrder = Mockito.inOrder(mockThingHandler, mockBridgeHandler);
75
76         try {
77             Mockito.when(mockBridgeHandler.checkPresence(testSensorId)).thenReturn(OnOffType.ON);
78             Mockito.when(mockBridgeHandler.readDecimalType(eq(testSensorId), any())).thenReturn(new DecimalType(10.0));
79
80             testDevice.enableChannel(CHANNEL_HUMIDITY);
81             testDevice.enableChannel(CHANNEL_ABSOLUTE_HUMIDITY);
82             testDevice.enableChannel(CHANNEL_DEWPOINT);
83             testDevice.configureChannels();
84             testDevice.refresh(mockBridgeHandler, true);
85
86             inOrder.verify(mockBridgeHandler, times(2)).readDecimalType(eq(testSensorId), any());
87             inOrder.verify(mockThingHandler).postUpdate(eq(CHANNEL_HUMIDITY), eq(new QuantityType<>("10.0 %")));
88             inOrder.verify(mockThingHandler).postUpdate(eq(CHANNEL_ABSOLUTE_HUMIDITY),
89                     eq(new QuantityType<>("0.9381970824113001000 g/m³")));
90             inOrder.verify(mockThingHandler).postUpdate(eq(CHANNEL_DEWPOINT),
91                     eq(new QuantityType<>("-20.31395053870025 °C")));
92
93             inOrder.verifyNoMoreInteractions();
94         } catch (OwException e) {
95             Assert.fail("caught unexpected OwException");
96         }
97     }
98 }