Update onboarding upload status during processing 24/126624/8
authorandre.schmid <andre.schmid@est.tech>
Tue, 18 Jan 2022 10:16:37 +0000 (10:16 +0000)
committerMichael Morris <michael.morris@est.tech>
Tue, 25 Jan 2022 09:57:30 +0000 (09:57 +0000)
Updates the onboarding upload status during the VSP package processing

Change-Id: Idc705220ad26e62577b52c6f9126aeae51a33fe0
Issue-ID: SDC-3848
Signed-off-by: andre.schmid <andre.schmid@est.tech>
openecomp-be/api/openecomp-sdc-rest-webapp/openecomp-sdc-common-rest/src/main/resources/errorCodesToResponseStatusMapping.json
openecomp-be/api/openecomp-sdc-rest-webapp/vendor-software-products-rest/vendor-software-products-rest-services/src/main/java/org/openecomp/sdcrests/vsp/rest/exception/OrchestrationTemplateCandidateUploadManagerExceptionSupplier.java
openecomp-be/api/openecomp-sdc-rest-webapp/vendor-software-products-rest/vendor-software-products-rest-services/src/main/java/org/openecomp/sdcrests/vsp/rest/services/OrchestrationTemplateCandidateImpl.java
openecomp-be/api/openecomp-sdc-rest-webapp/vendor-software-products-rest/vendor-software-products-rest-services/src/main/java/org/openecomp/sdcrests/vsp/rest/services/OrchestrationTemplateCandidateUploadManager.java
openecomp-be/api/openecomp-sdc-rest-webapp/vendor-software-products-rest/vendor-software-products-rest-services/src/main/java/org/openecomp/sdcrests/vsp/rest/services/OrchestrationTemplateCandidateUploadManagerImpl.java
openecomp-be/api/openecomp-sdc-rest-webapp/vendor-software-products-rest/vendor-software-products-rest-services/src/test/java/org/openecomp/sdcrests/vsp/rest/services/OrchestrationTemplateCandidateImplTest.java
openecomp-be/api/openecomp-sdc-rest-webapp/vendor-software-products-rest/vendor-software-products-rest-services/src/test/java/org/openecomp/sdcrests/vsp/rest/services/OrchestrationTemplateCandidateUploadManagerImplTest.java
openecomp-be/lib/openecomp-sdc-vendor-software-product-lib/openecomp-sdc-vendor-software-product-api/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/dao/type/VspUploadStatus.java
openecomp-be/lib/openecomp-sdc-vendor-software-product-lib/openecomp-sdc-vendor-software-product-core/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/errors/VendorSoftwareProductErrorCodes.java

index 95b2c07..96c06ae 100644 (file)
@@ -4,8 +4,11 @@
   "VSP_PROCESSING_IN_PROGRESS": "FORBIDDEN",
   "VSP_CREATE_UPLOAD_LOCK_ERROR": "INTERNAL_SERVER_ERROR",
   "VSP_UPDATE_UPLOAD_LOCK_ERROR": "INTERNAL_SERVER_ERROR",
+  "VSP_UNABLE_UPDATE_UPLOAD_STATUS_ERROR": "INTERNAL_SERVER_ERROR",
   "VSP_UPLOAD_LOCK_NOT_FOUND_ERROR": "NOT_FOUND",
+  "VSP_UPLOAD_STATUS_NOT_FOUND_ERROR": "NOT_FOUND",
   "VSP_UPLOAD_ALREADY_FINISHED_ERROR": "INTERNAL_SERVER_ERROR",
+  "VSP_UPLOAD_ALREADY_IN_STATUS_ERROR": "BAD_REQUEST",
   "ORCHESTRATION_NOT_FOUND": "NOT_FOUND",
   "UPLOAD_INVALID" : "PRECONDITION_FAILED",
   "PACKAGE_NOT_FOUND": "NOT_FOUND",
index 2eb0261..643f59b 100644 (file)
@@ -25,7 +25,9 @@ import static org.openecomp.sdc.vendorsoftwareproduct.errors.VendorSoftwareProdu
 import static org.openecomp.sdc.vendorsoftwareproduct.errors.VendorSoftwareProductErrorCodes.VSP_PROCESSING_IN_PROGRESS;
 import static org.openecomp.sdc.vendorsoftwareproduct.errors.VendorSoftwareProductErrorCodes.VSP_UPDATE_UPLOAD_LOCK_ERROR;
 import static org.openecomp.sdc.vendorsoftwareproduct.errors.VendorSoftwareProductErrorCodes.VSP_UPLOAD_ALREADY_FINISHED_ERROR;
+import static org.openecomp.sdc.vendorsoftwareproduct.errors.VendorSoftwareProductErrorCodes.VSP_UPLOAD_ALREADY_IN_STATUS_ERROR;
 import static org.openecomp.sdc.vendorsoftwareproduct.errors.VendorSoftwareProductErrorCodes.VSP_UPLOAD_LOCK_NOT_FOUND_ERROR;
