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.any;
16 import static org.mockito.ArgumentMatchers.eq;
17 import static org.mockito.Mockito.times;
18 import static org.openhab.binding.onewire.internal.OwBindingConstants.*;
20 import java.util.HashMap;
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;
37 * Tests cases for {@link DS18x20}.
39 * @author Jan N. Klug - Initial contribution
42 public class DS18x20Test extends DeviceTestParent<DS18x20> {
45 public void setupMocks() {
46 setupMocks(THING_TYPE_BASIC, DS18x20.class);
48 Map<String, Object> channelConfig = new HashMap<>();
49 channelConfig.put(CONFIG_IGNORE_POR, true);
50 addChannel(CHANNEL_TEMPERATURE, "Number:Temperature", new Configuration(channelConfig));
54 public void temperatureTest() {
55 final DS18x20 testDevice = instantiateDevice();
56 final InOrder inOrder = Mockito.inOrder(mockThingHandler, mockBridgeHandler);
59 Mockito.when(mockBridgeHandler.checkPresence(testSensorId)).thenReturn(OnOffType.ON);
60 Mockito.when(mockBridgeHandler.readDecimalType(eq(testSensorId), any())).thenReturn(new DecimalType(15.0));
62 testDevice.enableChannel(CHANNEL_TEMPERATURE);
63 testDevice.configureChannels();
64 testDevice.refresh(mockBridgeHandler, true);
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");
74 public void temperatureIgnorePORTest() {
75 final DS18x20 testDevice = instantiateDevice();
76 final InOrder inOrder = Mockito.inOrder(mockThingHandler, mockBridgeHandler);
79 Mockito.when(mockBridgeHandler.checkPresence(testSensorId)).thenReturn(OnOffType.ON);
80 Mockito.when(mockBridgeHandler.readDecimalType(eq(testSensorId), any())).thenReturn(new DecimalType(85.0));
82 testDevice.enableChannel(CHANNEL_TEMPERATURE);
83 testDevice.configureChannels();
84 testDevice.refresh(mockBridgeHandler, true);
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");