1 package org.onap.vid.services;
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.domain.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.*;
46 import javax.inject.Inject;
47 import java.io.IOException;
48 import java.lang.reflect.Method;
50 import java.time.Instant;
51 import java.time.LocalDateTime;
52 import java.time.ZoneId;
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;
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.*;
71 @ContextConfiguration(classes = {DataSourceConfig.class, SystemProperties.class, MockedAaiClientAndFeatureManagerConfig.class})
72 public class AsyncInstantiationBusinessLogicTest extends AsyncInstantiationBaseTest {
76 private DataAccessService dataAccessService;
79 private JobAdapter jobAdapter;
82 private JobsBrokerService jobsBrokerService;
87 private SessionFactory sessionFactory;
89 private AsyncInstantiationBusinessLogic asyncInstantiationBL;
91 private int serviceCount = 0;
93 private static final String UPDATE_SERVICE_INFO_EXCEPTION_MESSAGE =
94 "Failed to retrieve job with uuid .* from ServiceInfo table. Instances found: .*";
96 private static final String DELETE_SERVICE_INFO_STATUS_EXCEPTION_MESSAGE =
97 "Service status does not allow deletion from the queue";
100 void initServicesInfoService() {
101 MockitoAnnotations.initMocks(this);
102 asyncInstantiationBL = new AsyncInstantiationBusinessLogicImpl(dataAccessService, jobAdapter, jobsBrokerService, sessionFactory, aaiClient);
103 createInstanceParamsMaps();
108 mockAaiClientAnyNameFree();
112 void resetServiceCount() {
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());
125 private void createNewTestServicesInfoForFilter(String userId) {
126 LocalDateTime createdDate, modifiedDate;
127 LocalDateTime NOW = LocalDateTime.now();
131 uuid = UUID.randomUUID();
133 createdDate = NOW.minusYears(1);
134 addNewServiceInfo(uuid, userId, "Old", createdDate, createdDate, COMPLETED, false);
136 uuid = UUID.randomUUID();
138 createdDate = NOW.minusDays(20);
139 modifiedDate = NOW.minusDays(19);
140 addNewServiceInfo(uuid, userId, "Hidden", createdDate, modifiedDate, PAUSE, true);
142 createNewTestServicesInfo(String.valueOf(userId));
145 private void createNewTestServicesInfo(String userId) {
147 LocalDateTime createdDate, modifiedDate;
148 LocalDateTime NOW = LocalDateTime.now();
151 uuid = UUID.randomUUID();
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);
158 uuid = UUID.randomUUID();
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);
167 modifiedDate = NOW.minusDays(19);
168 addNewServiceInfo(uuid, userId, "service instance 1", createdDate, modifiedDate, FAILED, false);
171 // Job to a different user
172 uuid = UUID.randomUUID();
175 createdDate = NOW.minusMonths(2);
176 addNewServiceInfo(uuid, "2221", "service instance 7", createdDate, createdDate, COMPLETED, false);
180 private UUID createServicesInfoWithDefaultValues(Job.JobStatus status) {
182 LocalDateTime NOW = LocalDateTime.now();
185 uuid = UUID.randomUUID();
186 addNewJob(uuid, status);
188 addNewServiceInfo(uuid, null, "service instance 1", NOW, NOW, status, false);
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;
201 private static Date toDate(LocalDateTime localDateTime) {
202 return Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
205 private LocalDateTime fromDate(Date date) {
206 return Instant.ofEpochMilli(date.getTime())
207 .atZone(ZoneId.systemDefault())
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));
222 serviceInfo.setHidden(isHidden);
223 dataAccessService.saveDomainObject(serviceInfo, getPropsMap());
224 setCreateDateToServiceInfo(uuid, createDate);
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);
242 private void addNewJob(UUID uuid) {
243 addNewJob(uuid, null);
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());
254 public void testServiceInfoAreOrderedAsExpected() {
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));
263 public void testServiceInfoAreFilteredAsExpected() {
265 createNewTestServicesInfoForFilter(String.valueOf(userId));
266 List<ServiceInfo> expectedOrderServiceInfo = getFullList();
268 List<ServiceInfo> expectedFilterByUser = expectedOrderServiceInfo.stream().filter(x ->
269 !x.getServiceInstanceName().equals("Old") && !x.getServiceInstanceName().equals("Hidden")
271 ).collect(Collectors.toList());
274 List<ServiceInfo> serviceInfoFilteredByUser = asyncInstantiationBL.getAllServicesInfo();
275 assertThat("Services aren't ordered filtered as expected", serviceInfoFilteredByUser, equalTo(expectedFilterByUser));
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);
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);
318 protected void verifySearchNodeTypeByName(String unique, String resourceName, ResourceType serviceInstance) {
319 verify(aaiClient, times(1)).searchNodeTypeByName(resourceName + unique, serviceInstance);
322 private HashMap<String, Object> getPropsMap() {
323 HashMap<String, Object> props = new HashMap<>();
324 props.put(FusionObject.Parameters.PARAM_USERID, 0);
328 @Test(enabled = false) //probably not needed with name uniqueness feature
329 public void pushBulkJob_bulkWithSize3_instancesNamesAreExactlyAsExpected() {
332 final ServiceInstantiation request = generateMockServiceInstantiationPayload(
334 createVnfList(instanceParamsMapWithoutParams, Collections.EMPTY_LIST, true),
335 bulkSize, true,PROJECT_NAME, true
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);
344 final List<UUID> uuids = asyncInstantiationBL.pushBulkJob(request, "myUserId");
347 ArgumentCaptor<ServiceInstantiation> serviceInstantiationCaptor = new ArgumentCaptor<ServiceInstantiation>();
348 verify(jobAdapter, times(bulkSize)).createJob(any(), serviceInstantiationCaptor.capture(), any(), any(), any());
350 assertThat(serviceInstantiationCaptor.getAllValues().stream().map(v -> v.getInstanceName()).collect(Collectors.toList()),
351 containsInAnyOrder("vPE_Service_001", "vPE_Service_002", "vPE_Service_003"));
353 assertThat(uuids, hasSize(bulkSize));
357 public void generateMockServiceInstantiationPayload_serializeBackAndForth_sourceShouldBeTheSame() throws IOException {
358 ServiceInstantiation serviceInstantiationPayload = generateMockServiceInstantiationPayload(
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);
365 final ServiceInstantiation asObject = mapper.readValue(asString, ServiceInstantiation.class);
366 final String asString2 = mapper.writeValueAsString(asObject);
368 JsonAssert.assertJsonEquals(asString, asString2);
371 public static class ServiceInfoComparator implements Comparator<ServiceInfo> {
374 public int compare(ServiceInfo o1, ServiceInfo o2) {
377 compare = o1.getCreatedBulkDate().compareTo(o2.getCreatedBulkDate());
382 // check jobStatus priority
383 int o1Priority = getPriority(o1);
384 int o2Priority = getPriority(o2);
385 compare = o1Priority - o2Priority;
390 // check statusModifiedDate
391 return o1.getStatusModifiedDate().compareTo(o2.getStatusModifiedDate());
394 private int getPriority(ServiceInfo o) throws JSONException {
395 Job.JobStatus status = o.getJobStatus();
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)}
422 private ServiceInstantiation generateMockServiceInstantiationPayload(boolean isPause, Map<String, Vnf> vnfs) {
423 return generateMockServiceInstantiationPayload(isPause, vnfs, 1, true, PROJECT_NAME, false);
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);
434 Optional<ServiceInfo> optionalServiceInfo = getJobById(uuid);
435 assertThat(optionalServiceInfo.get().getServiceInstanceName(), equalTo(STEPH_CURRY));
436 assertThat(optionalServiceInfo.get().getJobStatus(), equalTo(Job.JobStatus.IN_PROGRESS));
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();
444 private UUID createJobAndServiceInfo() {
445 UUID uuid = UUID.randomUUID();
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());
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"));
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"));
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"));
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"));
486 public void createServiceInfo_WithUserProvidedNamingFalse_ServiceInfoIsAsExpected() throws IOException {
487 createServiceInfo_WithUserProvidedNamingFalse_ServiceInfoIsAsExpected(true);
491 public void createServiceInfo_WithUserProvidedNamingFalseAndNoVfmodules_ServiceInfoIsAsExpected() throws IOException {
492 createServiceInfo_WithUserProvidedNamingFalse_ServiceInfoIsAsExpected(false);
495 private void createServiceInfo_WithUserProvidedNamingFalse_ServiceInfoIsAsExpected(boolean withVfmodules) throws IOException {
496 ServiceInstantiation serviceInstantiationPayload = generateMockServiceInstantiationPayload(true,
497 createVnfList(vfModuleInstanceParamsMapWithParamsToRemove, Collections.EMPTY_LIST, false),
499 false,PROJECT_NAME, true);
502 resource = this.getClass().getResource("/payload_jsons/bulk_service_request_ecomp_naming.json");
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");
509 RequestDetailsWrapper<ServiceInstantiationRequestDetails> result =
510 asyncInstantiationBL.generateServiceInstantiationRequest(null, serviceInstantiationPayload, "az2016");
512 String expected = IOUtils.toString(resource, "UTF-8");
513 MsoOperationalEnvironmentTest.assertThatExpectationIsLikeObject(expected, result);
517 public void checkIfNullProjectNameSentToMso(){
518 ServiceInstantiation serviceInstantiationPayload = generateMockServiceInstantiationPayload(true,
519 createVnfList(vfModuleInstanceParamsMapWithParamsToRemove, Collections.EMPTY_LIST, 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),
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"));
539 public void pushBulkJob_verifyCreatedDateBehavior_createdDateIsTheSameForAllServicesInSameBulk() {
540 LocalDateTime startTestDate = LocalDateTime.now().withNano(0);
541 final ServiceInstantiation request = generateMockServiceInstantiationPayload(
543 createVnfList(instanceParamsMapWithoutParams, Collections.EMPTY_LIST, true),
544 100, true,PROJECT_NAME, true
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);
552 asyncInstantiationBL.pushBulkJob(request, "myUserId");
553 List<ServiceInfo> serviceInfoList = dataAccessService.getList(ServiceInfo.class, getPropsMap());
555 List<Date> creationDates = new ArrayList<>();
556 for (ServiceInfo serviceInfo : serviceInfoList) {
557 creationDates.add(serviceInfo.getCreatedBulkDate());
559 LocalDateTime endTestDate = LocalDateTime.now();
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));
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}
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));
591 private void createNewAuditStatus(JobAuditStatus auditStatus)
593 Date createdDate= auditStatus.getCreated();
594 dataAccessService.saveDomainObject(auditStatus, getPropsMap());
595 setDateToStatus(auditStatus.getSource(), auditStatus.getJobStatus(), createdDate);
600 private static final String MSO_ARBITRARY_STATUS = "completed mso status";
603 public static Object[][] auditStatuses(Method test) {
604 return new Object[][]{
607 new String[]{ JobStatus.PENDING.toString(), JobStatus.IN_PROGRESS.toString()}
610 new String[]{ JobStatus.IN_PROGRESS.toString(), MSO_ARBITRARY_STATUS }
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);
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));
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->
654 asyncInstantiationBL.auditVidStatus(jobUuid, status);
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));
662 public static Object[][] msoAuditStatuses(Method test) {
663 UUID jobUuid = UUID.randomUUID();
664 UUID requestId = UUID.randomUUID();
665 return new Object[][]{
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"
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"
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());
698 List<String> statusesFromDB = asyncInstantiationBL.getAuditStatuses(jobUuid, SourceStatus.MSO).stream().map(auditStatus -> auditStatus.getJobStatus()).collect(Collectors.toList());
699 assertThat( assertionReason, statusesFromDB, is(expectedStatuses));
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),
712 Assert.assertEquals(list.size(),2);
713 assertThat(list,everyItem(hasProperty("jobStatus", is(sameStatus.toString()))));
717 public void verifyAsyncRequestStatus_canBeReadFromSample() throws IOException {
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\"" +
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\"" +
737 ObjectMapper objectMapper = new ObjectMapper();
738 AsyncRequestStatus asyncRequestStatus = objectMapper.readValue(body, AsyncRequestStatus.class);
739 assertThat(asyncRequestStatus.request.requestStatus.getRequestState(), equalTo("COMPLETE"));
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");
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());
756 asyncInstantiationBL.deleteJob(uuid);
757 } catch (Exception e) {
758 assertNull(asyncInstantiationBL.getServiceInfoByJobId(uuid).getDeletedAt(), "service info shouldn't deleted");
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[][]{});
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));
777 asyncInstantiationBL.hideServiceInfo(uuidToHide);
778 serviceInfoList = listServicesUUID();
779 assertThat(serviceInfoList, hasItem(uuidToShown));
780 assertThat(serviceInfoList, not(hasItem(uuidToHide)));
784 protected List<UUID> listServicesUUID() {
785 return asyncInstantiationBL.getAllServicesInfo().stream().map(ServiceInfo::getJobId).collect(Collectors.toList());
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[][]{});
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);
801 asyncInstantiationBL.hideServiceInfo(uuid);
802 } catch (Exception e) {
803 assertFalse(asyncInstantiationBL.getServiceInfoByJobId(uuid).isHidden(), "service info shouldn't be hidden");
809 public void whenUseGetCounterInMultiThreads_EachThreadGetDifferentCounter() throws InterruptedException {
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)
819 assertTrue( expectedResults.remove(future.get()), "got unexpected counter");
821 catch (Exception e) {
822 throw new RuntimeException(e);
826 assertThat(expectedResults.size(), is(0));
830 public void whenUseGetCounterForSameName_numbersReturnedByOrder() {
832 String name = UUID.randomUUID().toString();
834 for (int i=1; i<=SIZE; i++) {
835 assertThat(asyncInstantiationBL.getCounterForName(name), is(i));
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"));
848 private String someCommonStepsAndGetName() {
849 mockAaiClientAaiStatusOK();
850 return UUID.randomUUID().toString();
853 private void mockAaiClientAaiStatusOK() {
854 when(aaiClient.searchNodeTypeByName(eq(AsyncInstantiationBusinessLogicImpl.NAME_FOR_CHECK_AAI_STATUS), any())).thenReturn(aaiNodeQueryResponseNameFree());
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);
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);
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"));