]> git.basschouten.com Git - openhab-addons.git/blob
734ea910549ed732cc45251439bfd96687da37d6
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 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 import java.nio.charset.StandardCharsets;
22
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.LENIENT)
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 = 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);
92
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)));
101
102         when(millheatAccountMock.getConfiguration()).thenReturn(configuration);
103         when(millheatAccountMock.getUID()).thenReturn(new ThingUID("millheat:account:thinguid"));
104
105         final MillheatAccountConfiguration accountConfig = new MillheatAccountConfiguration();
106         accountConfig.username = "username";
107         accountConfig.password = "password";
108         when(configuration.as(eq(MillheatAccountConfiguration.class))).thenReturn(accountConfig);
109
110         final MillheatAccountHandler subject = new MillheatAccountHandler(millheatAccountMock, httpClient,
111                 bundleContext);
112         MillheatModel model = subject.refreshModel();
113         assertEquals(1, model.getHomes().size());
114
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")));
119     }
120 }