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.innogysmarthome.internal.client;
15 import static org.junit.jupiter.api.Assertions.*;
16 import static org.mockito.Mockito.*;
18 import java.util.Arrays;
19 import java.util.Collections;
21 import org.eclipse.jetty.client.HttpClient;
22 import org.eclipse.jetty.client.api.ContentResponse;
23 import org.eclipse.jetty.client.api.Request;
24 import org.eclipse.jetty.http.HttpHeader;
25 import org.eclipse.jetty.http.HttpMethod;
26 import org.eclipse.jetty.http.HttpStatus;
27 import org.junit.jupiter.api.BeforeEach;
28 import org.junit.jupiter.api.Test;
29 import org.junit.jupiter.api.extension.ExtendWith;
30 import org.mockito.Mock;
31 import org.mockito.junit.jupiter.MockitoExtension;
32 import org.openhab.core.auth.client.oauth2.AccessTokenResponse;
33 import org.openhab.core.auth.client.oauth2.OAuthClientService;
36 * @author Sven Strohschein - Initial contribution
38 @ExtendWith(MockitoExtension.class)
39 public class InnogyClientTest {
41 private static final String DEVICES_URL = "https://api.services-smarthome.de/API/1.1/device";
43 private InnogyClient client;
45 private OAuthClientService oAuthClient;
47 private HttpClient httpClient;
50 public void before() throws Exception {
51 AccessTokenResponse accessTokenResponse = new AccessTokenResponse();
52 accessTokenResponse.setAccessToken("accessToken");
53 when(oAuthClient.getAccessTokenResponse()).thenReturn(accessTokenResponse);
55 client = new InnogyClient(oAuthClient, httpClient);
59 public void testGetDevices() throws Exception {
60 mockRequest(DEVICES_URL, "[ { id: 123 }, { id: 789, type: 'VariableActuator' } ]");
61 assertEquals(2, client.getDevices(Arrays.asList("123", "456")).size());
65 public void testGetDevicesNoDeviceIds() throws Exception {
66 mockRequest(DEVICES_URL, "[ { id: 123 } ]");
67 assertEquals(0, client.getDevices(Collections.emptyList()).size());
71 public void testGetDevicesFalseDeviceIds() throws Exception {
72 mockRequest(DEVICES_URL, "[ { id: 789 }]");
73 assertEquals(0, client.getDevices(Arrays.asList("123", "456")).size());
77 public void testGetDevicesNoDevicesNoDeviceIds() throws Exception {
78 mockRequest(DEVICES_URL, "[]");
79 assertEquals(0, client.getDevices(Collections.emptyList()).size());
83 public void testGetDevicesNoDevicesDeviceIds() throws Exception {
84 mockRequest(DEVICES_URL, "[]");
85 assertEquals(0, client.getDevices(Arrays.asList("123", "456")).size());
88 private void mockRequest(String url, String responseContent) throws Exception {
89 ContentResponse response = mock(ContentResponse.class);
90 when(response.getStatus()).thenReturn(HttpStatus.OK_200);
91 when(response.getContentAsString()).thenReturn(responseContent);
93 Request requestMock = mock(Request.class);
94 when(httpClient.newRequest(url)).thenReturn(requestMock);
95 when(requestMock.method(any(HttpMethod.class))).thenReturn(requestMock);
96 when(requestMock.header(any(HttpHeader.class), any())).thenReturn(requestMock);
97 when(requestMock.idleTimeout(anyLong(), any())).thenReturn(requestMock);
98 when(requestMock.timeout(anyLong(), any())).thenReturn(requestMock);
99 when(requestMock.send()).thenReturn(response);