]> git.basschouten.com Git - openhab-addons.git/blob
41dd7784d60c5558d7ddcb33995d7dae6f92d58a
[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.argThat;
16 import static org.mockito.ArgumentMatchers.eq;
17 import static org.mockito.Mockito.verify;
18 import static org.mockito.Mockito.when;
19
20 import java.util.concurrent.ExecutionException;
21 import java.util.concurrent.TimeoutException;
22
23 import org.eclipse.jdt.annotation.NonNullByDefault;
24 import org.junit.jupiter.api.Test;
25 import org.junit.jupiter.params.ParameterizedTest;
26 import org.junit.jupiter.params.provider.MethodSource;
27 import org.openhab.binding.boschshc.internal.devices.bridge.dto.Device;
28 import org.openhab.binding.boschshc.internal.exceptions.BoschSHCException;
29 import org.openhab.core.config.core.Configuration;
30 import org.openhab.core.thing.ThingStatus;
31 import org.openhab.core.thing.ThingStatusDetail;
32
33 /**
34  * Abstract unit test implementation for device handlers.
35  *
36  * @author David Pace - Initial contribution
37  *
38  * @param <T> type of the device handler to be tested
39  */
40 @NonNullByDefault
41 public abstract class AbstractBoschSHCDeviceHandlerTest<T extends BoschSHCDeviceHandler>
42         extends AbstractBoschSHCHandlerTest<T> {
43
44     @Override
45     protected void configureDevice(Device device) {
46         super.configureDevice(device);
47
48         device.id = getDeviceID();
49     }
50
51     @Override
52     protected Configuration getConfiguration() {
53         Configuration configuration = super.getConfiguration();
54         configuration.put("id", getDeviceID());
55         return configuration;
56     }
57
58     protected abstract String getDeviceID();
59
60     @Test
61     void initializeInvalidDeviceId() {
62         getFixture().getThing().getConfiguration().remove("id");
63         getFixture().initialize();
64
65         verify(getCallback()).statusUpdated(eq(getThing()),
66                 argThat(status -> status.getStatus().equals(ThingStatus.OFFLINE)
67                         && status.getStatusDetail().equals(ThingStatusDetail.CONFIGURATION_ERROR)));
68     }
69
70     @ParameterizedTest
71     @MethodSource("org.openhab.binding.boschshc.internal.tests.common.CommonTestUtils#getExecutionExceptionAndInterruptedExceptionArguments()")
72     void initializeHandleExceptionDuringDeviceInfoRestCall(Exception exception)
73             throws BoschSHCException, InterruptedException, TimeoutException, ExecutionException {
74         when(getBridgeHandler().getDeviceInfo(getDeviceID())).thenThrow(exception);
75
76         getFixture().initialize();
77
78         verify(getCallback()).statusUpdated(eq(getThing()),
79                 argThat(status -> status.getStatus().equals(ThingStatus.OFFLINE)
80                         && status.getStatusDetail().equals(ThingStatusDetail.CONFIGURATION_ERROR)));
81     }
82 }