]> git.basschouten.com Git - openhab-addons.git/blob
e59f9fbc575903ccafebdfcff094a7a1f0211918
[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;
14
15 import static org.mockito.ArgumentMatchers.*;
16 import static org.mockito.Mockito.*;
17 import static org.mockito.MockitoAnnotations.initMocks;
18
19 import java.util.Collections;
20 import java.util.HashSet;
21 import java.util.Set;
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;
28
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;
42
43 import com.google.gson.Gson;
44 import com.google.gson.GsonBuilder;
45
46 /**
47  * Tests the {@link DiscoverComponents} class.
48  *
49  * @author David Graeff - Initial contribution
50  */
51 public class DiscoverComponentsTest extends JavaOSGiTest {
52     @Mock
53     MqttBrokerConnection connection;
54
55     @Mock
56     ComponentDiscovered discovered;
57
58     @Mock
59     TransformationServiceProvider transformationServiceProvider;
60
61     @Mock
62     ChannelStateUpdateListener channelStateUpdateListener;
63
64     @Mock
65     AvailabilityTracker availabilityTracker;
66
67     @Before
68     public void setUp() {
69         initMocks(this);
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(),
77                 anyBoolean());
78         doReturn(null).when(transformationServiceProvider).getTransformationService(any());
79     }
80
81     @Test
82     public void discoveryTimeTest() throws InterruptedException, ExecutionException, TimeoutException {
83         // Create a scheduler
84         ScheduledExecutorService scheduler = new ScheduledThreadPoolExecutor(1);
85
86         Gson gson = new GsonBuilder().registerTypeAdapterFactory(new ChannelConfigurationTypeAdapterFactory()).create();
87
88         DiscoverComponents discover = spy(new DiscoverComponents(ThingChannelConstants.testHomeAssistantThing,
89                 scheduler, channelStateUpdateListener, availabilityTracker, gson, transformationServiceProvider));
90
91         HandlerConfiguration config = new HandlerConfiguration("homeassistant",
92                 Collections.singletonList("switch/object"));
93
94         Set<HaID> discoveryIds = new HashSet<>();
95         discoveryIds.addAll(HaID.fromConfig(config));
96
97         discover.startDiscovery(connection, 50, discoveryIds, discovered).get(100, TimeUnit.MILLISECONDS);
98
99     }
100 }