2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 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.sdc.vendorsoftwareproduct.upload.csar;
24 import org.mockito.Mock;
25 import org.mockito.MockitoAnnotations;
26 import org.mockito.Spy;
27 import org.openecomp.core.model.dao.ServiceModelDao;
28 import org.openecomp.core.model.types.ServiceElement;
29 import org.openecomp.core.utilities.orchestration.OnboardingTypesEnum;
30 import org.openecomp.sdc.common.errors.Messages;
31 import org.openecomp.sdc.datatypes.error.ErrorMessage;
32 import org.openecomp.sdc.healing.api.HealingManager;
33 import org.openecomp.sdc.tosca.datatypes.ToscaServiceModel;
34 import org.openecomp.sdc.vendorsoftwareproduct.dao.OrchestrationTemplateCandidateDao;
35 import org.openecomp.sdc.vendorsoftwareproduct.dao.OrchestrationTemplateDao;
36 import org.openecomp.sdc.vendorsoftwareproduct.dao.VendorSoftwareProductDao;
37 import org.openecomp.sdc.vendorsoftwareproduct.dao.VendorSoftwareProductInfoDao;
38 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.VspDetails;
39 import org.openecomp.sdc.vendorsoftwareproduct.impl.OrchestrationTemplateCandidateManagerImpl;
40 import org.openecomp.sdc.vendorsoftwareproduct.services.composition.CompositionDataExtractor;
41 import org.openecomp.sdc.vendorsoftwareproduct.services.composition.CompositionEntityDataManager;
42 import org.openecomp.sdc.vendorsoftwareproduct.services.impl.filedatastructuremodule.CandidateServiceImpl;
43 import org.openecomp.sdc.vendorsoftwareproduct.services.impl.filedatastructuremodule.ManifestCreatorNamingConventionImpl;
44 import org.openecomp.sdc.vendorsoftwareproduct.types.UploadFileResponse;
45 import org.openecomp.sdc.versioning.dao.types.Version;
46 import org.testng.annotations.BeforeMethod;
47 import org.testng.annotations.Test;
49 import java.io.ByteArrayInputStream;
50 import java.io.InputStream;
51 import java.util.List;
52 import java.util.function.Predicate;
54 import static org.junit.Assert.*;
55 import static org.mockito.Matchers.any;
56 import static org.mockito.Mockito.doReturn;
58 public class UploadCSARFileTest {
59 private static final String USER1 = "vspTestUser1";
61 public static final Version VERSION01 = new Version(0, 1);
64 private VendorSoftwareProductDao vendorSoftwareProductDaoMock;
66 private OrchestrationTemplateDao orchestrationTemplateDataDaoMock;
69 private CandidateServiceImpl candidateService;
71 private HealingManager healingManagerMock;
73 private CompositionDataExtractor compositionDataExtractorMock;
75 private ServiceModelDao<ToscaServiceModel, ServiceElement> serviceModelDaoMock;
77 private CompositionEntityDataManager compositionEntityDataManagerMock;
79 private VendorSoftwareProductInfoDao vspInfoDaoMock;
81 private OrchestrationTemplateCandidateDao orchestrationTemplateCandidateDao;
83 private ManifestCreatorNamingConventionImpl manifestCreator;
85 private OrchestrationTemplateCandidateManagerImpl candidateManager;
88 public static String id001 = null;
90 public static Version activeVersion002 = null;
94 public void setUp() throws Exception {
95 MockitoAnnotations.initMocks(this);
96 candidateService = new CandidateServiceImpl(manifestCreator,orchestrationTemplateCandidateDao);
97 candidateManager = new OrchestrationTemplateCandidateManagerImpl( vendorSoftwareProductDaoMock,
99 orchestrationTemplateDataDaoMock,
100 candidateService, healingManagerMock,
101 compositionDataExtractorMock,
103 compositionEntityDataManagerMock,
112 public void testSuccessfulUploadFile() throws Exception {
113 VspDetails vspDetails = new VspDetails("dummyId", new Version(1, 0));
114 doReturn(vspDetails).when(vspInfoDaoMock).get(any(VspDetails.class));
116 try (InputStream is = getClass().getResourceAsStream("/vspmanager.csar/SDCmock.csar")) {
117 UploadFileResponse uploadFileResponse = candidateManager.upload(id001, activeVersion002, is, USER1, "csar", "SDCmock");
118 assertEquals(uploadFileResponse.getOnboardingType(), OnboardingTypesEnum.CSAR);
119 assertEquals(0, uploadFileResponse.getErrors().size());
120 assertTrue(uploadFileResponse.getErrors().isEmpty());
125 public void testFail1UploadFile() throws Exception {
126 VspDetails vspDetails = new VspDetails("dummyId", new Version(1, 0));
127 doReturn(vspDetails).when(vspInfoDaoMock).get(any(VspDetails.class));
129 try (InputStream is = getClass().getResourceAsStream("/vspmanager.csar/SDCmockFail1.csar")) {
130 UploadFileResponse uploadFileResponse = candidateManager.upload(id001, activeVersion002, is, USER1,
131 "csar", "SDCmockFail1");
132 assertEquals(uploadFileResponse.getOnboardingType(), OnboardingTypesEnum.CSAR);
133 assertEquals(1, uploadFileResponse.getErrors().size());
134 assertTrue( uploadFileResponse.getErrors().values().stream()
135 .filter(getListPredicate(Messages.CSAR_FILES_NOT_ALLOWED
136 .getErrorMessage().substring(0, 7))).findAny().isPresent());
140 private Predicate<List<ErrorMessage>> getListPredicate(String substring) {
141 return error -> isEquals(substring, error);
144 private boolean isEquals(String substring, List<ErrorMessage> error) {
145 return error.iterator().next().getMessage().contains(substring);
149 public void testFail2UploadFile() throws Exception {
150 VspDetails vspDetails = new VspDetails("dummyId", new Version(1, 0));
151 doReturn(vspDetails).when(vspInfoDaoMock).get(any(VspDetails.class));
153 try (InputStream is = getClass().getResourceAsStream("/vspmanager.csar/SDCmockFail2.csar")) {
154 UploadFileResponse uploadFileResponse = candidateManager.upload(id001, activeVersion002, is, USER1,
155 "csar", "SDCmockFail2");
156 assertEquals(uploadFileResponse.getOnboardingType(), OnboardingTypesEnum.CSAR);
157 assertEquals(1, uploadFileResponse.getErrors().size());
158 assertTrue( uploadFileResponse.getErrors().values().stream()
159 .filter(getListPredicate(Messages.CSAR_FILE_NOT_FOUND
160 .getErrorMessage().substring(0,7))).findAny().isPresent());
164 public void testFail3UploadFile() throws Exception {
165 VspDetails vspDetails = new VspDetails("dummyId", new Version(1, 0));
166 doReturn(vspDetails).when(vspInfoDaoMock).get(any(VspDetails.class));
168 try (InputStream is = getClass().getResourceAsStream("/vspmanager.csar/SDCmockFail3.csar")) {
169 UploadFileResponse uploadFileResponse = candidateManager.upload(id001, activeVersion002, is, USER1,
170 "csar", "SDCmockFail3");
171 assertEquals(uploadFileResponse.getOnboardingType(), OnboardingTypesEnum.CSAR);
172 assertEquals(1, uploadFileResponse.getErrors().size());
177 public void testUploadFileIsNotZip() throws Exception {
178 VspDetails vspDetails = new VspDetails("dummyId", new Version(1, 0));
179 doReturn(vspDetails).when(vspInfoDaoMock).get(any(VspDetails.class));
181 try (InputStream is = new ByteArrayInputStream( "Thia is not a zip file".getBytes() );) {
182 UploadFileResponse uploadFileResponse = candidateManager.upload(id001, activeVersion002, is, USER1,
184 assertEquals(uploadFileResponse.getOnboardingType(), OnboardingTypesEnum.CSAR);
185 assertFalse(uploadFileResponse.getErrors().isEmpty());
186 assertTrue( uploadFileResponse.getErrors().values().stream()
187 .filter(getListPredicate(Messages.CSAR_FILE_NOT_FOUND
188 .getErrorMessage().substring(0,7))).findAny().isPresent());
192 public void testUploadFileIsEmpty() throws Exception {
193 VspDetails vspDetails = new VspDetails("dummyId", new Version(1, 0));
194 doReturn(vspDetails).when(vspInfoDaoMock).get(any(VspDetails.class));
196 try (InputStream is = new ByteArrayInputStream( new byte[]{} )) {
197 UploadFileResponse uploadFileResponse = candidateManager.upload(id001,
198 activeVersion002, is, USER1, "csar", "file");
199 assertEquals(uploadFileResponse.getOnboardingType(), OnboardingTypesEnum.CSAR);
200 assertEquals(1, uploadFileResponse.getErrors().size());
205 public void testMFError() throws Exception {
206 VspDetails vspDetails = new VspDetails("dummyId", new Version(1, 0));
207 doReturn(vspDetails).when(vspInfoDaoMock).get(any(VspDetails.class));
209 try (InputStream is = getClass().getResourceAsStream("/vspmanager.csar/SDCmockBrokenMF.csar")) {
210 UploadFileResponse uploadFileResponse = candidateManager.upload(id001, activeVersion002, is, USER1, "csar", "SDCmockBrokenMF");
211 assertEquals(uploadFileResponse.getOnboardingType(), OnboardingTypesEnum.CSAR);
212 assertEquals(1, uploadFileResponse.getErrors().size());
213 assertTrue( uploadFileResponse.getErrors()
215 .filter(getListPredicate(Messages.MANIFEST_NO_METADATA.getErrorMessage())).findAny().isPresent());