2 * Copyright (c) 2010-2024 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.boschshc.internal.devices;
15 import static org.mockito.ArgumentMatchers.any;
16 import static org.mockito.ArgumentMatchers.anyString;
17 import static org.mockito.ArgumentMatchers.eq;
18 import static org.mockito.ArgumentMatchers.same;
19 import static org.mockito.Mockito.lenient;
20 import static org.mockito.Mockito.verify;
21 import static org.mockito.Mockito.when;
23 import java.util.concurrent.ExecutionException;
24 import java.util.concurrent.TimeoutException;
26 import org.eclipse.jdt.annotation.NonNullByDefault;
27 import org.junit.jupiter.api.BeforeEach;
28 import org.junit.jupiter.api.Test;
29 import org.junit.jupiter.api.extension.ExtendWith;
30 import org.mockito.Mock;
31 import org.mockito.junit.jupiter.MockitoExtension;
32 import org.openhab.binding.boschshc.internal.devices.bridge.BridgeHandler;
33 import org.openhab.binding.boschshc.internal.devices.bridge.dto.Device;
34 import org.openhab.binding.boschshc.internal.exceptions.BoschSHCException;
35 import org.openhab.core.config.core.Configuration;
36 import org.openhab.core.thing.Bridge;
37 import org.openhab.core.thing.ChannelUID;
38 import org.openhab.core.thing.Thing;
39 import org.openhab.core.thing.ThingStatus;
40 import org.openhab.core.thing.ThingStatusDetail;
41 import org.openhab.core.thing.ThingStatusInfo;
42 import org.openhab.core.thing.ThingTypeUID;
43 import org.openhab.core.thing.ThingUID;
44 import org.openhab.core.thing.binding.ThingHandlerCallback;
47 * Abstract unit test implementation for all types of handlers.
49 * @author David Pace - Initial contribution
51 * @param <T> type of the handler to be tested
54 @ExtendWith(MockitoExtension.class)
55 public abstract class AbstractBoschSHCHandlerTest<T extends BoschSHCHandler> {
59 private @Mock @NonNullByDefault({}) Thing thing;
61 private @Mock @NonNullByDefault({}) Bridge bridge;
63 protected @Mock @NonNullByDefault({}) BridgeHandler bridgeHandler;
65 private @Mock @NonNullByDefault({}) ThingHandlerCallback callback;
67 private @NonNullByDefault({}) Device device;
69 protected AbstractBoschSHCHandlerTest() {
70 this.fixture = createFixture();
74 void beforeEach() throws InterruptedException, TimeoutException, ExecutionException, BoschSHCException {
75 fixture = createFixture();
76 lenient().when(thing.getUID()).thenReturn(getThingUID());
77 when(thing.getBridgeUID()).thenReturn(new ThingUID("boschshc", "shc", "myBridgeUID"));
78 when(callback.getBridge(any())).thenReturn(bridge);
79 fixture.setCallback(callback);
80 when(bridge.getHandler()).thenReturn(bridgeHandler);
81 lenient().when(thing.getConfiguration()).thenReturn(getConfiguration());
83 device = new Device();
84 configureDevice(device);
85 lenient().when(bridgeHandler.getDeviceInfo(anyString())).thenReturn(device);
90 protected abstract T createFixture();
92 protected T getFixture() {
96 protected ThingUID getThingUID() {
97 return new ThingUID(getThingTypeUID(), "abcdef");
100 protected abstract ThingTypeUID getThingTypeUID();
102 protected ChannelUID getChannelUID(String channelID) {
103 return new ChannelUID(getThingUID(), channelID);
106 protected Configuration getConfiguration() {
107 return new Configuration();
110 protected Thing getThing() {
114 protected BridgeHandler getBridgeHandler() {
115 return bridgeHandler;
118 protected ThingHandlerCallback getCallback() {
122 protected Device getDevice() {
126 protected void configureDevice(Device device) {
127 // abstract implementation is empty, subclasses may override
131 public void testInitialize() {
132 ThingStatusInfo expectedStatusInfo = new ThingStatusInfo(ThingStatus.ONLINE, ThingStatusDetail.NONE, null);
133 verify(callback).statusUpdated(same(thing), eq(expectedStatusInfo));