2 * Copyright (c) 2010-2024 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.sensibo.internal.handler;
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;
20 import java.io.IOException;
21 import java.nio.charset.StandardCharsets;
22 import java.util.List;
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;
37 import com.github.tomakehurst.wiremock.WireMockServer;
38 import com.github.tomakehurst.wiremock.client.WireMock;
39 import com.github.tomakehurst.wiremock.core.WireMockConfiguration;
42 * @author Arne Seime - Initial contribution
44 @ExtendWith(MockitoExtension.class)
45 public class SensiboAccountHandlerTest {
46 private WireMockServer wireMockServer;
48 private HttpClient httpClient;
50 private @Mock Configuration configuration;
51 private @Mock Bridge sensiboAccountMock;
54 public void setUp() throws Exception {
55 wireMockServer = new WireMockServer(WireMockConfiguration.options().dynamicPort());
56 wireMockServer.start();
58 int port = wireMockServer.port();
59 WireMock.configureFor("localhost", port);
61 httpClient = new HttpClient();
64 SensiboAccountHandler.API_ENDPOINT = "http://localhost:" + port + "/api"; // https://home.sensibo.com/api/v2
68 public void shutdown() throws Exception {
73 public void testInitialize1() throws InterruptedException, IOException {
74 testInitialize("/get_pods_response.json", "/get_pod_details_response.json");
78 public void testInitializeMarco() throws InterruptedException, IOException {
79 testInitialize("/get_pods_response.json", "/get_pod_details_response_marco.json");
82 private void testInitialize(String podsResponse, String podDetailsResponse)
83 throws InterruptedException, IOException {
85 final SensiboAccountConfiguration accountConfig = new SensiboAccountConfiguration();
86 accountConfig.apiKey = "APIKEY";
87 when(configuration.as(eq(SensiboAccountConfiguration.class))).thenReturn(accountConfig);
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)));
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)));
101 when(sensiboAccountMock.getConfiguration()).thenReturn(configuration);
102 when(sensiboAccountMock.getUID()).thenReturn(new ThingUID("sensibo:account:thinguid"));
104 final SensiboAccountHandler subject = new SensiboAccountHandler(sensiboAccountMock, httpClient);
105 // Async, poll for status
106 subject.initialize();
108 // Verify num things found == 1
110 for (int i = 0; i < 20; i++) {
111 final List<SensiboSky> things = subject.getModel().getPods();
112 numPods = things.size();
121 assertEquals(1, numPods);