]> git.basschouten.com Git - openhab-addons.git/blob
80592e1398b9969806d44d687eea507c2145c4f5
[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.ArrayList;
21 import java.util.List;
22
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;
37
38 /**
39  * Test for IHC / ELKO binding
40  *
41  * @author Pauli Anttila - Initial contribution
42  */
43 public class IhcResourceInteractionServiceTest {
44
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";
48
49     @BeforeEach
50     public void setUp() throws IhcExecption, SocketTimeoutException {
51         ihcResourceInteractionService = spy(new IhcResourceInteractionService(host, 0, new IhcConnectionPool()));
52
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");
62
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());
79
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");
89
90         doReturn(updateOkResult).when(ihcResourceInteractionService).sendQuery(eq(url), any(), eq(update100001),
91                 anyInt());
92         doReturn(updateOkResult).when(ihcResourceInteractionService).sendQuery(eq(url), any(), eq(update200002),
93                 anyInt());
94         doReturn(updateOkResult).when(ihcResourceInteractionService).sendQuery(eq(url), any(), eq(update300003),
95                 anyInt());
96         doReturn(updateOkResult).when(ihcResourceInteractionService).sendQuery(eq(url), any(), eq(update400004),
97                 anyInt());
98         doReturn(updateOkResult).when(ihcResourceInteractionService).sendQuery(eq(url), any(), eq(update500005),
99                 anyInt());
100         doReturn(updateOkResult).when(ihcResourceInteractionService).sendQuery(eq(url), any(), eq(update600006),
101                 anyInt());
102         doReturn(updateOkResult).when(ihcResourceInteractionService).sendQuery(eq(url), any(), eq(update700007),
103                 anyInt());
104         doReturn(updateOkResult).when(ihcResourceInteractionService).sendQuery(eq(url), any(), eq(update800008),
105                 anyInt());
106
107         final String updateFailureResult = ResourceFileUtils.getFileContent("ResourceValueUpdateFailureResult.xml");
108         final String update100011 = ResourceFileUtils.getFileContent("ResourceValueUpdate100011.xml");
109
110         doReturn(updateFailureResult).when(ihcResourceInteractionService).sendQuery(eq(url), any(), eq(update100011),
111                 anyInt());
112
113         final String resourceValueNotificationsQuery = ResourceFileUtils
114                 .getFileContent("ResourceValueNotificationsQuery.xml");
115         final String resourceValueNotificationsResponse = ResourceFileUtils
116                 .getFileContent("ResourceValueNotificationsResponse.xml");
117
118         doReturn(resourceValueNotificationsResponse).when(ihcResourceInteractionService).sendQuery(eq(url), any(),
119                 eq(resourceValueNotificationsQuery), anyInt());
120     }
121
122     @Test
123     public void testWSBooleanValueQuery() throws IhcExecption {
124         final WSBooleanValue val = (WSBooleanValue) ihcResourceInteractionService.resourceQuery(11111);
125         assertEquals(11111, val.resourceID);
126         assertEquals(true, val.value);
127     }
128
129     @Test
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);
136     }
137
138     @Test
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);
145     }
146
147     @Test
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);
154     }
155
156     @Test
157     public void testWSTimerValueQuery() throws IhcExecption {
158         final WSTimerValue val = (WSTimerValue) ihcResourceInteractionService.resourceQuery(55555);
159         assertEquals(55555, val.resourceID);
160         assertEquals(13851, val.milliseconds);
161     }
162
163     @Test
164     public void testWSWeekdayValueQuery() throws IhcExecption {
165         final WSWeekdayValue val = (WSWeekdayValue) ihcResourceInteractionService.resourceQuery(66666);
166         assertEquals(66666, val.resourceID);
167         assertEquals(2, val.weekdayNumber);
168     }
169
170     @Test
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);
177     }
178
179     @Test
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);
186     }
187
188     @Test
189     public void testWSBooleanValueUpdate() throws IhcExecption {
190         boolean result = ihcResourceInteractionService.resourceUpdate(new WSBooleanValue(100001, true));
191         assertTrue(result);
192     }
193
194     @Test
195     public void testWSBooleanValueUpdateFailure() throws IhcExecption {
196         boolean result = ihcResourceInteractionService.resourceUpdate(new WSBooleanValue(100011, true));
197         assertFalse(result);
198     }
199
200     @Test
201     public void testWSFloatingPointValueUpdate() throws IhcExecption {
202         boolean result = ihcResourceInteractionService
203                 .resourceUpdate(new WSFloatingPointValue(200002, 24.1, -1000.0, 1000.0));
204         assertTrue(result);
205     }
206
207     @Test
208     public void testWSEnumValueUpdate() throws IhcExecption {
209         boolean result = ihcResourceInteractionService.resourceUpdate(new WSEnumValue(300003, 11111, 22222, "test123"));
210         assertTrue(result);
211     }
212
213     @Test
214     public void testWSIntegerValueUpdate() throws IhcExecption {
215         boolean result = ihcResourceInteractionService.resourceUpdate(new WSIntegerValue(400004, 201, -1000, 1000));
216         assertTrue(result);
217     }
218
219     @Test
220     public void testWSTimerValueUpdate() throws IhcExecption {
221         boolean result = ihcResourceInteractionService.resourceUpdate(new WSTimerValue(500005, 2134));
222         assertTrue(result);
223     }
224
225     @Test
226     public void testWSWeekdayValueUpdate() throws IhcExecption {
227         boolean result = ihcResourceInteractionService.resourceUpdate(new WSWeekdayValue(600006, 4));
228         assertTrue(result);
229     }
230
231     @Test
232     public void testWSDateValueUpdate() throws IhcExecption {
233         boolean result = ihcResourceInteractionService
234                 .resourceUpdate(new WSDateValue(700007, (short) 2018, (byte) 3, (byte) 24));
235         assertTrue(result);
236     }
237
238     @Test
239     public void testWSTimeValueUpdate() throws IhcExecption {
240         boolean result = ihcResourceInteractionService.resourceUpdate(new WSTimeValue(800008, 15, 34, 45));
241         assertTrue(result);
242     }
243
244     @Test
245     public void testResourceValueNotifications() throws IhcExecption, SocketTimeoutException {
246         final List<WSResourceValue> list = ihcResourceInteractionService.waitResourceValueNotifications(1);
247         assertEquals(8, list.size());
248
249         List<WSResourceValue> found = new ArrayList<>();
250
251         for (WSResourceValue val : list) {
252             switch (val.resourceID) {
253                 case 10454030:
254                     assertEquals(2018, ((WSDateValue) val).year);
255                     assertEquals(9, ((WSDateValue) val).month);
256                     assertEquals(28, ((WSDateValue) val).day);
257                     found.add(val);
258                     break;
259                 case 10454541:
260                     assertEquals(10, ((WSTimeValue) val).hours);
261                     assertEquals(20, ((WSTimeValue) val).minutes);
262                     assertEquals(30, ((WSTimeValue) val).seconds);
263                     found.add(val);
264                     break;
265                 case 10447883:
266                     assertEquals(456789, ((WSIntegerValue) val).value);
267                     assertEquals(-2147483648, ((WSIntegerValue) val).minimumValue);
268                     assertEquals(2147483647, ((WSIntegerValue) val).maximumValue);
269                     found.add(val);
270                     break;
271                 case 4133210:
272                     assertEquals(false, ((WSBooleanValue) val).value);
273                     found.add(val);
274                     break;
275                 case 3988827:
276                     assertEquals(true, ((WSBooleanValue) val).value);
277                     found.add(val);
278                     break;
279                 case 4159252:
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);
283                     found.add(val);
284                     break;
285                 case 3515401:
286                     assertEquals(2, ((WSWeekdayValue) val).weekdayNumber);
287                     found.add(val);
288                     break;
289                 case 7419663:
290                     assertEquals(4236359, ((WSEnumValue) val).definitionTypeID);
291                     assertEquals(4236872, ((WSEnumValue) val).enumValueID);
292                     assertEquals("testVal", ((WSEnumValue) val).enumName);
293                     found.add(val);
294                     break;
295             }
296         }
297         assertEquals(8, found.size());
298     }
299 }