]> git.basschouten.com Git - openhab-addons.git/blob
0a1391234cb1a04c8d014b8641bb2ba400dd3b33
[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                 """);
81         assertEquals("1200", client.refreshStatus());
82     }
83
84     @Test
85     public void testGetDevices() throws Exception {
86         mockRequest(DEVICES_URL, "[ { id: 123 }, { id: 789, type: 'VariableActuator' } ]");
87         assertEquals(2, client.getDevices(Arrays.asList("123", "456")).size());
88     }
89
90     @Test
91     public void testGetDevicesNoDeviceIds() throws Exception {
92         mockRequest(DEVICES_URL, "[ { id: 123 } ]");
93         assertEquals(0, client.getDevices(Collections.emptyList()).size());
94     }
95
96     @Test
97     public void testGetDevicesFalseDeviceIds() throws Exception {
98         mockRequest(DEVICES_URL, "[ { id: 789 }]");
99         assertEquals(0, client.getDevices(Arrays.asList("123", "456")).size());
100     }
101
102     @Test
103     public void testGetDevicesNoDevicesNoDeviceIds() throws Exception {
104         mockRequest(DEVICES_URL, "[]");
105         assertEquals(0, client.getDevices(Collections.emptyList()).size());
106     }
107
108     @Test
109     public void testGetDevicesNoDevicesDeviceIds() throws Exception {
110         mockRequest(DEVICES_URL, "[]");
111         assertEquals(0, client.getDevices(Arrays.asList("123", "456")).size());
112     }
113
114     @Test
115     public void testGetCapabilityStates() throws Exception {
116         mockRequest(CAPABILITY_STATES_URL,
117                 "[{\"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\"}}}]");
118         assertEquals(3, client.getCapabilityStates().size());
119     }
120
121     @Test
122     public void testGetCapabilityStatesStateNULL() throws Exception {
123         mockRequest(CAPABILITY_STATES_URL,
124                 "[{\"id\":\"123\",\"state\":{\"isOpen\":{\"value\":false,\"lastChanged\":\"2022-03-12T20:54:50.6930000Z\"}}},{\"id\":\"456\",\"state\":[]},{\"id\":\"789\",\"state\":[]}]");
125         List<CapabilityStateDTO> capabilityStates = client.getCapabilityStates();
126         assertEquals(3, capabilityStates.size());
127     }
128
129     private void mockRequest(String url, String responseContent) throws Exception {
130         ContentResponse response = mock(ContentResponse.class);
131         when(response.getStatus()).thenReturn(HttpStatus.OK_200);
132         when(response.getContentAsString()).thenReturn(responseContent);
133
134         HttpURLConnection connectionMock = mock(HttpURLConnection.class);
135         when(connectionMock.getResponseCode()).thenReturn(HttpStatus.OK_200);
136         when(connectionMock.getInputStream())
137                 .thenReturn(new ByteArrayInputStream(responseContent.getBytes(StandardCharsets.UTF_8)));
138
139         when(connectionFactoryMock.createRequest(eq(url))).thenReturn(connectionMock);
140         when(connectionFactoryMock.createBaseRequest(eq(url), any(), any())).thenReturn(connectionMock);
141     }
142 }