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