2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.boschshc.internal.devices.userdefinedstate;
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;
27 import java.util.UUID;
28 import java.util.concurrent.ExecutionException;
29 import java.util.concurrent.TimeoutException;
30 import java.util.stream.Stream;
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;
52 * Unit tests for UserStateHandlerTest
54 * @author Patrick Gell - Initial contribution
57 class UserStateHandlerTest extends AbstractBoschSHCHandlerTest<UserStateHandler> {
59 private final Configuration config = new Configuration(Map.of("id", UUID.randomUUID().toString()));
62 protected UserStateHandler createFixture() {
63 return new UserStateHandler(getThing());
67 protected ThingTypeUID getThingTypeUID() {
68 return BoschSHCBindingConstants.THING_TYPE_USER_DEFINED_STATE;
72 protected Configuration getConfiguration() {
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);
82 ArgumentCaptor<String> deviceId = ArgumentCaptor.forClass(String.class);
83 ArgumentCaptor<UserStateServiceState> stateClass = ArgumentCaptor.forClass(UserStateServiceState.class);
85 verify(getBridgeHandler()).getUserStateInfo(config.get("id").toString());
86 verify(getBridgeHandler()).getState(anyString(), anyString(), any());
87 verify(getBridgeHandler()).putState(deviceId.capture(), anyString(), stateClass.capture());
89 assertNotNull(deviceId.getValue());
90 assertEquals(channel.getThingUID().getId(), deviceId.getValue());
92 assertNotNull(stateClass.getValue());
93 assertTrue(stateClass.getValue().isState());
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);
106 verify(getCallback()).getBridge(any(ThingUID.class));
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));
114 private static Stream<Arguments> provideExceptions() {
115 return Stream.of(Arguments.of(new TimeoutException("test exception")),
116 Arguments.of(new InterruptedException("test exception")));