Get manifest location from Meta file
[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.junit.After;
4 import org.junit.Assert;
5 import org.junit.Before;
6 import org.junit.Test;
7 import org.mockito.InjectMocks;
8 import org.mockito.Mock;
9 import org.mockito.MockitoAnnotations;
10 import org.mockito.Spy;
11 import org.openecomp.sdc.common.errors.CoreException;
12 import org.openecomp.sdc.common.errors.ErrorCategory;
13 import org.openecomp.sdc.common.errors.ErrorCode;
14 import org.openecomp.sdc.vendorsoftwareproduct.CompositionEntityDataManager;
15 import org.openecomp.sdc.vendorsoftwareproduct.dao.ComputeDao;
16 import org.openecomp.sdc.vendorsoftwareproduct.dao.DeploymentFlavorDao;
17 import org.openecomp.sdc.vendorsoftwareproduct.dao.VendorSoftwareProductInfoDao;
18 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.ComputeEntity;
19 import org.openecomp.sdc.vendorsoftwareproduct.errors.VendorSoftwareProductErrorCodes;
20 import org.openecomp.sdc.vendorsoftwareproduct.types.CompositionEntityResponse;
21 import org.openecomp.sdc.vendorsoftwareproduct.types.ListComputeResponse;
22 import org.openecomp.sdc.vendorsoftwareproduct.types.QuestionnaireResponse;
23 import org.openecomp.sdc.vendorsoftwareproduct.types.composition.CompositionEntityType;
24 import org.openecomp.sdc.vendorsoftwareproduct.types.composition.CompositionEntityValidationData;
25 import org.openecomp.sdc.vendorsoftwareproduct.types.composition.ComputeData;
26 import org.openecomp.sdc.versioning.dao.types.Version;
27 import org.openecomp.sdc.versioning.errors.VersioningErrorCodes;
28
29 import java.util.Arrays;
30 import java.util.Collection;
31
32 import static org.mockito.ArgumentMatchers.any;
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
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   @Before
65   public void setUp() throws Exception {
66     MockitoAnnotations.initMocks(this);
67   }
68
69   @After
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(any());
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(any(), any());
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(any());
118
119     computeManager.createCompute(expected);
120     verify(computeDao).create(expected);
121   }
122
123   @Test(expected = CoreException.class)
124   public void testCreateManualComputeWithDuplicateName() {
125     ComputeEntity expected = createCompute(VSP_ID, VERSION, COMPONENT_ID, COMPUTE1_ID);
126     doReturn(true).when(vspInfoDao).isManual(any(), any());
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(any());
146
147     doReturn(new CompositionEntityValidationData(CompositionEntityType.compute, COMPUTE1_ID))
148         .when(compositionEntityDataManagerMock)
149         .validateEntity(any(), any(), any());
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(any());
171
172     doReturn(true).when(vspInfoDao).isManual(any(), any());
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(any(), any(), any());
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(any());
199
200     String updatedName = COMPUTE1_ID + " name updated";
201     CompositionEntityValidationData toBeReturned =
202         new CompositionEntityValidationData(CompositionEntityType.compute, COMPUTE1_ID);
203
204     toBeReturned.setErrors(Arrays.asList("#/name: "+updatedName+" is not a valid value."+
205         COMPUTE1_ID+"is the only possible value for this field"));
206     doReturn(toBeReturned).when(compositionEntityDataManagerMock).validateEntity(any(),any(),any());
207
208     ComputeEntity computeEntity = new ComputeEntity(VSP_ID, VERSION, COMPONENT_ID, COMPUTE1_ID);
209     ComputeData computeData = new ComputeData();
210     computeData.setName(updatedName);
211     computeData.setDescription(COMPUTE1_ID + " desc updated");
212     computeEntity.setComputeCompositionData(computeData);
213
214     CompositionEntityValidationData output = computeManager.updateCompute(computeEntity);
215
216     Assert.assertEquals(output.getErrors(), toBeReturned.getErrors());
217   }
218
219   @Test
220   public void testUpdateManualComputeQuestionnaire() throws Exception {
221     String json = "{\"md5\" :\"FFDSD33SS\"}";
222     doReturn(true).when(vspInfoDao).isManual(any(), any());
223     doReturn(new ComputeEntity(null, null, null, null)).when(computeDao).get(any());
224
225     computeManager
226         .updateComputeQuestionnaire(VSP_ID, VERSION, COMPONENT_ID, COMPUTE1_ID, json);
227     verify(computeDao).updateQuestionnaireData(VSP_ID, VERSION, COMPONENT_ID, COMPUTE1_ID, json);
228   }
229
230   @Test
231   public void testGetNonExistingComputeId_negative() {
232     testGet_negative(VSP_ID, VERSION, COMPONENT_ID, "non existing compute id",
233         VersioningErrorCodes.VERSIONABLE_SUB_ENTITY_NOT_FOUND);
234   }
235
236   @Test
237   public void testGet() {
238     ComputeEntity expected = createCompute(VSP_ID, VERSION, COMPONENT_ID, COMPUTE1_ID);
239     doReturn(expected).when(computeDao).get(any());
240     String compositionSchema = "schema string";
241     doReturn(compositionSchema).when(computeManager).getComputeCompositionSchema(any());
242
243     CompositionEntityResponse<ComputeData> response =
244         computeManager.getCompute(VSP_ID, VERSION, COMPONENT_ID, COMPUTE1_ID);
245     Assert.assertEquals(response.getId(), expected.getId());
246     Assert
247         .assertEquals(response.getData().getName(), expected.getComputeCompositionData().getName());
248     Assert.assertEquals(response.getData().getDescription(), expected.getComputeCompositionData().
249         getDescription());
250     Assert.assertEquals(response.getSchema(), compositionSchema);
251   }
252
253   @Test
254   public void testGetQuestionnaire() throws Exception {
255     ComputeEntity compute = new ComputeEntity(VSP_ID, VERSION, COMPONENT_ID, COMPUTE1_ID);
256     compute.setQuestionnaireData("{}");
257     doReturn(compute).when(computeDao)
258         .getQuestionnaireData(VSP_ID, VERSION, COMPONENT_ID, COMPUTE1_ID);
259
260     String schema = "schema string";
261
262     doReturn(schema).when(computeManager).getComputeQuestionnaireSchema(any());
263
264     QuestionnaireResponse questionnaire =
265         computeManager.getComputeQuestionnaire(VSP_ID, VERSION, COMPONENT_ID, COMPUTE1_ID);
266
267     Assert.assertNotNull(questionnaire);
268     Assert.assertEquals(questionnaire.getData(), compute.getQuestionnaireData());
269     Assert.assertEquals(questionnaire.getSchema(), schema);
270     Assert.assertNull(questionnaire.getErrorMessage());
271   }
272
273   @Test
274   public void testDeleteOnNotManualCompute() {
275     ComputeEntity expected = createCompute(VSP_ID, VERSION, COMPONENT_ID, COMPUTE1_ID);
276     doReturn(expected).when(computeDao).get(any());
277     testDelete_negative(VSP_ID, VERSION, COMPONENT_ID, COMPUTE1_ID,
278         VendorSoftwareProductErrorCodes.VSP_COMPOSITION_EDIT_NOT_ALLOWED);
279   }
280
281   @Test
282   public void testDeleteOnManualCompute() {
283     ComputeEntity expected = createCompute(VSP_ID, VERSION, COMPONENT_ID, COMPUTE1_ID);
284     doReturn(expected).when(computeDao).get(any());
285     doReturn(true).when(vspInfoDao).isManual(any(), any());
286     doNothing().when(computeManager).deleteUniqueValue(VSP_ID, VERSION, COMPONENT_ID,
287         expected.getComputeCompositionData().getName());
288
289     computeManager.deleteCompute(VSP_ID, VERSION, COMPONENT_ID, COMPUTE1_ID);
290     verify(computeDao).delete(any());
291   }
292
293   @Test
294   public void testDeleteOnNotExistCompute() {
295     testDelete_negative(VSP_ID, VERSION, COMPONENT_ID, COMPUTE1_ID,
296         VendorSoftwareProductErrorCodes.VSP_COMPOSITION_EDIT_NOT_ALLOWED);
297   }
298
299
300   private void testDelete_negative(String vspId, Version version, String componentId,
301                                    String computeId,
302                                    String expectedErrorCode) {
303     try {
304       computeManager.deleteCompute(vspId, version, componentId, computeId);
305       Assert.fail();
306     } catch (CoreException exception) {
307       Assert.assertEquals(exception.code().id(), expectedErrorCode);
308     }
309   }
310
311   private void testGet_negative(String vspId, Version version, String componentId, String computeId,
312                                 String expectedErrorCode) {
313     try {
314       computeManager.getCompute(vspId, version, componentId, computeId);
315       Assert.fail();
316     } catch (CoreException exception) {
317       Assert.assertEquals(exception.code().id(), expectedErrorCode);
318     }
319   }
320
321   private void testList_negative(String vspId, Version version, String componentId,
322                                  String expectedErrorCode, String expectedErrorMsg) {
323     try {
324       computeManager.listComputes(vspId, version, componentId);
325       Assert.fail();
326     } catch (CoreException exception) {
327       Assert.assertEquals(exception.code().id(), expectedErrorCode);
328       Assert.assertEquals(exception.getMessage(), expectedErrorMsg);
329     }
330   }
331
332
333   private void testUpdate_negative(String vspId, Version version, String componentId,
334                                    String computeId, String expectedErrorCode) {
335     try {
336       computeManager.updateCompute(new ComputeEntity(vspId, version, componentId, computeId));
337       Assert.fail();
338     } catch (CoreException exception) {
339       Assert.assertEquals(exception.code().id(), expectedErrorCode);
340     }
341   }
342
343   private void testCreate_negative(ComputeEntity computeEntity1, String expectedErrorCode) {
344     try {
345       computeManager.createCompute(computeEntity1);
346       Assert.fail();
347     } catch (CoreException exception) {
348       Assert.assertEquals(exception.code().id(), expectedErrorCode);
349     }
350   }
351
352   private static ComputeEntity createCompute(String vspId, Version version, String compId,
353                                              String computeId) {
354     ComputeEntity computeEntity1 = new ComputeEntity(vspId, version, compId, computeId);
355     ComputeData computeData = new ComputeData();
356     computeData.setName(computeId + "name");
357     computeData.setDescription(computeId + "desc");
358     computeEntity1.setComputeCompositionData(computeData);
359     return computeEntity1;
360   }
361 }