]> git.basschouten.com Git - openhab-addons.git/blob
005a7be28affdbf1feeba746c2b9a33db774f22e
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2020 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.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.*;
21
22 import java.util.concurrent.CompletableFuture;
23
24 import javax.naming.ConfigurationException;
25
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;
41
42 /**
43  * Tests cases for {@link ThingHandler} to test the json transformation.
44  *
45  * @author David Graeff - Initial contribution
46  */
47 public class ChannelStateTransformationTests {
48
49     @Mock
50     private TransformationService jsonPathService;
51
52     @Mock
53     private TransformationServiceProvider transformationServiceProvider;
54
55     @Mock
56     private ThingHandlerCallback callback;
57
58     @Mock
59     private Thing thing;
60
61     @Mock
62     private AbstractBrokerHandler bridgeHandler;
63
64     @Mock
65     private MqttBrokerConnection connection;
66
67     private GenericMQTTThingHandler thingHandler;
68
69     @Before
70     public void setUp() throws ConfigurationException, MqttException {
71         initMocks(this);
72
73         ThingStatusInfo thingStatus = new ThingStatusInfo(ThingStatus.ONLINE, ThingStatusDetail.NONE, null);
74
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());
80
81         // Return the mocked connection object if the bridge handler is asked for it
82         when(bridgeHandler.getConnectionAsync()).thenReturn(CompletableFuture.completedFuture(connection));
83
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());
89
90         thingHandler = spy(new GenericMQTTThingHandler(thing, mock(MqttChannelStateDescriptionProvider.class),
91                 transformationServiceProvider, 1500));
92         when(transformationServiceProvider.getTransformationService(anyString())).thenReturn(jsonPathService);
93
94         thingHandler.setCallback(callback);
95         // Return the bridge handler if the thing handler asks for it
96         doReturn(bridgeHandler).when(thingHandler).getBridgeHandler();
97
98         // We are by default online
99         doReturn(thingStatus).when(thingHandler).getBridgeStatus();
100     }
101
102     @SuppressWarnings("null")
103     @Test
104     public void initialize() throws MqttException {
105         when(thing.getChannels()).thenReturn(thingChannelListWithJson);
106
107         thingHandler.initialize();
108         ChannelState channelConfig = thingHandler.getChannelState(textChannelUID);
109         assertThat(channelConfig.transformationsIn.get(0).pattern, is(jsonPathPattern));
110     }
111
112     @SuppressWarnings("null")
113     @Test
114     public void processMessageWithJSONPath() throws Exception {
115         when(jsonPathService.transform(jsonPathPattern, jsonPathJSON)).thenReturn("23.2");
116
117         thingHandler.initialize();
118         ChannelState channelConfig = thingHandler.getChannelState(textChannelUID);
119         channelConfig.setChannelStateUpdateListener(thingHandler);
120
121         ChannelStateTransformation transformation = channelConfig.transformationsIn.get(0);
122
123         byte payload[] = jsonPathJSON.getBytes();
124         assertThat(transformation.pattern, is(jsonPathPattern));
125         // Test process message
126         channelConfig.processMessage(channelConfig.getStateTopic(), payload);
127
128         verify(callback).stateUpdated(eq(textChannelUID), argThat(arg -> "23.2".equals(arg.toString())));
129         assertThat(channelConfig.getCache().getChannelState().toString(), is("23.2"));
130     }
131 }