2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2018 - 2019 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;
24 import static javax.ws.rs.core.Response.Status.BAD_REQUEST;
25 import static javax.ws.rs.core.Response.Status.NOT_FOUND;
26 import static javax.ws.rs.core.Response.Status.OK;
27 import static org.mockito.ArgumentMatchers.any;
28 import static org.mockito.ArgumentMatchers.eq;
29 import static org.mockito.Mockito.verify;
30 import static org.mockito.Mockito.when;
31 import static org.mockito.MockitoAnnotations.initMocks;
33 import java.io.UnsupportedEncodingException;
35 import java.util.Optional;
36 import java.util.UUID;
37 import javax.servlet.http.HttpServletRequest;
38 import javax.ws.rs.client.Client;
39 import javax.ws.rs.client.Entity;
40 import javax.ws.rs.client.Invocation;
41 import javax.ws.rs.client.WebTarget;
42 import javax.ws.rs.core.MediaType;
43 import javax.ws.rs.core.Response;
44 import org.mockito.Mock;
45 import org.mockito.Mockito;
46 import org.onap.vid.aai.ExceptionWithRequestInfo;
47 import org.onap.vid.aai.exceptions.InvalidPropertyException;
48 import org.onap.vid.exceptions.GenericUncheckedException;
49 import org.onap.vid.utils.Logging;
50 import org.onap.vid.utils.Unchecked;
51 import org.springframework.http.HttpMethod;
52 import org.testng.Assert;
53 import org.testng.annotations.BeforeMethod;
54 import org.testng.annotations.Test;
57 public class AAIRestInterfaceTest {
59 private static final String PATH = "path";
60 private static final String HTTP_LOCALHOST = "http://localhost/";
62 private Client client;
64 private WebTarget webTarget;
66 private Invocation.Builder builder;
68 private Invocation invocation;
70 private ServletRequestHelper servletRequestHelper;
72 private HttpsAuthClient httpsAuthClient;
74 private HttpServletRequest httpServletRequest;
76 private Response response;
78 private SystemPropertyHelper systemPropertyHelper;
80 private Logging loggingService;
83 private AAIRestInterface testSubject;
86 public void setUp() throws Exception {
88 mockSystemProperties();
89 testSubject = createTestSubject();
90 when(client.target(HTTP_LOCALHOST+PATH)).thenReturn(webTarget);
91 when(webTarget.request()).thenReturn(builder);
92 when(builder.accept(Mockito.anyString())).thenReturn(builder);
93 when(builder.header(Mockito.anyString(), Mockito.anyString())).thenReturn(builder);
94 when(builder.build(Mockito.anyString())).thenReturn(invocation);
95 when(builder.build(Mockito.anyString(), any(Entity.class))).thenReturn(invocation);
96 when(invocation.invoke()).thenReturn(response);
97 when(servletRequestHelper.extractOrGenerateRequestId()).thenReturn(UUID.randomUUID().toString());
100 private AAIRestInterface createTestSubject() {
101 return new AAIRestInterface(Optional.of(client), httpsAuthClient, servletRequestHelper, systemPropertyHelper, loggingService);
105 public void shouldEncodeURL() throws Exception {
106 String nodeKey = "some unusual uri";
107 String nodeKey2 = "some/usual/uri";
108 Assert.assertEquals(testSubject.encodeURL(nodeKey), "some%20unusual%20uri");
109 Assert.assertEquals(testSubject.encodeURL(nodeKey2), "some%2Fusual%2Furi");
113 public void shouldSetRestSrvrBaseURL() {
114 String baseUrl = "anything";
115 testSubject.setRestSrvrBaseURL(baseUrl);
116 Assert.assertEquals(testSubject.getRestSrvrBaseURL(), baseUrl);
120 public void shouldExecuteRestJsonPutMethodWithResponse200() {
122 String payload = "{\"id\": 1}";
123 Entity<String> entity = Entity.entity(payload, MediaType.APPLICATION_JSON);
126 when(response.getStatusInfo()).thenReturn(OK);
127 Response finalResponse = testSubject.RestPut("", PATH, payload, false, true).getResponse();
130 verify(builder).build(HttpMethod.PUT.name(), entity);
131 Assert.assertEquals(response, finalResponse);
134 @Test(expectedExceptions = {ExceptionWithRequestInfo.class})
135 public void shouldFailWhenRestJsonPutMethodExecuted() {
137 String payload = "{\"id\": 1}";
140 when(builder.build(eq(HttpMethod.PUT.name()), any(Entity.class))).thenThrow(new GenericUncheckedException("msg"));
141 testSubject.RestPut("", PATH, payload, false, true);
147 public void shouldExecuteRestJsonPutMethodWithResponse400() {
149 String payload = "{\"id\": 1}";
150 Entity<String> entity = Entity.entity(payload, MediaType.APPLICATION_JSON);
153 when(response.getStatusInfo()).thenReturn(BAD_REQUEST);
154 when(response.getStatus()).thenReturn(BAD_REQUEST.getStatusCode());
155 Response finalResponse = testSubject.RestPut("", PATH, payload, false, true).getResponse();
158 verify(builder).build(HttpMethod.PUT.name(), entity);
159 Assert.assertEquals(response, finalResponse);
163 public void shouldExecuteRestPostMethodWithResponse200() {
165 String payload = "{\"id\": 1}";
166 Entity<String> entity = Entity.entity(payload, MediaType.APPLICATION_JSON);
169 when(builder.post(Mockito.any(Entity.class))).thenReturn(response);
170 when(response.getStatusInfo()).thenReturn(OK);
171 Response finalResponse = testSubject.RestPost("", PATH, payload, false);
174 verify(builder).post(entity);
175 Assert.assertEquals(response, finalResponse);
179 public void shouldExecuteRestPostMethodWithResponse400() {
181 String payload = "{\"id\": 1}";
182 Entity<String> entity = Entity.entity(payload, MediaType.APPLICATION_JSON);
185 when(builder.post(Mockito.any(Entity.class))).thenReturn(response);
186 when(response.getStatusInfo()).thenReturn(BAD_REQUEST);
187 when(response.getStatus()).thenReturn(BAD_REQUEST.getStatusCode());
188 Response finalResponse = testSubject.RestPost("", PATH, payload, false);
191 verify(builder).post(entity);
192 Assert.assertEquals(response, finalResponse);
196 public void shouldFailWhenRestPostMethodExecuted() {
198 String payload = "{\"id\": 1}";
199 Entity<String> entity = Entity.entity(payload, MediaType.APPLICATION_JSON);
202 when(builder.post(Mockito.any(Entity.class))).thenThrow(new RuntimeException());
203 Response finalResponse = testSubject.RestPost("", PATH, payload, false);
206 verify(builder).post(entity);
207 Assert.assertNull(finalResponse);
211 public void shouldExecuteRestDeleteMethodWithResponse400() {
214 when(builder.delete()).thenReturn(response);
215 when(response.getStatusInfo()).thenReturn(BAD_REQUEST);
216 String reason = "Any reason";
217 when(response.readEntity(String.class)).thenReturn(reason);
218 when(response.getStatus()).thenReturn(BAD_REQUEST.getStatusCode());
219 boolean finalResponse = testSubject.Delete("", "", PATH);
222 verify(builder).delete();
223 Assert.assertFalse(finalResponse);
227 public void shouldExecuteRestDeleteMethodWithResponse404() {
230 when(builder.delete()).thenReturn(response);
231 when(response.getStatusInfo()).thenReturn(NOT_FOUND);
232 String reason = "Any reason";
233 when(response.readEntity(String.class)).thenReturn(reason);
234 when(response.getStatus()).thenReturn(NOT_FOUND.getStatusCode());
235 boolean finalResponse = testSubject.Delete("", "", PATH);
238 verify(builder).delete();
239 Assert.assertFalse(finalResponse);
243 public void shouldFailWhenRestDeleteExecuted() {
246 when(builder.delete()).thenThrow(new RuntimeException());
247 boolean finalResponse = testSubject.Delete("", "", PATH);
249 verify(builder).delete();
250 Assert.assertFalse(finalResponse);
254 public void shouldExecuteRestGetMethodWithResponse200() {
257 when(response.getStatusInfo()).thenReturn(OK);
258 Response finalResponse = testSubject.RestGet("", "", Unchecked.toURI(PATH), false).getResponse();
261 Assert.assertEquals(response, finalResponse);
265 public void shouldExecuteRestGetMethodWithResponse400() {
268 when(response.getStatusInfo()).thenReturn(BAD_REQUEST);
269 when(response.getStatus()).thenReturn(BAD_REQUEST.getStatusCode());
270 Response finalResponse = testSubject.RestGet("", "", Unchecked.toURI(PATH), false).getResponse();
273 Assert.assertEquals(response, finalResponse);
277 public void shouldFailWhenRestGetMethodExecuted() {
280 when(builder.build(HttpMethod.GET.name())).thenThrow(new RuntimeException());
281 Response finalResponse = testSubject.RestGet("", "", Unchecked.toURI(PATH), false).getResponse();
284 Assert.assertNull(finalResponse);
287 private void mockSystemProperties() throws UnsupportedEncodingException, InvalidPropertyException {
288 when(systemPropertyHelper.getEncodedCredentials()).thenReturn("someCredentials");
289 when(systemPropertyHelper.getFullServicePath(Mockito.anyString())).thenReturn("http://localhost/path");
290 when(systemPropertyHelper.getFullServicePath(Mockito.any(URI.class))).thenReturn("http://localhost/path");
291 when(systemPropertyHelper.getServiceBasePath(Mockito.anyString())).thenReturn("http://localhost/path");