]> git.basschouten.com Git - openhab-addons.git/blob
4c35a1c2e5f8fbb8a3280f6f20e559b33e12113f
[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.*;
16 import static org.mockito.Mockito.*;
17
18 import java.util.concurrent.ExecutionException;
19 import java.util.concurrent.TimeoutException;
20
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22 import org.junit.jupiter.api.BeforeEach;
23 import org.junit.jupiter.api.Test;
24 import org.junit.jupiter.api.extension.ExtendWith;
25 import org.mockito.Mock;
26 import org.mockito.junit.jupiter.MockitoExtension;
27 import org.openhab.binding.boschshc.internal.devices.bridge.BridgeHandler;
28 import org.openhab.binding.boschshc.internal.exceptions.BoschSHCException;
29 import org.openhab.core.config.core.Configuration;
30 import org.openhab.core.thing.Bridge;
31 import org.openhab.core.thing.ChannelUID;
32 import org.openhab.core.thing.Thing;
33 import org.openhab.core.thing.ThingStatus;
34 import org.openhab.core.thing.ThingStatusDetail;
35 import org.openhab.core.thing.ThingStatusInfo;
36 import org.openhab.core.thing.ThingTypeUID;
37 import org.openhab.core.thing.ThingUID;
38 import org.openhab.core.thing.binding.ThingHandlerCallback;
39
40 /**
41  * Abstract unit test implementation for all types of handlers.
42  *
43  * @author David Pace - Initial contribution
44  *
45  * @param <T> type of the handler to be tested
46  */
47 @NonNullByDefault
48 @ExtendWith(MockitoExtension.class)
49 public abstract class AbstractBoschSHCHandlerTest<T extends BoschSHCHandler> {
50
51     private T fixture;
52
53     private @Mock @NonNullByDefault({}) Thing thing;
54
55     private @Mock @NonNullByDefault({}) Bridge bridge;
56
57     protected @Mock @NonNullByDefault({}) BridgeHandler bridgeHandler;
58
59     private @Mock @NonNullByDefault({}) ThingHandlerCallback callback;
60
61     protected AbstractBoschSHCHandlerTest() {
62         this.fixture = createFixture();
63     }
64
65     @BeforeEach
66     void beforeEach() throws InterruptedException, TimeoutException, ExecutionException, BoschSHCException {
67         fixture = createFixture();
68         lenient().when(thing.getUID()).thenReturn(getThingUID());
69         when(thing.getBridgeUID()).thenReturn(new ThingUID("boschshc", "shc", "myBridgeUID"));
70         when(callback.getBridge(any())).thenReturn(bridge);
71         fixture.setCallback(callback);
72         when(bridge.getHandler()).thenReturn(bridgeHandler);
73         lenient().when(thing.getConfiguration()).thenReturn(getConfiguration());
74
75         fixture.initialize();
76     }
77
78     protected abstract T createFixture();
79
80     protected T getFixture() {
81         return fixture;
82     }
83
84     protected ThingUID getThingUID() {
85         return new ThingUID(getThingTypeUID(), "abcdef");
86     }
87
88     protected abstract ThingTypeUID getThingTypeUID();
89
90     protected ChannelUID getChannelUID(String channelID) {
91         return new ChannelUID(getThingUID(), channelID);
92     }
93
94     protected Configuration getConfiguration() {
95         return new Configuration();
96     }
97
98     protected Thing getThing() {
99         return thing;
100     }
101
102     protected BridgeHandler getBridgeHandler() {
103         return bridgeHandler;
104     }
105
106     protected ThingHandlerCallback getCallback() {
107         return callback;
108     }
109
110     @Test
111     public void testInitialize() {
112         ThingStatusInfo expectedStatusInfo = new ThingStatusInfo(ThingStatus.ONLINE, ThingStatusDetail.NONE, null);
113         verify(callback).statusUpdated(same(thing), eq(expectedStatusInfo));
114     }
115 }