Onboard Package Handling
[sdc.git] / openecomp-be / backend / openecomp-sdc-vendor-software-product-manager / src / test / java / org / openecomp / sdc / vendorsoftwareproduct / upload / csar / UploadCSARFileTest.java
1 /*
2  * Copyright © 2016-2017 European Support Limited
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package org.openecomp.sdc.vendorsoftwareproduct.upload.csar;
18
19
20 import static junit.framework.TestCase.assertTrue;
21 import static org.junit.Assert.assertEquals;
22 import static org.mockito.ArgumentMatchers.any;
23 import static org.mockito.Mockito.doReturn;
24
25 import java.io.File;
26 import java.io.IOException;
27 import java.io.InputStream;
28 import java.nio.ByteBuffer;
29 import java.util.List;
30 import java.util.function.Predicate;
31 import org.apache.commons.io.IOUtils;
32 import org.junit.Before;
33 import org.junit.Test;
34 import org.mockito.InjectMocks;
35 import org.mockito.Mock;
36 import org.mockito.MockitoAnnotations;
37 import org.mockito.Spy;
38 import org.openecomp.core.utilities.orchestration.OnboardingTypesEnum;
39 import org.openecomp.sdc.common.errors.Messages;
40 import org.openecomp.sdc.datatypes.error.ErrorMessage;
41 import org.openecomp.sdc.logging.api.Logger;
42 import org.openecomp.sdc.logging.api.LoggerFactory;
43 import org.openecomp.sdc.vendorsoftwareproduct.dao.OrchestrationTemplateCandidateDao;
44 import org.openecomp.sdc.vendorsoftwareproduct.dao.VendorSoftwareProductInfoDao;
45 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.VspDetails;
46 import org.openecomp.sdc.vendorsoftwareproduct.impl.OrchestrationTemplateCandidateManagerImpl;
47 import org.openecomp.sdc.vendorsoftwareproduct.services.impl.filedatastructuremodule.CandidateServiceImpl;
48 import org.openecomp.sdc.vendorsoftwareproduct.services.impl.filedatastructuremodule.ManifestCreatorNamingConventionImpl;
49 import org.openecomp.sdc.vendorsoftwareproduct.types.OnboardPackageInfo;
50 import org.openecomp.sdc.vendorsoftwareproduct.types.UploadFileResponse;
51 import org.openecomp.sdc.versioning.dao.types.Version;
52
53 public class UploadCSARFileTest {
54
55   private static final Logger LOGGER = LoggerFactory.getLogger(UploadCSARFileTest.class);
56
57   @Spy
58   private CandidateServiceImpl candidateService;
59   @Mock
60   private VendorSoftwareProductInfoDao vspInfoDaoMock;
61   @Mock
62   private OrchestrationTemplateCandidateDao orchestrationTemplateCandidateDao;
63   @Mock
64   private ManifestCreatorNamingConventionImpl manifestCreator;
65
66   @InjectMocks
67   private OrchestrationTemplateCandidateManagerImpl candidateManager;
68
69   private OnboardPackageInfo onboardPackageInfo;
70   private final VspDetails vspDetails = new VspDetails(id001, activeVersion002);
71
72   private static String id001 = "dummyId";
73   private static Version activeVersion002 = new Version("dummyVersion");
74   private static final String BASE_DIR = "/vspmanager.csar";
75
76
77   @Before
78   public void setUp() throws Exception {
79     MockitoAnnotations.initMocks(this);
80     candidateService = new CandidateServiceImpl(manifestCreator, orchestrationTemplateCandidateDao);
81     candidateManager = new OrchestrationTemplateCandidateManagerImpl(vspInfoDaoMock,
82         candidateService);
83   }
84
85   @Test
86   public void testSuccessfulUploadFile() throws Exception {
87     doReturn(vspDetails).when(vspInfoDaoMock).get(any(VspDetails.class));
88
89     testCsarUpload("successfulUpload.csar", 0);
90   }
91
92   @Test
93   public void testIllegalUploadInvalidFileInRoot() throws Exception {
94     doReturn(vspDetails).when(vspInfoDaoMock).get(any(VspDetails.class));
95
96     UploadFileResponse response = testCsarUpload("invalidFileInRoot.csar", 1);
97     assertTrue(response.getErrors().values().stream().anyMatch(
98         getListPredicate(Messages.CSAR_FILES_NOT_ALLOWED.getErrorMessage().substring(0, 7))));
99   }
100
101   @Test
102   public void testIllegalUploadMissingMainServiceTemplate() throws Exception {
103     doReturn(vspDetails).when(vspInfoDaoMock).get(any(VspDetails.class));
104
105     UploadFileResponse response = testCsarUpload("missingMainServiceTemplate.csar", 1);
106     assertTrue(response.getErrors().values().stream().anyMatch(
107         getListPredicate(Messages.CSAR_FILE_NOT_FOUND.getErrorMessage().substring(0, 7))));
108   }
109
110   @Test
111   public void testUploadFileIsNotZip() throws Exception {
112     doReturn(vspDetails).when(vspInfoDaoMock).get(any(VspDetails.class));
113
114     UploadFileResponse response = testCsarUpload("notCsar.txt", 1);
115     assertEquals("no csar file was uploaded or file doesn't exist",
116         response.getErrors().values().iterator().next().get(0).getMessage());
117   }
118
119   @Test
120   public void testUploadFileIsEmpty() throws Exception {
121     doReturn(vspDetails).when(vspInfoDaoMock).get(any(VspDetails.class));
122     onboardPackageInfo = new OnboardPackageInfo("file", OnboardingTypesEnum.CSAR.toString(),
123             ByteBuffer.wrap(new byte[]{}));
124     UploadFileResponse uploadFileResponse = candidateManager.upload(vspDetails, onboardPackageInfo);
125     assertEquals(1, uploadFileResponse.getErrors().size());
126   }
127
128   @Test
129   public void testInvalidManifestContent() throws Exception {
130
131     doReturn(vspDetails).when(vspInfoDaoMock).get(any(VspDetails.class));
132
133     try (InputStream inputStream = getClass()
134         .getResourceAsStream(BASE_DIR + "/invalidManifestContent.csar")) {
135       onboardPackageInfo = new OnboardPackageInfo("invalidManifestContent",
136               OnboardingTypesEnum.CSAR.toString(), convertFileInputStream(inputStream));
137       UploadFileResponse response =
138           candidateManager.upload(vspDetails, onboardPackageInfo);
139       assertEquals(1, response.getErrors().size());
140       assertEquals(response.getErrors().values().iterator().next().get(0).getMessage(),
141           "Manifest " +
142               "contains invalid line : aaa: vCSCF");
143
144     }
145   }
146
147   private Predicate<List<ErrorMessage>> getListPredicate(String substring) {
148     return error -> isEquals(substring, error);
149   }
150
151   private boolean isEquals(String substring, List<ErrorMessage> error) {
152     return error.iterator().next().getMessage().contains(substring);
153   }
154
155   private UploadFileResponse testCsarUpload(final String csarFileName,
156                                             final int expectedErrorsNumber) throws IOException {
157     UploadFileResponse uploadFileResponse;
158     try (final InputStream inputStream = getClass()
159         .getResourceAsStream(BASE_DIR + File.separator + csarFileName)) {
160       onboardPackageInfo = new OnboardPackageInfo(csarFileName, OnboardingTypesEnum.CSAR.toString(),
161               convertFileInputStream(inputStream));
162       uploadFileResponse = candidateManager.upload(vspDetails, onboardPackageInfo);
163       assertEquals(expectedErrorsNumber, uploadFileResponse.getErrors().size());
164     }
165     return uploadFileResponse;
166   }
167
168   private ByteBuffer convertFileInputStream(final InputStream fileInputStream) {
169     byte[] fileContent = new byte[0];
170     try {
171       fileContent = IOUtils.toByteArray(fileInputStream);
172     } catch (final IOException e) {
173       LOGGER.error(String.format("Could not convert %s into byte[]", fileInputStream), e);
174     }
175     return ByteBuffer.wrap(fileContent);
176   }
177
178 }