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.ArgumentMatchers.same;
22 import static org.mockito.Mockito.lenient;
23 import static org.mockito.Mockito.verify;
24 import static org.mockito.Mockito.when;
26 import java.util.concurrent.ExecutionException;
27 import java.util.concurrent.TimeoutException;
29 import org.eclipse.jdt.annotation.NonNullByDefault;
30 import org.junit.jupiter.api.BeforeEach;
31 import org.junit.jupiter.api.Test;
32 import org.junit.jupiter.api.extension.ExtendWith;
33 import org.mockito.Mock;
34 import org.mockito.junit.jupiter.MockitoExtension;
35 import org.openhab.binding.boschshc.internal.devices.bridge.BridgeHandler;
36 import org.openhab.binding.boschshc.internal.devices.bridge.dto.Device;
37 import org.openhab.binding.boschshc.internal.exceptions.BoschSHCException;
38 import org.openhab.core.config.core.Configuration;
39 import org.openhab.core.thing.Bridge;
40 import org.openhab.core.thing.ChannelUID;
41 import org.openhab.core.thing.Thing;
42 import org.openhab.core.thing.ThingStatus;
43 import org.openhab.core.thing.ThingStatusDetail;
44 import org.openhab.core.thing.ThingStatusInfo;
45 import org.openhab.core.thing.ThingTypeUID;
46 import org.openhab.core.thing.ThingUID;
47 import org.openhab.core.thing.binding.ThingHandlerCallback;
50 * Abstract unit test implementation for all types of handlers.
52 * @author David Pace - Initial contribution
54 * @param <T> type of the handler to be tested
57 @ExtendWith(MockitoExtension.class)
58 public abstract class AbstractBoschSHCHandlerTest<T extends BoschSHCHandler> {
62 private @Mock @NonNullByDefault({}) Thing thing;
64 private @Mock @NonNullByDefault({}) Bridge bridge;
66 private @Mock @NonNullByDefault({}) BridgeHandler bridgeHandler;
68 private @Mock @NonNullByDefault({}) ThingHandlerCallback callback;
70 private @NonNullByDefault({}) Device device;
72 protected AbstractBoschSHCHandlerTest() {
73 this.fixture = createFixture();
77 void beforeEach() throws InterruptedException, TimeoutException, ExecutionException, BoschSHCException {
78 fixture = createFixture();
79 lenient().when(thing.getUID()).thenReturn(getThingUID());
80 when(thing.getBridgeUID()).thenReturn(new ThingUID("boschshc", "shc", "myBridgeUID"));
81 when(callback.getBridge(any())).thenReturn(bridge);
82 fixture.setCallback(callback);
83 when(bridge.getHandler()).thenReturn(bridgeHandler);
84 lenient().when(thing.getConfiguration()).thenReturn(getConfiguration());
86 device = new Device();
87 configureDevice(device);
88 lenient().when(bridgeHandler.getDeviceInfo(anyString())).thenReturn(device);
93 protected abstract T createFixture();
95 protected T getFixture() {
99 protected ThingUID getThingUID() {
100 return new ThingUID(getThingTypeUID(), "abcdef");
103 protected abstract ThingTypeUID getThingTypeUID();
105 protected ChannelUID getChannelUID(String channelID) {
106 return new ChannelUID(getThingUID(), channelID);
109 protected Configuration getConfiguration() {
110 return new Configuration();
113 protected Thing getThing() {
117 protected BridgeHandler getBridgeHandler() {
118 return bridgeHandler;
121 protected ThingHandlerCallback getCallback() {
125 protected Device getDevice() {
129 protected void configureDevice(Device device) {
130 // abstract implementation is empty, subclasses may override
134 void testInitialize() {
135 ThingStatusInfo expectedStatusInfo = new ThingStatusInfo(ThingStatus.ONLINE, ThingStatusDetail.NONE, null);
136 verify(callback).statusUpdated(same(thing), eq(expectedStatusInfo));
140 void testGetBridgeHandler() throws BoschSHCException {
141 assertThat(fixture.getBridgeHandler(), sameInstance(bridgeHandler));
145 void testGetBridgeHandlerThrowExceptionIfBridgeIsNull() throws BoschSHCException {
146 when(callback.getBridge(any())).thenReturn(null);
147 assertThrows(BoschSHCException.class, () -> fixture.getBridgeHandler());
151 void testGetBridgeHandlerThrowExceptionIfBridgeHandlerIsNull() throws BoschSHCException {
152 when(bridge.getHandler()).thenReturn(null);
153 assertThrows(BoschSHCException.class, () -> fixture.getBridgeHandler());