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