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);
77 private final String candidateId = UUID.randomUUID().toString();
78 private final String softwareProductId = UUID.randomUUID().toString();
79 private final String versionId = UUID.randomUUID().toString();
80 private final String user = "cs0008";
82 private OrchestrationTemplateCandidateManager candidateManager;
84 private VendorSoftwareProductManager vendorSoftwareProductManager;
86 private ActivityLogManager activityLogManager;
88 private ArtifactStorageManager artifactStorageManager;
90 private PackageSizeReducer packageSizeReducer;
91 private OrchestrationTemplateCandidateImpl orchestrationTemplateCandidate;
96 MockitoAnnotations.openMocks(this);
97 UploadFileResponse uploadFileResponse = new UploadFileResponse();
98 uploadFileResponse.setOnboardingType(OnboardingTypesEnum.ZIP);
99 uploadFileResponse.setNetworkPackageName("test");
100 when(candidateManager.upload(any(), any())).thenReturn(uploadFileResponse);
102 // get using the candidate manager.
103 Optional<Pair<String, byte[]>> zipFile = Optional.of(Pair.of("Hello", "World".getBytes()));
105 when(candidateManager.get(
106 ArgumentMatchers.eq(candidateId),
107 ArgumentMatchers.any())).thenReturn(zipFile);
109 when(vendorSoftwareProductManager.get(
110 ArgumentMatchers.eq(softwareProductId),
111 ArgumentMatchers.any())).thenReturn(zipFile);
113 OrchestrationTemplateActionResponse processResponse = new OrchestrationTemplateActionResponse();
114 processResponse.setStatus(UploadFileStatus.Success);
115 when(candidateManager.process(
116 ArgumentMatchers.eq(candidateId),
117 ArgumentMatchers.any())).thenReturn(processResponse);
119 ValidationResponse vr = new ValidationResponse();
120 when(candidateManager.updateFilesDataStructure(
121 ArgumentMatchers.eq(candidateId),
122 ArgumentMatchers.any(),
123 ArgumentMatchers.any())).thenReturn(vr);
125 FilesDataStructure fds = new FilesDataStructure();
126 fds.setArtifacts(Arrays.asList("a", "b"));
127 fds.setNested(Arrays.asList("foo", "bar"));
128 fds.setUnassigned(Arrays.asList("c", "d"));
129 fds.setModules(Arrays.asList(new Module(), new Module()));
131 when(candidateManager.getFilesDataStructure(
132 ArgumentMatchers.eq(candidateId),
133 ArgumentMatchers.any())).thenReturn(Optional.of(fds));
135 orchestrationTemplateCandidate =
136 new OrchestrationTemplateCandidateImpl(candidateManager, vendorSoftwareProductManager, activityLogManager,
137 artifactStorageManager, packageSizeReducer);
139 } catch (Exception e) {
140 logger.error(e.getMessage(), e);
145 void uploadSignedTest() {
146 Response response = orchestrationTemplateCandidate
147 .upload("1", "1", mockAttachment("filename.zip", this.getClass().getResource("/files/sample-signed.zip")),
149 assertEquals(Status.OK.getStatusCode(), response.getStatus());
150 assertTrue(((UploadFileResponseDto) response.getEntity()).getErrors().isEmpty());
154 void uploadNotSignedTest() throws IOException {
155 Response response = orchestrationTemplateCandidate.upload("1", "1",
156 mockAttachment("filename.csar", this.getClass().getResource("/files/sample-not-signed.csar")), "1");
157 assertEquals(Status.OK.getStatusCode(), response.getStatus());
158 assertTrue(((UploadFileResponseDto) response.getEntity()).getErrors().isEmpty());
162 void uploadNotSignedArtifactStorageManagerIsEnabledTest() throws IOException {
163 when(artifactStorageManager.isEnabled()).thenReturn(true);
164 final Path path = Path.of("src/test/resources/files/sample-not-signed.csar");
165 when(artifactStorageManager.upload(anyString(), anyString(), any())).thenReturn(new PersistentStorageArtifactInfo(path));
166 final byte[] bytes = Files.readAllBytes(path);
167 when(packageSizeReducer.reduce(any())).thenReturn(bytes);
169 Response response = orchestrationTemplateCandidate.upload("1", "1",
170 mockAttachment("filename.csar", this.getClass().getResource("/files/sample-not-signed.csar")), "1");
171 assertEquals(Status.OK.getStatusCode(), response.getStatus());
172 assertTrue(((UploadFileResponseDto) response.getEntity()).getErrors().isEmpty());
175 private Attachment mockAttachment(final String fileName, final URL fileToUpload) {
176 final Attachment attachment = Mockito.mock(Attachment.class);
177 when(attachment.getContentDisposition()).thenReturn(new ContentDisposition("test"));
178 final DataHandler dataHandler = Mockito.mock(DataHandler.class);
179 when(dataHandler.getName()).thenReturn(fileName);
180 when(attachment.getDataHandler()).thenReturn(dataHandler);
181 byte[] bytes = "upload package Test".getBytes();
182 if (Objects.nonNull(fileToUpload)) {
184 bytes = IOUtils.toByteArray(fileToUpload);
185 } catch (final IOException e) {
186 logger.error("unexpected exception", e);
187 fail("Not able to convert file to byte array");
190 when(attachment.getObject(ArgumentMatchers.any())).thenReturn(bytes);
195 void uploadSignNotValidTest() {
196 Response response = orchestrationTemplateCandidate
197 .upload("1", "1", mockAttachment("filename.zip", null), "1");
198 assertEquals(Status.NOT_ACCEPTABLE.getStatusCode(), response.getStatus());
199 assertFalse(((UploadFileResponseDto) response.getEntity()).getErrors().isEmpty());
203 void testCandidateGet() throws IOException {
204 Response rsp = orchestrationTemplateCandidate.get(candidateId, versionId, user);
205 assertEquals(Response.Status.OK.getStatusCode(), rsp.getStatus(), "Response status equals");
206 assertNotEquals(rsp.getHeaderString("Content-Disposition").indexOf("Candidate"), -1);
207 byte[] content = (byte[]) rsp.getEntity();
208 assertEquals("World", new String(content));
212 void testVendorSoftwareProductGet() throws IOException {
213 Response rsp = orchestrationTemplateCandidate.get(softwareProductId, versionId, user);
214 assertEquals(Response.Status.OK.getStatusCode(), rsp.getStatus(), "Response status equals");
215 assertNotEquals(rsp.getHeaderString("Content-Disposition").indexOf("Processed"), -1);
216 byte[] content = (byte[]) rsp.getEntity();
217 assertEquals("World", new String(content));
221 void testMissingGet() throws IOException {
222 Response rsp = orchestrationTemplateCandidate.get(UUID.randomUUID().toString(), versionId, user);
223 assertEquals(Response.Status.NOT_FOUND.getStatusCode(), rsp.getStatus(), "Response status equals");
229 Response rsp = orchestrationTemplateCandidate.abort(candidateId, versionId);
230 assertEquals(Response.Status.OK.getStatusCode(), rsp.getStatus(), "Response status equals");
231 assertNull(rsp.getEntity());
232 } catch (Exception ex) {
233 logger.error("unexpected exception", ex);
234 fail("abort should not throw an exception");
241 Response rsp = orchestrationTemplateCandidate.process(candidateId, versionId, user);
242 assertEquals(Response.Status.OK.getStatusCode(), rsp.getStatus(), "Response status equals");
243 assertNotNull(rsp.getEntity());
244 OrchestrationTemplateActionResponseDto dto = (OrchestrationTemplateActionResponseDto) rsp.getEntity();
245 assertEquals(UploadFileStatus.Success, dto.getStatus(), "status check");
246 } catch (Exception ex) {
247 logger.error("unexpected exception", ex);
248 fail("abort should not throw an exception");
253 void testFilesDataStructureUpload() {
255 FileDataStructureDto dto = new FileDataStructureDto();
256 dto.setArtifacts(Arrays.asList("a", "b", "c"));
257 Response rsp = orchestrationTemplateCandidate.updateFilesDataStructure(candidateId, versionId, dto, user);
258 assertEquals(Response.Status.OK.getStatusCode(), rsp.getStatus(), "Response status equals");
259 } catch (Exception ex) {
260 logger.error("unexpected exception", ex);
261 fail("abort should not throw an exception");
266 void testFilesDataStructureGet() {
268 FileDataStructureDto dto = new FileDataStructureDto();
269 dto.setArtifacts(Arrays.asList("a", "b", "c"));
270 Response rsp = orchestrationTemplateCandidate.getFilesDataStructure(candidateId, versionId, user);
271 assertEquals(Response.Status.OK.getStatusCode(), rsp.getStatus(), "Response status equals");
272 } catch (Exception ex) {
273 logger.error("unexpected exception", ex);
274 fail("abort should not throw an exception");