+import static org.openecomp.sdc.vendorsoftwareproduct.errors.VendorSoftwareProductErrorCodes.VSP_UPLOAD_STATUS_NOT_FOUND_ERROR;
 
 import java.util.UUID;
 import java.util.function.Supplier;
@@ -42,12 +44,19 @@ public class OrchestrationTemplateCandidateUploadManagerExceptionSupplier {
     }
 
     public static Supplier<CoreException> vspUploadAlreadyInProgress(final String vspId, final String vspVersionId) {
-        final String errorMsg = String.format("There is a processing in progress for the VSP %s, version %s", vspId, vspVersionId);
+        final String errorMsg = String.format("There is a processing in progress for the VSP '%s', version '%s'", vspId, vspVersionId);
         return () -> new CoreException(new ErrorCodeBuilder().withId(VSP_PROCESSING_IN_PROGRESS).withMessage(errorMsg).build());
     }
 
     public static Supplier<CoreException> couldNotCreateLock(final String vspId, final String vspVersionId, final Exception exception) {
-        final String errorMsg = String.format("Could not create a lock for the VSP %s, version %s", vspId, vspVersionId);
+        final String errorMsg = String.format("Could not create a lock for the VSP '%s', version '%s'", vspId, vspVersionId);
+        final ErrorCode errorCode = new ErrorCodeBuilder().withId(VSP_CREATE_UPLOAD_LOCK_ERROR).withMessage(errorMsg).build();
+        return () -> new CoreException(errorCode, exception);
+    }
+
+    public static Supplier<CoreException> couldNotUpdateStatus(final String vspId, final String vspVersionId, final VspUploadStatus status,
+                                                               final Exception exception) {
+        final String errorMsg = String.format("Could not update upload status for the VSP '%s', version '%s', to '%s'", vspId, vspVersionId, status);
         final ErrorCode errorCode = new ErrorCodeBuilder().withId(VSP_CREATE_UPLOAD_LOCK_ERROR).withMessage(errorMsg).build();
         return () -> new CoreException(errorCode, exception);
     }
@@ -59,11 +68,23 @@ public class OrchestrationTemplateCandidateUploadManagerExceptionSupplier {
     }
 
     public static Supplier<CoreException> couldNotFindLock(final UUID lockId, final String vspId, final String vspVersionId) {
-        final String errorMsg = String.format("Could not find lock '%s' for the VSP %s, version %s", lockId, vspId, vspVersionId);
+        final String errorMsg = String.format("Could not find lock '%s' for the VSP '%s', version '%s'", lockId, vspId, vspVersionId);
         final ErrorCode errorCode = new ErrorCodeBuilder().withId(VSP_UPLOAD_LOCK_NOT_FOUND_ERROR).withMessage(errorMsg).build();
         return () -> new CoreException(errorCode);
     }
 
+    public static Supplier<CoreException> couldNotFindStatus(final String vspId, final String vspVersionId) {
+        final String errorMsg = String.format("Could not find upload status for the VSP '%s', version '%s'", vspId, vspVersionId);
+        final ErrorCode errorCode = new ErrorCodeBuilder().withId(VSP_UPLOAD_STATUS_NOT_FOUND_ERROR).withMessage(errorMsg).build();
+        return () -> new CoreException(errorCode);
+    }
+
+    public static Supplier<CoreException> alreadyInStatusBeingUpdated(final String vspId, final String vspVersionId, final VspUploadStatus status) {
+        final String errorMsg = String.format("The upload for the VSP '%s', version '%s' is already in the status '%s'", status, vspId, vspVersionId);
+        final ErrorCode errorCode = new ErrorCodeBuilder().withId(VSP_UPLOAD_ALREADY_IN_STATUS_ERROR).withMessage(errorMsg).build();
+        return () -> new CoreException(errorCode);
+    }
+
     public static Supplier<CoreException> uploadAlreadyFinished(final UUID lockId, final String vspId, final String vspVersionId) {
         final String errorMsg = String.format("The upload was already finished for lock '%s', VSP '%s', version '%s'", lockId, vspId, vspVersionId);
         final ErrorCode errorCode = new ErrorCodeBuilder().withId(VSP_UPLOAD_ALREADY_FINISHED_ERROR).withMessage(errorMsg).build();
@@ -75,10 +96,17 @@ public class OrchestrationTemplateCandidateUploadManagerExceptionSupplier {
     }
 
     public static Supplier<IllegalArgumentException> invalidCompleteStatus(final VspUploadStatus status) {
-        String errorMsg = String.format("Invalid complete status '%s'. Expecting one of: %s",
+        final String errorMsg = String.format("Invalid complete status '%s'. Expecting one of: %s",
             status,
             VspUploadStatus.getCompleteStatus().stream().map(Enum::name).collect(Collectors.joining(", "))
         );
         return () -> new IllegalArgumentException(errorMsg);
     }
+
+    public static Supplier<IllegalArgumentException> invalidCompletionStatus(final VspUploadStatus status) {
+        final String errorMsg = String.format("Can't update to a status that represents a upload completion as '%s'", status);
+        return () -> new IllegalArgumentException(errorMsg);
+    }
+
+
 }
index 1477ce1..6615447 100644 (file)
@@ -152,6 +152,8 @@ public class OrchestrationTemplateCandidateImpl implements OrchestrationTemplate
                 fileToUploadBytes = fileToUpload.getObject(byte[].class);
             }
 
