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.hamcrest.MatcherAssert.assertThat;
16 import static org.hamcrest.Matchers.sameInstance;
17 import static org.junit.jupiter.api.Assertions.assertThrows;
18 import static org.mockito.ArgumentMatchers.any;
19 import static org.mockito.ArgumentMatchers.anyString;
20 import static org.mockito.ArgumentMatchers.eq;
21 import static org.mockito.Mockito.lenient;
22 import static org.mockito.Mockito.verify;
23 import static org.mockito.Mockito.when;
25 import java.util.concurrent.ExecutionException;
26 import java.util.concurrent.TimeoutException;
28 import org.eclipse.jdt.annotation.NonNullByDefault;
29 import org.junit.jupiter.api.BeforeEach;
30 import org.junit.jupiter.api.Test;
31 import org.junit.jupiter.api.extension.ExtendWith;
32 import org.mockito.Mock;
33 import org.mockito.junit.jupiter.MockitoExtension;
34 import org.openhab.binding.boschshc.internal.devices.bridge.BridgeHandler;
35 import org.openhab.binding.boschshc.internal.devices.bridge.dto.Device;
36 import org.openhab.binding.boschshc.internal.exceptions.BoschSHCException;
37 import org.openhab.core.config.core.Configuration;
38 import org.openhab.core.thing.Bridge;
39 import org.openhab.core.thing.ChannelUID;
40 import org.openhab.core.thing.Thing;
41 import org.openhab.core.thing.ThingStatus;
42 import org.openhab.core.thing.ThingStatusDetail;
43 import org.openhab.core.thing.ThingStatusInfo;
44 import org.openhab.core.thing.ThingTypeUID;
45 import org.openhab.core.thing.ThingUID;
46 import org.openhab.core.thing.binding.ThingHandlerCallback;
49 * Abstract unit test implementation for all types of handlers.
51 * @author David Pace - Initial contribution
53 * @param <T> type of the handler to be tested
56 @ExtendWith(MockitoExtension.class)
57 public abstract class AbstractBoschSHCHandlerTest<T extends BoschSHCHandler> {
61 private @Mock @NonNullByDefault({}) Thing thing;
63 private @Mock @NonNullByDefault({}) Bridge bridge;
65 private @Mock @NonNullByDefault({}) BridgeHandler bridgeHandler;
67 private @Mock @NonNullByDefault({}) ThingHandlerCallback callback;
69 private @NonNullByDefault({}) Device device;
71 protected AbstractBoschSHCHandlerTest() {
72 this.fixture = createFixture();
76 void beforeEach() throws InterruptedException, TimeoutException, ExecutionException, BoschSHCException {
77 fixture = createFixture();
78 lenient().when(thing.getUID()).thenReturn(getThingUID());
79 when(thing.getBridgeUID()).thenReturn(new ThingUID("boschshc", "shc", "myBridgeUID"));
80 when(callback.getBridge(any())).thenReturn(bridge);
81 fixture.setCallback(callback);
82 when(bridge.getHandler()).thenReturn(bridgeHandler);
83 lenient().when(thing.getConfiguration()).thenReturn(getConfiguration());
85 device = new Device();
86 configureDevice(device);
87 lenient().when(bridgeHandler.getDeviceInfo(anyString())).thenReturn(device);
92 protected abstract T createFixture();
94 protected T getFixture() {
98 protected ThingUID getThingUID() {
99 return new ThingUID(getThingTypeUID(), "abcdef");
102 protected abstract ThingTypeUID getThingTypeUID();
104 protected ChannelUID getChannelUID(String channelID) {
105 return new ChannelUID(getThingUID(), channelID);
108 protected Configuration getConfiguration() {
109 return new Configuration();
112 protected Thing getThing() {
116 protected BridgeHandler getBridgeHandler() {
117 return bridgeHandler;
120 protected ThingHandlerCallback getCallback() {
124 protected Device getDevice() {
128 protected void configureDevice(Device device) {
129 // abstract implementation is empty, subclasses may override
133 void testInitialize() {
134 ThingStatusInfo expectedStatusInfo = new ThingStatusInfo(ThingStatus.ONLINE, ThingStatusDetail.NONE, null);
135 verify(callback).statusUpdated(any(Thing.class), eq(expectedStatusInfo));
139 void testGetBridgeHandler() throws BoschSHCException {
140 assertThat(fixture.getBridgeHandler(), sameInstance(bridgeHandler));
144 void testGetBridgeHandlerThrowExceptionIfBridgeIsNull() throws BoschSHCException {
145 when(callback.getBridge(any())).thenReturn(null);
146 assertThrows(BoschSHCException.class, () -> fixture.getBridgeHandler());
150 void testGetBridgeHandlerThrowExceptionIfBridgeHandlerIsNull() throws BoschSHCException {
151 when(bridge.getHandler()).thenReturn(null);
152 assertThrows(BoschSHCException.class, () -> fixture.getBridgeHandler());