diff options
author | andre.schmid <andre.schmid@est.tech> | 2022-02-23 21:09:56 +0000 |
---|---|---|
committer | andre.schmid <andre.schmid@est.tech> | 2022-03-03 10:41:23 +0000 |
commit | 3dcbae860f1a4bc8e6596cddc9cb19611d0c3dc7 (patch) | |
tree | 01a34ad9bdc56a437632752ba3d1aedcaf5d0db2 /openecomp-be/api/openecomp-sdc-rest-webapp/vendor-software-products-rest/vendor-software-products-rest-services/src/test/java/org/openecomp | |
parent | c4a8271d664deb39e69fbba329b11ff57b9b276b (diff) |
Obtain upload lock before uploading
Before uploading, the system will now set the status as uploading in
order to have more control of the upload process and status.
Without that the UI status updates could show up incorrectly.
Also, this behaviour removes the need to upload a file to set the
upload in progress, which avoids a concurrent upload try to upload
a file if there is another upload in progress.
Change-Id: Ic008560aa57e1ee7a50389ad26f1a8890f1cf198
Issue-ID: SDC-3888
Signed-off-by: andre.schmid <andre.schmid@est.tech>
Diffstat (limited to 'openecomp-be/api/openecomp-sdc-rest-webapp/vendor-software-products-rest/vendor-software-products-rest-services/src/test/java/org/openecomp')
1 files changed, 74 insertions, 9 deletions
diff --git a/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 b/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 index 802d6d885a..8b31261d6a 100644 --- a/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 +++ b/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 @@ -32,6 +32,7 @@ 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 static org.openecomp.sdcrests.vsp.rest.exception.OrchestrationTemplateCandidateUploadManagerExceptionSupplier.vspUploadAlreadyInProgress; import java.io.IOException; import java.io.InputStream; @@ -50,6 +51,7 @@ import org.apache.commons.io.IOUtils; import org.apache.commons.lang3.tuple.Pair; import org.apache.cxf.jaxrs.ext.multipart.Attachment; import org.apache.cxf.jaxrs.ext.multipart.ContentDisposition; +import org.jetbrains.annotations.NotNull; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.mockito.ArgumentMatchers; @@ -66,6 +68,7 @@ import org.openecomp.sdc.be.csar.storage.MinIoStorageArtifactStorageConfig.Crede import org.openecomp.sdc.be.csar.storage.MinIoStorageArtifactStorageConfig.EndPoint; import org.openecomp.sdc.be.csar.storage.PackageSizeReducer; import org.openecomp.sdc.be.csar.storage.StorageFactory; +import org.openecomp.sdc.common.errors.CoreException; import org.openecomp.sdc.logging.api.Logger; import org.openecomp.sdc.logging.api.LoggerFactory; import org.openecomp.sdc.vendorsoftwareproduct.OrchestrationTemplateCandidateManager; @@ -159,9 +162,14 @@ class OrchestrationTemplateCandidateImplTest { void uploadSignedTest() throws IOException { 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()); + when(orchestrationTemplateCandidateUploadManager.findLatestStatus(vspId, versionId, user)).thenReturn(Optional.empty()); + final UUID lockId = UUID.randomUUID(); + when(orchestrationTemplateCandidateUploadManager.putUploadInProgress(vspId, versionId, user)) + .thenReturn(createVspUploadStatus(lockId, VspUploadStatus.UPLOADING)); + when(orchestrationTemplateCandidateUploadManager.putUploadInValidation(vspId, versionId, user)) + .thenReturn(createVspUploadStatus(lockId, VspUploadStatus.VALIDATING)); + when(orchestrationTemplateCandidateUploadManager.putUploadInProcessing(vspId, versionId, user)) + .thenReturn(createVspUploadStatus(lockId, VspUploadStatus.PROCESSING)); Response response = orchestrationTemplateCandidate .upload(vspId, versionId, mockAttachment("filename.zip", this.getClass().getResource("/files/sample-signed.zip")), user); assertEquals(Status.OK.getStatusCode(), response.getStatus()); @@ -172,15 +180,28 @@ class OrchestrationTemplateCandidateImplTest { void uploadNotSignedTest() throws IOException { 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()); + when(orchestrationTemplateCandidateUploadManager.findLatestStatus(vspId, versionId, user)).thenReturn(Optional.empty()); + final UUID lockId = UUID.randomUUID(); + when(orchestrationTemplateCandidateUploadManager.putUploadInProgress(vspId, versionId, user)) + .thenReturn(createVspUploadStatus(lockId, VspUploadStatus.UPLOADING)); + when(orchestrationTemplateCandidateUploadManager.putUploadInValidation(vspId, versionId, user)) + .thenReturn(createVspUploadStatus(lockId, VspUploadStatus.VALIDATING)); + when(orchestrationTemplateCandidateUploadManager.putUploadInProcessing(vspId, versionId, user)) + .thenReturn(createVspUploadStatus(lockId, VspUploadStatus.PROCESSING)); Response response = orchestrationTemplateCandidate.upload(vspId, versionId, mockAttachment("filename.csar", this.getClass().getResource("/files/sample-not-signed.csar")), user); assertEquals(Status.OK.getStatusCode(), response.getStatus()); assertTrue(((UploadFileResponseDto) response.getEntity()).getErrors().isEmpty()); } + @NotNull + private VspUploadStatusDto createVspUploadStatus(final UUID lockId, final VspUploadStatus uploadStatus) { + final VspUploadStatusDto vspUploadStatusProcessing = new VspUploadStatusDto(); + vspUploadStatusProcessing.setLockId(lockId); + vspUploadStatusProcessing.setStatus(uploadStatus); + return vspUploadStatusProcessing; + } + @Test void uploadNotSignedArtifactStorageManagerIsEnabledTest() throws IOException { when(storageFactory.createArtifactStorageManager()).thenReturn(artifactStorageManager); @@ -195,7 +216,9 @@ class OrchestrationTemplateCandidateImplTest { final byte[] bytes = Files.readAllBytes(path); when(packageSizeReducer.reduce(any())).thenReturn(bytes); - when(orchestrationTemplateCandidateUploadManager.putUploadInProgress(vspId, versionId, user)).thenReturn(new VspUploadStatusDto()); + final VspUploadStatusDto vspUploadStatusDto = new VspUploadStatusDto(); + vspUploadStatusDto.setStatus(VspUploadStatus.UPLOADING); + when(orchestrationTemplateCandidateUploadManager.findLatestStatus(vspId, versionId, user)).thenReturn(Optional.of(vspUploadStatusDto)); when(orchestrationTemplateCandidateUploadManager.putUploadInValidation(vspId, versionId, user)).thenReturn(new VspUploadStatusDto()); when(orchestrationTemplateCandidateUploadManager.putUploadInProcessing(vspId, versionId, user)).thenReturn(new VspUploadStatusDto()); @@ -230,6 +253,10 @@ class OrchestrationTemplateCandidateImplTest { @Test void uploadSignNotValidTest() throws IOException { //given + final VspUploadStatusDto vspUploadStatusDto = new VspUploadStatusDto(); + vspUploadStatusDto.setStatus(VspUploadStatus.UPLOADING); + when(orchestrationTemplateCandidateUploadManager.findLatestStatus(candidateId, versionId, user)) + .thenReturn(Optional.of(vspUploadStatusDto)); when(orchestrationTemplateCandidateUploadManager.putUploadInValidation(candidateId, versionId, user)).thenReturn(new VspUploadStatusDto()); //when Response response = orchestrationTemplateCandidate @@ -318,8 +345,8 @@ class OrchestrationTemplateCandidateImplTest { @Test void finishUploadMustBeCalledWhenExceptionHappensTest() { //given - final VspUploadStatusDto vspUploadStatusDto = new VspUploadStatusDto(); - vspUploadStatusDto.setLockId(UUID.randomUUID()); + final VspUploadStatusDto vspUploadStatusDto = createVspUploadStatus(UUID.randomUUID(), VspUploadStatus.UPLOADING); + when(orchestrationTemplateCandidateUploadManager.findLatestStatus(candidateId, versionId, user)).thenReturn(Optional.empty()); when(orchestrationTemplateCandidateUploadManager.putUploadInProgress(candidateId, versionId, user)).thenReturn(vspUploadStatusDto); final RuntimeException forcedException = new RuntimeException(); when(fileToUpload.getDataHandler()).thenThrow(forcedException); @@ -331,4 +358,42 @@ class OrchestrationTemplateCandidateImplTest { verify(orchestrationTemplateCandidateUploadManager) .putUploadAsFinished(candidateId, versionId, vspUploadStatusDto.getLockId(), VspUploadStatus.ERROR, user); } + +// @Test +// void uploadTestWithLatestStatusComplete() { +// final VspUploadStatusDto vspUploadStatusDto = new VspUploadStatusDto(); +// vspUploadStatusDto.setComplete(true); +// //given +// when(orchestrationTemplateCandidateUploadManager.findLatestStatus(candidateId, versionId, user)).thenReturn(Optional.of(vspUploadStatusDto)); +// final Attachment mock = Mockito.mock(Attachment.class); +// when(mock.getDataHandler()).thenReturn(Mockito.mock(DataHandler.class)); +// //when +// final CoreException actualException = assertThrows(CoreException.class, +// () -> orchestrationTemplateCandidate.upload(candidateId, versionId, mock, user)); +// final CoreException expectedException = couldNotAcceptPackageNoUploadInProgress(candidateId, versionId).get(); +// //then +// assertEquals(expectedException.code().id(), actualException.code().id()); +// assertEquals(expectedException.code().message(), actualException.code().message()); +// verify(orchestrationTemplateCandidateUploadManager).findLatestStatus(candidateId, versionId, user); +// } + + @Test + void uploadTestWithUploadInProgress() { + final VspUploadStatusDto vspUploadStatusDto = new VspUploadStatusDto(); + vspUploadStatusDto.setComplete(false); + vspUploadStatusDto.setStatus(VspUploadStatus.PROCESSING); + //given + when(orchestrationTemplateCandidateUploadManager.findLatestStatus(candidateId, versionId, user)).thenReturn(Optional.of(vspUploadStatusDto)); + final Attachment mock = Mockito.mock(Attachment.class); + when(mock.getDataHandler()).thenReturn(Mockito.mock(DataHandler.class)); + //when + final CoreException actualException = assertThrows(CoreException.class, + () -> orchestrationTemplateCandidate.upload(candidateId, versionId, mock, user)); + final CoreException expectedException = vspUploadAlreadyInProgress(candidateId, versionId).get(); + //then + assertEquals(expectedException.code().id(), actualException.code().id()); + assertEquals(expectedException.code().message(), actualException.code().message()); + verify(orchestrationTemplateCandidateUploadManager).findLatestStatus(candidateId, versionId, user); + } + } |