[SDC] Onboarding 1710 rebase.
[sdc.git] / openecomp-be / backend / openecomp-sdc-vendor-software-product-manager / src / test / java / org / openecomp / sdc / vendorsoftwareproduct / services / impl / CandidateServiceImplTest.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.services.impl;
22
23 import org.apache.commons.collections4.CollectionUtils;
24 import org.mockito.InjectMocks;
25 import org.mockito.Mock;
26 import org.mockito.MockitoAnnotations;
27 import org.openecomp.core.utilities.json.JsonUtil;
28 import org.openecomp.sdc.common.errors.Messages;
29 import org.openecomp.sdc.datatypes.error.ErrorMessage;
30 import org.openecomp.sdc.heat.datatypes.manifest.FileData;
31 import org.openecomp.sdc.heat.datatypes.manifest.ManifestContent;
32 import org.openecomp.sdc.heat.datatypes.structure.HeatStructureTree;
33 import org.openecomp.sdc.vendorsoftwareproduct.dao.VendorSoftwareProductDao;
34 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.VspDetails;
35 import org.openecomp.sdc.vendorsoftwareproduct.services.filedatastructuremodule.ManifestCreator;
36 import org.openecomp.sdc.vendorsoftwareproduct.services.impl.filedatastructuremodule.CandidateServiceImpl;
37 import org.openecomp.sdc.vendorsoftwareproduct.types.candidateheat.FilesDataStructure;
38 import org.openecomp.sdc.vendorsoftwareproduct.types.candidateheat.Module;
39 import org.openecomp.sdc.versioning.dao.types.Version;
40 import org.testng.Assert;
41 import org.testng.annotations.BeforeMethod;
42 import org.testng.annotations.Test;
43
44 import java.util.ArrayList;
45 import java.util.Collections;
46 import java.util.List;
47 import java.util.Optional;
48
49 import static org.mockito.Mockito.doReturn;
50 import static org.testng.Assert.assertEquals;
51 import static org.testng.Assert.assertTrue;
52
53 /**
54  * @author Avrahamg
55  * @since November 09, 2016
56  */
57 public class CandidateServiceImplTest {
58   @Mock
59   private ManifestCreator manifestCreatorMock;
60   @Mock
61   private VendorSoftwareProductDao vendorSoftwareProductDaoMock;
62   @InjectMocks
63   private CandidateServiceImpl candidateService;
64
65   @BeforeMethod
66   public void setUp() throws Exception {
67     MockitoAnnotations.initMocks(this);
68   }
69
70   @Test
71   public void shouldReturnOptionalPresentIfInputStreamIsNull() {
72     assertTrue(candidateService.validateNonEmptyFileToUpload(null).isPresent());
73   }
74
75   // end validateNonEmptyFileToUpload tests
76   // start validateNonEmptyFileToUpload tests
77   @Test
78   public void shouldReturnOptionalEmptyIfUploadedFileDataIsNotNull() {
79     assertEquals(candidateService.validateRawZipData(new byte[]{}), Optional.empty());
80   }
81
82   @Test
83   public void shouldReturnOptionalPresentIfUploadedFileDataIsNull() {
84     assertTrue(candidateService.validateRawZipData(null).isPresent());
85   }
86   // end validateNonEmptyFileToUpload tests
87   // start heatStructureTreeToFileDataStructure tests
88
89   @Test
90   public void shouldValidateManifestInZipMatchesFileDataStructureFromDB() {
91     VspDetails vspDetails = new VspDetails("vspTest", null);
92     vspDetails.setName("vspTest");
93     vspDetails.setDescription("Test description");
94     vspDetails.setVersion(new Version(0, 1));
95     //vspDetails.setOnboardingMethod(VSPCommon.OnboardingMethod.HEAT.name());
96     vspDetails.setOnboardingMethod("HEAT");
97
98     FilesDataStructure structure = JsonUtil.json2Object(getExpectedJson(), FilesDataStructure.class);
99
100     Optional<ManifestContent> expectedManifest = getExpectedManifestJson();
101     doReturn(expectedManifest)
102         .when(manifestCreatorMock).createManifest(vspDetails, structure);
103
104     String expectedManifestJson = JsonUtil.object2Json(expectedManifest.get());
105     String actualManifest = candidateService.createManifest(vspDetails, structure);
106     Assert.assertEquals(actualManifest, expectedManifestJson);
107   }
108
109   @Test
110   public void shouldReturnValidationErrorOnMissingfModule() {
111     FilesDataStructure filesDataStructure = new FilesDataStructure();
112     filesDataStructure.setArtifacts(Collections.singletonList("artifact.sh"));
113
114     Optional<List<ErrorMessage>> validateErrors =
115         candidateService.validateFileDataStructure(filesDataStructure);
116     assertValidationErrorIsAsExpected(validateErrors, 1, Messages.NO_MODULES_IN_MANIFEST
117         .getErrorMessage());
118   }
119
120   @Test
121   public void shouldReturnValidationErrorOnVolumeEnvWithoutVolumeYaml() {
122     FilesDataStructure filesDataStructure = new FilesDataStructure();
123     Module module = new Module();
124     module.setName("test");
125     module.setYaml("base_file.yml");
126     module.setVolEnv("vol_env.env");
127     filesDataStructure.setModules(Collections.singletonList(module));
128
129     Optional<List<ErrorMessage>> validateErrors =
130         candidateService.validateFileDataStructure(filesDataStructure);
131     assertValidationErrorIsAsExpected(validateErrors, 1, String.format(Messages
132         .MODULE_IN_MANIFEST_VOL_ENV_NO_VOL.getErrorMessage(), module.getName()));
133   }
134
135   @Test
136   public void shouldReturnValidationErrorOnModuleWithoutYaml() {
137     FilesDataStructure filesDataStructure = new FilesDataStructure();
138     Module module = new Module();
139     module.setName("test");
140     filesDataStructure.setModules(Collections.singletonList(module));
141
142     Optional<List<ErrorMessage>> validateErrors =
143         candidateService.validateFileDataStructure(filesDataStructure);
144     assertValidationErrorIsAsExpected(validateErrors, 1, String.format(Messages
145         .MODULE_IN_MANIFEST_NO_YAML.getErrorMessage(), module.getName()));
146   }
147
148   private void assertValidationErrorIsAsExpected(Optional<List<ErrorMessage>> validateErrors,
149                                                  int errorListSize, String expectedErrorMessage) {
150     if (validateErrors.isPresent()) {
151       List<ErrorMessage> errorMessages = validateErrors.get();
152       Assert.assertEquals(errorMessages.size(), errorListSize);
153       Assert.assertEquals(errorMessages.get(0).getMessage(), expectedErrorMessage);
154     }
155   }
156
157   private String getExpectedJson() {
158     return "{\n" +
159         "  \"modules\": [\n" +
160         "    {\n" +
161         "      \"isBase\": false,\n" +
162         "      \"yaml\": \"file2.yaml\"\n" +
163         "    },\n" +
164         "    {\n" +
165         "      \"isBase\": true,\n" +
166         "      \"yaml\": \"file1.yaml\",\n" +
167         "      \"vol\": \"file1_vol.yaml\",\n" +
168         "      \"volEnv\": \"file1.env\"\n" +
169         "    }\n" +
170         "  ],\n" +
171         "  \"unassigned\": [\n" +
172         "    \"file3.yml\"\n" +
173         "  ],\n" +
174         "  \"artifacts\": [\n" +
175         "    \"file2.sh\"\n" +
176         "  ],\n" +
177         "  \"nested\": []\n" +
178         "}";
179   }
180
181   private Optional<ManifestContent> getExpectedManifestJson() {
182     ManifestContent mock = new ManifestContent();
183     mock.setDescription("Test description");
184     mock.setName("vspTest");
185     mock.setVersion("0.1");
186
187
188     List<FileData> mockFileData = new ArrayList<>();
189     FileData fileData = createFileData("file2.yaml", false, FileData.Type.HEAT, null);
190     mockFileData.add(fileData);
191     fileData = createFileData("file1.yaml", true, FileData.Type.HEAT, null);
192     mockFileData.add(fileData);
193     fileData = createFileData("file1_vol.yaml", null, FileData.Type.HEAT_VOL, fileData);
194     fileData = createFileData("file1.env", null, FileData.Type.HEAT_ENV, mockFileData.get(1).getData().get(0));
195     mockFileData.add(createFileData("file2.sh", null, FileData.Type.OTHER, null));
196     mockFileData.add(createFileData("file3.yml", null, FileData.Type.OTHER, null));
197     mock.setData(mockFileData);
198     return Optional.of(mock);
199   }
200
201   private FileData createFileData(String fileName, Boolean isBase, FileData.Type fileType,
202                                   FileData fileDataToAddTo) {
203     FileData fileData = new FileData();
204     fileData.setFile(fileName);
205     if(isBase != null) {
206       fileData.setBase(isBase);
207     }
208     fileData.setType(fileType);
209     addFileDataToList(fileDataToAddTo, fileData);
210     return fileData;
211   }
212
213   private void addFileDataToList(FileData fileDataToAddTo, FileData fileData) {
214     if(fileDataToAddTo != null)
215     {
216       List<FileData> list = fileDataToAddTo.getData();
217       if(CollectionUtils.isEmpty(list))
218       {
219         list = new ArrayList<>();
220       }
221       list.add(fileData);
222       fileDataToAddTo.setData(list);
223
224     }
225   }
226
227   private HeatStructureTree createHeatWithEnvAndVolIncludeVolEnv() {
228     HeatStructureTree heat1 = createBasicHeatTree("file1.yaml", true, FileData.Type.HEAT);
229     addEnvToHeat(heat1, "file1.env");
230     HeatStructureTree heat1Vol =
231         createBasicHeatTree("file1_vol.yaml", false, FileData.Type.HEAT_VOL);
232     addEnvToHeat(heat1Vol, "file1_vol.env");
233     heat1.addVolumeFileToVolumeList(heat1Vol);
234     return heat1;
235   }
236
237   private void addEnvToHeat(HeatStructureTree toAddHeat, String envFileName) {
238     HeatStructureTree heat1Env = createBasicHeatTree(envFileName, false, FileData.Type.HEAT_ENV);
239     toAddHeat.setEnv(heat1Env);
240   }
241
242   private HeatStructureTree createBasicHeatTree(String fileName, boolean isBase,
243                                                 FileData.Type type) {
244     HeatStructureTree heat = new HeatStructureTree();
245     heat.setFileName(fileName);
246     heat.setBase(isBase);
247     heat.setType(type);
248     return heat;
249   }
250
251 }