]> git.basschouten.com Git - openhab-addons.git/blob
4b92192f3d1e0a471624b0dba3027fb023c34c58
[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.ihc.internal.ws.services;
14
15 import static org.junit.jupiter.api.Assertions.*;
16 import static org.mockito.ArgumentMatchers.*;
17 import static org.mockito.Mockito.*;
18
19 import java.net.SocketTimeoutException;
20 import java.util.Arrays;
21 import java.util.HashMap;
22 import java.util.Map;
23
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;
33
34 /**
35  * Test for IHC / ELKO binding
36  *
37  * @author Pauli Anttila - Initial contribution
38  */
39 public class IhcControllerServiceTest {
40
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<>();
45     private String query;
46     private final int timeout = 100;
47
48     @BeforeEach
49     public void setUp() throws IhcExecption, SocketTimeoutException {
50         ihcControllerService = spy(new IhcControllerService(host, timeout, new IhcConnectionPool()));
51
52         query = ResourceFileUtils.getFileContent("EmptyQuery.xml");
53         requestProps.clear();
54     }
55
56     @Test
57     public void projectInfoTest() throws IhcExecption {
58         requestProps.put("SOAPAction", "getProjectInfo");
59         final String projectInfoResponse = ResourceFileUtils.getFileContent("GetProjectInfoResponse.xml");
60
61         doReturn(projectInfoResponse).when(ihcControllerService).sendQuery(eq(url), eq(requestProps), eq(query),
62                 eq(timeout));
63
64         final WSProjectInfo result = ihcControllerService.getProjectInfo();
65
66         assertEquals("Pertti 'Speedy' Keinonen", result.getCustomerName());
67     }
68
69     @Test
70     public void projectNumberOfSegmentsTest() throws IhcExecption {
71         final String projectNumberOfSegmentsResponse = ResourceFileUtils
72                 .getFileContent("GetProjectNumberOfSegmentsResponse.xml");
73
74         requestProps.put("SOAPAction", "getIHCProjectNumberOfSegments");
75         doReturn(projectNumberOfSegmentsResponse).when(ihcControllerService).sendQuery(eq(url), eq(requestProps),
76                 eq(query), eq(timeout));
77
78         final int result = ihcControllerService.getProjectNumberOfSegments();
79
80         assertEquals(28, result);
81     }
82
83     @Test
84     public void projectSegmentationSizeTest() throws IhcExecption {
85         final String projectSegmentationSizeResponse = ResourceFileUtils
86                 .getFileContent("GetProjectSegmentationSizeResponse.xml");
87
88         requestProps.put("SOAPAction", "getIHCProjectSegmentationSize");
89
90         doReturn(projectSegmentationSizeResponse).when(ihcControllerService).sendQuery(eq(url), eq(requestProps),
91                 eq(query), anyInt());
92
93         final int result = ihcControllerService.getProjectSegmentationSize();
94
95         assertEquals(7500, result);
96     }
97
98     @Test
99     public void controllerStateTest() throws IhcExecption {
100         final String controllerStateResponse = ResourceFileUtils.getFileContent("ControllerStateResponse.xml");
101
102         requestProps.put("SOAPAction", "getState");
103         doReturn(controllerStateResponse).when(ihcControllerService).sendQuery(eq(url), eq(requestProps), eq(query),
104                 anyInt());
105
106         final WSControllerState result = ihcControllerService.getControllerState();
107
108         assertEquals(IhcClient.CONTROLLER_STATE_READY, result.getState());
109     }
110
111     @Test
112     public void waitForControllerStateChangeQueryTest() throws IhcExecption {
113         final String waitForControllerStateChangeQuery = ResourceFileUtils
114                 .getFileContent("WaitForControllerStateChangeQuery.xml");
115         final String waitForControllerStateChangeResponse = ResourceFileUtils
116                 .getFileContent("WaitForControllerStateChangeResponse.xml");
117
118         requestProps.put("SOAPAction", "waitForControllerStateChange");
119         doReturn(waitForControllerStateChangeResponse).when(ihcControllerService).sendQuery(eq(url), eq(requestProps),
120                 eq(waitForControllerStateChangeQuery), anyInt());
121
122         WSControllerState previousState = new WSControllerState();
123         previousState.setState(IhcClient.CONTROLLER_STATE_INITIALIZE);
124         final WSControllerState result = ihcControllerService.waitStateChangeNotifications(previousState, 5);
125
126         assertEquals(IhcClient.CONTROLLER_STATE_READY, result.getState());
127     }
128
129     @Test
130     public void projectSegmentQueryTest() throws IhcExecption {
131         final String projectSegmentQuery = ResourceFileUtils.getFileContent("GetProjectSegmentQuery.xml");
132         final String projectSegmentResponse = ResourceFileUtils.getFileContent("GetProjectSegmentResponse.xml");
133
134         requestProps.put("SOAPAction", "getIHCProjectSegment");
135         doReturn(projectSegmentResponse).when(ihcControllerService).sendQuery(eq(url), eq(requestProps),
136                 eq(projectSegmentQuery), anyInt());
137
138         final byte[] expectedResult = "LvVF4VWSi0WqRKps7lGH6U....OBCl1gwKGbvYM1SDh".getBytes();
139         final WSFile result = ihcControllerService.getProjectSegment(1, 1001, 2002);
140
141         assertTrue(Arrays.equals(expectedResult, result.getData()), "Result bytes doesn't match to expected bytes");
142     }
143 }