]> git.basschouten.com Git - openhab-addons.git/blob
93c9d38bc78f839ffae77cbfb3e0a1d1ad4b0630
[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.millheat.internal;
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
22 import org.apache.commons.io.IOUtils;
23 import org.eclipse.jetty.client.HttpClient;
24 import org.junit.jupiter.api.AfterEach;
25 import org.junit.jupiter.api.BeforeEach;
26 import org.junit.jupiter.api.Test;
27 import org.junit.jupiter.api.extension.ExtendWith;
28 import org.mockito.Mock;
29 import org.mockito.junit.jupiter.MockitoExtension;
30 import org.mockito.junit.jupiter.MockitoSettings;
31 import org.mockito.quality.Strictness;
32 import org.openhab.binding.millheat.internal.config.MillheatAccountConfiguration;
33 import org.openhab.binding.millheat.internal.handler.MillheatAccountHandler;
34 import org.openhab.binding.millheat.internal.model.MillheatModel;
35 import org.openhab.core.config.core.Configuration;
36 import org.openhab.core.thing.Bridge;
37 import org.openhab.core.thing.ThingUID;
38 import org.osgi.framework.BundleContext;
39
40 import com.github.tomakehurst.wiremock.WireMockServer;
41 import com.github.tomakehurst.wiremock.client.WireMock;
42 import com.github.tomakehurst.wiremock.core.WireMockConfiguration;
43
44 /**
45  * @author Arne Seime - Initial contribution
46  */
47 @ExtendWith(MockitoExtension.class)
48 @MockitoSettings(strictness = Strictness.WARN)
49 public class MillHeatAccountHandlerTest {
50
51     private WireMockServer wireMockServer;
52
53     private HttpClient httpClient;
54
55     private @Mock BundleContext bundleContext;
56     private @Mock Configuration configuration;
57     private @Mock Bridge millheatAccountMock;
58
59     @BeforeEach
60     public void setUp() throws Exception {
61         wireMockServer = new WireMockServer(WireMockConfiguration.options().dynamicPort());
62         wireMockServer.start();
63
64         int port = wireMockServer.port();
65         WireMock.configureFor("localhost", port);
66
67         httpClient = new HttpClient();
68         httpClient.start();
69
70         MillheatAccountHandler.authEndpoint = "http://localhost:" + port + "/zc-account/v1/";
71         MillheatAccountHandler.serviceEndpoint = "http://localhost:" + port + "/millService/v1/";
72     }
73
74     @AfterEach
75     public void shutdown() throws Exception {
76         httpClient.stop();
77         wireMockServer.stop();
78         wireMockServer.resetAll();
79     }
80
81     @Test
82     public void testUpdateModel() throws InterruptedException, IOException, MillheatCommunicationException {
83         final String getHomesResponse = IOUtils.toString(getClass().getResourceAsStream("/select_home_list_ok.json"));
84         final String getRoomsByHomeResponse = IOUtils
85                 .toString(getClass().getResourceAsStream("/get_rooms_by_home_ok.json"));
86         final String getDeviceByRoomResponse = IOUtils
87                 .toString(getClass().getResourceAsStream("/get_device_by_room_ok.json"));
88         final String getIndependentDevicesResponse = IOUtils
89                 .toString(getClass().getResourceAsStream("/get_independent_devices_ok.json"));
90
91         stubFor(post(urlEqualTo("/millService/v1/selectHomeList"))
92                 .willReturn(aResponse().withStatus(200).withBody(getHomesResponse)));
93         stubFor(post(urlEqualTo("/millService/v1/selectRoombyHome"))
94                 .willReturn(aResponse().withStatus(200).withBody(getRoomsByHomeResponse)));
95         stubFor(post(urlEqualTo("/millService/v1/selectDevicebyRoom"))
96                 .willReturn(aResponse().withStatus(200).withBody(getDeviceByRoomResponse)));
97         stubFor(post(urlEqualTo("/millService/v1/getIndependentDevices"))
98                 .willReturn(aResponse().withStatus(200).withBody(getIndependentDevicesResponse)));
99
100         when(millheatAccountMock.getConfiguration()).thenReturn(configuration);
101         when(millheatAccountMock.getUID()).thenReturn(new ThingUID("millheat:account:thinguid"));
102
103         final MillheatAccountConfiguration accountConfig = new MillheatAccountConfiguration();
104         accountConfig.username = "username";
105         accountConfig.password = "password";
106         when(configuration.as(eq(MillheatAccountConfiguration.class))).thenReturn(accountConfig);
107
108         final MillheatAccountHandler subject = new MillheatAccountHandler(millheatAccountMock, httpClient,
109                 bundleContext);
110         MillheatModel model = subject.refreshModel();
111         assertEquals(1, model.getHomes().size());
112
113         verify(postRequestedFor(urlMatching("/millService/v1/selectHomeList")));
114         verify(postRequestedFor(urlMatching("/millService/v1/selectRoombyHome")));
115         verify(postRequestedFor(urlMatching("/millService/v1/selectDevicebyRoom")));
116         verify(postRequestedFor(urlMatching("/millService/v1/getIndependentDevices")));
117     }
118 }