243da26152f4d03aa6a2a820bb4bde175241cda0
[sdc.git] /
1 package org.openecomp.sdc.vendorsoftwareproduct.impl;
2
3 import org.mockito.*;
4 import org.openecomp.sdc.common.errors.CoreException;
5 import org.openecomp.sdc.vendorsoftwareproduct.NicManager;
6 import org.openecomp.sdc.vendorsoftwareproduct.dao.ComponentDao;
7 import org.openecomp.sdc.vendorsoftwareproduct.dao.VendorSoftwareProductInfoDao;
8 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.ComponentEntity;
9 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.NicEntity;
10 import org.openecomp.sdc.vendorsoftwareproduct.errors.VendorSoftwareProductErrorCodes;
11 import org.openecomp.sdc.vendorsoftwareproduct.CompositionEntityDataManager;
12 import org.openecomp.sdc.vendorsoftwareproduct.types.CompositionEntityResponse;
13 import org.openecomp.sdc.vendorsoftwareproduct.types.QuestionnaireResponse;
14 import org.openecomp.sdc.vendorsoftwareproduct.types.composition.ComponentData;
15 import org.openecomp.sdc.vendorsoftwareproduct.types.composition.CompositionEntityType;
16 import org.openecomp.sdc.vendorsoftwareproduct.types.composition.CompositionEntityValidationData;
17 import org.openecomp.sdc.vendorsoftwareproduct.types.composition.Nic;
18 import org.openecomp.sdc.versioning.dao.types.Version;
19 import org.openecomp.sdc.versioning.errors.VersioningErrorCodes;
20 import org.testng.Assert;
21 import org.testng.annotations.AfterMethod;
22 import org.testng.annotations.BeforeMethod;
23 import org.testng.annotations.Test;
24
25 import java.util.ArrayList;
26 import java.util.Arrays;
27 import java.util.Collection;
28
29 import static org.mockito.Matchers.anyObject;
30 import static org.mockito.Mockito.doReturn;
31 import static org.mockito.Mockito.never;
32 import static org.mockito.Mockito.verify;
33
34 public class ComponentManagerImplTest {
35   private static final String VSP_ID = "VSP_ID";
36   private static final Version VERSION = new Version("version_id");
37   private static final String COMP1_ID = "comp1";
38   private static final String COMP2_ID = "comp2";
39   private static final String COMP_NOT_EXIST_MSG =
40       "Vendor Software Product Component with Id comp1 does not exist " +
41           "for Vendor Software Product with id VSP_ID and version version_id";
42
43   @Mock
44   private ComponentDao componentDaoMock;
45   @Mock
46   private CompositionEntityDataManager compositionEntityDataManagerMock;
47   @Mock
48   private NicManager nicManagerMock;
49   @Mock
50   private VendorSoftwareProductInfoDao vspInfoDao;
51   @InjectMocks
52   @Spy
53   private ComponentManagerImpl componentManager;
54
55   @BeforeMethod
56   public void setUp() throws Exception {
57     MockitoAnnotations.initMocks(this);
58   }
59
60   @AfterMethod
61   public void tearDown() {
62     componentManager = null;
63   }
64
65   @Test
66   public void testListWhenNone() {
67     Collection<ComponentEntity> components = componentManager.listComponents(VSP_ID, VERSION);
68     Assert.assertEquals(components.size(), 0);
69   }
70
71   @Test(expectedExceptions = CoreException.class,
72       expectedExceptionsMessageRegExp = COMP_NOT_EXIST_MSG)
73   public void validateExceptionWhenTryingToRetriveNotExistingComponentEntity() {
74     doReturn(null).when(componentDaoMock).get(anyObject());
75     componentManager.validateComponentExistence(VSP_ID, VERSION, COMP1_ID);
76   }
77
78   @Test
79   public void testList() {
80     doReturn(Arrays.asList(
81         createComponent(VSP_ID, VERSION, COMP1_ID),
82         createComponent(VSP_ID, VERSION, COMP2_ID)))
83         .when(componentDaoMock).list(anyObject());
84
85     Collection<ComponentEntity> actual = componentManager.listComponents(VSP_ID, VERSION);
86     Assert.assertEquals(actual.size(), 2);
87   }
88
89   @Test
90   public void testDeleteListOnUploadVsp_negative() {
91     testDeleteList_negative(VSP_ID, VERSION,
92         VendorSoftwareProductErrorCodes.VSP_COMPOSITION_EDIT_NOT_ALLOWED);
93   }
94
95   @Test
96   public void testCreate() {
97     ComponentEntity expected = new ComponentEntity(VSP_ID, null, null);
98     ComponentData compData = new ComponentData();
99     compData.setName("comp1 name");
100     compData.setDescription("comp1 desc");
101     expected.setComponentCompositionData(compData);
102
103     doReturn(true).when(vspInfoDao).isManual(anyObject(), anyObject());
104     Collection<ComponentEntity> vspComponentList = new ArrayList<>();
105     doReturn(vspComponentList).when(componentDaoMock).list(anyObject());
106     doReturn(expected).when(compositionEntityDataManagerMock).createComponent(anyObject(), Matchers.anyBoolean());
107
108     ComponentEntity created = componentManager.createComponent(expected);
109     Assert.assertNotNull(created);
110     //expected.setId(created.getId());
111     //expected.setVersion(VERSION);
112
113     //ComponentEntity actual = componentDaoMock.getComponent(VSP_ID, VERSION, created.getId());
114
115     //Assert.assertEquals(actual, expected);
116     //return created.getId();
117   }
118
119   @Test
120   public void testCreateWithVspCompListMoreThanOne() {
121     ComponentEntity expected = new ComponentEntity(VSP_ID, null, null);
122     ComponentData compData = new ComponentData();
123     compData.setName("comp1 name");
124     compData.setDescription("comp1 desc");
125     expected.setComponentCompositionData(compData);
126
127     doReturn(true).when(vspInfoDao).isManual(anyObject(), anyObject());
128     Collection<ComponentEntity> vspComponentList = new ArrayList<>();
129     vspComponentList.add(expected);
130     doReturn(vspComponentList).when(componentDaoMock).list(anyObject());
131
132     try {
133        componentManager.createComponent(expected);
134     } catch (CoreException exception) {
135       Assert.assertEquals("Creation of only one VFC per VSP allowed.", exception.code().message());
136       Assert.assertEquals(VendorSoftwareProductErrorCodes.VSP_VFC_COUNT_EXCEED,
137           exception.code().id());
138     }
139   }
140
141   @Test
142   public void testUpdateComp() {
143     ComponentEntity expected = new ComponentEntity(VSP_ID, null, COMP1_ID);
144     ComponentData compData = new ComponentData();
145     compData.setName("comp1 name");
146     compData.setDescription("comp1 desc");
147     expected.setComponentCompositionData(compData);
148
149     doReturn(expected).when(componentDaoMock).get(anyObject());
150     doReturn(true).when(vspInfoDao).isManual(anyObject(), anyObject());
151     Collection<ComponentEntity> vspComponentList = new ArrayList<>();
152     vspComponentList.add(expected);
153     doReturn(vspComponentList).when(componentDaoMock).list(anyObject());
154     doReturn(new CompositionEntityValidationData(null, null)).when(compositionEntityDataManagerMock)
155         .validateEntity(anyObject(), anyObject(), anyObject());
156
157     CompositionEntityValidationData created = componentManager.updateComponent(expected);
158     Assert.assertNotNull(created);
159   }
160
161   @Test
162   public void testUpdateCompWithSameVfcDisplayName() {
163     ComponentEntity expected = new ComponentEntity(VSP_ID, null, COMP1_ID);
164     ComponentData compData = new ComponentData();
165     compData.setName("comp1 name");
166     compData.setDescription("comp1 desc");
167     compData.setDisplayName("comp1 displayname");
168     expected.setComponentCompositionData(compData);
169
170     doReturn(expected).when(componentDaoMock).get(anyObject());
171     doReturn(true).when(vspInfoDao).isManual(anyObject(), anyObject());
172     Collection<ComponentEntity> vspComponentList = new ArrayList<>();
173     vspComponentList.add(expected);
174     ComponentEntity expected2 = new ComponentEntity(VSP_ID + "2", null, COMP1_ID + "2");
175     expected2.setComponentCompositionData(compData);
176     vspComponentList.add(expected2);
177     doReturn(vspComponentList).when(componentDaoMock).list(anyObject());
178     doReturn(new CompositionEntityValidationData(null, null)).when(compositionEntityDataManagerMock)
179         .validateEntity(anyObject(), anyObject(), anyObject());
180
181     try {
182        componentManager.updateComponent(expected);
183     } catch (CoreException exception) {
184       Assert.assertEquals("VFC with specified name already present in given VSP.",
185           exception.code().message());
186       Assert.assertEquals(VendorSoftwareProductErrorCodes.VSP_VFC_DUPLICATE_NAME,
187           exception.code().id());
188     }
189   }
190
191 /*    @Test
192     public void testCreateWithExistingName_negative() {
193         ComponentEntity component = new ComponentEntity(VSP_ID, null, null);
194         ComponentData compData = new ComponentData();
195         compData.setName("comp1 name");
196         compData.setDescription("comp1 desc");
197         component.setComponentCompositionData(compData);
198         testCreate_negative(component, USER, UniqueValueUtil.UNIQUE_VALUE_VIOLATION);
199     }*/
200
201 /*    @Test
202     public void testCreateWithExistingNameUnderOtherVsp() {
203         testCreate(vsp2Id);
204     }*/
205
206   @Test
207   public void testCreateOnUploadVsp_negative() {
208     testCreate_negative(new ComponentEntity(VSP_ID, VERSION, null),
209         VendorSoftwareProductErrorCodes.VFC_ADD_NOT_ALLOWED_IN_HEAT_ONBOARDING);
210   }
211
212   @Test
213   public void testUpdateNonExistingComponentId_negative() {
214     String componentId = "non existing component id";
215     doReturn(null).when(componentDaoMock).get(anyObject());
216
217     testUpdate_negative(VSP_ID, VERSION, componentId,
218         VersioningErrorCodes.VERSIONABLE_SUB_ENTITY_NOT_FOUND);
219   }
220
221   @Test
222   public void testUpdateOnUploadVsp() {
223     doReturn(createComponent(VSP_ID, VERSION, COMP1_ID)).when(componentDaoMock)
224         .get(anyObject());
225
226     doReturn(new CompositionEntityValidationData(CompositionEntityType.component, COMP1_ID))
227         .when(compositionEntityDataManagerMock)
228         .validateEntity(anyObject(), anyObject(), anyObject());
229
230     ComponentEntity component = new ComponentEntity(VSP_ID, VERSION, COMP1_ID);
231     ComponentData compData = new ComponentData();
232     compData.setName(COMP1_ID + " name");                // no change
233     compData.setDisplayName(COMP1_ID + " display name"); // no change
234     compData.setDescription(COMP1_ID + " desc updated"); // allowed change
235     component.setComponentCompositionData(compData);
236
237
238     CompositionEntityValidationData validationData =
239         componentManager.updateComponent(component);
240     Assert.assertTrue(validationData == null || validationData.getErrors() == null);
241     verify(componentDaoMock).update(component);
242   }
243
244   @Test
245   public void testIllegalUpdateOnUploadVsp() {
246     doReturn(createComponent(VSP_ID, VERSION, COMP1_ID))
247         .when(componentDaoMock).get(anyObject());
248
249     CompositionEntityValidationData toBeReturned =
250         new CompositionEntityValidationData(CompositionEntityType.component, COMP1_ID);
251     toBeReturned.setErrors(Arrays.asList("error1", "error2"));
252     doReturn(toBeReturned)
253         .when(compositionEntityDataManagerMock)
254         .validateEntity(anyObject(), anyObject(), anyObject());
255
256     ComponentEntity component = new ComponentEntity(VSP_ID, VERSION, COMP1_ID);
257     ComponentData compData = new ComponentData();
258     compData.setName("comp1 name updated");// not allowed: changed name + omitted display name
259     component.setComponentCompositionData(compData);
260
261     CompositionEntityValidationData validationData =
262         componentManager.updateComponent(component);
263     Assert.assertNotNull(validationData);
264     Assert.assertEquals(validationData.getErrors().size(), 2);
265
266     verify(componentDaoMock, never()).update(component);
267   }
268
269   @Test
270   public void testGetNonExistingComponentId_negative() {
271     String componentId = "non existing component id";
272     doReturn(null).when(componentDaoMock).get(anyObject());
273
274     testGet_negative(VSP_ID, VERSION, componentId,
275         VersioningErrorCodes.VERSIONABLE_SUB_ENTITY_NOT_FOUND);
276   }
277
278   @Test
279   public void testGet() {
280     ComponentEntity expected = createComponent(VSP_ID, VERSION, COMP1_ID);
281     doReturn(expected).when(componentDaoMock).get(anyObject());
282
283     doReturn("schema string").when(componentManager).getComponentCompositionSchema(anyObject());
284
285     testGet(VSP_ID, VERSION, COMP1_ID, expected);
286   }
287
288
289
290
291 /*
292     @Test(dependsOnMethods = {"testUpdateOnUploadVsp", "testList"})
293     public void testCreateWithERemovedName() {
294         testCreate(VSP_ID);
295     }
296
297     @Test(dependsOnMethods = "testList")
298     public void testDeleteNonExistingComponentId_negative() {
299         testDelete_negative(VSP_ID, "non existing component id", USER, VersioningErrorCodes.VERSIONABLE_SUB_ENTITY_NOT_FOUND);
300     }*/
301
302
303
304 /*
305     @Test(dependsOnMethods = "testList")
306     public void testDelete() {
307         componentManager.deleteComponent(VSP_ID, COMP1_ID, USER);
308         ComponentEntity actual = componentDaoMock.getComponent(VSP_ID, VERSION, COMP1_ID);
309         Assert.assertNull(actual);
310     }*/
311
312   @Test
313   public void testDeleteOnUploadVsp_negative() {
314     testDelete_negative(VSP_ID, VERSION, COMP1_ID,
315         VendorSoftwareProductErrorCodes.VSP_COMPOSITION_EDIT_NOT_ALLOWED);
316   }
317
318   @Test(expectedExceptions = CoreException.class,
319       expectedExceptionsMessageRegExp = COMP_NOT_EXIST_MSG)
320   public void testGetNonExistingComponentQuestionnaire() throws Exception {
321     componentManager.getQuestionnaire(VSP_ID, VERSION, COMP1_ID);
322   }
323
324   @Test
325   public void testComponentNullQuestionnaire() {
326     doReturn(new ComponentEntity(VSP_ID, VERSION, COMP1_ID)).when(componentDaoMock)
327         .getQuestionnaireData(VSP_ID, VERSION, COMP1_ID);
328     String schema = "schema string";
329     doReturn(schema).when(componentManager).getComponentQuestionnaireSchema(anyObject());
330
331     QuestionnaireResponse questionnaire =
332         componentManager.getQuestionnaire(VSP_ID, VERSION, COMP1_ID);
333     Assert.assertNotNull(questionnaire);
334     Assert.assertNull(questionnaire.getData());
335     Assert.assertEquals(questionnaire.getSchema(), schema);
336     Assert.assertNull(questionnaire.getErrorMessage());
337   }
338
339
340   @Test
341   public void testGetQuestionnaire() throws Exception {
342     ComponentEntity component = new ComponentEntity(VSP_ID, VERSION, COMP1_ID);
343     component.setQuestionnaireData("{}");
344     doReturn(component).when(componentDaoMock).getQuestionnaireData(VSP_ID, VERSION, COMP1_ID);
345
346     NicEntity nicEntity1 = new NicEntity();
347     Nic nic1 = new Nic();
348     nic1.setName("nic1");
349     nicEntity1.setNicCompositionData(nic1);
350
351     NicEntity nicEntity2 = new NicEntity();
352     Nic nic2 = new Nic();
353     nic2.setName("nic2");
354     nicEntity2.setNicCompositionData(nic2);
355
356     doReturn(Arrays.asList(nicEntity1, nicEntity2))
357         .when(nicManagerMock).listNics(VSP_ID, VERSION, COMP1_ID);
358
359     String schema = "schema string";
360     doReturn(schema).when(componentManager).getComponentQuestionnaireSchema(anyObject());
361
362     QuestionnaireResponse questionnaire =
363         componentManager.getQuestionnaire(VSP_ID, VERSION, COMP1_ID);
364     Assert.assertNotNull(questionnaire);
365     Assert.assertEquals(questionnaire.getData(), component.getQuestionnaireData());
366     Assert.assertEquals(questionnaire.getSchema(), schema);
367     Assert.assertNull(questionnaire.getErrorMessage());
368   }
369
370   @Test(expectedExceptions = CoreException.class,
371       expectedExceptionsMessageRegExp = COMP_NOT_EXIST_MSG)
372   public void testUpdateNonExistingComponentQuestionnaire() throws Exception {
373     doReturn(null).when(componentDaoMock).get(anyObject());
374     componentManager.updateQuestionnaire(VSP_ID, VERSION, COMP1_ID, "questionnaire data");
375   }
376
377   @Test
378   public void testUpdateQuestionnaire() throws Exception {
379     ComponentEntity component = createComponent(VSP_ID, VERSION, COMP1_ID);
380     doReturn(component).when(componentDaoMock).get(anyObject());
381
382     componentManager.updateQuestionnaire(VSP_ID, VERSION, COMP1_ID, "questionnaire data");
383
384     verify(componentDaoMock)
385         .updateQuestionnaireData(VSP_ID, VERSION, COMP1_ID, "questionnaire data");
386   }
387
388   private void testGet(String vspId, Version version, String componentId,
389                        ComponentEntity expected) {
390
391     CompositionEntityResponse<ComponentData>
392         response = componentManager.getComponent(vspId, version, componentId);
393     Assert.assertEquals(response.getId(), expected.getId());
394     Assert.assertEquals(response.getData(), expected.getComponentCompositionData());
395     Assert.assertNotNull(response.getSchema());
396   }
397
398   private void testCreate_negative(ComponentEntity component,
399                                    String expectedErrorCode) {
400     try {
401       componentManager.createComponent(component);
402       Assert.fail();
403     } catch (CoreException exception) {
404       Assert.assertEquals(exception.code().id(), expectedErrorCode);
405     }
406   }
407
408   private void testGet_negative(String vspId, Version version, String componentId,
409                                 String expectedErrorCode) {
410     try {
411       componentManager.getComponent(vspId, version, componentId);
412       Assert.fail();
413     } catch (CoreException exception) {
414       Assert.assertEquals(exception.code().id(), expectedErrorCode);
415     }
416   }
417
418   private void testUpdate_negative(String vspId, Version version, String componentId,
419                                    String expectedErrorCode) {
420     try {
421       componentManager.updateComponent(new ComponentEntity(vspId, version, componentId));
422       Assert.fail();
423     } catch (CoreException exception) {
424       Assert.assertEquals(exception.code().id(), expectedErrorCode);
425     }
426   }
427
428   private void testDeleteList_negative(String vspId, Version version,
429                                        String expectedErrorCode) {
430     try {
431       componentManager.deleteComponents(vspId, version);
432       Assert.fail();
433     } catch (CoreException exception) {
434       Assert.assertEquals(exception.code().id(), expectedErrorCode);
435     }
436   }
437
438   private void testDelete_negative(String vspId, Version version, String componentId,
439                                    String expectedErrorCode) {
440     try {
441       componentManager.deleteComponent(vspId, version, componentId);
442       Assert.fail();
443     } catch (CoreException exception) {
444       Assert.assertEquals(exception.code().id(), expectedErrorCode);
445     }
446   }
447
448
449   private static ComponentEntity createComponent(String vspId, Version version, String compId) {
450     ComponentEntity componentEntity = new ComponentEntity(vspId, version, compId);
451     ComponentData compData = new ComponentData();
452     compData.setName(compId + " name");
453     compData.setDisplayName(compId + " display name");
454     compData.setDescription(compId + " desc");
455     componentEntity.setComponentCompositionData(compData);
456     componentEntity.setQuestionnaireData("{}");
457     return componentEntity;
458   }
459 }