]> git.basschouten.com Git - openhab-addons.git/blob
1b489f898ab787e8d640d64bc4a6f7d1de04aa35
[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.userdefinedstate;
14
15 import static org.junit.jupiter.api.Assertions.assertEquals;
16 import static org.junit.jupiter.api.Assertions.assertNotNull;
17 import static org.junit.jupiter.api.Assertions.assertTrue;
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.reset;
24 import static org.mockito.Mockito.verify;
25
26 import java.util.Map;
27 import java.util.UUID;
28 import java.util.concurrent.ExecutionException;
29 import java.util.concurrent.TimeoutException;
30 import java.util.stream.Stream;
31
32 import org.eclipse.jdt.annotation.NonNullByDefault;
33 import org.junit.jupiter.api.Test;
34 import org.junit.jupiter.params.ParameterizedTest;
35 import org.junit.jupiter.params.provider.Arguments;
36 import org.junit.jupiter.params.provider.MethodSource;
37 import org.mockito.ArgumentCaptor;
38 import org.openhab.binding.boschshc.internal.devices.AbstractBoschSHCHandlerTest;
39 import org.openhab.binding.boschshc.internal.devices.BoschSHCBindingConstants;
40 import org.openhab.binding.boschshc.internal.exceptions.BoschSHCException;
41 import org.openhab.binding.boschshc.internal.services.userstate.dto.UserStateServiceState;
42 import org.openhab.core.config.core.Configuration;
43 import org.openhab.core.library.types.OnOffType;
44 import org.openhab.core.thing.ChannelUID;
45 import org.openhab.core.thing.ThingStatus;
46 import org.openhab.core.thing.ThingStatusDetail;
47 import org.openhab.core.thing.ThingStatusInfo;
48 import org.openhab.core.thing.ThingTypeUID;
49 import org.openhab.core.thing.ThingUID;
50
51 /**
52  * Unit tests for UserStateHandlerTest
53  *
54  * @author Patrick Gell - Initial contribution
55  */
56 @NonNullByDefault
57 class UserStateHandlerTest extends AbstractBoschSHCHandlerTest<UserStateHandler> {
58
59     private final Configuration config = new Configuration(Map.of("id", UUID.randomUUID().toString()));
60
61     @Override
62     protected UserStateHandler createFixture() {
63         return new UserStateHandler(getThing());
64     }
65
66     @Override
67     protected ThingTypeUID getThingTypeUID() {
68         return BoschSHCBindingConstants.THING_TYPE_USER_DEFINED_STATE;
69     }
70
71     @Override
72     protected Configuration getConfiguration() {
73         return config;
74     }
75
76     @Test
77     void testHandleCommandSetState()
78             throws InterruptedException, TimeoutException, ExecutionException, BoschSHCException {
79         var channel = new ChannelUID(getThing().getUID(), BoschSHCBindingConstants.CHANNEL_USER_DEFINED_STATE);
80         getFixture().handleCommand(channel, OnOffType.ON);
81
82         ArgumentCaptor<String> deviceId = ArgumentCaptor.forClass(String.class);
83         ArgumentCaptor<UserStateServiceState> stateClass = ArgumentCaptor.forClass(UserStateServiceState.class);
84
85         verify(getBridgeHandler()).getUserStateInfo(config.get("id").toString());
86         verify(getBridgeHandler()).getState(anyString(), anyString(), any());
87         verify(getBridgeHandler()).putState(deviceId.capture(), anyString(), stateClass.capture());
88
89         assertNotNull(deviceId.getValue());
90         assertEquals(channel.getThingUID().getId(), deviceId.getValue());
91
92         assertNotNull(stateClass.getValue());
93         assertTrue(stateClass.getValue().isState());
94     }
95
96     @ParameterizedTest()
97     @MethodSource("provideExceptions")
98     void testHandleCommandSetStateUpdatesThingStatusOnException(Exception mockException)
99             throws InterruptedException, TimeoutException, ExecutionException {
100         reset(getCallback());
101         lenient().when(getBridgeHandler().putState(anyString(), anyString(), any(UserStateServiceState.class)))
102                 .thenThrow(mockException);
103         var channel = new ChannelUID(getThing().getUID(), BoschSHCBindingConstants.CHANNEL_USER_DEFINED_STATE);
104         getFixture().handleCommand(channel, OnOffType.ON);
105
106         verify(getCallback()).getBridge(any(ThingUID.class));
107
108         ThingStatusInfo expectedStatusInfo = new ThingStatusInfo(ThingStatus.OFFLINE,
109                 ThingStatusDetail.COMMUNICATION_ERROR,
110                 String.format("Error while putting user-defined state for %s", channel.getThingUID().getId()));
111         verify(getCallback()).statusUpdated(same(getThing()), eq(expectedStatusInfo));
112     }
113
114     private static Stream<Arguments> provideExceptions() {
115         return Stream.of(Arguments.of(new TimeoutException("test exception")),
116                 Arguments.of(new InterruptedException("test exception")));
117     }
118 }