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