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