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.test;
15 import static org.mockito.ArgumentMatchers.any;
16 import static org.openhab.binding.onewire.internal.OwBindingConstants.THING_TYPE_OWSERVER;
18 import java.util.ArrayList;
19 import java.util.BitSet;
20 import java.util.HashMap;
21 import java.util.List;
24 import org.eclipse.jdt.annotation.NonNullByDefault;
25 import org.eclipse.jdt.annotation.Nullable;
26 import org.junit.After;
27 import org.junit.Assert;
28 import org.mockito.InOrder;
29 import org.mockito.Mock;
30 import org.mockito.Mockito;
31 import org.openhab.binding.onewire.internal.OwDynamicStateDescriptionProvider;
32 import org.openhab.binding.onewire.internal.OwException;
33 import org.openhab.binding.onewire.internal.handler.OwBaseThingHandler;
34 import org.openhab.binding.onewire.internal.handler.OwserverBridgeHandler;
35 import org.openhab.core.config.core.Configuration;
36 import org.openhab.core.library.types.DecimalType;
37 import org.openhab.core.library.types.OnOffType;
38 import org.openhab.core.test.java.JavaTest;
39 import org.openhab.core.thing.Bridge;
40 import org.openhab.core.thing.Channel;
41 import org.openhab.core.thing.Thing;
42 import org.openhab.core.thing.binding.ThingHandler;
43 import org.openhab.core.thing.binding.ThingHandlerCallback;
44 import org.openhab.core.thing.binding.builder.BridgeBuilder;
47 * Base class for thing handler tests.
49 * @author Jan N. Klug - Initial contribution
52 public abstract class AbstractThingHandlerTest extends JavaTest {
54 protected Map<String, Object> bridgeProperties = new HashMap<>();
55 protected Map<String, String> thingProperties = new HashMap<>();
56 protected Map<String, Object> thingConfiguration = new HashMap<>();
57 protected Map<String, Object> channelProperties = new HashMap<>();
61 protected ThingHandlerCallback thingHandlerCallback;
65 protected OwDynamicStateDescriptionProvider stateProvider;
69 protected ThingHandlerCallback bridgeHandlerCallback;
73 protected OwserverBridgeHandler bridgeHandler;
77 protected OwserverBridgeHandler secondBridgeHandler;
79 protected List<Channel> channels = new ArrayList<>();
81 protected @Nullable Bridge bridge;
82 protected @Nullable Thing thing;
83 protected @Nullable OwBaseThingHandler thingHandler;
85 protected @Nullable InOrder inOrder;
88 public void tearDown() {
89 final ThingHandler thingHandler = this.thingHandler;
90 if (thingHandler != null) {
91 thingHandler.dispose();
95 protected void initializeHandlerMocks() {
96 final ThingHandler thingHandler = this.thingHandler;
97 if (thingHandler == null) {
98 Assert.fail("thingHandler is null");
102 thingHandler.getThing().setHandler(thingHandler);
103 thingHandler.setCallback(thingHandlerCallback);
105 Mockito.doAnswer(answer -> {
106 ((Thing) answer.getArgument(0)).setStatusInfo(answer.getArgument(1));
108 }).when(thingHandlerCallback).statusUpdated(any(), any());
110 inOrder = Mockito.inOrder(bridgeHandler);
113 public void initializeBridge() throws OwException {
114 bridgeProperties = new HashMap<>();
115 final Bridge bridge = BridgeBuilder.create(THING_TYPE_OWSERVER, "testbridge").withLabel("Test Bridge")
116 .withConfiguration(new Configuration(bridgeProperties)).build();
117 bridge.setHandler(bridgeHandler);
118 this.bridge = bridge;
120 Mockito.doAnswer(answer -> {
121 ((Thing) answer.getArgument(0)).setStatusInfo(answer.getArgument(1));
123 }).when(bridgeHandlerCallback).statusUpdated(any(), any());
125 Mockito.doAnswer(answer -> OnOffType.ON).when(bridgeHandler).checkPresence(any());
127 Mockito.doAnswer(answer -> new DecimalType(10)).when(bridgeHandler).readDecimalType(any(), any());
129 Mockito.doAnswer(answer -> new BitSet(8)).when(bridgeHandler).readBitSet(any(), any());
131 Mockito.doAnswer(answer -> {
132 final OwBaseThingHandler thingHandler = this.thingHandler;
133 if (thingHandler == null) {
134 Assert.fail("thingHandler is null");
138 thingHandler.updateSensorProperties(secondBridgeHandler);
139 thingHandler.initialize();
141 }).when(bridgeHandler).scheduleForPropertiesUpdate(any());