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.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;
32 * Tests cases for {@link DS1923}.
34 * @author Jan N. Klug - Initial contribution
35 * @author Michał Wójcik - Adapted to DS1923
38 public class DS1923Test extends DeviceTestParent<DS1923> {
40 public void setupMocks() {
41 setupMocks(THING_TYPE_MS_TX, DS1923.class);
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");
50 public void temperatureChannel() {
51 final DS1923 testDevice = instantiateDevice();
52 final InOrder inOrder = Mockito.inOrder(mockThingHandler, mockBridgeHandler);
55 Mockito.when(mockBridgeHandler.checkPresence(testSensorId)).thenReturn(OnOffType.ON);
56 Mockito.when(mockBridgeHandler.readDecimalType(eq(testSensorId), any())).thenReturn(new DecimalType(10.0));
58 testDevice.enableChannel(CHANNEL_TEMPERATURE);
59 testDevice.configureChannels();
60 testDevice.refresh(mockBridgeHandler, true);
62 inOrder.verify(mockBridgeHandler).readDecimalType(eq(testSensorId), any());
63 inOrder.verify(mockThingHandler).postUpdate(eq(CHANNEL_TEMPERATURE), eq(new QuantityType<>("10.0 °C")));
65 inOrder.verifyNoMoreInteractions();
66 } catch (OwException e) {
67 Assert.fail("caught unexpected OwException");
72 public void humidityChannel() {
73 final DS1923 testDevice = instantiateDevice();
74 final InOrder inOrder = Mockito.inOrder(mockThingHandler, mockBridgeHandler);
77 Mockito.when(mockBridgeHandler.checkPresence(testSensorId)).thenReturn(OnOffType.ON);
78 Mockito.when(mockBridgeHandler.readDecimalType(eq(testSensorId), any())).thenReturn(new DecimalType(10.0));
80 testDevice.enableChannel(CHANNEL_HUMIDITY);
81 testDevice.enableChannel(CHANNEL_ABSOLUTE_HUMIDITY);
82 testDevice.enableChannel(CHANNEL_DEWPOINT);
83 testDevice.configureChannels();
84 testDevice.refresh(mockBridgeHandler, true);
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")));
93 inOrder.verifyNoMoreInteractions();
94 } catch (OwException e) {
95 Assert.fail("caught unexpected OwException");