Add collaboration feature
[sdc.git] / openecomp-be / backend / openecomp-sdc-vendor-software-product-manager / src / test / java / org / openecomp / sdc / vendorsoftwareproduct / upload / csar / UploadCSARFileTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.openecomp.sdc.vendorsoftwareproduct.upload.csar;
22
23
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;
47
48 import java.io.ByteArrayInputStream;
49 import java.io.InputStream;
50 import java.util.List;
51 import java.util.function.Predicate;
52
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;
58
59 public class UploadCSARFileTest {
60
61   public static final Version VERSION01 = new Version("0.1");
62
63   @Mock
64   private OrchestrationTemplateDao orchestrationTemplateDataDaoMock;
65   @Spy
66   private CandidateServiceImpl candidateService;
67   @Mock
68   private HealingManager healingManagerMock;
69   @Mock
70   private CompositionDataExtractor compositionDataExtractorMock;
71   @Mock
72   private ServiceModelDao<ToscaServiceModel, ServiceElement> serviceModelDaoMock;
73   @Mock
74   private CompositionEntityDataManager compositionEntityDataManagerMock;
75   @Mock
76   private VendorSoftwareProductInfoDao vspInfoDaoMock;
77   @Mock
78   private OrchestrationTemplateCandidateDao orchestrationTemplateCandidateDao;
79   @Mock
80   private ManifestCreatorNamingConventionImpl manifestCreator;
81
82   private OrchestrationTemplateCandidateManagerImpl candidateManager;
83
84
85   public static String id001 = null;
86
87   public static Version activeVersion002 = null;
88
89
90   @BeforeMethod
91   public void setUp() throws Exception {
92     MockitoAnnotations.initMocks(this);
93     candidateService = new CandidateServiceImpl(manifestCreator, orchestrationTemplateCandidateDao);
94     candidateManager = new OrchestrationTemplateCandidateManagerImpl(vspInfoDaoMock,
95         candidateService, healingManagerMock);
96   }
97
98   @Test
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));
102
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());
109     }
110   }
111
112   @Test
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));
116
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))));
125     }
126   }
127
128   private Predicate<List<ErrorMessage>> getListPredicate(String substring) {
129     return error -> isEquals(substring, error);
130   }
131
132   private boolean isEquals(String substring, List<ErrorMessage> error) {
133     return error.iterator().next().getMessage().contains(substring);
134   }
135
136   @Test
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));
140
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))));
149     }
150   }
151
152   @Test
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));
156
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());
163     }
164   }
165
166   @Test
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));
170
171     try (InputStream is = new ByteArrayInputStream("Thia is not a zip file".getBytes());) {
172       UploadFileResponse uploadFileResponse =
173           candidateManager.upload(id001, activeVersion002, is,
174               "csar", "file");
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))));
179     }
180   }
181
182   @Test
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));
186
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());
192     }
193   }
194
195   @Test
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));
199
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())));
207
208     }
209   }
210
211
212 }