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