2 * Copyright (c) 2010-2023 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;
21 import java.nio.charset.StandardCharsets;
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.LENIENT)
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 = new String(
84 getClass().getResourceAsStream("/select_home_list_ok.json").readAllBytes(), StandardCharsets.UTF_8);
85 final String getRoomsByHomeResponse = new String(
86 getClass().getResourceAsStream("/get_rooms_by_home_ok.json").readAllBytes(), StandardCharsets.UTF_8);
87 final String getDeviceByRoomResponse = new String(
88 getClass().getResourceAsStream("/get_device_by_room_ok.json").readAllBytes(), StandardCharsets.UTF_8);
89 final String getIndependentDevicesResponse = new String(
90 getClass().getResourceAsStream("/get_independent_devices_ok.json").readAllBytes(),
91 StandardCharsets.UTF_8);
93 stubFor(post(urlEqualTo("/millService/v1/selectHomeList"))
94 .willReturn(aResponse().withStatus(200).withBody(getHomesResponse)));
95 stubFor(post(urlEqualTo("/millService/v1/selectRoombyHome"))
96 .willReturn(aResponse().withStatus(200).withBody(getRoomsByHomeResponse)));
97 stubFor(post(urlEqualTo("/millService/v1/selectDevicebyRoom"))
98 .willReturn(aResponse().withStatus(200).withBody(getDeviceByRoomResponse)));
99 stubFor(post(urlEqualTo("/millService/v1/getIndependentDevices"))
100 .willReturn(aResponse().withStatus(200).withBody(getIndependentDevicesResponse)));
102 when(millheatAccountMock.getConfiguration()).thenReturn(configuration);
103 when(millheatAccountMock.getUID()).thenReturn(new ThingUID("millheat:account:thinguid"));
105 final MillheatAccountConfiguration accountConfig = new MillheatAccountConfiguration();
106 accountConfig.username = "username";
107 accountConfig.password = "password";
108 when(configuration.as(eq(MillheatAccountConfiguration.class))).thenReturn(accountConfig);
110 final MillheatAccountHandler subject = new MillheatAccountHandler(millheatAccountMock, httpClient,
112 MillheatModel model = subject.refreshModel();
113 assertEquals(1, model.getHomes().size());
115 verify(postRequestedFor(urlMatching("/millService/v1/selectHomeList")));
116 verify(postRequestedFor(urlMatching("/millService/v1/selectRoombyHome")));
117 verify(postRequestedFor(urlMatching("/millService/v1/selectDevicebyRoom")));
118 verify(postRequestedFor(urlMatching("/millService/v1/getIndependentDevices")));