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.internal;
15 import static org.junit.jupiter.api.Assertions.*;
16 import static org.mockito.ArgumentMatchers.*;
17 import static org.mockito.Mockito.times;
18 import static org.openhab.binding.onewire.internal.OwBindingConstants.*;
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.eclipse.jdt.annotation.Nullable;
22 import org.junit.jupiter.api.BeforeEach;
23 import org.junit.jupiter.api.Test;
24 import org.mockito.InOrder;
25 import org.mockito.Mockito;
26 import org.openhab.binding.onewire.internal.device.OwSensorType;
27 import org.openhab.binding.onewire.internal.handler.BasicThingHandler;
28 import org.openhab.binding.onewire.internal.handler.OwBaseThingHandler;
29 import org.openhab.binding.onewire.test.AbstractThingHandlerTest;
30 import org.openhab.core.config.core.Configuration;
31 import org.openhab.core.thing.Bridge;
32 import org.openhab.core.thing.Thing;
33 import org.openhab.core.thing.ThingStatus;
34 import org.openhab.core.thing.binding.ThingHandler;
35 import org.openhab.core.thing.binding.builder.ThingBuilder;
38 * Tests cases for {@link BasicThingHandler}.
40 * @author Jan N. Klug - Initial contribution
43 public class BasicThingHandlerTest extends AbstractThingHandlerTest {
44 private static final String TEST_ID = "00.000000000000";
47 public void setup() throws OwException {
50 final Bridge bridge = this.bridge;
52 fail("bridge is null");
56 thingConfiguration.put(CONFIG_ID, TEST_ID);
58 thing = ThingBuilder.create(THING_TYPE_BASIC, "testthing").withLabel("Test thing")
59 .withConfiguration(new Configuration(thingConfiguration)).withProperties(thingProperties)
60 .withBridge(bridge.getUID()).build();
62 final Thing thing = this.thing;
64 fail("thing is null");
68 thingHandler = new BasicThingHandler(thing, stateProvider) {
70 protected @Nullable Bridge getBridge() {
75 initializeHandlerMocks();
79 public void testInitializationEndsWithUnknown() throws OwException {
80 final ThingHandler thingHandler = this.thingHandler;
81 if (thingHandler == null) {
82 fail("thingHandler is null");
86 Mockito.doAnswer(answer -> OwSensorType.DS2401).when(secondBridgeHandler).getType(any());
88 thingHandler.initialize();
90 waitForAssert(() -> assertEquals(ThingStatus.UNKNOWN, thingHandler.getThing().getStatusInfo().getStatus()));
94 public void testRefreshAnalog() throws OwException {
95 final OwBaseThingHandler thingHandler = this.thingHandler;
96 final InOrder inOrder = this.inOrder;
97 if (thingHandler == null || inOrder == null) {
98 fail("prerequisite is null");
102 Mockito.doAnswer(answer -> OwSensorType.DS18B20).when(secondBridgeHandler).getType(any());
104 thingHandler.initialize();
105 waitForAssert(() -> assertEquals(ThingStatus.UNKNOWN, thingHandler.getThing().getStatusInfo().getStatus()));
107 thingHandler.refresh(bridgeHandler, System.currentTimeMillis());
109 inOrder.verify(bridgeHandler, times(1)).checkPresence(new SensorId(TEST_ID));
110 inOrder.verify(bridgeHandler, times(1)).readDecimalType(eq(new SensorId(TEST_ID)), any());
112 inOrder.verifyNoMoreInteractions();
116 public void testRefreshDigital() throws OwException {
117 final OwBaseThingHandler thingHandler = this.thingHandler;
118 final InOrder inOrder = this.inOrder;
119 if (thingHandler == null || inOrder == null) {
120 fail("prerequisite is null");
124 Mockito.doAnswer(answer -> OwSensorType.DS2408).when(secondBridgeHandler).getType(any());
126 thingHandler.initialize();
127 waitForAssert(() -> assertEquals(ThingStatus.UNKNOWN, thingHandler.getThing().getStatusInfo().getStatus()));
129 thingHandler.refresh(bridgeHandler, System.currentTimeMillis());
131 inOrder.verify(bridgeHandler, times(1)).checkPresence(new SensorId(TEST_ID));
132 inOrder.verify(bridgeHandler, times(2)).readBitSet(eq(new SensorId(TEST_ID)), any());
134 inOrder.verifyNoMoreInteractions();