]> git.basschouten.com Git - openhab-addons.git/blob
95b25921a96661b9f57f3c49572e0c3b6ddfdcd8
[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.draytonwiser.internal.discovery;
14
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;
20
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;
30
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;
52
53 /**
54  * Test class for {@link DraytonWiserDiscoveryService}.
55  *
56  * @author Hilbrand Bouwkamp - Initial contribution
57  */
58 @RunWith(Parameterized.class)
59 public class DraytonWiserDiscoveryServiceTest {
60
61     @Mock
62     private HeatHubHandler bridgeHandler;
63     @Mock
64     private Bridge bridge;
65     @Mock
66     private HttpClient httpClient;
67     @Mock
68     private Request request;
69
70     private DraytonWiserApi api;
71     private final String jsonFile;
72     private final int expectedResults;
73
74     public DraytonWiserDiscoveryServiceTest(final String jsonFile, final int expectedResults) {
75         this.jsonFile = jsonFile;
76         this.expectedResults = expectedResults;
77     }
78
79     @Parameters(name = "{0}")
80     public static List<Object[]> data() {
81         return Arrays.asList(new Object[] { "../test1.json", 11 }, new Object[] { "../test2.json", 22 });
82     }
83
84     @Before
85     public void before() {
86         initMocks(this);
87         api = new DraytonWiserApi(httpClient);
88         api.setConfiguration(new HeatHubConfiguration());
89
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();
97     }
98
99     @Test
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() {
108             @Override
109             protected void thingDiscovered(final DiscoveryResult discoveryResult) {
110                 discoveryResults.add(discoveryResult);
111             }
112         };
113         service.setThingHandler(bridgeHandler);
114         final DomainDTO domain = api.getDomain();
115
116         if (domain == null) {
117             fail("DomainDTO object is null");
118         } else {
119             service.onRefresh(new DraytonWiserDTO(domain));
120             assertThat(discoveryResults.size(), is(expectedResults));
121         }
122     }
123 }