2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.openecomp.sdcrests.vsp.rest.services;
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;
37 import java.io.IOException;
38 import java.io.InputStream;
39 import java.io.OutputStream;
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.junit.jupiter.api.io.TempDir;
58 import org.mockito.ArgumentCaptor;
59 import org.mockito.ArgumentMatchers;
60 import org.mockito.InjectMocks;
61 import org.mockito.Mock;
62 import org.mockito.Mockito;
63 import org.mockito.MockitoAnnotations;
64 import org.openecomp.core.utilities.orchestration.OnboardingTypesEnum;
65 import org.openecomp.sdc.activitylog.ActivityLogManager;
66 import org.openecomp.sdc.be.csar.storage.ArtifactStorageManager;
67 import org.openecomp.sdc.be.csar.storage.MinIoArtifactInfo;
68 import org.openecomp.sdc.be.csar.storage.MinIoStorageArtifactStorageConfig;
69 import org.openecomp.sdc.be.csar.storage.MinIoStorageArtifactStorageConfig.Credentials;
70 import org.openecomp.sdc.be.csar.storage.MinIoStorageArtifactStorageConfig.EndPoint;
71 import org.openecomp.sdc.be.csar.storage.PackageSizeReducer;
72 import org.openecomp.sdc.be.csar.storage.StorageFactory;
73 import org.openecomp.sdc.common.errors.CoreException;
74 import org.openecomp.sdc.logging.api.Logger;
75 import org.openecomp.sdc.logging.api.LoggerFactory;
76 import org.openecomp.sdc.vendorsoftwareproduct.OrchestrationTemplateCandidateManager;
77 import org.openecomp.sdc.vendorsoftwareproduct.VendorSoftwareProductManager;
78 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.VspUploadStatus;
79 import org.openecomp.sdc.vendorsoftwareproduct.types.OrchestrationTemplateActionResponse;
80 import org.openecomp.sdc.vendorsoftwareproduct.types.UploadFileResponse;
81 import org.openecomp.sdc.vendorsoftwareproduct.types.UploadFileStatus;
82 import org.openecomp.sdc.vendorsoftwareproduct.types.ValidationResponse;
83 import org.openecomp.sdc.vendorsoftwareproduct.types.candidateheat.FilesDataStructure;
84 import org.openecomp.sdc.vendorsoftwareproduct.types.candidateheat.Module;
85 import org.openecomp.sdcrests.vendorsoftwareproducts.types.FileDataStructureDto;
86 import org.openecomp.sdcrests.vendorsoftwareproducts.types.OrchestrationTemplateActionResponseDto;
87 import org.openecomp.sdcrests.vendorsoftwareproducts.types.UploadFileResponseDto;
88 import org.openecomp.sdcrests.vendorsoftwareproducts.types.VspUploadStatusDto;
90 class OrchestrationTemplateCandidateImplTest {
92 private final Logger logger = LoggerFactory.getLogger(OrchestrationTemplateCandidateImplTest.class);
93 private final String candidateId = UUID.randomUUID().toString();
94 private final String softwareProductId = UUID.randomUUID().toString();
95 private final String versionId = UUID.randomUUID().toString();
96 private final String user = "cs0008";
98 private OrchestrationTemplateCandidateManager candidateManager;
100 private VendorSoftwareProductManager vendorSoftwareProductManager;
102 private ActivityLogManager activityLogManager;
104 private PackageSizeReducer packageSizeReducer;
106 private OrchestrationTemplateCandidateUploadManager orchestrationTemplateCandidateUploadManager;
108 private StorageFactory storageFactory;
110 private Attachment fileToUpload;
112 private ArtifactStorageManager artifactStorageManager;
114 private OrchestrationTemplateCandidateImpl orchestrationTemplateCandidate;
120 public void setUp() throws IOException {
121 MockitoAnnotations.openMocks(this);
122 UploadFileResponse uploadFileResponse = new UploadFileResponse();
123 uploadFileResponse.setOnboardingType(OnboardingTypesEnum.ZIP);
124 uploadFileResponse.setNetworkPackageName("test");
125 when(candidateManager.upload(any(), any())).thenReturn(uploadFileResponse);
127 // get using the candidate manager.
128 Optional<Pair<String, byte[]>> zipFile = Optional.of(Pair.of("Hello", "World".getBytes()));
130 when(candidateManager.get(
131 ArgumentMatchers.eq(candidateId),
132 ArgumentMatchers.any())).thenReturn(zipFile);
134 when(vendorSoftwareProductManager.get(
135 ArgumentMatchers.eq(softwareProductId),
136 ArgumentMatchers.any())).thenReturn(zipFile);
138 OrchestrationTemplateActionResponse processResponse = new OrchestrationTemplateActionResponse();
139 processResponse.setStatus(UploadFileStatus.Success);
140 when(candidateManager.process(
141 ArgumentMatchers.eq(candidateId),
142 ArgumentMatchers.any())).thenReturn(processResponse);
144 ValidationResponse vr = new ValidationResponse();
145 when(candidateManager.updateFilesDataStructure(
146 ArgumentMatchers.eq(candidateId),
147 ArgumentMatchers.any(),
148 ArgumentMatchers.any())).thenReturn(vr);
150 FilesDataStructure fds = new FilesDataStructure();
151 fds.setArtifacts(Arrays.asList("a", "b"));
152 fds.setNested(Arrays.asList("foo", "bar"));
153 fds.setUnassigned(Arrays.asList("c", "d"));
154 fds.setModules(Arrays.asList(new Module(), new Module()));
156 when(candidateManager.getFilesDataStructure(
157 ArgumentMatchers.eq(candidateId),
158 ArgumentMatchers.any())).thenReturn(Optional.of(fds));
159 when(storageFactory.createArtifactStorageManager()).thenReturn(artifactStorageManager);
160 when(storageFactory.createPackageSizeReducer()).thenReturn(Optional.of(packageSizeReducer));
164 void uploadSignedTest() throws IOException {
165 final String vspId = "vspId";
166 final String versionId = "versionId";
167 when(orchestrationTemplateCandidateUploadManager.findLatestStatus(vspId, versionId, user)).thenReturn(Optional.empty());
168 final UUID lockId = UUID.randomUUID();
169 when(orchestrationTemplateCandidateUploadManager.putUploadInProgress(vspId, versionId, user))
170 .thenReturn(createVspUploadStatus(lockId, VspUploadStatus.UPLOADING));
171 when(orchestrationTemplateCandidateUploadManager.putUploadInValidation(vspId, versionId, user))
172 .thenReturn(createVspUploadStatus(lockId, VspUploadStatus.VALIDATING));
173 when(orchestrationTemplateCandidateUploadManager.putUploadInProcessing(vspId, versionId, user))
174 .thenReturn(createVspUploadStatus(lockId, VspUploadStatus.PROCESSING));
175 Response response = orchestrationTemplateCandidate
176 .upload(vspId, versionId, mockAttachment("filename.zip", this.getClass().getResource("/files/sample-signed.zip")), user);
177 assertEquals(Status.OK.getStatusCode(), response.getStatus());
178 assertTrue(((UploadFileResponseDto) response.getEntity()).getErrors().isEmpty());
182 void uploadNotSignedTest() throws IOException {
183 final String vspId = "vspId";
184 final String versionId = "versionId";
185 when(orchestrationTemplateCandidateUploadManager.findLatestStatus(vspId, versionId, user)).thenReturn(Optional.empty());
186 final UUID lockId = UUID.randomUUID();
187 when(orchestrationTemplateCandidateUploadManager.putUploadInProgress(vspId, versionId, user))
188 .thenReturn(createVspUploadStatus(lockId, VspUploadStatus.UPLOADING));
189 when(orchestrationTemplateCandidateUploadManager.putUploadInValidation(vspId, versionId, user))
190 .thenReturn(createVspUploadStatus(lockId, VspUploadStatus.VALIDATING));
191 when(orchestrationTemplateCandidateUploadManager.putUploadInProcessing(vspId, versionId, user))
192 .thenReturn(createVspUploadStatus(lockId, VspUploadStatus.PROCESSING));
193 Response response = orchestrationTemplateCandidate.upload(vspId, versionId,
194 mockAttachment("filename.csar", this.getClass().getResource("/files/sample-not-signed.csar")), user);
195 assertEquals(Status.OK.getStatusCode(), response.getStatus());
196 assertTrue(((UploadFileResponseDto) response.getEntity()).getErrors().isEmpty());
200 void uploadArtifactStorageTest() throws IOException {
202 final String vspId = "vspId";
203 final String versionId = "versionId";
204 when(orchestrationTemplateCandidateUploadManager.findLatestStatus(vspId, versionId, user)).thenReturn(Optional.empty());
205 final UUID lockId = UUID.randomUUID();
206 when(orchestrationTemplateCandidateUploadManager.putUploadInProgress(vspId, versionId, user))
207 .thenReturn(createVspUploadStatus(lockId, VspUploadStatus.UPLOADING));
208 when(orchestrationTemplateCandidateUploadManager.putUploadInValidation(vspId, versionId, user))
209 .thenReturn(createVspUploadStatus(lockId, VspUploadStatus.VALIDATING));
210 when(orchestrationTemplateCandidateUploadManager.putUploadInProcessing(vspId, versionId, user))
211 .thenReturn(createVspUploadStatus(lockId, VspUploadStatus.PROCESSING));
212 when(artifactStorageManager.isEnabled()).thenReturn(true);
213 final MinIoStorageArtifactStorageConfig minIoConfig =
214 new MinIoStorageArtifactStorageConfig(true,
215 new EndPoint("", 9000, true),
216 new Credentials("", ""), tempDir.toString(), 1000
219 when(artifactStorageManager.getStorageConfiguration()).thenReturn(minIoConfig);
220 final MinIoArtifactInfo artifactInfo = new MinIoArtifactInfo(vspId, versionId);
221 final Attachment attachmentMock = mockAttachment("filename.csar", this.getClass().getResource("/files/sample-not-signed.csar"));
222 final byte[] attachmentBytes = attachmentMock.getObject(byte[].class);
223 artifactInfo.setBytes(attachmentBytes);
224 final ArgumentCaptor<Path> reduceTempDirectoryArg = ArgumentCaptor.forClass(Path.class);
225 when(packageSizeReducer.reduce(reduceTempDirectoryArg.capture())).thenReturn(attachmentBytes);
226 when(artifactStorageManager.upload(eq(vspId), eq(versionId), any(InputStream.class))).thenReturn(artifactInfo);
228 Response response = orchestrationTemplateCandidate.upload(vspId, versionId, attachmentMock, user);
230 assertEquals(Status.OK.getStatusCode(), response.getStatus());
231 assertTrue(((UploadFileResponseDto) response.getEntity()).getErrors().isEmpty());
232 final Path actualReduceTempFolder = reduceTempDirectoryArg.getValue();
233 final Path expectedReduceTempFolder = tempDir.resolve(Path.of(vspId, versionId));
234 assertTrue(actualReduceTempFolder.startsWith(expectedReduceTempFolder),
235 String.format("Reduce temporary directory should be '%s'", expectedReduceTempFolder));
239 private VspUploadStatusDto createVspUploadStatus(final UUID lockId, final VspUploadStatus uploadStatus) {
240 final VspUploadStatusDto vspUploadStatusProcessing = new VspUploadStatusDto();
241 vspUploadStatusProcessing.setLockId(lockId);
242 vspUploadStatusProcessing.setStatus(uploadStatus);
243 return vspUploadStatusProcessing;
247 void uploadNotSignedArtifactStorageManagerIsEnabledTest() throws IOException {
248 when(artifactStorageManager.isEnabled()).thenReturn(true);
249 when(artifactStorageManager.getStorageConfiguration()).thenReturn(new MinIoStorageArtifactStorageConfig
250 (true, new EndPoint("host", 9000, false), new Credentials("accessKey", "secretKey"), "tempPath", 10_000_000));
252 final Path path = Path.of("src/test/resources/files/sample-not-signed.csar");
253 final String vspId = "vspId";
254 final String versionId = "versionId";
255 when(artifactStorageManager.upload(eq(vspId), eq(versionId), any())).thenReturn(new MinIoArtifactInfo("vspId", "name"));
256 final byte[] bytes = Files.readAllBytes(path);
257 when(packageSizeReducer.reduce(any())).thenReturn(bytes);
259 final VspUploadStatusDto vspUploadStatusDto = new VspUploadStatusDto();
260 vspUploadStatusDto.setStatus(VspUploadStatus.UPLOADING);
261 when(orchestrationTemplateCandidateUploadManager.findLatestStatus(vspId, versionId, user)).thenReturn(Optional.of(vspUploadStatusDto));
262 when(orchestrationTemplateCandidateUploadManager.putUploadInValidation(vspId, versionId, user)).thenReturn(new VspUploadStatusDto());
263 when(orchestrationTemplateCandidateUploadManager.putUploadInProcessing(vspId, versionId, user)).thenReturn(new VspUploadStatusDto());
265 Response response = orchestrationTemplateCandidate.upload(vspId, versionId,
266 mockAttachment("filename.csar", this.getClass().getResource("/files/sample-not-signed.csar")), user);
267 assertEquals(Status.OK.getStatusCode(), response.getStatus());
268 assertTrue(((UploadFileResponseDto) response.getEntity()).getErrors().isEmpty());
271 private Attachment mockAttachment(final String fileName, final URL fileToUpload) throws IOException {
272 final Attachment attachment = Mockito.mock(Attachment.class);
273 final InputStream inputStream = Mockito.mock(InputStream.class);
274 when(attachment.getContentDisposition()).thenReturn(new ContentDisposition("test"));
275 final DataHandler dataHandler = Mockito.mock(DataHandler.class);
276 when(dataHandler.getName()).thenReturn(fileName);
277 when(attachment.getDataHandler()).thenReturn(dataHandler);
278 when(dataHandler.getInputStream()).thenReturn(inputStream);
279 when(inputStream.transferTo(any(OutputStream.class))).thenReturn(0L);
280 byte[] bytes = "upload package Test".getBytes();
281 if (Objects.nonNull(fileToUpload)) {
283 bytes = IOUtils.toByteArray(fileToUpload);
284 } catch (final IOException e) {
285 logger.error("unexpected exception", e);
286 fail("Not able to convert file to byte array");
289 when(attachment.getObject(ArgumentMatchers.any())).thenReturn(bytes);
294 void uploadSignNotValidTest() throws IOException {
296 final VspUploadStatusDto vspUploadStatusDto = new VspUploadStatusDto();
297 vspUploadStatusDto.setStatus(VspUploadStatus.UPLOADING);
298 when(orchestrationTemplateCandidateUploadManager.findLatestStatus(candidateId, versionId, user))
299 .thenReturn(Optional.of(vspUploadStatusDto));
300 when(orchestrationTemplateCandidateUploadManager.putUploadInValidation(candidateId, versionId, user)).thenReturn(new VspUploadStatusDto());
302 Response response = orchestrationTemplateCandidate
303 .upload(candidateId, versionId, mockAttachment("filename.zip", null), user);
305 assertEquals(Status.NOT_ACCEPTABLE.getStatusCode(), response.getStatus());
306 assertFalse(((UploadFileResponseDto) response.getEntity()).getErrors().isEmpty());
310 void testCandidateGet() throws IOException {
311 Response rsp = orchestrationTemplateCandidate.get(candidateId, versionId, user);
312 assertEquals(Response.Status.OK.getStatusCode(), rsp.getStatus(), "Response status equals");
313 assertNotEquals(rsp.getHeaderString("Content-Disposition").indexOf("Candidate"), -1);
314 byte[] content = (byte[]) rsp.getEntity();
315 assertEquals("World", new String(content));
319 void testVendorSoftwareProductGet() throws IOException {
320 Response rsp = orchestrationTemplateCandidate.get(softwareProductId, versionId, user);
321 assertEquals(Response.Status.OK.getStatusCode(), rsp.getStatus(), "Response status equals");
322 assertNotEquals(rsp.getHeaderString("Content-Disposition").indexOf("Processed"), -1);
323 byte[] content = (byte[]) rsp.getEntity();
324 assertEquals("World", new String(content));
328 void testMissingGet() throws IOException {
329 Response rsp = orchestrationTemplateCandidate.get(UUID.randomUUID().toString(), versionId, user);
330 assertEquals(Response.Status.NOT_FOUND.getStatusCode(), rsp.getStatus(), "Response status equals");
336 Response rsp = orchestrationTemplateCandidate.abort(candidateId, versionId);
337 assertEquals(Response.Status.OK.getStatusCode(), rsp.getStatus(), "Response status equals");
338 assertNull(rsp.getEntity());
339 } catch (Exception ex) {
340 logger.error("unexpected exception", ex);
341 fail("abort should not throw an exception");
348 Response rsp = orchestrationTemplateCandidate.process(candidateId, versionId, user);
349 assertEquals(Response.Status.OK.getStatusCode(), rsp.getStatus(), "Response status equals");
350 assertNotNull(rsp.getEntity());
351 OrchestrationTemplateActionResponseDto dto = (OrchestrationTemplateActionResponseDto) rsp.getEntity();
352 assertEquals(UploadFileStatus.Success, dto.getStatus(), "status check");
353 } catch (Exception ex) {
354 logger.error("unexpected exception", ex);
355 fail("abort should not throw an exception");
360 void testFilesDataStructureUpload() {
362 FileDataStructureDto dto = new FileDataStructureDto();
363 dto.setArtifacts(Arrays.asList("a", "b", "c"));
364 Response rsp = orchestrationTemplateCandidate.updateFilesDataStructure(candidateId, versionId, dto, user);
365 assertEquals(Response.Status.OK.getStatusCode(), rsp.getStatus(), "Response status equals");
366 } catch (Exception ex) {
367 logger.error("unexpected exception", ex);
368 fail("abort should not throw an exception");
373 void testFilesDataStructureGet() {
375 FileDataStructureDto dto = new FileDataStructureDto();
376 dto.setArtifacts(Arrays.asList("a", "b", "c"));
377 Response rsp = orchestrationTemplateCandidate.getFilesDataStructure(candidateId, versionId, user);
378 assertEquals(Response.Status.OK.getStatusCode(), rsp.getStatus(), "Response status equals");
379 } catch (Exception ex) {
380 logger.error("unexpected exception", ex);
381 fail("abort should not throw an exception");
386 void finishUploadMustBeCalledWhenExceptionHappensTest() {
388 final VspUploadStatusDto vspUploadStatusDto = createVspUploadStatus(UUID.randomUUID(), VspUploadStatus.UPLOADING);
389 when(orchestrationTemplateCandidateUploadManager.findLatestStatus(candidateId, versionId, user)).thenReturn(Optional.empty());
390 when(orchestrationTemplateCandidateUploadManager.putUploadInProgress(candidateId, versionId, user)).thenReturn(vspUploadStatusDto);
391 final RuntimeException forcedException = new RuntimeException();
392 when(fileToUpload.getDataHandler()).thenThrow(forcedException);
394 final RuntimeException actualException = assertThrows(RuntimeException.class,
395 () -> orchestrationTemplateCandidate.upload(candidateId, versionId, fileToUpload, user));
397 assertEquals(forcedException, actualException);
398 verify(orchestrationTemplateCandidateUploadManager)
399 .putUploadAsFinished(candidateId, versionId, vspUploadStatusDto.getLockId(), VspUploadStatus.ERROR, user);
403 // void uploadTestWithLatestStatusComplete() {
404 // final VspUploadStatusDto vspUploadStatusDto = new VspUploadStatusDto();
405 // vspUploadStatusDto.setComplete(true);
407 // when(orchestrationTemplateCandidateUploadManager.findLatestStatus(candidateId, versionId, user)).thenReturn(Optional.of(vspUploadStatusDto));
408 // final Attachment mock = Mockito.mock(Attachment.class);
409 // when(mock.getDataHandler()).thenReturn(Mockito.mock(DataHandler.class));
411 // final CoreException actualException = assertThrows(CoreException.class,
412 // () -> orchestrationTemplateCandidate.upload(candidateId, versionId, mock, user));
413 // final CoreException expectedException = couldNotAcceptPackageNoUploadInProgress(candidateId, versionId).get();
415 // assertEquals(expectedException.code().id(), actualException.code().id());
416 // assertEquals(expectedException.code().message(), actualException.code().message());
417 // verify(orchestrationTemplateCandidateUploadManager).findLatestStatus(candidateId, versionId, user);
421 void uploadTestWithUploadInProgress() {
422 final VspUploadStatusDto vspUploadStatusDto = new VspUploadStatusDto();
423 vspUploadStatusDto.setComplete(false);
424 vspUploadStatusDto.setStatus(VspUploadStatus.PROCESSING);
426 when(orchestrationTemplateCandidateUploadManager.findLatestStatus(candidateId, versionId, user)).thenReturn(Optional.of(vspUploadStatusDto));
427 final Attachment mock = Mockito.mock(Attachment.class);
428 when(mock.getDataHandler()).thenReturn(Mockito.mock(DataHandler.class));
430 final CoreException actualException = assertThrows(CoreException.class,
431 () -> orchestrationTemplateCandidate.upload(candidateId, versionId, mock, user));
432 final CoreException expectedException = vspUploadAlreadyInProgress(candidateId, versionId).get();
434 assertEquals(expectedException.code().id(), actualException.code().id());
435 assertEquals(expectedException.code().message(), actualException.code().message());
436 verify(orchestrationTemplateCandidateUploadManager).findLatestStatus(candidateId, versionId, user);