]> git.basschouten.com Git - openhab-addons.git/blob
80f5eafa5a795a9515f59d61a0ed0b4162f45d88
[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.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;
22
23 import java.io.IOException;
24 import java.util.List;
25
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;
39
40 import com.github.tomakehurst.wiremock.core.WireMockConfiguration;
41 import com.github.tomakehurst.wiremock.junit.WireMockRule;
42
43 /**
44  * @author Arne Seime - Initial contribution
45  */
46 public class SensiboAccountHandlerTest {
47     @Rule
48     public WireMockRule wireMockRule = new WireMockRule(WireMockConfiguration.options().dynamicPort());
49
50     @Mock
51     private Bridge sensiboAccountMock;
52
53     private HttpClient httpClient;
54     @Mock
55     private Configuration configuration;
56
57     @Before
58     public void setUp() throws Exception {
59         MockitoAnnotations.initMocks(this);
60         httpClient = new HttpClient();
61         httpClient.start();
62         SensiboAccountHandler.API_ENDPOINT = "http://localhost:" + wireMockRule.port() + "/api"; // https://home.sensibo.com/api/v2
63     }
64
65     @After
66     public void shutdown() throws Exception {
67         httpClient.stop();
68     }
69
70     @Test
71     public void testInitialize1() throws InterruptedException, IOException {
72         testInitialize("/get_pods_response.json", "/get_pod_details_response.json");
73     }
74
75     @Test
76     public void testInitializeMarco() throws InterruptedException, IOException {
77         testInitialize("/get_pods_response.json", "/get_pod_details_response_marco.json");
78     }
79
80     private void testInitialize(String podsResponse, String podDetailsResponse)
81             throws InterruptedException, IOException {
82         // Setup account
83         final SensiboAccountConfiguration accountConfig = new SensiboAccountConfiguration();
84         accountConfig.apiKey = "APIKEY";
85         when(configuration.as(eq(SensiboAccountConfiguration.class))).thenReturn(accountConfig);
86
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)));
91
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)));
96
97         when(sensiboAccountMock.getConfiguration()).thenReturn(configuration);
98         when(sensiboAccountMock.getUID()).thenReturn(new ThingUID("sensibo:account:thinguid"));
99
100         final SensiboAccountHandler subject = new SensiboAccountHandler(sensiboAccountMock, httpClient);
101         // Async, poll for status
102         subject.initialize();
103
104         // Verify num things found == 1
105         int numPods = 0;
106         for (int i = 0; i < 20; i++) {
107             final List<SensiboSky> things = subject.getModel().getPods();
108             numPods = things.size();
109             if (numPods == 1) {
110                 break;
111             } else {
112                 // Wait some more
113                 Thread.sleep(200);
114             }
115         }
116
117         assertEquals(1, numPods);
118     }
119 }