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