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.initMocks;
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.openhab.core.io.transport.mqtt.MqttBrokerConnection;
30 import org.openhab.core.test.java.JavaOSGiTest;
31 import org.junit.Before;
32 import org.junit.Test;
33 import org.mockito.Mock;
34 import org.openhab.binding.mqtt.generic.AvailabilityTracker;
35 import org.openhab.binding.mqtt.generic.ChannelStateUpdateListener;
36 import org.openhab.binding.mqtt.generic.TransformationServiceProvider;
37 import org.openhab.binding.mqtt.homeassistant.internal.ChannelConfigurationTypeAdapterFactory;
38 import org.openhab.binding.mqtt.homeassistant.internal.DiscoverComponents;
39 import org.openhab.binding.mqtt.homeassistant.internal.DiscoverComponents.ComponentDiscovered;
40 import org.openhab.binding.mqtt.homeassistant.internal.HaID;
41 import org.openhab.binding.mqtt.homeassistant.internal.HandlerConfiguration;
43 import com.google.gson.Gson;
44 import com.google.gson.GsonBuilder;
47 * Tests the {@link DiscoverComponents} class.
49 * @author David Graeff - Initial contribution
51 public class DiscoverComponentsTest extends JavaOSGiTest {
53 MqttBrokerConnection connection;
56 ComponentDiscovered discovered;
59 TransformationServiceProvider transformationServiceProvider;
62 ChannelStateUpdateListener channelStateUpdateListener;
65 AvailabilityTracker availabilityTracker;
70 CompletableFuture<Void> voidFutureComplete = new CompletableFuture<>();
71 voidFutureComplete.complete(null);
72 doReturn(voidFutureComplete).when(connection).unsubscribeAll();
73 doReturn(CompletableFuture.completedFuture(true)).when(connection).subscribe(any(), any());
74 doReturn(CompletableFuture.completedFuture(true)).when(connection).unsubscribe(any(), any());
75 doReturn(CompletableFuture.completedFuture(true)).when(connection).publish(any(), any());
76 doReturn(CompletableFuture.completedFuture(true)).when(connection).publish(any(), any(), anyInt(),
78 doReturn(null).when(transformationServiceProvider).getTransformationService(any());
82 public void discoveryTimeTest() throws InterruptedException, ExecutionException, TimeoutException {
84 ScheduledExecutorService scheduler = new ScheduledThreadPoolExecutor(1);
86 Gson gson = new GsonBuilder().registerTypeAdapterFactory(new ChannelConfigurationTypeAdapterFactory()).create();
88 DiscoverComponents discover = spy(new DiscoverComponents(ThingChannelConstants.testHomeAssistantThing,
89 scheduler, channelStateUpdateListener, availabilityTracker, gson, transformationServiceProvider));
91 HandlerConfiguration config = new HandlerConfiguration("homeassistant",
92 Collections.singletonList("switch/object"));
94 Set<HaID> discoveryIds = new HashSet<>();
95 discoveryIds.addAll(HaID.fromConfig(config));
97 discover.startDiscovery(connection, 50, discoveryIds, discovered).get(100, TimeUnit.MILLISECONDS);