2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2018 Nokia. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.onap.vid.aai.util;
23 import org.junit.Before;
24 import org.junit.Test;
25 import org.junit.runner.RunWith;
26 import org.mockito.Mock;
27 import org.mockito.Mockito;
28 import org.mockito.junit.MockitoJUnitRunner;
29 import org.onap.vid.aai.ExceptionWithRequestInfo;
30 import org.onap.vid.aai.exceptions.InvalidPropertyException;
31 import org.onap.vid.exceptions.GenericUncheckedException;
32 import org.onap.vid.utils.Unchecked;
33 import org.springframework.http.HttpMethod;
34 import org.testng.Assert;
36 import javax.servlet.http.HttpServletRequest;
37 import javax.ws.rs.client.Client;
38 import javax.ws.rs.client.Entity;
39 import javax.ws.rs.client.Invocation;
40 import javax.ws.rs.client.WebTarget;
41 import javax.ws.rs.core.MediaType;
42 import javax.ws.rs.core.Response;
43 import java.io.UnsupportedEncodingException;
45 import java.util.Optional;
46 import java.util.UUID;
48 import static javax.ws.rs.core.Response.Status.*;
49 import static junit.framework.TestCase.fail;
50 import static org.mockito.ArgumentMatchers.any;
51 import static org.mockito.ArgumentMatchers.eq;
52 import static org.mockito.Mockito.verify;
53 import static org.mockito.Mockito.when;
55 @RunWith(MockitoJUnitRunner.class)
56 public class SingleAAIRestInterfaceTest {
58 private static final String PATH = "path";
59 private static final String HTTP_LOCALHOST = "http://localhost/";
61 private Client client;
63 private WebTarget webTarget;
65 private Invocation.Builder builder;
67 private Invocation invocation;
69 private ServletRequestHelper servletRequestHelper;
71 private HttpsAuthClient httpsAuthClient;
73 private HttpServletRequest httpServletRequest;
75 private Response response;
77 private SystemPropertyHelper systemPropertyHelper;
79 private AAIRestInterface testSubject;
82 public void setUp() throws Exception {
83 mockSystemProperties();
84 testSubject = createTestSubject();
85 when(client.target(HTTP_LOCALHOST+PATH)).thenReturn(webTarget);
86 when(webTarget.request()).thenReturn(builder);
87 when(builder.accept(Mockito.anyString())).thenReturn(builder);
88 when(builder.header(Mockito.anyString(), Mockito.anyString())).thenReturn(builder);
89 when(builder.build(Mockito.anyString())).thenReturn(invocation);
90 when(builder.build(Mockito.anyString(), any(Entity.class))).thenReturn(invocation);
91 when(invocation.invoke()).thenReturn(response);
92 when(servletRequestHelper.extractOrGenerateRequestId()).thenReturn(UUID.randomUUID().toString());
95 private AAIRestInterface createTestSubject() {
96 return new AAIRestInterface(Optional.of(client), httpsAuthClient, servletRequestHelper, systemPropertyHelper);
100 public void testEncodeURL() throws Exception {
101 String nodeKey = "some unusual uri";
102 Assert.assertEquals(testSubject.encodeURL(nodeKey), "some%20unusual%20uri");
106 public void testSetRestSrvrBaseURLWithNullValue() {
107 testSubject.SetRestSrvrBaseURL(null);
111 public void testSetRestSrvrBaseURL() {
112 String baseUrl = "anything";
113 testSubject.SetRestSrvrBaseURL(baseUrl);
114 Assert.assertEquals(testSubject.getRestSrvrBaseURL(), baseUrl);
118 public void testRestJsonPutWithResponse200() {
120 String methodName = "RestPut";
121 String payload = "{\"id\": 1}";
122 Entity<String> entity = Entity.entity(payload, MediaType.APPLICATION_JSON);
125 when(response.getStatusInfo()).thenReturn(OK);
126 Response finalResponse = testSubject.RestPut("", PATH, payload, false, true).getResponse();
129 verify(builder).build(HttpMethod.PUT.name(), entity);
130 Assert.assertEquals(response, finalResponse);
133 @Test(expected = ExceptionWithRequestInfo.class)
134 public void testFailedRestJsonPut() {
136 String methodName = "RestPut";
137 String payload = "{\"id\": 1}";
138 Entity<String> entity = Entity.entity(payload, MediaType.APPLICATION_JSON);
141 when(builder.build(eq(HttpMethod.PUT.name()), any(Entity.class))).thenThrow(new GenericUncheckedException("msg"));
142 Response finalResponse = testSubject.RestPut("", PATH, payload, false, true).getResponse();
145 fail("expected unreachable: exception to be thrown");
149 public void testRestJsonPutWithResponse400() {
151 String methodName = "RestPut";
152 String payload = "{\"id\": 1}";
153 Entity<String> entity = Entity.entity(payload, MediaType.APPLICATION_JSON);
156 when(response.getStatusInfo()).thenReturn(BAD_REQUEST);
157 when(response.getStatus()).thenReturn(BAD_REQUEST.getStatusCode());
158 Response finalResponse = testSubject.RestPut("", PATH, payload, false, true).getResponse();
161 verify(builder).build(HttpMethod.PUT.name(), entity);
162 Assert.assertEquals(response, finalResponse);
166 public void testRestPostWithResponse200() {
168 String methodName = "RestPost";
169 String payload = "{\"id\": 1}";
170 Entity<String> entity = Entity.entity(payload, MediaType.APPLICATION_JSON);
173 when(builder.post(Mockito.any(Entity.class))).thenReturn(response);
174 when(response.getStatusInfo()).thenReturn(OK);
175 Response finalResponse = testSubject.RestPost("", PATH, payload, false);
178 verify(builder).post(entity);
179 Assert.assertEquals(response, finalResponse);
183 public void testRestPostWithResponse400() {
185 String methodName = "RestPost";
186 String payload = "{\"id\": 1}";
187 Entity<String> entity = Entity.entity(payload, MediaType.APPLICATION_JSON);
190 when(builder.post(Mockito.any(Entity.class))).thenReturn(response);
191 when(response.getStatusInfo()).thenReturn(BAD_REQUEST);
192 when(response.getStatus()).thenReturn(BAD_REQUEST.getStatusCode());
193 Response finalResponse = testSubject.RestPost("", PATH, payload, false);
196 verify(builder).post(entity);
197 Assert.assertEquals(response, finalResponse);
201 public void testFailedRestPost() {
203 String methodName = "RestPost";
204 String payload = "{\"id\": 1}";
205 Entity<String> entity = Entity.entity(payload, MediaType.APPLICATION_JSON);
208 when(builder.post(Mockito.any(Entity.class))).thenThrow(new RuntimeException());
209 Response finalResponse = testSubject.RestPost("", PATH, payload, false);
212 verify(builder).post(entity);
213 Assert.assertEquals(finalResponse, null);
217 public void testRestDeleteWithResponse400() {
219 String methodName = "Delete";
222 when(builder.delete()).thenReturn(response);
223 when(response.getStatusInfo()).thenReturn(BAD_REQUEST);
224 String reason = "Any reason";
225 when(response.readEntity(String.class)).thenReturn(reason);
226 when(response.getStatus()).thenReturn(BAD_REQUEST.getStatusCode());
227 boolean finalResponse = testSubject.Delete("", "", PATH);
230 verify(builder).delete();
231 Assert.assertFalse(finalResponse);
235 public void testRestDeleteWithResponse404() {
237 String methodName = "Delete";
240 when(builder.delete()).thenReturn(response);
241 when(response.getStatusInfo()).thenReturn(NOT_FOUND);
242 String reason = "Any reason";
243 when(response.readEntity(String.class)).thenReturn(reason);
244 when(response.getStatus()).thenReturn(NOT_FOUND.getStatusCode());
245 boolean finalResponse = testSubject.Delete("", "", PATH);
248 verify(builder).delete();
249 Assert.assertFalse(finalResponse);
253 public void testFailedRestDelete() {
255 String methodName = "Delete";
258 when(builder.delete()).thenThrow(new RuntimeException());
259 boolean finalResponse = testSubject.Delete("", "", PATH);
262 verify(builder).delete();
263 Assert.assertFalse(finalResponse);
267 public void testRestJsonGetWithResponse200() {
269 String methodName = "RestGet";
272 when(response.getStatusInfo()).thenReturn(OK);
273 Response finalResponse = testSubject.RestGet("", "", Unchecked.toURI(PATH), false).getResponse();
276 Assert.assertEquals(response, finalResponse);
280 public void testRestJsonGetWithResponse400() {
282 String methodName = "RestGet";
285 when(response.getStatusInfo()).thenReturn(BAD_REQUEST);
286 when(response.getStatus()).thenReturn(BAD_REQUEST.getStatusCode());
287 Response finalResponse = testSubject.RestGet("", "", Unchecked.toURI(PATH), false).getResponse();
290 Assert.assertEquals(response, finalResponse);
294 public void testFailedRestGet() {
296 String methodName = "RestGet";
299 when(builder.build(HttpMethod.GET.name())).thenThrow(new RuntimeException());
300 Response finalResponse = testSubject.RestGet("", "", Unchecked.toURI(PATH), false).getResponse();
303 Assert.assertEquals(finalResponse, null);
306 private void mockSystemProperties() throws UnsupportedEncodingException, InvalidPropertyException {
307 when(systemPropertyHelper.getEncodedCredentials()).thenReturn("someCredentials");
308 when(systemPropertyHelper.getFullServicePath(Mockito.anyString())).thenReturn("http://localhost/path");
309 when(systemPropertyHelper.getFullServicePath(Mockito.any(URI.class))).thenReturn("http://localhost/path");
310 when(systemPropertyHelper.getServiceBasePath(Mockito.anyString())).thenReturn("http://localhost/path");