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.VendorSoftwareProductInfoDao;
37 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.VspDetails;
38 import org.openecomp.sdc.vendorsoftwareproduct.impl.OrchestrationTemplateCandidateManagerImpl;
39 import org.openecomp.sdc.vendorsoftwareproduct.services.composition.CompositionDataExtractor;
40 import org.openecomp.sdc.vendorsoftwareproduct.services.composition.CompositionEntityDataManager;
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 import java.io.ByteArrayInputStream;
49 import java.io.InputStream;
50 import java.util.List;
51 import java.util.function.Predicate;
53 import static org.junit.Assert.assertEquals;
54 import static org.junit.Assert.assertFalse;
55 import static org.junit.Assert.assertTrue;
56 import static org.mockito.Matchers.any;
57 import static org.mockito.Mockito.doReturn;
59 public class UploadCSARFileTest {
61 public static final Version VERSION01 = new Version("0.1");
64 private OrchestrationTemplateDao orchestrationTemplateDataDaoMock;
66 private CandidateServiceImpl candidateService;
68 private HealingManager healingManagerMock;
70 private CompositionDataExtractor compositionDataExtractorMock;
72 private ServiceModelDao<ToscaServiceModel, ServiceElement> serviceModelDaoMock;
74 private CompositionEntityDataManager compositionEntityDataManagerMock;
76 private VendorSoftwareProductInfoDao vspInfoDaoMock;
78 private OrchestrationTemplateCandidateDao orchestrationTemplateCandidateDao;
80 private ManifestCreatorNamingConventionImpl manifestCreator;
82 private OrchestrationTemplateCandidateManagerImpl candidateManager;
85 public static String id001 = null;
87 public static Version activeVersion002 = null;
91 public void setUp() throws Exception {
92 MockitoAnnotations.initMocks(this);
93 candidateService = new CandidateServiceImpl(manifestCreator, orchestrationTemplateCandidateDao);
94 candidateManager = new OrchestrationTemplateCandidateManagerImpl(vspInfoDaoMock,
95 candidateService, healingManagerMock);
99 public void testSuccessfulUploadFile() throws Exception {
100 VspDetails vspDetails = new VspDetails("dummyId", new Version(1, 0));
101 doReturn(vspDetails).when(vspInfoDaoMock).get(any(VspDetails.class));
103 try (InputStream is = getClass().getResourceAsStream("/vspmanager.csar/SDCmock.csar")) {
104 UploadFileResponse uploadFileResponse =
105 candidateManager.upload(id001, activeVersion002, is, "csar", "SDCmock");
106 assertEquals(uploadFileResponse.getOnboardingType(), OnboardingTypesEnum.CSAR);
107 assertEquals(0, uploadFileResponse.getErrors().size());
108 assertTrue(uploadFileResponse.getErrors().isEmpty());
113 public void testFail1UploadFile() throws Exception {
114 VspDetails vspDetails = new VspDetails("dummyId", new Version(1, 0));
115 doReturn(vspDetails).when(vspInfoDaoMock).get(any(VspDetails.class));
117 try (InputStream is = getClass().getResourceAsStream("/vspmanager.csar/SDCmockFail1.csar")) {
118 UploadFileResponse uploadFileResponse =
119 candidateManager.upload(id001, activeVersion002, is,
120 "csar", "SDCmockFail1");
121 assertEquals(uploadFileResponse.getOnboardingType(), OnboardingTypesEnum.CSAR);
122 assertEquals(1, uploadFileResponse.getErrors().size());
123 assertTrue(uploadFileResponse.getErrors().values().stream().anyMatch(
124 getListPredicate(Messages.CSAR_FILES_NOT_ALLOWED.getErrorMessage().substring(0, 7))));
128 private Predicate<List<ErrorMessage>> getListPredicate(String substring) {
129 return error -> isEquals(substring, error);
132 private boolean isEquals(String substring, List<ErrorMessage> error) {
133 return error.iterator().next().getMessage().contains(substring);
137 public void testFail2UploadFile() throws Exception {
138 VspDetails vspDetails = new VspDetails("dummyId", new Version(1, 0));
139 doReturn(vspDetails).when(vspInfoDaoMock).get(any(VspDetails.class));
141 try (InputStream is = getClass().getResourceAsStream("/vspmanager.csar/SDCmockFail2.csar")) {
142 UploadFileResponse uploadFileResponse =
143 candidateManager.upload(id001, activeVersion002, is,
144 "csar", "SDCmockFail2");
145 assertEquals(uploadFileResponse.getOnboardingType(), OnboardingTypesEnum.CSAR);
146 assertEquals(1, uploadFileResponse.getErrors().size());
147 assertTrue(uploadFileResponse.getErrors().values().stream().anyMatch(
148 getListPredicate(Messages.CSAR_FILE_NOT_FOUND.getErrorMessage().substring(0, 7))));
153 public void testFail3UploadFile() throws Exception {
154 VspDetails vspDetails = new VspDetails("dummyId", new Version(1, 0));
155 doReturn(vspDetails).when(vspInfoDaoMock).get(any(VspDetails.class));
157 try (InputStream is = getClass().getResourceAsStream("/vspmanager.csar/SDCmockFail3.csar")) {
158 UploadFileResponse uploadFileResponse =
159 candidateManager.upload(id001, activeVersion002, is,
160 "csar", "SDCmockFail3");
161 assertEquals(uploadFileResponse.getOnboardingType(), OnboardingTypesEnum.CSAR);
162 assertEquals(1, uploadFileResponse.getErrors().size());
167 public void testUploadFileIsNotZip() throws Exception {
168 VspDetails vspDetails = new VspDetails("dummyId", new Version(1, 0));
169 doReturn(vspDetails).when(vspInfoDaoMock).get(any(VspDetails.class));
171 try (InputStream is = new ByteArrayInputStream("Thia is not a zip file".getBytes());) {
172 UploadFileResponse uploadFileResponse =
173 candidateManager.upload(id001, activeVersion002, is,
175 assertEquals(uploadFileResponse.getOnboardingType(), OnboardingTypesEnum.CSAR);
176 assertFalse(uploadFileResponse.getErrors().isEmpty());
177 assertTrue(uploadFileResponse.getErrors().values().stream().anyMatch(
178 getListPredicate(Messages.CSAR_FILE_NOT_FOUND.getErrorMessage().substring(0, 7))));
183 public void testUploadFileIsEmpty() throws Exception {
184 VspDetails vspDetails = new VspDetails("dummyId", new Version(1, 0));
185 doReturn(vspDetails).when(vspInfoDaoMock).get(any(VspDetails.class));
187 try (InputStream is = new ByteArrayInputStream(new byte[]{})) {
188 UploadFileResponse uploadFileResponse = candidateManager.upload(id001,
189 activeVersion002, is, "csar", "file");
190 assertEquals(uploadFileResponse.getOnboardingType(), OnboardingTypesEnum.CSAR);
191 assertEquals(1, uploadFileResponse.getErrors().size());
196 public void testMFError() throws Exception {
197 VspDetails vspDetails = new VspDetails("dummyId", new Version(1, 0));
198 doReturn(vspDetails).when(vspInfoDaoMock).get(any(VspDetails.class));
200 try (InputStream is = getClass().getResourceAsStream("/vspmanager.csar/SDCmockBrokenMF.csar")) {
201 UploadFileResponse uploadFileResponse =
202 candidateManager.upload(id001, activeVersion002, is, "csar", "SDCmockBrokenMF");
203 assertEquals(uploadFileResponse.getOnboardingType(), OnboardingTypesEnum.CSAR);
204 assertEquals(1, uploadFileResponse.getErrors().size());
205 assertTrue(uploadFileResponse.getErrors().values().stream()
206 .anyMatch(getListPredicate(Messages.MANIFEST_NO_METADATA.getErrorMessage())));