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 / tree / UploadFileTest.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.tree;
22
23
24 import org.mockito.InjectMocks;
25 import org.mockito.Mock;
26 import org.mockito.MockitoAnnotations;
27 import org.mockito.Spy;
28 import org.openecomp.core.model.dao.ServiceModelDao;
29 import org.openecomp.core.model.types.ServiceElement;
30 import org.openecomp.core.utilities.orchestration.OnboardingTypesEnum;
31 import org.openecomp.sdc.healing.api.HealingManager;
32 import org.openecomp.sdc.tosca.datatypes.ToscaServiceModel;
33 import org.openecomp.sdc.vendorsoftwareproduct.dao.OrchestrationTemplateDao;
34 import org.openecomp.sdc.vendorsoftwareproduct.dao.VendorSoftwareProductDao;
35 import org.openecomp.sdc.vendorsoftwareproduct.dao.VendorSoftwareProductInfoDao;
36 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.UploadDataEntity;
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.types.UploadFileResponse;
43 import org.openecomp.sdc.vendorsoftwareproduct.utils.VSPCommon;
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.*;
49 import java.net.URL;
50 import java.util.zip.ZipOutputStream;
51
52 import static org.mockito.Matchers.any;
53 import static org.mockito.Mockito.doReturn;
54 import static org.testng.Assert.assertEquals;
55
56 public class UploadFileTest {
57     private static final String USER1 = "vspTestUser1";
58
59     public static final Version VERSION01 = new Version(0, 1);
60     @Mock
61     private VendorSoftwareProductDao vendorSoftwareProductDaoMock;
62     @Mock
63     private OrchestrationTemplateDao orchestrationTemplateDataDaoMock;
64     @Spy
65     private CandidateServiceImpl candidateService;
66     @Mock
67     private HealingManager healingManagerMock;
68     @Mock
69     private CompositionDataExtractor compositionDataExtractorMock;
70     @Mock
71     private ServiceModelDao<ToscaServiceModel, ServiceElement> serviceModelDaoMock;
72     @Mock
73     private CompositionEntityDataManager compositionEntityDataManagerMock;
74     @Mock
75     private VendorSoftwareProductInfoDao vspInfoDaoMock;
76
77     @InjectMocks
78     private OrchestrationTemplateCandidateManagerImpl candidateManager;
79
80     private static String vlm1Id;
81     public static String id001 = null;
82     public static String id002 = null;
83
84     public static Version activeVersion002 = null;
85
86
87     @BeforeMethod
88     public void setUp() throws Exception {
89         MockitoAnnotations.initMocks(this);
90     }
91
92     @Test
93     public void testUploadFile() {
94         VspDetails vspDetails = new VspDetails("dummyId", new Version(1, 0));
95         doReturn(vspDetails).when(vspInfoDaoMock).get(any(VspDetails.class));
96         candidateManager.upload(id001, activeVersion002, getZipInputStream("/legalUpload"), USER1, OnboardingTypesEnum.ZIP.toString(), "legalUpload");
97     }
98
99
100     private void testLegalUpload(String vspId, Version version, InputStream upload, String user) {
101         UploadFileResponse uploadFileResponse = candidateManager.upload(vspId, activeVersion002,
102             upload, user, OnboardingTypesEnum.ZIP.toString(),"file" );
103         assertEquals(uploadFileResponse.getOnboardingType(), OnboardingTypesEnum.ZIP);
104         UploadDataEntity uploadData =
105                 orchestrationTemplateDataDaoMock.getOrchestrationTemplate(vspId, version);
106
107     }
108
109     public InputStream getZipInputStream(String name) {
110         URL url = this.getClass().getResource(name);
111         File templateDir = new File(url.getFile());
112
113         ByteArrayOutputStream baos = new ByteArrayOutputStream();
114         try (ZipOutputStream zos = new ZipOutputStream(baos)) {
115             VSPCommon.zipDir(templateDir, "", zos, true);
116         } catch (IOException e) {
117             e.printStackTrace();
118         }
119         return new ByteArrayInputStream(baos.toByteArray());
120     }
121
122
123 }