+
+            vspUploadStatus = orchestrationTemplateCandidateUploadManager.putUploadInValidation(vspId, versionId, user);
             final var onboardingPackageProcessor =
                 new OnboardingPackageProcessor(filename, fileToUploadBytes, new CnfPackageValidator(), artifactInfo);
             final ErrorMessage[] errorMessages = onboardingPackageProcessor.getErrorMessages().toArray(new ErrorMessage[0]);
@@ -166,13 +168,14 @@ public class OrchestrationTemplateCandidateImpl implements OrchestrationTemplate
             }
             final var version = new Version(versionId);
             final var vspDetails = vendorSoftwareProductManager.getVsp(vspId, version);
+            vspUploadStatus = orchestrationTemplateCandidateUploadManager.putUploadInProcessing(vspId, versionId, user);
             response = processOnboardPackage(onboardPackageInfo, vspDetails, errorMessages);
             final UploadFileResponseDto entity = (UploadFileResponseDto) response.getEntity();
             if (artifactStorageManager.isEnabled()) {
-                if (!entity.getErrors().isEmpty()) {
-                    artifactStorageManager.delete(artifactInfo);
-                } else {
+                if (entity.getErrors().isEmpty()) {
                     artifactStorageManager.put(vspId, versionId + ".reduced", new ByteArrayInputStream(fileToUploadBytes));
+                } else {
+                    artifactStorageManager.delete(artifactInfo);
                 }
             }
             orchestrationTemplateCandidateUploadManager
index 0f33580..c4b3e2e 100644 (file)
@@ -51,6 +51,26 @@ public interface OrchestrationTemplateCandidateUploadManager {
     VspUploadStatusDto putUploadAsFinished(final String vspId, final String vspVersionId, final UUID lockId, final VspUploadStatus completionStatus,
                                            final String user);
 
+    /**
+     * Updates the upload status to a validation state.
+     *
+     * @param vspId        the Vendor Software Product id
+     * @param vspVersionId the Vendor Software Product version id
+     * @param user         the current user
+     * @return the updated upload status
+     */
+    VspUploadStatusDto putUploadInValidation(final String vspId, final String vspVersionId, final String user);
+
+    /**
+     * Updates the upload status to a processing state.
+     *
+     * @param vspId        the Vendor Software Product id
+     * @param vspVersionId the Vendor Software Product version id
+     * @param user         the current user
+     * @return the updated upload status
+     */
+    VspUploadStatusDto putUploadInProcessing(String vspId, String vspVersionId, String user);
+
     /**
      * Finds the latest upload status for a given Vendor Software Product version.
      *
index d7cfe04..df280bd 100644 (file)
 
 package org.openecomp.sdcrests.vsp.rest.services;
 
+import static org.openecomp.sdcrests.vsp.rest.exception.OrchestrationTemplateCandidateUploadManagerExceptionSupplier.alreadyInStatusBeingUpdated;
 import static org.openecomp.sdcrests.vsp.rest.exception.OrchestrationTemplateCandidateUploadManagerExceptionSupplier.couldNotCreateLock;
 import static org.openecomp.sdcrests.vsp.rest.exception.OrchestrationTemplateCandidateUploadManagerExceptionSupplier.couldNotFindLock;
+import static org.openecomp.sdcrests.vsp.rest.exception.OrchestrationTemplateCandidateUploadManagerExceptionSupplier.couldNotFindStatus;
 import static org.openecomp.sdcrests.vsp.rest.exception.OrchestrationTemplateCandidateUploadManagerExceptionSupplier.couldNotUpdateLock;
+import static org.openecomp.sdcrests.vsp.rest.exception.OrchestrationTemplateCandidateUploadManagerExceptionSupplier.couldNotUpdateStatus;
+import static org.openecomp.sdcrests.vsp.rest.exception.OrchestrationTemplateCandidateUploadManagerExceptionSupplier.invalidCompletionStatus;
 import static org.openecomp.sdcrests.vsp.rest.exception.OrchestrationTemplateCandidateUploadManagerExceptionSupplier.uploadAlreadyFinished;
 import static org.openecomp.sdcrests.vsp.rest.exception.OrchestrationTemplateCandidateUploadManagerExceptionSupplier.vspUploadAlreadyInProgress;
 
@@ -58,28 +62,31 @@ public class OrchestrationTemplateCandidateUploadManagerImpl implements Orchestr
 
     private static final Logger LOGGER = LoggerFactory.getLogger(OrchestrationTemplateCandidateUploadManagerImpl.class);
 
-    private final VspUploadStatusRecordDao uploadManagerDao;
+    private final VspUploadStatusRecordDao vspUploadStatusRecordDao;
     private final VspUploadStatusRecordMapper vspUploadStatusRecordMapper;
     private final VendorSoftwareProductManager vendorSoftwareProductManager;
     private final Lock startUploadLock;
+    private final Lock updateStatusLock;
 
     @Autowired
     public OrchestrationTemplateCandidateUploadManagerImpl(
-        @Qualifier("vsp-upload-status-record-dao-impl") final VspUploadStatusRecordDao uploadManagerDao) {
+        @Qualifier("vsp-upload-status-record-dao-impl") final VspUploadStatusRecordDao vspUploadStatusRecordDao) {
 
-        this.uploadManagerDao = uploadManagerDao;
+        this.vspUploadStatusRecordDao = vspUploadStatusRecordDao;
         this.vendorSoftwareProductManager = VspManagerFactory.getInstance().createInterface();
         this.vspUploadStatusRecordMapper = new VspUploadStatusRecordMapper();
         startUploadLock = new ReentrantLock();
+        updateStatusLock = new ReentrantLock();
     }
 
     //for tests purpose
-    OrchestrationTemplateCandidateUploadManagerImpl(final VspUploadStatusRecordDao uploadManagerDao,
+    OrchestrationTemplateCandidateUploadManagerImpl(final VspUploadStatusRecordDao vspUploadStatusRecordDao,
                                                     final VendorSoftwareProductManager vendorSoftwareProductManager) {
-        this.uploadManagerDao = uploadManagerDao;
+        this.vspUploadStatusRecordDao = vspUploadStatusRecordDao;
         this.vendorSoftwareProductManager = vendorSoftwareProductManager;
         this.vspUploadStatusRecordMapper = new VspUploadStatusRecordMapper();
         startUploadLock = new ReentrantLock();
+        updateStatusLock = new ReentrantLock();
     }
 
     @Override
@@ -90,7 +97,7 @@ public class OrchestrationTemplateCandidateUploadManagerImpl implements Orchestr
         final VspUploadStatusRecord vspUploadStatusRecord;
         startUploadLock.lock();
         try {
-            final List<VspUploadStatusRecord> uploadInProgressList = uploadManagerDao.findAllInProgress(vspId, vspVersionId);
+            final List<VspUploadStatusRecord> uploadInProgressList = vspUploadStatusRecordDao.findAllInProgress(vspId, vspVersionId);
             if (!uploadInProgressList.isEmpty()) {
                 throw vspUploadAlreadyInProgress(vspId, vspVersionId).get();
             }
@@ -102,7 +109,7 @@ public class OrchestrationTemplateCandidateUploadManagerImpl implements Orchestr
             vspUploadStatusRecord.setLockId(UUID.randomUUID());
             vspUploadStatusRecord.setCreated(new Date());
 
-            uploadManagerDao.create(vspUploadStatusRecord);
+            vspUploadStatusRecordDao.create(vspUploadStatusRecord);
             LOGGER.debug("Upload lock '{}' created for VSP id '{}', version '{}'", vspUploadStatusRecord.getLockId(), vspId, vspVersionId);
         } catch (final CoreException e) {
             throw e;
@@ -115,6 +122,57 @@ public class OrchestrationTemplateCandidateUploadManagerImpl implements Orchestr
         return vspUploadStatusRecordMapper.applyMapping(vspUploadStatusRecord, VspUploadStatusDto.class);
     }
 
+    @Override
+    public VspUploadStatusDto putUploadInValidation(final String vspId, final String vspVersionId, final String user) {
+        return updateToNotFinalStatus(vspId, vspVersionId, VspUploadStatus.VALIDATING, user);
+    }
+
+    @Override
+    public VspUploadStatusDto putUploadInProcessing(final String vspId, final String vspVersionId, final String user) {
+        return updateToNotFinalStatus(vspId, vspVersionId, VspUploadStatus.PROCESSING, user);
+    }
+
+    private VspUploadStatusDto updateToNotFinalStatus(final String vspId, final String vspVersionId, final VspUploadStatus status, final String user) {
+        LOGGER.debug("Updating upload status to '{}' for VSP id '{}', version '{}', triggered by user '{}'", status, vspId, vspVersionId, user);
+        if (status.isCompleteStatus()) {
+            throw invalidCompletionStatus(status).get();
+        }
+        updateStatusLock.lock();
+        try {
+            final Optional<VspUploadStatusRecord> vspUploadStatusRecordOptional = vspUploadStatusRecordDao.findLatest(vspId, vspVersionId);
+            if (vspUploadStatusRecordOptional.isEmpty()) {
+                throw couldNotFindStatus(vspId, vspVersionId).get();
+            }
+
+            final VspUploadStatusRecord vspUploadStatusRecord = vspUploadStatusRecordOptional.get();
+            final VspUploadStatus currentStatus = vspUploadStatusRecord.getStatus();
+            if (currentStatus == status) {
+                throw alreadyInStatusBeingUpdated(vspId, vspVersionId, status).get();
+            }
+            return updateStatus(vspUploadStatusRecord, status);
+        } finally {
+            updateStatusLock.unlock();
+        }
+    }
+
+    private VspUploadStatusDto updateStatus(final VspUploadStatusRecord vspUploadStatusRecord, final VspUploadStatus status) {
+        final VspUploadStatus currentStatus = vspUploadStatusRecord.getStatus();
+        vspUploadStatusRecord.setStatus(status);
+        vspUploadStatusRecord.setUpdated(new Date());
+
+        final String vspId = vspUploadStatusRecord.getVspId();
+        final String vspVersionId = vspUploadStatusRecord.getVspVersionId();
+        try {
+            vspUploadStatusRecordDao.update(vspUploadStatusRecord);
+            LOGGER.debug("Upload lock '{}' status updated from '{}' to '{}' for VSP id '{}', version '{}'",
+                vspUploadStatusRecord.getLockId(), currentStatus, status, vspId, vspVersionId);
+        } catch (final Exception e) {
+            throw couldNotUpdateStatus(vspId, vspVersionId, status, e).get();
+        }
+
+        return vspUploadStatusRecordMapper.applyMapping(vspUploadStatusRecord, VspUploadStatusDto.class);
+    }
+
     @Override
     public VspUploadStatusDto putUploadAsFinished(final String vspId, final String vspVersionId, final UUID lockId, final VspUploadStatus completionStatus,
                                                   final String user) {
@@ -123,7 +181,7 @@ public class OrchestrationTemplateCandidateUploadManagerImpl implements Orchestr
             throw OrchestrationTemplateCandidateUploadManagerExceptionSupplier.invalidCompleteStatus(completionStatus).get();
         }
         final Optional<VspUploadStatusRecord> vspUploadStatusOptional =
-            uploadManagerDao.findByVspIdAndVersionIdAndLockId(vspId, vspVersionId, lockId);
+            vspUploadStatusRecordDao.findByVspIdAndVersionIdAndLockId(vspId, vspVersionId, lockId);
         if (vspUploadStatusOptional.isEmpty()) {
             throw couldNotFindLock(lockId, vspId, vspVersionId).get();
         }
@@ -138,7 +196,7 @@ public class OrchestrationTemplateCandidateUploadManagerImpl implements Orchestr
         vspUploadStatusRecord.setIsComplete(true);
 
         try {
-            uploadManagerDao.update(vspUploadStatusRecord);
+            vspUploadStatusRecordDao.update(vspUploadStatusRecord);
             LOGGER.debug("Upload complete for VSP '{}', version '{}', lock '{}'",
                 vspUploadStatusRecord.getLockId(), vspUploadStatusRecord.getVspId(), vspUploadStatusRecord.getVspVersionId());
         } catch (final Exception e) {
@@ -160,7 +218,7 @@ public class OrchestrationTemplateCandidateUploadManagerImpl implements Orchestr
     public Optional<VspUploadStatusDto> findLatestStatus(final String vspId, final String vspVersionId, final String user) {
         checkVspExists(vspId, vspVersionId);
 
-        final Optional<VspUploadStatusRecord> vspUploadStatus = uploadManagerDao.findLatest(vspId, vspVersionId);
+        final Optional<VspUploadStatusRecord> vspUploadStatus = vspUploadStatusRecordDao.findLatest(vspId, vspVersionId);
         if (vspUploadStatus.isEmpty()) {
             return Optional.empty();
         }
index 6e0231a..758505f 100644 (file)
@@ -25,10 +25,12 @@ import static org.junit.jupiter.api.Assertions.assertFalse;
 import static org.junit.jupiter.api.Assertions.assertNotEquals;
 import static org.junit.jupiter.api.Assertions.assertNotNull;
 import static org.junit.jupiter.api.Assertions.assertNull;
+import static org.junit.jupiter.api.Assertions.assertThrows;
 import static org.junit.jupiter.api.Assertions.assertTrue;
 import static org.junit.jupiter.api.Assertions.fail;
 import static org.mockito.ArgumentMatchers.any;
 import static org.mockito.ArgumentMatchers.eq;
+import static org.mockito.Mockito.verify;
 import static org.mockito.Mockito.when;
 
 import java.io.IOException;
@@ -67,6 +69,7 @@ import org.openecomp.sdc.logging.api.Logger;
 import org.openecomp.sdc.logging.api.LoggerFactory;
 import org.openecomp.sdc.vendorsoftwareproduct.OrchestrationTemplateCandidateManager;
 import org.openecomp.sdc.vendorsoftwareproduct.VendorSoftwareProductManager;
+import org.openecomp.sdc.vendorsoftwareproduct.dao.type.VspUploadStatus;
 import org.openecomp.sdc.vendorsoftwareproduct.types.OrchestrationTemplateActionResponse;
 import org.openecomp.sdc.vendorsoftwareproduct.types.UploadFileResponse;
 import org.openecomp.sdc.vendorsoftwareproduct.types.UploadFileStatus;
@@ -152,6 +155,8 @@ class OrchestrationTemplateCandidateImplTest {
         final String vspId = "vspId";
         final String versionId = "versionId";
         when(orchestrationTemplateCandidateUploadManager.putUploadInProgress(vspId, versionId, user)).thenReturn(new VspUploadStatusDto());
+        when(orchestrationTemplateCandidateUploadManager.putUploadInValidation(vspId, versionId, user)).thenReturn(new VspUploadStatusDto());
+        when(orchestrationTemplateCandidateUploadManager.putUploadInProcessing(vspId, versionId, user)).thenReturn(new VspUploadStatusDto());
         Response response = orchestrationTemplateCandidate
             .upload(vspId, versionId, mockAttachment("filename.zip", this.getClass().getResource("/files/sample-signed.zip")), user);
         assertEquals(Status.OK.getStatusCode(), response.getStatus());
@@ -163,6 +168,8 @@ class OrchestrationTemplateCandidateImplTest {
         final String vspId = "vspId";
         final String versionId = "versionId";
         when(orchestrationTemplateCandidateUploadManager.putUploadInProgress(vspId, versionId, user)).thenReturn(new VspUploadStatusDto());
+        when(orchestrationTemplateCandidateUploadManager.putUploadInValidation(vspId, versionId, user)).thenReturn(new VspUploadStatusDto());
+        when(orchestrationTemplateCandidateUploadManager.putUploadInProcessing(vspId, versionId, user)).thenReturn(new VspUploadStatusDto());
         Response response = orchestrationTemplateCandidate.upload(vspId, versionId,
             mockAttachment("filename.csar", this.getClass().getResource("/files/sample-not-signed.csar")), user);
         assertEquals(Status.OK.getStatusCode(), response.getStatus());
@@ -183,6 +190,8 @@ class OrchestrationTemplateCandidateImplTest {
         when(packageSizeReducer.reduce(any())).thenReturn(bytes);
 
         when(orchestrationTemplateCandidateUploadManager.putUploadInProgress(vspId, versionId, user)).thenReturn(new VspUploadStatusDto());
+        when(orchestrationTemplateCandidateUploadManager.putUploadInValidation(vspId, versionId, user)).thenReturn(new VspUploadStatusDto());
+        when(orchestrationTemplateCandidateUploadManager.putUploadInProcessing(vspId, versionId, user)).thenReturn(new VspUploadStatusDto());
 
         Response response = orchestrationTemplateCandidate.upload(vspId, versionId,
             mockAttachment("filename.csar", this.getClass().getResource("/files/sample-not-signed.csar")), user);
@@ -296,4 +305,22 @@ class OrchestrationTemplateCandidateImplTest {
         }
     }
 
+    @Test
+    void finishUploadMustBeCalledWhenExceptionHappensTest() {
+        //given
+        final VspUploadStatusDto vspUploadStatusDto = new VspUploadStatusDto();
+        vspUploadStatusDto.setLockId(UUID.randomUUID());
+        when(orchestrationTemplateCandidateUploadManager.putUploadInProgress(candidateId, versionId, user)).thenReturn(vspUploadStatusDto);
+        final RuntimeException forcedException = new RuntimeException();
+        when(artifactStorageManager.isEnabled()).thenThrow(forcedException);
+        final Attachment mock = Mockito.mock(Attachment.class);
+        when(mock.getDataHandler()).thenReturn(Mockito.mock(DataHandler.class));
+        //when
+        final RuntimeException actualException = assertThrows(RuntimeException.class,
+            () -> orchestrationTemplateCandidate.upload(candidateId, versionId, mock, user));
+        //then
+        assertEquals(forcedException, actualException);
+        verify(orchestrationTemplateCandidateUploadManager)
+            .putUploadAsFinished(candidateId, versionId, vspUploadStatusDto.getLockId(), VspUploadStatus.ERROR, user);
+    }
 }
index 9a7629f..c156085 100644 (file)
@@ -24,13 +24,19 @@ package org.openecomp.sdcrests.vsp.rest.services;
 
 import static org.junit.jupiter.api.Assertions.assertEquals;
 import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertNotEquals;
 import static org.junit.jupiter.api.Assertions.assertNotNull;
 import static org.junit.jupiter.api.Assertions.assertThrows;
 import static org.junit.jupiter.api.Assertions.assertTrue;
 import static org.mockito.ArgumentMatchers.any;
 import static org.mockito.Mockito.doThrow;
 import static org.mockito.Mockito.when;
+import static org.openecomp.sdcrests.vsp.rest.exception.OrchestrationTemplateCandidateUploadManagerExceptionSupplier.alreadyInStatusBeingUpdated;
+import static org.openecomp.sdcrests.vsp.rest.exception.OrchestrationTemplateCandidateUploadManagerExceptionSupplier.couldNotFindStatus;
+import static org.openecomp.sdcrests.vsp.rest.exception.OrchestrationTemplateCandidateUploadManagerExceptionSupplier.couldNotUpdateStatus;
 
+import java.text.ParseException;
+import java.text.SimpleDateFormat;
 import java.util.Date;
 import java.util.List;
 import java.util.Optional;
@@ -309,4 +315,119 @@ class OrchestrationTemplateCandidateUploadManagerImplTest {
         assertEquals(expectedCoreException.code().message(), actualCoreException.code().message());
     }
 
+    @Test
+    void startValidationSuccessTest() throws ParseException {
+        //given
+        final String vspId = "vspId";
+        final String vspVersionId = "vspVersionId";
+        final UUID lockId = UUID.randomUUID();
+        final String username = "username";
+        final Date created = new SimpleDateFormat("dd/MM/yyyy").parse("01/01/1900");
+        final Date updated = new SimpleDateFormat("dd/MM/yyyy").parse("02/01/1900");
+        final VspUploadStatusRecord vspUploadStatusRecord = new VspUploadStatusRecord();
+        vspUploadStatusRecord.setVspId(vspId);
+        vspUploadStatusRecord.setVspVersionId(vspVersionId);
+        vspUploadStatusRecord.setLockId(lockId);
+        vspUploadStatusRecord.setCreated(created);
+        vspUploadStatusRecord.setUpdated(updated);
+        vspUploadStatusRecord.setStatus(VspUploadStatus.UPLOADING);
+        when(vspUploadStatusRecordDao.findLatest(vspId, vspVersionId)).thenReturn(Optional.of(vspUploadStatusRecord));
+        //when
+        final VspUploadStatusDto vspUploadStatusDto = packageUploadManagerImpl.putUploadInValidation(vspId, vspVersionId, username);
+        //then
+        assertEquals(VspUploadStatus.VALIDATING, vspUploadStatusDto.getStatus());
+        assertNotEquals(updated, vspUploadStatusDto.getUpdated());
+        assertEquals(vspId, vspUploadStatusDto.getVspId());
+        assertEquals(vspVersionId, vspUploadStatusDto.getVspVersionId());
+        assertEquals(lockId, vspUploadStatusDto.getLockId());
+        assertEquals(created, vspUploadStatusDto.getCreated());
+        assertFalse(vspUploadStatusDto.isComplete());
+    }
+
+    @Test
+    void startProcessingSuccessTest() throws ParseException {
+        //given
+        final String vspId = "vspId";
+        final String vspVersionId = "vspVersionId";
+        final UUID lockId = UUID.randomUUID();
+        final String username = "username";
+        final Date created = new SimpleDateFormat("dd/MM/yyyy").parse("01/01/1900");
+        final Date updated = new SimpleDateFormat("dd/MM/yyyy").parse("02/01/1900");
+        final VspUploadStatusRecord vspUploadStatusRecord = new VspUploadStatusRecord();
+        vspUploadStatusRecord.setVspId(vspId);
+        vspUploadStatusRecord.setVspVersionId(vspVersionId);
+        vspUploadStatusRecord.setLockId(lockId);
+        vspUploadStatusRecord.setCreated(created);
+        vspUploadStatusRecord.setUpdated(updated);
+        vspUploadStatusRecord.setStatus(VspUploadStatus.UPLOADING);
+        when(vspUploadStatusRecordDao.findLatest(vspId, vspVersionId)).thenReturn(Optional.of(vspUploadStatusRecord));
+        //when
+        final VspUploadStatusDto vspUploadStatusDto = packageUploadManagerImpl.putUploadInProcessing(vspId, vspVersionId, username);
+        //then
+        assertEquals(VspUploadStatus.PROCESSING, vspUploadStatusDto.getStatus());
+        assertNotEquals(updated, vspUploadStatusDto.getUpdated());
+        assertEquals(vspId, vspUploadStatusDto.getVspId());
+        assertEquals(vspVersionId, vspUploadStatusDto.getVspVersionId());
+        assertEquals(lockId, vspUploadStatusDto.getLockId());
+        assertEquals(created, vspUploadStatusDto.getCreated());
+        assertFalse(vspUploadStatusDto.isComplete());
+    }
+
+
+    @Test
+    void startProcessing_statusNotFoundTest() {
+        //given
+        final String vspId = "vspId";
+        final String vspVersionId = "vspVersionId";
+        when(vspUploadStatusRecordDao.findLatest(vspId, vspVersionId)).thenReturn(Optional.empty());
+        //when/then
+        final CoreException actualCoreException = assertThrows(CoreException.class,
+            () -> packageUploadManagerImpl.putUploadInProcessing(vspId, vspVersionId, "username"));
+
+        final CoreException expectedCoreException = couldNotFindStatus(vspId, vspVersionId).get();
+        assertEquals(expectedCoreException.code().id(), actualCoreException.code().id());
+        assertEquals(expectedCoreException.getMessage(), actualCoreException.getMessage());
+    }
+
+    @Test
+    void startProcessing_alreadyInGivenStatusTest() {
+        //given
+        final String vspId = "vspId";
+        final String vspVersionId = "vspVersionId";
+        final VspUploadStatus processingStatus = VspUploadStatus.PROCESSING;
+        final VspUploadStatusRecord vspUploadStatusRecord = new VspUploadStatusRecord();
+        vspUploadStatusRecord.setStatus(processingStatus);
+        when(vspUploadStatusRecordDao.findLatest(vspId, vspVersionId)).thenReturn(Optional.of(vspUploadStatusRecord));
+
+        //when/then
+        final CoreException actualCoreException = assertThrows(CoreException.class,
+            () -> packageUploadManagerImpl.putUploadInProcessing(vspId, vspVersionId, "username"));
+
+        final CoreException expectedCoreException = alreadyInStatusBeingUpdated(vspId, vspVersionId, processingStatus).get();
+        assertEquals(expectedCoreException.code().id(), actualCoreException.code().id());
+        assertEquals(expectedCoreException.getMessage(), actualCoreException.getMessage());
+    }
+
+    @Test
+    void updateStatus_couldNotUpdateTest() {
+        //given
+        final String vspId = "vspId";
+        final String vspVersionId = "vspVersionId";
+        final VspUploadStatusRecord vspUploadStatusRecord = new VspUploadStatusRecord();
+        vspUploadStatusRecord.setVspId(vspId);
+        vspUploadStatusRecord.setVspVersionId(vspVersionId);
+        vspUploadStatusRecord.setStatus(VspUploadStatus.UPLOADING);
+        when(vspUploadStatusRecordDao.findLatest(vspId, vspVersionId)).thenReturn(Optional.of(vspUploadStatusRecord));
+        final RuntimeException exception = new RuntimeException("test");
+        doThrow(exception).when(vspUploadStatusRecordDao).update(vspUploadStatusRecord);
+
+        //when/then
+        final CoreException actualCoreException = assertThrows(CoreException.class,
+            () -> packageUploadManagerImpl.putUploadInProcessing(vspId, vspVersionId, "username"));
+
+        final CoreException expectedCoreException = couldNotUpdateStatus(vspId, vspVersionId, VspUploadStatus.PROCESSING, exception).get();
+        assertEquals(expectedCoreException.code().id(), actualCoreException.code().id());
+        assertEquals(expectedCoreException.getMessage(), actualCoreException.getMessage());
+    }
+
 }
\ No newline at end of file
index 353dd2d..21b2cae 100644 (file)
@@ -79,9 +79,12 @@ public class VendorSoftwareProductErrorCodes {
     public static final String VSP_ONBOARD_METHOD_UPDATE_NOT_ALLOWED = "VSP_ONBOARD_METHOD_UPDATE_NOT_ALLOWED";
     public static final String VSP_PROCESSING_IN_PROGRESS = "VSP_PROCESSING_IN_PROGRESS";
     public static final String VSP_CREATE_UPLOAD_LOCK_ERROR = "VSP_CREATE_UPLOAD_LOCK_ERROR";
+    public static final String VSP_UNABLE_UPDATE_UPLOAD_STATUS_ERROR = "VSP_UNABLE_UPDATE_UPLOAD_STATUS_ERROR";
     public static final String VSP_UPDATE_UPLOAD_LOCK_ERROR = "VSP_UPDATE_UPLOAD_LOCK_ERROR";
     public static final String VSP_UPLOAD_LOCK_NOT_FOUND_ERROR = "VSP_UPLOAD_LOCK_NOT_FOUND_ERROR";
+    public static final String VSP_UPLOAD_STATUS_NOT_FOUND_ERROR = "VSP_UPLOAD_STATUS_NOT_FOUND_ERROR";
     public static final String VSP_UPLOAD_ALREADY_FINISHED_ERROR = "VSP_UPLOAD_ALREADY_FINISHED_ERROR";
+    public static final String VSP_UPLOAD_ALREADY_IN_STATUS_ERROR = "VSP_UPLOAD_ALREADY_IN_STATUS_ERROR";
 
     private VendorSoftwareProductErrorCodes() {
     }