]> git.basschouten.com Git - openhab-addons.git/blob
33713f1b7390e9de96fb1c3f902e04a75e011216
[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.livisismarthome.internal.client;
14
15 import static org.junit.jupiter.api.Assertions.*;
16 import static org.mockito.Mockito.*;
17
18 import java.io.ByteArrayInputStream;
19 import java.net.HttpURLConnection;
20 import java.nio.charset.StandardCharsets;
21 import java.util.Arrays;
22 import java.util.Collections;
23 import java.util.List;
24
25 import org.eclipse.jdt.annotation.NonNullByDefault;
26 import org.eclipse.jetty.client.api.ContentResponse;
27 import org.eclipse.jetty.http.HttpStatus;
28 import org.junit.jupiter.api.BeforeEach;
29 import org.junit.jupiter.api.Test;
30 import org.openhab.binding.livisismarthome.internal.client.api.entity.capability.CapabilityStateDTO;
31 import org.openhab.binding.livisismarthome.internal.handler.LivisiBridgeConfiguration;
32 import org.openhab.core.auth.client.oauth2.AccessTokenResponse;
33 import org.openhab.core.auth.client.oauth2.OAuthClientService;
34
35 /**
36  * @author Sven Strohschein - Initial contribution
37  */
38 @NonNullByDefault
39 public class LivisiClientTest {
40
41     private static final String STATUS_URL = "http://127.0.0.1:8080/status";
42     private static final String DEVICES_URL = "http://127.0.0.1:8080/device";
43     private static final String CAPABILITY_STATES_URL = "http://127.0.0.1:8080/capability/states";
44
45     private @NonNullByDefault({}) LivisiClient client;
46     private @NonNullByDefault({}) URLConnectionFactory connectionFactoryMock;
47
48     @BeforeEach
49     public void before() throws Exception {
50         AccessTokenResponse accessTokenResponse = new AccessTokenResponse();
51         accessTokenResponse.setAccessToken("accessToken");
52
53         OAuthClientService oAuthClientMock = mock(OAuthClientService.class);
54         when(oAuthClientMock.getAccessTokenResponse()).thenReturn(accessTokenResponse);
55
56         connectionFactoryMock = mock(URLConnectionFactory.class);
57
58         LivisiBridgeConfiguration bridgeConfiguration = new LivisiBridgeConfiguration();
59         bridgeConfiguration.host = "127.0.0.1";
60         client = new LivisiClient(bridgeConfiguration, oAuthClientMock, connectionFactoryMock);
61     }
62
63     @Test
64     public void testRefreshStatusSHC1() throws Exception {
65         mockRequest(STATUS_URL,
66                 "{\"serialNumber\":\"123\",\"connected\":true,\"appVersion\":\"3.1.1025.0\",\"osVersion\":\"1.914\",\"configVersion\":\"1200\",\"controllerType\":\"Classic\"}");
67         assertEquals("1200", client.refreshStatus());
68     }
69
70     @Test
71     public void testRefreshStatusSHC2() throws Exception {
72         mockRequest(STATUS_URL,
73                 "{\"gateway\": {\"serialNumber\": \"123\","
74                         + "\"appVersion\": \"1.2.37.430\",\"osVersion\": \"8.17\",\"configVersion\": 1200,"
75                         + "\"operationStatus\": \"active\",\"network\": "
76                         + "{\"ethCableAttached\": true,\"inUseAdapter\": \"eth\",\"hotspotActive\": false,"
77                         + "\"wpsActive\": false,\"backendAvailable\": true,\"ethMacAddress\": "
78                         + "[{\"id\": \"456\",\"config\": {\"name\": \"Arbeitszimmer\",\"type\": \"Other\"},"
79                         + "\"desc\": \"/desc/location\"}]}}}");
80         assertEquals("1200", client.refreshStatus());
81     }
82
83     @Test
84     public void testGetDevices() throws Exception {
85         mockRequest(DEVICES_URL, "[ { id: 123 }, { id: 789, type: 'VariableActuator' } ]");
86         assertEquals(2, client.getDevices(Arrays.asList("123", "456")).size());
87     }
88
89     @Test
90     public void testGetDevicesNoDeviceIds() throws Exception {
91         mockRequest(DEVICES_URL, "[ { id: 123 } ]");
92         assertEquals(0, client.getDevices(Collections.emptyList()).size());
93     }
94
95     @Test
96     public void testGetDevicesFalseDeviceIds() throws Exception {
97         mockRequest(DEVICES_URL, "[ { id: 789 }]");
98         assertEquals(0, client.getDevices(Arrays.asList("123", "456")).size());
99     }
100
101     @Test
102     public void testGetDevicesNoDevicesNoDeviceIds() throws Exception {
103         mockRequest(DEVICES_URL, "[]");
104         assertEquals(0, client.getDevices(Collections.emptyList()).size());
105     }
106
107     @Test
108     public void testGetDevicesNoDevicesDeviceIds() throws Exception {
109         mockRequest(DEVICES_URL, "[]");
110         assertEquals(0, client.getDevices(Arrays.asList("123", "456")).size());
111     }
112
113     @Test
114     public void testGetCapabilityStates() throws Exception {
115         mockRequest(CAPABILITY_STATES_URL,
116                 "[{\"id\":\"123\",\"state\":{\"isOpen\":{\"value\":false,\"lastChanged\":\"2022-03-12T20:54:50.6930000Z\"}}},{\"id\":\"456\",\"state\":{\"isOpen\":{\"value\":false,\"lastChanged\":\"2022-03-13T13:48:36.6830000Z\"}}},{\"id\":\"789\",\"state\":{\"isOpen\":{\"value\":true,\"lastChanged\":\"2022-03-13T13:48:36.6830000Z\"}}}]");
117         assertEquals(3, client.getCapabilityStates().size());
118     }
119
120     @Test
121     public void testGetCapabilityStatesStateNULL() throws Exception {
122         mockRequest(CAPABILITY_STATES_URL,
123                 "[{\"id\":\"123\",\"state\":{\"isOpen\":{\"value\":false,\"lastChanged\":\"2022-03-12T20:54:50.6930000Z\"}}},{\"id\":\"456\",\"state\":[]},{\"id\":\"789\",\"state\":[]}]");
124         List<CapabilityStateDTO> capabilityStates = client.getCapabilityStates();
125         assertEquals(3, capabilityStates.size());
126     }
127
128     private void mockRequest(String url, String responseContent) throws Exception {
129         ContentResponse response = mock(ContentResponse.class);
130         when(response.getStatus()).thenReturn(HttpStatus.OK_200);
131         when(response.getContentAsString()).thenReturn(responseContent);
132
133         HttpURLConnection connectionMock = mock(HttpURLConnection.class);
134         when(connectionMock.getResponseCode()).thenReturn(HttpStatus.OK_200);
135         when(connectionMock.getInputStream())
136                 .thenReturn(new ByteArrayInputStream(responseContent.getBytes(StandardCharsets.UTF_8)));
137
138         when(connectionFactoryMock.createRequest(eq(url))).thenReturn(connectionMock);
139         when(connectionFactoryMock.createBaseRequest(eq(url), any(), any())).thenReturn(connectionMock);
140     }
141 }