2 * ============LICENSE_START=======================================================
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
22 package org.onap.vid.aai;
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;
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;
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.*;
83 @ContextConfiguration(classes = {LocalWebConfig.class, SystemProperties.class})
85 public class AaiClientTest {
87 private AaiClient aaiClientMock;
88 private ServletContext servletContext;
91 public void initMocks() {
92 aaiClientMock = mock(AaiClient.class);
93 aaiClientMock.logger = mock(EELFLoggerDelegate.class);
94 servletContext = mock(ServletContext.class);
96 when(servletContext.getRealPath(any(String.class))).thenReturn("");
98 when(aaiClientMock.doAaiGet(any(String.class), any(Boolean.class))).thenReturn(null);
102 public static Object[][] logicalLinkData() {
103 return new Object[][]{
104 {"", "network/logical-links/logical-link/"},
105 {"link", "network/logical-links/logical-link/link"}
109 @Test(dataProvider = "logicalLinkData")
110 public void getLogicalLink_Link_Is_Empty(String link, String expectedUrl) {
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));
118 public static Object[][] subscribersResults() {
119 return new Object[][]{
120 {new SubscriberList(new ArrayList<Subscriber>() {{
121 add(new Subscriber());
122 add(new Subscriber());
124 {new SubscriberList(new ArrayList<Subscriber>() {{
125 add(new Subscriber());
127 {new SubscriberList(new ArrayList<Subscriber>()), false}
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(
138 isAvailable ? "OK" : "No subscriber received",
141 Mockito.when(aaiClientMock.getAllSubscribers(true)).thenReturn(
142 new AaiResponseWithRequestInfo<>(
143 HttpMethod.GET, "url", new AaiResponse<>(subscribers, null, 200),
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())));
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")
158 private static String statusDataReflected(ExternalComponentStatus status) {
159 return new ReflectionToStringBuilder(status, ToStringStyle.SHORT_PREFIX_STYLE)
160 .setExcludeFieldNames("metadata")
165 public static Object[][] rawData() {
166 return new Object[][]{
167 {"errorMessage",}, {""}, {null}
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,
176 ExternalComponentStatus result = callProbeAaiGetAllSubscribersAndAssertNotAvailable();
177 assertThat(result.getMetadata(), instanceOf(HttpRequestMetadata.class));
178 assertEquals(((HttpRequestMetadata) result.getMetadata()).getRawData(), rawData);
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()},
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);
203 assertThat(result.getMetadata().getDescription(), containsString(description));
206 private ExternalComponentStatus callProbeAaiGetAllSubscribersAndAssertNotAvailable() {
207 Mockito.when(aaiClientMock.probeAaiGetAllSubscribers()).thenCallRealMethod();
208 ExternalComponentStatus result = aaiClientMock.probeAaiGetAllSubscribers();
209 assertFalse(result.isAvailable());
215 public void getTenants_Arguments_Are_Null_Or_Empty() {
217 when(aaiClientMock.getTenants(any(), any())).thenCallRealMethod();
219 AaiResponse response = aaiClientMock.getTenants("", "");
221 assertEquals(response.getErrorMessage(), "{\"statusText\":\" Failed to retrieve LCP Region & Tenants from A&AI, Subscriber ID or Service Type is missing.\"}");
224 response = aaiClientMock.getTenants(null, null);
226 assertEquals(response.getErrorMessage(), "{\"statusText\":\" Failed to retrieve LCP Region & Tenants from A&AI, Subscriber ID or Service Type is missing.\"}");
230 public void getTenants_Arguments_Are_Valid_But_Tenants_Not_Exist() {
232 when(aaiClientMock.getTenants(any(String.class), any(String.class))).thenCallRealMethod();
234 Response generalEmptyResponse = mock(Response.class);
235 when(aaiClientMock.doAaiGet(any(String.class), any(Boolean.class))).thenReturn(generalEmptyResponse);
237 AaiResponse response = aaiClientMock.getTenants("subscriberId", "serviceType");
239 assertEquals(response.getErrorMessage(), "{\"statusText\":\" A&AI has no LCP Region & Tenants associated to subscriber 'subscriberId' and service type 'serviceType'\"}");
244 public void getTenants_Arguments_Are_Valid_Get_The_Tenanats() {
246 when(aaiClientMock.getTenants(any(String.class), any(String.class))).thenCallRealMethod();
249 Response generalEmptyResponse = mock(Response.class);
251 when(generalEmptyResponse.readEntity(String.class)).thenReturn(tenantResponseRaw);
252 when(generalEmptyResponse.getStatus()).thenReturn(200);
253 when(generalEmptyResponse.getStatusInfo()).thenReturn(new Response.StatusType() {
255 public int getStatusCode() {
260 public Response.Status.Family getFamily() {
261 return Response.Status.Family.SUCCESSFUL;
265 public String getReasonPhrase() {
271 when(aaiClientMock.doAaiGet(any(String.class), any(Boolean.class))).thenReturn(generalEmptyResponse);
273 AaiResponse<GetTenantsResponse[]> response = aaiClientMock.getTenants("subscriberId", "serviceType");
275 Assert.assertTrue(response.t.length > 0);
278 final String tenantResponseRaw = "" +
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\"" +
291 "\"relationship-key\": \"cloud-region.cloud-region-id\"," +
292 "\"relationship-value\": \"AAIAIC25\"" +
295 "\"relationship-key\": \"tenant.tenant-id\"," +
296 "\"relationship-value\": \"092eb9e8e4b7412e8787dd091bc58e86\"" +
299 "\"related-to-property\": [{" +
300 "\"property-key\": \"tenant.tenant-name\"," +
301 "\"property-value\": \"USP-SIP-IC-24335-T-01\"" +
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}
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";
320 "{\"result-data\": [{" +
321 "\"resource-type\": \"" + resourceType + "\"," +
322 "\"resource-link\": \"" + link + "\"" +
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));
331 public void aaiNodeQueryEmptyResponseDeserializationTest() throws IOException {
333 AaiNodeQueryResponse nodeQueryResponse = new ObjectMapper().readValue(json, AaiNodeQueryResponse.class);
334 assertNull(nodeQueryResponse.resultData);
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"}
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));
355 public static Object[][] aaiClientInternalExceptions() {
356 return Stream.<Pair<Class<? extends Throwable>, UncheckedBiConsumer<HttpsAuthClient, Client>>>of(
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());
364 when(httpsAuthClientMock.getClient(any())).thenThrow(e);
367 Pair.of(StringIndexOutOfBoundsException.class, mockExceptionOnClientProvider(new StringIndexOutOfBoundsException(4))),
369 Pair.of(NullPointerException.class, mockExceptionOnClientProvider(new NullPointerException("null"))),
371 Pair.of(FileNotFoundException.class, mockExceptionOnClientProvider(new FileNotFoundException("vid/WEB-INF/cert/aai-client-cert.p12"))),
373 Pair.of(BadPaddingException.class, mockExceptionOnClientProvider(
374 new IOException("keystore password was incorrect", new BadPaddingException("Given final block not properly padded")))
376 Pair.of(GenericUncheckedException.class, mockExceptionOnClientProvider(new GenericUncheckedException("basa"))),
378 Pair.of(NullPointerException.class, (httpsAuthClientMock, javaxClientMock) ->
379 when(httpsAuthClientMock.getClient(any())).thenReturn(null)),
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"))
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))
396 Pair.of(GenericUncheckedException.class, (httpsAuthClientMock, javaxClientMock) ->
397 when(javaxClientMock.target(anyString())).thenThrow(new GenericUncheckedException("basa")))
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[][]{});
406 private static UncheckedBiConsumer<HttpsAuthClient, Client> mockExceptionOnClientProvider(Exception e) {
407 return (httpsAuthClientMock, javaxClientMock) ->
408 when(httpsAuthClientMock.getClient(any())).thenThrow(e);
411 @Test(dataProvider = "aaiClientInternalExceptions")
412 public void propagateExceptions_internalsThrowException_ExceptionRethrown(Class<? extends Throwable> expectedType, BiConsumer<HttpsAuthClient, Client> setupMocks, boolean propagateExceptions) throws Exception {
415 this test -> AaiClient -> AAIRestInterface -> HttpsAuthClient -> javax's Client
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.
423 HttpsAuthClient httpsAuthClientMock = mock(HttpsAuthClient.class);
424 TestUtils.JavaxRsClientMocks mocks = new TestUtils.JavaxRsClientMocks();
425 Client javaxClientMock = mocks.getFakeClient();
426 Response responseMock = mocks.getFakeResponse();
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);
433 // define atomic method under test, including reset of "aaiRestInterface.client"
434 final Function<Boolean, Response> doAaiGet = (propagateExceptions1) -> {
436 FieldUtils.writeField(aaiRestInterface, "client", null, true);
437 return aaiClient.doAaiGet("uri", false, propagateExceptions1).getResponse();
438 } catch (IllegalAccessException e) {
439 throw new RuntimeException(e);
443 // verify setup again
444 assertThat("mocks setup should make doAaiGet return our responseMock", doAaiGet.apply(true), is(sameInstance(responseMock)));
448 setupMocks.accept(httpsAuthClientMock, javaxClientMock);
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));
457 // Verify that "don't propagate" really still work
458 Assert.fail("calling doAaiGet when propagateExceptions is false must result with no exception", e);
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)");
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);
482 aaiClient.getAllSubscribers(true);
484 verify(response).readEntity(String.class);
488 public interface UncheckedBiConsumer<T, U> extends BiConsumer<T, U> {
490 default void accept(T t, U u) {
493 } catch (Exception e) {
494 throw new RuntimeException(e);
498 void acceptThrows(T t, U u) throws Exception;