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.ArrayList;
21 import java.util.List;
23 import org.junit.jupiter.api.BeforeEach;
24 import org.junit.jupiter.api.Test;
25 import org.openhab.binding.ihc.internal.ws.ResourceFileUtils;
26 import org.openhab.binding.ihc.internal.ws.exeptions.IhcExecption;
27 import org.openhab.binding.ihc.internal.ws.http.IhcConnectionPool;
28 import org.openhab.binding.ihc.internal.ws.resourcevalues.WSBooleanValue;
29 import org.openhab.binding.ihc.internal.ws.resourcevalues.WSDateValue;
30 import org.openhab.binding.ihc.internal.ws.resourcevalues.WSEnumValue;
31 import org.openhab.binding.ihc.internal.ws.resourcevalues.WSFloatingPointValue;
32 import org.openhab.binding.ihc.internal.ws.resourcevalues.WSIntegerValue;
33 import org.openhab.binding.ihc.internal.ws.resourcevalues.WSResourceValue;
34 import org.openhab.binding.ihc.internal.ws.resourcevalues.WSTimeValue;
35 import org.openhab.binding.ihc.internal.ws.resourcevalues.WSTimerValue;
36 import org.openhab.binding.ihc.internal.ws.resourcevalues.WSWeekdayValue;
39 * Test for IHC / ELKO binding
41 * @author Pauli Anttila - Initial contribution
43 public class IhcResourceInteractionServiceTest {
45 private IhcResourceInteractionService ihcResourceInteractionService;
46 private final String host = "1.1.1.1";
47 private final String url = "https://1.1.1.1/ws/ResourceInteractionService";
50 public void setUp() throws IhcExecption, SocketTimeoutException {
51 ihcResourceInteractionService = spy(new IhcResourceInteractionService(host, 0, new IhcConnectionPool()));
53 final String query = ResourceFileUtils.getFileContent("ResourceValueQueryTemplate.xml");
54 final String response11111 = ResourceFileUtils.getFileContent("ResourceValueQueryResponse11111.xml");
55 final String response22222 = ResourceFileUtils.getFileContent("ResourceValueQueryResponse22222.xml");
56 final String response33333 = ResourceFileUtils.getFileContent("ResourceValueQueryResponse33333.xml");
57 final String response44444 = ResourceFileUtils.getFileContent("ResourceValueQueryResponse44444.xml");
58 final String response55555 = ResourceFileUtils.getFileContent("ResourceValueQueryResponse55555.xml");
59 final String response66666 = ResourceFileUtils.getFileContent("ResourceValueQueryResponse66666.xml");
60 final String response77777 = ResourceFileUtils.getFileContent("ResourceValueQueryResponse77777.xml");
61 final String response88888 = ResourceFileUtils.getFileContent("ResourceValueQueryResponse88888.xml");
63 doReturn(response11111).when(ihcResourceInteractionService).sendQuery(eq(url), any(),
64 eq(String.format(query, 11111)), anyInt());
65 doReturn(response22222).when(ihcResourceInteractionService).sendQuery(eq(url), any(),
66 eq(String.format(query, 22222)), anyInt());
67 doReturn(response33333).when(ihcResourceInteractionService).sendQuery(eq(url), any(),
68 eq(String.format(query, 33333)), anyInt());
69 doReturn(response44444).when(ihcResourceInteractionService).sendQuery(eq(url), any(),
70 eq(String.format(query, 44444)), anyInt());
71 doReturn(response55555).when(ihcResourceInteractionService).sendQuery(eq(url), any(),
72 eq(String.format(query, 55555)), anyInt());
73 doReturn(response66666).when(ihcResourceInteractionService).sendQuery(eq(url), any(),
74 eq(String.format(query, 66666)), anyInt());
75 doReturn(response77777).when(ihcResourceInteractionService).sendQuery(eq(url), any(),
76 eq(String.format(query, 77777)), anyInt());
77 doReturn(response88888).when(ihcResourceInteractionService).sendQuery(eq(url), any(),
78 eq(String.format(query, 88888)), anyInt());
80 final String updateOkResult = ResourceFileUtils.getFileContent("ResourceValueUpdateOkResult.xml");
81 final String update100001 = ResourceFileUtils.getFileContent("ResourceValueUpdate100001.xml");
82 final String update200002 = ResourceFileUtils.getFileContent("ResourceValueUpdate200002.xml");
83 final String update300003 = ResourceFileUtils.getFileContent("ResourceValueUpdate300003.xml");
84 final String update400004 = ResourceFileUtils.getFileContent("ResourceValueUpdate400004.xml");
85 final String update500005 = ResourceFileUtils.getFileContent("ResourceValueUpdate500005.xml");
86 final String update600006 = ResourceFileUtils.getFileContent("ResourceValueUpdate600006.xml");
87 final String update700007 = ResourceFileUtils.getFileContent("ResourceValueUpdate700007.xml");
88 final String update800008 = ResourceFileUtils.getFileContent("ResourceValueUpdate800008.xml");
90 doReturn(updateOkResult).when(ihcResourceInteractionService).sendQuery(eq(url), any(), eq(update100001),
92 doReturn(updateOkResult).when(ihcResourceInteractionService).sendQuery(eq(url), any(), eq(update200002),
94 doReturn(updateOkResult).when(ihcResourceInteractionService).sendQuery(eq(url), any(), eq(update300003),
96 doReturn(updateOkResult).when(ihcResourceInteractionService).sendQuery(eq(url), any(), eq(update400004),
98 doReturn(updateOkResult).when(ihcResourceInteractionService).sendQuery(eq(url), any(), eq(update500005),
100 doReturn(updateOkResult).when(ihcResourceInteractionService).sendQuery(eq(url), any(), eq(update600006),
102 doReturn(updateOkResult).when(ihcResourceInteractionService).sendQuery(eq(url), any(), eq(update700007),
104 doReturn(updateOkResult).when(ihcResourceInteractionService).sendQuery(eq(url), any(), eq(update800008),
107 final String updateFailureResult = ResourceFileUtils.getFileContent("ResourceValueUpdateFailureResult.xml");
108 final String update100011 = ResourceFileUtils.getFileContent("ResourceValueUpdate100011.xml");
110 doReturn(updateFailureResult).when(ihcResourceInteractionService).sendQuery(eq(url), any(), eq(update100011),
113 final String resourceValueNotificationsQuery = ResourceFileUtils
114 .getFileContent("ResourceValueNotificationsQuery.xml");
115 final String resourceValueNotificationsResponse = ResourceFileUtils
116 .getFileContent("ResourceValueNotificationsResponse.xml");
118 doReturn(resourceValueNotificationsResponse).when(ihcResourceInteractionService).sendQuery(eq(url), any(),
119 eq(resourceValueNotificationsQuery), anyInt());
123 public void testWSBooleanValueQuery() throws IhcExecption {
124 final WSBooleanValue val = (WSBooleanValue) ihcResourceInteractionService.resourceQuery(11111);
125 assertEquals(11111, val.resourceID);
126 assertEquals(true, val.value);
130 public void testWSFloatingPointValueQuery() throws IhcExecption {
131 final WSFloatingPointValue val = (WSFloatingPointValue) ihcResourceInteractionService.resourceQuery(22222);
132 assertEquals(22222, val.resourceID);
133 assertEquals(24.399999618530273, val.value, 0.000001);
134 assertEquals(-1000.0, val.minimumValue, 0.01);
135 assertEquals(1000.0, val.maximumValue, 0.01);
139 public void testWSEnumValueQuery() throws IhcExecption {
140 final WSEnumValue val = (WSEnumValue) ihcResourceInteractionService.resourceQuery(33333);
141 assertEquals(33333, val.resourceID);
142 assertEquals(4236359, val.definitionTypeID);
143 assertEquals(4236872, val.enumValueID);
144 assertEquals("test value", val.enumName);
148 public void testWSIntegerValueQuery() throws IhcExecption {
149 final WSIntegerValue val = (WSIntegerValue) ihcResourceInteractionService.resourceQuery(44444);
150 assertEquals(44444, val.resourceID);
151 assertEquals(424561, val.value);
152 assertEquals(-2147483648, val.minimumValue);
153 assertEquals(2147483647, val.maximumValue);
157 public void testWSTimerValueQuery() throws IhcExecption {
158 final WSTimerValue val = (WSTimerValue) ihcResourceInteractionService.resourceQuery(55555);
159 assertEquals(55555, val.resourceID);
160 assertEquals(13851, val.milliseconds);
164 public void testWSWeekdayValueQuery() throws IhcExecption {
165 final WSWeekdayValue val = (WSWeekdayValue) ihcResourceInteractionService.resourceQuery(66666);
166 assertEquals(66666, val.resourceID);
167 assertEquals(2, val.weekdayNumber);
171 public void testWSDateValueQuery() throws IhcExecption {
172 final WSDateValue val = (WSDateValue) ihcResourceInteractionService.resourceQuery(77777);
173 assertEquals(77777, val.resourceID);
174 assertEquals(2018, val.year);
175 assertEquals(10, val.month);
176 assertEquals(22, val.day);
180 public void testWSTimeValueQuery() throws IhcExecption {
181 final WSTimeValue val = (WSTimeValue) ihcResourceInteractionService.resourceQuery(88888);
182 assertEquals(88888, val.resourceID);
183 assertEquals(16, val.hours);
184 assertEquals(32, val.minutes);
185 assertEquals(45, val.seconds);
189 public void testWSBooleanValueUpdate() throws IhcExecption {
190 boolean result = ihcResourceInteractionService.resourceUpdate(new WSBooleanValue(100001, true));
195 public void testWSBooleanValueUpdateFailure() throws IhcExecption {
196 boolean result = ihcResourceInteractionService.resourceUpdate(new WSBooleanValue(100011, true));
201 public void testWSFloatingPointValueUpdate() throws IhcExecption {
202 boolean result = ihcResourceInteractionService
203 .resourceUpdate(new WSFloatingPointValue(200002, 24.1, -1000.0, 1000.0));
208 public void testWSEnumValueUpdate() throws IhcExecption {
209 boolean result = ihcResourceInteractionService.resourceUpdate(new WSEnumValue(300003, 11111, 22222, "test123"));
214 public void testWSIntegerValueUpdate() throws IhcExecption {
215 boolean result = ihcResourceInteractionService.resourceUpdate(new WSIntegerValue(400004, 201, -1000, 1000));
220 public void testWSTimerValueUpdate() throws IhcExecption {
221 boolean result = ihcResourceInteractionService.resourceUpdate(new WSTimerValue(500005, 2134));
226 public void testWSWeekdayValueUpdate() throws IhcExecption {
227 boolean result = ihcResourceInteractionService.resourceUpdate(new WSWeekdayValue(600006, 4));
232 public void testWSDateValueUpdate() throws IhcExecption {
233 boolean result = ihcResourceInteractionService
234 .resourceUpdate(new WSDateValue(700007, (short) 2018, (byte) 3, (byte) 24));
239 public void testWSTimeValueUpdate() throws IhcExecption {
240 boolean result = ihcResourceInteractionService.resourceUpdate(new WSTimeValue(800008, 15, 34, 45));
245 public void testResourceValueNotifications() throws IhcExecption, SocketTimeoutException {
246 final List<WSResourceValue> list = ihcResourceInteractionService.waitResourceValueNotifications(1);
247 assertEquals(8, list.size());
249 List<WSResourceValue> found = new ArrayList<>();
251 for (WSResourceValue val : list) {
252 switch (val.resourceID) {
254 assertEquals(2018, ((WSDateValue) val).year);
255 assertEquals(9, ((WSDateValue) val).month);
256 assertEquals(28, ((WSDateValue) val).day);
260 assertEquals(10, ((WSTimeValue) val).hours);
261 assertEquals(20, ((WSTimeValue) val).minutes);
262 assertEquals(30, ((WSTimeValue) val).seconds);
266 assertEquals(456789, ((WSIntegerValue) val).value);
267 assertEquals(-2147483648, ((WSIntegerValue) val).minimumValue);
268 assertEquals(2147483647, ((WSIntegerValue) val).maximumValue);
272 assertEquals(false, ((WSBooleanValue) val).value);
276 assertEquals(true, ((WSBooleanValue) val).value);
280 assertEquals(24.50, ((WSFloatingPointValue) val).value, 0.01);
281 assertEquals(-1000.00, ((WSFloatingPointValue) val).minimumValue, 0.01);
282 assertEquals(1000.00, ((WSFloatingPointValue) val).maximumValue, 0.01);
286 assertEquals(2, ((WSWeekdayValue) val).weekdayNumber);
290 assertEquals(4236359, ((WSEnumValue) val).definitionTypeID);
291 assertEquals(4236872, ((WSEnumValue) val).enumValueID);
292 assertEquals("testVal", ((WSEnumValue) val).enumName);
297 assertEquals(8, found.size());