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