Obtain upload lock before uploading
[sdc.git] / 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
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.openecomp.sdcrests.vsp.rest.services;
22
23 import static org.junit.jupiter.api.Assertions.assertEquals;
24 import static org.junit.jupiter.api.Assertions.assertFalse;
25 import static org.junit.jupiter.api.Assertions.assertNotEquals;
26 import static org.junit.jupiter.api.Assertions.assertNotNull;
27 import static org.junit.jupiter.api.Assertions.assertNull;
28 import static org.junit.jupiter.api.Assertions.assertThrows;
29 import static org.junit.jupiter.api.Assertions.assertTrue;
30 import static org.junit.jupiter.api.Assertions.fail;
31 import static org.mockito.ArgumentMatchers.any;
32 import static org.mockito.ArgumentMatchers.eq;
33 import static org.mockito.Mockito.verify;
34 import static org.mockito.Mockito.when;
35 import static org.openecomp.sdcrests.vsp.rest.exception.OrchestrationTemplateCandidateUploadManagerExceptionSupplier.vspUploadAlreadyInProgress;
36
37 import java.io.IOException;
38 import java.io.InputStream;
39 import java.io.OutputStream;
40 import java.net.URL;
41 import java.nio.file.Files;
42 import java.nio.file.Path;
43 import java.util.Arrays;
44 import java.util.Objects;
45 import java.util.Optional;
46 import java.util.UUID;
47 import javax.activation.DataHandler;
48 import javax.ws.rs.core.Response;
49 import javax.ws.rs.core.Response.Status;
50 import org.apache.commons.io.IOUtils;
51 import org.apache.commons.lang3.tuple.Pair;
52 import org.apache.cxf.jaxrs.ext.multipart.Attachment;
53 import org.apache.cxf.jaxrs.ext.multipart.ContentDisposition;
54 import org.jetbrains.annotations.NotNull;
55 import org.junit.jupiter.api.BeforeEach;
56 import org.junit.jupiter.api.Test;
57 import org.mockito.ArgumentMatchers;
58 import org.mockito.InjectMocks;
59 import org.mockito.Mock;
60 import org.mockito.Mockito;
61 import org.mockito.MockitoAnnotations;
62 import org.openecomp.core.utilities.orchestration.OnboardingTypesEnum;
63 import org.openecomp.sdc.activitylog.ActivityLogManager;
64 import org.openecomp.sdc.be.csar.storage.ArtifactStorageManager;
65 import org.openecomp.sdc.be.csar.storage.MinIoArtifactInfo;
66 import org.openecomp.sdc.be.csar.storage.MinIoStorageArtifactStorageConfig;
67 import org.openecomp.sdc.be.csar.storage.MinIoStorageArtifactStorageConfig.Credentials;
68 import org.openecomp.sdc.be.csar.storage.MinIoStorageArtifactStorageConfig.EndPoint;
69 import org.openecomp.sdc.be.csar.storage.PackageSizeReducer;
70 import org.openecomp.sdc.be.csar.storage.StorageFactory;
71 import org.openecomp.sdc.common.errors.CoreException;
72 import org.openecomp.sdc.logging.api.Logger;
73 import org.openecomp.sdc.logging.api.LoggerFactory;
74 import org.openecomp.sdc.vendorsoftwareproduct.OrchestrationTemplateCandidateManager;
75 import org.openecomp.sdc.vendorsoftwareproduct.VendorSoftwareProductManager;
76 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.VspUploadStatus;
77 import org.openecomp.sdc.vendorsoftwareproduct.types.OrchestrationTemplateActionResponse;
78 import org.openecomp.sdc.vendorsoftwareproduct.types.UploadFileResponse;
79 import org.openecomp.sdc.vendorsoftwareproduct.types.UploadFileStatus;
80 import org.openecomp.sdc.vendorsoftwareproduct.types.ValidationResponse;
81 import org.openecomp.sdc.vendorsoftwareproduct.types.candidateheat.FilesDataStructure;
82 import org.openecomp.sdc.vendorsoftwareproduct.types.candidateheat.Module;
83 import org.openecomp.sdcrests.vendorsoftwareproducts.types.FileDataStructureDto;
84 import org.openecomp.sdcrests.vendorsoftwareproducts.types.OrchestrationTemplateActionResponseDto;
85 import org.openecomp.sdcrests.vendorsoftwareproducts.types.UploadFileResponseDto;
86 import org.openecomp.sdcrests.vendorsoftwareproducts.types.VspUploadStatusDto;
87
88 class OrchestrationTemplateCandidateImplTest {
89
90     private final Logger logger = LoggerFactory.getLogger(OrchestrationTemplateCandidateImplTest.class);
91     private final String candidateId = UUID.randomUUID().toString();
92     private final String softwareProductId = UUID.randomUUID().toString();
93     private final String versionId = UUID.randomUUID().toString();
94     private final String user = "cs0008";
95     @Mock
96     private OrchestrationTemplateCandidateManager candidateManager;
97     @Mock
98     private VendorSoftwareProductManager vendorSoftwareProductManager;
99     @Mock
100     private ActivityLogManager activityLogManager;
101     @Mock
102     private ArtifactStorageManager artifactStorageManager;
103     @Mock
104     private PackageSizeReducer packageSizeReducer;
105     @Mock
106     private OrchestrationTemplateCandidateUploadManager orchestrationTemplateCandidateUploadManager;
107     @Mock
108     private StorageFactory storageFactory;
109     @Mock
110     private Attachment fileToUpload;
111     @InjectMocks
112     private OrchestrationTemplateCandidateImpl orchestrationTemplateCandidate;
113
114     @BeforeEach
115     public void setUp() {
116         try {
117             MockitoAnnotations.openMocks(this);
118             UploadFileResponse uploadFileResponse = new UploadFileResponse();
119             uploadFileResponse.setOnboardingType(OnboardingTypesEnum.ZIP);
120             uploadFileResponse.setNetworkPackageName("test");
121             when(candidateManager.upload(any(), any())).thenReturn(uploadFileResponse);
122
123             // get using the candidate manager.
124             Optional<Pair<String, byte[]>> zipFile = Optional.of(Pair.of("Hello", "World".getBytes()));
125
126             when(candidateManager.get(
127                 ArgumentMatchers.eq(candidateId),
128                 ArgumentMatchers.any())).thenReturn(zipFile);
129
130             when(vendorSoftwareProductManager.get(
131                 ArgumentMatchers.eq(softwareProductId),
132                 ArgumentMatchers.any())).thenReturn(zipFile);
133
134             OrchestrationTemplateActionResponse processResponse = new OrchestrationTemplateActionResponse();
135             processResponse.setStatus(UploadFileStatus.Success);
136             when(candidateManager.process(
137                 ArgumentMatchers.eq(candidateId),
138                 ArgumentMatchers.any())).thenReturn(processResponse);
139
140             ValidationResponse vr = new ValidationResponse();
141             when(candidateManager.updateFilesDataStructure(
142                 ArgumentMatchers.eq(candidateId),
143                 ArgumentMatchers.any(),
144                 ArgumentMatchers.any())).thenReturn(vr);
145
146             FilesDataStructure fds = new FilesDataStructure();
147             fds.setArtifacts(Arrays.asList("a", "b"));
148             fds.setNested(Arrays.asList("foo", "bar"));
149             fds.setUnassigned(Arrays.asList("c", "d"));
150             fds.setModules(Arrays.asList(new Module(), new Module()));
151
152             when(candidateManager.getFilesDataStructure(
153                 ArgumentMatchers.eq(candidateId),
154                 ArgumentMatchers.any())).thenReturn(Optional.of(fds));
155
156         } catch (Exception e) {
157             logger.error(e.getMessage(), e);
158         }
159     }
160
161     @Test
162     void uploadSignedTest() throws IOException {
163         final String vspId = "vspId";
164         final String versionId = "versionId";
165         when(orchestrationTemplateCandidateUploadManager.findLatestStatus(vspId, versionId, user)).thenReturn(Optional.empty());
166         final UUID lockId = UUID.randomUUID();
167         when(orchestrationTemplateCandidateUploadManager.putUploadInProgress(vspId, versionId, user))
168             .thenReturn(createVspUploadStatus(lockId, VspUploadStatus.UPLOADING));
169         when(orchestrationTemplateCandidateUploadManager.putUploadInValidation(vspId, versionId, user))
170             .thenReturn(createVspUploadStatus(lockId, VspUploadStatus.VALIDATING));
171         when(orchestrationTemplateCandidateUploadManager.putUploadInProcessing(vspId, versionId, user))
172             .thenReturn(createVspUploadStatus(lockId, VspUploadStatus.PROCESSING));
173         Response response = orchestrationTemplateCandidate
174             .upload(vspId, versionId, mockAttachment("filename.zip", this.getClass().getResource("/files/sample-signed.zip")), user);
175         assertEquals(Status.OK.getStatusCode(), response.getStatus());
176         assertTrue(((UploadFileResponseDto) response.getEntity()).getErrors().isEmpty());
177     }
178
179     @Test
180     void uploadNotSignedTest() throws IOException {
181         final String vspId = "vspId";
182         final String versionId = "versionId";
183         when(orchestrationTemplateCandidateUploadManager.findLatestStatus(vspId, versionId, user)).thenReturn(Optional.empty());
184         final UUID lockId = UUID.randomUUID();
185         when(orchestrationTemplateCandidateUploadManager.putUploadInProgress(vspId, versionId, user))
186             .thenReturn(createVspUploadStatus(lockId, VspUploadStatus.UPLOADING));
187         when(orchestrationTemplateCandidateUploadManager.putUploadInValidation(vspId, versionId, user))
188             .thenReturn(createVspUploadStatus(lockId, VspUploadStatus.VALIDATING));
189         when(orchestrationTemplateCandidateUploadManager.putUploadInProcessing(vspId, versionId, user))
190             .thenReturn(createVspUploadStatus(lockId, VspUploadStatus.PROCESSING));
191         Response response = orchestrationTemplateCandidate.upload(vspId, versionId,
192             mockAttachment("filename.csar", this.getClass().getResource("/files/sample-not-signed.csar")), user);
193         assertEquals(Status.OK.getStatusCode(), response.getStatus());
194         assertTrue(((UploadFileResponseDto) response.getEntity()).getErrors().isEmpty());
195     }
196
197     @NotNull
198     private VspUploadStatusDto createVspUploadStatus(final UUID lockId, final VspUploadStatus uploadStatus) {
199         final VspUploadStatusDto vspUploadStatusProcessing = new VspUploadStatusDto();
200         vspUploadStatusProcessing.setLockId(lockId);
201         vspUploadStatusProcessing.setStatus(uploadStatus);
202         return vspUploadStatusProcessing;
203     }
204
205     @Test
206     void uploadNotSignedArtifactStorageManagerIsEnabledTest() throws IOException {
207         when(storageFactory.createArtifactStorageManager()).thenReturn(artifactStorageManager);
208         when(artifactStorageManager.isEnabled()).thenReturn(true);
209         when(artifactStorageManager.getStorageConfiguration()).thenReturn(new MinIoStorageArtifactStorageConfig
210             (true, new EndPoint("host", 9000, false), new Credentials("accessKey", "secretKey"), "tempPath", 10_000_000));
211
212         final Path path = Path.of("src/test/resources/files/sample-not-signed.csar");
213         final String vspId = "vspId";
214         final String versionId = "versionId";
215         when(artifactStorageManager.upload(eq(vspId), eq(versionId), any())).thenReturn(new MinIoArtifactInfo("vspId", "name"));
216         final byte[] bytes = Files.readAllBytes(path);
217         when(packageSizeReducer.reduce(any())).thenReturn(bytes);
218
219         final VspUploadStatusDto vspUploadStatusDto = new VspUploadStatusDto();
220         vspUploadStatusDto.setStatus(VspUploadStatus.UPLOADING);
221         when(orchestrationTemplateCandidateUploadManager.findLatestStatus(vspId, versionId, user)).thenReturn(Optional.of(vspUploadStatusDto));
222         when(orchestrationTemplateCandidateUploadManager.putUploadInValidation(vspId, versionId, user)).thenReturn(new VspUploadStatusDto());
223         when(orchestrationTemplateCandidateUploadManager.putUploadInProcessing(vspId, versionId, user)).thenReturn(new VspUploadStatusDto());
224
225         Response response = orchestrationTemplateCandidate.upload(vspId, versionId,
226             mockAttachment("filename.csar", this.getClass().getResource("/files/sample-not-signed.csar")), user);
227         assertEquals(Status.OK.getStatusCode(), response.getStatus());
228         assertTrue(((UploadFileResponseDto) response.getEntity()).getErrors().isEmpty());
229     }
230
231     private Attachment mockAttachment(final String fileName, final URL fileToUpload) throws IOException {
232         final Attachment attachment = Mockito.mock(Attachment.class);
233         final InputStream inputStream = Mockito.mock(InputStream.class);
234         when(attachment.getContentDisposition()).thenReturn(new ContentDisposition("test"));
235         final DataHandler dataHandler = Mockito.mock(DataHandler.class);
236         when(dataHandler.getName()).thenReturn(fileName);
237         when(attachment.getDataHandler()).thenReturn(dataHandler);
238         when(dataHandler.getInputStream()).thenReturn(inputStream);
239         when(inputStream.transferTo(any(OutputStream.class))).thenReturn(0L);
240         byte[] bytes = "upload package Test".getBytes();
241         if (Objects.nonNull(fileToUpload)) {
242             try {
243                 bytes = IOUtils.toByteArray(fileToUpload);
244             } catch (final IOException e) {
245                 logger.error("unexpected exception", e);
246                 fail("Not able to convert file to byte array");
247             }
248         }
249         when(attachment.getObject(ArgumentMatchers.any())).thenReturn(bytes);
250         return attachment;
251     }
252
253     @Test
254     void uploadSignNotValidTest() throws IOException {
255         //given
256         final VspUploadStatusDto vspUploadStatusDto = new VspUploadStatusDto();
257         vspUploadStatusDto.setStatus(VspUploadStatus.UPLOADING);
258         when(orchestrationTemplateCandidateUploadManager.findLatestStatus(candidateId, versionId, user))
259             .thenReturn(Optional.of(vspUploadStatusDto));
260         when(orchestrationTemplateCandidateUploadManager.putUploadInValidation(candidateId, versionId, user)).thenReturn(new VspUploadStatusDto());
261         //when
262         Response response = orchestrationTemplateCandidate
263             .upload(candidateId, versionId, mockAttachment("filename.zip", null), user);
264         //then
265         assertEquals(Status.NOT_ACCEPTABLE.getStatusCode(), response.getStatus());
266         assertFalse(((UploadFileResponseDto) response.getEntity()).getErrors().isEmpty());
267     }
268
269     @Test
270     void testCandidateGet() throws IOException {
271         Response rsp = orchestrationTemplateCandidate.get(candidateId, versionId, user);
272         assertEquals(Response.Status.OK.getStatusCode(), rsp.getStatus(), "Response status equals");
273         assertNotEquals(rsp.getHeaderString("Content-Disposition").indexOf("Candidate"), -1);
274         byte[] content = (byte[]) rsp.getEntity();
275         assertEquals("World", new String(content));
276     }
277
278     @Test
279     void testVendorSoftwareProductGet() throws IOException {
280         Response rsp = orchestrationTemplateCandidate.get(softwareProductId, versionId, user);
281         assertEquals(Response.Status.OK.getStatusCode(), rsp.getStatus(), "Response status equals");
282         assertNotEquals(rsp.getHeaderString("Content-Disposition").indexOf("Processed"), -1);
283         byte[] content = (byte[]) rsp.getEntity();
284         assertEquals("World", new String(content));
285     }
286
287     @Test
288     void testMissingGet() throws IOException {
289         Response rsp = orchestrationTemplateCandidate.get(UUID.randomUUID().toString(), versionId, user);
290         assertEquals(Response.Status.NOT_FOUND.getStatusCode(), rsp.getStatus(), "Response status equals");
291     }
292
293     @Test
294     void testAbort() {
295         try {
296             Response rsp = orchestrationTemplateCandidate.abort(candidateId, versionId);
297             assertEquals(Response.Status.OK.getStatusCode(), rsp.getStatus(), "Response status equals");
298             assertNull(rsp.getEntity());
299         } catch (Exception ex) {
300             logger.error("unexpected exception", ex);
301             fail("abort should not throw an exception");
302         }
303     }
304
305     @Test
306     void testProcess() {
307         try {
308             Response rsp = orchestrationTemplateCandidate.process(candidateId, versionId, user);
309             assertEquals(Response.Status.OK.getStatusCode(), rsp.getStatus(), "Response status equals");
310             assertNotNull(rsp.getEntity());
311             OrchestrationTemplateActionResponseDto dto = (OrchestrationTemplateActionResponseDto) rsp.getEntity();
312             assertEquals(UploadFileStatus.Success, dto.getStatus(), "status check");
313         } catch (Exception ex) {
314             logger.error("unexpected exception", ex);
315             fail("abort should not throw an exception");
316         }
317     }
318
319     @Test
320     void testFilesDataStructureUpload() {
321         try {
322             FileDataStructureDto dto = new FileDataStructureDto();
323             dto.setArtifacts(Arrays.asList("a", "b", "c"));
324             Response rsp = orchestrationTemplateCandidate.updateFilesDataStructure(candidateId, versionId, dto, user);
325             assertEquals(Response.Status.OK.getStatusCode(), rsp.getStatus(), "Response status equals");
326         } catch (Exception ex) {
327             logger.error("unexpected exception", ex);
328             fail("abort should not throw an exception");
329         }
330     }
331
332     @Test
333     void testFilesDataStructureGet() {
334         try {
335             FileDataStructureDto dto = new FileDataStructureDto();
336             dto.setArtifacts(Arrays.asList("a", "b", "c"));
337             Response rsp = orchestrationTemplateCandidate.getFilesDataStructure(candidateId, versionId, user);
338             assertEquals(Response.Status.OK.getStatusCode(), rsp.getStatus(), "Response status equals");
339         } catch (Exception ex) {
340             logger.error("unexpected exception", ex);
341             fail("abort should not throw an exception");
342         }
343     }
344
345     @Test
346     void finishUploadMustBeCalledWhenExceptionHappensTest() {
347         //given
348         final VspUploadStatusDto vspUploadStatusDto = createVspUploadStatus(UUID.randomUUID(), VspUploadStatus.UPLOADING);
349         when(orchestrationTemplateCandidateUploadManager.findLatestStatus(candidateId, versionId, user)).thenReturn(Optional.empty());
350         when(orchestrationTemplateCandidateUploadManager.putUploadInProgress(candidateId, versionId, user)).thenReturn(vspUploadStatusDto);
351         final RuntimeException forcedException = new RuntimeException();
352         when(fileToUpload.getDataHandler()).thenThrow(forcedException);
353         //when
354         final RuntimeException actualException = assertThrows(RuntimeException.class,
355             () -> orchestrationTemplateCandidate.upload(candidateId, versionId, fileToUpload, user));
356         //then
357         assertEquals(forcedException, actualException);
358         verify(orchestrationTemplateCandidateUploadManager)
359             .putUploadAsFinished(candidateId, versionId, vspUploadStatusDto.getLockId(), VspUploadStatus.ERROR, user);
360     }
361
362 //    @Test
363 //    void uploadTestWithLatestStatusComplete() {
364 //        final VspUploadStatusDto vspUploadStatusDto = new VspUploadStatusDto();
365 //        vspUploadStatusDto.setComplete(true);
366 //        //given
367 //        when(orchestrationTemplateCandidateUploadManager.findLatestStatus(candidateId, versionId, user)).thenReturn(Optional.of(vspUploadStatusDto));
368 //        final Attachment mock = Mockito.mock(Attachment.class);
369 //        when(mock.getDataHandler()).thenReturn(Mockito.mock(DataHandler.class));
370 //        //when
371 //        final CoreException actualException = assertThrows(CoreException.class,
372 //            () -> orchestrationTemplateCandidate.upload(candidateId, versionId, mock, user));
373 //        final CoreException expectedException = couldNotAcceptPackageNoUploadInProgress(candidateId, versionId).get();
374 //        //then
375 //        assertEquals(expectedException.code().id(), actualException.code().id());
376 //        assertEquals(expectedException.code().message(), actualException.code().message());
377 //        verify(orchestrationTemplateCandidateUploadManager).findLatestStatus(candidateId, versionId, user);
378 //    }
379
380     @Test
381     void uploadTestWithUploadInProgress() {
382         final VspUploadStatusDto vspUploadStatusDto = new VspUploadStatusDto();
383         vspUploadStatusDto.setComplete(false);
384         vspUploadStatusDto.setStatus(VspUploadStatus.PROCESSING);
385         //given
386         when(orchestrationTemplateCandidateUploadManager.findLatestStatus(candidateId, versionId, user)).thenReturn(Optional.of(vspUploadStatusDto));
387         final Attachment mock = Mockito.mock(Attachment.class);
388         when(mock.getDataHandler()).thenReturn(Mockito.mock(DataHandler.class));
389         //when
390         final CoreException actualException = assertThrows(CoreException.class,
391             () -> orchestrationTemplateCandidate.upload(candidateId, versionId, mock, user));
392         final CoreException expectedException = vspUploadAlreadyInProgress(candidateId, versionId).get();
393         //then
394         assertEquals(expectedException.code().id(), actualException.code().id());
395         assertEquals(expectedException.code().message(), actualException.code().message());
396         verify(orchestrationTemplateCandidateUploadManager).findLatestStatus(candidateId, versionId, user);
397     }
398
399 }