]> git.basschouten.com Git - openhab-addons.git/blob
7e0836d34e96c62d7ff8c764bc75ab68f5d9ffca
[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.sensibo.internal.handler;
14
15 import static com.github.tomakehurst.wiremock.client.WireMock.*;
16 import static org.junit.jupiter.api.Assertions.assertEquals;
17 import static org.mockito.ArgumentMatchers.eq;
18 import static org.mockito.Mockito.when;
19
20 import java.io.IOException;
21 import java.util.List;
22
23 import org.apache.commons.io.IOUtils;
24 import org.eclipse.jetty.client.HttpClient;
25 import org.junit.jupiter.api.AfterEach;
26 import org.junit.jupiter.api.BeforeEach;
27 import org.junit.jupiter.api.Test;
28 import org.junit.jupiter.api.extension.ExtendWith;
29 import org.mockito.Mock;
30 import org.mockito.junit.jupiter.MockitoExtension;
31 import org.openhab.binding.sensibo.internal.config.SensiboAccountConfiguration;
32 import org.openhab.binding.sensibo.internal.model.SensiboSky;
33 import org.openhab.core.config.core.Configuration;
34 import org.openhab.core.thing.Bridge;
35 import org.openhab.core.thing.ThingUID;
36
37 import com.github.tomakehurst.wiremock.WireMockServer;
38 import com.github.tomakehurst.wiremock.client.WireMock;
39 import com.github.tomakehurst.wiremock.core.WireMockConfiguration;
40
41 /**
42  * @author Arne Seime - Initial contribution
43  */
44 @ExtendWith(MockitoExtension.class)
45 public class SensiboAccountHandlerTest {
46     private WireMockServer wireMockServer;
47
48     private HttpClient httpClient;
49
50     private @Mock Configuration configuration;
51     private @Mock Bridge sensiboAccountMock;
52
53     @BeforeEach
54     public void setUp() throws Exception {
55         wireMockServer = new WireMockServer(WireMockConfiguration.options().dynamicPort());
56         wireMockServer.start();
57
58         int port = wireMockServer.port();
59         WireMock.configureFor("localhost", port);
60
61         httpClient = new HttpClient();
62         httpClient.start();
63
64         SensiboAccountHandler.API_ENDPOINT = "http://localhost:" + port + "/api"; // https://home.sensibo.com/api/v2
65     }
66
67     @AfterEach
68     public void shutdown() throws Exception {
69         httpClient.stop();
70     }
71
72     @Test
73     public void testInitialize1() throws InterruptedException, IOException {
74         testInitialize("/get_pods_response.json", "/get_pod_details_response.json");
75     }
76
77     @Test
78     public void testInitializeMarco() throws InterruptedException, IOException {
79         testInitialize("/get_pods_response.json", "/get_pod_details_response_marco.json");
80     }
81
82     private void testInitialize(String podsResponse, String podDetailsResponse)
83             throws InterruptedException, IOException {
84         // Setup account
85         final SensiboAccountConfiguration accountConfig = new SensiboAccountConfiguration();
86         accountConfig.apiKey = "APIKEY";
87         when(configuration.as(eq(SensiboAccountConfiguration.class))).thenReturn(accountConfig);
88
89         // Setup initial response
90         final String getPodsResponse = IOUtils.toString(getClass().getResourceAsStream(podsResponse));
91         stubFor(get(urlEqualTo("/api/v2/users/me/pods?apiKey=APIKEY"))
92                 .willReturn(aResponse().withStatus(200).withBody(getPodsResponse)));
93
94         // Setup 2nd response with details
95         final String getPodDetailsResponse = IOUtils.toString(getClass().getResourceAsStream(podDetailsResponse));
96         stubFor(get(urlEqualTo("/api/v2/pods/PODID?apiKey=APIKEY&fields=*"))
97                 .willReturn(aResponse().withStatus(200).withBody(getPodDetailsResponse)));
98
99         when(sensiboAccountMock.getConfiguration()).thenReturn(configuration);
100         when(sensiboAccountMock.getUID()).thenReturn(new ThingUID("sensibo:account:thinguid"));
101
102         final SensiboAccountHandler subject = new SensiboAccountHandler(sensiboAccountMock, httpClient);
103         // Async, poll for status
104         subject.initialize();
105
106         // Verify num things found == 1
107         int numPods = 0;
108         for (int i = 0; i < 20; i++) {
109             final List<SensiboSky> things = subject.getModel().getPods();
110             numPods = things.size();
111             if (numPods == 1) {
112                 break;
113             } else {
114                 // Wait some more
115                 Thread.sleep(200);
116             }
117         }
118
119         assertEquals(1, numPods);
120     }
121 }