]> git.basschouten.com Git - openhab-addons.git/blob
c7e3bc1dd4a3773d27cd688832f923392e5e335f
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 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.nio.charset.StandardCharsets;
22 import java.util.List;
23
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 = new String(getClass().getResourceAsStream(podsResponse).readAllBytes(),
91                 StandardCharsets.UTF_8);
92         stubFor(get(urlEqualTo("/api/v2/users/me/pods?apiKey=APIKEY"))
93                 .willReturn(aResponse().withStatus(200).withBody(getPodsResponse)));
94
95         // Setup 2nd response with details
96         final String getPodDetailsResponse = new String(
97                 getClass().getResourceAsStream(podDetailsResponse).readAllBytes(), StandardCharsets.UTF_8);
98         stubFor(get(urlEqualTo("/api/v2/pods/PODID?apiKey=APIKEY&fields=*"))
99                 .willReturn(aResponse().withStatus(200).withBody(getPodDetailsResponse)));
100
101         when(sensiboAccountMock.getConfiguration()).thenReturn(configuration);
102         when(sensiboAccountMock.getUID()).thenReturn(new ThingUID("sensibo:account:thinguid"));
103
104         final SensiboAccountHandler subject = new SensiboAccountHandler(sensiboAccountMock, httpClient);
105         // Async, poll for status
106         subject.initialize();
107
108         // Verify num things found == 1
109         int numPods = 0;
110         for (int i = 0; i < 20; i++) {
111             final List<SensiboSky> things = subject.getModel().getPods();
112             numPods = things.size();
113             if (numPods == 1) {
114                 break;
115             } else {
116                 // Wait some more
117                 Thread.sleep(200);
118             }
119         }
120
121         assertEquals(1, numPods);
122     }
123 }