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