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.draytonwiser.internal.discovery;
15 import static org.hamcrest.CoreMatchers.is;
16 import static org.junit.Assert.*;
17 import static org.mockito.ArgumentMatchers.*;
18 import static org.mockito.Mockito.doReturn;
19 import static org.mockito.MockitoAnnotations.initMocks;
21 import java.io.IOException;
22 import java.net.URISyntaxException;
23 import java.nio.file.Files;
24 import java.nio.file.Paths;
25 import java.util.ArrayList;
26 import java.util.Arrays;
27 import java.util.List;
28 import java.util.concurrent.ExecutionException;
29 import java.util.concurrent.TimeoutException;
31 import org.eclipse.jetty.client.HttpClient;
32 import org.eclipse.jetty.client.HttpContentResponse;
33 import org.eclipse.jetty.client.HttpResponse;
34 import org.eclipse.jetty.client.api.Request;
35 import org.eclipse.jetty.server.Response;
36 import org.junit.Before;
37 import org.junit.Test;
38 import org.junit.runner.RunWith;
39 import org.junit.runners.Parameterized;
40 import org.junit.runners.Parameterized.Parameters;
41 import org.mockito.Mock;
42 import org.openhab.binding.draytonwiser.internal.DraytonWiserBindingConstants;
43 import org.openhab.binding.draytonwiser.internal.api.DraytonWiserApi;
44 import org.openhab.binding.draytonwiser.internal.api.DraytonWiserApiException;
45 import org.openhab.binding.draytonwiser.internal.handler.HeatHubConfiguration;
46 import org.openhab.binding.draytonwiser.internal.handler.HeatHubHandler;
47 import org.openhab.binding.draytonwiser.internal.model.DomainDTO;
48 import org.openhab.binding.draytonwiser.internal.model.DraytonWiserDTO;
49 import org.openhab.core.config.discovery.DiscoveryResult;
50 import org.openhab.core.thing.Bridge;
51 import org.openhab.core.thing.ThingUID;
54 * Test class for {@link DraytonWiserDiscoveryService}.
56 * @author Hilbrand Bouwkamp - Initial contribution
58 @RunWith(Parameterized.class)
59 public class DraytonWiserDiscoveryServiceTest {
62 private HeatHubHandler bridgeHandler;
64 private Bridge bridge;
66 private HttpClient httpClient;
68 private Request request;
70 private DraytonWiserApi api;
71 private final String jsonFile;
72 private final int expectedResults;
74 public DraytonWiserDiscoveryServiceTest(final String jsonFile, final int expectedResults) {
75 this.jsonFile = jsonFile;
76 this.expectedResults = expectedResults;
79 @Parameters(name = "{0}")
80 public static List<Object[]> data() {
81 return Arrays.asList(new Object[] { "../test1.json", 11 }, new Object[] { "../test2.json", 22 });
85 public void before() {
87 api = new DraytonWiserApi(httpClient);
88 api.setConfiguration(new HeatHubConfiguration());
90 doReturn(request).when(httpClient).newRequest((String) any());
91 doReturn(request).when(request).method((String) any());
92 doReturn(request).when(request).header((String) any(), any());
93 doReturn(request).when(request).content(any());
94 doReturn(request).when(request).timeout(anyLong(), any());
95 doReturn(bridge).when(bridgeHandler).getThing();
96 doReturn(new ThingUID(DraytonWiserBindingConstants.THING_TYPE_BRIDGE, "1")).when(bridge).getUID();
100 public void testDiscovery() throws IOException, URISyntaxException, InterruptedException, TimeoutException,
101 ExecutionException, DraytonWiserApiException {
102 final byte[] content = Files.readAllBytes(Paths.get(getClass().getResource(jsonFile).toURI()));
103 final HttpResponse response = new HttpResponse(null, null);
104 response.status(Response.SC_OK);
105 doReturn(new HttpContentResponse(response, content, null, null)).when(request).send();
106 final List<DiscoveryResult> discoveryResults = new ArrayList<>();
107 final DraytonWiserDiscoveryService service = new DraytonWiserDiscoveryService() {
109 protected void thingDiscovered(final DiscoveryResult discoveryResult) {
110 discoveryResults.add(discoveryResult);
113 service.setThingHandler(bridgeHandler);
114 final DomainDTO domain = api.getDomain();
116 if (domain == null) {
117 fail("DomainDTO object is null");
119 service.onRefresh(new DraytonWiserDTO(domain));
120 assertThat(discoveryResults.size(), is(expectedResults));