201ec0981d131679448f67436bc918cbbc02a1a8
[vid.git] / vid-app-common / src / test / java / org / onap / vid / aai / AaiClientTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * VID
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * Modifications Copyright (C) 2018 Nokia. All rights reserved.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.vid.aai;
23
24 import com.fasterxml.jackson.databind.ObjectMapper;
25 import com.google.common.collect.ImmutableList;
26 import org.apache.commons.lang3.builder.ReflectionToStringBuilder;
27 import org.apache.commons.lang3.builder.ToStringStyle;
28 import org.apache.commons.lang3.exception.ExceptionUtils;
29 import org.apache.commons.lang3.reflect.FieldUtils;
30 import org.apache.commons.lang3.tuple.Pair;
31 import org.mockito.Mockito;
32 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
33 import org.onap.portalsdk.core.util.SystemProperties;
34 import org.onap.vid.aai.model.AaiGetTenatns.GetTenantsResponse;
35 import org.onap.vid.aai.model.AaiNodeQueryResponse;
36 import org.onap.vid.aai.model.PortDetailsTranslator;
37 import org.onap.vid.aai.model.ResourceType;
38 import org.onap.vid.aai.util.AAIRestInterface;
39 import org.onap.vid.aai.util.HttpsAuthClient;
40 import org.onap.vid.aai.util.ServletRequestHelper;
41 import org.onap.vid.aai.util.SystemPropertyHelper;
42 import org.onap.vid.controllers.LocalWebConfig;
43 import org.onap.vid.exceptions.GenericUncheckedException;
44 import org.onap.vid.model.Subscriber;
45 import org.onap.vid.model.SubscriberList;
46 import org.onap.vid.model.probes.ExternalComponentStatus;
47 import org.onap.vid.model.probes.HttpRequestMetadata;
48 import org.onap.vid.model.probes.StatusMetadata;
49 import org.onap.vid.testUtils.TestUtils;
50 import org.springframework.http.HttpMethod;
51 import org.springframework.test.context.ContextConfiguration;
52 import org.springframework.test.context.web.WebAppConfiguration;
53 import org.testng.Assert;
54 import org.testng.annotations.BeforeMethod;
55 import org.testng.annotations.DataProvider;
56 import org.testng.annotations.Test;
57 import sun.security.provider.certpath.SunCertPathBuilderException;
58 import sun.security.validator.ValidatorException;
59
60 import javax.crypto.BadPaddingException;
61 import javax.net.ssl.SSLHandshakeException;
62 import javax.servlet.ServletContext;
63 import javax.ws.rs.ProcessingException;
64 import javax.ws.rs.client.Client;
65 import javax.ws.rs.core.Response;
66 import java.io.FileNotFoundException;
67 import java.io.IOException;
68 import java.security.cert.CertificateException;
69 import java.util.ArrayList;
70 import java.util.function.BiConsumer;
71 import java.util.function.Function;
72 import java.util.stream.Collectors;
73 import java.util.stream.Stream;
74
75 import static org.apache.commons.lang3.StringUtils.equalsIgnoreCase;
76 import static org.hamcrest.CoreMatchers.*;
77 import static org.hamcrest.MatcherAssert.assertThat;
78 import static org.mockito.ArgumentMatchers.any;
79 import static org.mockito.ArgumentMatchers.*;
80 import static org.mockito.Mockito.*;
81 import static org.testng.Assert.*;
82
83 @ContextConfiguration(classes = {LocalWebConfig.class, SystemProperties.class})
84 @WebAppConfiguration
85 public class AaiClientTest {
86
87     private AaiClient aaiClientMock;
88     private ServletContext servletContext;
89
90     @BeforeMethod
91     public void initMocks() {
92         aaiClientMock = mock(AaiClient.class);
93         aaiClientMock.logger = mock(EELFLoggerDelegate.class);
94         servletContext = mock(ServletContext.class);
95
96         when(servletContext.getRealPath(any(String.class))).thenReturn("");
97
98         when(aaiClientMock.doAaiGet(any(String.class), any(Boolean.class))).thenReturn(null);
99     }
100
101     @DataProvider
102     public static Object[][] logicalLinkData() {
103         return new Object[][]{
104                 {"", "network/logical-links/logical-link/"},
105                 {"link", "network/logical-links/logical-link/link"}
106         };
107     }
108
109     @Test(dataProvider = "logicalLinkData")
110     public void getLogicalLink_Link_Is_Empty(String link, String expectedUrl) {
111
112         when(aaiClientMock.getLogicalLink(any(String.class))).thenCallRealMethod();
113         aaiClientMock.getLogicalLink(link);
114         Mockito.verify(aaiClientMock).doAaiGet(argThat(s -> equalsIgnoreCase(s, expectedUrl)), any(Boolean.class));
115     }
116
117     @DataProvider
118     public static Object[][] subscribersResults() {
119         return new Object[][]{
120                 {new SubscriberList(new ArrayList<Subscriber>() {{
121                     add(new Subscriber());
122                     add(new Subscriber());
123                 }}), true},
124                 {new SubscriberList(new ArrayList<Subscriber>() {{
125                     add(new Subscriber());
126                 }}), true},
127                 {new SubscriberList(new ArrayList<Subscriber>()), false}
128         };
129     }
130
131     @Test(dataProvider = "subscribersResults")
132     public void testProbeAaiGetAllSubscribers_returnsTwoToZeroSubscribers_ResultsAsExpected(SubscriberList subscribers, boolean isAvailable) {
133         ExternalComponentStatus expectedStatus = new ExternalComponentStatus(ExternalComponentStatus.Component.AAI, isAvailable, new HttpRequestMetadata(
134                 HttpMethod.GET,
135                 200,
136                 "url",
137                 "rawData",
138                 isAvailable ? "OK" : "No subscriber received",
139                 0
140         ));
141         Mockito.when(aaiClientMock.getAllSubscribers(true)).thenReturn(
142                 new AaiResponseWithRequestInfo<>(
143                         HttpMethod.GET, "url", new AaiResponse<>(subscribers, null, 200),
144                         "rawData"));
145         Mockito.when(aaiClientMock.probeAaiGetAllSubscribers()).thenCallRealMethod();
146         ExternalComponentStatus result = aaiClientMock.probeAaiGetAllSubscribers();
147         assertThat(statusDataReflected(result), is(statusDataReflected(expectedStatus)));
148         assertThat(requestMetadataReflected(result.getMetadata()), is(requestMetadataReflected(expectedStatus.getMetadata())));
149     }
150
151     //serialize fields except of fields we cannot know ahead of time
152     private static String requestMetadataReflected(StatusMetadata metadata) {
153         return new ReflectionToStringBuilder(metadata, ToStringStyle.SHORT_PREFIX_STYLE)
154                 .setExcludeFieldNames("duration")
155                 .toString();
156     }
157
158     private static String statusDataReflected(ExternalComponentStatus status) {
159         return new ReflectionToStringBuilder(status, ToStringStyle.SHORT_PREFIX_STYLE)
160                 .setExcludeFieldNames("metadata")
161                 .toString();
162     }
163
164     @DataProvider
165     public static Object[][] rawData() {
166         return new Object[][]{
167                 {"errorMessage",}, {""}, {null}
168         };
169     }
170
171     @Test(dataProvider = "rawData")
172     public void testProbeAaiGetFullSubscribersWithNullResponse_returnsNotAvailableWithErrorRawData(String rawData) {
173         Mockito.when(aaiClientMock.getAllSubscribers(true)).thenReturn(
174                 new AaiResponseWithRequestInfo<>(HttpMethod.GET, "url", null,
175                         rawData));
176         ExternalComponentStatus result = callProbeAaiGetAllSubscribersAndAssertNotAvailable();
177         assertThat(result.getMetadata(), instanceOf(HttpRequestMetadata.class));
178         assertEquals(((HttpRequestMetadata) result.getMetadata()).getRawData(), rawData);
179     }
180
181     @DataProvider
182     public static Object[][] exceptions() {
183         return new Object[][]{
184                 {"NullPointerException", "errorMessage",
185                         new ExceptionWithRequestInfo(HttpMethod.GET, "url",
186                                 "errorMessage", null, new NullPointerException())},
187                 {"RuntimeException", null,
188                         new ExceptionWithRequestInfo(HttpMethod.GET, "url",
189                                 null, null, new RuntimeException())},
190                 {"RuntimeException", null,
191                         new RuntimeException()},
192         };
193     }
194
195     @Test(dataProvider = "exceptions")
196     public void testProbeAaiGetFullSubscribersWithNullResponse_returnsNotAvailableWithErrorRawData(String description, String expectedRawData, Exception exception) {
197         Mockito.when(aaiClientMock.getAllSubscribers(true)).thenThrow(exception);
198         ExternalComponentStatus result = callProbeAaiGetAllSubscribersAndAssertNotAvailable();
199         if (exception instanceof ExceptionWithRequestInfo) {
200             assertThat(result.getMetadata(), instanceOf(HttpRequestMetadata.class));
201             assertEquals(((HttpRequestMetadata) result.getMetadata()).getRawData(), expectedRawData);
202         }
203         assertThat(result.getMetadata().getDescription(), containsString(description));
204     }
205
206     private ExternalComponentStatus callProbeAaiGetAllSubscribersAndAssertNotAvailable() {
207         Mockito.when(aaiClientMock.probeAaiGetAllSubscribers()).thenCallRealMethod();
208         ExternalComponentStatus result = aaiClientMock.probeAaiGetAllSubscribers();
209         assertFalse(result.isAvailable());
210         return result;
211     }
212
213
214     @Test
215     public void getTenants_Arguments_Are_Null_Or_Empty() {
216
217         when(aaiClientMock.getTenants(any(), any())).thenCallRealMethod();
218
219         AaiResponse response = aaiClientMock.getTenants("", "");
220
221         assertEquals(response.getErrorMessage(), "{\"statusText\":\" Failed to retrieve LCP Region & Tenants from A&AI, Subscriber ID or Service Type is missing.\"}");
222
223
224         response = aaiClientMock.getTenants(null, null);
225
226         assertEquals(response.getErrorMessage(), "{\"statusText\":\" Failed to retrieve LCP Region & Tenants from A&AI, Subscriber ID or Service Type is missing.\"}");
227     }
228
229     @Test
230     public void getTenants_Arguments_Are_Valid_But_Tenants_Not_Exist() {
231
232         when(aaiClientMock.getTenants(any(String.class), any(String.class))).thenCallRealMethod();
233
234         Response generalEmptyResponse = mock(Response.class);
235         when(aaiClientMock.doAaiGet(any(String.class), any(Boolean.class))).thenReturn(generalEmptyResponse);
236
237         AaiResponse response = aaiClientMock.getTenants("subscriberId", "serviceType");
238
239         assertEquals(response.getErrorMessage(), "{\"statusText\":\" A&AI has no LCP Region & Tenants associated to subscriber 'subscriberId' and service type 'serviceType'\"}");
240
241     }
242
243     @Test
244     public void getTenants_Arguments_Are_Valid_Get_The_Tenanats() {
245
246         when(aaiClientMock.getTenants(any(String.class), any(String.class))).thenCallRealMethod();
247
248
249         Response generalEmptyResponse = mock(Response.class);
250
251         when(generalEmptyResponse.readEntity(String.class)).thenReturn(tenantResponseRaw);
252         when(generalEmptyResponse.getStatus()).thenReturn(200);
253         when(generalEmptyResponse.getStatusInfo()).thenReturn(new Response.StatusType() {
254             @Override
255             public int getStatusCode() {
256                 return 200;
257             }
258
259             @Override
260             public Response.Status.Family getFamily() {
261                 return Response.Status.Family.SUCCESSFUL;
262             }
263
264             @Override
265             public String getReasonPhrase() {
266                 return null;
267             }
268         });
269
270
271         when(aaiClientMock.doAaiGet(any(String.class), any(Boolean.class))).thenReturn(generalEmptyResponse);
272
273         AaiResponse<GetTenantsResponse[]> response = aaiClientMock.getTenants("subscriberId", "serviceType");
274
275         Assert.assertTrue(response.t.length > 0);
276     }
277
278     final String tenantResponseRaw = "" +
279             "{" +
280             "\"service-type\": \"VIRTUAL USP\"," +
281             "\"resource-version\": \"1494001841964\"," +
282             "\"relationship-list\": {" +
283             "\"relationship\": [{" +
284             "\"related-to\": \"tenant\"," +
285             "\"related-link\": \"/aai/v11/cloud-infrastructure/cloud-regions/cloud-region/att-aic/AAIAIC25/tenants/tenant/092eb9e8e4b7412e8787dd091bc58e86\"," +
286             "\"relationship-data\": [{" +
287             "\"relationship-key\": \"cloud-region.cloud-owner\"," +
288             "\"relationship-value\": \"att-aic\"" +
289             "}," +
290             "{" +
291             "\"relationship-key\": \"cloud-region.cloud-region-id\"," +
292             "\"relationship-value\": \"AAIAIC25\"" +
293             "}," +
294             "{" +
295             "\"relationship-key\": \"tenant.tenant-id\"," +
296             "\"relationship-value\": \"092eb9e8e4b7412e8787dd091bc58e86\"" +
297             "}" +
298             "]," +
299             "\"related-to-property\": [{" +
300             "\"property-key\": \"tenant.tenant-name\"," +
301             "\"property-value\": \"USP-SIP-IC-24335-T-01\"" +
302             "}]" +
303             "}]" +
304             "}" +
305             "}";
306
307     @DataProvider
308     public static Object[][] resourceTypesProvider() {
309         return new Object[][]{
310                 {"service-instance", ResourceType.SERVICE_INSTANCE},
311                 {"generic-vnf", ResourceType.GENERIC_VNF},
312                 {"vf-module", ResourceType.VF_MODULE}
313         };
314     }
315
316     @Test(dataProvider = "resourceTypesProvider")
317     public void aaiNodeQueryResponseDeserializationTest(String resourceType, ResourceType expectedResourceType) throws IOException {
318         String link = "/aai/v12/business/customers/customer/a9a77d5a-123e-4ca2-9eb9-0b015d2ee0fb/service-subscriptions/service-subscription/Nimbus/service-instances/service-instance/7131d483-b450-406f-8e30-0c650645fc67";
319         String json =
320                 "{\"result-data\": [{" +
321                         "\"resource-type\": \"" + resourceType + "\"," +
322                         "\"resource-link\": \"" + link + "\"" +
323                         "}]}";
324
325         AaiNodeQueryResponse nodeQueryResponse = new ObjectMapper().readValue(json, AaiNodeQueryResponse.class);
326         assertThat(nodeQueryResponse.resultData.get(0).resourceLink, equalTo(link));
327         assertThat(nodeQueryResponse.resultData.get(0).resourceType, is(expectedResourceType));
328     }
329
330     @Test
331     public void aaiNodeQueryEmptyResponseDeserializationTest() throws IOException {
332         String json = "{}";
333         AaiNodeQueryResponse nodeQueryResponse = new ObjectMapper().readValue(json, AaiNodeQueryResponse.class);
334         assertNull(nodeQueryResponse.resultData);
335     }
336
337     @DataProvider
338     public static Object[][] nameAndResourceTypeProvider() {
339         return new Object[][]{
340                 {"SRIOV_SVC", ResourceType.SERVICE_INSTANCE, "search/nodes-query?search-node-type=service-instance&filter=service-instance-name:EQUALS:SRIOV_SVC"},
341                 {"b1707vidnf", ResourceType.GENERIC_VNF, "search/nodes-query?search-node-type=generic-vnf&filter=vnf-name:EQUALS:b1707vidnf"},
342                 {"connectivity_test", ResourceType.VF_MODULE, "search/nodes-query?search-node-type=vf-module&filter=vf-module-name:EQUALS:connectivity_test"},
343                 {"MjVg1234", ResourceType.VOLUME_GROUP, "search/nodes-query?search-node-type=volume-group&filter=volume-group-name:EQUALS:MjVg1234"}
344         };
345     }
346
347     @Test(dataProvider = "nameAndResourceTypeProvider")
348     public void whenSearchNodeTypeByName_callRightAaiPath(String name, ResourceType type, String expectedUrl) {
349         when(aaiClientMock.searchNodeTypeByName(any(String.class), any(ResourceType.class))).thenCallRealMethod();
350         aaiClientMock.searchNodeTypeByName(name, type);
351         Mockito.verify(aaiClientMock).doAaiGet(eq(expectedUrl), eq(false));
352     }
353
354     @DataProvider
355     public static Object[][] aaiClientInternalExceptions() {
356         return Stream.<Pair<Class<? extends Throwable>, UncheckedBiConsumer<HttpsAuthClient, Client>>>of(
357
358                 // Exception out of httpsAuthClientMock
359                 Pair.of(CertificateException.class, (httpsAuthClientMock, javaxClientMock) -> {
360                     final CertificateException e0 = new CertificateException("No X509TrustManager implementation available");
361                     SSLHandshakeException e = new SSLHandshakeException(e0.toString());
362                     e.initCause(e0);
363
364                     when(httpsAuthClientMock.getClient(any())).thenThrow(e);
365                 }),
366
367                 Pair.of(StringIndexOutOfBoundsException.class, mockExceptionOnClientProvider(new StringIndexOutOfBoundsException(4))),
368
369                 Pair.of(NullPointerException.class, mockExceptionOnClientProvider(new NullPointerException("null"))),
370
371                 Pair.of(FileNotFoundException.class, mockExceptionOnClientProvider(new FileNotFoundException("vid/WEB-INF/cert/aai-client-cert.p12"))),
372
373                 Pair.of(BadPaddingException.class, mockExceptionOnClientProvider(
374                         new IOException("keystore password was incorrect", new BadPaddingException("Given final block not properly padded")))
375                 ),
376                 Pair.of(GenericUncheckedException.class, mockExceptionOnClientProvider(new GenericUncheckedException("basa"))),
377
378                 Pair.of(NullPointerException.class, (httpsAuthClientMock, javaxClientMock) ->
379                         when(httpsAuthClientMock.getClient(any())).thenReturn(null)),
380
381
382                 // Exception out of javax's Client
383                 Pair.of(SSLHandshakeException.class, (httpsAuthClientMock, javaxClientMock) -> {
384                     when(javaxClientMock.target(anyString())).thenThrow(
385                             new ProcessingException(new SSLHandshakeException("Received fatal alert: certificate_expired"))
386                     );
387                 }),
388
389                 Pair.of(SunCertPathBuilderException.class, (httpsAuthClientMock, javaxClientMock) -> {
390                     SunCertPathBuilderException e0 = new SunCertPathBuilderException("unable to find valid certification path to requested target");
391                     when(javaxClientMock.target(anyString())).thenThrow(
392                             new ProcessingException(new ValidatorException("PKIX path building failed: " + e0.toString(), e0))
393                     );
394                 }),
395
396                 Pair.of(GenericUncheckedException.class, (httpsAuthClientMock, javaxClientMock) ->
397                         when(javaxClientMock.target(anyString())).thenThrow(new GenericUncheckedException("basa")))
398
399         ).flatMap(l -> Stream.of(
400                 // double each case to propagateExceptions = true/false, to verify that "don't propagate" really still work
401                 ImmutableList.of(l.getLeft(), l.getRight(), true).toArray(),
402                 ImmutableList.of(l.getLeft(), l.getRight(), false).toArray()
403         )).collect(Collectors.toList()).toArray(new Object[][]{});
404     }
405
406     private static UncheckedBiConsumer<HttpsAuthClient, Client> mockExceptionOnClientProvider(Exception e) {
407         return (httpsAuthClientMock, javaxClientMock) ->
408                 when(httpsAuthClientMock.getClient(any())).thenThrow(e);
409     }
410
411     @Test(dataProvider = "aaiClientInternalExceptions")
412     public void propagateExceptions_internalsThrowException_ExceptionRethrown(Class<? extends Throwable> expectedType, BiConsumer<HttpsAuthClient, Client> setupMocks, boolean propagateExceptions) throws Exception {
413         /*
414         Call chain is like:
415             this test -> AaiClient -> AAIRestInterface -> HttpsAuthClient -> javax's Client
416
417         In this test, *AaiClient* and *AAIRestInterface* are under test (actual
418         implementation is used), while HttpsAuthClient and the javax's Client are
419         mocked to return pseudo-responses or - better- throw exceptions.
420          */
421
422         // prepare mocks
423         HttpsAuthClient httpsAuthClientMock = mock(HttpsAuthClient.class);
424         TestUtils.JavaxRsClientMocks mocks = new TestUtils.JavaxRsClientMocks();
425         Client javaxClientMock = mocks.getFakeClient();
426         Response responseMock = mocks.getFakeResponse();
427
428         // prepare real AAIRestInterface and AaiClient, and wire mocks
429         AAIRestInterface aaiRestInterface = new AAIRestInterface(httpsAuthClientMock, new ServletRequestHelper(), new SystemPropertyHelper());
430         final AaiClient aaiClient = new AaiClient(aaiRestInterface, null);
431         when(httpsAuthClientMock.getClient(any())).thenReturn(javaxClientMock);
432
433         // define atomic method under test, including reset of "aaiRestInterface.client"
434         final Function<Boolean, Response> doAaiGet = (propagateExceptions1) -> {
435             try {
436                 FieldUtils.writeField(aaiRestInterface, "client", null, true);
437                 return aaiClient.doAaiGet("uri", false, propagateExceptions1).getResponse();
438             } catch (IllegalAccessException e) {
439                 throw new RuntimeException(e);
440             }
441         };
442
443         // verify setup again
444         assertThat("mocks setup should make doAaiGet return our responseMock", doAaiGet.apply(true), is(sameInstance(responseMock)));
445
446
447         /// TEST:
448         setupMocks.accept(httpsAuthClientMock, javaxClientMock);
449
450         try {
451             final Response response = doAaiGet.apply(propagateExceptions);
452         } catch (Exception e) {
453             if (propagateExceptions) {
454                 assertThat("root cause incorrect for " + ExceptionUtils.getStackTrace(e), ExceptionUtils.getRootCause(e), instanceOf(expectedType));
455                 return; // ok, done
456             } else {
457                 // Verify that "don't propagate" really still work
458                 Assert.fail("calling doAaiGet when propagateExceptions is false must result with no exception", e);
459             }
460         }
461
462         // If no exception caught
463         // We're asserting that the legacy behaviour is still in place. Hopefully
464         // one day we will remove the non-propagateExceptions case
465         assertFalse(propagateExceptions, "calling doAaiGet when propagateExceptions is 'true' must result with an exception (in this test)");
466     }
467
468     @Test
469     public void shouldProperlyReadResponseOnceWhenSubscribersAreNotPresent() {
470         AAIRestInterface restInterface = mock(AAIRestInterface.class);
471         PortDetailsTranslator portDetailsTranslator = mock(PortDetailsTranslator.class);
472         Response response = mock(Response.class);
473         when(response.getStatus()).thenReturn(404);
474         when(response.readEntity(String.class)).thenReturn("sampleEntity");
475         when(response.getStatusInfo()).thenReturn(Response.Status.NOT_FOUND);
476         ResponseWithRequestInfo responseWithRequestInfo = new ResponseWithRequestInfo(response, "test", HttpMethod.GET);
477         when(restInterface.RestGet(eq("VidAaiController"), any(String.class),
478                 eq("business/customers?subscriber-type=INFRA&depth=0"), eq(false), eq(true))).thenReturn(responseWithRequestInfo);
479         AaiClient aaiClient = new AaiClient(restInterface, portDetailsTranslator);
480
481
482         aaiClient.getAllSubscribers(true);
483
484         verify(response).readEntity(String.class);
485     }
486
487     @FunctionalInterface
488     public interface UncheckedBiConsumer<T, U> extends BiConsumer<T, U> {
489         @Override
490         default void accept(T t, U u) {
491             try {
492                 acceptThrows(t, u);
493             } catch (Exception e) {
494                 throw new RuntimeException(e);
495             }
496         }
497
498         void acceptThrows(T t, U u) throws Exception;
499     }
500 }