]> git.basschouten.com Git - openhab-addons.git/blob
8d8001ebb23f9ce2c1a3163a8bdda0dd84c9e514
[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.openMocks;
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.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;
43
44 import com.google.gson.Gson;
45 import com.google.gson.GsonBuilder;
46
47 /**
48  * Tests the {@link DiscoverComponents} class.
49  *
50  * @author David Graeff - Initial contribution
51  */
52 public class DiscoverComponentsTest extends JavaOSGiTest {
53
54     private AutoCloseable mocksCloseable;
55
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;
61
62     @BeforeEach
63     public void beforeEach() {
64         mocksCloseable = openMocks(this);
65
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(),
73                 anyBoolean());
74         doReturn(null).when(transformationServiceProvider).getTransformationService(any());
75     }
76
77     @AfterEach
78     public void afterEach() throws Exception {
79         mocksCloseable.close();
80     }
81
82     @Test
83     public void discoveryTimeTest() throws InterruptedException, ExecutionException, TimeoutException {
84         // Create a scheduler
85         ScheduledExecutorService scheduler = new ScheduledThreadPoolExecutor(1);
86
87         Gson gson = new GsonBuilder().registerTypeAdapterFactory(new ChannelConfigurationTypeAdapterFactory()).create();
88
89         DiscoverComponents discover = spy(new DiscoverComponents(ThingChannelConstants.testHomeAssistantThing,
90                 scheduler, channelStateUpdateListener, availabilityTracker, gson, transformationServiceProvider));
91
92         HandlerConfiguration config = new HandlerConfiguration("homeassistant",
93                 Collections.singletonList("switch/object"));
94
95         Set<HaID> discoveryIds = new HashSet<>();
96         discoveryIds.addAll(HaID.fromConfig(config));
97
98         discover.startDiscovery(connection, 50, discoveryIds, discovered).get(100, TimeUnit.MILLISECONDS);
99     }
100 }