2 * Copyright (c) 2010-2022 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.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.*;
21 import java.util.concurrent.CompletableFuture;
23 import javax.naming.ConfigurationException;
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;
46 * Tests cases for {@link ThingHandler} to test the json transformation.
48 * @author David Graeff - Initial contribution
50 @ExtendWith(MockitoExtension.class)
51 @MockitoSettings(strictness = Strictness.WARN)
52 public class ChannelStateTransformationTests {
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;
61 private GenericMQTTThingHandler thingHandler;
64 public void setUp() throws ConfigurationException, MqttException {
65 ThingStatusInfo thingStatus = new ThingStatusInfo(ThingStatus.ONLINE, ThingStatusDetail.NONE, null);
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());
73 // Return the mocked connection object if the bridge handler is asked for it
74 when(bridgeHandler.getConnectionAsync()).thenReturn(CompletableFuture.completedFuture(connection));
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());
82 thingHandler = spy(new GenericMQTTThingHandler(thing, mock(MqttChannelStateDescriptionProvider.class),
83 transformationServiceProvider, 1500));
84 when(transformationServiceProvider.getTransformationService(anyString())).thenReturn(jsonPathService);
86 thingHandler.setCallback(callback);
87 // Return the bridge handler if the thing handler asks for it
88 doReturn(bridgeHandler).when(thingHandler).getBridgeHandler();
90 // We are by default online
91 doReturn(thingStatus).when(thingHandler).getBridgeStatus();
94 @SuppressWarnings("null")
96 public void initialize() throws MqttException {
97 when(thing.getChannels()).thenReturn(thingChannelListWithJson);
99 thingHandler.initialize();
100 ChannelState channelConfig = thingHandler.getChannelState(textChannelUID);
101 assertThat(channelConfig.transformationsIn.get(0).pattern, is(jsonPathPattern));
104 @SuppressWarnings("null")
106 public void processMessageWithJSONPath() throws Exception {
107 when(jsonPathService.transform(jsonPathPattern, jsonPathJSON)).thenReturn("23.2");
109 thingHandler.initialize();
110 ChannelState channelConfig = thingHandler.getChannelState(textChannelUID);
111 channelConfig.setChannelStateUpdateListener(thingHandler);
113 ChannelStateTransformation transformation = channelConfig.transformationsIn.get(0);
115 byte payload[] = jsonPathJSON.getBytes();
116 assertThat(transformation.pattern, is(jsonPathPattern));
117 // Test process message
118 channelConfig.processMessage(channelConfig.getStateTopic(), payload);
120 verify(callback).stateUpdated(eq(textChannelUID), argThat(arg -> "23.2".equals(arg.toString())));
121 assertThat(channelConfig.getCache().getChannelState().toString(), is("23.2"));