Create new VSP, onboard from TOSCA file - UI
[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.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;
48
49 import java.io.ByteArrayInputStream;
50 import java.io.InputStream;
51 import java.util.List;
52 import java.util.function.Predicate;
53
54 import static org.junit.Assert.*;
55 import static org.mockito.Matchers.any;
56 import static org.mockito.Mockito.doReturn;
57
58 public class UploadCSARFileTest {
59     private static final String USER1 = "vspTestUser1";
60
61     public static final Version VERSION01 = new Version(0, 1);
62
63     @Mock
64     private VendorSoftwareProductDao vendorSoftwareProductDaoMock;
65     @Mock
66     private OrchestrationTemplateDao orchestrationTemplateDataDaoMock;
67
68     @Spy
69     private CandidateServiceImpl candidateService;
70     @Mock
71     private HealingManager healingManagerMock;
72     @Mock
73     private CompositionDataExtractor compositionDataExtractorMock;
74     @Mock
75     private ServiceModelDao<ToscaServiceModel, ServiceElement> serviceModelDaoMock;
76     @Mock
77     private CompositionEntityDataManager compositionEntityDataManagerMock;
78     @Mock
79     private VendorSoftwareProductInfoDao vspInfoDaoMock;
80     @Mock
81     private OrchestrationTemplateCandidateDao orchestrationTemplateCandidateDao;
82     @Mock
83     private ManifestCreatorNamingConventionImpl manifestCreator;
84
85     private OrchestrationTemplateCandidateManagerImpl candidateManager;
86
87
88     public static String id001 = null;
89
90     public static Version activeVersion002 = null;
91
92
93     @BeforeMethod
94     public void setUp() throws Exception {
95         MockitoAnnotations.initMocks(this);
96         candidateService = new CandidateServiceImpl(manifestCreator,orchestrationTemplateCandidateDao);
97         candidateManager = new OrchestrationTemplateCandidateManagerImpl( vendorSoftwareProductDaoMock,
98                 vspInfoDaoMock,
99                 orchestrationTemplateDataDaoMock,
100                 candidateService,  healingManagerMock,
101                 compositionDataExtractorMock,
102                 serviceModelDaoMock,
103                 compositionEntityDataManagerMock,
104                 null,
105                 null,
106                 null,
107                 null,
108                 null);
109     }
110
111     @Test
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));
115
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());
121         }
122     }
123
124     @Test
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));
128
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());
137         }
138     }
139
140     private Predicate<List<ErrorMessage>> getListPredicate(String substring) {
141         return error -> isEquals(substring, error);
142     }
143
144     private boolean isEquals(String substring, List<ErrorMessage> error) {
145         return error.iterator().next().getMessage().contains(substring);
146     }
147
148     @Test
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));
152
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());
161         }
162     }
163     @Test
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));
167
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());
173         }
174     }
175
176     @Test
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));
180
181         try (InputStream is = new ByteArrayInputStream( "Thia is not a zip file".getBytes() );) {
182             UploadFileResponse uploadFileResponse = candidateManager.upload(id001, activeVersion002, is, USER1,
183                     "csar", "file");
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());
189         }
190     }
191     @Test
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));
195
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());
201         }
202     }
203
204     @Test
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));
208
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()
214                     .values().stream()
215                     .filter(getListPredicate(Messages.MANIFEST_NO_METADATA.getErrorMessage())).findAny().isPresent());
216
217         }
218     }
219
220
221 }