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