]> git.basschouten.com Git - openhab-addons.git/blob
3dd82dd1da7369516e43f5b97393020293e9489a
[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.innogysmarthome.internal.client;
14
15 import static org.junit.jupiter.api.Assertions.*;
16 import static org.mockito.Mockito.*;
17
18 import java.util.Arrays;
19 import java.util.Collections;
20
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;
34
35 /**
36  * @author Sven Strohschein - Initial contribution
37  */
38 @ExtendWith(MockitoExtension.class)
39 public class InnogyClientTest {
40
41     private static final String DEVICES_URL = "https://api.services-smarthome.de/API/1.1/device";
42
43     private InnogyClient client;
44     @Mock
45     private OAuthClientService oAuthClient;
46     @Mock
47     private HttpClient httpClient;
48
49     @BeforeEach
50     public void before() throws Exception {
51         AccessTokenResponse accessTokenResponse = new AccessTokenResponse();
52         accessTokenResponse.setAccessToken("accessToken");
53         when(oAuthClient.getAccessTokenResponse()).thenReturn(accessTokenResponse);
54
55         client = new InnogyClient(oAuthClient, httpClient);
56     }
57
58     @Test
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());
62     }
63
64     @Test
65     public void testGetDevicesNoDeviceIds() throws Exception {
66         mockRequest(DEVICES_URL, "[ { id: 123 } ]");
67         assertEquals(0, client.getDevices(Collections.emptyList()).size());
68     }
69
70     @Test
71     public void testGetDevicesFalseDeviceIds() throws Exception {
72         mockRequest(DEVICES_URL, "[ { id: 789 }]");
73         assertEquals(0, client.getDevices(Arrays.asList("123", "456")).size());
74     }
75
76     @Test
77     public void testGetDevicesNoDevicesNoDeviceIds() throws Exception {
78         mockRequest(DEVICES_URL, "[]");
79         assertEquals(0, client.getDevices(Collections.emptyList()).size());
80     }
81
82     @Test
83     public void testGetDevicesNoDevicesDeviceIds() throws Exception {
84         mockRequest(DEVICES_URL, "[]");
85         assertEquals(0, client.getDevices(Arrays.asList("123", "456")).size());
86     }
87
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);
92
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);
100     }
101 }