]> git.basschouten.com Git - openhab-addons.git/blob
7c2d33c7b67a62d2b413f48ea1b85724eafae1ff
[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.homeassistant.internal;
14
15 import static org.mockito.ArgumentMatchers.*;
16 import static org.mockito.Mockito.*;
17
18 import java.io.IOException;
19 import java.net.URISyntaxException;
20 import java.nio.file.Files;
21 import java.nio.file.Path;
22 import java.nio.file.Paths;
23 import java.util.Objects;
24 import java.util.Set;
25 import java.util.UUID;
26 import java.util.concurrent.CompletableFuture;
27 import java.util.concurrent.ConcurrentHashMap;
28 import java.util.concurrent.ConcurrentMap;
29
30 import org.eclipse.jdt.annotation.NonNullByDefault;
31 import org.junit.jupiter.api.Assertions;
32 import org.junit.jupiter.api.BeforeEach;
33 import org.junit.jupiter.api.extension.ExtendWith;
34 import org.mockito.Mock;
35 import org.mockito.junit.jupiter.MockitoExtension;
36 import org.mockito.junit.jupiter.MockitoSettings;
37 import org.mockito.quality.Strictness;
38 import org.openhab.binding.mqtt.generic.MqttChannelStateDescriptionProvider;
39 import org.openhab.binding.mqtt.generic.MqttChannelTypeProvider;
40 import org.openhab.binding.mqtt.generic.TransformationServiceProvider;
41 import org.openhab.binding.mqtt.handler.BrokerHandler;
42 import org.openhab.binding.mqtt.homeassistant.generic.internal.MqttBindingConstants;
43 import org.openhab.core.io.transport.mqtt.MqttBrokerConnection;
44 import org.openhab.core.io.transport.mqtt.MqttMessageSubscriber;
45 import org.openhab.core.test.java.JavaTest;
46 import org.openhab.core.test.storage.VolatileStorageService;
47 import org.openhab.core.thing.Bridge;
48 import org.openhab.core.thing.Thing;
49 import org.openhab.core.thing.ThingStatus;
50 import org.openhab.core.thing.ThingStatusDetail;
51 import org.openhab.core.thing.ThingStatusInfo;
52 import org.openhab.core.thing.ThingTypeUID;
53 import org.openhab.core.thing.ThingUID;
54 import org.openhab.core.thing.binding.builder.BridgeBuilder;
55 import org.openhab.core.thing.binding.builder.ThingBuilder;
56 import org.openhab.core.thing.type.ChannelTypeRegistry;
57 import org.openhab.core.thing.type.ThingType;
58 import org.openhab.core.thing.type.ThingTypeBuilder;
59 import org.openhab.core.thing.type.ThingTypeRegistry;
60 import org.openhab.transform.jinja.internal.JinjaTransformationService;
61 import org.openhab.transform.jinja.internal.profiles.JinjaTransformationProfile;
62
63 /**
64  * Abstract class for HomeAssistant unit tests.
65  *
66  * @author Anton Kharuzhy - Initial contribution
67  */
68 @ExtendWith(MockitoExtension.class)
69 @MockitoSettings(strictness = Strictness.LENIENT)
70 @NonNullByDefault
71 public abstract class AbstractHomeAssistantTests extends JavaTest {
72     public static final String BINDING_ID = "mqtt";
73
74     public static final String BRIDGE_TYPE_ID = "broker";
75     public static final String BRIDGE_TYPE_LABEL = "MQTT Broker";
76     public static final ThingTypeUID BRIDGE_TYPE_UID = new ThingTypeUID(BINDING_ID, BRIDGE_TYPE_ID);
77     public static final String BRIDGE_ID = UUID.randomUUID().toString();
78     public static final ThingUID BRIDGE_UID = new ThingUID(BRIDGE_TYPE_UID, BRIDGE_ID);
79
80     public static final String HA_TYPE_LABEL = "Home Assistant Thing";
81     public static final ThingTypeUID HA_TYPE_UID = new ThingTypeUID(BINDING_ID, "homeassistant_dynamic_type");
82     public static final String HA_ID = UUID.randomUUID().toString();
83     public static final ThingUID HA_UID = new ThingUID(MqttBindingConstants.HOMEASSISTANT_MQTT_THING, HA_ID);
84     public static final ThingType HA_THING_TYPE = ThingTypeBuilder
85             .instance(MqttBindingConstants.HOMEASSISTANT_MQTT_THING, HA_TYPE_LABEL).build();
86
87     protected @Mock @NonNullByDefault({}) MqttBrokerConnection bridgeConnection;
88     protected @Mock @NonNullByDefault({}) ThingTypeRegistry thingTypeRegistry;
89     protected @Mock @NonNullByDefault({}) TransformationServiceProvider transformationServiceProvider;
90
91     protected @NonNullByDefault({}) MqttChannelTypeProvider channelTypeProvider;
92     protected @NonNullByDefault({}) MqttChannelStateDescriptionProvider stateDescriptionProvider;
93     protected @NonNullByDefault({}) ChannelTypeRegistry channelTypeRegistry;
94
95     protected final Bridge bridgeThing = BridgeBuilder.create(BRIDGE_TYPE_UID, BRIDGE_UID).build();
96     protected final BrokerHandler bridgeHandler = spy(new BrokerHandler(bridgeThing));
97     protected final Thing haThing = ThingBuilder.create(HA_TYPE_UID, HA_UID).withBridge(BRIDGE_UID).build();
98     protected final ConcurrentMap<String, Set<MqttMessageSubscriber>> subscriptions = new ConcurrentHashMap<>();
99
100     private final JinjaTransformationService jinjaTransformationService = new JinjaTransformationService();
101
102     @BeforeEach
103     public void beforeEachAbstractHomeAssistantTests() {
104         when(thingTypeRegistry.getThingType(BRIDGE_TYPE_UID))
105                 .thenReturn(ThingTypeBuilder.instance(BRIDGE_TYPE_UID, BRIDGE_TYPE_LABEL).build());
106         when(thingTypeRegistry.getThingType(MqttBindingConstants.HOMEASSISTANT_MQTT_THING)).thenReturn(HA_THING_TYPE);
107         when(transformationServiceProvider
108                 .getTransformationService(JinjaTransformationProfile.PROFILE_TYPE_UID.getId()))
109                 .thenReturn(jinjaTransformationService);
110
111         channelTypeProvider = spy(new MqttChannelTypeProvider(thingTypeRegistry, new VolatileStorageService()));
112         stateDescriptionProvider = spy(new MqttChannelStateDescriptionProvider());
113         channelTypeRegistry = spy(new ChannelTypeRegistry());
114
115         setupConnection();
116
117         // Return the mocked connection object if the bridge handler is asked for it
118         when(bridgeHandler.getConnectionAsync()).thenReturn(CompletableFuture.completedFuture(bridgeConnection));
119
120         bridgeThing.setStatusInfo(new ThingStatusInfo(ThingStatus.ONLINE, ThingStatusDetail.ONLINE.NONE, ""));
121         bridgeThing.setHandler(bridgeHandler);
122
123         haThing.setStatusInfo(new ThingStatusInfo(ThingStatus.ONLINE, ThingStatusDetail.ONLINE.NONE, ""));
124     }
125
126     protected void setupConnection() {
127         doAnswer(invocation -> {
128             final var topic = (String) invocation.getArgument(0);
129             final var subscriber = (MqttMessageSubscriber) invocation.getArgument(1);
130
131             subscriptions.putIfAbsent(topic, ConcurrentHashMap.newKeySet());
132             Set<MqttMessageSubscriber> subscribers = subscriptions.get(topic);
133             Objects.requireNonNull(subscribers); // Invariant, thanks to putIfAbsent above. To make compiler happy
134             subscribers.add(subscriber);
135             return CompletableFuture.completedFuture(true);
136         }).when(bridgeConnection).subscribe(any(), any());
137
138         doAnswer(invocation -> {
139             final var topic = (String) invocation.getArgument(0);
140             final var subscriber = (MqttMessageSubscriber) invocation.getArgument(1);
141             final var topicSubscriptions = subscriptions.get(topic);
142
143             if (topicSubscriptions != null) {
144                 topicSubscriptions.remove(subscriber);
145             }
146             return CompletableFuture.completedFuture(true);
147         }).when(bridgeConnection).unsubscribe(any(), any());
148
149         doAnswer(invocation -> {
150             subscriptions.clear();
151             return CompletableFuture.completedFuture(true);
152         }).when(bridgeConnection).unsubscribeAll();
153
154         doReturn(CompletableFuture.completedFuture(true)).when(bridgeConnection).publish(any(), any(), anyInt(),
155                 anyBoolean());
156     }
157
158     /**
159      * @param relativePath path from src/test/java/org/openhab/binding/mqtt/homeassistant/internal
160      * @return path
161      */
162     @SuppressWarnings("null")
163     protected Path getResourcePath(String relativePath) {
164         try {
165             return Paths.get(AbstractHomeAssistantTests.class.getResource(relativePath).toURI());
166         } catch (URISyntaxException e) {
167             Assertions.fail(e);
168         }
169         throw new IllegalArgumentException();
170     }
171
172     protected String getResourceAsString(String relativePath) {
173         try {
174             return Files.readString(getResourcePath(relativePath));
175         } catch (IOException e) {
176             Assertions.fail(e);
177         }
178         throw new IllegalArgumentException();
179     }
180
181     protected byte[] getResourceAsByteArray(String relativePath) {
182         try {
183             return Files.readAllBytes(getResourcePath(relativePath));
184         } catch (IOException e) {
185             Assertions.fail(e);
186         }
187         throw new IllegalArgumentException();
188     }
189
190     protected static String configTopicToMqtt(String configTopic) {
191         return HandlerConfiguration.DEFAULT_BASETOPIC + "/" + configTopic + "/config";
192     }
193 }