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.internal;
15 import static org.junit.Assert.assertEquals;
16 import static org.mockito.ArgumentMatchers.any;
17 import static org.mockito.ArgumentMatchers.eq;
18 import static org.mockito.Mockito.times;
19 import static org.openhab.binding.onewire.internal.OwBindingConstants.CONFIG_ID;
20 import static org.openhab.binding.onewire.internal.OwBindingConstants.THING_TYPE_BASIC;
22 import org.eclipse.jdt.annotation.NonNullByDefault;
23 import org.eclipse.jdt.annotation.Nullable;
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.mockito.MockitoAnnotations;
30 import org.openhab.binding.onewire.internal.device.OwSensorType;
31 import org.openhab.binding.onewire.internal.handler.BasicThingHandler;
32 import org.openhab.binding.onewire.internal.handler.OwBaseThingHandler;
33 import org.openhab.binding.onewire.test.AbstractThingHandlerTest;
34 import org.openhab.core.config.core.Configuration;
35 import org.openhab.core.thing.Bridge;
36 import org.openhab.core.thing.Thing;
37 import org.openhab.core.thing.ThingStatus;
38 import org.openhab.core.thing.binding.ThingHandler;
39 import org.openhab.core.thing.binding.builder.ThingBuilder;
42 * Tests cases for {@link BasicThingHandler}.
44 * @author Jan N. Klug - Initial contribution
47 public class BasicThingHandlerTest extends AbstractThingHandlerTest {
48 private static final String TEST_ID = "00.000000000000";
51 public void setup() throws OwException {
52 MockitoAnnotations.initMocks(this);
56 final Bridge bridge = this.bridge;
58 Assert.fail("bridge is null");
62 thingConfiguration.put(CONFIG_ID, TEST_ID);
64 thing = ThingBuilder.create(THING_TYPE_BASIC, "testthing").withLabel("Test thing")
65 .withConfiguration(new Configuration(thingConfiguration)).withProperties(thingProperties)
66 .withBridge(bridge.getUID()).build();
68 final Thing thing = this.thing;
70 Assert.fail("thing is null");
74 thingHandler = new BasicThingHandler(thing, stateProvider) {
76 protected @Nullable Bridge getBridge() {
81 initializeHandlerMocks();
85 public void testInitializationEndsWithUnknown() throws OwException {
86 final ThingHandler thingHandler = this.thingHandler;
87 if (thingHandler == null) {
88 Assert.fail("thingHandler is null");
92 Mockito.doAnswer(answer -> {
93 return OwSensorType.DS2401;
94 }).when(secondBridgeHandler).getType(any());
96 thingHandler.initialize();
98 waitForAssert(() -> assertEquals(ThingStatus.UNKNOWN, thingHandler.getThing().getStatusInfo().getStatus()));
102 public void testRefreshAnalog() throws OwException {
103 final OwBaseThingHandler thingHandler = this.thingHandler;
104 final InOrder inOrder = this.inOrder;
105 if (thingHandler == null || inOrder == null) {
106 Assert.fail("prerequisite is null");
110 Mockito.doAnswer(answer -> {
111 return OwSensorType.DS18B20;
112 }).when(secondBridgeHandler).getType(any());
114 thingHandler.initialize();
115 waitForAssert(() -> assertEquals(ThingStatus.UNKNOWN, thingHandler.getThing().getStatusInfo().getStatus()));
117 thingHandler.refresh(bridgeHandler, System.currentTimeMillis());
119 inOrder.verify(bridgeHandler, times(1)).checkPresence(new SensorId(TEST_ID));
120 inOrder.verify(bridgeHandler, times(1)).readDecimalType(eq(new SensorId(TEST_ID)), any());
122 inOrder.verifyNoMoreInteractions();
126 public void testRefreshDigital() throws OwException {
127 final OwBaseThingHandler thingHandler = this.thingHandler;
128 final InOrder inOrder = this.inOrder;
129 if (thingHandler == null || inOrder == null) {
130 Assert.fail("prerequisite is null");
134 Mockito.doAnswer(answer -> {
135 return OwSensorType.DS2408;
136 }).when(secondBridgeHandler).getType(any());
138 thingHandler.initialize();
139 waitForAssert(() -> assertEquals(ThingStatus.UNKNOWN, thingHandler.getThing().getStatusInfo().getStatus()));
141 thingHandler.refresh(bridgeHandler, System.currentTimeMillis());
143 inOrder.verify(bridgeHandler, times(1)).checkPresence(new SensorId(TEST_ID));
144 inOrder.verify(bridgeHandler, times(2)).readBitSet(eq(new SensorId(TEST_ID)), any());
146 inOrder.verifyNoMoreInteractions();