10f09260efcc9688d4a72c6f8e42139f89af44b2
[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 static org.mockito.Matchers.any;
25 import static org.mockito.Mockito.doReturn;
26 import static org.testng.Assert.assertEquals;
27
28 import java.io.ByteArrayInputStream;
29 import java.io.ByteArrayOutputStream;
30 import java.io.File;
31 import java.io.IOException;
32 import java.io.InputStream;
33 import java.net.URL;
34 import java.util.zip.ZipOutputStream;
35 import org.mockito.InjectMocks;
36 import org.mockito.Mock;
37 import org.mockito.MockitoAnnotations;
38 import org.mockito.Spy;
39 import org.openecomp.core.model.dao.ServiceModelDao;
40 import org.openecomp.core.model.types.ServiceElement;
41 import org.openecomp.core.utilities.orchestration.OnboardingTypesEnum;
42 import org.openecomp.sdc.healing.api.HealingManager;
43 import org.openecomp.sdc.tosca.datatypes.ToscaServiceModel;
44 import org.openecomp.sdc.vendorsoftwareproduct.CompositionEntityDataManager;
45 import org.openecomp.sdc.vendorsoftwareproduct.dao.OrchestrationTemplateDao;
46 import org.openecomp.sdc.vendorsoftwareproduct.dao.VendorSoftwareProductInfoDao;
47 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.OrchestrationTemplateEntity;
48 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.VspDetails;
49 import org.openecomp.sdc.vendorsoftwareproduct.impl.OrchestrationTemplateCandidateManagerImpl;
50 import org.openecomp.sdc.vendorsoftwareproduct.services.composition.CompositionDataExtractor;
51 import org.openecomp.sdc.vendorsoftwareproduct.services.impl.filedatastructuremodule.CandidateServiceImpl;
52 import org.openecomp.sdc.vendorsoftwareproduct.types.UploadFileResponse;
53 import org.openecomp.sdc.vendorsoftwareproduct.utils.VSPCommon;
54 import org.openecomp.sdc.versioning.dao.types.Version;
55 import org.testng.annotations.BeforeMethod;
56 import org.testng.annotations.Test;
57
58 public class UploadFileTest {
59   private static final String USER1 = "vspTestUser1";
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
78   @InjectMocks
79   private OrchestrationTemplateCandidateManagerImpl candidateManager;
80
81   private static String vlm1Id;
82   public static String id001 = null;
83   public static String id002 = null;
84
85   public static Version activeVersion002 = null;
86
87
88   @BeforeMethod
89   public void setUp() throws Exception {
90     MockitoAnnotations.initMocks(this);
91   }
92
93   @Test
94   public void testUploadFile() {
95     VspDetails vspDetails = new VspDetails("dummyId", new Version(1, 0));
96     doReturn(vspDetails).when(vspInfoDaoMock).get(any(VspDetails.class));
97     candidateManager.upload(id001, activeVersion002, getZipInputStream("/legalUpload"),
98         OnboardingTypesEnum.ZIP.toString(), "legalUpload");
99   }
100
101
102   private void testLegalUpload(String vspId, Version version, InputStream upload, String user) {
103     UploadFileResponse uploadFileResponse = candidateManager.upload(vspId, activeVersion002,
104         upload, OnboardingTypesEnum.ZIP.toString(), "file");
105     assertEquals(uploadFileResponse.getOnboardingType(), OnboardingTypesEnum.ZIP);
106     OrchestrationTemplateEntity uploadData = orchestrationTemplateDataDaoMock.get(vspId, version);
107
108   }
109
110   public InputStream getZipInputStream(String name) {
111     URL url = this.getClass().getResource(name);
112     File templateDir = new File(url.getFile());
113
114     ByteArrayOutputStream baos = new ByteArrayOutputStream();
115     try (ZipOutputStream zos = new ZipOutputStream(baos)) {
116       VSPCommon.zipDir(templateDir, "", zos, true);
117     } catch (IOException e) {
118       e.printStackTrace();
119     }
120     return new ByteArrayInputStream(baos.toByteArray());
121   }
122
123
124 }