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.mockito.ArgumentMatchers.eq;
17 import static org.mockito.Mockito.when;
19 import java.io.IOException;
21 import org.apache.commons.io.IOUtils;
22 import org.eclipse.jetty.client.HttpClient;
23 import org.junit.After;
24 import org.junit.Assert;
25 import org.junit.Before;
26 import org.junit.Rule;
27 import org.junit.Test;
28 import org.mockito.Mock;
29 import org.mockito.MockitoAnnotations;
30 import org.openhab.binding.millheat.internal.config.MillheatAccountConfiguration;
31 import org.openhab.binding.millheat.internal.handler.MillheatAccountHandler;
32 import org.openhab.binding.millheat.internal.model.MillheatModel;
33 import org.openhab.core.config.core.Configuration;
34 import org.openhab.core.thing.Bridge;
35 import org.openhab.core.thing.ThingUID;
36 import org.osgi.framework.BundleContext;
38 import com.github.tomakehurst.wiremock.core.WireMockConfiguration;
39 import com.github.tomakehurst.wiremock.junit.WireMockRule;
42 * @author Arne Seime - Initial contribution
44 public class MillHeatAccountHandlerTest {
46 public WireMockRule wireMockRule = new WireMockRule(WireMockConfiguration.options().dynamicPort());
49 private Bridge millheatAccountMock;
51 private HttpClient httpClient;
53 private Configuration configuration;
56 private BundleContext bundleContext;
59 public void setUp() throws Exception {
60 MockitoAnnotations.initMocks(this);
61 httpClient = new HttpClient();
63 MillheatAccountHandler.authEndpoint = "http://localhost:" + wireMockRule.port() + "/zc-account/v1/";
64 MillheatAccountHandler.serviceEndpoint = "http://localhost:" + wireMockRule.port() + "/millService/v1/";
68 public void shutdown() throws Exception {
73 public void testUpdateModel() throws InterruptedException, IOException, MillheatCommunicationException {
74 final String getHomesResponse = IOUtils.toString(getClass().getResourceAsStream("/select_home_list_ok.json"));
75 final String getRoomsByHomeResponse = IOUtils
76 .toString(getClass().getResourceAsStream("/get_rooms_by_home_ok.json"));
77 final String getDeviceByRoomResponse = IOUtils
78 .toString(getClass().getResourceAsStream("/get_device_by_room_ok.json"));
79 final String getIndependentDevicesResponse = IOUtils
80 .toString(getClass().getResourceAsStream("/get_independent_devices_ok.json"));
82 stubFor(post(urlEqualTo("/millService/v1/selectHomeList"))
83 .willReturn(aResponse().withStatus(200).withBody(getHomesResponse)));
84 stubFor(post(urlEqualTo("/millService/v1/selectRoombyHome"))
85 .willReturn(aResponse().withStatus(200).withBody(getRoomsByHomeResponse)));
86 stubFor(post(urlEqualTo("/millService/v1/selectDevicebyRoom"))
87 .willReturn(aResponse().withStatus(200).withBody(getDeviceByRoomResponse)));
88 stubFor(post(urlEqualTo("/millService/v1/getIndependentDevices"))
89 .willReturn(aResponse().withStatus(200).withBody(getIndependentDevicesResponse)));
91 when(millheatAccountMock.getConfiguration()).thenReturn(configuration);
92 when(millheatAccountMock.getUID()).thenReturn(new ThingUID("millheat:account:thinguid"));
94 final MillheatAccountConfiguration accountConfig = new MillheatAccountConfiguration();
95 accountConfig.username = "username";
96 accountConfig.password = "password";
97 when(configuration.as(eq(MillheatAccountConfiguration.class))).thenReturn(accountConfig);
99 final MillheatAccountHandler subject = new MillheatAccountHandler(millheatAccountMock, httpClient,
101 MillheatModel model = subject.refreshModel();
102 Assert.assertEquals(1, model.getHomes().size());
104 verify(postRequestedFor(urlMatching("/millService/v1/selectHomeList")));
105 verify(postRequestedFor(urlMatching("/millService/v1/selectRoombyHome")));
106 verify(postRequestedFor(urlMatching("/millService/v1/selectDevicebyRoom")));
107 verify(postRequestedFor(urlMatching("/millService/v1/getIndependentDevices")));