2 * Copyright © 2016-2017 European Support Limited
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package org.openecomp.sdc.vendorsoftwareproduct.upload.csar;
20 import static junit.framework.TestCase.assertTrue;
21 import static org.junit.Assert.assertEquals;
22 import static org.mockito.ArgumentMatchers.any;
23 import static org.mockito.Mockito.doReturn;
25 import java.io.ByteArrayInputStream;
27 import java.io.IOException;
28 import java.io.InputStream;
29 import java.util.List;
30 import java.util.function.Predicate;
32 import org.junit.Before;
33 import org.junit.Test;
34 import org.mockito.InjectMocks;
35 import org.mockito.Mock;
36 import org.mockito.MockitoAnnotations;
37 import org.mockito.Spy;
38 import org.openecomp.sdc.common.errors.Messages;
39 import org.openecomp.sdc.datatypes.error.ErrorMessage;
40 import org.openecomp.sdc.vendorsoftwareproduct.dao.OrchestrationTemplateCandidateDao;
41 import org.openecomp.sdc.vendorsoftwareproduct.dao.VendorSoftwareProductInfoDao;
42 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.VspDetails;
43 import org.openecomp.sdc.vendorsoftwareproduct.impl.OrchestrationTemplateCandidateManagerImpl;
44 import org.openecomp.sdc.vendorsoftwareproduct.services.impl.filedatastructuremodule.CandidateServiceImpl;
45 import org.openecomp.sdc.vendorsoftwareproduct.services.impl.filedatastructuremodule.ManifestCreatorNamingConventionImpl;
46 import org.openecomp.sdc.vendorsoftwareproduct.types.UploadFileResponse;
47 import org.openecomp.sdc.versioning.dao.types.Version;
49 public class UploadCSARFileTest {
51 public static final Version VERSION01 = new Version("0.1");
54 private CandidateServiceImpl candidateService;
56 private VendorSoftwareProductInfoDao vspInfoDaoMock;
58 private OrchestrationTemplateCandidateDao orchestrationTemplateCandidateDao;
60 private ManifestCreatorNamingConventionImpl manifestCreator;
63 private OrchestrationTemplateCandidateManagerImpl candidateManager;
66 private static String id001 = "dummyId";
67 private static Version activeVersion002 = new Version("dummyVersion");
68 private static final String BASE_DIR = "/vspmanager.csar";
69 private static final String CSAR = "csar";
73 public void setUp() throws Exception {
74 MockitoAnnotations.initMocks(this);
75 candidateService = new CandidateServiceImpl(manifestCreator, orchestrationTemplateCandidateDao);
76 candidateManager = new OrchestrationTemplateCandidateManagerImpl(vspInfoDaoMock,
81 public void testSuccessfulUploadFile() throws Exception {
82 VspDetails vspDetails = new VspDetails(id001, activeVersion002);
83 doReturn(vspDetails).when(vspInfoDaoMock).get(any(VspDetails.class));
85 testCsarUpload("successfulUpload.csar", 0);
89 public void testIllegalUploadInvalidFileInRoot() throws Exception {
90 VspDetails vspDetails = new VspDetails(id001, activeVersion002);
91 doReturn(vspDetails).when(vspInfoDaoMock).get(any(VspDetails.class));
93 UploadFileResponse response = testCsarUpload("invalidFileInRoot.csar", 1);
94 assertTrue(response.getErrors().values().stream().anyMatch(
95 getListPredicate(Messages.CSAR_FILES_NOT_ALLOWED.getErrorMessage().substring(0, 7))));
99 public void testIllegalUploadMissingMainServiceTemplate() throws Exception {
100 VspDetails vspDetails = new VspDetails(id001, activeVersion002);
101 doReturn(vspDetails).when(vspInfoDaoMock).get(any(VspDetails.class));
103 UploadFileResponse response = testCsarUpload("missingMainServiceTemplate.csar", 1);
104 assertTrue(response.getErrors().values().stream().anyMatch(
105 getListPredicate(Messages.CSAR_FILE_NOT_FOUND.getErrorMessage().substring(0, 7))));
109 public void testUploadFileIsNotZip() throws Exception {
110 VspDetails vspDetails = new VspDetails(id001, activeVersion002);
111 doReturn(vspDetails).when(vspInfoDaoMock).get(any(VspDetails.class));
113 UploadFileResponse response = testCsarUpload("notCsar.txt", 1);
114 assertEquals("no csar file was uploaded or file doesn't exist",
115 response.getErrors().values().iterator().next().get(0).getMessage());
119 public void testUploadFileIsEmpty() throws Exception {
120 VspDetails vspDetails = new VspDetails(id001, activeVersion002);
121 doReturn(vspDetails).when(vspInfoDaoMock).get(any(VspDetails.class));
123 try (InputStream is = new ByteArrayInputStream(new byte[]{})) {
124 UploadFileResponse uploadFileResponse = candidateManager.upload(id001,
125 activeVersion002, is, "csar", "file");
126 assertEquals(1, uploadFileResponse.getErrors().size());
131 public void testInvalidManifestContent() throws Exception {
132 VspDetails vspDetails = new VspDetails(id001, activeVersion002);
133 doReturn(vspDetails).when(vspInfoDaoMock).get(any(VspDetails.class));
136 try (InputStream is = getClass()
137 .getResourceAsStream(BASE_DIR + "/invalidManifestContent.csar")) {
138 UploadFileResponse response =
139 candidateManager.upload(id001, activeVersion002, is, "csar", "invalidManifestContent");
140 assertEquals(1, response.getErrors().size());
141 assertEquals(response.getErrors().values().iterator().next().get(0).getMessage(),
143 "contains invalid line : aaa: vCSCF");
148 private Predicate<List<ErrorMessage>> getListPredicate(String substring) {
149 return error -> isEquals(substring, error);
152 private boolean isEquals(String substring, List<ErrorMessage> error) {
153 return error.iterator().next().getMessage().contains(substring);
156 private UploadFileResponse testCsarUpload(String csarFileName, int expectedErrorsNumber)
158 UploadFileResponse uploadFileResponse;
159 try (InputStream is = getClass()
160 .getResourceAsStream(BASE_DIR + File.separator + csarFileName)) {
162 candidateManager.upload(id001, activeVersion002, is, CSAR, csarFileName);
163 assertEquals(expectedErrorsNumber, uploadFileResponse.getErrors().size());
165 return uploadFileResponse;