Implement improved MinIo client
[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
36 import java.io.IOException;
37 import java.io.InputStream;
38 import java.io.OutputStream;
39 import java.net.URL;
40 import java.nio.file.Files;
41 import java.nio.file.Path;
42 import java.util.Arrays;
43 import java.util.Objects;
44 import java.util.Optional;
45 import java.util.UUID;
46 import javax.activation.DataHandler;
47 import javax.ws.rs.core.Response;
48 import javax.ws.rs.core.Response.Status;
49 import org.apache.commons.io.IOUtils;
50 import org.apache.commons.lang3.tuple.Pair;
51 import org.apache.cxf.jaxrs.ext.multipart.Attachment;
52 import org.apache.cxf.jaxrs.ext.multipart.ContentDisposition;
53 import org.junit.jupiter.api.BeforeEach;
54 import org.junit.jupiter.api.Test;
55 import org.mockito.ArgumentMatchers;
56 import org.mockito.InjectMocks;
57 import org.mockito.Mock;
58 import org.mockito.Mockito;
59 import org.mockito.MockitoAnnotations;
60 import org.openecomp.core.utilities.orchestration.OnboardingTypesEnum;
61 import org.openecomp.sdc.activitylog.ActivityLogManager;
62 import org.openecomp.sdc.be.csar.storage.ArtifactStorageManager;
63 import org.openecomp.sdc.be.csar.storage.MinIoArtifactInfo;
64 import org.openecomp.sdc.be.csar.storage.MinIoStorageArtifactStorageConfig;
65 import org.openecomp.sdc.be.csar.storage.MinIoStorageArtifactStorageConfig.Credentials;
66 import org.openecomp.sdc.be.csar.storage.MinIoStorageArtifactStorageConfig.EndPoint;
67 import org.openecomp.sdc.be.csar.storage.PackageSizeReducer;
68 import org.openecomp.sdc.be.csar.storage.StorageFactory;
69 import org.openecomp.sdc.logging.api.Logger;
70 import org.openecomp.sdc.logging.api.LoggerFactory;
71 import org.openecomp.sdc.vendorsoftwareproduct.OrchestrationTemplateCandidateManager;
72 import org.openecomp.sdc.vendorsoftwareproduct.VendorSoftwareProductManager;
73 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.VspUploadStatus;
74 import org.openecomp.sdc.vendorsoftwareproduct.types.OrchestrationTemplateActionResponse;
75 import org.openecomp.sdc.vendorsoftwareproduct.types.UploadFileResponse;
76 import org.openecomp.sdc.vendorsoftwareproduct.types.UploadFileStatus;
77 import org.openecomp.sdc.vendorsoftwareproduct.types.ValidationResponse;
78 import org.openecomp.sdc.vendorsoftwareproduct.types.candidateheat.FilesDataStructure;
79 import org.openecomp.sdc.vendorsoftwareproduct.types.candidateheat.Module;
80 import org.openecomp.sdcrests.vendorsoftwareproducts.types.FileDataStructureDto;
81 import org.openecomp.sdcrests.vendorsoftwareproducts.types.OrchestrationTemplateActionResponseDto;
82 import org.openecomp.sdcrests.vendorsoftwareproducts.types.UploadFileResponseDto;
83 import org.openecomp.sdcrests.vendorsoftwareproducts.types.VspUploadStatusDto;
84
85 class OrchestrationTemplateCandidateImplTest {
86
87     private final Logger logger = LoggerFactory.getLogger(OrchestrationTemplateCandidateImplTest.class);
88     private final String candidateId = UUID.randomUUID().toString();
89     private final String softwareProductId = UUID.randomUUID().toString();
90     private final String versionId = UUID.randomUUID().toString();
91     private final String user = "cs0008";
92     @Mock
93     private OrchestrationTemplateCandidateManager candidateManager;
94     @Mock
95     private VendorSoftwareProductManager vendorSoftwareProductManager;
96     @Mock
97     private ActivityLogManager activityLogManager;
98     @Mock
99     private ArtifactStorageManager artifactStorageManager;
100     @Mock
101     private PackageSizeReducer packageSizeReducer;
102     @Mock
103     private OrchestrationTemplateCandidateUploadManager orchestrationTemplateCandidateUploadManager;
104     @Mock
105     private StorageFactory storageFactory;
106     @Mock
107     private Attachment fileToUpload;
108     @InjectMocks
109     private OrchestrationTemplateCandidateImpl orchestrationTemplateCandidate;
110
111     @BeforeEach
112     public void setUp() {
113         try {
114             MockitoAnnotations.openMocks(this);
115             UploadFileResponse uploadFileResponse = new UploadFileResponse();
116             uploadFileResponse.setOnboardingType(OnboardingTypesEnum.ZIP);
117             uploadFileResponse.setNetworkPackageName("test");
118             when(candidateManager.upload(any(), any())).thenReturn(uploadFileResponse);
119
120             // get using the candidate manager.
121             Optional<Pair<String, byte[]>> zipFile = Optional.of(Pair.of("Hello", "World".getBytes()));
122
123             when(candidateManager.get(
124                 ArgumentMatchers.eq(candidateId),
125                 ArgumentMatchers.any())).thenReturn(zipFile);
126
127             when(vendorSoftwareProductManager.get(
128                 ArgumentMatchers.eq(softwareProductId),
129                 ArgumentMatchers.any())).thenReturn(zipFile);
130
131             OrchestrationTemplateActionResponse processResponse = new OrchestrationTemplateActionResponse();
132             processResponse.setStatus(UploadFileStatus.Success);
133             when(candidateManager.process(
134                 ArgumentMatchers.eq(candidateId),
135                 ArgumentMatchers.any())).thenReturn(processResponse);
136
137             ValidationResponse vr = new ValidationResponse();
138             when(candidateManager.updateFilesDataStructure(
139                 ArgumentMatchers.eq(candidateId),
140                 ArgumentMatchers.any(),
141                 ArgumentMatchers.any())).thenReturn(vr);
142
143             FilesDataStructure fds = new FilesDataStructure();
144             fds.setArtifacts(Arrays.asList("a", "b"));
145             fds.setNested(Arrays.asList("foo", "bar"));
146             fds.setUnassigned(Arrays.asList("c", "d"));
147             fds.setModules(Arrays.asList(new Module(), new Module()));
148
149             when(candidateManager.getFilesDataStructure(
150                 ArgumentMatchers.eq(candidateId),
151                 ArgumentMatchers.any())).thenReturn(Optional.of(fds));
152
153         } catch (Exception e) {
154             logger.error(e.getMessage(), e);
155         }
156     }
157
158     @Test
159     void uploadSignedTest() throws IOException {
160         final String vspId = "vspId";
161         final String versionId = "versionId";
162         when(orchestrationTemplateCandidateUploadManager.putUploadInProgress(vspId, versionId, user)).thenReturn(new VspUploadStatusDto());
163         when(orchestrationTemplateCandidateUploadManager.putUploadInValidation(vspId, versionId, user)).thenReturn(new VspUploadStatusDto());
164         when(orchestrationTemplateCandidateUploadManager.putUploadInProcessing(vspId, versionId, user)).thenReturn(new VspUploadStatusDto());
165         Response response = orchestrationTemplateCandidate
166             .upload(vspId, versionId, mockAttachment("filename.zip", this.getClass().getResource("/files/sample-signed.zip")), user);
167         assertEquals(Status.OK.getStatusCode(), response.getStatus());
168         assertTrue(((UploadFileResponseDto) response.getEntity()).getErrors().isEmpty());
169     }
170
171     @Test
172     void uploadNotSignedTest() throws IOException {
173         final String vspId = "vspId";
174         final String versionId = "versionId";
175         when(orchestrationTemplateCandidateUploadManager.putUploadInProgress(vspId, versionId, user)).thenReturn(new VspUploadStatusDto());
176         when(orchestrationTemplateCandidateUploadManager.putUploadInValidation(vspId, versionId, user)).thenReturn(new VspUploadStatusDto());
177         when(orchestrationTemplateCandidateUploadManager.putUploadInProcessing(vspId, versionId, user)).thenReturn(new VspUploadStatusDto());
178         Response response = orchestrationTemplateCandidate.upload(vspId, versionId,
179             mockAttachment("filename.csar", this.getClass().getResource("/files/sample-not-signed.csar")), user);
180         assertEquals(Status.OK.getStatusCode(), response.getStatus());
181         assertTrue(((UploadFileResponseDto) response.getEntity()).getErrors().isEmpty());
182     }
183
184     @Test
185     void uploadNotSignedArtifactStorageManagerIsEnabledTest() throws IOException {
186         when(storageFactory.createArtifactStorageManager()).thenReturn(artifactStorageManager);
187         when(artifactStorageManager.isEnabled()).thenReturn(true);
188         when(artifactStorageManager.getStorageConfiguration()).thenReturn(new MinIoStorageArtifactStorageConfig
189             (true, new EndPoint("host", 9000, false), new Credentials("accessKey", "secretKey"), "tempPath", 10_000_000));
190
191         final Path path = Path.of("src/test/resources/files/sample-not-signed.csar");
192         final String vspId = "vspId";
193         final String versionId = "versionId";
194         when(artifactStorageManager.upload(eq(vspId), eq(versionId), any())).thenReturn(new MinIoArtifactInfo("vspId", "name"));
195         final byte[] bytes = Files.readAllBytes(path);
196         when(packageSizeReducer.reduce(any())).thenReturn(bytes);
197
198         when(orchestrationTemplateCandidateUploadManager.putUploadInProgress(vspId, versionId, user)).thenReturn(new VspUploadStatusDto());
199         when(orchestrationTemplateCandidateUploadManager.putUploadInValidation(vspId, versionId, user)).thenReturn(new VspUploadStatusDto());
200         when(orchestrationTemplateCandidateUploadManager.putUploadInProcessing(vspId, versionId, user)).thenReturn(new VspUploadStatusDto());
201
202         Response response = orchestrationTemplateCandidate.upload(vspId, versionId,
203             mockAttachment("filename.csar", this.getClass().getResource("/files/sample-not-signed.csar")), user);
204         assertEquals(Status.OK.getStatusCode(), response.getStatus());
205         assertTrue(((UploadFileResponseDto) response.getEntity()).getErrors().isEmpty());
206     }
207
208     private Attachment mockAttachment(final String fileName, final URL fileToUpload) throws IOException {
209         final Attachment attachment = Mockito.mock(Attachment.class);
210         final InputStream inputStream = Mockito.mock(InputStream.class);
211         when(attachment.getContentDisposition()).thenReturn(new ContentDisposition("test"));
212         final DataHandler dataHandler = Mockito.mock(DataHandler.class);
213         when(dataHandler.getName()).thenReturn(fileName);
214         when(attachment.getDataHandler()).thenReturn(dataHandler);
215         when(dataHandler.getInputStream()).thenReturn(inputStream);
216         when(inputStream.transferTo(any(OutputStream.class))).thenReturn(0L);
217         byte[] bytes = "upload package Test".getBytes();
218         if (Objects.nonNull(fileToUpload)) {
219             try {
220                 bytes = IOUtils.toByteArray(fileToUpload);
221             } catch (final IOException e) {
222                 logger.error("unexpected exception", e);
223                 fail("Not able to convert file to byte array");
224             }
225         }
226         when(attachment.getObject(ArgumentMatchers.any())).thenReturn(bytes);
227         return attachment;
228     }
229
230     @Test
231     void uploadSignNotValidTest() throws IOException {
232         //given
233         when(orchestrationTemplateCandidateUploadManager.putUploadInValidation(candidateId, versionId, user)).thenReturn(new VspUploadStatusDto());
234         //when
235         Response response = orchestrationTemplateCandidate
236             .upload(candidateId, versionId, mockAttachment("filename.zip", null), user);
237         //then
238         assertEquals(Status.NOT_ACCEPTABLE.getStatusCode(), response.getStatus());
239         assertFalse(((UploadFileResponseDto) response.getEntity()).getErrors().isEmpty());
240     }
241
242     @Test
243     void testCandidateGet() throws IOException {
244         Response rsp = orchestrationTemplateCandidate.get(candidateId, versionId, user);
245         assertEquals(Response.Status.OK.getStatusCode(), rsp.getStatus(), "Response status equals");
246         assertNotEquals(rsp.getHeaderString("Content-Disposition").indexOf("Candidate"), -1);
247         byte[] content = (byte[]) rsp.getEntity();
248         assertEquals("World", new String(content));
249     }
250
251     @Test
252     void testVendorSoftwareProductGet() throws IOException {
253         Response rsp = orchestrationTemplateCandidate.get(softwareProductId, versionId, user);
254         assertEquals(Response.Status.OK.getStatusCode(), rsp.getStatus(), "Response status equals");
255         assertNotEquals(rsp.getHeaderString("Content-Disposition").indexOf("Processed"), -1);
256         byte[] content = (byte[]) rsp.getEntity();
257         assertEquals("World", new String(content));
258     }
259
260     @Test
261     void testMissingGet() throws IOException {
262         Response rsp = orchestrationTemplateCandidate.get(UUID.randomUUID().toString(), versionId, user);
263         assertEquals(Response.Status.NOT_FOUND.getStatusCode(), rsp.getStatus(), "Response status equals");
264     }
265
266     @Test
267     void testAbort() {
268         try {
269             Response rsp = orchestrationTemplateCandidate.abort(candidateId, versionId);
270             assertEquals(Response.Status.OK.getStatusCode(), rsp.getStatus(), "Response status equals");
271             assertNull(rsp.getEntity());
272         } catch (Exception ex) {
273             logger.error("unexpected exception", ex);
274             fail("abort should not throw an exception");
275         }
276     }
277
278     @Test
279     void testProcess() {
280         try {
281             Response rsp = orchestrationTemplateCandidate.process(candidateId, versionId, user);
282             assertEquals(Response.Status.OK.getStatusCode(), rsp.getStatus(), "Response status equals");
283             assertNotNull(rsp.getEntity());
284             OrchestrationTemplateActionResponseDto dto = (OrchestrationTemplateActionResponseDto) rsp.getEntity();
285             assertEquals(UploadFileStatus.Success, dto.getStatus(), "status check");
286         } catch (Exception ex) {
287             logger.error("unexpected exception", ex);
288             fail("abort should not throw an exception");
289         }
290     }
291
292     @Test
293     void testFilesDataStructureUpload() {
294         try {
295             FileDataStructureDto dto = new FileDataStructureDto();
296             dto.setArtifacts(Arrays.asList("a", "b", "c"));
297             Response rsp = orchestrationTemplateCandidate.updateFilesDataStructure(candidateId, versionId, dto, user);
298             assertEquals(Response.Status.OK.getStatusCode(), rsp.getStatus(), "Response status equals");
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 testFilesDataStructureGet() {
307         try {
308             FileDataStructureDto dto = new FileDataStructureDto();
309             dto.setArtifacts(Arrays.asList("a", "b", "c"));
310             Response rsp = orchestrationTemplateCandidate.getFilesDataStructure(candidateId, versionId, user);
311             assertEquals(Response.Status.OK.getStatusCode(), rsp.getStatus(), "Response status equals");
312         } catch (Exception ex) {
313             logger.error("unexpected exception", ex);
314             fail("abort should not throw an exception");
315         }
316     }
317
318     @Test
319     void finishUploadMustBeCalledWhenExceptionHappensTest() {
320         //given
321         final VspUploadStatusDto vspUploadStatusDto = new VspUploadStatusDto();
322         vspUploadStatusDto.setLockId(UUID.randomUUID());
323         when(orchestrationTemplateCandidateUploadManager.putUploadInProgress(candidateId, versionId, user)).thenReturn(vspUploadStatusDto);
324         final RuntimeException forcedException = new RuntimeException();
325         when(fileToUpload.getDataHandler()).thenThrow(forcedException);
326         //when
327         final RuntimeException actualException = assertThrows(RuntimeException.class,
328             () -> orchestrationTemplateCandidate.upload(candidateId, versionId, fileToUpload, user));
329         //then
330         assertEquals(forcedException, actualException);
331         verify(orchestrationTemplateCandidateUploadManager)
332             .putUploadAsFinished(candidateId, versionId, vspUploadStatusDto.getLockId(), VspUploadStatus.ERROR, user);
333     }
334 }