]> git.basschouten.com Git - openhab-addons.git/blob
755fd51357f6f9c85a50b3bdab97312c4de4c8e9
[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.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;
24
25 import java.util.concurrent.ExecutionException;
26 import java.util.concurrent.TimeoutException;
27
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;
47
48 /**
49  * Abstract unit test implementation for all types of handlers.
50  *
51  * @author David Pace - Initial contribution
52  *
53  * @param <T> type of the handler to be tested
54  */
55 @NonNullByDefault
56 @ExtendWith(MockitoExtension.class)
57 public abstract class AbstractBoschSHCHandlerTest<T extends BoschSHCHandler> {
58
59     private T fixture;
60
61     private @Mock @NonNullByDefault({}) Thing thing;
62
63     private @Mock @NonNullByDefault({}) Bridge bridge;
64
65     private @Mock @NonNullByDefault({}) BridgeHandler bridgeHandler;
66
67     private @Mock @NonNullByDefault({}) ThingHandlerCallback callback;
68
69     private @NonNullByDefault({}) Device device;
70
71     protected AbstractBoschSHCHandlerTest() {
72         this.fixture = createFixture();
73     }
74
75     @BeforeEach
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());
84
85         device = new Device();
86         configureDevice(device);
87         lenient().when(bridgeHandler.getDeviceInfo(anyString())).thenReturn(device);
88
89         fixture.initialize();
90     }
91
92     protected abstract T createFixture();
93
94     protected T getFixture() {
95         return fixture;
96     }
97
98     protected ThingUID getThingUID() {
99         return new ThingUID(getThingTypeUID(), "abcdef");
100     }
101
102     protected abstract ThingTypeUID getThingTypeUID();
103
104     protected ChannelUID getChannelUID(String channelID) {
105         return new ChannelUID(getThingUID(), channelID);
106     }
107
108     protected Configuration getConfiguration() {
109         return new Configuration();
110     }
111
112     protected Thing getThing() {
113         return thing;
114     }
115
116     protected BridgeHandler getBridgeHandler() {
117         return bridgeHandler;
118     }
119
120     protected ThingHandlerCallback getCallback() {
121         return callback;
122     }
123
124     protected Device getDevice() {
125         return device;
126     }
127
128     protected void configureDevice(Device device) {
129         // abstract implementation is empty, subclasses may override
130     }
131
132     @Test
133     void testInitialize() {
134         ThingStatusInfo expectedStatusInfo = new ThingStatusInfo(ThingStatus.ONLINE, ThingStatusDetail.NONE, null);
135         verify(callback).statusUpdated(any(Thing.class), eq(expectedStatusInfo));
136     }
137
138     @Test
139     void testGetBridgeHandler() throws BoschSHCException {
140         assertThat(fixture.getBridgeHandler(), sameInstance(bridgeHandler));
141     }
142
143     @Test
144     void testGetBridgeHandlerThrowExceptionIfBridgeIsNull() throws BoschSHCException {
145         when(callback.getBridge(any())).thenReturn(null);
146         assertThrows(BoschSHCException.class, () -> fixture.getBridgeHandler());
147     }
148
149     @Test
150     void testGetBridgeHandlerThrowExceptionIfBridgeHandlerIsNull() throws BoschSHCException {
151         when(bridge.getHandler()).thenReturn(null);
152         assertThrows(BoschSHCException.class, () -> fixture.getBridgeHandler());
153     }
154 }