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