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;
15 import static org.mockito.ArgumentMatchers.*;
16 import static org.mockito.Mockito.*;
17 import static org.mockito.MockitoAnnotations.openMocks;
19 import java.util.Collections;
20 import java.util.HashSet;
22 import java.util.concurrent.CompletableFuture;
23 import java.util.concurrent.ExecutionException;
24 import java.util.concurrent.ScheduledExecutorService;
25 import java.util.concurrent.ScheduledThreadPoolExecutor;
26 import java.util.concurrent.TimeUnit;
27 import java.util.concurrent.TimeoutException;
29 import org.junit.jupiter.api.AfterEach;
30 import org.junit.jupiter.api.BeforeEach;
31 import org.junit.jupiter.api.Test;
32 import org.mockito.Mock;
33 import org.openhab.binding.mqtt.generic.AvailabilityTracker;
34 import org.openhab.binding.mqtt.generic.ChannelStateUpdateListener;
35 import org.openhab.binding.mqtt.generic.TransformationServiceProvider;
36 import org.openhab.binding.mqtt.homeassistant.internal.ChannelConfigurationTypeAdapterFactory;
37 import org.openhab.binding.mqtt.homeassistant.internal.DiscoverComponents;
38 import org.openhab.binding.mqtt.homeassistant.internal.DiscoverComponents.ComponentDiscovered;
39 import org.openhab.binding.mqtt.homeassistant.internal.HaID;
40 import org.openhab.binding.mqtt.homeassistant.internal.HandlerConfiguration;
41 import org.openhab.core.io.transport.mqtt.MqttBrokerConnection;
42 import org.openhab.core.test.java.JavaOSGiTest;
44 import com.google.gson.Gson;
45 import com.google.gson.GsonBuilder;
48 * Tests the {@link DiscoverComponents} class.
50 * @author David Graeff - Initial contribution
52 public class DiscoverComponentsTest extends JavaOSGiTest {
54 private AutoCloseable mocksCloseable;
56 private @Mock MqttBrokerConnection connection;
57 private @Mock ComponentDiscovered discovered;
58 private @Mock TransformationServiceProvider transformationServiceProvider;
59 private @Mock ChannelStateUpdateListener channelStateUpdateListener;
60 private @Mock AvailabilityTracker availabilityTracker;
63 public void beforeEach() {
64 mocksCloseable = openMocks(this);
66 CompletableFuture<Void> voidFutureComplete = new CompletableFuture<>();
67 voidFutureComplete.complete(null);
68 doReturn(voidFutureComplete).when(connection).unsubscribeAll();
69 doReturn(CompletableFuture.completedFuture(true)).when(connection).subscribe(any(), any());
70 doReturn(CompletableFuture.completedFuture(true)).when(connection).unsubscribe(any(), any());
71 doReturn(CompletableFuture.completedFuture(true)).when(connection).publish(any(), any());
72 doReturn(CompletableFuture.completedFuture(true)).when(connection).publish(any(), any(), anyInt(),
74 doReturn(null).when(transformationServiceProvider).getTransformationService(any());
78 public void afterEach() throws Exception {
79 mocksCloseable.close();
83 public void discoveryTimeTest() throws InterruptedException, ExecutionException, TimeoutException {
85 ScheduledExecutorService scheduler = new ScheduledThreadPoolExecutor(1);
87 Gson gson = new GsonBuilder().registerTypeAdapterFactory(new ChannelConfigurationTypeAdapterFactory()).create();
89 DiscoverComponents discover = spy(new DiscoverComponents(ThingChannelConstants.testHomeAssistantThing,
90 scheduler, channelStateUpdateListener, availabilityTracker, gson, transformationServiceProvider));
92 HandlerConfiguration config = new HandlerConfiguration("homeassistant",
93 Collections.singletonList("switch/object"));
95 Set<HaID> discoveryIds = new HashSet<>();
96 discoveryIds.addAll(HaID.fromConfig(config));
98 discover.startDiscovery(connection, 50, discoveryIds, discovered).get(100, TimeUnit.MILLISECONDS);