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.Mock;
21 import org.mockito.MockitoAnnotations;
22 import org.mockito.Spy;
23 import org.openecomp.core.utilities.orchestration.OnboardingTypesEnum;
24 import org.openecomp.sdc.common.errors.Messages;
25 import org.openecomp.sdc.datatypes.error.ErrorMessage;
26 import org.openecomp.sdc.vendorsoftwareproduct.dao.OrchestrationTemplateCandidateDao;
27 import org.openecomp.sdc.vendorsoftwareproduct.dao.VendorSoftwareProductInfoDao;
28 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.VspDetails;
29 import org.openecomp.sdc.vendorsoftwareproduct.impl.OrchestrationTemplateCandidateManagerImpl;
30 import org.openecomp.sdc.vendorsoftwareproduct.services.impl.filedatastructuremodule.CandidateServiceImpl;
31 import org.openecomp.sdc.vendorsoftwareproduct.services.impl.filedatastructuremodule.ManifestCreatorNamingConventionImpl;
32 import org.openecomp.sdc.vendorsoftwareproduct.types.UploadFileResponse;
33 import org.openecomp.sdc.versioning.dao.types.Version;
34 import org.testng.annotations.BeforeMethod;
35 import org.testng.annotations.Test;
37 import java.io.ByteArrayInputStream;
38 import java.io.InputStream;
39 import java.util.List;
40 import java.util.function.Predicate;
42 import static org.junit.Assert.assertEquals;
43 import static org.junit.Assert.assertFalse;
44 import static org.junit.Assert.assertTrue;
45 import static org.mockito.Matchers.any;
46 import static org.mockito.Mockito.doReturn;
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;
61 private OrchestrationTemplateCandidateManagerImpl candidateManager;
64 public static String id001 = null;
66 public static Version activeVersion002 = null;
70 public void setUp() throws Exception {
71 MockitoAnnotations.initMocks(this);
72 candidateService = new CandidateServiceImpl(manifestCreator, orchestrationTemplateCandidateDao);
73 candidateManager = new OrchestrationTemplateCandidateManagerImpl(vspInfoDaoMock,
78 public void testSuccessfulUploadFile() throws Exception {
79 VspDetails vspDetails = new VspDetails("dummyId", new Version(1, 0));
80 doReturn(vspDetails).when(vspInfoDaoMock).get(any(VspDetails.class));
82 try (InputStream is = getClass().getResourceAsStream("/vspmanager.csar/SDCmock.csar")) {
83 UploadFileResponse uploadFileResponse =
84 candidateManager.upload(id001, activeVersion002, is, "csar", "SDCmock");
85 assertEquals(uploadFileResponse.getOnboardingType(), OnboardingTypesEnum.CSAR);
86 assertEquals(0, uploadFileResponse.getErrors().size());
87 assertTrue(uploadFileResponse.getErrors().isEmpty());
92 public void testFail1UploadFile() throws Exception {
93 VspDetails vspDetails = new VspDetails("dummyId", new Version(1, 0));
94 doReturn(vspDetails).when(vspInfoDaoMock).get(any(VspDetails.class));
96 try (InputStream is = getClass().getResourceAsStream("/vspmanager.csar/SDCmockFail1.csar")) {
97 UploadFileResponse uploadFileResponse =
98 candidateManager.upload(id001, activeVersion002, is,
99 "csar", "SDCmockFail1");
100 assertEquals(uploadFileResponse.getOnboardingType(), OnboardingTypesEnum.CSAR);
101 assertEquals(1, uploadFileResponse.getErrors().size());
102 assertTrue(uploadFileResponse.getErrors().values().stream().anyMatch(
103 getListPredicate(Messages.CSAR_FILES_NOT_ALLOWED.getErrorMessage().substring(0, 7))));
107 private Predicate<List<ErrorMessage>> getListPredicate(String substring) {
108 return error -> isEquals(substring, error);
111 private boolean isEquals(String substring, List<ErrorMessage> error) {
112 return error.iterator().next().getMessage().contains(substring);
116 public void testFail2UploadFile() throws Exception {
117 VspDetails vspDetails = new VspDetails("dummyId", new Version(1, 0));
118 doReturn(vspDetails).when(vspInfoDaoMock).get(any(VspDetails.class));
120 try (InputStream is = getClass().getResourceAsStream("/vspmanager.csar/SDCmockFail2.csar")) {
121 UploadFileResponse uploadFileResponse =
122 candidateManager.upload(id001, activeVersion002, is,
123 "csar", "SDCmockFail2");
124 assertEquals(uploadFileResponse.getOnboardingType(), OnboardingTypesEnum.CSAR);
125 assertEquals(1, uploadFileResponse.getErrors().size());
126 assertTrue(uploadFileResponse.getErrors().values().stream().anyMatch(
127 getListPredicate(Messages.CSAR_FILE_NOT_FOUND.getErrorMessage().substring(0, 7))));
132 public void testFail3UploadFile() throws Exception {
133 VspDetails vspDetails = new VspDetails("dummyId", new Version(1, 0));
134 doReturn(vspDetails).when(vspInfoDaoMock).get(any(VspDetails.class));
136 try (InputStream is = getClass().getResourceAsStream("/vspmanager.csar/SDCmockFail3.csar")) {
137 UploadFileResponse uploadFileResponse =
138 candidateManager.upload(id001, activeVersion002, is,
139 "csar", "SDCmockFail3");
140 assertEquals(uploadFileResponse.getOnboardingType(), OnboardingTypesEnum.CSAR);
141 assertEquals(1, uploadFileResponse.getErrors().size());
146 public void testUploadFileIsNotZip() throws Exception {
147 VspDetails vspDetails = new VspDetails("dummyId", new Version(1, 0));
148 doReturn(vspDetails).when(vspInfoDaoMock).get(any(VspDetails.class));
150 try (InputStream is = new ByteArrayInputStream("Thia is not a zip file".getBytes());) {
151 UploadFileResponse uploadFileResponse =
152 candidateManager.upload(id001, activeVersion002, is,
154 assertEquals(uploadFileResponse.getOnboardingType(), OnboardingTypesEnum.CSAR);
155 assertFalse(uploadFileResponse.getErrors().isEmpty());
156 assertTrue(uploadFileResponse.getErrors().values().stream().anyMatch(
157 getListPredicate(Messages.CSAR_FILE_NOT_FOUND.getErrorMessage().substring(0, 7))));
162 public void testUploadFileIsEmpty() throws Exception {
163 VspDetails vspDetails = new VspDetails("dummyId", new Version(1, 0));
164 doReturn(vspDetails).when(vspInfoDaoMock).get(any(VspDetails.class));
166 try (InputStream is = new ByteArrayInputStream(new byte[]{})) {
167 UploadFileResponse uploadFileResponse = candidateManager.upload(id001,
168 activeVersion002, is, "csar", "file");
169 assertEquals(uploadFileResponse.getOnboardingType(), OnboardingTypesEnum.CSAR);
170 assertEquals(1, uploadFileResponse.getErrors().size());
175 public void testMFError() throws Exception {
176 VspDetails vspDetails = new VspDetails("dummyId", new Version(1, 0));
177 doReturn(vspDetails).when(vspInfoDaoMock).get(any(VspDetails.class));
179 try (InputStream is = getClass().getResourceAsStream("/vspmanager.csar/SDCmockBrokenMF.csar")) {
180 UploadFileResponse uploadFileResponse =
181 candidateManager.upload(id001, activeVersion002, is, "csar", "SDCmockBrokenMF");
182 assertEquals(uploadFileResponse.getOnboardingType(), OnboardingTypesEnum.CSAR);
183 assertEquals(1, uploadFileResponse.getErrors().size());
184 assertTrue(uploadFileResponse.getErrors().values().stream()
185 .anyMatch(getListPredicate(Messages.MANIFEST_NO_METADATA.getErrorMessage())));