217cdf585b9bd24c9ea06e193227bc7787a88954
[sdc.git] /
1 package org.openecomp.sdc.vendorsoftwareproduct.impl;
2
3 import org.mockito.InjectMocks;
4 import org.mockito.Mock;
5 import org.mockito.MockitoAnnotations;
6 import org.mockito.Spy;
7 import org.openecomp.sdc.common.errors.CoreException;
8 import org.openecomp.sdc.common.errors.ErrorCategory;
9 import org.openecomp.sdc.common.errors.ErrorCode;
10 import org.openecomp.sdc.vendorsoftwareproduct.dao.ComputeDao;
11 import org.openecomp.sdc.vendorsoftwareproduct.dao.DeploymentFlavorDao;
12 import org.openecomp.sdc.vendorsoftwareproduct.dao.VendorSoftwareProductInfoDao;
13 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.ComputeEntity;
14 import org.openecomp.sdc.vendorsoftwareproduct.errors.VendorSoftwareProductErrorCodes;
15 import org.openecomp.sdc.vendorsoftwareproduct.CompositionEntityDataManager;
16 import org.openecomp.sdc.vendorsoftwareproduct.types.CompositionEntityResponse;
17 import org.openecomp.sdc.vendorsoftwareproduct.types.ListComputeResponse;
18 import org.openecomp.sdc.vendorsoftwareproduct.types.QuestionnaireResponse;
19 import org.openecomp.sdc.vendorsoftwareproduct.types.composition.CompositionEntityType;
20 import org.openecomp.sdc.vendorsoftwareproduct.types.composition.CompositionEntityValidationData;
21 import org.openecomp.sdc.vendorsoftwareproduct.types.composition.ComputeData;
22 import org.openecomp.sdc.versioning.dao.types.Version;
23 import org.openecomp.sdc.versioning.errors.VersioningErrorCodes;
24 import org.testng.Assert;
25 import org.testng.annotations.AfterMethod;
26 import org.testng.annotations.BeforeMethod;
27 import org.testng.annotations.Test;
28
29 import java.util.Arrays;
30 import java.util.Collection;
31
32 import static org.mockito.Matchers.anyObject;
33 import static org.mockito.Mockito.doNothing;
34 import static org.mockito.Mockito.doReturn;
35 import static org.mockito.Mockito.doThrow;
36 import static org.mockito.Mockito.never;
37 import static org.mockito.Mockito.verify;
38
39 public class ComputeManagerImplTest {
40
41   private static final String COMPUTE_NOT_EXIST_MSG =
42       "Vendor Software Product COMPUTE with Id compute1 does not exist for Vendor Software Product with " +
43           "id VSP_ID and version version_id";
44
45   private static final String VSP_ID = "VSP_ID";
46   private static final Version VERSION = new Version("version_id");
47   private static final String COMPONENT_ID = "COMPONENT_ID";
48   private static final String COMPUTE1_ID = "compute1";
49   private static final String COMPUTE2_ID = "compute2";
50
51   @Mock
52   private ComputeDao computeDao;
53   @Mock
54   private CompositionEntityDataManager compositionEntityDataManagerMock;
55   @Mock
56   private VendorSoftwareProductInfoDao vspInfoDao;
57   @Mock
58   private DeploymentFlavorDao deploymentFlavorDao;
59   @InjectMocks
60   @Spy
61   private ComputeManagerImpl computeManager;
62
63   @BeforeMethod
64   public void setUp() throws Exception {
65     MockitoAnnotations.initMocks(this);
66   }
67
68   @AfterMethod
69   public void tearDown() {
70     computeManager = null;
71   }
72
73   @Test
74   public void testListWhenNone() {
75     Collection<ListComputeResponse> computes =
76         computeManager.listComputes(VSP_ID, VERSION, COMPONENT_ID);
77     Assert.assertEquals(computes.size(), 0);
78   }
79
80   @Test
81   public void testList() {
82     doReturn(Arrays.asList(
83         createCompute(VSP_ID, VERSION, COMPONENT_ID, COMPUTE1_ID),
84         createCompute(VSP_ID, VERSION, COMPONENT_ID, COMPUTE2_ID)))
85         .when(computeDao).list(anyObject());
86
87
88     Collection<ListComputeResponse> computes =
89         computeManager.listComputes(VSP_ID, VERSION, COMPONENT_ID);
90     Assert.assertEquals(computes.size(), 2);
91     for (ListComputeResponse compute : computes) {
92       Assert.assertEquals(compute.getComputeEntity().getComputeCompositionData().getName(),
93           COMPUTE1_ID.equals(compute.getComputeEntity().getId())
94               ? "compute1name"
95               : "compute2name");
96     }
97   }
98
99   @Test
100   public void testCreateOnNotManualCompute_negative() {
101     testCreate_negative(new ComputeEntity(VSP_ID, VERSION, COMPONENT_ID, null),
102         VendorSoftwareProductErrorCodes.ADD_COMPUTE_NOT_ALLOWED_IN_HEAT_ONBOARDING);
103   }
104
105   @Test
106   public void testCreateManualCompute() {
107     ComputeEntity expected = createCompute(VSP_ID, VERSION, COMPONENT_ID, COMPUTE1_ID);
108     doReturn(true).when(vspInfoDao).isManual(anyObject(), anyObject());
109     doNothing().when(computeManager)
110         .validateUniqueName(VSP_ID, VERSION, COMPONENT_ID,
111             expected.getComputeCompositionData().getName());
112     doNothing().when(computeManager)
113         .createUniqueName(VSP_ID, VERSION, COMPONENT_ID,
114             expected.getComputeCompositionData().getName());
115     String questionnaireSchema = "{}";
116     doReturn(questionnaireSchema).when(computeManager).getComputeQuestionnaireSchema(anyObject());
117
118     computeManager.createCompute(expected);
119     verify(computeDao).create(expected);
120   }
121
122   @Test(expectedExceptions = CoreException.class)
123   public void testCreateManualComputeWithDuplicateName() {
124     ComputeEntity expected = createCompute(VSP_ID, VERSION, COMPONENT_ID, COMPUTE1_ID);
125     doReturn(true).when(vspInfoDao).isManual(anyObject(), anyObject());
126
127     doThrow(new CoreException(
128         new ErrorCode.ErrorCodeBuilder().withCategory(ErrorCategory.APPLICATION).build()))
129         .when(computeManager).validateUniqueName(VSP_ID, VERSION, COMPONENT_ID,
130         expected.getComputeCompositionData().getName());
131
132     computeManager.createCompute(expected);
133   }
134
135   @Test
136   public void testUpdateNonExistingComputeId_negative() {
137     testUpdate_negative(VSP_ID, VERSION, COMPONENT_ID, COMPUTE1_ID,
138         VersioningErrorCodes.VERSIONABLE_SUB_ENTITY_NOT_FOUND);
139   }
140
141   @Test
142   public void testUpdateCompute() {
143     ComputeEntity retrieved = createCompute(VSP_ID, VERSION, COMPONENT_ID, COMPUTE1_ID);
144     doReturn(retrieved).when(computeDao).get(anyObject());
145
146     doReturn(new CompositionEntityValidationData(CompositionEntityType.compute, COMPUTE1_ID))
147         .when(compositionEntityDataManagerMock)
148         .validateEntity(anyObject(), anyObject(), anyObject());
149
150     ComputeEntity computeEntity = new ComputeEntity(VSP_ID, VERSION, COMPONENT_ID, COMPUTE1_ID);
151     ComputeData computeData = new ComputeData();
152     computeData.setName(COMPUTE1_ID + "name");
153     computeData.setDescription(COMPUTE1_ID + "desc updated");
154     computeEntity.setComputeCompositionData(computeData);
155
156     doNothing().when(computeManager)
157         .updateUniqueName(VSP_ID, VERSION, COMPONENT_ID, retrieved.getComputeCompositionData().getName(),
158             computeData.getName());
159
160     CompositionEntityValidationData validationData =
161         computeManager.updateCompute(computeEntity);
162     Assert.assertTrue(validationData == null || validationData.getErrors() == null);
163     verify(computeDao).update(computeEntity);
164   }
165
166   @Test
167   public void testIllegalComputeUpdate() {
168     doReturn(createCompute(VSP_ID, VERSION, COMPONENT_ID, COMPUTE1_ID))
169         .when(computeDao).get(anyObject());
170
171     doReturn(true).when(vspInfoDao).isManual(anyObject(), anyObject());
172
173     CompositionEntityValidationData toBeReturned =
174         new CompositionEntityValidationData(CompositionEntityType.compute, COMPUTE1_ID);
175     toBeReturned.setErrors(Arrays.asList("error1", "error2"));
176     doReturn(toBeReturned)
177         .when(compositionEntityDataManagerMock)
178         .validateEntity(anyObject(), anyObject(), anyObject());
179
180     ComputeEntity computeEntity = new ComputeEntity(VSP_ID, VERSION, COMPONENT_ID, COMPUTE1_ID);
181     ComputeData computeData = new ComputeData();
182     computeData.setName(COMPUTE1_ID + "_name_updated");
183     computeData.setDescription(COMPUTE1_ID + " desc updated");
184     computeEntity.setComputeCompositionData(computeData);
185
186     CompositionEntityValidationData validationData =
187         computeManager.updateCompute(computeEntity);
188     Assert.assertNotNull(validationData);
189     Assert.assertEquals(validationData.getErrors().size(), 2);
190
191     verify(computeDao, never()).update(computeEntity);
192   }
193
194   @Test
195   public void testUpdateHEATComputeName() throws Exception {
196     doReturn(createCompute(VSP_ID, VERSION, COMPONENT_ID, COMPUTE1_ID))
197         .when(computeDao).get(anyObject());
198
199     String updatedName = COMPUTE1_ID + " name updated";
200     CompositionEntityValidationData toBeReturned =
201         new CompositionEntityValidationData(CompositionEntityType.compute, COMPUTE1_ID);
202
203     toBeReturned.setErrors(Arrays.asList("#/name: "+updatedName+" is not a valid value."+
204         COMPUTE1_ID+"is the only possible value for this field"));
205     doReturn(toBeReturned).when(compositionEntityDataManagerMock).validateEntity(anyObject(),anyObject(),anyObject());
206
207     ComputeEntity computeEntity = new ComputeEntity(VSP_ID, VERSION, COMPONENT_ID, COMPUTE1_ID);
208     ComputeData computeData = new ComputeData();
209     computeData.setName(updatedName);
210     computeData.setDescription(COMPUTE1_ID + " desc updated");
211     computeEntity.setComputeCompositionData(computeData);
212
213     CompositionEntityValidationData output = computeManager.updateCompute(computeEntity);
214
215     Assert.assertEquals(output.getErrors(), toBeReturned.getErrors());
216   }
217
218   @Test
219   public void testUpdateManualComputeQuestionnaire() throws Exception {
220     String json = "{\"md5\" :\"FFDSD33SS\"}";
221     doReturn(true).when(vspInfoDao).isManual(anyObject(), anyObject());
222     doReturn(new ComputeEntity(null, null, null, null)).when(computeDao).get(anyObject());
223
224     computeManager
225         .updateComputeQuestionnaire(VSP_ID, VERSION, COMPONENT_ID, COMPUTE1_ID, json);
226     verify(computeDao).updateQuestionnaireData(VSP_ID, VERSION, COMPONENT_ID, COMPUTE1_ID, json);
227   }
228
229   @Test
230   public void testGetNonExistingComputeId_negative() {
231     testGet_negative(VSP_ID, VERSION, COMPONENT_ID, "non existing compute id",
232         VersioningErrorCodes.VERSIONABLE_SUB_ENTITY_NOT_FOUND);
233   }
234
235   @Test
236   public void testGet() {
237     ComputeEntity expected = createCompute(VSP_ID, VERSION, COMPONENT_ID, COMPUTE1_ID);
238     doReturn(expected).when(computeDao).get(anyObject());
239     String compositionSchema = "schema string";
240     doReturn(compositionSchema).when(computeManager).getComputeCompositionSchema(anyObject());
241
242     CompositionEntityResponse<ComputeData> response =
243         computeManager.getCompute(VSP_ID, VERSION, COMPONENT_ID, COMPUTE1_ID);
244     Assert.assertEquals(response.getId(), expected.getId());
245     Assert
246         .assertEquals(response.getData().getName(), expected.getComputeCompositionData().getName());
247     Assert.assertEquals(response.getData().getDescription(), expected.getComputeCompositionData().
248         getDescription());
249     Assert.assertEquals(response.getSchema(), compositionSchema);
250   }
251
252   @Test
253   public void testGetQuestionnaire() throws Exception {
254     ComputeEntity compute = new ComputeEntity(VSP_ID, VERSION, COMPONENT_ID, COMPUTE1_ID);
255     compute.setQuestionnaireData("{}");
256     doReturn(compute).when(computeDao)
257         .getQuestionnaireData(VSP_ID, VERSION, COMPONENT_ID, COMPUTE1_ID);
258
259     String schema = "schema string";
260
261     doReturn(schema).when(computeManager).getComputeQuestionnaireSchema(anyObject());
262
263     QuestionnaireResponse questionnaire =
264         computeManager.getComputeQuestionnaire(VSP_ID, VERSION, COMPONENT_ID, COMPUTE1_ID);
265
266     Assert.assertNotNull(questionnaire);
267     Assert.assertEquals(questionnaire.getData(), compute.getQuestionnaireData());
268     Assert.assertEquals(questionnaire.getSchema(), schema);
269     Assert.assertNull(questionnaire.getErrorMessage());
270   }
271
272   @Test
273   public void testDeleteOnNotManualCompute() {
274     ComputeEntity expected = createCompute(VSP_ID, VERSION, COMPONENT_ID, COMPUTE1_ID);
275     doReturn(expected).when(computeDao).get(anyObject());
276     testDelete_negative(VSP_ID, VERSION, COMPONENT_ID, COMPUTE1_ID,
277         VendorSoftwareProductErrorCodes.VSP_COMPOSITION_EDIT_NOT_ALLOWED);
278   }
279
280   @Test
281   public void testDeleteOnManualCompute() {
282     ComputeEntity expected = createCompute(VSP_ID, VERSION, COMPONENT_ID, COMPUTE1_ID);
283     doReturn(expected).when(computeDao).get(anyObject());
284     doReturn(true).when(vspInfoDao).isManual(anyObject(), anyObject());
285     doNothing().when(computeManager).deleteUniqueValue(VSP_ID, VERSION, COMPONENT_ID,
286         expected.getComputeCompositionData().getName());
287
288     computeManager.deleteCompute(VSP_ID, VERSION, COMPONENT_ID, COMPUTE1_ID);
289     verify(computeDao).delete(anyObject());
290   }
291
292   @Test
293   public void testDeleteOnNotExistCompute() {
294     testDelete_negative(VSP_ID, VERSION, COMPONENT_ID, COMPUTE1_ID,
295         VendorSoftwareProductErrorCodes.VSP_COMPOSITION_EDIT_NOT_ALLOWED);
296   }
297
298
299   private void testDelete_negative(String vspId, Version version, String componentId,
300                                    String computeId,
301                                    String expectedErrorCode) {
302     try {
303       computeManager.deleteCompute(vspId, version, componentId, computeId);
304       Assert.fail();
305     } catch (CoreException exception) {
306       Assert.assertEquals(exception.code().id(), expectedErrorCode);
307     }
308   }
309
310   private void testGet_negative(String vspId, Version version, String componentId, String computeId,
311                                 String expectedErrorCode) {
312     try {
313       computeManager.getCompute(vspId, version, componentId, computeId);
314       Assert.fail();
315     } catch (CoreException exception) {
316       Assert.assertEquals(exception.code().id(), expectedErrorCode);
317     }
318   }
319
320   private void testList_negative(String vspId, Version version, String componentId,
321                                  String expectedErrorCode, String expectedErrorMsg) {
322     try {
323       computeManager.listComputes(vspId, version, componentId);
324       Assert.fail();
325     } catch (CoreException exception) {
326       Assert.assertEquals(exception.code().id(), expectedErrorCode);
327       Assert.assertEquals(exception.getMessage(), expectedErrorMsg);
328     }
329   }
330
331
332   private void testUpdate_negative(String vspId, Version version, String componentId,
333                                    String computeId, String expectedErrorCode) {
334     try {
335       computeManager.updateCompute(new ComputeEntity(vspId, version, componentId, computeId));
336       Assert.fail();
337     } catch (CoreException exception) {
338       Assert.assertEquals(exception.code().id(), expectedErrorCode);
339     }
340   }
341
342   private void testCreate_negative(ComputeEntity computeEntity1, String expectedErrorCode) {
343     try {
344       computeManager.createCompute(computeEntity1);
345       Assert.fail();
346     } catch (CoreException exception) {
347       Assert.assertEquals(exception.code().id(), expectedErrorCode);
348     }
349   }
350
351   private static ComputeEntity createCompute(String vspId, Version version, String compId,
352                                              String computeId) {
353     ComputeEntity computeEntity1 = new ComputeEntity(vspId, version, compId, computeId);
354     ComputeData computeData = new ComputeData();
355     computeData.setName(computeId + "name");
356     computeData.setDescription(computeId + "desc");
357     computeEntity1.setComputeCompositionData(computeData);
358     return computeEntity1;
359   }
360 }