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 org.mockito.InjectMocks;
21 import org.mockito.Mock;
22 import org.mockito.MockitoAnnotations;
23 import org.mockito.Spy;
24 import org.openecomp.core.utilities.orchestration.OnboardingTypesEnum;
25 import org.openecomp.sdc.common.errors.Messages;
26 import org.openecomp.sdc.datatypes.error.ErrorMessage;
27 import org.openecomp.sdc.vendorsoftwareproduct.dao.OrchestrationTemplateCandidateDao;
28 import org.openecomp.sdc.vendorsoftwareproduct.dao.VendorSoftwareProductInfoDao;
29 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.VspDetails;
30 import org.openecomp.sdc.vendorsoftwareproduct.impl.OrchestrationTemplateCandidateManagerImpl;
31 import org.openecomp.sdc.vendorsoftwareproduct.services.impl.filedatastructuremodule.CandidateServiceImpl;
32 import org.openecomp.sdc.vendorsoftwareproduct.services.impl.filedatastructuremodule.ManifestCreatorNamingConventionImpl;
33 import org.openecomp.sdc.vendorsoftwareproduct.types.UploadFileResponse;
34 import org.openecomp.sdc.versioning.dao.types.Version;
35 import org.testng.annotations.BeforeMethod;
36 import org.testng.annotations.Test;
38 import java.io.ByteArrayInputStream;
40 import java.io.IOException;
41 import java.io.InputStream;
42 import java.util.List;
43 import java.util.function.Predicate;
45 import static org.junit.Assert.assertEquals;
46 import static org.junit.Assert.assertFalse;
47 import static org.junit.Assert.assertTrue;
48 import static org.mockito.Matchers.any;
49 import static org.mockito.Mockito.doReturn;
51 public class UploadCSARFileTest {
53 public static final Version VERSION01 = new Version("0.1");
56 private CandidateServiceImpl candidateService;
58 private VendorSoftwareProductInfoDao vspInfoDaoMock;
60 private OrchestrationTemplateCandidateDao orchestrationTemplateCandidateDao;
62 private ManifestCreatorNamingConventionImpl manifestCreator;
65 private OrchestrationTemplateCandidateManagerImpl candidateManager;
68 private static String id001 = "dummyId";
69 private static Version activeVersion002 = new Version("dummyVersion");
70 private static final String BASE_DIR = "/vspmanager.csar";
71 private static final String CSAR = "csar";
75 public void setUp() throws Exception {
76 MockitoAnnotations.initMocks(this);
77 candidateService = new CandidateServiceImpl(manifestCreator, orchestrationTemplateCandidateDao);
78 candidateManager = new OrchestrationTemplateCandidateManagerImpl(vspInfoDaoMock,
83 public void testSuccessfulUploadFile() throws Exception {
84 VspDetails vspDetails = new VspDetails(id001, activeVersion002);
85 doReturn(vspDetails).when(vspInfoDaoMock).get(any(VspDetails.class));
87 testCsarUpload("successfulUpload.csar", 0);
91 public void testIllegalUploadInvalidFileInRoot() throws Exception {
92 VspDetails vspDetails = new VspDetails(id001, activeVersion002);
93 doReturn(vspDetails).when(vspInfoDaoMock).get(any(VspDetails.class));
95 UploadFileResponse response = testCsarUpload("invalidFileInRoot.csar", 1);
96 assertTrue(response.getErrors().values().stream().anyMatch(
97 getListPredicate(Messages.CSAR_FILES_NOT_ALLOWED.getErrorMessage().substring(0, 7))));
101 public void testIllegalUploadMissingMainServiceTemplate() throws Exception {
102 VspDetails vspDetails = new VspDetails(id001, activeVersion002);
103 doReturn(vspDetails).when(vspInfoDaoMock).get(any(VspDetails.class));
105 UploadFileResponse response = testCsarUpload("missingMainServiceTemplate.csar", 1);
106 assertTrue(response.getErrors().values().stream().anyMatch(
107 getListPredicate(Messages.CSAR_FILE_NOT_FOUND.getErrorMessage().substring(0, 7))));
111 public void testUploadFileIsNotZip() throws Exception {
112 VspDetails vspDetails = new VspDetails(id001, activeVersion002);
113 doReturn(vspDetails).when(vspInfoDaoMock).get(any(VspDetails.class));
115 UploadFileResponse response = testCsarUpload("notCsar.txt", 1);
116 assertEquals("no csar file was uploaded or file doesn't exist",
117 response.getErrors().values().iterator().next().get(0).getMessage());
121 public void testUploadFileIsEmpty() throws Exception {
122 VspDetails vspDetails = new VspDetails(id001, activeVersion002);
123 doReturn(vspDetails).when(vspInfoDaoMock).get(any(VspDetails.class));
125 try (InputStream is = new ByteArrayInputStream(new byte[]{})) {
126 UploadFileResponse uploadFileResponse = candidateManager.upload(id001,
127 activeVersion002, is, "csar", "file");
128 assertEquals(1, uploadFileResponse.getErrors().size());
133 public void testInvalidManifestContent() throws Exception {
134 VspDetails vspDetails = new VspDetails(id001, activeVersion002);
135 doReturn(vspDetails).when(vspInfoDaoMock).get(any(VspDetails.class));
138 try (InputStream is = getClass()
139 .getResourceAsStream(BASE_DIR + "/invalidManifestContent.csar")) {
140 UploadFileResponse response =
141 candidateManager.upload(id001, activeVersion002, is, "csar", "invalidManifestContent");
142 assertEquals(1, response.getErrors().size());
143 assertEquals(response.getErrors().values().iterator().next().get(0).getMessage(),
145 "contains invalid line : aaa: vCSCF");
150 private Predicate<List<ErrorMessage>> getListPredicate(String substring) {
151 return error -> isEquals(substring, error);
154 private boolean isEquals(String substring, List<ErrorMessage> error) {
155 return error.iterator().next().getMessage().contains(substring);
158 private UploadFileResponse testCsarUpload(String csarFileName, int expectedErrorsNumber)
160 UploadFileResponse uploadFileResponse;
161 try (InputStream is = getClass()
162 .getResourceAsStream(BASE_DIR + File.separator + csarFileName)) {
164 candidateManager.upload(id001, activeVersion002, is, CSAR, csarFileName);
165 assertEquals(expectedErrorsNumber, uploadFileResponse.getErrors().size());
167 return uploadFileResponse;