Merge from ECOMP's repository
[vid.git] / vid-app-common / src / test / java / org / onap / vid / aai / util / SingleAAIRestInterfaceTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * VID
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 package org.onap.vid.aai.util;
22
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;
35
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;
44 import java.net.URI;
45 import java.util.Optional;
46 import java.util.UUID;
47
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;
54
55 @RunWith(MockitoJUnitRunner.class)
56 public class SingleAAIRestInterfaceTest {
57
58     private static final String PATH = "path";
59     private static final String HTTP_LOCALHOST = "http://localhost/";
60     @Mock
61     private Client client;
62     @Mock
63     private WebTarget webTarget;
64     @Mock
65     private Invocation.Builder builder;
66     @Mock
67     private Invocation invocation;
68     @Mock
69     private ServletRequestHelper servletRequestHelper;
70     @Mock
71     private HttpsAuthClient httpsAuthClient;
72     @Mock
73     private HttpServletRequest httpServletRequest;
74     @Mock
75     private Response response;
76     @Mock
77     private SystemPropertyHelper systemPropertyHelper;
78
79     private AAIRestInterface testSubject;
80
81     @Before
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());
93     }
94
95     private AAIRestInterface createTestSubject() {
96         return new AAIRestInterface(Optional.of(client), httpsAuthClient, servletRequestHelper, systemPropertyHelper);
97     }
98
99     @Test
100     public void testEncodeURL() throws Exception {
101         String nodeKey = "some unusual uri";
102         Assert.assertEquals(testSubject.encodeURL(nodeKey), "some%20unusual%20uri");
103     }
104
105     @Test
106     public void testSetRestSrvrBaseURLWithNullValue() {
107         testSubject.SetRestSrvrBaseURL(null);
108     }
109
110     @Test
111     public void testSetRestSrvrBaseURL() {
112         String baseUrl = "anything";
113         testSubject.SetRestSrvrBaseURL(baseUrl);
114         Assert.assertEquals(testSubject.getRestSrvrBaseURL(), baseUrl);
115     }
116
117     @Test
118     public void testRestJsonPutWithResponse200() {
119         // given
120         String methodName = "RestPut";
121         String payload = "{\"id\": 1}";
122         Entity<String> entity = Entity.entity(payload, MediaType.APPLICATION_JSON);
123
124         // when
125         when(response.getStatusInfo()).thenReturn(OK);
126         Response finalResponse = testSubject.RestPut("", PATH, payload, false, true).getResponse();
127
128         // then
129         verify(builder).build(HttpMethod.PUT.name(), entity);
130         Assert.assertEquals(response, finalResponse);
131     }
132
133     @Test(expected = ExceptionWithRequestInfo.class)
134     public void testFailedRestJsonPut() {
135         // given
136         String methodName = "RestPut";
137         String payload = "{\"id\": 1}";
138         Entity<String> entity = Entity.entity(payload, MediaType.APPLICATION_JSON);
139
140         // when
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();
143
144         // then
145         fail("expected unreachable: exception to be thrown");
146     }
147
148     @Test
149     public void testRestJsonPutWithResponse400() {
150         // given
151         String methodName = "RestPut";
152         String payload = "{\"id\": 1}";
153         Entity<String> entity = Entity.entity(payload, MediaType.APPLICATION_JSON);
154
155         // when
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();
159
160         // then
161         verify(builder).build(HttpMethod.PUT.name(), entity);
162         Assert.assertEquals(response, finalResponse);
163     }
164
165     @Test
166     public void testRestPostWithResponse200() {
167         // given
168         String methodName = "RestPost";
169         String payload = "{\"id\": 1}";
170         Entity<String> entity = Entity.entity(payload, MediaType.APPLICATION_JSON);
171
172         // when
173         when(builder.post(Mockito.any(Entity.class))).thenReturn(response);
174         when(response.getStatusInfo()).thenReturn(OK);
175         Response finalResponse = testSubject.RestPost("", PATH, payload, false);
176
177         // then
178         verify(builder).post(entity);
179         Assert.assertEquals(response, finalResponse);
180     }
181
182     @Test
183     public void testRestPostWithResponse400() {
184         // given
185         String methodName = "RestPost";
186         String payload = "{\"id\": 1}";
187         Entity<String> entity = Entity.entity(payload, MediaType.APPLICATION_JSON);
188
189         // when
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);
194
195         // then
196         verify(builder).post(entity);
197         Assert.assertEquals(response, finalResponse);
198     }
199
200     @Test
201     public void testFailedRestPost() {
202         // given
203         String methodName = "RestPost";
204         String payload = "{\"id\": 1}";
205         Entity<String> entity = Entity.entity(payload, MediaType.APPLICATION_JSON);
206
207         // when
208         when(builder.post(Mockito.any(Entity.class))).thenThrow(new RuntimeException());
209         Response finalResponse = testSubject.RestPost("", PATH, payload, false);
210
211         // then
212         verify(builder).post(entity);
213         Assert.assertEquals(finalResponse, null);
214     }
215
216     @Test
217     public void testRestDeleteWithResponse400() {
218         // given
219         String methodName = "Delete";
220
221         // when
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);
228
229         // then
230         verify(builder).delete();
231         Assert.assertFalse(finalResponse);
232     }
233
234     @Test
235     public void testRestDeleteWithResponse404() {
236         // given
237         String methodName = "Delete";
238
239         // when
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);
246
247         // then
248         verify(builder).delete();
249         Assert.assertFalse(finalResponse);
250     }
251
252     @Test
253     public void testFailedRestDelete() {
254         // given
255         String methodName = "Delete";
256
257         // when
258         when(builder.delete()).thenThrow(new RuntimeException());
259         boolean finalResponse = testSubject.Delete("", "", PATH);
260
261         // then
262         verify(builder).delete();
263         Assert.assertFalse(finalResponse);
264     }
265
266     @Test
267     public void testRestJsonGetWithResponse200() {
268         // given
269         String methodName = "RestGet";
270
271         // when
272         when(response.getStatusInfo()).thenReturn(OK);
273         Response finalResponse = testSubject.RestGet("", "", Unchecked.toURI(PATH), false).getResponse();
274
275         // then
276         Assert.assertEquals(response, finalResponse);
277     }
278
279     @Test
280     public void testRestJsonGetWithResponse400() {
281         // given
282         String methodName = "RestGet";
283
284         // when
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();
288
289         // then
290         Assert.assertEquals(response, finalResponse);
291     }
292
293     @Test
294     public void testFailedRestGet() {
295         // given
296         String methodName = "RestGet";
297
298         // when
299         when(builder.build(HttpMethod.GET.name())).thenThrow(new RuntimeException());
300         Response finalResponse = testSubject.RestGet("", "", Unchecked.toURI(PATH), false).getResponse();
301
302         // then
303         Assert.assertEquals(finalResponse, null);
304     }
305
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");
311     }
312
313 }