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.exceptions.InvalidPropertyException;
30 import org.testng.Assert;
32 import javax.servlet.http.HttpServletRequest;
33 import javax.ws.rs.client.Client;
34 import javax.ws.rs.client.Entity;
35 import javax.ws.rs.client.Invocation;
36 import javax.ws.rs.client.WebTarget;
37 import javax.ws.rs.core.MediaType;
38 import javax.ws.rs.core.Response;
39 import java.io.UnsupportedEncodingException;
40 import java.util.Optional;
41 import java.util.UUID;
43 import static javax.ws.rs.core.Response.Status.*;
44 import static org.mockito.Mockito.verify;
45 import static org.mockito.Mockito.when;
47 @RunWith(MockitoJUnitRunner.class)
48 public class SingleAAIRestInterfaceTest {
50 private static final String PATH = "path";
51 private static final String HTTP_LOCALHOST = "http://localhost/";
53 private Client client;
55 private WebTarget webTarget;
57 private Invocation.Builder builder;
59 private ServletRequestHelper servletRequestHelper;
61 private HttpsAuthClient httpsAuthClient;
63 private HttpServletRequest httpServletRequest;
65 private Response response;
67 private SystemPropertyHelper systemPropertyHelper;
69 private AAIRestInterface testSubject;
72 public void setUp() throws Exception {
73 mockSystemProperties();
74 testSubject = createTestSubject();
75 when(client.target(HTTP_LOCALHOST+PATH)).thenReturn(webTarget);
76 when(webTarget.request()).thenReturn(builder);
77 when(builder.accept(Mockito.anyString())).thenReturn(builder);
78 when(builder.header(Mockito.anyString(), Mockito.anyString())).thenReturn(builder);
79 when(servletRequestHelper.extractOrGenerateRequestId()).thenReturn(UUID.randomUUID().toString());
82 private AAIRestInterface createTestSubject() {
83 return new AAIRestInterface(Optional.of(client), httpsAuthClient, servletRequestHelper, systemPropertyHelper);
87 public void testEncodeURL() throws Exception {
88 String nodeKey = "some unusual uri";
89 Assert.assertEquals(testSubject.encodeURL(nodeKey), "some%20unusual%20uri");
93 public void testSetRestSrvrBaseURLWithNullValue() throws Exception {
94 testSubject.SetRestSrvrBaseURL(null);
98 public void testSetRestSrvrBaseURL() throws Exception {
99 String baseUrl = "anything";
100 testSubject.SetRestSrvrBaseURL(baseUrl);
101 Assert.assertEquals(testSubject.getRestSrvrBaseURL(), baseUrl);
105 public void testRestJsonPutWithResponse200() throws Exception {
107 String methodName = "RestPut";
108 String payload = "{\"id\": 1}";
109 Entity<String> entity = Entity.entity(payload, MediaType.APPLICATION_JSON);
112 when(builder.put(Mockito.any(Entity.class))).thenReturn(response);
113 when(response.getStatusInfo()).thenReturn(OK);
114 Response finalResponse = testSubject.RestPut("", PATH, payload, false);
117 verify(builder).put(entity);
118 Assert.assertEquals(response, finalResponse);
122 public void testFailedRestJsonPut() throws Exception {
124 String methodName = "RestPut";
125 String payload = "{\"id\": 1}";
126 Entity<String> entity = Entity.entity(payload, MediaType.APPLICATION_JSON);
129 when(builder.put(Mockito.any(Entity.class))).thenThrow(new RuntimeException());
130 Response finalResponse = testSubject.RestPut("", PATH, payload, false);
133 verify(builder).put(entity);
134 Assert.assertEquals(finalResponse, null);
138 public void testRestJsonPutWithResponse400() throws Exception {
140 String methodName = "RestPut";
141 String payload = "{\"id\": 1}";
142 Entity<String> entity = Entity.entity(payload, MediaType.APPLICATION_JSON);
145 when(builder.put(Mockito.any(Entity.class))).thenReturn(response);
146 when(response.getStatusInfo()).thenReturn(BAD_REQUEST);
147 when(response.getStatus()).thenReturn(BAD_REQUEST.getStatusCode());
148 Response finalResponse = testSubject.RestPut("", PATH, payload, false);
151 verify(builder).put(entity);
152 Assert.assertEquals(response, finalResponse);
156 public void testRestPostWithResponse200() throws Exception {
158 String methodName = "RestPost";
159 String payload = "{\"id\": 1}";
160 Entity<String> entity = Entity.entity(payload, MediaType.APPLICATION_JSON);
163 when(builder.post(Mockito.any(Entity.class))).thenReturn(response);
164 when(response.getStatusInfo()).thenReturn(OK);
165 Response finalResponse = testSubject.RestPost("", PATH, payload, false);
168 verify(builder).post(entity);
169 Assert.assertEquals(response, finalResponse);
173 public void testRestPostWithResponse400() throws Exception {
175 String methodName = "RestPost";
176 String payload = "{\"id\": 1}";
177 Entity<String> entity = Entity.entity(payload, MediaType.APPLICATION_JSON);
180 when(builder.post(Mockito.any(Entity.class))).thenReturn(response);
181 when(response.getStatusInfo()).thenReturn(BAD_REQUEST);
182 when(response.getStatus()).thenReturn(BAD_REQUEST.getStatusCode());
183 Response finalResponse = testSubject.RestPost("", PATH, payload, false);
186 verify(builder).post(entity);
187 Assert.assertEquals(response, finalResponse);
191 public void testFailedRestPost() throws Exception {
193 String methodName = "RestPost";
194 String payload = "{\"id\": 1}";
195 Entity<String> entity = Entity.entity(payload, MediaType.APPLICATION_JSON);
198 when(builder.post(Mockito.any(Entity.class))).thenThrow(new RuntimeException());
199 Response finalResponse = testSubject.RestPost("", PATH, payload, false);
202 verify(builder).post(entity);
203 Assert.assertEquals(finalResponse, null);
207 public void testRestDeleteWithResponse400() throws Exception {
209 String methodName = "Delete";
212 when(builder.delete()).thenReturn(response);
213 when(response.getStatusInfo()).thenReturn(BAD_REQUEST);
214 String reason = "Any reason";
215 when(response.readEntity(String.class)).thenReturn(reason);
216 when(response.getStatus()).thenReturn(BAD_REQUEST.getStatusCode());
217 boolean finalResponse = testSubject.Delete("", "", PATH);
220 verify(builder).delete();
221 Assert.assertFalse(finalResponse);
225 public void testRestDeleteWithResponse404() throws Exception {
227 String methodName = "Delete";
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 testFailedRestDelete() throws Exception {
245 String methodName = "Delete";
248 when(builder.delete()).thenThrow(new RuntimeException());
249 boolean finalResponse = testSubject.Delete("", "", PATH);
252 verify(builder).delete();
253 Assert.assertFalse(finalResponse);
257 public void testRestJsonGetWithResponse200() throws Exception {
259 String methodName = "RestGet";
262 when(builder.get()).thenReturn(response);
263 when(response.getStatusInfo()).thenReturn(OK);
264 Response finalResponse = testSubject.RestGet("", "", PATH, false).getResponse();
267 Assert.assertEquals(response, finalResponse);
271 public void testRestJsonGetWithResponse400() throws Exception {
273 String methodName = "RestGet";
276 when(builder.get()).thenReturn(response);
277 when(response.getStatusInfo()).thenReturn(BAD_REQUEST);
278 when(response.getStatus()).thenReturn(BAD_REQUEST.getStatusCode());
279 Response finalResponse = testSubject.RestGet("", "", PATH, false).getResponse();
282 Assert.assertEquals(response, finalResponse);
286 public void testFailedRestGet() throws Exception {
288 String methodName = "RestGet";
291 when(builder.get()).thenThrow(new RuntimeException());
292 Response finalResponse = testSubject.RestGet("", "", PATH, false).getResponse();
295 Assert.assertEquals(finalResponse, null);
298 private void mockSystemProperties() throws UnsupportedEncodingException, InvalidPropertyException {
299 when(systemPropertyHelper.getAAIServerUrl()).thenReturn(Optional.of(HTTP_LOCALHOST));
300 when(systemPropertyHelper.getAAIUseClientCert()).thenReturn(Optional.of("cert"));
301 when(systemPropertyHelper.getAAIVIDPasswd()).thenReturn(Optional.of("passwd"));
302 when(systemPropertyHelper.getAAIVIDUsername()).thenReturn(Optional.of("user"));
303 when(systemPropertyHelper.getEncodedCredentials()).thenReturn("someCredentials");
304 when(systemPropertyHelper.getFullServicePath(Mockito.anyString())).thenReturn("http://localhost/path");
305 when(systemPropertyHelper.getServiceBasePath(Mockito.anyString())).thenReturn("http://localhost/path");