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.assertTrue;
29 import static org.junit.jupiter.api.Assertions.fail;
30 import static org.mockito.ArgumentMatchers.any;
31 import static org.mockito.ArgumentMatchers.anyString;
32 import static org.mockito.Mockito.when;
34 import java.io.IOException;
36 import java.nio.file.Files;
37 import java.nio.file.Path;
38 import java.util.Arrays;
39 import java.util.Objects;
40 import java.util.Optional;
41 import java.util.UUID;
42 import javax.activation.DataHandler;
43 import javax.ws.rs.core.Response;
44 import javax.ws.rs.core.Response.Status;
45 import org.apache.commons.io.IOUtils;
46 import org.apache.commons.lang3.tuple.Pair;
47 import org.apache.cxf.jaxrs.ext.multipart.Attachment;
48 import org.apache.cxf.jaxrs.ext.multipart.ContentDisposition;
49 import org.junit.jupiter.api.BeforeEach;
50 import org.junit.jupiter.api.Test;
51 import org.mockito.ArgumentMatchers;
52 import org.mockito.Mock;
53 import org.mockito.Mockito;
54 import org.mockito.MockitoAnnotations;
55 import org.openecomp.core.utilities.orchestration.OnboardingTypesEnum;
56 import org.openecomp.sdc.activitylog.ActivityLogManager;
57 import org.openecomp.sdc.be.csar.storage.ArtifactStorageManager;
58 import org.openecomp.sdc.be.csar.storage.PackageSizeReducer;
59 import org.openecomp.sdc.be.csar.storage.PersistentStorageArtifactInfo;
60 import org.openecomp.sdc.logging.api.Logger;
61 import org.openecomp.sdc.logging.api.LoggerFactory;
62 import org.openecomp.sdc.vendorsoftwareproduct.OrchestrationTemplateCandidateManager;
63 import org.openecomp.sdc.vendorsoftwareproduct.VendorSoftwareProductManager;
64 import org.openecomp.sdc.vendorsoftwareproduct.types.OrchestrationTemplateActionResponse;
65 import org.openecomp.sdc.vendorsoftwareproduct.types.UploadFileResponse;
66 import org.openecomp.sdc.vendorsoftwareproduct.types.UploadFileStatus;
67 import org.openecomp.sdc.vendorsoftwareproduct.types.ValidationResponse;
68 import org.openecomp.sdc.vendorsoftwareproduct.types.candidateheat.FilesDataStructure;
69 import org.openecomp.sdc.vendorsoftwareproduct.types.candidateheat.Module;
70 import org.openecomp.sdcrests.vendorsoftwareproducts.types.FileDataStructureDto;
71 import org.openecomp.sdcrests.vendorsoftwareproducts.types.OrchestrationTemplateActionResponseDto;
72 import org.openecomp.sdcrests.vendorsoftwareproducts.types.UploadFileResponseDto;
74 class OrchestrationTemplateCandidateImplTest {
76 private final Logger logger = LoggerFactory.getLogger(OrchestrationTemplateCandidateImplTest.class);
79 private OrchestrationTemplateCandidateManager candidateManager;
81 private VendorSoftwareProductManager vendorSoftwareProductManager;
83 private ActivityLogManager activityLogManager;
85 private ArtifactStorageManager artifactStorageManager;
87 private PackageSizeReducer packageSizeReducer;
89 private OrchestrationTemplateCandidateImpl orchestrationTemplateCandidate;
91 private final String candidateId = UUID.randomUUID().toString();
92 private final String softwareProductId = UUID.randomUUID().toString();
93 private final String versionId = UUID.randomUUID().toString();
95 private final String user = "cs0008";
100 MockitoAnnotations.openMocks(this);
101 UploadFileResponse uploadFileResponse = new UploadFileResponse();
102 uploadFileResponse.setOnboardingType(OnboardingTypesEnum.ZIP);
103 uploadFileResponse.setNetworkPackageName("test");
104 when(candidateManager.upload(any(), any())).thenReturn(uploadFileResponse);
106 // get using the candidate manager.
107 Optional<Pair<String, byte[]>> zipFile = Optional.of(Pair.of("Hello", "World".getBytes()));
109 when(candidateManager.get(
110 ArgumentMatchers.eq(candidateId),
111 ArgumentMatchers.any())).thenReturn(zipFile);
113 when(vendorSoftwareProductManager.get(
114 ArgumentMatchers.eq(softwareProductId),
115 ArgumentMatchers.any())).thenReturn(zipFile);
117 OrchestrationTemplateActionResponse processResponse = new OrchestrationTemplateActionResponse();
118 processResponse.setStatus(UploadFileStatus.Success);
119 when(candidateManager.process(
120 ArgumentMatchers.eq(candidateId),
121 ArgumentMatchers.any())).thenReturn(processResponse);
123 ValidationResponse vr = new ValidationResponse();
124 when(candidateManager.updateFilesDataStructure(
125 ArgumentMatchers.eq(candidateId),
126 ArgumentMatchers.any(),
127 ArgumentMatchers.any())).thenReturn(vr);
129 FilesDataStructure fds = new FilesDataStructure();
130 fds.setArtifacts(Arrays.asList("a", "b"));
131 fds.setNested(Arrays.asList("foo", "bar"));
132 fds.setUnassigned(Arrays.asList("c", "d"));
133 fds.setModules(Arrays.asList(new Module(), new Module()));
135 when(candidateManager.getFilesDataStructure(
136 ArgumentMatchers.eq(candidateId),
137 ArgumentMatchers.any())).thenReturn(Optional.of(fds));
139 orchestrationTemplateCandidate =
140 new OrchestrationTemplateCandidateImpl(candidateManager, vendorSoftwareProductManager, activityLogManager,
141 artifactStorageManager, packageSizeReducer);
143 } catch (Exception e) {
144 logger.error(e.getMessage(), e);
149 void uploadSignedTest() {
150 Response response = orchestrationTemplateCandidate
151 .upload("1", "1", mockAttachment("filename.zip", this.getClass().getResource("/files/sample-signed.zip")),
153 assertEquals(Status.OK.getStatusCode(), response.getStatus());
154 assertTrue(((UploadFileResponseDto) response.getEntity()).getErrors().isEmpty());
158 void uploadNotSignedTest() throws IOException {
159 Response response = orchestrationTemplateCandidate.upload("1", "1",
160 mockAttachment("filename.csar", this.getClass().getResource("/files/sample-not-signed.csar")), "1");
161 assertEquals(Status.OK.getStatusCode(), response.getStatus());
162 assertTrue(((UploadFileResponseDto) response.getEntity()).getErrors().isEmpty());
166 void uploadNotSignedArtifactStorageManagerIsEnabledTest() throws IOException {
167 when(artifactStorageManager.isEnabled()).thenReturn(true);
168 final Path path = Path.of("src/test/resources/files/sample-not-signed.csar");
169 when(artifactStorageManager.upload(anyString(), anyString(), any())).thenReturn(new PersistentStorageArtifactInfo(path));
170 final byte[] bytes = Files.readAllBytes(path);
171 when(packageSizeReducer.reduce(any())).thenReturn(bytes);
173 Response response = orchestrationTemplateCandidate.upload("1", "1",
174 mockAttachment("filename.csar", this.getClass().getResource("/files/sample-not-signed.csar")), "1");
175 assertEquals(Status.OK.getStatusCode(), response.getStatus());
176 assertTrue(((UploadFileResponseDto) response.getEntity()).getErrors().isEmpty());
179 private Attachment mockAttachment(final String fileName, final URL fileToUpload) {
180 final Attachment attachment = Mockito.mock(Attachment.class);
181 when(attachment.getContentDisposition()).thenReturn(new ContentDisposition("test"));
182 final DataHandler dataHandler = Mockito.mock(DataHandler.class);
183 when(dataHandler.getName()).thenReturn(fileName);
184 when(attachment.getDataHandler()).thenReturn(dataHandler);
185 byte[] bytes = "upload package Test".getBytes();
186 if (Objects.nonNull(fileToUpload)) {
188 bytes = IOUtils.toByteArray(fileToUpload);
189 } catch (final IOException e) {
190 logger.error("unexpected exception", e);
191 fail("Not able to convert file to byte array");
194 when(attachment.getObject(ArgumentMatchers.any())).thenReturn(bytes);
199 void uploadSignNotValidTest() {
200 Response response = orchestrationTemplateCandidate
201 .upload("1", "1", mockAttachment("filename.zip", null), "1");
202 assertEquals(Status.NOT_ACCEPTABLE.getStatusCode(), response.getStatus());
203 assertFalse(((UploadFileResponseDto) response.getEntity()).getErrors().isEmpty());
207 void testCandidateGet() throws IOException {
208 Response rsp = orchestrationTemplateCandidate.get(candidateId, versionId, user);
209 assertEquals(Response.Status.OK.getStatusCode(), rsp.getStatus(), "Response status equals");
210 assertNotEquals(rsp.getHeaderString("Content-Disposition").indexOf("Candidate"), -1);
211 byte[] content = (byte[]) rsp.getEntity();
212 assertEquals("World", new String(content));
216 void testVendorSoftwareProductGet() throws IOException {
217 Response rsp = orchestrationTemplateCandidate.get(softwareProductId, versionId, user);
218 assertEquals(Response.Status.OK.getStatusCode(), rsp.getStatus(), "Response status equals");
219 assertNotEquals(rsp.getHeaderString("Content-Disposition").indexOf("Processed"), -1);
220 byte[] content = (byte[]) rsp.getEntity();
221 assertEquals("World", new String(content));
225 void testMissingGet() throws IOException {
226 Response rsp = orchestrationTemplateCandidate.get(UUID.randomUUID().toString(), versionId, user);
227 assertEquals(Response.Status.NOT_FOUND.getStatusCode(), rsp.getStatus(), "Response status equals");
233 Response rsp = orchestrationTemplateCandidate.abort(candidateId, versionId);
234 assertEquals(Response.Status.OK.getStatusCode(), rsp.getStatus(), "Response status equals");
235 assertNull(rsp.getEntity());
236 } catch (Exception ex) {
237 logger.error("unexpected exception", ex);
238 fail("abort should not throw an exception");
245 Response rsp = orchestrationTemplateCandidate.process(candidateId, versionId, user);
246 assertEquals(Response.Status.OK.getStatusCode(), rsp.getStatus(), "Response status equals");
247 assertNotNull(rsp.getEntity());
248 OrchestrationTemplateActionResponseDto dto = (OrchestrationTemplateActionResponseDto) rsp.getEntity();
249 assertEquals(UploadFileStatus.Success, dto.getStatus(), "status check");
250 } catch (Exception ex) {
251 logger.error("unexpected exception", ex);
252 fail("abort should not throw an exception");
257 void testFilesDataStructureUpload() {
259 FileDataStructureDto dto = new FileDataStructureDto();
260 dto.setArtifacts(Arrays.asList("a", "b", "c"));
261 Response rsp = orchestrationTemplateCandidate.updateFilesDataStructure(candidateId, versionId, dto, user);
262 assertEquals(Response.Status.OK.getStatusCode(), rsp.getStatus(), "Response status equals");
263 } catch (Exception ex) {
264 logger.error("unexpected exception", ex);
265 fail("abort should not throw an exception");
270 void testFilesDataStructureGet() {
272 FileDataStructureDto dto = new FileDataStructureDto();
273 dto.setArtifacts(Arrays.asList("a", "b", "c"));
274 Response rsp = orchestrationTemplateCandidate.getFilesDataStructure(candidateId, versionId, user);
275 assertEquals(Response.Status.OK.getStatusCode(), rsp.getStatus(), "Response status equals");
276 } catch (Exception ex) {
277 logger.error("unexpected exception", ex);
278 fail("abort should not throw an exception");