Heatbridge Updates
[so.git] / bpmn / MSOCommonBPMN / src / test / java / org / onap / so / bpmn / servicedecomposition / tasks / BBInputSetupUtilsTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Modifications Copyright (C) 2020 Nokia
8  * ================================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.so.bpmn.servicedecomposition.tasks;
24
25 import static com.shazam.shazamcrest.MatcherAssert.assertThat;
26 import static com.shazam.shazamcrest.matcher.Matchers.sameBeanAs;
27 import static org.junit.Assert.assertEquals;
28 import static org.junit.Assert.assertNull;
29 import static org.junit.Assert.assertTrue;
30 import static org.mockito.ArgumentMatchers.any;
31 import static org.mockito.ArgumentMatchers.anyString;
32 import static org.mockito.ArgumentMatchers.eq;
33 import static org.mockito.ArgumentMatchers.isA;
34 import static org.mockito.Mockito.doReturn;
35 import static org.mockito.Mockito.times;
36 import static org.mockito.Mockito.verify;
37 import java.io.File;
38 import java.io.IOException;
39 import java.nio.charset.StandardCharsets;
40 import java.nio.file.Files;
41 import java.nio.file.Paths;
42 import java.util.Arrays;
43 import java.util.List;
44 import java.util.Optional;
45 import org.junit.Before;
46 import org.junit.Rule;
47 import org.junit.Test;
48 import org.junit.rules.ExpectedException;
49 import org.junit.runner.RunWith;
50 import org.mockito.InjectMocks;
51 import org.mockito.Mock;
52 import org.mockito.Mockito;
53 import org.mockito.junit.MockitoJUnitRunner;
54 import org.onap.aai.domain.yang.CloudRegion;
55 import org.onap.aai.domain.yang.Configuration;
56 import org.onap.aai.domain.yang.Configurations;
57 import org.onap.aai.domain.yang.GenericVnf;
58 import org.onap.aai.domain.yang.GenericVnfs;
59 import org.onap.aai.domain.yang.InstanceGroup;
60 import org.onap.aai.domain.yang.L3Network;
61 import org.onap.aai.domain.yang.L3Networks;
62 import org.onap.aai.domain.yang.ServiceInstance;
63 import org.onap.aai.domain.yang.ServiceInstances;
64 import org.onap.aai.domain.yang.VolumeGroup;
65 import org.onap.aai.domain.yang.VolumeGroups;
66 import org.onap.aai.domain.yang.VpnBinding;
67 import org.onap.aaiclient.client.aai.AAIObjectPlurals;
68 import org.onap.aaiclient.client.aai.AAIObjectType;
69 import org.onap.aaiclient.client.aai.AAIResourcesClient;
70 import org.onap.aaiclient.client.aai.entities.uri.AAIPluralResourceUri;
71 import org.onap.aaiclient.client.aai.entities.uri.AAIResourceUri;
72 import org.onap.aaiclient.client.aai.entities.uri.AAIUriFactory;
73 import org.onap.aaiclient.client.graphinventory.entities.uri.Depth;
74 import org.onap.so.bpmn.common.InjectionHelper;
75 import org.onap.so.bpmn.servicedecomposition.bbobjects.Customer;
76 import org.onap.so.bpmn.servicedecomposition.bbobjects.ServiceSubscription;
77 import org.onap.so.bpmn.servicedecomposition.entities.ExecuteBuildingBlock;
78 import org.onap.so.bpmn.servicedecomposition.tasks.exceptions.MultipleObjectsFoundException;
79 import org.onap.so.bpmn.servicedecomposition.tasks.exceptions.NoServiceInstanceFoundException;
80 import org.onap.so.db.catalog.beans.CollectionResourceInstanceGroupCustomization;
81 import org.onap.so.db.catalog.beans.Service;
82 import org.onap.so.db.catalog.beans.VnfcInstanceGroupCustomization;
83 import org.onap.so.db.catalog.client.CatalogDbClient;
84 import org.onap.so.db.request.beans.InfraActiveRequests;
85 import org.onap.so.db.request.beans.RequestProcessingData;
86 import org.onap.so.db.request.client.RequestsDbClient;
87 import org.onap.so.serviceinstancebeans.CloudConfiguration;
88 import org.onap.so.serviceinstancebeans.RequestDetails;
89 import com.fasterxml.jackson.databind.DeserializationFeature;
90 import com.fasterxml.jackson.databind.ObjectMapper;
91
92 @RunWith(MockitoJUnitRunner.class)
93 public class BBInputSetupUtilsTest {
94     protected ObjectMapper mapper =
95             new ObjectMapper().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);;
96     private static final String RESOURCE_PATH = "src/test/resources/__files/ExecuteBuildingBlock/";
97
98     @InjectMocks
99     BBInputSetupUtils bbInputSetupUtils = new BBInputSetupUtils();
100
101     @Mock
102     protected CatalogDbClient MOCK_catalogDbClient;
103
104     @Mock
105     protected RequestsDbClient MOCK_requestsDbClient;
106
107     @Mock
108     protected AAIResourcesClient MOCK_aaiResourcesClient;
109
110     @Mock
111     protected InjectionHelper MOCK_injectionHelper;
112
113     @Rule
114     public ExpectedException expectedException = ExpectedException.none();
115
116     @Before
117     public void setup() {
118         doReturn(MOCK_aaiResourcesClient).when(MOCK_injectionHelper).getAaiClient();
119     }
120
121     @Test
122     public void getCatalogServiceByModelUUIDTest() throws IOException {
123         Service expected = mapper.readValue(new File(RESOURCE_PATH + "CatalogServiceExpected.json"), Service.class);
124         final String modelUUID = "modelUUIDTest";
125
126         doReturn(expected).when(MOCK_catalogDbClient).getServiceByID(modelUUID);
127
128         assertThat(bbInputSetupUtils.getCatalogServiceByModelUUID(modelUUID), sameBeanAs(expected));
129     }
130
131     @Test
132     public void getCatalogServiceByModelVersionAndModelInvariantUUIDTest() throws IOException {
133         final String modelVersion = "modelVersionTest";
134         final String modelInvariantUUID = "modelInvariantUUIDTest";
135         Service expectedService =
136                 mapper.readValue(new File(RESOURCE_PATH + "CatalogServiceExpected.json"), Service.class);
137
138         doReturn(expectedService).when(MOCK_catalogDbClient).getServiceByModelVersionAndModelInvariantUUID(modelVersion,
139                 modelInvariantUUID);
140
141         assertThat(bbInputSetupUtils.getCatalogServiceByModelVersionAndModelInvariantUUID(modelVersion,
142                 modelInvariantUUID), sameBeanAs(expectedService));
143     }
144
145     @Test
146     public void getVnfcInstanceGroupsTest() throws IOException {
147         final String modelCustomizationUUID = "modelCustomizationUUIDTest";
148         VnfcInstanceGroupCustomization vnfc = mapper.readValue(
149                 new File(RESOURCE_PATH + "VnfcInstanceGroupCustomization.json"), VnfcInstanceGroupCustomization.class);
150
151         doReturn(Arrays.asList(vnfc)).when(MOCK_catalogDbClient)
152                 .getVnfcInstanceGroupsByVnfResourceCust(modelCustomizationUUID);
153
154         assertThat(bbInputSetupUtils.getVnfcInstanceGroups(modelCustomizationUUID), sameBeanAs(Arrays.asList(vnfc)));
155     }
156
157     @Test
158     public void getRequestDetailsTest() throws IOException {
159         final String requestId = "requestId";
160         InfraActiveRequests infraActiveRequest = mapper
161                 .readValue(new File(RESOURCE_PATH + "InfraActiveRequestExpected.json"), InfraActiveRequests.class);
162         RequestDetails expected =
163                 mapper.readValue(new File(RESOURCE_PATH + "RequestDetailsExpected.json"), RequestDetails.class);
164
165         doReturn(infraActiveRequest).when(MOCK_requestsDbClient).getInfraActiveRequestbyRequestId(requestId);
166
167         assertThat(bbInputSetupUtils.getRequestDetails(requestId), sameBeanAs(expected));
168     }
169
170     @Test
171     public void getRequestDetailsNullTest() throws IOException {
172         assertNull(bbInputSetupUtils.getRequestDetails(""));
173     }
174
175     @Test
176     public void getCloudRegionTest() {
177         CloudConfiguration cloudConfig = new CloudConfiguration();
178         cloudConfig.setLcpCloudRegionId("lcpCloudRegionId");
179         Optional<CloudRegion> expected = Optional.of(new CloudRegion());
180
181         doReturn(expected).when(MOCK_aaiResourcesClient).get(CloudRegion.class,
182                 AAIUriFactory.createResourceUri(AAIObjectType.CLOUD_REGION, cloudConfig.getCloudOwner(),
183                         cloudConfig.getLcpCloudRegionId()).depth(Depth.TWO));
184
185         assertThat(bbInputSetupUtils.getCloudRegion(cloudConfig), sameBeanAs(expected.get()));
186     }
187
188     @Test
189     public void getCloudRegionNullTest() {
190         CloudConfiguration cloudConfig = new CloudConfiguration();
191         cloudConfig.setLcpCloudRegionId("lcpCloudRegionId");
192
193         assertNull(bbInputSetupUtils.getCloudRegion(cloudConfig));
194     }
195
196     @Test
197     public void getCloudRegionEmptyIdTest() {
198         CloudConfiguration cloudConfig = new CloudConfiguration();
199         cloudConfig.setLcpCloudRegionId("");
200
201         assertNull(bbInputSetupUtils.getCloudRegion(cloudConfig));
202     }
203
204     @Test
205     public void getAAIInstanceGroupTest() {
206         final String instanceGroupId = "instanceGroupId";
207         Optional<InstanceGroup> expected = Optional.of(new InstanceGroup());
208         expected.get().setId(instanceGroupId);
209
210         doReturn(expected).when(MOCK_aaiResourcesClient).get(InstanceGroup.class,
211                 AAIUriFactory.createResourceUri(AAIObjectType.INSTANCE_GROUP, instanceGroupId));
212
213         assertThat(bbInputSetupUtils.getAAIInstanceGroup(instanceGroupId), sameBeanAs(expected.get()));
214     }
215
216     @Test
217     public void getAAIInstanceGroupNullTest() {
218         assertNull(bbInputSetupUtils.getAAIInstanceGroup(""));
219     }
220
221     @Test
222     public void getAAICustomerTest() {
223         final String globalSubscriberId = "globalSubscriberId";
224         Optional<org.onap.aai.domain.yang.Customer> expected = Optional.of(new org.onap.aai.domain.yang.Customer());
225         expected.get().setGlobalCustomerId(globalSubscriberId);
226
227         doReturn(expected).when(MOCK_aaiResourcesClient).get(org.onap.aai.domain.yang.Customer.class,
228                 AAIUriFactory.createResourceUri(AAIObjectType.CUSTOMER, globalSubscriberId));
229
230         assertThat(bbInputSetupUtils.getAAICustomer(globalSubscriberId), sameBeanAs(expected.get()));
231     }
232
233     @Test
234     public void getAAICustomerNullTest() {
235         assertNull(bbInputSetupUtils.getAAICustomer(""));
236     }
237
238     @Test
239     public void getAAIServiceSubscriptionTest() {
240         final String globalSubscriberId = "globalSubscriberId";
241         final String subscriptionServiceType = "subscriptionServiceType";
242         Optional<org.onap.aai.domain.yang.ServiceSubscription> expected =
243                 Optional.of(new org.onap.aai.domain.yang.ServiceSubscription());
244
245         expected.get().setServiceType(subscriptionServiceType);
246         doReturn(expected).when(MOCK_aaiResourcesClient).get(org.onap.aai.domain.yang.ServiceSubscription.class,
247                 AAIUriFactory.createResourceUri(AAIObjectType.SERVICE_SUBSCRIPTION, globalSubscriberId,
248                         subscriptionServiceType));
249
250         assertThat(bbInputSetupUtils.getAAIServiceSubscription(globalSubscriberId, subscriptionServiceType),
251                 sameBeanAs(expected.get()));
252     }
253
254     @Test
255     public void getAAIServiceSubscriptionErrorsTest() {
256         assertNull(bbInputSetupUtils.getAAIServiceSubscription(null, null));
257         assertNull(bbInputSetupUtils.getAAIServiceSubscription("", ""));
258         assertNull(bbInputSetupUtils.getAAIServiceSubscription("", null));
259         assertNull(bbInputSetupUtils.getAAIServiceSubscription(null, ""));
260     }
261
262     @Test
263     public void getAAIServiceInstanceByIdTest() {
264         final String serviceInstanceId = "serviceInstanceId";
265         ServiceInstance expectedServiceInstance = new ServiceInstance();
266
267         doReturn(Optional.of(expectedServiceInstance)).when(MOCK_aaiResourcesClient).get(isA(Class.class),
268                 isA(AAIResourceUri.class));
269
270         assertThat(bbInputSetupUtils.getAAIServiceInstanceById(serviceInstanceId), sameBeanAs(expectedServiceInstance));
271     }
272
273     @Test
274     public void getAAIServiceInstanceById_ifEmptyReturnNull() {
275         doReturn(Optional.empty()).when(MOCK_aaiResourcesClient).get(eq(ServiceInstance.class),
276                 any(AAIResourceUri.class));
277
278         assertNull(bbInputSetupUtils.getAAIServiceInstanceById("any"));
279     }
280
281     @Test
282     public void getAAIServiceInstanceByIdAndCustomerTest() {
283         final String globalCustomerId = "globalCustomerId";
284         final String serviceType = "serviceType";
285         final String serviceInstanceId = "serviceInstanceId";
286         ServiceInstance expected = new ServiceInstance();
287         expected.setServiceInstanceId(serviceInstanceId);
288
289         doReturn(Optional.of(expected)).when(MOCK_aaiResourcesClient).get(ServiceInstance.class, AAIUriFactory
290                 .createResourceUri(AAIObjectType.SERVICE_INSTANCE, globalCustomerId, serviceType, serviceInstanceId)
291                 .depth(Depth.TWO));
292
293         assertThat(bbInputSetupUtils.getAAIServiceInstanceByIdAndCustomer(globalCustomerId, serviceType,
294                 serviceInstanceId), sameBeanAs(expected));
295     }
296
297     @Test
298     public void getAAIServiceInstanceByIdAndCustomerNullTest() {
299         doReturn(Optional.empty()).when(MOCK_aaiResourcesClient).get(eq(ServiceInstance.class),
300                 any(AAIResourceUri.class));
301
302         assertNull(bbInputSetupUtils.getAAIServiceInstanceByIdAndCustomer("", "", ""));
303     }
304
305     @Test
306     public void getAAIServiceInstanceByNameTest() throws Exception {
307         final String serviceInstanceName = "serviceInstanceName";
308
309         ServiceInstance expectedServiceInstance = new ServiceInstance();
310         expectedServiceInstance.setServiceInstanceId("serviceInstanceId");
311
312         ServiceSubscription serviceSubscription = new ServiceSubscription();
313         serviceSubscription.setServiceType("serviceType");
314
315         Customer customer = new Customer();
316         customer.setGlobalCustomerId("globalCustomerId");
317         customer.setServiceSubscription(serviceSubscription);
318
319         ServiceInstances serviceInstances = new ServiceInstances();
320         serviceInstances.getServiceInstance().add(expectedServiceInstance);
321
322         AAIPluralResourceUri expectedUri = AAIUriFactory
323                 .createResourceUri(AAIObjectPlurals.SERVICE_INSTANCE, customer.getGlobalCustomerId(),
324                         customer.getServiceSubscription().getServiceType())
325                 .queryParam("service-instance-name", serviceInstanceName).depth(Depth.TWO);
326         bbInputSetupUtils.getAAIServiceInstanceByName(serviceInstanceName, customer);
327
328         verify(MOCK_aaiResourcesClient, times(1)).getOne(org.onap.aai.domain.yang.ServiceInstances.class,
329                 org.onap.aai.domain.yang.ServiceInstance.class, expectedUri);
330     }
331
332     @Test
333     public void getAAIServiceInstanceByNameNullTest() throws Exception {
334         Customer customer = new Customer();
335         customer.setServiceSubscription(new ServiceSubscription());
336
337         assertNull(bbInputSetupUtils.getAAIServiceInstanceByName("", customer));
338     }
339
340     @Test
341     public void getOptionalAAIServiceInstanceByNameNullTest() throws Exception {
342         Optional<ServiceInstance> actual = bbInputSetupUtils.getAAIServiceInstanceByName("", "", "");
343
344         assertThat(actual, sameBeanAs(Optional.empty()));
345     }
346
347     @Test
348     public void getCatalogInstanceGroupNullTest() {
349         assertNull(bbInputSetupUtils.getCatalogInstanceGroup(""));
350     }
351
352     @Test
353     public void getCatalogInstanceGroupTest() throws IOException {
354         final String modelUUID = "modelUUIDTest";
355         org.onap.so.db.catalog.beans.InstanceGroup expectedInstanceGroup = mapper.readValue(
356                 new File(RESOURCE_PATH + "InstanceGroup.json"), org.onap.so.db.catalog.beans.InstanceGroup.class);
357
358         doReturn(expectedInstanceGroup).when(MOCK_catalogDbClient).getInstanceGroupByModelUUID(modelUUID);
359
360         assertThat(bbInputSetupUtils.getCatalogInstanceGroup(modelUUID), sameBeanAs(expectedInstanceGroup));
361     }
362
363     @Test
364     public void getCollectionResourceInstanceGroupCustomizationTest() {
365         final String modelCustomizationUUID = "modelCustomizationUUID";
366         CollectionResourceInstanceGroupCustomization expectedCollection =
367                 new CollectionResourceInstanceGroupCustomization();
368
369         doReturn(Arrays.asList(expectedCollection)).when(MOCK_catalogDbClient)
370                 .getCollectionResourceInstanceGroupCustomizationByModelCustUUID(modelCustomizationUUID);
371
372         assertThat(bbInputSetupUtils.getCollectionResourceInstanceGroupCustomization(modelCustomizationUUID),
373                 sameBeanAs(Arrays.asList(expectedCollection)));
374     }
375
376     @Test
377     public void getAAIConfigurationNullTest() {
378         assertNull(bbInputSetupUtils.getAAIConfiguration(""));
379     }
380
381     @Test
382     public void getAAIConfigurationTest() throws IOException {
383         final String configurationId = "configurationId";
384         Configuration expectedAaiConfiguration =
385                 mapper.readValue(new File(RESOURCE_PATH + "ConfigurationInput.json"), Configuration.class);
386
387         doReturn(Optional.of(expectedAaiConfiguration)).when(MOCK_aaiResourcesClient).get(isA(Class.class),
388                 isA(AAIResourceUri.class));
389
390         assertThat(bbInputSetupUtils.getAAIConfiguration(configurationId), sameBeanAs(expectedAaiConfiguration));
391     }
392
393     @Test
394     public void getAAIGenericVnfNullTest() {
395         assertNull(bbInputSetupUtils.getAAIGenericVnf(""));
396     }
397
398     @Test
399     public void getAAIGenericVnfTest() throws IOException {
400         final String vnfId = "vnfId";
401         GenericVnf expectedAaiVnf =
402                 mapper.readValue(new File(RESOURCE_PATH + "aaiGenericVnfInput.json"), GenericVnf.class);
403
404         doReturn(Optional.of(expectedAaiVnf)).when(MOCK_aaiResourcesClient).get(isA(Class.class),
405                 eq(AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, vnfId).depth(Depth.ONE)));
406
407         assertThat(bbInputSetupUtils.getAAIGenericVnf(vnfId), sameBeanAs(expectedAaiVnf));
408     }
409
410     @Test
411     public void getAAIResourceDepthOneTest() {
412         AAIResourceUri aaiResourceUri = AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, "anyVnfId");
413         AAIResourceUri expectedUri = aaiResourceUri.clone().depth(Depth.ONE);
414         AAIResourceUri aaiResourceUriClone = aaiResourceUri.clone();
415
416         bbInputSetupUtils.getAAIResourceDepthOne(aaiResourceUri);
417
418         verify(MOCK_aaiResourcesClient, times(1)).get(expectedUri);
419         assertEquals("Uri should not have changed", aaiResourceUriClone.build(), aaiResourceUri.build());
420     }
421
422     @Test
423     public void getAAIResourceDepthTwoTest() {
424         AAIResourceUri aaiResourceUri = AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, "anyVnfId");
425         AAIResourceUri expectedUri = aaiResourceUri.clone().depth(Depth.TWO);
426         AAIResourceUri aaiResourceUriClone = aaiResourceUri.clone();
427
428         bbInputSetupUtils.getAAIResourceDepthTwo(aaiResourceUri);
429
430         verify(MOCK_aaiResourcesClient, times(1)).get(expectedUri);
431         assertEquals("Uri should not have changed", aaiResourceUriClone.build(), aaiResourceUri.build());
432     }
433
434     @Test
435     public void getRelatedNetworkByNameFromServiceInstanceTest() throws Exception {
436         final String networkId = "id123";
437         final String networkName = "name123";
438
439         Optional<L3Networks> expected = Optional.of(new L3Networks());
440         L3Network network = new L3Network();
441         network.setNetworkId(networkId);
442         network.setNetworkName(networkName);
443         expected.get().getL3Network().add(network);
444         doReturn(expected).when(MOCK_aaiResourcesClient).get(eq(L3Networks.class), any(AAIPluralResourceUri.class));
445         Optional<L3Network> actual =
446                 bbInputSetupUtils.getRelatedNetworkByNameFromServiceInstance(networkId, networkName);
447
448         assertTrue(actual.isPresent());
449         assertEquals(networkId, actual.get().getNetworkId());
450         assertEquals(networkName, actual.get().getNetworkName());
451         assertEquals(expected.get().getL3Network().get(0).getNetworkId(), actual.get().getNetworkId());
452     }
453
454     @Test
455     public void getRelatedNetworkByNameFromServiceInstanceNotFoundTest() throws Exception {
456         String serviceInstanceId = "serviceInstanceId";
457         String networkName = "networkName";
458
459         doReturn(Optional.empty()).when(MOCK_aaiResourcesClient).get(eq(L3Networks.class),
460                 any(AAIPluralResourceUri.class));
461
462         Optional<L3Network> actualNetwork =
463                 bbInputSetupUtils.getRelatedNetworkByNameFromServiceInstance(serviceInstanceId, networkName);
464
465         assertEquals(Optional.empty(), actualNetwork);
466     }
467
468     @Test
469     public void getRelatedServiceInstanceFromInstanceGroupTest() throws Exception {
470         Optional<ServiceInstances> expected = Optional.of(new ServiceInstances());
471         ServiceInstance serviceInstance = new ServiceInstance();
472         serviceInstance.setServiceInstanceId("serviceInstanceId");
473         serviceInstance.setServiceInstanceName("serviceInstanceName");
474         expected.get().getServiceInstance().add(serviceInstance);
475
476         doReturn(expected).when(MOCK_aaiResourcesClient).get(eq(ServiceInstances.class),
477                 any(AAIPluralResourceUri.class));
478         Optional<ServiceInstance> actual = this.bbInputSetupUtils.getRelatedServiceInstanceFromInstanceGroup("ig-001");
479
480         assertTrue(actual.isPresent());
481         assertEquals(expected.get().getServiceInstance().get(0).getServiceInstanceId(),
482                 actual.get().getServiceInstanceId());
483     }
484
485     @Test
486     public void getRelatedServiceInstanceFromInstanceGroupMultipleExceptionTest() throws Exception {
487         final String instanceGroupId = "ig-001";
488         expectedException.expect(MultipleObjectsFoundException.class);
489         Optional<ServiceInstances> serviceInstances = Optional.of(new ServiceInstances());
490         ServiceInstance si1 = Mockito.mock(ServiceInstance.class);
491         ServiceInstance si2 = Mockito.mock(ServiceInstance.class);
492         serviceInstances.get().getServiceInstance().add(si1);
493         serviceInstances.get().getServiceInstance().add(si2);
494
495         doReturn(serviceInstances).when(MOCK_aaiResourcesClient).get(eq(ServiceInstances.class),
496                 any(AAIPluralResourceUri.class));
497         this.bbInputSetupUtils.getRelatedServiceInstanceFromInstanceGroup(instanceGroupId);
498     }
499
500     @Test
501     public void getRelatedServiceInstanceFromInstanceGroupNotFoundExceptionTest() throws Exception {
502         expectedException.expect(NoServiceInstanceFoundException.class);
503         Optional<ServiceInstances> serviceInstances = Optional.of(new ServiceInstances());
504
505         doReturn(serviceInstances).when(MOCK_aaiResourcesClient).get(eq(ServiceInstances.class),
506                 any(AAIPluralResourceUri.class));
507         this.bbInputSetupUtils.getRelatedServiceInstanceFromInstanceGroup("ig-001");
508     }
509
510     @Test
511     public void getRelatedVnfByNameFromServiceInstanceTest() throws Exception {
512         final String vnfId = "id123";
513         final String vnfName = "name123";
514         final String serviceInstanceId = "service-instance-id123";
515         GenericVnf vnf = new GenericVnf();
516         vnf.setVnfId(vnfId);
517         vnf.setVnfName(vnfName);
518         doReturn(Optional.of(vnf)).when(MOCK_aaiResourcesClient).getOne(GenericVnfs.class, GenericVnf.class,
519                 AAIUriFactory.createResourceUri(AAIObjectType.SERVICE_INSTANCE, serviceInstanceId)
520                         .relatedTo(AAIObjectPlurals.GENERIC_VNF).queryParam("vnf-name", vnfName));
521         Optional<GenericVnf> actual =
522                 this.bbInputSetupUtils.getRelatedVnfByNameFromServiceInstance(serviceInstanceId, vnfName);
523         assertTrue(actual.isPresent());
524         assertEquals(vnf.getVnfId(), actual.get().getVnfId());
525     }
526
527     @Test
528     public void getRelatedVnfByNameFromServiceInstanceNotFoundTest() throws Exception {
529         final String serviceInstanceId = "serviceInstanceId";
530         final String vnfName = "vnfName";
531
532         doReturn(Optional.empty()).when(MOCK_aaiResourcesClient).getOne(eq(GenericVnfs.class), eq(GenericVnf.class),
533                 any(AAIPluralResourceUri.class));
534
535         assertEquals(Optional.empty(),
536                 bbInputSetupUtils.getRelatedVnfByNameFromServiceInstance(serviceInstanceId, vnfName));
537     }
538
539     @Test
540     public void getRelatedVolumeGroupByNameFromVnfTest() throws Exception {
541         final String vnfId = "vnf-id123";
542         final String volumeGroupId = "id123";
543         final String volumeGroupName = "volume-group-name123";
544         VolumeGroup volumeGroup = new VolumeGroup();
545         volumeGroup.setVolumeGroupId(volumeGroupId);
546         volumeGroup.setVolumeGroupName(volumeGroupName);
547         doReturn(Optional.of(volumeGroup)).when(MOCK_aaiResourcesClient).getOne(VolumeGroups.class, VolumeGroup.class,
548                 AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, vnfId)
549                         .relatedTo(AAIObjectPlurals.VOLUME_GROUP).queryParam("volume-group-name", volumeGroupName));
550         Optional<VolumeGroup> actual =
551                 this.bbInputSetupUtils.getRelatedVolumeGroupByNameFromVnf(vnfId, volumeGroupName);
552         assertEquals(volumeGroup.getVolumeGroupId(), actual.get().getVolumeGroupId());
553     }
554
555     @Test
556     public void getRelatedVolumeGroupByNameFromVnfNotFoundTest() throws Exception {
557         String vnfId = "vnfId";
558         String volumeGroupName = "volumeGroupName";
559
560         doReturn(Optional.empty()).when(MOCK_aaiResourcesClient).getOne(eq(VolumeGroups.class), eq(VolumeGroup.class),
561                 any(AAIPluralResourceUri.class));
562
563         Optional<VolumeGroup> actualVolumeGroup =
564                 bbInputSetupUtils.getRelatedVolumeGroupByNameFromVnf(vnfId, volumeGroupName);
565
566         assertEquals(Optional.empty(), actualVolumeGroup);
567     }
568
569     @Test
570     public void getRelatedVolumeGroupByNameFromVfModuleTest() throws Exception {
571         VolumeGroup volumeGroup = new VolumeGroup();
572         volumeGroup.setVolumeGroupId("id123");
573         volumeGroup.setVolumeGroupName("name123");
574         doReturn(Optional.of(volumeGroup)).when(MOCK_aaiResourcesClient).getOne(VolumeGroups.class, VolumeGroup.class,
575                 AAIUriFactory.createResourceUri(AAIObjectType.VF_MODULE, "vnf-id123", "vf-module-id123")
576                         .relatedTo(AAIObjectPlurals.VOLUME_GROUP)
577                         .queryParam("volume-group-name", "volume-group-name123"));
578         Optional<VolumeGroup> actual = this.bbInputSetupUtils.getRelatedVolumeGroupByNameFromVfModule("vnf-id123",
579                 "vf-module-id123", "volume-group-name123");
580         assertTrue(actual.isPresent());
581         assertEquals(volumeGroup.getVolumeGroupId(), actual.get().getVolumeGroupId());
582     }
583
584     @Test
585     public void getRelatedVolumeGroupFromVfModuleMultipleVolumeGroupsExceptionTest() throws Exception {
586         expectedException.expect(Exception.class);
587         final String vnfId = "vnfId";
588         final String volumeGroupId = "volumeGroupId";
589
590         VolumeGroup volumeGroup = new VolumeGroup();
591         volumeGroup.setVolumeGroupId("id123");
592         volumeGroup.setVolumeGroupName("name123");
593
594         VolumeGroups expectedVolumeGroup = new VolumeGroups();
595         expectedVolumeGroup.getVolumeGroup().add(volumeGroup);
596         expectedVolumeGroup.getVolumeGroup().add(volumeGroup);
597
598         doReturn(expectedVolumeGroup).when(MOCK_aaiResourcesClient).get(eq(VolumeGroups.class),
599                 any(AAIResourceUri.class));
600
601         bbInputSetupUtils.getRelatedVolumeGroupFromVfModule(vnfId, volumeGroupId);
602     }
603
604     @Test
605     public void getRelatedVolumeGroupFromVfModuleNotFoundTest() throws Exception {
606         final String vnfId = "vnfId";
607         final String volumeGroupId = "volumeGroupId";
608
609         doReturn(Optional.empty()).when(MOCK_aaiResourcesClient).getOne(eq(VolumeGroups.class), eq(VolumeGroup.class),
610                 any(AAIPluralResourceUri.class));
611
612         Optional<VolumeGroup> actualVolumeGroup =
613                 bbInputSetupUtils.getRelatedVolumeGroupFromVfModule(vnfId, volumeGroupId);
614
615         assertEquals(Optional.empty(), actualVolumeGroup);
616     }
617
618     @Test
619     public void getRelatedVolumeGroupFromVfModuleTest() throws Exception {
620         VolumeGroup volumeGroup = new VolumeGroup();
621         volumeGroup.setVolumeGroupId("id123");
622         doReturn(Optional.of(volumeGroup)).when(MOCK_aaiResourcesClient).getOne(VolumeGroups.class, VolumeGroup.class,
623                 AAIUriFactory.createResourceUri(AAIObjectType.VF_MODULE, "vnf-id123", "vf-module-id123")
624                         .relatedTo(AAIObjectPlurals.VOLUME_GROUP));
625         Optional<VolumeGroup> actual =
626                 this.bbInputSetupUtils.getRelatedVolumeGroupFromVfModule("vnf-id123", "vf-module-id123");
627         assertTrue(actual.isPresent());
628         assertEquals(volumeGroup.getVolumeGroupId(), actual.get().getVolumeGroupId());
629     }
630
631     @Test
632     public void getRelatedVolumeGroupByNameFromVfModuleNotFoundTest() throws Exception {
633         String vnfId = "vnfId";
634         String volumeGroupId = "volumeGroupId";
635         String volumeGroupName = "volumeGroupName";
636
637         doReturn(Optional.empty()).when(MOCK_aaiResourcesClient).getOne(eq(VolumeGroups.class), eq(VolumeGroup.class),
638                 any(AAIPluralResourceUri.class));
639
640         Optional<VolumeGroup> actualVolumeGroup =
641                 bbInputSetupUtils.getRelatedVolumeGroupByNameFromVfModule(vnfId, volumeGroupId, volumeGroupName);
642
643         assertEquals(Optional.empty(), actualVolumeGroup);
644     }
645
646     @Test
647     public void loadOriginalFlowExecutionPathTest() throws IOException {
648         final String requestId = "123";
649         final String originalRequestId = "originalRequestId";
650         final String flowsToExecuteString = new String(
651                 Files.readAllBytes(Paths.get(RESOURCE_PATH + "FlowsToExecute.json")), StandardCharsets.UTF_8);
652
653         InfraActiveRequests request = new InfraActiveRequests();
654         ExecuteBuildingBlock[] expectedFlowsToExecute =
655                 mapper.readValue(flowsToExecuteString, ExecuteBuildingBlock[].class);
656
657         request.setRequestId(requestId);
658         request.setOriginalRequestId(originalRequestId);
659         doReturn(request).when(MOCK_requestsDbClient).getInfraActiveRequestbyRequestId(requestId);
660
661         RequestProcessingData requestProcessingData = new RequestProcessingData();
662         requestProcessingData.setValue(flowsToExecuteString);
663         doReturn(requestProcessingData).when(MOCK_requestsDbClient)
664                 .getRequestProcessingDataBySoRequestIdAndName(anyString(), anyString());
665
666         List<ExecuteBuildingBlock> flowsToExecute = bbInputSetupUtils.loadOriginalFlowExecutionPath(requestId);
667
668         assertEquals(mapper.writeValueAsString(expectedFlowsToExecute), mapper.writeValueAsString(flowsToExecute));
669     }
670
671     @Test
672     public void getRelatedConfigurationByNameFromServiceInstanceNotFoundTest() throws Exception {
673         assertEquals(Optional.empty(), bbInputSetupUtils.getRelatedConfigurationByNameFromServiceInstance("", ""));
674     }
675
676     @Test
677     public void getRelatedConfigurationByNameFromServiceInstanceTest() throws Exception {
678         Configuration configuration = new Configuration();
679         configuration.setConfigurationId("id123");
680         doReturn(Optional.of(configuration)).when(MOCK_aaiResourcesClient).getOne(Configurations.class,
681                 Configuration.class,
682                 AAIUriFactory.createResourceUri(AAIObjectType.SERVICE_INSTANCE, "service-instance-id123")
683                         .relatedTo(AAIObjectPlurals.CONFIGURATION)
684                         .queryParam("configuration-name", "configuration-name123"));
685         Optional<Configuration> actual = this.bbInputSetupUtils
686                 .getRelatedConfigurationByNameFromServiceInstance("service-instance-id123", "configuration-name123");
687         assertTrue(actual.isPresent());
688         assertEquals(configuration.getConfigurationId(), actual.get().getConfigurationId());
689     }
690
691     @Test
692     public void existsAAIVfModuleGloballyByNameTest() {
693         AAIPluralResourceUri expectedUri =
694                 AAIUriFactory.createNodesUri(AAIObjectPlurals.VF_MODULE).queryParam("vf-module-name", "testVfModule");
695         bbInputSetupUtils.existsAAIVfModuleGloballyByName("testVfModule");
696
697         verify(MOCK_aaiResourcesClient, times(1)).exists(expectedUri);
698     }
699
700     @Test
701     public void existsAAIConfigurationGloballyByNameTest() {
702         AAIPluralResourceUri expectedUri = AAIUriFactory.createResourceUri(AAIObjectPlurals.CONFIGURATION)
703                 .queryParam("configuration-name", "testConfig");
704         bbInputSetupUtils.existsAAIConfigurationGloballyByName("testConfig");
705
706         verify(MOCK_aaiResourcesClient, times(1)).exists(expectedUri);
707     }
708
709     @Test
710     public void existsAAINetworksGloballyByNameTest() {
711         AAIPluralResourceUri expectedUri =
712                 AAIUriFactory.createResourceUri(AAIObjectPlurals.L3_NETWORK).queryParam("network-name", "testNetwork");
713         bbInputSetupUtils.existsAAINetworksGloballyByName("testNetwork");
714
715         verify(MOCK_aaiResourcesClient, times(1)).exists(expectedUri);
716     }
717
718     @Test
719     public void existsAAIVolumeGroupGloballyByNameTest() {
720         AAIPluralResourceUri expectedUri = AAIUriFactory.createNodesUri(AAIObjectPlurals.VOLUME_GROUP)
721                 .queryParam("volume-group-name", "testVoumeGroup");
722
723         bbInputSetupUtils.existsAAIVolumeGroupGloballyByName("testVoumeGroup");
724         verify(MOCK_aaiResourcesClient, times(1)).exists(expectedUri);
725     }
726
727     @Test
728     public void shouldChangeInfraActiveRequestVnfId() throws IOException {
729         final String vnfId = "vnfId";
730         InfraActiveRequests infraActiveRequests = loadExpectedInfraActiveRequest();
731
732         bbInputSetupUtils.updateInfraActiveRequestVnfId(infraActiveRequests, vnfId);
733
734         assertEquals(vnfId, infraActiveRequests.getVnfId());
735     }
736
737     @Test
738     public void shouldChangeInfraActiveRequestVfModuleId() throws IOException {
739         final String vfModuleId = "vfModuleId";
740         InfraActiveRequests infraActiveRequests = loadExpectedInfraActiveRequest();
741
742         bbInputSetupUtils.updateInfraActiveRequestVfModuleId(infraActiveRequests, vfModuleId);
743
744         assertEquals(vfModuleId, infraActiveRequests.getVfModuleId());
745     }
746
747     @Test
748     public void shouldChangeInfraActiveRequestVolumeGroupId() throws IOException {
749         final String volumeGroupId = "volumeGroupId";
750         InfraActiveRequests infraActiveRequests = loadExpectedInfraActiveRequest();
751
752         bbInputSetupUtils.updateInfraActiveRequestVolumeGroupId(infraActiveRequests, volumeGroupId);
753
754         assertEquals(volumeGroupId, infraActiveRequests.getVolumeGroupId());
755     }
756
757     @Test
758     public void shouldChangeInfraActiveRequestNetworkId() throws IOException {
759         final String networkId = "activeRequestNetworkId";
760         InfraActiveRequests infraActiveRequests = loadExpectedInfraActiveRequest();
761
762         bbInputSetupUtils.updateInfraActiveRequestNetworkId(infraActiveRequests, networkId);
763
764         assertEquals(networkId, infraActiveRequests.getNetworkId());
765     }
766
767     @Test
768     public void getAAIVpnBindingNullTest() {
769         assertNull(bbInputSetupUtils.getAAIVpnBinding("vpnBindingId"));
770     }
771
772     @Test
773     public void getAAIVolumeGroupNullTest() {
774         VolumeGroup actualAaiVnf =
775                 bbInputSetupUtils.getAAIVolumeGroup("cloudOwnerId", "cloudRegionId", "volumeGroupId");
776         assertNull(actualAaiVnf);
777     }
778
779     @Test
780     public void getAAIVfModuleNullTest() {
781         assertNull(bbInputSetupUtils.getAAIVfModule("vnfId", "vfModuleId"));
782     }
783
784     @Test
785     public void getAAIL3NetworkNullTest() {
786         assertNull(bbInputSetupUtils.getAAIL3Network("networkId"));
787     }
788
789     @Test
790     public void getAICVpnBindingFromNetwork_noVpnBindingTest() throws IOException {
791         L3Network l3Network =
792                 mapper.readValue(new File(RESOURCE_PATH + "aaiL3NetworkInputWithSubnets.json"), L3Network.class);
793
794         Optional<VpnBinding> actual = bbInputSetupUtils.getAICVpnBindingFromNetwork(l3Network);
795         assertEquals(Optional.empty(), actual);
796     }
797
798     @Test
799     public void getAAIServiceInstancesGloballyByName_noAAIResourceTest() {
800         final String serviceInstanceName = "serviceInstanceName";
801
802         doReturn(Optional.empty()).when(MOCK_aaiResourcesClient).get(isA(Class.class), isA(AAIPluralResourceUri.class));
803         ServiceInstances actualServiceInstances =
804                 bbInputSetupUtils.getAAIServiceInstancesGloballyByName(serviceInstanceName);
805
806         assertNull(actualServiceInstances);
807     }
808
809     @Test
810     public void getAAIVnfsGloballyByName_noAAIResourceTest() {
811         final String vnfName = "vnfName";
812
813         doReturn(Optional.empty()).when(MOCK_aaiResourcesClient).get(isA(Class.class), isA(AAIPluralResourceUri.class));
814         GenericVnfs actualGenericVnfs = bbInputSetupUtils.getAAIVnfsGloballyByName(vnfName);
815
816         assertNull(actualGenericVnfs);
817     }
818
819     private InfraActiveRequests loadExpectedInfraActiveRequest() throws IOException {
820         return mapper.readValue(new File(RESOURCE_PATH + "InfraActiveRequestExpected.json"), InfraActiveRequests.class);
821     }
822 }