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.ihc.internal.ws.services;
15 import static org.junit.jupiter.api.Assertions.*;
16 import static org.mockito.ArgumentMatchers.*;
17 import static org.mockito.Mockito.*;
19 import java.net.SocketTimeoutException;
20 import java.util.Arrays;
21 import java.util.HashMap;
24 import org.junit.jupiter.api.BeforeEach;
25 import org.junit.jupiter.api.Test;
26 import org.openhab.binding.ihc.internal.ws.IhcClient;
27 import org.openhab.binding.ihc.internal.ws.ResourceFileUtils;
28 import org.openhab.binding.ihc.internal.ws.datatypes.WSControllerState;
29 import org.openhab.binding.ihc.internal.ws.datatypes.WSFile;
30 import org.openhab.binding.ihc.internal.ws.datatypes.WSProjectInfo;
31 import org.openhab.binding.ihc.internal.ws.exeptions.IhcExecption;
32 import org.openhab.binding.ihc.internal.ws.http.IhcConnectionPool;
35 * Test for IHC / ELKO binding
37 * @author Pauli Anttila - Initial contribution
39 public class IhcControllerServiceTest {
41 private IhcControllerService ihcControllerService;
42 private final String host = "1.1.1.1";
43 private final String url = "https://1.1.1.1/ws/ControllerService";
44 private Map<String, String> requestProps = new HashMap<>();
46 private final int timeout = 100;
49 public void setUp() throws IhcExecption, SocketTimeoutException {
50 ihcControllerService = spy(new IhcControllerService(host, timeout, new IhcConnectionPool()));
52 query = ResourceFileUtils.getFileContent("EmptyQuery.xml");
57 public void projectInfoTest() throws IhcExecption {
58 requestProps.put("SOAPAction", "getProjectInfo");
59 final String projectInfoResponse = ResourceFileUtils.getFileContent("GetProjectInfoResponse.xml");
61 doReturn(projectInfoResponse).when(ihcControllerService).sendQuery(eq(url), eq(requestProps), eq(query),
64 final WSProjectInfo result = ihcControllerService.getProjectInfo();
66 assertEquals("Pertti 'Speedy' Keinonen", result.getCustomerName());
70 public void projectNumberOfSegmentsTest() throws IhcExecption {
71 final String projectNumberOfSegmentsResponse = ResourceFileUtils
72 .getFileContent("GetProjectNumberOfSegmentsResponse.xml");
74 requestProps.put("SOAPAction", "getIHCProjectNumberOfSegments");
75 doReturn(projectNumberOfSegmentsResponse).when(ihcControllerService).sendQuery(eq(url), eq(requestProps),
76 eq(query), eq(timeout));
78 final int result = ihcControllerService.getProjectNumberOfSegments();
80 assertEquals(28, result);
84 public void projectSegmentationSizeTest() throws IhcExecption {
85 final String projectSegmentationSizeResponse = ResourceFileUtils
86 .getFileContent("GetProjectSegmentationSizeResponse.xml");
88 requestProps.put("SOAPAction", "getIHCProjectSegmentationSize");
90 doReturn(projectSegmentationSizeResponse).when(ihcControllerService).sendQuery(eq(url), eq(requestProps),
93 final int result = ihcControllerService.getProjectSegmentationSize();
95 assertEquals(7500, result);
99 public void controllerStateTest() throws IhcExecption {
100 final String controllerStateResponse = ResourceFileUtils.getFileContent("ControllerStateResponse.xml");
102 requestProps.put("SOAPAction", "getState");
103 doReturn(controllerStateResponse).when(ihcControllerService).sendQuery(eq(url), eq(requestProps), eq(query),
106 final WSControllerState result = ihcControllerService.getControllerState();
108 assertEquals(IhcClient.CONTROLLER_STATE_READY, result.getState());
112 public void waitForControllerStateChangeQueryTest() throws IhcExecption {
113 final String waitForControllerStateChangeQuery = ResourceFileUtils
114 .getFileContent("WaitForControllerStateChangeQuery.xml");
115 final String waitForControllerStateChangeResponse = ResourceFileUtils
116 .getFileContent("WaitForControllerStateChangeResponse.xml");
118 requestProps.put("SOAPAction", "waitForControllerStateChange");
119 doReturn(waitForControllerStateChangeResponse).when(ihcControllerService).sendQuery(eq(url), eq(requestProps),
120 eq(waitForControllerStateChangeQuery), anyInt());
122 WSControllerState previousState = new WSControllerState();
123 previousState.setState(IhcClient.CONTROLLER_STATE_INITIALIZE);
124 final WSControllerState result = ihcControllerService.waitStateChangeNotifications(previousState, 5);
126 assertEquals(IhcClient.CONTROLLER_STATE_READY, result.getState());
130 public void projectSegmentQueryTest() throws IhcExecption {
131 final String projectSegmentQuery = ResourceFileUtils.getFileContent("GetProjectSegmentQuery.xml");
132 final String projectSegmentResponse = ResourceFileUtils.getFileContent("GetProjectSegmentResponse.xml");
134 requestProps.put("SOAPAction", "getIHCProjectSegment");
135 doReturn(projectSegmentResponse).when(ihcControllerService).sendQuery(eq(url), eq(requestProps),
136 eq(projectSegmentQuery), anyInt());
138 final byte[] expectedResult = "LvVF4VWSi0WqRKps7lGH6U....OBCl1gwKGbvYM1SDh".getBytes();
139 final WSFile result = ihcControllerService.getProjectSegment(1, 1001, 2002);
141 assertTrue(Arrays.equals(expectedResult, result.getData()), "Result bytes doesn't match to expected bytes");