7fad9019ccd5cfb35c63bfba587ee1f7cb5eeefa
[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.exceptions.InvalidPropertyException;
30 import org.testng.Assert;
31
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;
42
43 import static javax.ws.rs.core.Response.Status.*;
44 import static org.mockito.Mockito.verify;
45 import static org.mockito.Mockito.when;
46
47 @RunWith(MockitoJUnitRunner.class)
48 public class SingleAAIRestInterfaceTest {
49
50     private static final String PATH = "path";
51     private static final String HTTP_LOCALHOST = "http://localhost/";
52     @Mock
53     private Client client;
54     @Mock
55     private WebTarget webTarget;
56     @Mock
57     private Invocation.Builder builder;
58     @Mock
59     private ServletRequestHelper servletRequestHelper;
60     @Mock
61     private HttpsAuthClient httpsAuthClient;
62     @Mock
63     private HttpServletRequest httpServletRequest;
64     @Mock
65     private Response response;
66     @Mock
67     private SystemPropertyHelper systemPropertyHelper;
68
69     private AAIRestInterface testSubject;
70
71     @Before
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());
80     }
81
82     private AAIRestInterface createTestSubject() {
83         return new AAIRestInterface(Optional.of(client), httpsAuthClient, servletRequestHelper, systemPropertyHelper);
84     }
85
86     @Test
87     public void testEncodeURL() throws Exception {
88         String nodeKey = "some unusual uri";
89         Assert.assertEquals(testSubject.encodeURL(nodeKey), "some%20unusual%20uri");
90     }
91
92     @Test
93     public void testSetRestSrvrBaseURLWithNullValue() throws Exception {
94         testSubject.SetRestSrvrBaseURL(null);
95     }
96
97     @Test
98     public void testSetRestSrvrBaseURL() throws Exception {
99         String baseUrl = "anything";
100         testSubject.SetRestSrvrBaseURL(baseUrl);
101         Assert.assertEquals(testSubject.getRestSrvrBaseURL(), baseUrl);
102     }
103
104     @Test
105     public void testRestJsonPutWithResponse200() throws Exception {
106         // given
107         String methodName = "RestPut";
108         String payload = "{\"id\": 1}";
109         Entity<String> entity = Entity.entity(payload, MediaType.APPLICATION_JSON);
110
111         // when
112         when(builder.put(Mockito.any(Entity.class))).thenReturn(response);
113         when(response.getStatusInfo()).thenReturn(OK);
114         Response finalResponse = testSubject.RestPut("", PATH, payload, false);
115
116         // then
117         verify(builder).put(entity);
118         Assert.assertEquals(response, finalResponse);
119     }
120
121     @Test
122     public void testFailedRestJsonPut() throws Exception {
123         // given
124         String methodName = "RestPut";
125         String payload = "{\"id\": 1}";
126         Entity<String> entity = Entity.entity(payload, MediaType.APPLICATION_JSON);
127
128         // when
129         when(builder.put(Mockito.any(Entity.class))).thenThrow(new RuntimeException());
130         Response finalResponse = testSubject.RestPut("", PATH, payload, false);
131
132         // then
133         verify(builder).put(entity);
134         Assert.assertEquals(finalResponse, null);
135     }
136
137     @Test
138     public void testRestJsonPutWithResponse400() throws Exception {
139         // given
140         String methodName = "RestPut";
141         String payload = "{\"id\": 1}";
142         Entity<String> entity = Entity.entity(payload, MediaType.APPLICATION_JSON);
143
144         // when
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);
149
150         // then
151         verify(builder).put(entity);
152         Assert.assertEquals(response, finalResponse);
153     }
154
155     @Test
156     public void testRestPostWithResponse200() throws Exception {
157         // given
158         String methodName = "RestPost";
159         String payload = "{\"id\": 1}";
160         Entity<String> entity = Entity.entity(payload, MediaType.APPLICATION_JSON);
161
162         // when
163         when(builder.post(Mockito.any(Entity.class))).thenReturn(response);
164         when(response.getStatusInfo()).thenReturn(OK);
165         Response finalResponse = testSubject.RestPost("", PATH, payload, false);
166
167         // then
168         verify(builder).post(entity);
169         Assert.assertEquals(response, finalResponse);
170     }
171
172     @Test
173     public void testRestPostWithResponse400() throws Exception {
174         // given
175         String methodName = "RestPost";
176         String payload = "{\"id\": 1}";
177         Entity<String> entity = Entity.entity(payload, MediaType.APPLICATION_JSON);
178
179         // when
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);
184
185         // then
186         verify(builder).post(entity);
187         Assert.assertEquals(response, finalResponse);
188     }
189
190     @Test
191     public void testFailedRestPost() throws Exception {
192         // given
193         String methodName = "RestPost";
194         String payload = "{\"id\": 1}";
195         Entity<String> entity = Entity.entity(payload, MediaType.APPLICATION_JSON);
196
197         // when
198         when(builder.post(Mockito.any(Entity.class))).thenThrow(new RuntimeException());
199         Response finalResponse = testSubject.RestPost("", PATH, payload, false);
200
201         // then
202         verify(builder).post(entity);
203         Assert.assertEquals(finalResponse, null);
204     }
205
206     @Test
207     public void testRestDeleteWithResponse400() throws Exception {
208         // given
209         String methodName = "Delete";
210
211         // when
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);
218
219         // then
220         verify(builder).delete();
221         Assert.assertFalse(finalResponse);
222     }
223
224     @Test
225     public void testRestDeleteWithResponse404() throws Exception {
226         // given
227         String methodName = "Delete";
228
229         // when
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);
236
237         // then
238         verify(builder).delete();
239         Assert.assertFalse(finalResponse);
240     }
241
242     @Test
243     public void testFailedRestDelete() throws Exception {
244         // given
245         String methodName = "Delete";
246
247         // when
248         when(builder.delete()).thenThrow(new RuntimeException());
249         boolean finalResponse = testSubject.Delete("", "", PATH);
250
251         // then
252         verify(builder).delete();
253         Assert.assertFalse(finalResponse);
254     }
255
256     @Test
257     public void testRestJsonGetWithResponse200() throws Exception {
258         // given
259         String methodName = "RestGet";
260
261         // when
262         when(builder.get()).thenReturn(response);
263         when(response.getStatusInfo()).thenReturn(OK);
264         Response finalResponse = testSubject.RestGet("", "", PATH, false).getResponse();
265
266         // then
267         Assert.assertEquals(response, finalResponse);
268     }
269
270     @Test
271     public void testRestJsonGetWithResponse400() throws Exception {
272         // given
273         String methodName = "RestGet";
274
275         // when
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();
280
281         // then
282         Assert.assertEquals(response, finalResponse);
283     }
284
285     @Test
286     public void testFailedRestGet() throws Exception {
287         // given
288         String methodName = "RestGet";
289
290         // when
291         when(builder.get()).thenThrow(new RuntimeException());
292         Response finalResponse = testSubject.RestGet("", "", PATH, false).getResponse();
293
294         // then
295         Assert.assertEquals(finalResponse, null);
296     }
297
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");
306     }
307
308 }