bb47180e4fb5b475ebbc4d08eb1ef0218db9fce4
[vid.git] / vid-app-common / src / test / java / org / onap / vid / services / AaiServiceImplTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * VID
4  * ================================================================================
5  * Copyright (C) 2017 - 2019 AT&T Intellectual Property. All rights reserved.
6  * Modifications Copyright (C) 2018 - 2019 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.services;
23
24 import com.fasterxml.jackson.core.JsonProcessingException;
25 import com.fasterxml.jackson.databind.JsonNode;
26 import com.fasterxml.jackson.databind.ObjectMapper;
27 import io.joshworks.restclient.http.HttpResponse;
28 import org.apache.http.HttpStatus;
29 import org.jetbrains.annotations.NotNull;
30 import org.junit.Test;
31 import org.junit.runner.RunWith;
32 import org.mockito.InjectMocks;
33 import org.mockito.Mock;
34 import org.mockito.junit.MockitoJUnitRunner;
35 import org.onap.vid.aai.AaiClientInterface;
36 import org.onap.vid.aai.AaiGetVnfResponse;
37 import org.onap.vid.aai.AaiOverTLSClientInterface;
38 import org.onap.vid.aai.AaiResponse;
39 import org.onap.vid.aai.AaiResponseTranslator;
40 import org.onap.vid.aai.ServiceInstancesSearchResults;
41 import org.onap.vid.aai.ServiceSubscription;
42 import org.onap.vid.aai.ServiceSubscriptions;
43 import org.onap.vid.aai.Services;
44 import org.onap.vid.aai.SubscriberFilteredResults;
45 import org.onap.vid.aai.model.AaiGetInstanceGroupsByCloudRegion;
46 import org.onap.vid.aai.model.AaiGetNetworkCollectionDetails.AaiGetRelatedInstanceGroupsByVnfId;
47 import org.onap.vid.aai.model.AaiGetNetworkCollectionDetails.RelatedToProperty;
48 import org.onap.vid.aai.model.AaiGetOperationalEnvironments.OperationalEnvironmentList;
49 import org.onap.vid.aai.model.AaiGetPnfs.Pnf;
50 import org.onap.vid.aai.model.AaiGetServicesRequestModel.GetServicesAAIRespone;
51 import org.onap.vid.aai.model.AaiGetTenatns.GetTenantsResponse;
52 import org.onap.vid.aai.model.GetServiceModelsByDistributionStatusResponse;
53 import org.onap.vid.aai.model.InstanceGroupInfo;
54 import org.onap.vid.aai.model.LogicalLinkResponse;
55 import org.onap.vid.aai.model.Model;
56 import org.onap.vid.aai.model.ModelVer;
57 import org.onap.vid.aai.model.ModelVers;
58 import org.onap.vid.aai.model.PortDetailsTranslator;
59 import org.onap.vid.aai.model.Properties;
60 import org.onap.vid.aai.model.Relationship;
61 import org.onap.vid.aai.model.RelationshipData;
62 import org.onap.vid.aai.model.RelationshipList;
63 import org.onap.vid.aai.model.Result;
64 import org.onap.vid.aai.model.ServiceRelationships;
65 import org.onap.vid.aai.model.VnfResult;
66 import org.onap.vid.asdc.beans.Service;
67 import org.onap.vid.model.Subscriber;
68 import org.onap.vid.model.SubscriberList;
69 import org.onap.vid.model.aaiTree.AAITreeNode;
70 import org.onap.vid.model.aaiTree.RelatedVnf;
71 import org.onap.vid.model.aaiTree.ServiceInstance;
72 import org.onap.vid.roles.RoleValidator;
73
74 import javax.ws.rs.core.Response;
75 import java.util.Arrays;
76 import java.util.Collection;
77 import java.util.Collections;
78 import java.util.HashMap;
79 import java.util.List;
80 import java.util.Map;
81
82 import static org.assertj.core.api.Assertions.assertThat;
83 import static org.mockito.ArgumentMatchers.any;
84 import static org.mockito.ArgumentMatchers.anyString;
85 import static org.mockito.ArgumentMatchers.eq;
86 import static org.mockito.Mockito.mock;
87 import static org.mockito.Mockito.when;
88
89 @RunWith(MockitoJUnitRunner.class)
90 public class AaiServiceImplTest {
91
92         private static final String GLOBAL_CUSTOMER_ID = "GLOBAL_CUSTOMER_ID";
93         private static final String CLOUD_REGION_ID = "CLOUD_REGION_ID";
94         private static final String VNF_TYPE = "VNF_TYPE";
95         private static final String TENANT_ID = "TENANT_ID";
96         private static final String TENANT_NAME = "TENANT_NAME";
97         private static final String SERVICE_TYPE = "SERVICE_TYPE";
98         private static final String CORRECT_VALUE = "CORRECT_VALUE";
99         private static final String SUBSCRIBER_ID = "SUBSCRIBER_ID_EXPECTED";
100         private static final String STATUS_TEXT = "STATUS_TEXT";
101         private static final String GLOBAL_SUBSCRIBER_ID = "GLOBAL_SUBSCRIBER_ID";
102         private static final String GLOBAL_SUBSCRIBER_ID_NULL_RESPONSE = "ID_NULL";
103         private static final String VNF_INSTANCE_ID_OK = "VNF_INSTANCE_ID_OK";
104         private static final String VNF_INSTANCE_ID_FAIL = "VNF_INSTANCE_ID_FAIL";
105         private static final String PARENT_NAME = "PARENT_NAME";
106         private static final String PARENT_ID = "PARENT_ID";
107         private static final String INVARIANT_ID = "INVARIANT_ID";
108         private static final String GROUP_TYPE_FAILING = "GROUP_TYPE_FAILING";
109         private static final String GROUP_ROLE_OK = "GROUP_ROLE_OK";
110         private static final String GROUP_ROLE_FAILING = "GROUP_ROLE_FAILING";
111         private static final String group_type_ok = "GROUP_TYPE_OK";
112         private static final String CLOUD_TYPE = "CLOUD_TYPE";
113
114         @Mock
115         private HttpResponse<SubscriberList> responseAllSubscribers;
116         @Mock
117         private AaiResponse<OperationalEnvironmentList> aaiResponseOpEnvList;
118         @Mock
119         private AaiResponse aaiResponse;
120         @Mock
121         private AaiResponse<JsonNode> aaiResponseJsonNode;
122         @Mock
123         private RoleValidator roleValidator;
124
125         @Mock
126         private AaiClientInterface aaiClient;
127         @Mock
128         private AaiOverTLSClientInterface aaiOverTLSClient;
129         @Mock
130         private AaiResponseTranslator aaiResponseTranslator;
131         @Mock
132         private AAIServiceTree aaiServiceTree;
133
134         @InjectMocks
135         private AaiServiceImpl aaiService;
136
137
138         @Test
139         public void shouldGetFullSubscriberListWithoutValidator() {
140                 when(aaiOverTLSClient.getAllSubscribers()).thenReturn(responseAllSubscribers);
141
142                 HttpResponse<SubscriberList> actualResponse = aaiService.getFullSubscriberList();
143
144                 assertThat(actualResponse).isEqualTo(responseAllSubscribers);
145         }
146
147         @Test
148         public void shouldGetFullSubscriberListWithValidator() {
149                 Subscriber subscriber = createSubscriber();
150                 SubscriberList subscriberList = new SubscriberList(Collections.singletonList(subscriber));
151
152                 when(aaiOverTLSClient.getAllSubscribers()).thenReturn(responseAllSubscribers);
153                 when(responseAllSubscribers.getBody()).thenReturn(subscriberList);
154                 when(responseAllSubscribers.getStatusText()).thenReturn(STATUS_TEXT);
155                 when(responseAllSubscribers.getStatus()).thenReturn(HttpStatus.SC_OK);
156                 SubscriberFilteredResults expectedSubscribers = new SubscriberFilteredResults(roleValidator, subscriberList,
157                                 STATUS_TEXT, HttpStatus.SC_OK);
158
159                 SubscriberFilteredResults actualSubscribers = aaiService.getFullSubscriberList(roleValidator);
160
161                 assertThat(actualSubscribers.getHttpCode()).isEqualTo(expectedSubscribers.getHttpCode());
162                 assertThat(actualSubscribers.getErrorMessage()).isEqualTo(expectedSubscribers.getErrorMessage());
163         }
164
165         @Test
166         public void shouldGetOperationalEnvironments() {
167                 when(aaiClient.getOperationalEnvironments(anyString(), anyString()))
168                                 .thenReturn(aaiResponseOpEnvList);
169
170                 AaiResponse<OperationalEnvironmentList> expectedEnvList =
171                                 aaiService.getOperationalEnvironments(anyString(), anyString());
172
173                 assertThat(expectedEnvList).isEqualTo(aaiResponseOpEnvList);
174         }
175
176         @Test
177         public void shouldGetSubscriberData() {
178                 Services services = createAaiResponseServices();
179                 AaiResponse<Services> aaiResponseServices = new AaiResponse<>(services, null, HttpStatus.SC_OK);
180
181                 when(aaiClient.getSubscriberData(SUBSCRIBER_ID)).thenReturn(aaiResponseServices);
182                 when(roleValidator.isServicePermitted(eq(GLOBAL_CUSTOMER_ID), anyString())).thenReturn(Boolean.TRUE);
183
184                 AaiResponse actualResponse = aaiService.getSubscriberData(SUBSCRIBER_ID, roleValidator);
185                 List<ServiceSubscription> actualServiceSubscriptions = ((AaiResponse<Services>) actualResponse)
186                                 .getT().serviceSubscriptions.serviceSubscription;
187
188                 assertThat(actualResponse).isEqualTo(aaiResponseServices);
189                 assertThat(actualServiceSubscriptions).allMatch(s -> s.isPermitted);
190         }
191
192         @Test
193         public void shouldGetServiceInstanceEmptySearchResults() {
194                 ServiceInstancesSearchResults serviceInstancesSearchResults = new ServiceInstancesSearchResults();
195                 AaiResponse<ServiceInstancesSearchResults> emptyResponse = new AaiResponse<>(serviceInstancesSearchResults,
196                                 null, HttpStatus.SC_OK);
197
198                 AaiResponse actualResponse = aaiService.getServiceInstanceSearchResults(null, null,
199                                 null, null, null);
200
201                 assertThat(actualResponse).isEqualToComparingFieldByFieldRecursively(emptyResponse);
202         }
203
204         @Test
205         public void shouldGetVersionByInvariantId() {
206                 Response response = mock(Response.class);
207                 when(aaiClient.getVersionByInvariantId(any())).thenReturn(response);
208
209                 Response actualResponse = aaiService.getVersionByInvariantId(any());
210
211                 assertThat(actualResponse).isEqualTo(response);
212         }
213
214         @Test
215         public void shouldGetSpecificPnf() {
216                 AaiResponse<Pnf> expectedResponse = new AaiResponse<>(Pnf.builder().build(), null, HttpStatus.SC_OK);
217                 when(aaiClient.getSpecificPnf(anyString())).thenReturn(expectedResponse);
218
219                 AaiResponse<Pnf> actualResponse = aaiService.getSpecificPnf(anyString());
220
221                 assertThat(actualResponse).isEqualTo(expectedResponse);
222         }
223
224         @Test
225         public void shouldGetPnfData() {
226                 when(aaiClient.getPNFData(anyString(), anyString(), anyString(), anyString(), anyString(), anyString(),
227                                 anyString())).thenReturn(aaiResponse);
228
229                 AaiResponse actualResponse = aaiService.getPNFData(anyString(), anyString(), anyString(), anyString(),
230                                 anyString(), anyString(), anyString());
231
232                 assertThat(actualResponse).isEqualTo(aaiResponse);
233         }
234
235         @Test
236         public void shouldGetServices() {
237                 org.onap.vid.aai.model.AaiGetServicesRequestModel.Service s1 =
238                                 createService("ID1", "V1", "D1");
239                 org.onap.vid.aai.model.AaiGetServicesRequestModel.Service s2 =
240                                 createService("ID2", "V2", "D2");
241
242                 GetServicesAAIRespone services = new GetServicesAAIRespone();
243                 services.service = Arrays.asList(s1, s2);
244
245                 AaiResponse<GetServicesAAIRespone> aaiResponseServices =
246                                 new AaiResponse<>(services, null, HttpStatus.SC_OK);
247
248                 when(aaiClient.getServices()).thenReturn(aaiResponseServices);
249
250                 Object actualObjectOfResponse = aaiService.getServices(roleValidator).getT();
251
252                 assertThat(actualObjectOfResponse).isNotNull();
253                 assertThat(((GetServicesAAIRespone) actualObjectOfResponse).service).allMatch(s -> s.isPermitted);
254         }
255
256         @Test
257         public void shouldGetTenants() {
258                 GetTenantsResponse tenant1 = new GetTenantsResponse("REGION_ID1", "CLOUD_OWNER1",
259                                 "TENANT_NAME1", "TENANT_ID1", true);
260                 GetTenantsResponse tenant2 = new GetTenantsResponse("REGION_ID2", "CLOUD_OWNER2",
261                                 "TENANT_NAME2", "TENANT_ID2", false);
262                 GetTenantsResponse[] tenants = {tenant1, tenant2};
263                 AaiResponse<GetTenantsResponse[]> aaiGetTenantsResponse = new AaiResponse<>(tenants,
264                                 null, HttpStatus.SC_OK);
265
266                 when(aaiClient.getTenants(anyString(), anyString())).thenReturn(aaiGetTenantsResponse);
267                 when(roleValidator.isTenantPermitted(anyString(), anyString(), anyString()))
268                                 .thenReturn(Boolean.TRUE);
269
270                 GetTenantsResponse[] actualResponses = aaiService
271                                 .getTenants(anyString(), anyString(), roleValidator).getT();
272
273                 assertThat(actualResponses).isNotNull();
274                 assertThat(actualResponses.length).isEqualTo(2);
275                 assertThat(actualResponses).allMatch(tenant -> tenant.isPermitted);
276         }
277
278         @Test
279         public void shouldGetVNFDataWithoutFiltering() {
280                 when(aaiClient.getVNFData(anyString(), anyString(), anyString())).thenReturn(aaiResponse);
281
282                 AaiResponse actualResponse = aaiService.getVNFData(anyString(), anyString(), anyString());
283
284                 assertThat(actualResponse).isEqualTo(aaiResponse);
285         }
286
287         @Test
288         public void shouldGetVNFDataWithFiltering() {
289                 VnfResult vnfResult1 = createVnfResult("ID1", "generic-vnf");
290                 VnfResult vnfResult2 = createVnfResult("ID2", "service-instance");
291                 VnfResult vnfResult3 = createVnfResult("ID3", "anything-else");
292
293                 List<VnfResult> vnfResults = Arrays.asList(vnfResult1, vnfResult2, vnfResult3);
294                 AaiResponse<AaiGetVnfResponse> aaiResponseGetVnfResponse = createAaiResponseVnfResponse(vnfResults);
295
296                 vnfResults = Arrays.asList(vnfResult1, vnfResult2);
297                 AaiResponse<AaiGetVnfResponse> expectedResponseWithReturnedVnfs = createAaiResponseVnfResponse(vnfResults);
298                 AaiResponse expectedResponseWithoutReturnedVnfs = new AaiResponse();
299
300                 when(aaiClient.getVNFData(GLOBAL_SUBSCRIBER_ID, SERVICE_TYPE)).thenReturn(aaiResponseGetVnfResponse);
301                 when(aaiClient.getVNFData(GLOBAL_SUBSCRIBER_ID_NULL_RESPONSE, SERVICE_TYPE)).thenReturn(null);
302
303                 AaiResponse<AaiGetVnfResponse> actualResponseWithReturnedVnfs =
304                                 aaiService.getVNFData(GLOBAL_SUBSCRIBER_ID, SERVICE_TYPE);
305                 AaiResponse<AaiGetVnfResponse> actualResponseWithoutReturnedVnfs =
306                                 aaiService.getVNFData(GLOBAL_SUBSCRIBER_ID_NULL_RESPONSE, SERVICE_TYPE);
307
308                 assertThat(actualResponseWithReturnedVnfs)
309                                 .isEqualToComparingFieldByFieldRecursively(expectedResponseWithReturnedVnfs);
310                 assertThat(actualResponseWithoutReturnedVnfs)
311                                 .isEqualToComparingFieldByField(expectedResponseWithoutReturnedVnfs);
312         }
313
314         @Test
315         public void shouldGetAaiZones() {
316                 when(aaiClient.getAllAicZones()).thenReturn(aaiResponse);
317
318                 AaiResponse actualResponse = aaiService.getAaiZones();
319
320                 assertThat(actualResponse).isEqualTo(aaiResponse);
321         }
322
323         @Test
324         public void shouldGetAicZoneForPnf() {
325                 ServiceRelationships relationsService = createServiceRelationships();
326                 AaiResponse<ServiceRelationships> expectedServiceInstanceResp =
327                                 new AaiResponse<>(relationsService, null, HttpStatus.SC_OK);
328                 AaiResponse<String> expectedResponse = new AaiResponse<>(CORRECT_VALUE, null, HttpStatus.SC_OK);
329
330                 when(aaiClient.getServiceInstance(anyString(), anyString(), anyString()))
331                                 .thenReturn(expectedServiceInstanceResp);
332
333                 AaiResponse actualResponse = aaiService.getAicZoneForPnf(anyString(), anyString(), anyString());
334
335                 assertThat(actualResponse).isEqualToComparingFieldByField(expectedResponse);
336         }
337
338         @Test
339         public void shouldGetNodeTemplateInstances() {
340                 when(aaiClient.getNodeTemplateInstances(anyString(), anyString(), anyString(),
341                                 anyString(), anyString())).thenReturn(aaiResponse);
342
343                 AaiResponse expectedResponse = aaiService.getNodeTemplateInstances(anyString(), anyString(), anyString(),
344                                 anyString(), anyString());
345
346                 assertThat(expectedResponse).isEqualTo(aaiResponse);
347         }
348
349         @Test
350         public void shouldGetNetworkCollectionDetails() {
351                 when(aaiClient.getNetworkCollectionDetails(anyString())).thenReturn(aaiResponse);
352
353                 AaiResponse expectedResponse = aaiService.getNetworkCollectionDetails(anyString());
354
355                 assertThat(expectedResponse).isEqualTo(aaiResponse);
356         }
357
358         @Test
359         public void shouldGetInstanceGroupsByCloudRegion() {
360                 AaiGetInstanceGroupsByCloudRegion aaiGetInstanceGroupsByCloudRegion =
361                                 mock(AaiGetInstanceGroupsByCloudRegion.class);
362                 AaiResponse<AaiGetInstanceGroupsByCloudRegion> expectedResponse =
363                                 new AaiResponse<>(aaiGetInstanceGroupsByCloudRegion, null, HttpStatus.SC_OK);
364
365                 when(aaiClient.getInstanceGroupsByCloudRegion(anyString(), anyString(), anyString()))
366                                 .thenReturn(expectedResponse);
367                 AaiResponse<AaiGetInstanceGroupsByCloudRegion> actualResponse =
368                                 aaiService.getInstanceGroupsByCloudRegion(anyString(), anyString(), anyString());
369
370                 assertThat(actualResponse).isEqualTo(expectedResponse);
371         }
372
373         @Test
374         public void shouldGetServicesByDistributionStatus() {
375                 Result resultWithModelType = createResult("MODEL_TYPE1", "1");
376                 Result resultWithEmptyModelType = createResult(null, "2");
377                 Result resultWithoutModel = new Result();
378                 resultWithoutModel.setModel(null);
379                 Result resultWithoutValidModel = createResultWithoutValidModel();
380                 List<Result> results = Arrays.asList(resultWithModelType, resultWithEmptyModelType, resultWithoutModel,
381                                 resultWithoutValidModel);
382
383                 GetServiceModelsByDistributionStatusResponse serviceModels = new GetServiceModelsByDistributionStatusResponse();
384                 serviceModels.setResults(results);
385
386                 AaiResponse<GetServiceModelsByDistributionStatusResponse> serviceModelsByDistributionStatusResponse
387                                 = new AaiResponse<>(serviceModels, null, HttpStatus.SC_OK);
388
389                 Service[] expectedServices = {
390                                 createService("MODEL_TYPE1", "1"),
391                                 createService("", "2")
392                 };
393
394                 when(aaiClient.getServiceModelsByDistributionStatus()).thenReturn(serviceModelsByDistributionStatusResponse);
395                 Collection<Service> actualServices = aaiService.getServicesByDistributionStatus();
396
397                 assertThat(actualServices)
398                                 .hasSize(2)
399                                 .usingFieldByFieldElementComparator()
400                                 .containsExactly(expectedServices);
401         }
402
403         @Test
404         public void shouldReturnEmptyListOfServices() {
405                 AaiResponse<GetServiceModelsByDistributionStatusResponse> emptyResponse
406                                 = new AaiResponse<>(null, null, HttpStatus.SC_OK);
407
408                 when(aaiClient.getServiceModelsByDistributionStatus()).thenReturn(emptyResponse);
409                 Collection<Service> actualServices = aaiService.getServicesByDistributionStatus();
410
411                 assertThat(actualServices).isEqualTo(Collections.EMPTY_LIST);
412         }
413
414         @Test
415         public void shouldGetServiceInstanceAssociatedPnfs() {
416                 ServiceRelationships relationsList = createServiceRelationships();
417                 LogicalLinkResponse logicalLinkResponse = new LogicalLinkResponse();
418                 logicalLinkResponse.setRelationshipList(relationsList.getRelationshipList());
419
420                 AaiResponse<LogicalLinkResponse> aaiResponseLogicalLinkResponse =
421                                 new AaiResponse<>(logicalLinkResponse, null, HttpStatus.SC_OK);
422                 AaiResponse<ServiceRelationships> aaiResponseServiceRelations =
423                                 new AaiResponse<>(relationsList, null, HttpStatus.SC_OK);
424
425                 when(aaiClient.getServiceInstance(anyString(), anyString(), anyString()))
426                                 .thenReturn(aaiResponseServiceRelations);
427                 when(aaiClient.getLogicalLink(anyString())).thenReturn(aaiResponseLogicalLinkResponse);
428
429
430                 List<String> expectedPnfs = Collections.singletonList(CORRECT_VALUE);
431                 List<String> actualPnfs = aaiService.getServiceInstanceAssociatedPnfs(anyString(), anyString(), anyString());
432
433                 assertThat(actualPnfs).isEqualTo(expectedPnfs);
434         }
435
436         @Test
437         public void shouldGetPortMirroringConfigData() {
438                 AaiResponseTranslator.PortMirroringConfigData expectedData
439                                 = mock(AaiResponseTranslator.PortMirroringConfigData.class);
440
441                 when(aaiClient.getCloudRegionAndSourceByPortMirroringConfigurationId(anyString())).thenReturn(aaiResponseJsonNode);
442                 when(aaiResponseTranslator.extractPortMirroringConfigData(aaiResponseJsonNode)).thenReturn(expectedData);
443
444                 AaiResponseTranslator.PortMirroringConfigData actualData = aaiService.getPortMirroringConfigData(anyString());
445                 assertThat(actualData).isEqualTo(expectedData);
446         }
447
448
449         @Test
450         public void shouldGetInstanceGroupsByVnfInstanceId() {
451                 List<InstanceGroupInfo> instanceGroupInfo = Collections.singletonList(new InstanceGroupInfo(CORRECT_VALUE));
452                 AaiGetRelatedInstanceGroupsByVnfId relatedInstanceGroups = new AaiGetRelatedInstanceGroupsByVnfId();
453                 relatedInstanceGroups.setRelationshipList(createRelationshipList());
454
455                 AaiResponse<AaiGetRelatedInstanceGroupsByVnfId> correctCodeResponse =
456                                 new AaiResponse<>(relatedInstanceGroups, null, HttpStatus.SC_OK);
457
458                 AaiResponse<List<InstanceGroupInfo>> expectedCorrectCodeResponse =
459                                 new AaiResponse<>(instanceGroupInfo, null, HttpStatus.SC_OK);
460                 AaiResponse<AaiGetRelatedInstanceGroupsByVnfId> expectedIncorrectCodeResponse =
461                                 new AaiResponse<>(relatedInstanceGroups, null, HttpStatus.SC_PAYMENT_REQUIRED);
462                 List<InstanceGroupInfo> expectedCorrectResponseObject = expectedCorrectCodeResponse.getT();
463
464                 when(aaiClient.getInstanceGroupsByVnfInstanceId(VNF_INSTANCE_ID_OK)).thenReturn(correctCodeResponse);
465                 when(aaiClient.getInstanceGroupsByVnfInstanceId(VNF_INSTANCE_ID_FAIL)).thenReturn(expectedIncorrectCodeResponse);
466
467                 AaiResponse actualCorrectCodeResponse = aaiService.getInstanceGroupsByVnfInstanceId(VNF_INSTANCE_ID_OK);
468                 AaiResponse actualIncorrectCodeResponse = aaiService.getInstanceGroupsByVnfInstanceId(VNF_INSTANCE_ID_FAIL);
469
470                 List<InstanceGroupInfo> actualCorrectResponseObject =
471                                 (List<InstanceGroupInfo>) actualCorrectCodeResponse.getT();
472
473                 assertThat(actualCorrectResponseObject)
474                                 .usingFieldByFieldElementComparator()
475                                 .hasSameElementsAs(expectedCorrectResponseObject);
476
477                 assertThat(actualIncorrectCodeResponse).isEqualTo(expectedIncorrectCodeResponse);
478         }
479
480         @Test
481         public void shouldGetHomingDataByVfModule() {
482                 GetTenantsResponse expectedResponse = new GetTenantsResponse();
483                 when(aaiClient.getHomingDataByVfModule(anyString(), anyString())).thenReturn(expectedResponse);
484
485                 GetTenantsResponse actualResponse = aaiService.getHomingDataByVfModule(anyString(), anyString());
486                 assertThat(actualResponse).isEqualTo(expectedResponse);
487         }
488
489         @Test
490         public void shouldSearchGroupMembers() {
491                 Properties properties = createProperties();
492                 Map<String, Properties> regionsAndTenants = createRegionsAndTenantsMap(properties);
493
494                 AAITreeNode validTreeNode = new AAITreeNode();
495                 addAdditionalPropertiesToAaiTreeNode(validTreeNode);
496                 List<AAITreeNode> validNodes = Arrays.asList(validTreeNode, validTreeNode);
497
498                 AAITreeNode validBranch = createTree(validNodes);
499                 addAdditionalPropertiesToAaiTreeNode(validBranch);
500                 List<AAITreeNode> testedBranches = Collections.singletonList(validBranch);
501
502                 AAITreeNode testedTree = createTree(testedBranches);
503
504                 RelatedVnf expectedVnf = createExpectedVnf(validBranch);
505                 List<RelatedVnf> expectedResult = Collections.singletonList(expectedVnf);
506
507                 when(aaiServiceTree.buildAAITree(anyString(), any())).thenReturn(Collections.singletonList(testedTree));
508                 when(aaiClient.getCloudRegionAndTenantByVnfId(anyString())).thenReturn(regionsAndTenants);
509
510                 List<RelatedVnf> actualGroupMembers = aaiService.searchGroupMembers(GLOBAL_CUSTOMER_ID, SERVICE_TYPE,
511                                 INVARIANT_ID, GROUP_TYPE_FAILING, GROUP_ROLE_FAILING);
512
513                 assertThat(actualGroupMembers)
514                                 .usingFieldByFieldElementComparator()
515                                 .hasSameElementsAs(expectedResult);
516         }
517
518         @Test
519         public void shouldGetPortMirroringSourcePorts() {
520                 PortDetailsTranslator.PortDetails details = mock(PortDetailsTranslator.PortDetails.class);
521                 List<PortDetailsTranslator.PortDetails> expectedDetailsList = Arrays.asList(
522                                 details, details, details
523                 );
524
525                 when(aaiClient.getPortMirroringSourcePorts(anyString())).thenReturn(expectedDetailsList);
526                 List<PortDetailsTranslator.PortDetails> actualDetails = aaiService.getPortMirroringSourcePorts(anyString());
527
528                 assertThat(actualDetails).isEqualTo(expectedDetailsList);
529         }
530
531         @Test
532         public void shouldGetAAIServiceTree() throws JsonProcessingException {
533                 ServiceInstance serviceInstance = mock(ServiceInstance.class);
534                 String expectedResult = new ObjectMapper().writeValueAsString(serviceInstance);
535
536                 when(aaiServiceTree.getServiceInstanceTopology(anyString(), anyString(), anyString()))
537                                 .thenReturn(serviceInstance);
538                 String actualResult = aaiService.getAAIServiceTree(anyString(), anyString(), anyString());
539
540                 assertThat(actualResult).isEqualTo(expectedResult);
541         }
542
543         @NotNull
544         private Map<String, Properties> createRegionsAndTenantsMap(Properties properties) {
545                 Map<String, Properties> regionsAndTenants = new HashMap<>();
546                 regionsAndTenants.put("tenant", properties);
547                 regionsAndTenants.put("cloud-region", properties);
548                 return regionsAndTenants;
549         }
550
551         private Properties createProperties() {
552                 Properties properties = new Properties();
553                 properties.setTenantId(TENANT_ID);
554                 properties.setTenantName(TENANT_NAME);
555                 properties.setCloudRegionId(CLOUD_REGION_ID);
556                 return properties;
557         }
558
559         @NotNull
560         private RelatedVnf createExpectedVnf(AAITreeNode validBranch) {
561                 RelatedVnf expectedVnf = RelatedVnf.from(validBranch);
562                 expectedVnf.setTenantId(TENANT_ID);
563                 expectedVnf.setTenantName(TENANT_NAME);
564                 expectedVnf.setLcpCloudRegionId(CLOUD_REGION_ID);
565                 expectedVnf.setServiceInstanceId(PARENT_ID);
566                 expectedVnf.setServiceInstanceName(PARENT_NAME);
567                 expectedVnf.setInstanceType(VNF_TYPE);
568
569                 return expectedVnf;
570         }
571
572
573         private AAITreeNode createTree(List<AAITreeNode> children) {
574                 AAITreeNode tree = new AAITreeNode();
575                 tree.addChildren(children);
576                 tree.setId(PARENT_ID);
577                 tree.setName(PARENT_NAME);
578                 return tree;
579         }
580
581         private void addAdditionalPropertiesToAaiTreeNode(AAITreeNode tree) {
582                 Map<String, Object> additionalProperties = new HashMap<>();
583                 additionalProperties.put("instance-group-role", GROUP_ROLE_OK);
584                 additionalProperties.put("instance-group-type", group_type_ok);
585                 additionalProperties.put("vnf-type", VNF_TYPE);
586                 additionalProperties.put("cloud-region", CLOUD_TYPE);
587                 tree.setAdditionalProperties(additionalProperties);
588         }
589
590         private org.onap.vid.asdc.beans.Service createService(String category, String suffix) {
591                 return new Service.ServiceBuilder()
592                                 .setUuid("MODELVER_VERSION_ID" + suffix)
593                                 .setInvariantUUID("MODEL_INVARIANT_NAME" + suffix)
594                                 .setCategory(category)
595                                 .setVersion("MODELVER_VERSION" + suffix)
596                                 .setName("MODELVER_NAME" + suffix)
597                                 .setDistributionStatus("MODELVER_DIST_STATUS" + suffix)
598                                 .setToscaModelURL(null)
599                                 .setLifecycleState(null)
600                                 .setArtifacts(null)
601                                 .setResources(null)
602                                 .build();
603         }
604
605         @NotNull
606         private Result createResultWithoutValidModel() {
607                 ModelVers modelVers = new ModelVers();
608                 modelVers.setModelVer(Collections.singletonList(new ModelVer()));
609
610                 Model model = new Model();
611                 model.setModelVers(modelVers);
612
613                 Result result1 = new Result();
614                 result1.setModel(model);
615                 return result1;
616         }
617
618         @NotNull
619         private Result createResult(String modelType, String suffix) {
620                 ModelVer modelVer = new ModelVer();
621                 modelVer.setModelVersionId("MODELVER_VERSION_ID" + suffix);
622                 modelVer.setModelVersion("MODELVER_VERSION" + suffix);
623                 modelVer.setModelName("MODELVER_NAME" + suffix);
624                 modelVer.setDistributionStatus("MODELVER_DIST_STATUS" + suffix);
625
626                 ModelVers modelVers = new ModelVers();
627                 modelVers.setModelVer(Collections.singletonList(modelVer));
628
629                 Model model = new Model();
630                 model.setModelType(modelType);
631                 model.setModelInvariantId("MODEL_INVARIANT_NAME" + suffix);
632                 model.setModelVers(modelVers);
633
634                 Result result = new Result();
635                 result.setModel(model);
636                 return result;
637         }
638
639         @NotNull
640         private ServiceRelationships createServiceRelationships() {
641                 RelationshipList relationsList = createRelationshipList(CORRECT_VALUE);
642                 ServiceRelationships relationsService = new ServiceRelationships();
643                 relationsService.setRelationshipList(relationsList);
644                 return relationsService;
645         }
646
647         @NotNull
648         private RelationshipList createRelationshipList(String expectedValue) {
649                 List<RelationshipData> relationsDataList = createRelationshipDataList(expectedValue);
650                 return createRelationshipList(relationsDataList);
651         }
652
653         @NotNull
654         private RelationshipList createRelationshipList(List<RelationshipData> relationsDataList) {
655                 Relationship relation1 = crateRelationship("any", relationsDataList);
656                 Relationship relation2 = crateRelationship("zone", relationsDataList);
657                 Relationship relation3 = crateRelationship("logical-link", relationsDataList);
658                 Relationship relation4 = crateRelationship("lag-interface", relationsDataList);
659                 Relationship relation5 = crateRelationship("pnf", relationsDataList);
660
661                 RelationshipList relationsList = new RelationshipList();
662                 relationsList.setRelationship(Arrays.asList(relation1, relation2, relation3, relation4, relation5));
663                 return relationsList;
664         }
665
666         @NotNull
667         private List<RelationshipData> createRelationshipDataList(String expectedValue) {
668                 RelationshipData relationData1 = createRelationshipData("any-key", "incorrect_key");
669                 RelationshipData relationData2 = createRelationshipData("zone.zone-id", expectedValue);
670                 RelationshipData relationData3 = createRelationshipData("logical-link.link-name", expectedValue);
671                 RelationshipData relationData4 = createRelationshipData("pnf.pnf-name", expectedValue);
672
673                 return Arrays.asList(relationData1, relationData2, relationData3, relationData4);
674         }
675
676         @NotNull
677         private Relationship crateRelationship(String relatedTo, List<RelationshipData> relationsDataList) {
678                 Relationship relation = new Relationship();
679                 relation.setRelatedTo(relatedTo);
680                 relation.setRelationDataList(relationsDataList);
681                 return relation;
682         }
683
684         @NotNull
685         private RelationshipData createRelationshipData(String key, String value) {
686                 RelationshipData relationData = new RelationshipData();
687                 relationData.setRelationshipKey(key);
688                 relationData.setRelationshipValue(value);
689                 return relationData;
690         }
691
692         private org.onap.vid.aai.model.AaiGetNetworkCollectionDetails.RelationshipList createRelationshipList() {
693                 RelatedToProperty property1 =
694                                 createRelatedToProperty("instance-group.instance-group-name", CORRECT_VALUE);
695                 RelatedToProperty property2 =
696                                 createRelatedToProperty("anything-key", "anything-value");
697                 List<RelatedToProperty> properties = Arrays.asList(property1, property2);
698
699                 org.onap.vid.aai.model.AaiGetNetworkCollectionDetails.Relationship relationship1 =
700                                 createRelationship("instance-group", properties);
701                 org.onap.vid.aai.model.AaiGetNetworkCollectionDetails.Relationship relationship2 =
702                                 createRelationship("any-key", properties);
703
704                 List<org.onap.vid.aai.model.AaiGetNetworkCollectionDetails.Relationship> relationships =
705                                 Arrays.asList(relationship1, relationship2);
706
707                 org.onap.vid.aai.model.AaiGetNetworkCollectionDetails.RelationshipList relationshipList =
708                                 new org.onap.vid.aai.model.AaiGetNetworkCollectionDetails.RelationshipList();
709                 relationshipList.setRelationship(relationships);
710
711                 return relationshipList;
712         }
713
714         @NotNull
715         private org.onap.vid.aai.model.AaiGetNetworkCollectionDetails.Relationship createRelationship(String relatedTo,
716                                                                                                       List<RelatedToProperty> relatedToPropertyList) {
717                 org.onap.vid.aai.model.AaiGetNetworkCollectionDetails.Relationship relationship1 =
718                                 new org.onap.vid.aai.model.AaiGetNetworkCollectionDetails.Relationship();
719                 relationship1.setRelatedTo(relatedTo);
720                 relationship1.setRelatedToPropertyList(relatedToPropertyList);
721                 return relationship1;
722         }
723
724         @NotNull
725         private RelatedToProperty createRelatedToProperty(String key, String value) {
726                 RelatedToProperty prop = new RelatedToProperty();
727                 prop.setPropertyKey(key);
728                 prop.setPropertyValue(value);
729                 return prop;
730         }
731
732         @NotNull
733         private AaiResponse<AaiGetVnfResponse> createAaiResponseVnfResponse(List<VnfResult> vnfResults) {
734                 AaiGetVnfResponse vnfResponse = new AaiGetVnfResponse();
735                 vnfResponse.setResults(vnfResults);
736                 return new AaiResponse<>(vnfResponse, null, HttpStatus.SC_OK);
737         }
738
739         private VnfResult createVnfResult(String id, String nodeType) {
740                 VnfResult result = new VnfResult();
741                 result.setJsonId(id);
742                 result.setJsonNodeType(nodeType);
743                 return result;
744         }
745
746
747         private org.onap.vid.aai.model.AaiGetServicesRequestModel.Service createService(String serviceId,
748                                                                                         String resourceVersion,
749                                                                                         String serviceDescription) {
750                 org.onap.vid.aai.model.AaiGetServicesRequestModel.Service service
751                                 = new org.onap.vid.aai.model.AaiGetServicesRequestModel.Service();
752                 service.isPermitted = false;
753                 service.resourceVersion = resourceVersion;
754                 service.serviceDescription = serviceDescription;
755                 service.serviceId = serviceId;
756                 return service;
757         }
758
759         @NotNull
760         private Services createAaiResponseServices() {
761                 ServiceSubscription sub1 = new ServiceSubscription();
762                 sub1.isPermitted = false;
763                 sub1.serviceType = "serviceSubsType1";
764
765                 ServiceSubscription sub2 = new ServiceSubscription();
766                 sub2.isPermitted = true;
767                 sub2.serviceType = "serviceSubsType2";
768
769                 ServiceSubscriptions serviceSubs = new ServiceSubscriptions();
770                 serviceSubs.serviceSubscription = Collections.singletonList(sub2);
771
772                 Services services = new Services();
773                 services.globalCustomerId = GLOBAL_CUSTOMER_ID;
774                 services.resourceVersion = "v-1";
775                 services.subscriberName = "name-1";
776                 services.subscriberType = "type-1";
777                 services.serviceSubscriptions = serviceSubs;
778                 return services;
779         }
780
781         @NotNull
782         private Subscriber createSubscriber() {
783                 Subscriber subscriber = new Subscriber();
784                 subscriber.globalCustomerId = "id-1";
785                 subscriber.resourceVersion = "v-1";
786                 subscriber.subscriberName = "name-1";
787                 subscriber.subscriberType = "type-1";
788                 return subscriber;
789         }
790 }