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