7a183c0dbb7d323106237a73c99513df94b9ce1e
[sdc.git] /
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.hamcrest.Matchers.is;
22 import static org.junit.Assert.assertEquals;
23 import static org.junit.Assert.assertThat;
24 import static org.mockito.ArgumentMatchers.any;
25 import static org.mockito.Mockito.doReturn;
26
27 import java.io.File;
28 import java.io.IOException;
29 import java.io.InputStream;
30 import java.nio.ByteBuffer;
31 import java.util.List;
32 import java.util.function.Predicate;
33 import org.apache.commons.io.IOUtils;
34 import org.junit.Before;
35 import org.junit.Test;
36 import org.mockito.InjectMocks;
37 import org.mockito.Mock;
38 import org.mockito.MockitoAnnotations;
39 import org.mockito.Spy;
40 import org.openecomp.core.utilities.orchestration.OnboardingTypesEnum;
41 import org.openecomp.sdc.common.errors.Messages;
42 import org.openecomp.sdc.datatypes.error.ErrorMessage;
43 import org.openecomp.sdc.logging.api.Logger;
44 import org.openecomp.sdc.logging.api.LoggerFactory;
45 import org.openecomp.sdc.vendorsoftwareproduct.dao.OrchestrationTemplateCandidateDao;
46 import org.openecomp.sdc.vendorsoftwareproduct.dao.VendorSoftwareProductInfoDao;
47 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.VspDetails;
48 import org.openecomp.sdc.vendorsoftwareproduct.impl.OrchestrationTemplateCandidateManagerImpl;
49 import org.openecomp.sdc.vendorsoftwareproduct.services.impl.filedatastructuremodule.CandidateServiceImpl;
50 import org.openecomp.sdc.vendorsoftwareproduct.services.impl.filedatastructuremodule.ManifestCreatorNamingConventionImpl;
51 import org.openecomp.sdc.vendorsoftwareproduct.types.OnboardPackageInfo;
52 import org.openecomp.sdc.vendorsoftwareproduct.types.UploadFileResponse;
53 import org.openecomp.sdc.versioning.dao.types.Version;
54
55 public class UploadCSARFileTest {
56
57   private static final Logger LOGGER = LoggerFactory.getLogger(UploadCSARFileTest.class);
58
59   @Spy
60   private CandidateServiceImpl candidateService;
61   @Mock
62   private VendorSoftwareProductInfoDao vspInfoDaoMock;
63   @Mock
64   private OrchestrationTemplateCandidateDao orchestrationTemplateCandidateDao;
65   @Mock
66   private ManifestCreatorNamingConventionImpl manifestCreator;
67
68   @InjectMocks
69   private OrchestrationTemplateCandidateManagerImpl candidateManager;
70
71   private OnboardPackageInfo onboardPackageInfo;
72   private final VspDetails vspDetails = new VspDetails(id001, activeVersion002);
73
74   private static String id001 = "dummyId";
75   private static Version activeVersion002 = new Version("dummyVersion");
76   private static final String BASE_DIR = "/vspmanager.csar";
77
78
79   @Before
80   public void setUp() throws Exception {
81     MockitoAnnotations.initMocks(this);
82     candidateService = new CandidateServiceImpl(manifestCreator, orchestrationTemplateCandidateDao);
83     candidateManager = new OrchestrationTemplateCandidateManagerImpl(vspInfoDaoMock,
84         candidateService);
85   }
86
87   @Test
88   public void testSuccessfulUploadFile() throws Exception {
89     doReturn(vspDetails).when(vspInfoDaoMock).get(any(VspDetails.class));
90
91     testCsarUpload("successfulUpload.csar", 0);
92   }
93
94   @Test
95   public void testIllegalUploadInvalidFileInRoot() throws Exception {
96     doReturn(vspDetails).when(vspInfoDaoMock).get(any(VspDetails.class));
97
98     UploadFileResponse response = testCsarUpload("invalidFileInRoot.csar", 1);
99     assertTrue(response.getErrors().values().stream().anyMatch(
100         getListPredicate(Messages.CSAR_FILES_NOT_ALLOWED.getErrorMessage().substring(0, 7))));
101   }
102
103   @Test
104   public void testIllegalUploadMissingMainServiceTemplate() throws Exception {
105     doReturn(vspDetails).when(vspInfoDaoMock).get(any(VspDetails.class));
106
107     UploadFileResponse response = testCsarUpload("missingMainServiceTemplate.csar", 1);
108     assertTrue(response.getErrors().values().stream().anyMatch(
109         getListPredicate(Messages.CSAR_FILE_NOT_FOUND.getErrorMessage().substring(0, 7))));
110   }
111
112   @Test
113   public void testUploadFileIsNotZip() throws Exception {
114     doReturn(vspDetails).when(vspInfoDaoMock).get(any(VspDetails.class));
115
116     UploadFileResponse response = testCsarUpload("notCsar.txt", 1);
117     assertEquals("no csar file was uploaded or file doesn't exist",
118         response.getErrors().values().iterator().next().get(0).getMessage());
119   }
120
121   @Test
122   public void testUploadFileIsEmpty() throws Exception {
123     doReturn(vspDetails).when(vspInfoDaoMock).get(any(VspDetails.class));
124     onboardPackageInfo = new OnboardPackageInfo("file", OnboardingTypesEnum.CSAR.toString(),
125             ByteBuffer.wrap(new byte[]{}));
126     UploadFileResponse uploadFileResponse = candidateManager.upload(vspDetails, onboardPackageInfo);
127     assertEquals(1, uploadFileResponse.getErrors().size());
128   }
129
130   @Test
131   public void testInvalidManifestContent() throws Exception {
132
133     doReturn(vspDetails).when(vspInfoDaoMock).get(any(VspDetails.class));
134
135     try (InputStream inputStream = getClass()
136         .getResourceAsStream(BASE_DIR + "/invalidManifestContent.csar")) {
137       onboardPackageInfo = new OnboardPackageInfo("invalidManifestContent",
138               OnboardingTypesEnum.CSAR.toString(), convertFileInputStream(inputStream));
139       UploadFileResponse response =
140           candidateManager.upload(vspDetails, onboardPackageInfo);
141       assertEquals(1, response.getErrors().size());
142       assertEquals(response.getErrors().values().iterator().next().get(0).getMessage(),
143           Messages.MANIFEST_ERROR_WITH_LINE
144               .formatMessage(Messages.MANIFEST_START_METADATA.getErrorMessage(), 1, "aaa: vCSCF")
145       );
146
147     }
148   }
149
150   private Predicate<List<ErrorMessage>> getListPredicate(String substring) {
151     return error -> isEquals(substring, error);
152   }
153
154   private boolean isEquals(String substring, List<ErrorMessage> error) {
155     return error.iterator().next().getMessage().contains(substring);
156   }
157
158   private UploadFileResponse testCsarUpload(final String csarFileName,
159                                             final int expectedErrorsNumber) throws IOException {
160     UploadFileResponse uploadFileResponse;
161     try (final InputStream inputStream = getClass()
162         .getResourceAsStream(BASE_DIR + File.separator + csarFileName)) {
163       onboardPackageInfo = new OnboardPackageInfo(csarFileName, OnboardingTypesEnum.CSAR.toString(),
164               convertFileInputStream(inputStream));
165       uploadFileResponse = candidateManager.upload(vspDetails, onboardPackageInfo);
166       assertThat(String.format("Expecting %s error(s) in file '%s'", expectedErrorsNumber, csarFileName), uploadFileResponse.getErrors().size(), is(expectedErrorsNumber));
167     }
168     return uploadFileResponse;
169   }
170
171   private ByteBuffer convertFileInputStream(final InputStream fileInputStream) {
172     byte[] fileContent = new byte[0];
173     try {
174       fileContent = IOUtils.toByteArray(fileInputStream);
175     } catch (final IOException e) {
176       LOGGER.error(String.format("Could not convert %s into byte[]", fileInputStream), e);
177     }
178     return ByteBuffer.wrap(fileContent);
179   }
180
181 }