]> git.basschouten.com Git - openhab-addons.git/blob
2c978562a6c615207ca19fe96e9b404ff9af8193
[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.mqtt.generic;
14
15 import static org.hamcrest.CoreMatchers.is;
16 import static org.hamcrest.MatcherAssert.assertThat;
17 import static org.mockito.ArgumentMatchers.*;
18 import static org.mockito.Mockito.*;
19 import static org.openhab.binding.mqtt.generic.internal.handler.ThingChannelConstants.*;
20
21 import java.util.concurrent.CompletableFuture;
22
23 import org.eclipse.jdt.annotation.NonNullByDefault;
24 import org.eclipse.jdt.annotation.Nullable;
25 import org.junit.jupiter.api.BeforeEach;
26 import org.junit.jupiter.api.Test;
27 import org.junit.jupiter.api.extension.ExtendWith;
28 import org.mockito.Mock;
29 import org.mockito.junit.jupiter.MockitoExtension;
30 import org.mockito.junit.jupiter.MockitoSettings;
31 import org.mockito.quality.Strictness;
32 import org.openhab.binding.mqtt.generic.internal.handler.GenericMQTTThingHandler;
33 import org.openhab.binding.mqtt.handler.AbstractBrokerHandler;
34 import org.openhab.core.config.core.Configuration;
35 import org.openhab.core.io.transport.mqtt.MqttBrokerConnection;
36 import org.openhab.core.thing.Thing;
37 import org.openhab.core.thing.ThingStatus;
38 import org.openhab.core.thing.ThingStatusDetail;
39 import org.openhab.core.thing.ThingStatusInfo;
40 import org.openhab.core.thing.binding.ThingHandler;
41 import org.openhab.core.thing.binding.ThingHandlerCallback;
42 import org.openhab.core.transform.TransformationService;
43
44 /**
45  * Tests cases for {@link ThingHandler} to test the json transformation.
46  *
47  * @author David Graeff - Initial contribution
48  */
49 @ExtendWith(MockitoExtension.class)
50 @MockitoSettings(strictness = Strictness.LENIENT)
51 @NonNullByDefault
52 public class ChannelStateTransformationTests {
53
54     private @Mock @NonNullByDefault({}) TransformationService jsonPathServiceMock;
55     private @Mock @NonNullByDefault({}) TransformationServiceProvider transformationServiceProviderMock;
56     private @Mock @NonNullByDefault({}) ThingHandlerCallback callbackMock;
57     private @Mock @NonNullByDefault({}) Thing thingMock;
58     private @Mock @NonNullByDefault({}) AbstractBrokerHandler bridgeHandlerMock;
59     private @Mock @NonNullByDefault({}) MqttBrokerConnection connectionMock;
60
61     private @NonNullByDefault({}) GenericMQTTThingHandler thingHandler;
62
63     @BeforeEach
64     public void setUp() throws Exception {
65         ThingStatusInfo thingStatus = new ThingStatusInfo(ThingStatus.ONLINE, ThingStatusDetail.NONE, null);
66
67         // Mock the thing: We need the thingUID and the bridgeUID
68         when(thingMock.getUID()).thenReturn(TEST_GENERIC_THING);
69         when(thingMock.getChannels()).thenReturn(THING_CHANNEL_LIST_WITH_JSON);
70         when(thingMock.getStatusInfo()).thenReturn(thingStatus);
71         when(thingMock.getConfiguration()).thenReturn(new Configuration());
72
73         // Return the mocked connection object if the bridge handler is asked for it
74         when(bridgeHandlerMock.getConnectionAsync()).thenReturn(CompletableFuture.completedFuture(connectionMock));
75
76         CompletableFuture<@Nullable Void> voidFutureComplete = new CompletableFuture<>();
77         voidFutureComplete.complete(null);
78         doReturn(voidFutureComplete).when(connectionMock).unsubscribeAll();
79         doReturn(CompletableFuture.completedFuture(true)).when(connectionMock).subscribe(any(), any());
80         doReturn(CompletableFuture.completedFuture(true)).when(connectionMock).unsubscribe(any(), any());
81
82         thingHandler = spy(new GenericMQTTThingHandler(thingMock, mock(MqttChannelStateDescriptionProvider.class),
83                 transformationServiceProviderMock, 1500));
84         when(transformationServiceProviderMock.getTransformationService(anyString())).thenReturn(jsonPathServiceMock);
85
86         thingHandler.setCallback(callbackMock);
87         // Return the bridge handler if the thing handler asks for it
88         doReturn(bridgeHandlerMock).when(thingHandler).getBridgeHandler();
89
90         // We are by default online
91         doReturn(thingStatus).when(thingHandler).getBridgeStatus();
92     }
93
94     @SuppressWarnings("null")
95     @Test
96     public void initialize() throws Exception {
97         when(thingMock.getChannels()).thenReturn(THING_CHANNEL_LIST_WITH_JSON);
98
99         thingHandler.initialize();
100         ChannelState channelConfig = thingHandler.getChannelState(TEXT_CHANNEL_UID);
101         assertThat(channelConfig.transformationsIn.get(0).pattern, is(JSON_PATH_PATTERN));
102     }
103
104     @SuppressWarnings("null")
105     @Test
106     public void processMessageWithJSONPath() throws Exception {
107         when(jsonPathServiceMock.transform(JSON_PATH_PATTERN, JSON_PATH_JSON)).thenReturn("23.2");
108
109         thingHandler.initialize();
110         ChannelState channelConfig = thingHandler.getChannelState(TEXT_CHANNEL_UID);
111         channelConfig.setChannelStateUpdateListener(thingHandler);
112
113         ChannelStateTransformation transformation = channelConfig.transformationsIn.get(0);
114
115         byte[] payload = JSON_PATH_JSON.getBytes();
116         assertThat(transformation.pattern, is(JSON_PATH_PATTERN));
117         // Test process message
118         channelConfig.processMessage(channelConfig.getStateTopic(), payload);
119
120         verify(callbackMock).stateUpdated(eq(TEXT_CHANNEL_UID), argThat(arg -> "23.2".equals(arg.toString())));
121         assertThat(channelConfig.getCache().getChannelState().toString(), is("23.2"));
122     }
123 }