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.livisismarthome.internal.client;
15 import static org.junit.jupiter.api.Assertions.*;
16 import static org.mockito.Mockito.*;
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;
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;
36 * @author Sven Strohschein - Initial contribution
39 public class LivisiClientTest {
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";
45 private @NonNullByDefault({}) LivisiClient client;
46 private @NonNullByDefault({}) URLConnectionFactory connectionFactoryMock;
49 public void before() throws Exception {
50 AccessTokenResponse accessTokenResponse = new AccessTokenResponse();
51 accessTokenResponse.setAccessToken("accessToken");
53 OAuthClientService oAuthClientMock = mock(OAuthClientService.class);
54 when(oAuthClientMock.getAccessTokenResponse()).thenReturn(accessTokenResponse);
56 connectionFactoryMock = mock(URLConnectionFactory.class);
58 LivisiBridgeConfiguration bridgeConfiguration = new LivisiBridgeConfiguration();
59 bridgeConfiguration.host = "127.0.0.1";
60 client = new LivisiClient(bridgeConfiguration, oAuthClientMock, connectionFactoryMock);
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());
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"}]}}}\
81 assertEquals("1200", client.refreshStatus());
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());
91 public void testGetDevicesNoDeviceIds() throws Exception {
92 mockRequest(DEVICES_URL, "[ { id: 123 } ]");
93 assertEquals(0, client.getDevices(Collections.emptyList()).size());
97 public void testGetDevicesFalseDeviceIds() throws Exception {
98 mockRequest(DEVICES_URL, "[ { id: 789 }]");
99 assertEquals(0, client.getDevices(Arrays.asList("123", "456")).size());
103 public void testGetDevicesNoDevicesNoDeviceIds() throws Exception {
104 mockRequest(DEVICES_URL, "[]");
105 assertEquals(0, client.getDevices(Collections.emptyList()).size());
109 public void testGetDevicesNoDevicesDeviceIds() throws Exception {
110 mockRequest(DEVICES_URL, "[]");
111 assertEquals(0, client.getDevices(Arrays.asList("123", "456")).size());
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());
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());
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);
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)));
139 when(connectionFactoryMock.createRequest(eq(url))).thenReturn(connectionMock);
140 when(connectionFactoryMock.createBaseRequest(eq(url), any(), any())).thenReturn(connectionMock);