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.sensibo.internal.handler;
15 import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
16 import static com.github.tomakehurst.wiremock.client.WireMock.get;
17 import static com.github.tomakehurst.wiremock.client.WireMock.stubFor;
18 import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo;
19 import static org.junit.Assert.assertEquals;
20 import static org.mockito.ArgumentMatchers.eq;
21 import static org.mockito.Mockito.when;
23 import java.io.IOException;
24 import java.util.List;
26 import org.apache.commons.io.IOUtils;
27 import org.eclipse.jetty.client.HttpClient;
28 import org.junit.After;
29 import org.junit.Before;
30 import org.junit.Rule;
31 import org.junit.Test;
32 import org.mockito.Mock;
33 import org.mockito.MockitoAnnotations;
34 import org.openhab.binding.sensibo.internal.config.SensiboAccountConfiguration;
35 import org.openhab.binding.sensibo.internal.model.SensiboSky;
36 import org.openhab.core.config.core.Configuration;
37 import org.openhab.core.thing.Bridge;
38 import org.openhab.core.thing.ThingUID;
40 import com.github.tomakehurst.wiremock.core.WireMockConfiguration;
41 import com.github.tomakehurst.wiremock.junit.WireMockRule;
44 * @author Arne Seime - Initial contribution
46 public class SensiboAccountHandlerTest {
48 public WireMockRule wireMockRule = new WireMockRule(WireMockConfiguration.options().dynamicPort());
51 private Bridge sensiboAccountMock;
53 private HttpClient httpClient;
55 private Configuration configuration;
58 public void setUp() throws Exception {
59 MockitoAnnotations.initMocks(this);
60 httpClient = new HttpClient();
62 SensiboAccountHandler.API_ENDPOINT = "http://localhost:" + wireMockRule.port() + "/api"; // https://home.sensibo.com/api/v2
66 public void shutdown() throws Exception {
71 public void testInitialize1() throws InterruptedException, IOException {
72 testInitialize("/get_pods_response.json", "/get_pod_details_response.json");
76 public void testInitializeMarco() throws InterruptedException, IOException {
77 testInitialize("/get_pods_response.json", "/get_pod_details_response_marco.json");
80 private void testInitialize(String podsResponse, String podDetailsResponse)
81 throws InterruptedException, IOException {
83 final SensiboAccountConfiguration accountConfig = new SensiboAccountConfiguration();
84 accountConfig.apiKey = "APIKEY";
85 when(configuration.as(eq(SensiboAccountConfiguration.class))).thenReturn(accountConfig);
87 // Setup initial response
88 final String getPodsResponse = IOUtils.toString(getClass().getResourceAsStream(podsResponse));
89 stubFor(get(urlEqualTo("/api/v2/users/me/pods?apiKey=APIKEY"))
90 .willReturn(aResponse().withStatus(200).withBody(getPodsResponse)));
92 // Setup 2nd response with details
93 final String getPodDetailsResponse = IOUtils.toString(getClass().getResourceAsStream(podDetailsResponse));
94 stubFor(get(urlEqualTo("/api/v2/pods/PODID?apiKey=APIKEY&fields=*"))
95 .willReturn(aResponse().withStatus(200).withBody(getPodDetailsResponse)));
97 when(sensiboAccountMock.getConfiguration()).thenReturn(configuration);
98 when(sensiboAccountMock.getUID()).thenReturn(new ThingUID("sensibo:account:thinguid"));
100 final SensiboAccountHandler subject = new SensiboAccountHandler(sensiboAccountMock, httpClient);
101 // Async, poll for status
102 subject.initialize();
104 // Verify num things found == 1
106 for (int i = 0; i < 20; i++) {
107 final List<SensiboSky> things = subject.getModel().getPods();
108 numPods = things.size();
117 assertEquals(1, numPods);