make Logging a service and inject it to SyncRestClient
[vid.git] / vid-app-common / src / test / java / org / onap / vid / aai / util / AAIRestInterfaceTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * VID
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
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
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;
32
33 import java.io.UnsupportedEncodingException;
34 import java.net.URI;
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;
55
56
57 public class AAIRestInterfaceTest {
58
59     private static final String PATH = "path";
60     private static final String HTTP_LOCALHOST = "http://localhost/";
61     @Mock
62     private Client client;
63     @Mock
64     private WebTarget webTarget;
65     @Mock
66     private Invocation.Builder builder;
67     @Mock
68     private Invocation invocation;
69     @Mock
70     private ServletRequestHelper servletRequestHelper;
71     @Mock
72     private HttpsAuthClient httpsAuthClient;
73     @Mock
74     private HttpServletRequest httpServletRequest;
75     @Mock
76     private Response response;
77     @Mock
78     private SystemPropertyHelper systemPropertyHelper;
79     @Mock
80     private Logging loggingService;
81
82
83     private AAIRestInterface testSubject;
84
85     @BeforeMethod
86     public void setUp() throws Exception {
87         initMocks(this);
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());
98     }
99
100     private AAIRestInterface createTestSubject() {
101         return new AAIRestInterface(Optional.of(client), httpsAuthClient, servletRequestHelper, systemPropertyHelper, loggingService);
102     }
103
104     @Test
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");
110     }
111
112     @Test
113     public void shouldSetRestSrvrBaseURL() {
114         String baseUrl = "anything";
115         testSubject.setRestSrvrBaseURL(baseUrl);
116         Assert.assertEquals(testSubject.getRestSrvrBaseURL(), baseUrl);
117     }
118
119     @Test
120     public void shouldExecuteRestJsonPutMethodWithResponse200() {
121         // given
122         String payload = "{\"id\": 1}";
123         Entity<String> entity = Entity.entity(payload, MediaType.APPLICATION_JSON);
124
125         // when
126         when(response.getStatusInfo()).thenReturn(OK);
127         Response finalResponse = testSubject.RestPut("", PATH, payload, false, true).getResponse();
128
129         // then
130         verify(builder).build(HttpMethod.PUT.name(), entity);
131         Assert.assertEquals(response, finalResponse);
132     }
133
134     @Test(expectedExceptions = {ExceptionWithRequestInfo.class})
135     public void shouldFailWhenRestJsonPutMethodExecuted() {
136         // given
137         String payload = "{\"id\": 1}";
138
139         // when
140         when(builder.build(eq(HttpMethod.PUT.name()), any(Entity.class))).thenThrow(new GenericUncheckedException("msg"));
141         testSubject.RestPut("", PATH, payload, false, true);
142
143         //then
144     }
145
146     @Test
147     public void shouldExecuteRestJsonPutMethodWithResponse400() {
148         // given
149         String payload = "{\"id\": 1}";
150         Entity<String> entity = Entity.entity(payload, MediaType.APPLICATION_JSON);
151
152         // when
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();
156
157         // then
158         verify(builder).build(HttpMethod.PUT.name(), entity);
159         Assert.assertEquals(response, finalResponse);
160     }
161
162     @Test
163     public void shouldExecuteRestPostMethodWithResponse200() {
164         // given
165         String payload = "{\"id\": 1}";
166         Entity<String> entity = Entity.entity(payload, MediaType.APPLICATION_JSON);
167
168         // when
169         when(builder.post(Mockito.any(Entity.class))).thenReturn(response);
170         when(response.getStatusInfo()).thenReturn(OK);
171         Response finalResponse = testSubject.RestPost("", PATH, payload, false);
172
173         // then
174         verify(builder).post(entity);
175         Assert.assertEquals(response, finalResponse);
176     }
177
178     @Test
179     public void shouldExecuteRestPostMethodWithResponse400() {
180         // given
181         String payload = "{\"id\": 1}";
182         Entity<String> entity = Entity.entity(payload, MediaType.APPLICATION_JSON);
183
184         // when
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);
189
190         // then
191         verify(builder).post(entity);
192         Assert.assertEquals(response, finalResponse);
193     }
194
195     @Test
196     public void shouldFailWhenRestPostMethodExecuted() {
197         // given
198         String payload = "{\"id\": 1}";
199         Entity<String> entity = Entity.entity(payload, MediaType.APPLICATION_JSON);
200
201         // when
202         when(builder.post(Mockito.any(Entity.class))).thenThrow(new RuntimeException());
203         Response finalResponse = testSubject.RestPost("", PATH, payload, false);
204
205         // then
206         verify(builder).post(entity);
207         Assert.assertNull(finalResponse);
208     }
209
210     @Test
211     public void shouldExecuteRestDeleteMethodWithResponse400() {
212         // given
213         // when
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);
220
221         // then
222         verify(builder).delete();
223         Assert.assertFalse(finalResponse);
224     }
225
226     @Test
227     public void shouldExecuteRestDeleteMethodWithResponse404() {
228         // given
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 shouldFailWhenRestDeleteExecuted() {
244         // given
245         // when
246         when(builder.delete()).thenThrow(new RuntimeException());
247         boolean finalResponse = testSubject.Delete("", "", PATH);
248         // then
249         verify(builder).delete();
250         Assert.assertFalse(finalResponse);
251     }
252
253     @Test
254     public void shouldExecuteRestGetMethodWithResponse200() {
255         // given
256         // when
257         when(response.getStatusInfo()).thenReturn(OK);
258         Response finalResponse = testSubject.RestGet("", "", Unchecked.toURI(PATH), false).getResponse();
259
260         // then
261         Assert.assertEquals(response, finalResponse);
262     }
263
264     @Test
265     public void shouldExecuteRestGetMethodWithResponse400() {
266         // given
267         // when
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();
271
272         // then
273         Assert.assertEquals(response, finalResponse);
274     }
275
276     @Test
277     public void shouldFailWhenRestGetMethodExecuted() {
278         // given
279         // when
280         when(builder.build(HttpMethod.GET.name())).thenThrow(new RuntimeException());
281         Response finalResponse = testSubject.RestGet("", "", Unchecked.toURI(PATH), false).getResponse();
282
283         // then
284         Assert.assertNull(finalResponse);
285     }
286
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");
292     }
293
294 }