]> git.basschouten.com Git - openhab-addons.git/blob
807522b7112da49fb9845923e2d551fc5ccdd22e
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 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 javax.naming.ConfigurationException;
24
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.io.transport.mqtt.MqttException;
37 import org.openhab.core.thing.Thing;
38 import org.openhab.core.thing.ThingStatus;
39 import org.openhab.core.thing.ThingStatusDetail;
40 import org.openhab.core.thing.ThingStatusInfo;
41 import org.openhab.core.thing.binding.ThingHandler;
42 import org.openhab.core.thing.binding.ThingHandlerCallback;
43 import org.openhab.core.transform.TransformationService;
44
45 /**
46  * Tests cases for {@link ThingHandler} to test the json transformation.
47  *
48  * @author David Graeff - Initial contribution
49  */
50 @ExtendWith(MockitoExtension.class)
51 @MockitoSettings(strictness = Strictness.WARN)
52 public class ChannelStateTransformationTests {
53
54     private @Mock TransformationService jsonPathService;
55     private @Mock TransformationServiceProvider transformationServiceProvider;
56     private @Mock ThingHandlerCallback callback;
57     private @Mock Thing thing;
58     private @Mock AbstractBrokerHandler bridgeHandler;
59     private @Mock MqttBrokerConnection connection;
60
61     private GenericMQTTThingHandler thingHandler;
62
63     @BeforeEach
64     public void setUp() throws ConfigurationException, MqttException {
65         ThingStatusInfo thingStatus = new ThingStatusInfo(ThingStatus.ONLINE, ThingStatusDetail.NONE, null);
66
67         // Mock the thing: We need the thingUID and the bridgeUID
68         when(thing.getUID()).thenReturn(testGenericThing);
69         when(thing.getChannels()).thenReturn(thingChannelListWithJson);
70         when(thing.getStatusInfo()).thenReturn(thingStatus);
71         when(thing.getConfiguration()).thenReturn(new Configuration());
72
73         // Return the mocked connection object if the bridge handler is asked for it
74         when(bridgeHandler.getConnectionAsync()).thenReturn(CompletableFuture.completedFuture(connection));
75
76         CompletableFuture<Void> voidFutureComplete = new CompletableFuture<>();
77         voidFutureComplete.complete(null);
78         doReturn(voidFutureComplete).when(connection).unsubscribeAll();
79         doReturn(CompletableFuture.completedFuture(true)).when(connection).subscribe(any(), any());
80         doReturn(CompletableFuture.completedFuture(true)).when(connection).unsubscribe(any(), any());
81
82         thingHandler = spy(new GenericMQTTThingHandler(thing, mock(MqttChannelStateDescriptionProvider.class),
83                 transformationServiceProvider, 1500));
84         when(transformationServiceProvider.getTransformationService(anyString())).thenReturn(jsonPathService);
85
86         thingHandler.setCallback(callback);
87         // Return the bridge handler if the thing handler asks for it
88         doReturn(bridgeHandler).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 MqttException {
97         when(thing.getChannels()).thenReturn(thingChannelListWithJson);
98
99         thingHandler.initialize();
100         ChannelState channelConfig = thingHandler.getChannelState(textChannelUID);
101         assertThat(channelConfig.transformationsIn.get(0).pattern, is(jsonPathPattern));
102     }
103
104     @SuppressWarnings("null")
105     @Test
106     public void processMessageWithJSONPath() throws Exception {
107         when(jsonPathService.transform(jsonPathPattern, jsonPathJSON)).thenReturn("23.2");
108
109         thingHandler.initialize();
110         ChannelState channelConfig = thingHandler.getChannelState(textChannelUID);
111         channelConfig.setChannelStateUpdateListener(thingHandler);
112
113         ChannelStateTransformation transformation = channelConfig.transformationsIn.get(0);
114
115         byte payload[] = jsonPathJSON.getBytes();
116         assertThat(transformation.pattern, is(jsonPathPattern));
117         // Test process message
118         channelConfig.processMessage(channelConfig.getStateTopic(), payload);
119
120         verify(callback).stateUpdated(eq(textChannelUID), argThat(arg -> "23.2".equals(arg.toString())));
121         assertThat(channelConfig.getCache().getChannelState().toString(), is("23.2"));
122     }
123 }