2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 - 2019 AT&T Intellectual Property. 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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.vid.aai;
23 import static java.util.stream.Collectors.toList;
24 import static org.apache.commons.lang3.StringUtils.equalsIgnoreCase;
25 import static org.hamcrest.MatcherAssert.assertThat;
26 import static org.hamcrest.Matchers.containsInAnyOrder;
27 import static org.hamcrest.Matchers.containsString;
28 import static org.hamcrest.Matchers.either;
29 import static org.hamcrest.Matchers.hasProperty;
30 import static org.hamcrest.Matchers.instanceOf;
31 import static org.hamcrest.Matchers.is;
32 import static org.hamcrest.Matchers.sameInstance;
33 import static org.mockito.ArgumentMatchers.any;
34 import static org.mockito.ArgumentMatchers.anyBoolean;
35 import static org.mockito.ArgumentMatchers.anyString;
36 import static org.mockito.ArgumentMatchers.argThat;
37 import static org.mockito.ArgumentMatchers.eq;
38 import static org.mockito.ArgumentMatchers.isNull;
39 import static org.mockito.ArgumentMatchers.nullable;
40 import static org.mockito.Mockito.mock;
41 import static org.mockito.Mockito.verify;
42 import static org.mockito.Mockito.when;
43 import static org.onap.vid.utils.KotlinUtilsKt.JACKSON_OBJECT_MAPPER;
44 import static org.onap.vid.utils.Unchecked.toURI;
45 import static org.testng.Assert.assertEquals;
46 import static org.testng.Assert.assertFalse;
47 import static org.testng.Assert.assertNull;
48 import static org.testng.Assert.fail;
50 import com.fasterxml.jackson.core.JsonProcessingException;
51 import com.fasterxml.jackson.databind.ObjectMapper;
52 import com.google.common.collect.ImmutableList;
53 import java.io.FileNotFoundException;
54 import java.io.IOException;
56 import java.security.cert.CertificateException;
57 import java.util.ArrayList;
59 import java.util.Optional;
60 import java.util.function.BiConsumer;
61 import java.util.function.Function;
62 import java.util.stream.Stream;
63 import javax.crypto.BadPaddingException;
64 import javax.net.ssl.SSLHandshakeException;
65 import javax.servlet.ServletContext;
66 import javax.ws.rs.ProcessingException;
67 import javax.ws.rs.client.Client;
68 import javax.ws.rs.core.Response;
69 import org.apache.commons.lang3.builder.ReflectionToStringBuilder;
70 import org.apache.commons.lang3.builder.ToStringStyle;
71 import org.apache.commons.lang3.exception.ExceptionUtils;
72 import org.apache.commons.lang3.reflect.FieldUtils;
73 import org.apache.commons.lang3.tuple.Pair;
74 import org.apache.http.HttpStatus;
75 import org.mockito.Mockito;
76 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
77 import org.onap.portalsdk.core.util.SystemProperties;
78 import org.onap.vid.aai.model.AaiGetTenatns.GetTenantsResponse;
79 import org.onap.vid.aai.model.CustomQuerySimpleResult;
80 import org.onap.vid.aai.model.ModelVer;
81 import org.onap.vid.aai.model.ModelVersions;
82 import org.onap.vid.aai.model.PortDetailsTranslator;
83 import org.onap.vid.aai.model.Properties;
84 import org.onap.vid.aai.model.RelatedToProperty;
85 import org.onap.vid.aai.model.ResourceType;
86 import org.onap.vid.aai.model.SimpleResult;
87 import org.onap.vid.aai.util.AAIRestInterface;
88 import org.onap.vid.aai.util.CacheProvider;
89 import org.onap.vid.aai.util.HttpsAuthClient;
90 import org.onap.vid.aai.util.ServletRequestHelper;
91 import org.onap.vid.aai.util.SystemPropertyHelper;
92 import org.onap.vid.controller.LocalWebConfig;
93 import org.onap.vid.exceptions.GenericUncheckedException;
94 import org.onap.vid.model.Subscriber;
95 import org.onap.vid.model.SubscriberList;
96 import org.onap.vid.model.probes.ExternalComponentStatus;
97 import org.onap.vid.model.probes.HttpRequestMetadata;
98 import org.onap.vid.model.probes.StatusMetadata;
99 import org.onap.vid.testUtils.TestUtils;
100 import org.onap.vid.utils.Unchecked;
101 import org.springframework.http.HttpMethod;
102 import org.springframework.test.context.ContextConfiguration;
103 import org.springframework.test.context.web.WebAppConfiguration;
104 import org.testng.Assert;
105 import org.testng.annotations.BeforeMethod;
106 import org.testng.annotations.DataProvider;
107 import org.testng.annotations.Test;
108 import sun.security.provider.certpath.SunCertPathBuilderException;
109 import sun.security.validator.ValidatorException;
111 @ContextConfiguration(classes = {LocalWebConfig.class, SystemProperties.class})
113 public class AaiClientTest {
115 private final String NO_LCP_REGION_AND_TENANTS_MSG = "A&AI has no LCP Region & Tenants associated to subscriber 'subscriberId' and service type 'serviceType'";
116 private AaiClient aaiClientMock;
117 private ServletContext servletContext;
120 public void initMocks(){
121 aaiClientMock = mock(AaiClient.class);
122 aaiClientMock.logger = mock(EELFLoggerDelegate.class);
123 aaiClientMock.objectMapper = new ObjectMapper();
124 servletContext = mock(ServletContext.class);
126 when(servletContext.getRealPath(any(String.class))).thenReturn("");
128 when(aaiClientMock.doAaiGet(any(String.class),any(Boolean.class))).thenReturn(null);
129 when(aaiClientMock.doAaiGet(any(URI.class), anyBoolean(), anyBoolean())).thenReturn(null);
133 public static Object[][] logicalLinkData() {
134 return new Object[][] {
135 {"", "network/logical-links/logical-link/"},
136 {"link", "network/logical-links/logical-link/link"}
140 @Test(dataProvider = "logicalLinkData")
141 public void getLogicalLink_Link_Is_Empty(String link, String expectedUrl) {
143 when(aaiClientMock.getLogicalLink(any(String.class))).thenCallRealMethod();
144 aaiClientMock.getLogicalLink(link);
145 verify(aaiClientMock).doAaiGet(argThat(s -> equalsIgnoreCase(s, expectedUrl)),any(Boolean.class));
149 public static Object[][] subscribersResults() {
150 return new Object[][] {
151 {new SubscriberList(new ArrayList<Subscriber>() {{ add(new Subscriber()); add(new Subscriber()); }}), true},
152 {new SubscriberList(new ArrayList<Subscriber>() {{ add(new Subscriber()); }}), true},
153 {new SubscriberList(new ArrayList<Subscriber>()), false}
157 @Test(dataProvider = "subscribersResults")
158 public void testProbeAaiGetAllSubscribers_returnsTwoToZeroSubscribers_ResultsAsExpected(SubscriberList subscribers, boolean isAvailable){
159 ExternalComponentStatus expectedStatus = new ExternalComponentStatus(ExternalComponentStatus.Component.AAI,isAvailable, new HttpRequestMetadata(
164 isAvailable ? "OK" : "No subscriber received",
167 Mockito.when(aaiClientMock.getAllSubscribers(true)).thenReturn(
168 new AaiResponseWithRequestInfo<>(
169 HttpMethod.GET, "url", new AaiResponse<>(subscribers, null, 200),
171 Mockito.when(aaiClientMock.probeComponent()).thenCallRealMethod();
172 ExternalComponentStatus result = aaiClientMock.probeComponent();
173 assertThat(statusDataReflected(result),is(statusDataReflected(expectedStatus)));
174 assertThat(requestMetadataReflected(result.getMetadata()),is(requestMetadataReflected(expectedStatus.getMetadata())));
177 @Test(expectedExceptions = Exception.class)
178 public void typedAaiGet_aaiRestInterfaceRestGetReturnsError_exceptionIsThrown() {
179 AAIRestInterface aaiRestInterface = mock(AAIRestInterface.class);
180 final ResponseWithRequestInfo responseWithRequestInfo = mockedResponseWithRequestInfo(Response.Status.INTERNAL_SERVER_ERROR, "entity");
181 mockForGetRequest(aaiRestInterface, responseWithRequestInfo);
182 final AaiClient aaiClient = new AaiClient(aaiRestInterface, null, null);
186 aaiClient.typedAaiGet(toURI("/irrelevant/url"), RelatedToProperty.class);
188 } catch (Exception e) {
189 assertThat(ExceptionUtils.getStackTrace(e), e, instanceOf(ExceptionWithRequestInfo.class));
190 ExceptionWithRequestInfo e2 = ((ExceptionWithRequestInfo) e);
191 assertThat(e2.getHttpCode(), is(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()));
192 assertThat(e2.getRawData(), is("entity"));
193 assertThat(e2.getHttpMethod(), is(HttpMethod.GET));
194 assertThat(e2.getRequestedUrl(), is("/my/mocked/url"));
200 @Test(expectedExceptions = Exception.class)
201 public void typedAaiGet_aaiRestInterfaceRestGetReturnsInparsableResponse_exceptionIsThrown() {
202 AAIRestInterface aaiRestInterface = mock(AAIRestInterface.class);
203 final ResponseWithRequestInfo responseWithRequestInfo = mockedResponseWithRequestInfo(Response.Status.OK, "entity");
204 mockForGetRequest(aaiRestInterface, responseWithRequestInfo);
205 final AaiClient aaiClient = new AaiClient(aaiRestInterface, null, null);
209 aaiClient.typedAaiGet(toURI("/irrelevant/url"), RelatedToProperty.class);
211 } catch (Exception e) {
212 assertThat(ExceptionUtils.getStackTrace(e), e, instanceOf(ExceptionWithRequestInfo.class));
213 assertThat(e.getCause(),
214 hasProperty("cause", is(instanceOf(com.fasterxml.jackson.core.JsonParseException.class)))
221 public void typedAaiGet_aaiRestInterfaceRestGetReturns_objectIsFine() {
222 AAIRestInterface aaiRestInterface = mock(AAIRestInterface.class);
223 final ResponseWithRequestInfo responseWithRequestInfo = mockedResponseWithRequestInfo(Response.Status.OK,
224 "{ \"property-key\": \"foo\", \"property-value\": \"bar\" }");
225 mockForGetRequest(aaiRestInterface, responseWithRequestInfo);
227 final AaiClient aaiClient = new AaiClient(aaiRestInterface, null, null);
229 final RelatedToProperty relatedToPropertyAaiResponse = aaiClient.typedAaiGet(toURI("/irrelevant/url"), RelatedToProperty.class);
231 assertThat(relatedToPropertyAaiResponse.getPropertyKey(), is("foo"));
232 assertThat(relatedToPropertyAaiResponse.getPropertyValue(), is("bar"));
235 private ResponseWithRequestInfo mockedResponseWithRequestInfo(Response.Status status, String entity) {
236 return mockedResponseWithRequestInfo(status, entity, "/my/mocked/url", HttpMethod.GET);
239 private ResponseWithRequestInfo mockedResponseWithRequestInfo(Response.Status status, String entity, String requestUrl, HttpMethod method) {
240 final Response mockResponse = mock(Response.class);
241 when(mockResponse.getStatus()).thenReturn(status.getStatusCode());
242 when(mockResponse.getStatusInfo()).thenReturn(status);
243 when(mockResponse.readEntity(String.class)).thenReturn(entity);
244 return new ResponseWithRequestInfo(mockResponse, requestUrl, method);
247 //serialize fields except of fields we cannot know ahead of time
248 private static String requestMetadataReflected(StatusMetadata metadata) {
249 return new ReflectionToStringBuilder(metadata, ToStringStyle.SHORT_PREFIX_STYLE)
250 .setExcludeFieldNames("duration")
254 private static String statusDataReflected(ExternalComponentStatus status) {
255 return new ReflectionToStringBuilder(status, ToStringStyle.SHORT_PREFIX_STYLE)
256 .setExcludeFieldNames("metadata")
261 public static Object[][] rawData() {
262 return new Object[][]{
263 {"errorMessage", }, {""}, {null}
267 @Test(dataProvider = "rawData")
268 public void testProbeAaiGetFullSubscribersWithNullResponse_returnsNotAvailableWithErrorRawData(String rawData){
269 Mockito.when(aaiClientMock.getAllSubscribers(true)).thenReturn(
270 new AaiResponseWithRequestInfo<>(HttpMethod.GET, "url", null,
272 ExternalComponentStatus result = callProbeAaiGetAllSubscribersAndAssertNotAvailable();
273 assertThat(result.getMetadata(), instanceOf(HttpRequestMetadata.class));
274 assertEquals(((HttpRequestMetadata) result.getMetadata()).getRawData(), rawData);
278 public static Object[][] exceptions() {
279 return new Object[][] {
280 {"NullPointerException", "errorMessage",
281 new ExceptionWithRequestInfo(HttpMethod.GET, "url",
282 "errorMessage", null, new NullPointerException())},
283 {"RuntimeException", null,
284 new ExceptionWithRequestInfo(HttpMethod.GET, "url",
285 null, null, new RuntimeException())},
286 {"RuntimeException", null,
287 new RuntimeException()},
291 @Test(dataProvider = "exceptions")
292 public void testProbeAaiGetFullSubscribersWithNullResponse_returnsNotAvailableWithErrorRawData(String description, String expectedRawData, Exception exception){
293 Mockito.when(aaiClientMock.getAllSubscribers(true)).thenThrow(exception);
294 ExternalComponentStatus result = callProbeAaiGetAllSubscribersAndAssertNotAvailable();
295 if (exception instanceof ExceptionWithRequestInfo) {
296 assertThat(result.getMetadata(), instanceOf(HttpRequestMetadata.class));
297 assertEquals(((HttpRequestMetadata) result.getMetadata()).getRawData(), expectedRawData);
299 assertThat(result.getMetadata().getDescription(), containsString(description));
302 private ExternalComponentStatus callProbeAaiGetAllSubscribersAndAssertNotAvailable() {
303 Mockito.when(aaiClientMock.probeComponent()).thenCallRealMethod();
304 ExternalComponentStatus result = aaiClientMock.probeComponent();
305 assertFalse(result.isAvailable());
311 public void getTenants_Arguments_Are_Null_Or_Empty() {
313 when(aaiClientMock.getTenants(any(), any())).thenCallRealMethod();
315 AaiResponse response = aaiClientMock.getTenants("", "");
317 assertEquals(response.getErrorMessage(), "{\"statusText\":\" Failed to retrieve LCP Region & Tenants from A&AI, Subscriber ID or Service Type is missing.\"}");
320 response = aaiClientMock.getTenants(null, null);
322 assertEquals(response.getErrorMessage(), "{\"statusText\":\" Failed to retrieve LCP Region & Tenants from A&AI, Subscriber ID or Service Type is missing.\"}");
325 @Test(expectedExceptions = AaiClient.ParsingGetTenantsResponseFailure.class, expectedExceptionsMessageRegExp = NO_LCP_REGION_AND_TENANTS_MSG)
326 public void getTenants_Arguments_Are_Valid_But_Tenants_Not_Exist() {
328 when(aaiClientMock.getTenantsNonCached(any(String.class),any(String.class))).thenCallRealMethod();
330 Response generalEmptyResponse = mock(Response.class);
331 when(aaiClientMock.doAaiGet(any(String.class),any(Boolean.class))).thenReturn(generalEmptyResponse);
333 aaiClientMock.getTenantsNonCached("subscriberId", "serviceType");
337 public void whenCacheThrowException_thenGetTenantReturnAaiResponse() {
338 CacheProvider mockCacheProvider = mock(CacheProvider.class);
339 CacheProvider.Cache mockCache = mock(CacheProvider.Cache.class);
340 AaiClient aaiClientUnderTest = new AaiClient(null, null, mockCacheProvider);
342 when(mockCacheProvider.aaiClientCacheFor(any(), any())).thenReturn(mockCache);
343 when(mockCache.get(any())).thenThrow(new AaiClient.ParsingGetTenantsResponseFailure(NO_LCP_REGION_AND_TENANTS_MSG));
344 AaiResponse aaiResponse = aaiClientUnderTest.getTenants("subscriberId", "serviceType");
345 assertEquals(HttpStatus.SC_INTERNAL_SERVER_ERROR, aaiResponse.getHttpCode());
346 assertEquals("{\"statusText\":\""+NO_LCP_REGION_AND_TENANTS_MSG+"\"}", aaiResponse.getErrorMessage());
350 public void getTenants_Arguments_Are_Valid_Get_The_Tenanats() {
352 when(aaiClientMock.getTenantsNonCached(any(String.class),any(String.class))).thenCallRealMethod();
355 Response generalEmptyResponse = mock(Response.class);
357 when(generalEmptyResponse.readEntity(String.class)).thenReturn(tenantResponseRaw);
358 when(generalEmptyResponse.getStatus()).thenReturn(200);
359 when(generalEmptyResponse.getStatusInfo()).thenReturn(Response.Status.OK);
362 when(aaiClientMock.doAaiGet(any(String.class),any(Boolean.class))).thenReturn(generalEmptyResponse);
364 AaiResponse<GetTenantsResponse[]> response = aaiClientMock.getTenantsNonCached("subscriberId", "serviceType");
366 GetTenantsResponse[] tenants = response.getT();
368 Assert.assertTrue(response.t.length> 0);
370 Assert.assertEquals(tenants[0].cloudOwner,"irma-aic-cloud-owner");
373 final String tenantResponseRaw ="" +
375 "\"service-type\": \"VIRTUAL USP\"," +
376 "\"resource-version\": \"1494001841964\"," +
377 "\"relationship-list\": {" +
378 "\"relationship\": [{" +
379 "\"related-to\": \"tenant\"," +
380 "\"related-link\": \"/aai/v11/cloud-infrastructure/cloud-regions/cloud-region/irma-aic/AAIAIC25/tenants/tenant/092eb9e8e4b7412e8787dd091bc58e86\"," +
381 "\"relationship-data\": [{" +
382 "\"relationship-key\": \"cloud-region.cloud-owner\"," +
383 "\"relationship-value\": \"irma-aic-cloud-owner\"" +
386 "\"relationship-key\": \"cloud-region.cloud-region-id\"," +
387 "\"relationship-value\": \"AAIAIC25\"" +
390 "\"relationship-key\": \"tenant.tenant-id\"," +
391 "\"relationship-value\": \"092eb9e8e4b7412e8787dd091bc58e86\"" +
394 "\"related-to-property\": [{" +
395 "\"property-key\": \"tenant.tenant-name\"," +
396 "\"property-value\": \"USP-SIP-IC-24335-T-01\"" +
402 final String vfModuleHomingResponseRaw ="{" +
403 " \"vf-module-id\": \"ed02354a-3217-45ce-a1cd-e0b69b7a8cea\"," +
404 " \"vf-module-name\": \"apndns_az_02_module_1\"," +
405 " \"heat-stack-id\": \"apndns_az_02_module_1/97a319f3-b095-4fff-befa-c657508ecaf8\"," +
406 " \"orchestration-status\": \"active\"," +
407 " \"is-base-vf-module\": false," +
408 " \"resource-version\": \"1530559380383\"," +
409 " \"model-invariant-id\": \"74450b48-0aa0-4743-8314-9163e92b7862\"," +
410 " \"model-version-id\": \"6bc01a2b-bc48-4991-b9fe-e22c2215d801\"," +
411 " \"model-customization-id\": \"74f638c2-0368-4212-8f73-e961005af17c\"," +
412 " \"module-index\": 0," +
413 " \"relationship-list\": {" +
414 " \"relationship\": [" +
416 " \"related-to\": \"l3-network\"," +
417 " \"relationship-label\": \"org.onap.relationships.inventory.DependsOn\"," +
418 " \"related-link\": \"/aai/v12/network/l3-networks/l3-network/335e62be-73a3-41e8-930b-1a677bcafea5\"," +
419 " \"relationship-data\": [" +
421 " \"relationship-key\": \"l3-network.network-id\"," +
422 " \"relationship-value\": \"335e62be-73a3-41e8-930b-1a677bcafea5\"" +
425 " \"related-to-property\": [" +
427 " \"property-key\": \"l3-network.network-name\"," +
428 " \"property-value\": \"MNS-FN-25180-T-02Shared_oam_protected_net_1\"" +
433 " \"related-to\": \"l3-network\"," +
434 " \"relationship-label\": \"org.onap.relationships.inventory.DependsOn\"," +
435 " \"related-link\": \"/aai/v12/network/l3-networks/l3-network/2db4ee3e-2ac7-4fc3-8739-ecf53416459e\"," +
436 " \"relationship-data\": [" +
438 " \"relationship-key\": \"l3-network.network-id\"," +
439 " \"relationship-value\": \"2db4ee3e-2ac7-4fc3-8739-ecf53416459e\"" +
442 " \"related-to-property\": [" +
444 " \"property-key\": \"l3-network.network-name\"," +
445 " \"property-value\": \"Mobisupport-FN-27099-T-02_int_apn_dns_net_1\"" +
450 " \"related-to\": \"volume-group\"," +
451 " \"relationship-label\": \"org.onap.relationships.inventory.Uses\"," +
452 " \"related-link\": \"/aai/v12/cloud-infrastructure/cloud-regions/cloud-region/irma-aic/rdm5b/volume-groups/volume-group/66013ebe-0c81-44b9-a24f-7c6acba73a39\"," +
453 " \"relationship-data\": [" +
455 " \"relationship-key\": \"cloud-region.cloud-owner\"," +
456 " \"relationship-value\": \"irma-aic\"" +
459 " \"relationship-key\": \"cloud-region.cloud-region-id\"," +
460 " \"relationship-value\": \"rdm5b\"" +
463 " \"relationship-key\": \"volume-group.volume-group-id\"," +
464 " \"relationship-value\": \"66013ebe-0c81-44b9-a24f-7c6acba73a39\"" +
469 " \"related-to\": \"vserver\"," +
470 " \"relationship-label\": \"org.onap.relationships.inventory.Uses\"," +
471 " \"related-link\": \"/aai/v12/cloud-infrastructure/cloud-regions/cloud-region/irma-aic/rdm5b/tenants/tenant/db1818f7f2e34862b378bfb2cc520f91/vservers/vserver/5eef9f6d-9933-4bc6-9a1a-862d61309437\"," +
472 " \"relationship-data\": [" +
474 " \"relationship-key\": \"cloud-region.cloud-owner\"," +
475 " \"relationship-value\": \"irma-aic\"" +
478 " \"relationship-key\": \"cloud-region.cloud-region-id\"," +
479 " \"relationship-value\": \"rdm5b\"" +
482 " \"relationship-key\": \"tenant.tenant-id\"," +
483 " \"relationship-value\": \"db1818f7f2e34862b378bfb2cc520f91\"" +
486 " \"relationship-key\": \"vserver.vserver-id\"," +
487 " \"relationship-value\": \"5eef9f6d-9933-4bc6-9a1a-862d61309437\"" +
490 " \"related-to-property\": [" +
492 " \"property-key\": \"vserver.vserver-name\"," +
493 " \"property-value\": \"zrdm5bfapn01dns002\"" +
501 public void get_homingDataForVfModule() {
502 when(aaiClientMock.getHomingDataByVfModule(any(String.class), any(String.class))).thenCallRealMethod();
504 Response homingResponse = mock(Response.class);
506 when(homingResponse.readEntity(String.class)).thenReturn(vfModuleHomingResponseRaw);
507 when(homingResponse.getStatus()).thenReturn(200);
508 when(homingResponse.getStatusInfo()).thenReturn(Response.Status.OK);
511 when(aaiClientMock.doAaiGet(any(String.class), any(Boolean.class))).thenReturn(homingResponse);
513 GetTenantsResponse tenant = aaiClientMock.getHomingDataByVfModule("vnfInstanceId", "vfModuleId");
515 Assert.assertEquals(tenant.cloudOwner,"irma-aic" );
516 Assert.assertEquals(tenant.cloudRegionID,"rdm5b");
517 Assert.assertEquals(tenant.tenantID,"db1818f7f2e34862b378bfb2cc520f91");
520 @Test(expectedExceptions = GenericUncheckedException.class, expectedExceptionsMessageRegExp = "A&AI has no homing data associated to vfModule 'vfModuleId' of vnf 'vnfInstanceId'")
521 public void getVfMoudule_Homing_Arguments_Are_Valid_But_Not_Exists() {
522 when(aaiClientMock.getHomingDataByVfModule(any(String.class), any(String.class))).thenCallRealMethod();
524 Response generalEmptyResponse = mock(Response.class);
525 when(aaiClientMock.doAaiGet(any(String.class),any(Boolean.class))).thenReturn(generalEmptyResponse);
527 aaiClientMock.getHomingDataByVfModule("vnfInstanceId", "vfModuleId");
531 public static Object[][] invalidDataId() {
532 return new String[][] {
538 @Test(dataProvider = "invalidDataId", expectedExceptions = GenericUncheckedException.class, expectedExceptionsMessageRegExp = "Failed to retrieve homing data associated to vfModule from A&AI, VNF InstanceId or VF Module Id is missing.")
539 public void getVfMoudule_Homing_Arguments_Are_Empty_Or_Null(String data) {
540 when(aaiClientMock.getHomingDataByVfModule(any(), any())).thenCallRealMethod();
541 aaiClientMock.getHomingDataByVfModule(data, data);
545 public static Object[][] resourceTypesProvider() {
546 return new Object[][] {
547 {"service-instance", ResourceType.SERVICE_INSTANCE},
548 {"generic-vnf", ResourceType.GENERIC_VNF},
549 {"vf-module", ResourceType.VF_MODULE}
554 public static Object[][] nameAndResourceTypeProvider() {
555 return new Object[][] {
556 {"SRIOV_SVC", ResourceType.SERVICE_INSTANCE, "nodes/service-instances?service-instance-name=SRIOV_SVC"},
557 {"b1707vidnf", ResourceType.GENERIC_VNF, "nodes/generic-vnfs?vnf-name=b1707vidnf"},
558 {"connectivity_test", ResourceType.VF_MODULE, "nodes/vf-modules?vf-module-name=connectivity_test"},
559 {"ByronPace", ResourceType.INSTANCE_GROUP, "nodes/instance-groups?instance-group-name=ByronPace"},
560 {"MjVg1234", ResourceType.VOLUME_GROUP, "nodes/volume-groups?volume-group-name=MjVg1234"}
564 @Test(dataProvider = "nameAndResourceTypeProvider")
565 public void whenSearchNodeTypeByName_callRightAaiPath(String name, ResourceType type, String expectedUrl) {
566 AAIRestInterface aaiRestInterface = mock(AAIRestInterface.class);
567 ResponseWithRequestInfo responseWithRequestInfo = mockedResponseWithRequestInfo(Response.Status.OK, "{}");
569 when(aaiRestInterface.RestGet(anyString(), anyString(), eq(toURI(expectedUrl)), anyBoolean(), anyBoolean()))
570 .thenReturn(responseWithRequestInfo);
572 AaiClient aaiClient = new AaiClient(aaiRestInterface, null, null);
574 aaiClient.isNodeTypeExistsByName(name, type);
578 public static Object[][] aaiClientInternalExceptions() {
579 return Stream.<Pair<Class<? extends Throwable>, UncheckedBiConsumer<HttpsAuthClient, Client>>>of(
581 // Exception out of httpsAuthClientMock
582 Pair.of(CertificateException.class, (httpsAuthClientMock, javaxClientMock) -> {
583 final CertificateException e0 = new CertificateException("No X509TrustManager implementation available");
584 SSLHandshakeException e = new SSLHandshakeException(e0.toString());
587 when(httpsAuthClientMock.getClient(any())).thenThrow(e);
590 Pair.of(StringIndexOutOfBoundsException.class, mockExceptionOnClientProvider(new StringIndexOutOfBoundsException(4))),
592 Pair.of(NullPointerException.class, mockExceptionOnClientProvider(new NullPointerException("null"))),
594 Pair.of(FileNotFoundException.class, mockExceptionOnClientProvider(new FileNotFoundException("vid/WEB-INF/cert/aai-client-cert.p12"))),
596 Pair.of(BadPaddingException.class, mockExceptionOnClientProvider(
597 new IOException("keystore password was incorrect", new BadPaddingException("Given final block not properly padded")))
599 Pair.of(GenericUncheckedException.class, mockExceptionOnClientProvider(new GenericUncheckedException("basa"))),
601 Pair.of(NullPointerException.class, (httpsAuthClientMock, javaxClientMock) ->
602 when(httpsAuthClientMock.getClient(any())).thenReturn(null)),
605 // Exception out of javax's Client
606 Pair.of(SSLHandshakeException.class, (httpsAuthClientMock, javaxClientMock) -> {
607 when(javaxClientMock.target(nullable(String.class))).thenThrow(
608 new ProcessingException(new SSLHandshakeException("Received fatal alert: certificate_expired"))
612 Pair.of(SunCertPathBuilderException.class, (httpsAuthClientMock, javaxClientMock) -> {
613 SunCertPathBuilderException e0 = new SunCertPathBuilderException("unable to find valid certification path to requested target");
614 when(javaxClientMock.target(nullable(String.class))).thenThrow(
615 new ProcessingException(new ValidatorException("PKIX path building failed: " + e0.toString(), e0))
619 Pair.of(GenericUncheckedException.class, (httpsAuthClientMock, javaxClientMock) ->
620 when(javaxClientMock.target(nullable(String.class))).thenThrow(new GenericUncheckedException("basa")))
622 ).flatMap(l -> Stream.of(
623 // double each case to propagateExceptions = true/false, to verify that "don't propagate" really still work
624 ImmutableList.of(l.getLeft(), l.getRight(), true).toArray(),
625 ImmutableList.of(l.getLeft(), l.getRight(), false).toArray()
626 )).collect(toList()).toArray(new Object[][]{});
629 private static UncheckedBiConsumer<HttpsAuthClient, Client> mockExceptionOnClientProvider(Exception e) {
630 return (httpsAuthClientMock, javaxClientMock) ->
631 when(httpsAuthClientMock.getClient(any())).thenThrow(e);
634 @Test(dataProvider = "aaiClientInternalExceptions")
635 public void propagateExceptions_internalsThrowException_ExceptionRethrown(Class<? extends Throwable> expectedType, BiConsumer<HttpsAuthClient, Client> setupMocks, boolean propagateExceptions) throws Exception {
638 this test -> AaiClient -> AAIRestInterface -> HttpsAuthClient -> javax's Client
640 In this test, *AaiClient* and *AAIRestInterface* are under test (actual
641 implementation is used), while HttpsAuthClient and the javax's Client are
642 mocked to return pseudo-responses or - better- throw exceptions.
646 HttpsAuthClient httpsAuthClientMock = mock(HttpsAuthClient.class);
647 TestUtils.JavaxRsClientMocks mocks = new TestUtils.JavaxRsClientMocks();
648 Client javaxClientMock = mocks.getFakeClient();
649 Response responseMock = mocks.getFakeResponse();
651 // prepare real AAIRestInterface and AaiClient, and wire mocks
652 AAIRestInterface aaiRestInterface = new AAIRestInterface(httpsAuthClientMock, mock(ServletRequestHelper.class), mock(SystemPropertyHelper.class));
653 final AaiClient aaiClient = new AaiClient(aaiRestInterface, null, null);
654 when(httpsAuthClientMock.getClient(any())).thenReturn(javaxClientMock);
656 // define atomic method under test, including reset of "aaiRestInterface.client"
657 final Function<Boolean, Response> doAaiGet = (propagateExceptions1) -> {
659 FieldUtils.writeField(aaiRestInterface, "client", null, true);
660 return aaiClient.doAaiGet("uri", false, propagateExceptions1).getResponse();
661 } catch (IllegalAccessException e) {
662 throw new RuntimeException(e);
666 // verify setup again
667 assertThat("mocks setup should make doAaiGet return our responseMock", doAaiGet.apply(true), is(sameInstance(responseMock)));
671 setupMocks.accept(httpsAuthClientMock, javaxClientMock);
674 final Response response = doAaiGet.apply(propagateExceptions);
675 } catch (Exception e) {
676 if (propagateExceptions) {
677 assertThat("root cause incorrect for " + ExceptionUtils.getStackTrace(e), ExceptionUtils.getRootCause(e), instanceOf(expectedType));
680 // Verify that "don't propagate" really still work
681 Assert.fail("calling doAaiGet when propagateExceptions is false must result with no exception", e);
685 // If no exception caught
686 // We're asserting that the legacy behaviour is still in place. Hopefully
687 // one day we will remove the non-propagateExceptions case
688 assertFalse(propagateExceptions, "calling doAaiGet when propagateExceptions is 'true' must result with an exception (in this test)");
692 public static Object[][] aaiClientGetCloudOwnerByCloudRegionId() {
694 final String cloudRegion = "{" +
695 " \"cloud-owner\": \"mure-royo-ru22\"," +
696 " \"cloud-region-id\": \"ravitu\"," +
697 " \"cloud-type\": \"openstack\"," +
698 " \"resource-version\": \"1523631256125\"," +
699 " \"relationship-list\": {" +
700 " \"relationship\": [{" +
701 " \"related-to\": \"pserver\"" +
707 String bodyWith0 = "{ \"cloud-region\": [" + " ]}";
708 String bodyWith1 = "{ \"cloud-region\": [" + cloudRegion + " ]}";
709 String bodyWith2 = "{ \"cloud-region\": [" + cloudRegion + ", " + cloudRegion + " ]}";
710 String bodyWithDifferent2 = "{ \"cloud-region\": [" + cloudRegion + ", " +
711 cloudRegion.replace("mure-royo-ru22", "nolay-umaxo") +
714 return new Object[][] {
715 { "regular single result", bodyWith1, false },
716 { "exceptional empty result", bodyWith0, true },
717 { "two same results", bodyWith2, false },
718 { "two incoherent results", bodyWithDifferent2, true },
722 @Test(dataProvider = "aaiClientGetCloudOwnerByCloudRegionId")
723 public void getCloudOwnerByCloudRegionIdNonCached(String desc, String body, boolean expectingException) {
724 final String cloudRegion = "ravitu";
725 AAIRestInterface aaiRestInterface = mock(AAIRestInterface.class);
726 final ResponseWithRequestInfo responseWithRequestInfo = mockedResponseWithRequestInfo(Response.Status.OK, body);
727 when(aaiRestInterface.doRest(anyString(), anyString(), eq(Unchecked.toURI("cloud-infrastructure/cloud-regions?cloud-region-id=" + cloudRegion)),
728 isNull(), eq(HttpMethod.GET), anyBoolean(), anyBoolean()))
729 .thenReturn(responseWithRequestInfo);
731 final AaiClient aaiClient = new AaiClient(aaiRestInterface, null, null);
734 final String result = aaiClient.getCloudOwnerByCloudRegionIdNonCached(cloudRegion);
735 if (expectingException) fail("expected failure on " + desc + ", got " + result);
737 assertThat(result, is("mure-royo-ru22"));
739 } catch (Exception e) {
740 if (!expectingException) throw e;
742 assertThat(e.toString(), either(
743 containsString("No cloud-owner found for " + cloudRegion))
744 .or(containsString("Conflicting cloud-owner found for " + cloudRegion)));
750 public static Object[][] cloudRegionAndTenantDataProvider() {
751 return new Object[][] {
752 { "APPC-24595-T-IST-02C", "mtn23b" },
753 { "APPC-24595-T-IST-02C", null },
760 public void testGetLatestVersionByInvariantId() throws IOException {
762 ModelVersions modelVersions = JACKSON_OBJECT_MAPPER.readValue("" +
764 " \"results\": [\n" +
767 " \"model-invariant-id\": \"f6342be5-d66b-4d03-a1aa-c82c3094c4ea\",\n" +
768 " \"model-type\": \"service\",\n" +
769 " \"resource-version\": \"1534274421300\"\n" +
773 " \"model-ver\": {\n" +
774 " \"model-version-id\": \"a92f899d-a3ec-465b-baed-1663b0a5aee1\",\n" +
775 " \"model-name\": \"NCM_VLAN_SVC_ym161f\",\n" +
776 " \"model-version\": \"bbb\",\n" +
777 " \"distribution-status\": \"DISTRIBUTION_COMPLETE_OK\",\n" +
778 " \"model-description\": \"Network Collection service for vLAN tagging\",\n" +
779 " \"resource-version\": \"1534788756086\"\n" +
783 " \"model-ver\": {\n" +
784 " \"model-version-id\": \"d2fda667-e92e-4cfa-9620-5da5de01a319\",\n" +
785 " \"model-name\": \"NCM_VLAN_SVC_ym161f\",\n" +
786 " \"model-version\": \"aaa\",\n" +
787 " \"distribution-status\": \"DISTRIBUTION_COMPLETE_OK\",\n" +
788 " \"model-description\": \"Network Collection service for vLAN tagging\",\n" +
789 " \"resource-version\": \"1534444087221\"\n" +
791 " }]}", ModelVersions.class);
794 final AaiClient aaiClient = new AaiClient(null, null, null);
796 assertThat(aaiClient.toModelVerStream(modelVersions).collect(toList()),
798 hasProperty("modelVersionId", is("a92f899d-a3ec-465b-baed-1663b0a5aee1")),
799 hasProperty("modelVersionId", is("d2fda667-e92e-4cfa-9620-5da5de01a319"))
805 public static Object[][] versionsDataProvider() {
806 return new Object[][] {
807 { Stream.of("10","20","30"), "30" },
808 { Stream.of("10","20","20"), "20" },
809 { Stream.of("c","b","a"), "c" },
810 { Stream.of("1.0","2.0","1.8"), "2.0" },
811 { Stream.of("1.0.7","2.0.2","2.0.9"), "2.0.9" },
812 { Stream.of("0","0","0"), "0" },
813 { Stream.of("","10"), "10" },
818 @Test(dataProvider = "versionsDataProvider")
819 public void maxModelVer(Stream<String> input, String expected) {
820 Stream<ModelVer> modelVerStream = input.map(version -> {
821 ModelVer mv = new ModelVer();
822 mv.setModelVersion(version);
826 final AaiClient aaiClient = new AaiClient(null, null, null);
828 assertThat(aaiClient.maxModelVer(modelVerStream), hasProperty("modelVersion", is(expected)));
831 @Test(expectedExceptions = GenericUncheckedException.class)
832 public void maxModelVerException() {
833 final AaiClient aaiClient = new AaiClient(null, null, null);
834 aaiClient.maxModelVer(Stream.of(new ModelVer()));
836 @Test(dataProvider = "cloudRegionAndTenantDataProvider")
837 public void getCloudRegionAndTenantByVnfId(String tenantName, String cloudRegionId) throws JsonProcessingException {
838 SimpleResult tenant = new SimpleResult();
839 if (tenantName != null) {
840 tenant.setJsonNodeType("tenant");
841 Properties tenantProps = new Properties();
842 tenantProps.setTenantName(tenantName);
843 tenant.setJsonProperties(tenantProps);
846 SimpleResult cloudRegion = new SimpleResult();
847 if (cloudRegionId != null) {
848 cloudRegion.setJsonNodeType("cloud-region");
849 Properties cloudRegionProps = new Properties();
850 cloudRegionProps.setCloudRegionId(cloudRegionId);
851 cloudRegion.setJsonProperties(cloudRegionProps);
854 CustomQuerySimpleResult customQuerySimpleResult = new CustomQuerySimpleResult(ImmutableList.of(tenant, cloudRegion));
855 String mockedBody = new ObjectMapper().writeValueAsString(customQuerySimpleResult);
857 AAIRestInterface aaiRestInterface = mock(AAIRestInterface.class);
858 final ResponseWithRequestInfo responseWithRequestInfo = mockedResponseWithRequestInfo(Response.Status.OK, mockedBody, "query?format=simple", HttpMethod.PUT);
859 when(aaiRestInterface.doRest(anyString(), anyString(), eq(Unchecked.toURI("query?format=simple")),
860 any(), eq(HttpMethod.PUT), anyBoolean(), anyBoolean()))
861 .thenReturn(responseWithRequestInfo);
863 final AaiClient aaiClient = new AaiClient(aaiRestInterface, null, null);
864 Map<String, Properties> result = aaiClient.getCloudRegionAndTenantByVnfId("anyVnfId");
865 if (tenantName != null) {
866 assertEquals(result.get("tenant").getTenantName(), tenantName);
868 assertNull(result.get("tenant"));
871 if (cloudRegionId != null) {
872 assertEquals(result.get("cloud-region").getCloudRegionId(), cloudRegionId);
874 assertNull(result.get("cloud-region"));
878 protected void mockForGetRequest(AAIRestInterface aaiRestInterface, ResponseWithRequestInfo responseWithRequestInfo) {
879 when(aaiRestInterface.doRest(anyString(), anyString(), any(URI.class), isNull(), eq(HttpMethod.GET) ,anyBoolean(), anyBoolean()))
880 .thenReturn(responseWithRequestInfo);
884 public void shouldProperlyReadResponseOnceWhenSubscribersAreNotPresent() {
885 AAIRestInterface restInterface = mock(AAIRestInterface.class);
886 PortDetailsTranslator portDetailsTranslator = mock(PortDetailsTranslator.class);
887 Response response = mock(Response.class);
888 when(response.getStatus()).thenReturn(404);
889 when(response.readEntity(String.class)).thenReturn("sampleEntity");
890 when(response.getStatusInfo()).thenReturn(Response.Status.NOT_FOUND);
891 ResponseWithRequestInfo responseWithRequestInfo = new ResponseWithRequestInfo(response, "test", HttpMethod.GET);
892 when(restInterface.RestGet(eq("VidAaiController"), any(String.class),
893 eq(Unchecked.toURI("business/customers?subscriber-type=INFRA&depth=0")), eq(false), eq(true))).thenReturn(responseWithRequestInfo);
894 AaiClient aaiClient = new AaiClient(restInterface, portDetailsTranslator, null);
897 aaiClient.getAllSubscribers(true);
899 verify(response).readEntity(String.class);
903 public interface UncheckedBiConsumer<T, U> extends BiConsumer<T, U> {
905 default void accept(T t, U u) {
908 } catch (Exception e) {
909 throw new RuntimeException(e);
913 void acceptThrows(T t, U u) throws Exception;
917 public void getLatestVersionByInvariantId_verifyCallingExpectedApi(){
919 when(aaiClientMock.getLatestVersionByInvariantId(anyString())).thenCallRealMethod();
921 aaiClientMock.getLatestVersionByInvariantId("model-invariant-id");
923 Mockito.verify(aaiClientMock).doAaiPut(argThat(url -> url.endsWith("query?format=resource&depth=0")),argThat(payload -> payload.contains("service-design-and-creation/models/model/model-invariant-id")),anyBoolean());
928 public static Object[][] getSubscriberDataDataProvider() {
929 return new Object[][] {
931 { "another id 123", false },
935 @Test(dataProvider = "getSubscriberDataDataProvider")
936 public void getSubscriberDataParams(String subscriberId, boolean omitServiceInstances) {
937 String depth = omitServiceInstances ? "1" : "2";
938 when(aaiClientMock.getSubscriberData(anyString(),anyBoolean())).thenCallRealMethod();
939 aaiClientMock.getSubscriberData(subscriberId, omitServiceInstances);
940 Mockito.verify(aaiClientMock).doAaiGet(argThat(s -> s.contains("customer/" + subscriberId + "?") && s.contains("depth=" + depth)),any(Boolean.class));
944 public void testToModelVerStream() throws IOException {
946 ModelVersions modelVersions = JACKSON_OBJECT_MAPPER.readValue("" +
948 " \"results\": [\n" +
951 " \"model-invariant-id\": \"f6342be5-d66b-4d03-a1aa-c82c3094c4ea\",\n" +
952 " \"model-type\": \"service\",\n" +
953 " \"resource-version\": \"1534274421300\"\n" +
957 " \"model-ver\": {\n" +
958 " \"model-version-id\": \"a92f899d-a3ec-465b-baed-1663b0a5aee1\",\n" +
959 " \"model-name\": \"NCM_VLAN_SVC_ym161f\",\n" +
960 " \"model-version\": \"bbb\",\n" +
961 " \"distribution-status\": \"DISTRIBUTION_COMPLETE_OK\",\n" +
962 " \"model-description\": \"Network Collection service for vLAN tagging\",\n" +
963 " \"resource-version\": \"1534788756086\"\n" +
967 " \"model-ver\": {\n" +
968 " \"model-version-id\": \"d2fda667-e92e-4cfa-9620-5da5de01a319\",\n" +
969 " \"model-name\": \"NCM_VLAN_SVC_ym161f\",\n" +
970 " \"model-version\": \"aaa\",\n" +
971 " \"distribution-status\": \"DISTRIBUTION_COMPLETE_OK\",\n" +
972 " \"model-description\": \"Network Collection service for vLAN tagging\",\n" +
973 " \"resource-version\": \"1534444087221\"\n" +
975 " }]}", ModelVersions.class);
978 final AaiClient aaiClient = new AaiClient(null, null, null);
980 assertThat(aaiClient.toModelVerStream(modelVersions).collect(toList()),
982 hasProperty("modelVersionId", is("a92f899d-a3ec-465b-baed-1663b0a5aee1")),
983 hasProperty("modelVersionId", is("d2fda667-e92e-4cfa-9620-5da5de01a319"))