Merge 1806 code of vid-common
[vid.git] / vid-app-common / src / test / java / org / onap / vid / services / AsyncInstantiationBusinessLogicTest.java
1 package org.onap.vid.services;
2
3 import com.fasterxml.jackson.databind.JsonNode;
4 import com.fasterxml.jackson.databind.ObjectMapper;
5 import jersey.repackaged.com.google.common.collect.ImmutableList;
6 import net.javacrumbs.jsonunit.JsonAssert;
7 import org.apache.commons.io.IOUtils;
8 import org.hibernate.SessionFactory;
9 import org.json.JSONException;
10 import org.mockito.ArgumentCaptor;
11 import org.mockito.Mock;
12 import org.mockito.Mockito;
13 import org.mockito.MockitoAnnotations;
14 import org.onap.vid.aai.exceptions.InvalidAAIResponseException;
15 import org.onap.vid.aai.model.ResourceType;
16 import org.onap.vid.changeManagement.RequestDetailsWrapper;
17 import org.onap.vid.exceptions.GenericUncheckedException;
18 import org.onap.vid.exceptions.MaxRetriesException;
19 import org.onap.vid.exceptions.OperationNotAllowedException;
20 import org.onap.vid.job.Job;
21 import org.onap.vid.job.Job.JobStatus;
22 import org.onap.vid.job.JobAdapter;
23 import org.onap.vid.job.JobsBrokerService;
24 import org.onap.vid.job.impl.JobDaoImpl;
25 import org.onap.vid.model.JobAuditStatus;
26 import org.onap.vid.model.JobAuditStatus.SourceStatus;
27 import org.onap.vid.model.NameCounter;
28 import org.onap.vid.model.ServiceInfo;
29 import org.onap.vid.model.serviceInstantiation.ServiceInstantiation;
30 import org.onap.vid.model.serviceInstantiation.Vnf;
31 import org.onap.vid.mso.model.ServiceInstantiationRequestDetails;
32 import org.onap.vid.mso.rest.AsyncRequestStatus;
33 import org.onap.vid.utils.DaoUtils;
34 import org.onap.vid.config.DataSourceConfig;
35 import org.onap.vid.config.MockedAaiClientAndFeatureManagerConfig;
36 import org.onap.vid.mso.MsoOperationalEnvironmentTest;
37 import org.onap.vid.services.AsyncInstantiationBaseTest;
38 import org.onap.portalsdk.core.FusionObject;
39 import org.onap.portalsdk.core.service.DataAccessService;
40 import org.onap.portalsdk.core.util.SystemProperties;
41 import org.springframework.beans.factory.annotation.Autowired;
42 import org.springframework.test.context.ContextConfiguration;
43 import org.testng.Assert;
44 import org.testng.annotations.*;
45
46 import javax.inject.Inject;
47 import java.io.IOException;
48 import java.lang.reflect.Method;
49 import java.net.URL;
50 import java.time.Instant;
51 import java.time.LocalDateTime;
52 import java.time.ZoneId;
53 import java.util.*;
54 import java.util.Optional;
55 import java.util.concurrent.Callable;
56 import java.util.concurrent.ExecutorService;
57 import java.util.concurrent.Executors;
58 import java.util.stream.Collectors;
59 import java.util.stream.IntStream;
60
61 import static org.hamcrest.MatcherAssert.assertThat;
62 import static org.hamcrest.Matchers.contains;
63 import static org.hamcrest.Matchers.*;
64 import static org.hamcrest.core.Every.everyItem;
65 import static org.hamcrest.core.IsEqual.equalTo;
66 import static org.mockito.Matchers.any;
67 import static org.mockito.Mockito.*;
68 import static org.onap.vid.job.Job.JobStatus.*;
69 import static org.testng.Assert.*;
70
71 @ContextConfiguration(classes = {DataSourceConfig.class, SystemProperties.class, MockedAaiClientAndFeatureManagerConfig.class})
72 public class AsyncInstantiationBusinessLogicTest extends AsyncInstantiationBaseTest {
73 /*
74 TO BE FIXED
75     @Inject
76     private DataAccessService dataAccessService;
77
78     @Mock
79     private JobAdapter jobAdapter;
80
81     @Mock
82     private JobsBrokerService jobsBrokerService;
83
84
85
86     @Autowired
87     private SessionFactory sessionFactory;
88
89     private AsyncInstantiationBusinessLogic asyncInstantiationBL;
90
91     private int serviceCount = 0;
92
93     private static final String UPDATE_SERVICE_INFO_EXCEPTION_MESSAGE =
94             "Failed to retrieve job with uuid .* from ServiceInfo table. Instances found: .*";
95
96     private static final String DELETE_SERVICE_INFO_STATUS_EXCEPTION_MESSAGE =
97             "Service status does not allow deletion from the queue";
98
99     @BeforeClass
100     void initServicesInfoService() {
101         MockitoAnnotations.initMocks(this);
102         asyncInstantiationBL = new AsyncInstantiationBusinessLogicImpl(dataAccessService, jobAdapter, jobsBrokerService, sessionFactory, aaiClient);
103         createInstanceParamsMaps();
104     }
105
106     @BeforeMethod
107     void defineMocks() {
108         mockAaiClientAnyNameFree();
109     }
110
111     @BeforeMethod
112     void resetServiceCount() {
113         serviceCount = 0;
114     }
115
116     @AfterMethod
117     void clearDb() {
118         dataAccessService.deleteDomainObjects(JobDaoImpl.class, "1=1", getPropsMap());
119         dataAccessService.deleteDomainObjects(ServiceInfo.class, "1=1", getPropsMap());
120         dataAccessService.deleteDomainObjects(JobAuditStatus.class, "1=1", getPropsMap());
121         dataAccessService.deleteDomainObjects(NameCounter.class, "1=1", getPropsMap());
122     }
123
124
125     private void createNewTestServicesInfoForFilter(String userId) {
126         LocalDateTime createdDate, modifiedDate;
127         LocalDateTime NOW = LocalDateTime.now();
128         UUID uuid;
129
130         // Old job
131         uuid = UUID.randomUUID();
132         addNewJob(uuid);
133         createdDate = NOW.minusYears(1);
134         addNewServiceInfo(uuid, userId, "Old", createdDate, createdDate, COMPLETED, false);
135
136         uuid = UUID.randomUUID();
137         addNewJob(uuid);
138         createdDate = NOW.minusDays(20);
139         modifiedDate = NOW.minusDays(19);
140         addNewServiceInfo(uuid, userId, "Hidden", createdDate, modifiedDate, PAUSE, true);
141
142         createNewTestServicesInfo(String.valueOf(userId));
143     }
144
145     private void createNewTestServicesInfo(String userId) {
146
147         LocalDateTime createdDate, modifiedDate;
148         LocalDateTime NOW = LocalDateTime.now();
149         UUID uuid;
150
151         uuid = UUID.randomUUID();
152         addNewJob(uuid);
153
154         createdDate = NOW.minusDays(40);
155         addNewServiceInfo(uuid, userId, "service instance 5", createdDate, createdDate, COMPLETED, false);
156         addNewServiceInfo(uuid, userId, "service instance 6", createdDate, createdDate, STOPPED, false);
157
158         uuid = UUID.randomUUID();
159         addNewJob(uuid);
160
161         createdDate = NOW.minusDays(20);
162         modifiedDate = NOW.minusDays(10);
163         addNewServiceInfo(uuid, userId, "service instance 4", createdDate, modifiedDate, STOPPED, false);
164         addNewServiceInfo(uuid, userId, "service instance 2", createdDate, modifiedDate, COMPLETED, false);
165         addNewServiceInfo(uuid, userId, "service instance 3", createdDate, modifiedDate, PAUSE, false);
166
167         modifiedDate = NOW.minusDays(19);
168         addNewServiceInfo(uuid, userId, "service instance 1", createdDate, modifiedDate, FAILED, false);
169
170
171         // Job to a different user
172         uuid = UUID.randomUUID();
173         addNewJob(uuid);
174
175         createdDate = NOW.minusMonths(2);
176         addNewServiceInfo(uuid, "2221", "service instance 7", createdDate, createdDate, COMPLETED, false);
177
178     }
179
180     private UUID createServicesInfoWithDefaultValues(Job.JobStatus status) {
181
182         LocalDateTime NOW = LocalDateTime.now();
183         UUID uuid;
184
185         uuid = UUID.randomUUID();
186         addNewJob(uuid, status);
187
188         addNewServiceInfo(uuid, null, "service instance 1", NOW, NOW, status, false);
189
190         return uuid;
191
192     }
193
194     private List<ServiceInfo> getFullList() {
195         List<ServiceInfo> expectedOrderServiceInfo = dataAccessService.getList(ServiceInfo.class, getPropsMap());
196         assertThat("Failed to retrieve all predefined services", expectedOrderServiceInfo.size(), equalTo(serviceCount));
197         expectedOrderServiceInfo.sort(new ServiceInfoComparator());
198         return expectedOrderServiceInfo;
199     }
200
201     private static Date toDate(LocalDateTime localDateTime) {
202         return Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
203     }
204
205     private LocalDateTime fromDate(Date date) {
206         return Instant.ofEpochMilli(date.getTime())
207                 .atZone(ZoneId.systemDefault())
208                 .toLocalDateTime();
209     }
210
211     private void addNewServiceInfo(UUID uuid, String userId, String serviceName, LocalDateTime createDate, LocalDateTime statusModifiedDate, Job.JobStatus status, boolean isHidden) {
212         ServiceInfo serviceInfo = new ServiceInfo();
213         serviceInfo.setJobId(uuid);
214         serviceInfo.setUserId(userId);
215         serviceInfo.setServiceInstanceName(serviceName);
216         serviceInfo.setStatusModifiedDate(toDate(statusModifiedDate));
217         serviceInfo.setJobStatus(status);
218         serviceInfo.setPause(false);
219         serviceInfo.setOwningEntityId("1234");
220         serviceInfo.setCreatedBulkDate(toDate(createDate));
221
222         serviceInfo.setHidden(isHidden);
223         dataAccessService.saveDomainObject(serviceInfo, getPropsMap());
224         setCreateDateToServiceInfo(uuid, createDate);
225         serviceCount++;
226
227     }
228
229     private void setCreateDateToServiceInfo(UUID jobUuid, LocalDateTime createDate) {
230         List<ServiceInfo> serviceInfoList = dataAccessService.getList(ServiceInfo.class, getPropsMap());
231         DaoUtils.tryWithSessionAndTransaction(sessionFactory, session -> {
232             serviceInfoList.stream()
233                     .filter(serviceInfo -> jobUuid.equals(serviceInfo.getJobId()))
234                     .forEach(serviceInfo -> {
235                         serviceInfo.setCreated(toDate(createDate));
236                         session.saveOrUpdate(serviceInfo);
237                     });
238             return 1;
239         });
240     }
241
242     private void addNewJob(UUID uuid) {
243         addNewJob(uuid, null);
244     }
245
246     private void addNewJob(UUID uuid, Job.JobStatus status) {
247         JobDaoImpl jobDao = new JobDaoImpl();
248         jobDao.setUuid(uuid);
249         jobDao.setStatus(status);
250         dataAccessService.saveDomainObject(jobDao, getPropsMap());
251     }
252
253     @Test
254     public void testServiceInfoAreOrderedAsExpected() {
255         int userId = 2222;
256         createNewTestServicesInfo(String.valueOf(userId));
257         List<ServiceInfo> expectedOrderServiceInfo = getFullList();
258         List<ServiceInfo> serviceInfoListResult = asyncInstantiationBL.getAllServicesInfo();
259         assertThat("Services aren't ordered as expected", serviceInfoListResult, equalTo(expectedOrderServiceInfo));
260     }
261
262     @Test
263     public void testServiceInfoAreFilteredAsExpected() {
264         int userId = 2222;
265         createNewTestServicesInfoForFilter(String.valueOf(userId));
266         List<ServiceInfo> expectedOrderServiceInfo = getFullList();
267
268         List<ServiceInfo> expectedFilterByUser = expectedOrderServiceInfo.stream().filter(x ->
269                 !x.getServiceInstanceName().equals("Old") && !x.getServiceInstanceName().equals("Hidden")
270
271         ).collect(Collectors.toList());
272
273
274         List<ServiceInfo> serviceInfoFilteredByUser = asyncInstantiationBL.getAllServicesInfo();
275         assertThat("Services aren't ordered filtered as expected", serviceInfoFilteredByUser, equalTo(expectedFilterByUser));
276     }
277
278     @Test(dataProvider = "pauseAndInstanceParams", enabled = false) //Test is irrelevant with unique names feature
279     public void createServiceInstantiationMsoRequest(Boolean isPause, HashMap<String, String> vfModuleInstanceParamsMap, List vnfInstanceParams) throws Exception {
280         ServiceInstantiation serviceInstantiationPayload = generateMockServiceInstantiationPayload(isPause, createVnfList(vfModuleInstanceParamsMap, vnfInstanceParams, true));
281         final URL resource = this.getClass().getResource("/payload_jsons/bulk_service_request.json");
282             RequestDetailsWrapper<ServiceInstantiationRequestDetails> result =
283                     asyncInstantiationBL.generateServiceInstantiationRequest(null, serviceInstantiationPayload, "az2016");
284             String expected = IOUtils.toString(resource, "UTF-8");
285             MsoOperationalEnvironmentTest.assertThatExpectationIsLikeObject(expected, result);
286     }
287
288     @Test(dataProvider = "pauseAndInstanceParams")
289     public void createServiceInstantiationMsoRequestUniqueName(Boolean isPause, HashMap<String, String> vfModuleInstanceParamsMap, List vnfInstanceParams) throws Exception {
290         Mockito.reset(aaiClient);
291         mockAaiClientAnyNameFree();
292         ServiceInstantiation serviceInstantiationPayload = generateMockServiceInstantiationPayload(isPause, createVnfList(vfModuleInstanceParamsMap, vnfInstanceParams, true));
293         final URL resource = this.getClass().getResource("/payload_jsons/bulk_service_request_unique_names.json");
294         List<UUID> uuids = new ArrayList<>();
295         for (int i = 0; i < 2; i++) {
296             UUID currentUuid = createJobAndServiceInfo();
297             uuids.add(currentUuid);
298             RequestDetailsWrapper<ServiceInstantiationRequestDetails> result =
299                     asyncInstantiationBL.generateServiceInstantiationRequest(currentUuid, serviceInstantiationPayload, "az2016");
300             String unique =  String.format("00%s", i + 1);
301             String expected = IOUtils.toString(resource, "UTF-8")
302                     .replace("{SERVICE_UNIQENESS}", unique)
303                     .replace("{VNF_UNIQENESS}", unique)
304                     .replace("{VF_MODULE_UNIQENESS}", unique)
305                     .replace("{VF_MODULE_2_UNIQENESS}", unique)
306                     .replace("{VG_UNIQUENESS}", unique);
307             MsoOperationalEnvironmentTest.assertThatExpectationIsLikeObject(expected, result);
308             Optional<ServiceInfo> optionalServiceInfo = getJobById(currentUuid);
309             assertThat(optionalServiceInfo.get().getServiceInstanceName(), equalTo("vPE_Service_" + unique));
310             verifySearchNodeTypeByName(unique, "vPE_Service_", ResourceType.SERVICE_INSTANCE);
311             verifySearchNodeTypeByName(unique, "vmxnjr001_", ResourceType.GENERIC_VNF);
312             verifySearchNodeTypeByName(unique, "vmxnjr001_AVPN_base_vPE_BV_base_", ResourceType.VF_MODULE);
313             verifySearchNodeTypeByName(unique, "vmxnjr001_AVPN_base_vRE_BV_expansion_", ResourceType.VF_MODULE);
314             verifySearchNodeTypeByName(unique, "myVgName_", ResourceType.VOLUME_GROUP);
315         }
316     }
317
318     protected void verifySearchNodeTypeByName(String unique, String resourceName, ResourceType serviceInstance) {
319         verify(aaiClient, times(1)).searchNodeTypeByName(resourceName + unique, serviceInstance);
320     }
321
322     private HashMap<String, Object> getPropsMap() {
323         HashMap<String, Object> props = new HashMap<>();
324         props.put(FusionObject.Parameters.PARAM_USERID, 0);
325         return props;
326     }
327
328     @Test(enabled = false) //probably not needed with name uniqueness feature
329     public void pushBulkJob_bulkWithSize3_instancesNamesAreExactlyAsExpected() {
330         int bulkSize = 3;
331
332         final ServiceInstantiation request = generateMockServiceInstantiationPayload(
333                 false,
334                 createVnfList(instanceParamsMapWithoutParams, Collections.EMPTY_LIST, true),
335                 bulkSize, true,PROJECT_NAME, true
336         );
337
338         // in "createJob()" we will probe the service, with the generated names
339         final Job job = mock(Job.class);
340         when(job.getStatus()).thenReturn(PENDING);
341         when(jobAdapter.createJob(any(), any(), any(), any(), any())).thenReturn(job);
342
343
344         final List<UUID> uuids = asyncInstantiationBL.pushBulkJob(request, "myUserId");
345
346
347         ArgumentCaptor<ServiceInstantiation> serviceInstantiationCaptor = new ArgumentCaptor<ServiceInstantiation>();
348         verify(jobAdapter, times(bulkSize)).createJob(any(), serviceInstantiationCaptor.capture(), any(), any(), any());
349
350         assertThat(serviceInstantiationCaptor.getAllValues().stream().map(v -> v.getInstanceName()).collect(Collectors.toList()),
351                 containsInAnyOrder("vPE_Service_001", "vPE_Service_002", "vPE_Service_003"));
352
353         assertThat(uuids, hasSize(bulkSize));
354     }
355
356     @Test
357     public void generateMockServiceInstantiationPayload_serializeBackAndForth_sourceShouldBeTheSame() throws IOException {
358         ServiceInstantiation serviceInstantiationPayload = generateMockServiceInstantiationPayload(
359                 false,
360                 createVnfList(instanceParamsMapWithoutParams, ImmutableList.of(vnfInstanceParamsMapWithParamsToRemove, vnfInstanceParamsMapWithParamsToRemove), true),
361                 2, false,PROJECT_NAME, false);
362         ObjectMapper mapper = new ObjectMapper();
363         final String asString = mapper.writeValueAsString(serviceInstantiationPayload);
364
365         final ServiceInstantiation asObject = mapper.readValue(asString, ServiceInstantiation.class);
366         final String asString2 = mapper.writeValueAsString(asObject);
367
368         JsonAssert.assertJsonEquals(asString, asString2);
369     }
370
371     public static class ServiceInfoComparator implements Comparator<ServiceInfo> {
372
373         @Override
374         public int compare(ServiceInfo o1, ServiceInfo o2) {
375             int compare;
376
377             compare = o1.getCreatedBulkDate().compareTo(o2.getCreatedBulkDate());
378             if (compare != 0) {
379                 return -compare;
380             }
381
382             // check jobStatus priority
383             int o1Priority = getPriority(o1);
384             int o2Priority = getPriority(o2);
385             compare = o1Priority - o2Priority;
386             if (compare != 0) {
387                 return compare;
388             }
389
390             // check statusModifiedDate
391             return o1.getStatusModifiedDate().compareTo(o2.getStatusModifiedDate());
392         }
393
394         private int getPriority(ServiceInfo o) throws JSONException {
395             Job.JobStatus status = o.getJobStatus();
396             switch (status) {
397                 case COMPLETED:
398                 case FAILED:
399                     return 1;
400                 case IN_PROGRESS:
401                     return 2;
402                 case PAUSE:
403                     return 3;
404                 case STOPPED:
405                 case PENDING:
406                     return 4;
407                 default:
408                     return 5;
409             }
410         }
411     }
412
413     @DataProvider
414     public Object[][] pauseAndInstanceParams() {
415         return new Object[][]{
416                 {Boolean.TRUE, instanceParamsMapWithoutParams, Collections.EMPTY_LIST},
417                 {Boolean.FALSE, instanceParamsMapWithoutParams, Collections.EMPTY_LIST},
418                 {Boolean.TRUE, vfModuleInstanceParamsMapWithParamsToRemove, Collections.singletonList(vnfInstanceParamsMapWithParamsToRemove)}
419         };
420     }
421
422     private ServiceInstantiation generateMockServiceInstantiationPayload(boolean isPause, Map<String, Vnf> vnfs) {
423         return generateMockServiceInstantiationPayload(isPause, vnfs, 1, true, PROJECT_NAME, false);
424     }
425
426     @Test
427     public void testUpdateServiceInfo_WithExistingServiceInfo_ServiceInfoIsUpdated() {
428         UUID uuid = createJobAndServiceInfo();
429         final String STEPH_CURRY = "Steph Curry";
430         asyncInstantiationBL.updateServiceInfo(uuid, x -> {
431             x.setServiceInstanceName(STEPH_CURRY);
432             x.setJobStatus(Job.JobStatus.IN_PROGRESS);
433         });
434         Optional<ServiceInfo> optionalServiceInfo = getJobById(uuid);
435         assertThat(optionalServiceInfo.get().getServiceInstanceName(), equalTo(STEPH_CURRY));
436         assertThat(optionalServiceInfo.get().getJobStatus(), equalTo(Job.JobStatus.IN_PROGRESS));
437     }
438
439     private Optional<ServiceInfo> getJobById(UUID jobId) {
440         List<ServiceInfo> serviceInfoList = dataAccessService.getList(ServiceInfo.class, null);
441         return serviceInfoList.stream().filter(x -> jobId.equals(x.getJobId())).findFirst();
442     }
443
444     private UUID createJobAndServiceInfo() {
445         UUID uuid = UUID.randomUUID();
446         addNewJob(uuid);
447         ServiceInfo serviceInfo = new ServiceInfo();
448         serviceInfo.setServiceInstanceName("Lebron James");
449         serviceInfo.setJobId(uuid);
450         serviceInfo.setJobStatus(Job.JobStatus.PENDING);
451         dataAccessService.saveDomainObject(serviceInfo, getPropsMap());
452         return uuid;
453     }
454
455     @Test(expectedExceptions = GenericUncheckedException.class, expectedExceptionsMessageRegExp = UPDATE_SERVICE_INFO_EXCEPTION_MESSAGE)
456     public void testUpdateServiceInfo_WithNonExisting_ThrowException() {
457         asyncInstantiationBL.updateServiceInfo(UUID.randomUUID(), x -> x.setServiceInstanceName("not matter"));
458     }
459
460     @Test(expectedExceptions = GenericUncheckedException.class, expectedExceptionsMessageRegExp = UPDATE_SERVICE_INFO_EXCEPTION_MESSAGE)
461     public void testUpdateServiceInfo_WithDoubleServiceWithSameJobUuid_ThrowException() {
462         UUID uuid = createJobAndServiceInfo();
463         ServiceInfo serviceInfo = new ServiceInfo();
464         serviceInfo.setJobId(uuid);
465         dataAccessService.saveDomainObject(serviceInfo, getPropsMap());
466         asyncInstantiationBL.updateServiceInfo(UUID.randomUUID(), x -> x.setServiceInstanceName("not matter"));
467     }
468
469
470
471     @Test
472     public void testRequestPath_WithPauseFlagTrue_RequestPathIsAsExpected() {
473         ServiceInstantiation serviceInstantiationPauseFlagTrue = generateMockServiceInstantiationPayload(true, createVnfList(instanceParamsMapWithoutParams, Collections.EMPTY_LIST, true));
474         String path = asyncInstantiationBL.getServiceInstantiationPath(serviceInstantiationPauseFlagTrue);
475         Assert.assertEquals(path, SystemProperties.getProperty("mso.restapi.serviceInstanceAssign"));
476     }
477
478     @Test
479     public void testRequestPath_WithPauseFlagFalse_RequestPathIsAsExpected() {
480         ServiceInstantiation serviceInstantiationPauseFlagFalse = generateMockServiceInstantiationPayload(false, createVnfList(instanceParamsMapWithoutParams, Collections.EMPTY_LIST, true));
481         String path = asyncInstantiationBL.getServiceInstantiationPath(serviceInstantiationPauseFlagFalse);
482         Assert.assertEquals(path, SystemProperties.getProperty("mso.restapi.serviceInstanceCreate"));
483     }
484
485     @Test
486     public void createServiceInfo_WithUserProvidedNamingFalse_ServiceInfoIsAsExpected() throws IOException {
487         createServiceInfo_WithUserProvidedNamingFalse_ServiceInfoIsAsExpected(true);
488     }
489
490     @Test
491     public void createServiceInfo_WithUserProvidedNamingFalseAndNoVfmodules_ServiceInfoIsAsExpected() throws IOException {
492         createServiceInfo_WithUserProvidedNamingFalse_ServiceInfoIsAsExpected(false);
493     }
494
495     private void createServiceInfo_WithUserProvidedNamingFalse_ServiceInfoIsAsExpected(boolean withVfmodules) throws IOException {
496         ServiceInstantiation serviceInstantiationPayload = generateMockServiceInstantiationPayload(true,
497                 createVnfList(vfModuleInstanceParamsMapWithParamsToRemove, Collections.EMPTY_LIST, false),
498                 1,
499                 false,PROJECT_NAME, true);
500         URL resource;
501         if (withVfmodules) {
502             resource = this.getClass().getResource("/payload_jsons/bulk_service_request_ecomp_naming.json");
503         } else {
504             // remove the vf modules
505             serviceInstantiationPayload.getVnfs().values().forEach(vnf -> vnf.getVfModules().clear());
506             resource = this.getClass().getResource("/payload_jsons/bulk_service_request_no_vfmodule_ecomp_naming.json");
507         }
508
509         RequestDetailsWrapper<ServiceInstantiationRequestDetails> result =
510                 asyncInstantiationBL.generateServiceInstantiationRequest(null, serviceInstantiationPayload, "az2016");
511
512         String expected = IOUtils.toString(resource, "UTF-8");
513         MsoOperationalEnvironmentTest.assertThatExpectationIsLikeObject(expected, result);
514     }
515
516     @Test
517     public void checkIfNullProjectNameSentToMso(){
518         ServiceInstantiation serviceInstantiationPayload = generateMockServiceInstantiationPayload(true,
519                 createVnfList(vfModuleInstanceParamsMapWithParamsToRemove, Collections.EMPTY_LIST, false),
520                 1,
521                 false,null,false);
522         RequestDetailsWrapper<ServiceInstantiationRequestDetails> result =
523                 asyncInstantiationBL.generateServiceInstantiationRequest(null, serviceInstantiationPayload, "az2016");
524         JsonNode jsonNode = new ObjectMapper().valueToTree(result.requestDetails);
525         Assert.assertTrue(jsonNode.get("project").isNull());
526         serviceInstantiationPayload = generateMockServiceInstantiationPayload(true,
527                 createVnfList(vfModuleInstanceParamsMapWithParamsToRemove, Collections.EMPTY_LIST, false),
528                 1,
529                 false,"not null",false);
530         result = asyncInstantiationBL.generateServiceInstantiationRequest(null, serviceInstantiationPayload, "az2016");
531         jsonNode = new ObjectMapper().valueToTree(result.requestDetails);
532         Assert.assertTrue(jsonNode.get("project").get("projectName").asText().equalsIgnoreCase("not null"));
533
534
535
536     }
537
538     @Test
539     public void pushBulkJob_verifyCreatedDateBehavior_createdDateIsTheSameForAllServicesInSameBulk() {
540         LocalDateTime startTestDate = LocalDateTime.now().withNano(0);
541         final ServiceInstantiation request = generateMockServiceInstantiationPayload(
542                 false,
543                 createVnfList(instanceParamsMapWithoutParams, Collections.EMPTY_LIST, true),
544                 100, true,PROJECT_NAME, true
545         );
546
547         // in "createJob()" we will probe the service, with the generated names
548         final Job job = mock(Job.class);
549         when(job.getStatus()).thenReturn(PENDING);
550         when(jobAdapter.createJob(any(), any(), any(), any(), any())).thenReturn(job);
551
552         asyncInstantiationBL.pushBulkJob(request, "myUserId");
553         List<ServiceInfo> serviceInfoList = dataAccessService.getList(ServiceInfo.class, getPropsMap());
554
555         List<Date> creationDates = new ArrayList<>();
556         for (ServiceInfo serviceInfo : serviceInfoList) {
557             creationDates.add(serviceInfo.getCreatedBulkDate());
558         }
559         LocalDateTime endTestDate = LocalDateTime.now();
560
561         //creation date of all services is the same
562         Assert.assertTrue(creationDates.stream().distinct().count() <= 1);
563         LocalDateTime creationDate = fromDate(creationDates.get(0));
564         assertFalse(creationDate.isBefore(startTestDate));
565         assertFalse(creationDate.isAfter(endTestDate));
566     }
567
568     @DataProvider
569     public static Object[][] msoToJobStatusDataProvider() {
570         return new Object[][]{
571                 {"IN_PROGRESS", JobStatus.IN_PROGRESS},
572                 {"INPROGRESS", JobStatus.IN_PROGRESS},
573                 {"IN ProGREsS", JobStatus.IN_PROGRESS},
574                 {"JAMES_HARDEN", JobStatus.IN_PROGRESS},
575                 {"FAILED", JobStatus.FAILED},
576                 {"COMpleTE", JobStatus.COMPLETED},
577                 {"PENDING", JobStatus.IN_PROGRESS},
578                 {"Paused", JobStatus.PAUSE},
579                 {"Pause", JobStatus.PAUSE},
580                 {"PENDING_MANUAL_TASK", JobStatus.PAUSE},
581                 {"UNLOCKED", JobStatus.IN_PROGRESS}
582         };
583     }
584
585     @Test(dataProvider = "msoToJobStatusDataProvider")
586     void whenGetStatusFromMso_calcRightJobStatus(String msoStatus, Job.JobStatus expectedJobStatus) {
587         AsyncRequestStatus asyncRequestStatus = asyncRequestStatusResponse(msoStatus);
588         assertThat(asyncInstantiationBL.calcStatus(asyncRequestStatus), equalTo(expectedJobStatus));
589     }
590
591     private void createNewAuditStatus(JobAuditStatus auditStatus)
592     {
593         Date createdDate= auditStatus.getCreated();
594         dataAccessService.saveDomainObject(auditStatus, getPropsMap());
595         setDateToStatus(auditStatus.getSource(), auditStatus.getJobStatus(), createdDate);
596     }
597
598
599
600     private static final String MSO_ARBITRARY_STATUS = "completed mso status";
601
602     @DataProvider
603     public static Object[][] auditStatuses(Method test) {
604         return new Object[][]{
605                 {
606                         SourceStatus.VID,
607                         new String[]{ JobStatus.PENDING.toString(), JobStatus.IN_PROGRESS.toString()}
608                 },
609                 {       SourceStatus.MSO,
610                         new String[]{ JobStatus.IN_PROGRESS.toString(), MSO_ARBITRARY_STATUS }
611                 }
612         };
613
614     }
615
616     private void setDateToStatus(SourceStatus source, String status, Date date) {
617         List<JobAuditStatus> jobAuditStatusList = dataAccessService.getList(JobAuditStatus.class, getPropsMap());
618         DaoUtils.tryWithSessionAndTransaction(sessionFactory, session -> {
619             jobAuditStatusList.stream()
620                     .filter(auditStatus -> source.equals(auditStatus.getSource()) && status.equals(auditStatus.getJobStatus()))
621                     .forEach(auditStatus -> {
622                         auditStatus.setCreated(date);
623                         session.saveOrUpdate(auditStatus);
624                     });
625             return 1;
626         });
627     }
628
629
630     @Test(dataProvider = "auditStatuses")
631     public void givenSomeAuditStatuses_getStatusesOfSpecificSourceAndJobId_getSortedResultsMatchingToParameters(SourceStatus expectedSource, String [] expectedSortedStatuses){
632         UUID jobUuid = UUID.randomUUID();
633         List<JobAuditStatus> auditStatusList = com.google.common.collect.ImmutableList.of(
634                 new JobAuditStatus(jobUuid, IN_PROGRESS.toString(), SourceStatus.VID, toDate(LocalDateTime.now().minusHours(2))),
635                 new JobAuditStatus(jobUuid, IN_PROGRESS.toString(), SourceStatus.MSO, UUID.randomUUID(),"",toDate(LocalDateTime.now().minusHours(30))),
636                 new JobAuditStatus(jobUuid, MSO_ARBITRARY_STATUS, SourceStatus.MSO, UUID.randomUUID(),"",toDate(LocalDateTime.now().minusHours(3))),
637                 new JobAuditStatus(jobUuid, PENDING.toString(), SourceStatus.VID, toDate(LocalDateTime.now().minusHours(3))),
638                 new JobAuditStatus(UUID.randomUUID(), PENDING.toString(), SourceStatus.VID, toDate(LocalDateTime.now().minusHours(3))));
639         auditStatusList.forEach((auditStatus) -> createNewAuditStatus(auditStatus));
640         List<JobAuditStatus> statuses = asyncInstantiationBL.getAuditStatuses(jobUuid, expectedSource);
641         List<String> statusesList = statuses.stream().map(status -> status.getJobStatus()).collect(Collectors.toList());
642         Assert.assertTrue(statuses.stream().allMatch(status -> (status.getSource().equals(expectedSource)&& status.getJobId().equals(jobUuid))),"Only statuses of " + expectedSource + " for " + jobUuid + " should be returned. Returned statuses: " + String.join(",", statusesList ));
643         assertThat(statusesList, contains(expectedSortedStatuses));
644     }
645
646
647
648     @Test
649     public void addSomeVidStatuses_getThem_verifyGetInsertedWithoutDuplicates(){
650         ImmutableList<JobStatus> statusesToBeInserted = ImmutableList.of(PENDING, IN_PROGRESS, IN_PROGRESS, COMPLETED);
651         UUID jobUuid = UUID.randomUUID();
652         statusesToBeInserted.forEach(status->
653             {
654                 asyncInstantiationBL.auditVidStatus(jobUuid, status);
655             });
656         List<String> statusesFromDB = asyncInstantiationBL.getAuditStatuses(jobUuid, SourceStatus.VID).stream().map(auditStatus -> auditStatus.getJobStatus()).collect(Collectors.toList());
657         List<String> statusesWithoutDuplicates = statusesToBeInserted.stream().distinct().map(x -> x.toString()).collect(Collectors.toList());
658         assertThat(statusesFromDB, is(statusesWithoutDuplicates));
659     }
660
661     @DataProvider
662     public static Object[][] msoAuditStatuses(Method test) {
663         UUID jobUuid = UUID.randomUUID();
664         UUID requestId = UUID.randomUUID();
665         return new Object[][]{
666                 {
667                         jobUuid,
668                         ImmutableList.of(
669                                 new JobAuditStatus(jobUuid, PENDING.toString(), SourceStatus.MSO, null, null),
670                                 new JobAuditStatus(jobUuid, IN_PROGRESS.toString(), SourceStatus.MSO, requestId, null),
671                                 new JobAuditStatus(jobUuid, IN_PROGRESS.toString(), SourceStatus.MSO, requestId, null),
672                                 new JobAuditStatus(jobUuid, IN_PROGRESS.toString(), SourceStatus.MSO, requestId, null),
673                                 new JobAuditStatus(jobUuid, COMPLETED.toString(), SourceStatus.MSO, requestId, null)),
674                         ImmutableList.of(PENDING.toString(), IN_PROGRESS.toString(), COMPLETED.toString()),
675                         "All distinct statuses should be without duplicates"
676                 },
677                 {
678                         jobUuid,
679                         ImmutableList.of(
680                                 new JobAuditStatus(jobUuid, PENDING.toString(), SourceStatus.MSO, null, null),
681                                 new JobAuditStatus(jobUuid, IN_PROGRESS.toString(), SourceStatus.MSO, requestId, null),
682                                 new JobAuditStatus(jobUuid, IN_PROGRESS.toString(), SourceStatus.MSO, requestId, "aa"),
683                                 new JobAuditStatus(jobUuid, IN_PROGRESS.toString(), SourceStatus.MSO, requestId, "aa"),
684                                 new JobAuditStatus(jobUuid, IN_PROGRESS.toString(), SourceStatus.MSO, UUID.randomUUID(), "aa"),
685                                 new JobAuditStatus(jobUuid, COMPLETED.toString(), SourceStatus.MSO, requestId, null)),
686                         ImmutableList.of(PENDING.toString(), IN_PROGRESS.toString(), IN_PROGRESS.toString(),IN_PROGRESS.toString(), COMPLETED.toString()),
687                         "Statuses should be without duplicates only with same requestId and additionalInfo"
688
689                 }
690         };
691     }
692
693     @Test(dataProvider = "msoAuditStatuses")
694     public void addSomeMsoStatuses_getThem_verifyGetInsertedWithoutDuplicates(UUID jobUuid, ImmutableList<JobAuditStatus> msoStatuses, ImmutableList<String> expectedStatuses, String assertionReason) {
695         msoStatuses.forEach(status -> {
696             asyncInstantiationBL.auditMsoStatus(status.getJobId(), status.getJobStatus(), status.getRequestId() != null ? status.getRequestId().toString() : null, status.getAdditionalInfo());
697         });
698         List<String> statusesFromDB = asyncInstantiationBL.getAuditStatuses(jobUuid, SourceStatus.MSO).stream().map(auditStatus -> auditStatus.getJobStatus()).collect(Collectors.toList());
699         assertThat( assertionReason, statusesFromDB, is(expectedStatuses));
700     }
701
702     @Test
703     public void addSameStatusOfVidAndMso_verifyThatBothWereAdded(){
704         UUID jobUuid = UUID.randomUUID();
705         JobStatus sameStatus = IN_PROGRESS;
706         asyncInstantiationBL.auditMsoStatus(jobUuid, sameStatus.toString(),null,null);
707         asyncInstantiationBL.auditVidStatus(jobUuid, sameStatus);
708         List<JobAuditStatus> list = dataAccessService.getList(
709                 JobAuditStatus.class,
710                 String.format(" where JOB_ID = '%s'", jobUuid),
711                 null, null);
712         Assert.assertEquals(list.size(),2);
713         assertThat(list,everyItem(hasProperty("jobStatus", is(sameStatus.toString()))));
714     }
715
716     @Test
717     public void verifyAsyncRequestStatus_canBeReadFromSample() throws IOException {
718         String body = "{" +
719                 "  \"request\": {" +
720                 "    \"requestId\": \"c0011670-0e1a-4b74-945d-8bf5aede1d9c\"," +
721                 "    \"startTime\": \"Mon, 11 Dec 2017 07:27:49 GMT\"," +
722                 "    \"requestScope\": \"service\"," +
723                 "    \"requestType\": \"createInstance\"," +
724                 "    \"instanceReferences\": {" +
725                 "      \"serviceInstanceId\": \"f8791436-8d55-4fde-b4d5-72dd2cf13cfb\"," +
726                 "      \"serviceInstanceName\": \"asdfasdf234234asdf\"," +
727                 "      \"requestorId\": \"il883e\"" +
728                 "    }," +
729                 "    \"requestStatus\": {" +
730                 "      \"requestState\": \"COMPLETE\"," +
731                 "      \"statusMessage\": \"Service Instance was created successfully.\"," +
732                 "      \"percentProgress\": 100," +
733                 "      \"finishTime\": \"Mon, 11 Dec 2017 07:27:53 GMT\"" +
734                 "    }" +
735                 "  }" +
736                 "}";
737         ObjectMapper objectMapper = new ObjectMapper();
738         AsyncRequestStatus asyncRequestStatus = objectMapper.readValue(body, AsyncRequestStatus.class);
739         assertThat(asyncRequestStatus.request.requestStatus.getRequestState(), equalTo("COMPLETE"));
740
741     }
742
743     @Test
744     public void deleteJobInfo_pending_deleted() {
745         doNothing().when(jobsBrokerService).delete(any());
746         UUID uuid = createServicesInfoWithDefaultValues(PENDING);
747         asyncInstantiationBL.deleteJob(uuid);
748         assertNotNull(asyncInstantiationBL.getServiceInfoByJobId(uuid).getDeletedAt(), "service info wasn't deleted");
749     }
750
751     @Test(expectedExceptions = IllegalStateException.class, expectedExceptionsMessageRegExp = DELETE_SERVICE_INFO_STATUS_EXCEPTION_MESSAGE)
752     public void deleteJobInfo_notAllowdStatus_shouldSendError() {
753         UUID uuid = createServicesInfoWithDefaultValues(COMPLETED);
754         doThrow(new IllegalStateException(DELETE_SERVICE_INFO_STATUS_EXCEPTION_MESSAGE)).when(jobsBrokerService).delete(any());
755         try {
756             asyncInstantiationBL.deleteJob(uuid);
757         } catch (Exception e) {
758             assertNull(asyncInstantiationBL.getServiceInfoByJobId(uuid).getDeletedAt(), "service info shouldn't deleted");
759             throw e;
760         }
761     }
762
763     @DataProvider
764     public Object[][] jobStatusesFinal() {
765         return Arrays.stream(Job.JobStatus.values())
766                 .filter(t -> ImmutableList.of(COMPLETED, FAILED, STOPPED).contains(t))
767                 .map(v -> new Object[]{v}).collect(Collectors.toList()).toArray(new Object[][]{});
768     }
769
770     @Test(dataProvider = "jobStatusesFinal")
771     public void whenHideService_theServiceNotReturnedInServiceList(JobStatus jobStatus) {
772         UUID uuidToHide = createServicesInfoWithDefaultValues(jobStatus);
773         UUID uuidToShown = createServicesInfoWithDefaultValues(jobStatus);
774         List<UUID> serviceInfoList = listServicesUUID();
775         assertThat(serviceInfoList, hasItems(uuidToHide, uuidToShown));
776
777         asyncInstantiationBL.hideServiceInfo(uuidToHide);
778         serviceInfoList = listServicesUUID();
779         assertThat(serviceInfoList, hasItem(uuidToShown));
780         assertThat(serviceInfoList, not(hasItem(uuidToHide)));
781
782     }
783
784     protected List<UUID> listServicesUUID() {
785         return asyncInstantiationBL.getAllServicesInfo().stream().map(ServiceInfo::getJobId).collect(Collectors.toList());
786     }
787
788     @DataProvider
789     public Object[][] jobStatusesNotFinal() {
790         return Arrays.stream(Job.JobStatus.values())
791                 .filter(t -> ImmutableList.of(PENDING, IN_PROGRESS, PAUSE).contains(t))
792                 .map(v -> new Object[]{v}).collect(Collectors.toList()).toArray(new Object[][]{});
793     }
794
795     @Test(  dataProvider = "jobStatusesNotFinal",
796             expectedExceptions = OperationNotAllowedException.class,
797             expectedExceptionsMessageRegExp = "jobId.*Service status does not allow hide service, status = .*")
798     public void hideServiceInfo_notAllowedStatus_shouldSendError(JobStatus jobStatus) {
799         UUID uuid = createServicesInfoWithDefaultValues(jobStatus);
800         try {
801             asyncInstantiationBL.hideServiceInfo(uuid);
802         } catch (Exception e) {
803             assertFalse(asyncInstantiationBL.getServiceInfoByJobId(uuid).isHidden(), "service info shouldn't be hidden");
804             throw e;
805         }
806     }
807
808     @Test
809     public void whenUseGetCounterInMultiThreads_EachThreadGetDifferentCounter() throws InterruptedException {
810         int SIZE = 200;
811         ExecutorService executor = Executors.newFixedThreadPool(SIZE);
812         List<Callable<Integer>> tasks = IntStream.rangeClosed(1, SIZE)
813                 .mapToObj(x-> ((Callable<Integer>)() -> asyncInstantiationBL.getCounterForName("a")))
814                 .collect(Collectors.toList());
815         Set<Integer> expectedResults = IntStream.rangeClosed(1, SIZE).boxed().collect(Collectors.toSet());
816         executor.invokeAll(tasks)
817                 .forEach(future -> {
818                     try {
819                         assertTrue( expectedResults.remove(future.get()), "got unexpected counter");
820                     }
821                     catch (Exception e) {
822                         throw new RuntimeException(e);
823                     }
824                 });
825
826         assertThat(expectedResults.size(), is(0));
827     }
828
829     @Test
830     public void whenUseGetCounterForSameName_numbersReturnedByOrder() {
831
832         String name = UUID.randomUUID().toString();
833         int SIZE=10;
834         for (int i=1; i<=SIZE; i++) {
835             assertThat(asyncInstantiationBL.getCounterForName(name), is(i));
836         }
837     }
838
839     @Test
840     public void whenNamedInUsedInAai_getNextNumber() {
841         String name = someCommonStepsAndGetName();
842         ResourceType type = ResourceType.GENERIC_VNF;
843         when(aaiClient.searchNodeTypeByName(name+"_001", type)).thenReturn(aaiNodeQueryResponseNameUsed(type));
844         when(aaiClient.searchNodeTypeByName(name+"_002", type)).thenReturn(aaiNodeQueryResponseNameFree());
845         assertThat(asyncInstantiationBL.getUniqueName(name, type), equalTo(name+"_002"));
846     }
847
848     private String someCommonStepsAndGetName() {
849         mockAaiClientAaiStatusOK();
850         return UUID.randomUUID().toString();
851     }
852
853     private void mockAaiClientAaiStatusOK() {
854         when(aaiClient.searchNodeTypeByName(eq(AsyncInstantiationBusinessLogicImpl.NAME_FOR_CHECK_AAI_STATUS), any())).thenReturn(aaiNodeQueryResponseNameFree());
855     }
856
857     @Test(expectedExceptions=InvalidAAIResponseException.class)
858     public void whenAaiBadResponseCode_throwInvalidAAIResponseException() {
859         String name = someCommonStepsAndGetName();
860         ResourceType type = ResourceType.SERVICE_INSTANCE;
861         when(aaiClient.searchNodeTypeByName(name+"_001", type)).thenReturn(aaiNodeQueryBadResponse());
862         asyncInstantiationBL.getUniqueName(name, type);
863     }
864
865     @Test(expectedExceptions=MaxRetriesException.class)
866     public void whenAaiAlwaysReturnNameUsed_throwInvalidAAIResponseException() {
867         String name = someCommonStepsAndGetName();
868         ResourceType type = ResourceType.VF_MODULE;
869         when(aaiClient.searchNodeTypeByName(any(), eq(type))).thenReturn(aaiNodeQueryResponseNameUsed(type));
870         asyncInstantiationBL.setMaxRetriesGettingFreeNameFromAai(10);
871         asyncInstantiationBL.getUniqueName(name, type);
872     }
873
874     @Test
875     public void testFormattingOfNameAndCounter() {
876         AsyncInstantiationBusinessLogicImpl bl = (AsyncInstantiationBusinessLogicImpl) asyncInstantiationBL;
877         assertThat(bl.formatNameAndCounter("x", 3), equalTo("x_003"));
878         assertThat(bl.formatNameAndCounter("x", 99), equalTo("x_099"));
879         assertThat(bl.formatNameAndCounter("x", 100), equalTo("x_100"));
880         assertThat(bl.formatNameAndCounter("x", 1234), equalTo("x_1234"));
881     }*/
882 }