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