2 * Copyright (c) 2010-2020 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.mqtt.generic;
15 import static org.hamcrest.CoreMatchers.is;
16 import static org.junit.Assert.assertThat;
17 import static org.mockito.ArgumentMatchers.*;
18 import static org.mockito.Mockito.*;
19 import static org.mockito.MockitoAnnotations.initMocks;
20 import static org.openhab.binding.mqtt.generic.internal.handler.ThingChannelConstants.*;
22 import java.util.concurrent.CompletableFuture;
24 import javax.naming.ConfigurationException;
26 import org.junit.Before;
27 import org.junit.Test;
28 import org.mockito.Mock;
29 import org.openhab.binding.mqtt.generic.internal.handler.GenericMQTTThingHandler;
30 import org.openhab.binding.mqtt.handler.AbstractBrokerHandler;
31 import org.openhab.core.config.core.Configuration;
32 import org.openhab.core.io.transport.mqtt.MqttBrokerConnection;
33 import org.openhab.core.io.transport.mqtt.MqttException;
34 import org.openhab.core.thing.Thing;
35 import org.openhab.core.thing.ThingStatus;
36 import org.openhab.core.thing.ThingStatusDetail;
37 import org.openhab.core.thing.ThingStatusInfo;
38 import org.openhab.core.thing.binding.ThingHandler;
39 import org.openhab.core.thing.binding.ThingHandlerCallback;
40 import org.openhab.core.transform.TransformationService;
43 * Tests cases for {@link ThingHandler} to test the json transformation.
45 * @author David Graeff - Initial contribution
47 public class ChannelStateTransformationTests {
50 private TransformationService jsonPathService;
53 private TransformationServiceProvider transformationServiceProvider;
56 private ThingHandlerCallback callback;
62 private AbstractBrokerHandler bridgeHandler;
65 private MqttBrokerConnection connection;
67 private GenericMQTTThingHandler thingHandler;
70 public void setUp() throws ConfigurationException, MqttException {
73 ThingStatusInfo thingStatus = new ThingStatusInfo(ThingStatus.ONLINE, ThingStatusDetail.NONE, null);
75 // Mock the thing: We need the thingUID and the bridgeUID
76 when(thing.getUID()).thenReturn(testGenericThing);
77 when(thing.getChannels()).thenReturn(thingChannelListWithJson);
78 when(thing.getStatusInfo()).thenReturn(thingStatus);
79 when(thing.getConfiguration()).thenReturn(new Configuration());
81 // Return the mocked connection object if the bridge handler is asked for it
82 when(bridgeHandler.getConnectionAsync()).thenReturn(CompletableFuture.completedFuture(connection));
84 CompletableFuture<Void> voidFutureComplete = new CompletableFuture<>();
85 voidFutureComplete.complete(null);
86 doReturn(voidFutureComplete).when(connection).unsubscribeAll();
87 doReturn(CompletableFuture.completedFuture(true)).when(connection).subscribe(any(), any());
88 doReturn(CompletableFuture.completedFuture(true)).when(connection).unsubscribe(any(), any());
90 thingHandler = spy(new GenericMQTTThingHandler(thing, mock(MqttChannelStateDescriptionProvider.class),
91 transformationServiceProvider, 1500));
92 when(transformationServiceProvider.getTransformationService(anyString())).thenReturn(jsonPathService);
94 thingHandler.setCallback(callback);
95 // Return the bridge handler if the thing handler asks for it
96 doReturn(bridgeHandler).when(thingHandler).getBridgeHandler();
98 // We are by default online
99 doReturn(thingStatus).when(thingHandler).getBridgeStatus();
102 @SuppressWarnings("null")
104 public void initialize() throws MqttException {
105 when(thing.getChannels()).thenReturn(thingChannelListWithJson);
107 thingHandler.initialize();
108 ChannelState channelConfig = thingHandler.getChannelState(textChannelUID);
109 assertThat(channelConfig.transformationsIn.get(0).pattern, is(jsonPathPattern));
112 @SuppressWarnings("null")
114 public void processMessageWithJSONPath() throws Exception {
115 when(jsonPathService.transform(jsonPathPattern, jsonPathJSON)).thenReturn("23.2");
117 thingHandler.initialize();
118 ChannelState channelConfig = thingHandler.getChannelState(textChannelUID);
119 channelConfig.setChannelStateUpdateListener(thingHandler);
121 ChannelStateTransformation transformation = channelConfig.transformationsIn.get(0);
123 byte payload[] = jsonPathJSON.getBytes();
124 assertThat(transformation.pattern, is(jsonPathPattern));
125 // Test process message
126 channelConfig.processMessage(channelConfig.getStateTopic(), payload);
128 verify(callback).stateUpdated(eq(textChannelUID), argThat(arg -> "23.2".equals(arg.toString())));
129 assertThat(channelConfig.getCache().getChannelState().toString(), is("23.2"));