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_MS_TX;
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.BasicMultisensorThingHandler;
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 BasicMultisensorThingHandler}.
44 * @author Jan N. Klug - Initial contribution
47 public class MultisensorThingHandlerTest 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_MS_TX, "testthing").withLabel("Test thing").withChannels(channels)
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 BasicMultisensorThingHandler(thing, stateProvider) {
76 protected @Nullable Bridge getBridge() {
81 initializeHandlerMocks();
83 Mockito.doAnswer(answer -> {
84 return OwSensorType.DS2438;
85 }).when(secondBridgeHandler).getType(any());
87 Mockito.doAnswer(answer -> {
88 OwPageBuffer pageBuffer = new OwPageBuffer(8);
89 pageBuffer.setByte(3, 0, (byte) 0x19);
91 }).when(secondBridgeHandler).readPages(any());
95 public void testInitializationEndsWithUnknown() {
96 final ThingHandler thingHandler = this.thingHandler;
97 if (thingHandler == null) {
98 Assert.fail("thingHandler is null");
101 thingHandler.initialize();
103 waitForAssert(() -> assertEquals(ThingStatus.UNKNOWN, thingHandler.getThing().getStatusInfo().getStatus()));
107 public void testRefresh() throws OwException {
108 final OwBaseThingHandler thingHandler = this.thingHandler;
109 final InOrder inOrder = this.inOrder;
110 if (thingHandler == null || inOrder == null) {
111 Assert.fail("prerequisite is null");
114 thingHandler.initialize();
116 // needed to determine initialization is finished
117 waitForAssert(() -> assertEquals(ThingStatus.UNKNOWN, thingHandler.getThing().getStatusInfo().getStatus()));
119 thingHandler.refresh(bridgeHandler, System.currentTimeMillis());
121 inOrder.verify(bridgeHandler, times(1)).checkPresence(new SensorId(TEST_ID));
122 inOrder.verify(bridgeHandler, times(3)).readDecimalType(eq(new SensorId(TEST_ID)), any());
124 inOrder.verifyNoMoreInteractions();