Improve testing stability
[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.junit.Assert.assertEquals;
25 import static org.mockito.ArgumentMatchers.any;
26 import static org.mockito.Mockito.doReturn;
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.nio.ByteBuffer;
35 import java.util.zip.ZipOutputStream;
36 import org.apache.commons.io.IOUtils;
37 import org.junit.Before;
38 import org.junit.Test;
39 import org.mockito.InjectMocks;
40 import org.mockito.Mock;
41 import org.mockito.MockitoAnnotations;
42 import org.mockito.Spy;
43 import org.openecomp.core.model.dao.ServiceModelDao;
44 import org.openecomp.core.model.types.ServiceElement;
45 import org.openecomp.core.utilities.orchestration.OnboardingTypesEnum;
46 import org.openecomp.sdc.healing.api.HealingManager;
47 import org.openecomp.sdc.logging.api.Logger;
48 import org.openecomp.sdc.logging.api.LoggerFactory;
49 import org.openecomp.sdc.tosca.datatypes.ToscaServiceModel;
50 import org.openecomp.sdc.vendorsoftwareproduct.CompositionEntityDataManager;
51 import org.openecomp.sdc.vendorsoftwareproduct.dao.OrchestrationTemplateDao;
52 import org.openecomp.sdc.vendorsoftwareproduct.dao.VendorSoftwareProductInfoDao;
53 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.OrchestrationTemplateEntity;
54 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.VspDetails;
55 import org.openecomp.sdc.vendorsoftwareproduct.exception.OnboardPackageException;
56 import org.openecomp.sdc.vendorsoftwareproduct.impl.OrchestrationTemplateCandidateManagerImpl;
57 import org.openecomp.sdc.vendorsoftwareproduct.services.composition.CompositionDataExtractor;
58 import org.openecomp.sdc.vendorsoftwareproduct.services.impl.filedatastructuremodule.CandidateServiceImpl;
59 import org.openecomp.sdc.vendorsoftwareproduct.types.OnboardPackageInfo;
60 import org.openecomp.sdc.vendorsoftwareproduct.types.UploadFileResponse;
61 import org.openecomp.sdc.vendorsoftwareproduct.utils.VSPCommon;
62 import org.openecomp.sdc.versioning.dao.types.Version;
63
64 public class UploadFileTest {
65   private static final Logger LOGGER = LoggerFactory.getLogger(UploadFileTest.class);
66   public static final Version VERSION01 = new Version(0, 1);
67
68   @Mock
69   private OrchestrationTemplateDao orchestrationTemplateDataDaoMock;
70   @Spy
71   private CandidateServiceImpl candidateService;
72   @Mock
73   private HealingManager healingManagerMock;
74   @Mock
75   private CompositionDataExtractor compositionDataExtractorMock;
76   @Mock
77   private ServiceModelDao<ToscaServiceModel> serviceModelDaoMock;
78   @Mock
79   private CompositionEntityDataManager compositionEntityDataManagerMock;
80   @Mock
81   private VendorSoftwareProductInfoDao vspInfoDaoMock;
82
83   private OnboardPackageInfo onboardPackageInfo;
84
85   @InjectMocks
86   private OrchestrationTemplateCandidateManagerImpl candidateManager;
87
88   public static String id001 = "dummyId";
89   public static Version activeVersion002 = new Version(1, 0);
90
91   private final VspDetails vspDetails = new VspDetails(id001, activeVersion002);
92
93   @Before
94   public void setUp() throws Exception {
95     MockitoAnnotations.openMocks(this);
96   }
97
98   @Test
99   public void testUploadFile() throws IOException, OnboardPackageException {
100     doReturn(vspDetails).when(vspInfoDaoMock).get(any(VspDetails.class));
101     try (final InputStream inputStream = getZipInputStream("/legalUpload")) {
102       onboardPackageInfo = new OnboardPackageInfo("legalUpload", OnboardingTypesEnum.ZIP.toString(),
103               convertFileInputStream(inputStream), OnboardingTypesEnum.ZIP);
104       candidateManager.upload(vspDetails, onboardPackageInfo);
105
106     }
107   }
108
109   private void testLegalUpload(String vspId, Version version, InputStream upload, String user)
110       throws IOException, OnboardPackageException {
111     onboardPackageInfo = new OnboardPackageInfo("file", OnboardingTypesEnum.ZIP.toString(),
112             convertFileInputStream(upload), OnboardingTypesEnum.ZIP);
113     final UploadFileResponse uploadFileResponse = candidateManager.upload(vspDetails, onboardPackageInfo);
114     assertEquals(OnboardingTypesEnum.ZIP, uploadFileResponse.getOnboardingType());
115     OrchestrationTemplateEntity uploadData = orchestrationTemplateDataDaoMock.get(vspId, version);
116
117   }
118
119   public InputStream getZipInputStream(String name) {
120     URL url = this.getClass().getResource(name);
121     File templateDir = new File(url.getFile());
122
123     ByteArrayOutputStream baos = new ByteArrayOutputStream();
124     try (ZipOutputStream zos = new ZipOutputStream(baos)) {
125       VSPCommon.zipDir(templateDir, "", zos, true);
126     } catch (IOException e) {
127       e.printStackTrace();
128     }
129     return new ByteArrayInputStream(baos.toByteArray());
130   }
131
132   private ByteBuffer convertFileInputStream(final InputStream fileInputStream) {
133     byte[] fileContent = new byte[0];
134     try {
135       fileContent = IOUtils.toByteArray(fileInputStream);
136     } catch (final IOException e) {
137       LOGGER.error(String.format("Could not convert %s into byte[]", fileInputStream), e);
138     }
139     return ByteBuffer.wrap(fileContent);
140   }
141
142 }