]> git.basschouten.com Git - openhab-addons.git/blob
3484d863cc9674804aedd7d3efdb882eefb493cd
[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.mockito.ArgumentMatchers.eq;
17 import static org.mockito.Mockito.when;
18
19 import java.io.IOException;
20
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;
37
38 import com.github.tomakehurst.wiremock.core.WireMockConfiguration;
39 import com.github.tomakehurst.wiremock.junit.WireMockRule;
40
41 /**
42  * @author Arne Seime - Initial contribution
43  */
44 public class MillHeatAccountHandlerTest {
45     @Rule
46     public WireMockRule wireMockRule = new WireMockRule(WireMockConfiguration.options().dynamicPort());
47
48     @Mock
49     private Bridge millheatAccountMock;
50
51     private HttpClient httpClient;
52     @Mock
53     private Configuration configuration;
54
55     @Mock
56     private BundleContext bundleContext;
57
58     @Before
59     public void setUp() throws Exception {
60         MockitoAnnotations.initMocks(this);
61         httpClient = new HttpClient();
62         httpClient.start();
63         MillheatAccountHandler.authEndpoint = "http://localhost:" + wireMockRule.port() + "/zc-account/v1/";
64         MillheatAccountHandler.serviceEndpoint = "http://localhost:" + wireMockRule.port() + "/millService/v1/";
65     }
66
67     @After
68     public void shutdown() throws Exception {
69         httpClient.stop();
70     }
71
72     @Test
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"));
81
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)));
90
91         when(millheatAccountMock.getConfiguration()).thenReturn(configuration);
92         when(millheatAccountMock.getUID()).thenReturn(new ThingUID("millheat:account:thinguid"));
93
94         final MillheatAccountConfiguration accountConfig = new MillheatAccountConfiguration();
95         accountConfig.username = "username";
96         accountConfig.password = "password";
97         when(configuration.as(eq(MillheatAccountConfiguration.class))).thenReturn(accountConfig);
98
99         final MillheatAccountHandler subject = new MillheatAccountHandler(millheatAccountMock, httpClient,
100                 bundleContext);
101         MillheatModel model = subject.refreshModel();
102         Assert.assertEquals(1, model.getHomes().size());
103
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")));
108     }
109 }