1 package org.openecomp.sdc.vendorsoftwareproduct.impl;
3 import org.junit.runner.RunWith;
4 import org.mockito.InjectMocks;
5 import org.mockito.Mock;
6 import org.mockito.MockitoAnnotations;
7 import org.mockito.Spy;
8 import org.openecomp.sdc.common.errors.CoreException;
9 import org.openecomp.sdc.common.errors.ValidationErrorBuilder;
10 import org.openecomp.sdc.vendorsoftwareproduct.ComputeManager;
11 import org.openecomp.sdc.vendorsoftwareproduct.NetworkManager;
12 import org.openecomp.sdc.vendorsoftwareproduct.dao.ComputeDao;
13 import org.openecomp.sdc.vendorsoftwareproduct.dao.DeploymentFlavorDao;
14 import org.openecomp.sdc.vendorsoftwareproduct.dao.NicDao;
15 import org.openecomp.sdc.vendorsoftwareproduct.dao.VendorSoftwareProductInfoDao;
16 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.DeploymentFlavorEntity;
17 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.NetworkEntity;
18 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.ComputeEntity;
19 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.VspDetails;
20 import org.openecomp.sdc.vendorsoftwareproduct.errors.DuplicateComputeInComponentErrorBuilder;
21 import org.openecomp.sdc.vendorsoftwareproduct.errors.VendorSoftwareProductErrorCodes;
22 import org.openecomp.sdc.vendorsoftwareproduct.services.composition.CompositionEntityDataManager;
23 import org.openecomp.sdc.vendorsoftwareproduct.services.schemagenerator.SchemaGenerator;
24 import org.openecomp.sdc.vendorsoftwareproduct.types.CompositionEntityResponse;
25 import org.openecomp.sdc.vendorsoftwareproduct.types.ListComputeResponse;
26 import org.openecomp.sdc.vendorsoftwareproduct.types.QuestionnaireResponse;
27 import org.openecomp.sdc.vendorsoftwareproduct.types.composition.*;
28 import org.openecomp.sdc.versioning.dao.types.Version;
29 import org.openecomp.sdc.versioning.errors.VersioningErrorCodes;
30 import org.testng.Assert;
31 import org.testng.annotations.BeforeMethod;
32 import org.testng.annotations.Test;
34 import java.util.ArrayList;
35 import java.util.Arrays;
36 import java.util.Collection;
37 import java.util.List;
39 import static org.mockito.Matchers.anyObject;
40 import static org.mockito.Mockito.doReturn;
41 import static org.mockito.Mockito.never;
42 import static org.mockito.Mockito.verify;
43 import static org.mockito.Mockito.when;
46 * Created by DIVESHM on 5/16/2017.
50 public class ComputeManagerImplTest {
52 private static final String COMPUTE_NOT_EXIST_MSG =
53 "Vendor Software Product COMPUTE with Id compute1 does not exist for Vendor Software Product with " +
54 "id VSP_ID and version 0.1";
56 private static final String USER = "computeTestUser";
57 private static final String VSP_ID = "VSP_ID";
58 private static final Version VERSION = new Version(0, 1);
59 private static final String COMPONENT_ID = "COMPONENT_ID";
60 private static final String COMPUTE1_ID = "compute1";
61 private static final String COMPUTE2_ID = "compute2";
64 private ComputeDao computeDao;
67 private CompositionEntityDataManager compositionEntityDataManagerMock;
70 private NetworkManager networkManagerMock;
73 private VendorSoftwareProductInfoDao vspInfoDao;
76 private ComputeEntity computeEntity;
79 private ListComputeResponse listComputeResponse;
82 private DeploymentFlavorEntity deploymentFlavorEntity;
85 private DeploymentFlavorDao deploymentFlavorDao;
88 private ComponentComputeAssociation componentComputeAssociation;
91 DeploymentFlavor deploymentFlavor;
95 private ComputeManagerImpl computeManager;
98 public void setUp() throws Exception {
99 MockitoAnnotations.initMocks(this);
103 public void testListWhenNone() {
104 Collection<ListComputeResponse> computes = computeManager.listCompute(VSP_ID, VERSION, COMPONENT_ID, USER);
105 Assert.assertEquals(computes.size(), 0);
109 public void testList(){
110 doReturn(Arrays.asList(
111 createCompute(VSP_ID, VERSION, COMPONENT_ID, COMPUTE1_ID),
112 createCompute(VSP_ID, VERSION, COMPONENT_ID, COMPUTE2_ID)))
113 .when(computeDao).list(anyObject());
116 Collection<ListComputeResponse> computes = computeManager.listCompute(VSP_ID, VERSION, COMPONENT_ID, USER);
117 Assert.assertEquals(computes.size(), 2);
118 for (ListComputeResponse compute : computes) {
119 Assert.assertEquals(compute.getComputeEntity().getComputeCompositionData().getName(),
120 COMPUTE1_ID.equals(compute.getComputeEntity().getId())
127 public void testCreateOnNotManualCompute_negative() {
129 testCreate_negative(new ComputeEntity(VSP_ID, VERSION, COMPONENT_ID, null), USER,
130 VendorSoftwareProductErrorCodes.ADD_COMPUTE_NOT_ALLOWED_IN_HEAT_ONBOARDING);
134 public void testCreateManualCompute() {
135 ComputeEntity expected = createCompute(VSP_ID, VERSION, COMPONENT_ID, COMPUTE1_ID);
136 doReturn(true).when(vspInfoDao).isManual(anyObject(), anyObject());
137 computeManager.createCompute(expected, USER);
138 verify(compositionEntityDataManagerMock).createCompute(expected);
139 verify(compositionEntityDataManagerMock).createCompute(expected);
143 public void testCreateManualComputeWithDuplicateName() {
144 ComputeEntity expected = createCompute(VSP_ID, VERSION, COMPONENT_ID, COMPUTE1_ID);
145 doReturn(true).when(vspInfoDao).isManual(anyObject(), anyObject());
147 ComputeEntity expectedDiffName = createCompute(VSP_ID, VERSION, COMPONENT_ID, COMPUTE1_ID);
148 expectedDiffName.setId(COMPUTE1_ID + "Name");
149 ComputeData computeData = expectedDiffName.getComputeCompositionData();
150 computeData.setName(COMPUTE1_ID + "Name");
151 expectedDiffName.setComputeCompositionData(computeData);
152 List<ComputeEntity> vfcImageList = new ArrayList<ComputeEntity>();
153 vfcImageList.add(expectedDiffName);
154 doReturn(vfcImageList).when(computeDao).list(anyObject());
157 computeManager.createCompute(expected, USER);
160 catch (CoreException ex) {
161 Assert.assertEquals(VendorSoftwareProductErrorCodes.DUPLICATE_COMPUTE_NAME_NOT_ALLOWED,
167 public void testCreateManualComputeWithIncorrectNameFormat() {
168 ComputeEntity expected = createCompute(VSP_ID, VERSION, COMPONENT_ID, COMPUTE1_ID);
169 doReturn(true).when(vspInfoDao).isManual(anyObject(), anyObject());
171 ComputeEntity expectedDiffName = createCompute(VSP_ID, VERSION, COMPONENT_ID, COMPUTE1_ID);
172 ComputeData computeData = expectedDiffName.getComputeCompositionData();
173 computeData.setName(COMPUTE1_ID + "Name/*");
174 expectedDiffName.setComputeCompositionData(computeData);
175 List<ComputeEntity> vfcImageList = new ArrayList<ComputeEntity>();
176 vfcImageList.add(expectedDiffName);
177 doReturn(vfcImageList).when(computeDao).list(anyObject());
180 computeManager.createCompute(expectedDiffName, USER);
183 catch (CoreException ex) {
184 Assert.assertEquals(VendorSoftwareProductErrorCodes.COMPUTE_NAME_FORMAT_NOT_ALLOWED,
190 public void testUpdateNonExistingComputeId_negative() {
192 testUpdate_negative(VSP_ID, VERSION, COMPONENT_ID, COMPUTE1_ID, USER,
193 VersioningErrorCodes.VERSIONABLE_SUB_ENTITY_NOT_FOUND);
197 public void testUpdateCompute() {
198 doReturn(createCompute(VSP_ID, VERSION, COMPONENT_ID, COMPUTE1_ID))
199 .when(computeDao).get(anyObject());
201 doReturn(new CompositionEntityValidationData(CompositionEntityType.compute, COMPUTE1_ID))
202 .when(compositionEntityDataManagerMock)
203 .validateEntity(anyObject(), anyObject(), anyObject());
205 ComputeEntity computeEntity = new ComputeEntity(VSP_ID, VERSION, COMPONENT_ID, COMPUTE1_ID);
206 ComputeData computeData = new ComputeData();
207 computeData.setName(COMPUTE1_ID + "name");
208 computeData.setDescription(COMPUTE1_ID + "desc updated");
209 computeEntity.setComputeCompositionData(computeData);
211 CompositionEntityValidationData validationData =
212 computeManager.updateCompute(computeEntity, USER);
213 Assert.assertTrue(validationData == null || validationData.getErrors() == null);
214 verify(computeDao).update(computeEntity);
218 public void testUpdateComputeWithIncorrectNameFormat() {
219 doReturn(createCompute(VSP_ID, VERSION, COMPONENT_ID, COMPUTE1_ID))
220 .when(computeDao).get(anyObject());
222 doReturn(new CompositionEntityValidationData(CompositionEntityType.compute, COMPUTE1_ID))
223 .when(compositionEntityDataManagerMock)
224 .validateEntity(anyObject(), anyObject(), anyObject());
226 doReturn(true).when(vspInfoDao).isManual(anyObject(), anyObject());
228 ComputeEntity computeEntity = new ComputeEntity(VSP_ID, VERSION, COMPONENT_ID, COMPUTE1_ID);
229 ComputeData computeData = new ComputeData();
230 computeData.setName(COMPUTE1_ID + "name/*");
231 computeData.setDescription(COMPUTE1_ID + "desc updated");
232 computeEntity.setComputeCompositionData(computeData);
235 computeManager.updateCompute(computeEntity, USER);
238 catch (CoreException ex) {
239 Assert.assertEquals(VendorSoftwareProductErrorCodes.COMPUTE_NAME_FORMAT_NOT_ALLOWED,
245 public void testIllegalComputeUpdate() {
246 doReturn(createCompute(VSP_ID, VERSION, COMPONENT_ID, COMPUTE1_ID))
247 .when(computeDao).get(anyObject());
249 doReturn(true).when(vspInfoDao).isManual(anyObject(), anyObject());
251 CompositionEntityValidationData toBeReturned =
252 new CompositionEntityValidationData(CompositionEntityType.compute, COMPUTE1_ID);
253 toBeReturned.setErrors(Arrays.asList("error1", "error2"));
254 doReturn(toBeReturned)
255 .when(compositionEntityDataManagerMock)
256 .validateEntity(anyObject(), anyObject(), anyObject());
258 ComputeEntity computeEntity = new ComputeEntity(VSP_ID, VERSION, COMPONENT_ID, COMPUTE1_ID);
259 ComputeData computeData = new ComputeData();
260 computeData.setName(COMPUTE1_ID + "_name_updated");
261 computeData.setDescription(COMPUTE1_ID + " desc updated");
262 computeEntity.setComputeCompositionData(computeData);
264 CompositionEntityValidationData validationData = computeManager.updateCompute(computeEntity, USER);
265 Assert.assertNotNull(validationData);
266 Assert.assertEquals(validationData.getErrors().size(), 2);
268 verify(computeDao, never()).update(computeEntity);
272 public void testUpdateHEATComputeName() throws Exception {
273 doReturn(createCompute(VSP_ID, VERSION, COMPONENT_ID, COMPUTE1_ID))
274 .when(computeDao).get(anyObject());
275 ComputeEntity computeEntity = new ComputeEntity(VSP_ID, VERSION, COMPONENT_ID, COMPUTE1_ID);
276 ComputeData computeData = new ComputeData();
277 computeData.setName(COMPUTE1_ID + " name updated");
278 computeData.setDescription(COMPUTE1_ID + " desc updated");
279 computeEntity.setComputeCompositionData(computeData);
282 computeManager.updateCompute(computeEntity, USER);
284 catch (CoreException ex) {
285 Assert.assertEquals(ex.code().id(), VendorSoftwareProductErrorCodes.UPDATE_COMPUTE_NOT_ALLOWED);
291 public void testUpdateManualComputeQuestionnaire() throws Exception {
292 String json = "{\"md5\" :\"FFDSD33SS\"}";
293 doReturn(true).when(vspInfoDao).isManual(anyObject(), anyObject());
294 doReturn(new ComputeEntity(null,null,null,null)).when(computeDao).get(anyObject());
296 computeManager.updateComputeQuestionnaire(VSP_ID, VERSION, COMPONENT_ID, COMPUTE1_ID, json, USER);
297 verify(computeDao).updateQuestionnaireData(VSP_ID, VERSION, COMPONENT_ID, COMPUTE1_ID, json);
301 public void testGetNonExistingComputeId_negative() {
302 testGet_negative(VSP_ID, VERSION, COMPONENT_ID, "non existing compute id", USER,
303 VersioningErrorCodes.VERSIONABLE_SUB_ENTITY_NOT_FOUND);
307 public void testGet() {
308 ComputeEntity expected = createCompute(VSP_ID, VERSION, COMPONENT_ID, COMPUTE1_ID);
309 doReturn(expected).when(computeDao).get(anyObject());
310 String compositionSchema = "schema string";
311 doReturn(compositionSchema).when(computeManager).getComputeCompositionSchema(anyObject());
313 CompositionEntityResponse<ComputeData> response =
314 computeManager.getCompute(VSP_ID, VERSION, COMPONENT_ID, COMPUTE1_ID, USER);
315 Assert.assertEquals(response.getId(), expected.getId());
316 Assert.assertEquals(response.getData().getName(), expected.getComputeCompositionData().getName());
317 Assert.assertEquals(response.getData().getDescription(), expected.getComputeCompositionData().
319 Assert.assertEquals(response.getSchema(), compositionSchema);
323 public void testGetQuestionnaire() throws Exception {
324 ComputeEntity compute = new ComputeEntity(VSP_ID, VERSION, COMPONENT_ID, COMPUTE1_ID);
325 compute.setQuestionnaireData("{}");
326 doReturn(compute).when(computeDao).getQuestionnaireData(VSP_ID, VERSION, COMPONENT_ID, COMPUTE1_ID);
328 String schema = "schema string";
331 doReturn(schema).when(computeManager).getComputeQuestionnaireSchema(anyObject());
333 QuestionnaireResponse questionnaire =
334 computeManager.getComputeQuestionnaire(VSP_ID, VERSION, COMPONENT_ID, COMPUTE1_ID, USER);
336 Assert.assertNotNull(questionnaire);
337 Assert.assertEquals(questionnaire.getData(), compute.getQuestionnaireData());
338 Assert.assertEquals(questionnaire.getSchema(), schema);
339 Assert.assertNull(questionnaire.getErrorMessage());
343 public void testDeleteOnNotManualCompute() {
344 ComputeEntity expected = createCompute(VSP_ID, VERSION, COMPONENT_ID, COMPUTE1_ID);
345 doReturn(expected).when(computeDao).get(anyObject());
346 testDelete_negative(VSP_ID, VERSION, COMPONENT_ID, COMPUTE1_ID, USER,
347 VendorSoftwareProductErrorCodes.VSP_COMPOSITION_EDIT_NOT_ALLOWED);
351 public void testDeleteOnManualCompute() {
352 ComputeEntity expected = createCompute(VSP_ID, VERSION, COMPONENT_ID, COMPUTE1_ID);
353 doReturn(expected).when(computeDao).get(anyObject());
354 doReturn(true).when(vspInfoDao).isManual(anyObject(), anyObject());
355 computeManager.deleteCompute(VSP_ID, VERSION, COMPONENT_ID, COMPUTE1_ID, USER);
356 verify(computeDao).delete(anyObject());
360 public void testDeleteOnNotExistCompute() {
361 testDelete_negative(VSP_ID, VERSION, COMPONENT_ID, COMPUTE1_ID, USER,
362 VendorSoftwareProductErrorCodes.VSP_COMPOSITION_EDIT_NOT_ALLOWED);
366 private void testDelete_negative(String vspId, Version version, String componentId, String computeId,
368 String expectedErrorCode) {
370 computeManager.deleteCompute(vspId, version, componentId, computeId, user);
372 } catch (CoreException exception) {
373 Assert.assertEquals(exception.code().id(), expectedErrorCode);
377 private void testGet_negative(String vspId, Version version, String componentId, String computeId,
378 String user, String expectedErrorCode) {
380 computeManager.getCompute(vspId, version, componentId, computeId, user);
382 } catch (CoreException exception) {
383 Assert.assertEquals(exception.code().id(), expectedErrorCode);
387 private void testList_negative(String vspId, Version version, String componentId, String user,
388 String expectedErrorCode, String expectedErrorMsg) {
390 computeManager.listCompute(vspId, version, componentId, user);
392 } catch (CoreException exception) {
393 Assert.assertEquals(exception.code().id(), expectedErrorCode);
394 Assert.assertEquals(exception.getMessage(), expectedErrorMsg);
400 private void testUpdate_negative(String vspId, Version version, String componentId, String computeId,
401 String user, String expectedErrorCode) {
403 computeManager.updateCompute(new ComputeEntity(vspId, version, componentId, computeId), user);
405 } catch (CoreException exception) {
406 Assert.assertEquals(exception.code().id(), expectedErrorCode);
410 private void testCreate_negative(ComputeEntity computeEntity1, String user, String expectedErrorCode) {
412 computeManager.createCompute(computeEntity1, user);
414 } catch (CoreException exception) {
415 Assert.assertEquals(exception.code().id(), expectedErrorCode);
419 static ComputeEntity createCompute(String vspId, Version version, String compId, String computeId){
420 ComputeEntity computeEntity1 = new ComputeEntity(vspId, version, compId, computeId);
421 ComputeData computeData = new ComputeData();
422 computeData.setName(computeId+"name");
423 computeData.setDescription(computeId+"desc");
424 computeEntity1.setComputeCompositionData(computeData);
425 return computeEntity1;