]> git.basschouten.com Git - openhab-addons.git/blob
c8efb9c9b31592af115a460340b0986914ab8a1f
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2024 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.boschshc.internal.devices;
14
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;
22
23 import java.util.concurrent.ExecutionException;
24 import java.util.concurrent.TimeoutException;
25
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;
45
46 /**
47  * Abstract unit test implementation for all types of handlers.
48  *
49  * @author David Pace - Initial contribution
50  *
51  * @param <T> type of the handler to be tested
52  */
53 @NonNullByDefault
54 @ExtendWith(MockitoExtension.class)
55 public abstract class AbstractBoschSHCHandlerTest<T extends BoschSHCHandler> {
56
57     private T fixture;
58
59     private @Mock @NonNullByDefault({}) Thing thing;
60
61     private @Mock @NonNullByDefault({}) Bridge bridge;
62
63     protected @Mock @NonNullByDefault({}) BridgeHandler bridgeHandler;
64
65     private @Mock @NonNullByDefault({}) ThingHandlerCallback callback;
66
67     private @NonNullByDefault({}) Device device;
68
69     protected AbstractBoschSHCHandlerTest() {
70         this.fixture = createFixture();
71     }
72
73     @BeforeEach
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());
82
83         device = new Device();
84         configureDevice(device);
85         lenient().when(bridgeHandler.getDeviceInfo(anyString())).thenReturn(device);
86
87         fixture.initialize();
88     }
89
90     protected abstract T createFixture();
91
92     protected T getFixture() {
93         return fixture;
94     }
95
96     protected ThingUID getThingUID() {
97         return new ThingUID(getThingTypeUID(), "abcdef");
98     }
99
100     protected abstract ThingTypeUID getThingTypeUID();
101
102     protected ChannelUID getChannelUID(String channelID) {
103         return new ChannelUID(getThingUID(), channelID);
104     }
105
106     protected Configuration getConfiguration() {
107         return new Configuration();
108     }
109
110     protected Thing getThing() {
111         return thing;
112     }
113
114     protected BridgeHandler getBridgeHandler() {
115         return bridgeHandler;
116     }
117
118     protected ThingHandlerCallback getCallback() {
119         return callback;
120     }
121
122     protected Device getDevice() {
123         return device;
124     }
125
126     protected void configureDevice(Device device) {
127         // abstract implementation is empty, subclasses may override
128     }
129
130     @Test
131     public void testInitialize() {
132         ThingStatusInfo expectedStatusInfo = new ThingStatusInfo(ThingStatus.ONLINE, ThingStatusDetail.NONE, null);
133         verify(callback).statusUpdated(same(thing), eq(expectedStatusInfo));
134     }
135 }