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 org.mockito.Matchers.any;
21 import static org.mockito.Mockito.doReturn;
22 import static org.testng.Assert.assertEquals;
23 import static org.testng.Assert.assertTrue;
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;
31 import org.mockito.InjectMocks;
32 import org.mockito.Mock;
33 import org.mockito.MockitoAnnotations;
34 import org.mockito.Spy;
35 import org.openecomp.sdc.common.errors.Messages;
36 import org.openecomp.sdc.datatypes.error.ErrorMessage;
37 import org.openecomp.sdc.vendorsoftwareproduct.dao.OrchestrationTemplateCandidateDao;
38 import org.openecomp.sdc.vendorsoftwareproduct.dao.VendorSoftwareProductInfoDao;
39 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.VspDetails;
40 import org.openecomp.sdc.vendorsoftwareproduct.impl.OrchestrationTemplateCandidateManagerImpl;
41 import org.openecomp.sdc.vendorsoftwareproduct.services.impl.filedatastructuremodule.CandidateServiceImpl;
42 import org.openecomp.sdc.vendorsoftwareproduct.services.impl.filedatastructuremodule.ManifestCreatorNamingConventionImpl;
43 import org.openecomp.sdc.vendorsoftwareproduct.types.UploadFileResponse;
44 import org.openecomp.sdc.versioning.dao.types.Version;
45 import org.testng.annotations.BeforeMethod;
46 import org.testng.annotations.Test;
48 public class UploadCSARFileTest {
50 public static final Version VERSION01 = new Version("0.1");
53 private CandidateServiceImpl candidateService;
55 private VendorSoftwareProductInfoDao vspInfoDaoMock;
57 private OrchestrationTemplateCandidateDao orchestrationTemplateCandidateDao;
59 private ManifestCreatorNamingConventionImpl manifestCreator;
62 private OrchestrationTemplateCandidateManagerImpl candidateManager;
65 private static String id001 = "dummyId";
66 private static Version activeVersion002 = new Version("dummyVersion");
67 private static final String BASE_DIR = "/vspmanager.csar";
68 private static final String CSAR = "csar";
72 public void setUp() throws Exception {
73 MockitoAnnotations.initMocks(this);
74 candidateService = new CandidateServiceImpl(manifestCreator, orchestrationTemplateCandidateDao);
75 candidateManager = new OrchestrationTemplateCandidateManagerImpl(vspInfoDaoMock,
80 public void testSuccessfulUploadFile() throws Exception {
81 VspDetails vspDetails = new VspDetails(id001, activeVersion002);
82 doReturn(vspDetails).when(vspInfoDaoMock).get(any(VspDetails.class));
84 testCsarUpload("successfulUpload.csar", 0);
88 public void testIllegalUploadInvalidFileInRoot() throws Exception {
89 VspDetails vspDetails = new VspDetails(id001, activeVersion002);
90 doReturn(vspDetails).when(vspInfoDaoMock).get(any(VspDetails.class));
92 UploadFileResponse response = testCsarUpload("invalidFileInRoot.csar", 1);
93 assertTrue(response.getErrors().values().stream().anyMatch(
94 getListPredicate(Messages.CSAR_FILES_NOT_ALLOWED.getErrorMessage().substring(0, 7))));
98 public void testIllegalUploadMissingMainServiceTemplate() throws Exception {
99 VspDetails vspDetails = new VspDetails(id001, activeVersion002);
100 doReturn(vspDetails).when(vspInfoDaoMock).get(any(VspDetails.class));
102 UploadFileResponse response = testCsarUpload("missingMainServiceTemplate.csar", 1);
103 assertTrue(response.getErrors().values().stream().anyMatch(
104 getListPredicate(Messages.CSAR_FILE_NOT_FOUND.getErrorMessage().substring(0, 7))));
108 public void testUploadFileIsNotZip() throws Exception {
109 VspDetails vspDetails = new VspDetails(id001, activeVersion002);
110 doReturn(vspDetails).when(vspInfoDaoMock).get(any(VspDetails.class));
112 UploadFileResponse response = testCsarUpload("notCsar.txt", 1);
113 assertEquals("no csar file was uploaded or file doesn't exist",
114 response.getErrors().values().iterator().next().get(0).getMessage());
118 public void testUploadFileIsEmpty() throws Exception {
119 VspDetails vspDetails = new VspDetails(id001, activeVersion002);
120 doReturn(vspDetails).when(vspInfoDaoMock).get(any(VspDetails.class));
122 try (InputStream is = new ByteArrayInputStream(new byte[]{})) {
123 UploadFileResponse uploadFileResponse = candidateManager.upload(id001,
124 activeVersion002, is, "csar", "file");
125 assertEquals(1, uploadFileResponse.getErrors().size());
130 public void testInvalidManifestContent() throws Exception {
131 VspDetails vspDetails = new VspDetails(id001, activeVersion002);
132 doReturn(vspDetails).when(vspInfoDaoMock).get(any(VspDetails.class));
135 try (InputStream is = getClass()
136 .getResourceAsStream(BASE_DIR + "/invalidManifestContent.csar")) {
137 UploadFileResponse response =
138 candidateManager.upload(id001, activeVersion002, is, "csar", "invalidManifestContent");
139 assertEquals(1, response.getErrors().size());
140 assertEquals(response.getErrors().values().iterator().next().get(0).getMessage(),
142 "contains invalid line : aaa: vCSCF");
147 private Predicate<List<ErrorMessage>> getListPredicate(String substring) {
148 return error -> isEquals(substring, error);
151 private boolean isEquals(String substring, List<ErrorMessage> error) {
152 return error.iterator().next().getMessage().contains(substring);
155 private UploadFileResponse testCsarUpload(String csarFileName, int expectedErrorsNumber)
157 UploadFileResponse uploadFileResponse;
158 try (InputStream is = getClass()
159 .getResourceAsStream(BASE_DIR + File.separator + csarFileName)) {
161 candidateManager.upload(id001, activeVersion002, is, CSAR, csarFileName);
162 assertEquals(expectedErrorsNumber, uploadFileResponse.getErrors().size());
164 return uploadFileResponse;