]> git.basschouten.com Git - openhab-addons.git/blob
a019de65dcf23ffb2d8f26ba0b1ca582e4a6a18a
[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.eq;
17 import static org.mockito.Mockito.*;
18
19 import java.net.SocketTimeoutException;
20 import java.util.HashMap;
21 import java.util.List;
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.ResourceFileUtils;
27 import org.openhab.binding.ihc.internal.ws.datatypes.WSRFDevice;
28 import org.openhab.binding.ihc.internal.ws.exeptions.IhcExecption;
29 import org.openhab.binding.ihc.internal.ws.http.IhcConnectionPool;
30
31 /**
32  * Test for IHC / ELKO binding
33  *
34  * @author Pauli Anttila - Initial contribution
35  */
36 public class IhcAirlinkManagementServiceTest {
37
38     private IhcAirlinkManagementService ihcAirlinkManagementService;
39     private final String host = "1.1.1.1";
40     private final String url = "https://1.1.1.1/ws/AirlinkManagementService";
41     private Map<String, String> requestProps = new HashMap<>();
42     private String query;
43     private final int timeout = 100;
44
45     @BeforeEach
46     public void setUp() throws IhcExecption, SocketTimeoutException {
47         ihcAirlinkManagementService = spy(new IhcAirlinkManagementService(host, timeout, new IhcConnectionPool()));
48
49         query = ResourceFileUtils.getFileContent("EmptyQuery.xml");
50
51         requestProps.clear();
52         requestProps.put("SOAPAction", "getDetectedDeviceList");
53     }
54
55     @Test
56     public void test() throws IhcExecption {
57         final String response = ResourceFileUtils.getFileContent("GetDetectedDeviceListResponse.xml");
58         doReturn(response).when(ihcAirlinkManagementService).sendQuery(eq(url), eq(requestProps), eq(query),
59                 eq(timeout));
60
61         final List<WSRFDevice> result = ihcAirlinkManagementService.getDetectedDeviceList();
62
63         assertEquals(1, result.size());
64
65         assertEquals(1, result.get(0).getBatteryLevel());
66         assertEquals(true, result.get(0).getDetected());
67         assertEquals(2049, result.get(0).getDeviceType());
68         assertEquals(123456789, result.get(0).getSerialNumber());
69         assertEquals(10, result.get(0).getSignalStrength());
70         assertEquals(1, result.get(0).getVersion());
71     }
72
73     @Test
74     public void testEmptyList() throws IhcExecption {
75         final String response = ResourceFileUtils.getFileContent("GetEmptyDetectedDeviceListResponse.xml");
76         doReturn(response).when(ihcAirlinkManagementService).sendQuery(eq(url), eq(requestProps), eq(query),
77                 eq(timeout));
78
79         final List<WSRFDevice> result = ihcAirlinkManagementService.getDetectedDeviceList();
80
81         assertEquals(0, result.size());
82     }
83 }