2 * Copyright (c) 2010-2023 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 java.util.HashMap;
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;
35 * Tests cases for {@link DS18x20}.
37 * @author Jan N. Klug - Initial contribution
40 public class DS18x20Test extends DeviceTestParent<DS18x20> {
43 public void setupMocks() {
44 setupMocks(THING_TYPE_BASIC, DS18x20.class);
46 Map<String, Object> channelConfig = new HashMap<>();
47 channelConfig.put(CONFIG_IGNORE_POR, true);
48 addChannel(CHANNEL_TEMPERATURE, "Number:Temperature", new Configuration(channelConfig));
52 public void temperatureTest() throws OwException {
53 final DS18x20 testDevice = instantiateDevice();
54 final InOrder inOrder = Mockito.inOrder(mockThingHandler, mockBridgeHandler);
56 Mockito.when(mockBridgeHandler.checkPresence(testSensorId)).thenReturn(OnOffType.ON);
57 Mockito.when(mockBridgeHandler.readDecimalType(eq(testSensorId), any())).thenReturn(new DecimalType(15.0));
59 testDevice.enableChannel(CHANNEL_TEMPERATURE);
60 testDevice.configureChannels();
61 testDevice.refresh(mockBridgeHandler, true);
63 inOrder.verify(mockBridgeHandler, times(1)).readDecimalType(eq(testSensorId), any());
64 inOrder.verify(mockThingHandler).postUpdate(eq(CHANNEL_TEMPERATURE), eq(new QuantityType<>("15.0 °C")));
68 public void temperatureIgnorePORTest() throws OwException {
69 final DS18x20 testDevice = instantiateDevice();
70 final InOrder inOrder = Mockito.inOrder(mockThingHandler, mockBridgeHandler);
72 Mockito.when(mockBridgeHandler.checkPresence(testSensorId)).thenReturn(OnOffType.ON);
73 Mockito.when(mockBridgeHandler.readDecimalType(eq(testSensorId), any())).thenReturn(new DecimalType(85.0));
75 testDevice.enableChannel(CHANNEL_TEMPERATURE);
76 testDevice.configureChannels();
77 testDevice.refresh(mockBridgeHandler, true);
79 inOrder.verify(mockBridgeHandler, times(1)).readDecimalType(eq(testSensorId), any());
80 inOrder.verify(mockThingHandler, times(0)).postUpdate(eq(CHANNEL_TEMPERATURE), any());