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.handler.EDSSensorThingHandler;
27 import org.openhab.binding.onewire.internal.handler.OwBaseThingHandler;
28 import org.openhab.binding.onewire.test.AbstractThingHandlerTest;
29 import org.openhab.core.config.core.Configuration;
30 import org.openhab.core.thing.Bridge;
31 import org.openhab.core.thing.ChannelUID;
32 import org.openhab.core.thing.Thing;
33 import org.openhab.core.thing.ThingStatus;
34 import org.openhab.core.thing.ThingUID;
35 import org.openhab.core.thing.binding.ThingHandler;
36 import org.openhab.core.thing.binding.builder.ChannelBuilder;
37 import org.openhab.core.thing.binding.builder.ThingBuilder;
40 * Tests cases for {@link EDSSensorThingHandler}.
42 * @author Jan N. Klug - Initial contribution
45 public class EDSSensorThingHandlerTest extends AbstractThingHandlerTest {
46 private static final String TEST_ID = "00.000000000000";
47 private static final ThingUID THING_UID = new ThingUID(THING_TYPE_EDS_ENV, "testthing");
48 private static final ChannelUID CHANNEL_UID_TEMPERATURE = new ChannelUID(THING_UID, CHANNEL_TEMPERATURE);
49 private static final ChannelUID CHANNEL_UID_HUMIDITY = new ChannelUID(THING_UID, CHANNEL_HUMIDITY);
50 private static final ChannelUID CHANNEL_UID_ABSOLUTE_HUMIDITY = new ChannelUID(THING_UID,
51 CHANNEL_ABSOLUTE_HUMIDITY);
52 private static final ChannelUID CHANNEL_UID_DEWPOINT = new ChannelUID(THING_UID, CHANNEL_DEWPOINT);
55 public void setup() throws OwException {
58 final Bridge bridge = this.bridge;
60 fail("bridge is null");
64 thingConfiguration.put(CONFIG_ID, TEST_ID);
66 channels.add(ChannelBuilder.create(CHANNEL_UID_TEMPERATURE, "Number:Temperature").build());
67 channels.add(ChannelBuilder.create(CHANNEL_UID_HUMIDITY, "Number:Dimensionless").build());
68 channels.add(ChannelBuilder.create(CHANNEL_UID_ABSOLUTE_HUMIDITY, "Number:Density").build());
69 channels.add(ChannelBuilder.create(CHANNEL_UID_DEWPOINT, "Number:Temperature").build());
71 thing = ThingBuilder.create(THING_TYPE_EDS_ENV, "testthing").withLabel("Test thing").withChannels(channels)
72 .withConfiguration(new Configuration(thingConfiguration)).withProperties(thingProperties)
73 .withBridge(bridge.getUID()).build();
75 final Thing thing = this.thing;
77 fail("thing is null");
81 thingHandler = new EDSSensorThingHandler(thing, stateProvider) {
83 protected @Nullable Bridge getBridge() {
88 initializeHandlerMocks();
90 Mockito.doAnswer(answer -> {
91 return new OwPageBuffer("EDS0065 ".getBytes());
92 }).when(secondBridgeHandler).readPages(any());
96 public void testInitializationEndsWithUnknown() {
97 final ThingHandler thingHandler = this.thingHandler;
98 if (thingHandler == null) {
99 fail("thingHandler is null");
103 thingHandler.initialize();
105 waitForAssert(() -> assertEquals(ThingStatus.UNKNOWN, thingHandler.getThing().getStatusInfo().getStatus()));
109 public void testRefresh() throws OwException {
110 final OwBaseThingHandler thingHandler = this.thingHandler;
111 final InOrder inOrder = this.inOrder;
112 if (thingHandler == null || inOrder == null) {
113 fail("prerequisite is null");
117 thingHandler.initialize();
119 // needed to determine initialization is finished
120 waitForAssert(() -> assertEquals(ThingStatus.UNKNOWN, thingHandler.getThing().getStatusInfo().getStatus()));
122 thingHandler.refresh(bridgeHandler, System.currentTimeMillis());
124 inOrder.verify(bridgeHandler, times(1)).checkPresence(new SensorId(TEST_ID));
125 inOrder.verify(bridgeHandler, times(2)).readDecimalType(eq(new SensorId(TEST_ID)), any());
127 inOrder.verifyNoMoreInteractions();