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.*;
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22 import org.eclipse.jdt.annotation.Nullable;
23 import org.junit.Assert;
24 import org.junit.Before;
25 import org.junit.Test;
26 import org.mockito.InOrder;
27 import org.mockito.Mockito;
28 import org.mockito.MockitoAnnotations;
29 import org.openhab.binding.onewire.internal.handler.EDSSensorThingHandler;
30 import org.openhab.binding.onewire.internal.handler.OwBaseThingHandler;
31 import org.openhab.binding.onewire.test.AbstractThingHandlerTest;
32 import org.openhab.core.config.core.Configuration;
33 import org.openhab.core.thing.*;
34 import org.openhab.core.thing.binding.ThingHandler;
35 import org.openhab.core.thing.binding.builder.ChannelBuilder;
36 import org.openhab.core.thing.binding.builder.ThingBuilder;
39 * Tests cases for {@link EDSSensorThingHandler}.
41 * @author Jan N. Klug - Initial contribution
44 public class EDSSensorThingHandlerTest extends AbstractThingHandlerTest {
45 private static final String TEST_ID = "00.000000000000";
46 private static final ThingUID THING_UID = new ThingUID(THING_TYPE_EDS_ENV, "testthing");
47 private static final ChannelUID CHANNEL_UID_TEMPERATURE = new ChannelUID(THING_UID, CHANNEL_TEMPERATURE);
48 private static final ChannelUID CHANNEL_UID_HUMIDITY = new ChannelUID(THING_UID, CHANNEL_HUMIDITY);
49 private static final ChannelUID CHANNEL_UID_ABSOLUTE_HUMIDITY = new ChannelUID(THING_UID,
50 CHANNEL_ABSOLUTE_HUMIDITY);
51 private static final ChannelUID CHANNEL_UID_DEWPOINT = new ChannelUID(THING_UID, CHANNEL_DEWPOINT);
54 public void setup() throws OwException {
55 MockitoAnnotations.initMocks(this);
59 final Bridge bridge = this.bridge;
61 Assert.fail("bridge is null");
65 thingConfiguration.put(CONFIG_ID, TEST_ID);
67 channels.add(ChannelBuilder.create(CHANNEL_UID_TEMPERATURE, "Number:Temperature").build());
68 channels.add(ChannelBuilder.create(CHANNEL_UID_HUMIDITY, "Number:Dimensionless").build());
69 channels.add(ChannelBuilder.create(CHANNEL_UID_ABSOLUTE_HUMIDITY, "Number:Density").build());
70 channels.add(ChannelBuilder.create(CHANNEL_UID_DEWPOINT, "Number:Temperature").build());
72 thing = ThingBuilder.create(THING_TYPE_EDS_ENV, "testthing").withLabel("Test thing").withChannels(channels)
73 .withConfiguration(new Configuration(thingConfiguration)).withProperties(thingProperties)
74 .withBridge(bridge.getUID()).build();
76 final Thing thing = this.thing;
78 Assert.fail("thing is null");
82 thingHandler = new EDSSensorThingHandler(thing, stateProvider) {
84 protected @Nullable Bridge getBridge() {
89 initializeHandlerMocks();
91 Mockito.doAnswer(answer -> {
92 return new OwPageBuffer("EDS0065 ".getBytes());
93 }).when(secondBridgeHandler).readPages(any());
97 public void testInitializationEndsWithUnknown() {
98 final ThingHandler thingHandler = this.thingHandler;
99 if (thingHandler == null) {
100 Assert.fail("thingHandler is null");
104 thingHandler.initialize();
106 waitForAssert(() -> assertEquals(ThingStatus.UNKNOWN, thingHandler.getThing().getStatusInfo().getStatus()));
110 public void testRefresh() throws OwException {
111 final OwBaseThingHandler thingHandler = this.thingHandler;
112 final InOrder inOrder = this.inOrder;
113 if (thingHandler == null || inOrder == null) {
114 Assert.fail("prerequisite is null");
118 thingHandler.initialize();
120 // needed to determine initialization is finished
121 waitForAssert(() -> assertEquals(ThingStatus.UNKNOWN, thingHandler.getThing().getStatusInfo().getStatus()));
123 thingHandler.refresh(bridgeHandler, System.currentTimeMillis());
125 inOrder.verify(bridgeHandler, times(1)).checkPresence(new SensorId(TEST_ID));
126 inOrder.verify(bridgeHandler, times(2)).readDecimalType(eq(new SensorId(TEST_ID)), any());
128 inOrder.verifyNoMoreInteractions();