047015e16abd2184ec46a0fd3efcc1b15b616bbc
[sdc.git] /
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.AfterMethod;
25 import org.testng.annotations.BeforeMethod;
26 import org.testng.annotations.Test;
27
28 import java.util.ArrayList;
29 import java.util.Arrays;
30 import java.util.Collection;
31
32 import static org.mockito.Matchers.anyObject;
33 import static org.mockito.Mockito.doReturn;
34 import static org.mockito.Mockito.never;
35 import static org.mockito.Mockito.verify;
36
37 public class ComponentManagerImplTest {
38   private static final String VSP_ID = "VSP_ID";
39   private static final Version VERSION = new Version("version_id");
40   private static final String COMP1_ID = "comp1";
41   private static final String COMP2_ID = "comp2";
42   private static final String COMP_NOT_EXIST_MSG =
43       "Vendor Software Product Component with Id comp1 does not exist " +
44           "for Vendor Software Product with id VSP_ID and version version_id";
45
46   @Mock
47   private ComponentDao componentDaoMock;
48   @Mock
49   private CompositionEntityDataManager compositionEntityDataManagerMock;
50   @Mock
51   private NicManager nicManagerMock;
52   @Mock
53   private VendorSoftwareProductInfoDao vspInfoDao;
54   @InjectMocks
55   @Spy
56   private ComponentManagerImpl componentManager;
57
58   @BeforeMethod
59   public void setUp() throws Exception {
60     MockitoAnnotations.initMocks(this);
61   }
62
63   @AfterMethod
64   public void tearDown() {
65     componentManager = null;
66   }
67
68   @Test
69   public void testListWhenNone() {
70     Collection<ComponentEntity> components = componentManager.listComponents(VSP_ID, VERSION);
71     Assert.assertEquals(components.size(), 0);
72   }
73
74   @Test(expectedExceptions = CoreException.class,
75       expectedExceptionsMessageRegExp = COMP_NOT_EXIST_MSG)
76   public void validateExceptionWhenTryingToRetriveNotExistingComponentEntity() {
77     doReturn(null).when(componentDaoMock).get(anyObject());
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(anyObject());
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(anyObject(), anyObject());
107     Collection<ComponentEntity> vspComponentList = new ArrayList<>();
108     doReturn(vspComponentList).when(componentDaoMock).list(anyObject());
109     doReturn(expected).when(compositionEntityDataManagerMock).createComponent(anyObject());
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(anyObject(), anyObject());
131     Collection<ComponentEntity> vspComponentList = new ArrayList<>();
132     vspComponentList.add(expected);
133     doReturn(vspComponentList).when(componentDaoMock).list(anyObject());
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(anyObject());
153     doReturn(true).when(vspInfoDao).isManual(anyObject(), anyObject());
154     Collection<ComponentEntity> vspComponentList = new ArrayList<>();
155     vspComponentList.add(expected);
156     doReturn(vspComponentList).when(componentDaoMock).list(anyObject());
157     doReturn(new CompositionEntityValidationData(null, null)).when(compositionEntityDataManagerMock)
158         .validateEntity(anyObject(), anyObject(), anyObject());
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(anyObject());
174     doReturn(true).when(vspInfoDao).isManual(anyObject(), anyObject());
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(anyObject());
181     doReturn(new CompositionEntityValidationData(null, null)).when(compositionEntityDataManagerMock)
182         .validateEntity(anyObject(), anyObject(), anyObject());
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(anyObject());
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(anyObject());
228
229     doReturn(new CompositionEntityValidationData(CompositionEntityType.component, COMP1_ID))
230         .when(compositionEntityDataManagerMock)
231         .validateEntity(anyObject(), anyObject(), anyObject());
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.setVfcCode(COMP1_ID + " display name"); // no change
238     compData.setNfcCode(COMP1_ID + " display name"); // no change
239     compData.setNfcFunction(COMP1_ID + " display name"); // no change
240     compData.setDescription(COMP1_ID + " desc updated"); // allowed change
241     component.setComponentCompositionData(compData);
242
243
244     CompositionEntityValidationData validationData =
245         componentManager.updateComponent(component);
246     Assert.assertTrue(validationData == null || validationData.getErrors() == null);
247     verify(componentDaoMock).update(component);
248   }
249
250   @Test
251   public void testIllegalUpdateOnUploadVsp() {
252     doReturn(createComponent(VSP_ID, VERSION, COMP1_ID))
253         .when(componentDaoMock).get(anyObject());
254
255     CompositionEntityValidationData toBeReturned =
256         new CompositionEntityValidationData(CompositionEntityType.component, COMP1_ID);
257     toBeReturned.setErrors(Arrays.asList("error1", "error2"));
258     doReturn(toBeReturned)
259         .when(compositionEntityDataManagerMock)
260         .validateEntity(anyObject(), anyObject(), anyObject());
261
262     ComponentEntity component = new ComponentEntity(VSP_ID, VERSION, COMP1_ID);
263     ComponentData compData = new ComponentData();
264     compData.setName("comp1 name updated");// not allowed: changed name + omitted display name
265     component.setComponentCompositionData(compData);
266
267     CompositionEntityValidationData validationData =
268         componentManager.updateComponent(component);
269     Assert.assertNotNull(validationData);
270     Assert.assertEquals(validationData.getErrors().size(), 2);
271
272     verify(componentDaoMock, never()).update(component);
273   }
274
275   @Test
276   public void testGetNonExistingComponentId_negative() {
277     String componentId = "non existing component id";
278     doReturn(null).when(componentDaoMock).get(anyObject());
279
280     testGet_negative(VSP_ID, VERSION, componentId,
281         VersioningErrorCodes.VERSIONABLE_SUB_ENTITY_NOT_FOUND);
282   }
283
284   @Test
285   public void testGet() {
286     ComponentEntity expected = createComponent(VSP_ID, VERSION, COMP1_ID);
287     doReturn(expected).when(componentDaoMock).get(anyObject());
288
289     doReturn("schema string").when(componentManager).getComponentCompositionSchema(anyObject());
290
291     testGet(VSP_ID, VERSION, COMP1_ID, expected);
292   }
293
294
295
296
297 /*
298     @Test(dependsOnMethods = {"testUpdateOnUploadVsp", "testList"})
299     public void testCreateWithERemovedName() {
300         testCreate(VSP_ID);
301     }
302
303     @Test(dependsOnMethods = "testList")
304     public void testDeleteNonExistingComponentId_negative() {
305         testDelete_negative(VSP_ID, "non existing component id", USER, VersioningErrorCodes.VERSIONABLE_SUB_ENTITY_NOT_FOUND);
306     }*/
307
308
309
310 /*
311     @Test(dependsOnMethods = "testList")
312     public void testDelete() {
313         componentManager.deleteComponent(VSP_ID, COMP1_ID, USER);
314         ComponentEntity actual = componentDaoMock.getComponent(VSP_ID, VERSION, COMP1_ID);
315         Assert.assertNull(actual);
316     }*/
317
318   @Test
319   public void testDeleteOnUploadVsp_negative() {
320     testDelete_negative(VSP_ID, VERSION, COMP1_ID,
321         VendorSoftwareProductErrorCodes.VSP_COMPOSITION_EDIT_NOT_ALLOWED);
322   }
323
324   @Test(expectedExceptions = CoreException.class,
325       expectedExceptionsMessageRegExp = COMP_NOT_EXIST_MSG)
326   public void testGetNonExistingComponentQuestionnaire() throws Exception {
327     componentManager.getQuestionnaire(VSP_ID, VERSION, COMP1_ID);
328   }
329
330   @Test
331   public void testComponentNullQuestionnaire() {
332     doReturn(new ComponentEntity(VSP_ID, VERSION, COMP1_ID)).when(componentDaoMock)
333         .getQuestionnaireData(VSP_ID, VERSION, COMP1_ID);
334     String schema = "schema string";
335     doReturn(schema).when(componentManager).getComponentQuestionnaireSchema(anyObject());
336
337     QuestionnaireResponse questionnaire =
338         componentManager.getQuestionnaire(VSP_ID, VERSION, COMP1_ID);
339     Assert.assertNotNull(questionnaire);
340     Assert.assertNull(questionnaire.getData());
341     Assert.assertEquals(questionnaire.getSchema(), schema);
342     Assert.assertNull(questionnaire.getErrorMessage());
343   }
344
345
346   @Test
347   public void testGetQuestionnaire() throws Exception {
348     ComponentEntity component = new ComponentEntity(VSP_ID, VERSION, COMP1_ID);
349     component.setQuestionnaireData("{}");
350     doReturn(component).when(componentDaoMock).getQuestionnaireData(VSP_ID, VERSION, COMP1_ID);
351
352     NicEntity nicEntity1 = new NicEntity();
353     Nic nic1 = new Nic();
354     nic1.setName("nic1");
355     nicEntity1.setNicCompositionData(nic1);
356
357     NicEntity nicEntity2 = new NicEntity();
358     Nic nic2 = new Nic();
359     nic2.setName("nic2");
360     nicEntity2.setNicCompositionData(nic2);
361
362     doReturn(Arrays.asList(nicEntity1, nicEntity2))
363         .when(nicManagerMock).listNics(VSP_ID, VERSION, COMP1_ID);
364
365     String schema = "schema string";
366     doReturn(schema).when(componentManager).getComponentQuestionnaireSchema(anyObject());
367
368     QuestionnaireResponse questionnaire =
369         componentManager.getQuestionnaire(VSP_ID, VERSION, COMP1_ID);
370     Assert.assertNotNull(questionnaire);
371     Assert.assertEquals(questionnaire.getData(), component.getQuestionnaireData());
372     Assert.assertEquals(questionnaire.getSchema(), schema);
373     Assert.assertNull(questionnaire.getErrorMessage());
374   }
375
376   @Test(expectedExceptions = CoreException.class,
377       expectedExceptionsMessageRegExp = COMP_NOT_EXIST_MSG)
378   public void testUpdateNonExistingComponentQuestionnaire() throws Exception {
379     doReturn(null).when(componentDaoMock).get(anyObject());
380     componentManager.updateQuestionnaire(VSP_ID, VERSION, COMP1_ID, "questionnaire data");
381   }
382
383   @Test
384   public void testUpdateQuestionnaire() throws Exception {
385     ComponentEntity component = createComponent(VSP_ID, VERSION, COMP1_ID);
386     doReturn(component).when(componentDaoMock).get(anyObject());
387
388     componentManager.updateQuestionnaire(VSP_ID, VERSION, COMP1_ID, "questionnaire data");
389
390     verify(componentDaoMock)
391         .updateQuestionnaireData(VSP_ID, VERSION, COMP1_ID, "questionnaire data");
392   }
393
394   private void testGet(String vspId, Version version, String componentId,
395                        ComponentEntity expected) {
396
397     CompositionEntityResponse<ComponentData>
398         response = componentManager.getComponent(vspId, version, componentId);
399     Assert.assertEquals(response.getId(), expected.getId());
400     Assert.assertEquals(response.getData(), expected.getComponentCompositionData());
401     Assert.assertNotNull(response.getSchema());
402   }
403
404   private void testCreate_negative(ComponentEntity component,
405                                    String expectedErrorCode) {
406     try {
407       componentManager.createComponent(component);
408       Assert.fail();
409     } catch (CoreException exception) {
410       Assert.assertEquals(exception.code().id(), expectedErrorCode);
411     }
412   }
413
414   private void testGet_negative(String vspId, Version version, String componentId,
415                                 String expectedErrorCode) {
416     try {
417       componentManager.getComponent(vspId, version, componentId);
418       Assert.fail();
419     } catch (CoreException exception) {
420       Assert.assertEquals(exception.code().id(), expectedErrorCode);
421     }
422   }
423
424   private void testUpdate_negative(String vspId, Version version, String componentId,
425                                    String expectedErrorCode) {
426     try {
427       componentManager.updateComponent(new ComponentEntity(vspId, version, componentId));
428       Assert.fail();
429     } catch (CoreException exception) {
430       Assert.assertEquals(exception.code().id(), expectedErrorCode);
431     }
432   }
433
434   private void testDeleteList_negative(String vspId, Version version,
435                                        String expectedErrorCode) {
436     try {
437       componentManager.deleteComponents(vspId, version);
438       Assert.fail();
439     } catch (CoreException exception) {
440       Assert.assertEquals(exception.code().id(), expectedErrorCode);
441     }
442   }
443
444   private void testDelete_negative(String vspId, Version version, String componentId,
445                                    String expectedErrorCode) {
446     try {
447       componentManager.deleteComponent(vspId, version, componentId);
448       Assert.fail();
449     } catch (CoreException exception) {
450       Assert.assertEquals(exception.code().id(), expectedErrorCode);
451     }
452   }
453
454
455   private static ComponentEntity createComponent(String vspId, Version version, String compId) {
456     ComponentEntity componentEntity = new ComponentEntity(vspId, version, compId);
457     ComponentData compData = new ComponentData();
458     compData.setName(compId + " name");
459     compData.setDisplayName(compId + " display name");
460     compData.setVfcCode(compId + " display name");
461     compData.setDescription(compId + " desc");
462     componentEntity.setComponentCompositionData(compData);
463     componentEntity.setQuestionnaireData("{}");
464     return componentEntity;
465   }
466 }