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.millheat.internal;
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;
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;
40 import com.github.tomakehurst.wiremock.WireMockServer;
41 import com.github.tomakehurst.wiremock.client.WireMock;
42 import com.github.tomakehurst.wiremock.core.WireMockConfiguration;
45 * @author Arne Seime - Initial contribution
47 @ExtendWith(MockitoExtension.class)
48 @MockitoSettings(strictness = Strictness.WARN)
49 public class MillHeatAccountHandlerTest {
51 private WireMockServer wireMockServer;
53 private HttpClient httpClient;
55 private @Mock BundleContext bundleContext;
56 private @Mock Configuration configuration;
57 private @Mock Bridge millheatAccountMock;
60 public void setUp() throws Exception {
61 wireMockServer = new WireMockServer(WireMockConfiguration.options().dynamicPort());
62 wireMockServer.start();
64 int port = wireMockServer.port();
65 WireMock.configureFor("localhost", port);
67 httpClient = new HttpClient();
70 MillheatAccountHandler.authEndpoint = "http://localhost:" + port + "/zc-account/v1/";
71 MillheatAccountHandler.serviceEndpoint = "http://localhost:" + port + "/millService/v1/";
75 public void shutdown() throws Exception {
77 wireMockServer.stop();
78 wireMockServer.resetAll();
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"));
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)));
100 when(millheatAccountMock.getConfiguration()).thenReturn(configuration);
101 when(millheatAccountMock.getUID()).thenReturn(new ThingUID("millheat:account:thinguid"));
103 final MillheatAccountConfiguration accountConfig = new MillheatAccountConfiguration();
104 accountConfig.username = "username";
105 accountConfig.password = "password";
106 when(configuration.as(eq(MillheatAccountConfiguration.class))).thenReturn(accountConfig);
108 final MillheatAccountHandler subject = new MillheatAccountHandler(millheatAccountMock, httpClient,
110 MillheatModel model = subject.refreshModel();
111 assertEquals(1, model.getHomes().size());
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")));