]> git.basschouten.com Git - openhab-addons.git/blob
289f1a6308a882d4bb3e747a66aed35981ca201c
[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.any;
16 import static org.mockito.ArgumentMatchers.eq;
17 import static org.mockito.Mockito.times;
18 import static org.openhab.binding.onewire.internal.OwBindingConstants.*;
19
20 import java.util.HashMap;
21 import java.util.Map;
22
23 import org.eclipse.jdt.annotation.NonNullByDefault;
24 import org.junit.Assert;
25 import org.junit.Before;
26 import org.junit.Test;
27 import org.mockito.InOrder;
28 import org.mockito.Mockito;
29 import org.openhab.binding.onewire.internal.OwException;
30 import org.openhab.binding.onewire.internal.device.DS18x20;
31 import org.openhab.core.config.core.Configuration;
32 import org.openhab.core.library.types.DecimalType;
33 import org.openhab.core.library.types.OnOffType;
34 import org.openhab.core.library.types.QuantityType;
35
36 /**
37  * Tests cases for {@link DS18x20}.
38  *
39  * @author Jan N. Klug - Initial contribution
40  */
41 @NonNullByDefault
42 public class DS18x20Test extends DeviceTestParent<DS18x20> {
43
44     @Before
45     public void setupMocks() {
46         setupMocks(THING_TYPE_BASIC, DS18x20.class);
47
48         Map<String, Object> channelConfig = new HashMap<>();
49         channelConfig.put(CONFIG_IGNORE_POR, true);
50         addChannel(CHANNEL_TEMPERATURE, "Number:Temperature", new Configuration(channelConfig));
51     }
52
53     @Test
54     public void temperatureTest() {
55         final DS18x20 testDevice = instantiateDevice();
56         final InOrder inOrder = Mockito.inOrder(mockThingHandler, mockBridgeHandler);
57
58         try {
59             Mockito.when(mockBridgeHandler.checkPresence(testSensorId)).thenReturn(OnOffType.ON);
60             Mockito.when(mockBridgeHandler.readDecimalType(eq(testSensorId), any())).thenReturn(new DecimalType(15.0));
61
62             testDevice.enableChannel(CHANNEL_TEMPERATURE);
63             testDevice.configureChannels();
64             testDevice.refresh(mockBridgeHandler, true);
65
66             inOrder.verify(mockBridgeHandler, times(1)).readDecimalType(eq(testSensorId), any());
67             inOrder.verify(mockThingHandler).postUpdate(eq(CHANNEL_TEMPERATURE), eq(new QuantityType<>("15.0 °C")));
68         } catch (OwException e) {
69             Assert.fail("caught unexpected OwException");
70         }
71     }
72
73     @Test
74     public void temperatureIgnorePORTest() {
75         final DS18x20 testDevice = instantiateDevice();
76         final InOrder inOrder = Mockito.inOrder(mockThingHandler, mockBridgeHandler);
77
78         try {
79             Mockito.when(mockBridgeHandler.checkPresence(testSensorId)).thenReturn(OnOffType.ON);
80             Mockito.when(mockBridgeHandler.readDecimalType(eq(testSensorId), any())).thenReturn(new DecimalType(85.0));
81
82             testDevice.enableChannel(CHANNEL_TEMPERATURE);
83             testDevice.configureChannels();
84             testDevice.refresh(mockBridgeHandler, true);
85
86             inOrder.verify(mockBridgeHandler, times(1)).readDecimalType(eq(testSensorId), any());
87             inOrder.verify(mockThingHandler, times(0)).postUpdate(eq(CHANNEL_TEMPERATURE), any());
88         } catch (OwException e) {
89             Assert.fail("caught unexpected OwException");
90         }
91     }
92 }