push addional code
[sdc.git] / openecomp-be / backend / openecomp-sdc-vendor-software-product-manager / src / test / java / org / openecomp / sdc / vendorsoftwareproduct / ComponentsTest.java
1 package org.openecomp.sdc.vendorsoftwareproduct;
2
3 import org.openecomp.sdc.common.errors.CoreException;
4 import org.openecomp.sdc.vendorsoftwareproduct.dao.VendorSoftwareProductDaoFactory;
5 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.VspDetails;
6 import org.openecomp.sdc.vendorsoftwareproduct.errors.VendorSoftwareProductErrorCodes;
7 import org.openecomp.sdc.vendorsoftwareproduct.impl.VendorSoftwareProductManagerImpl;
8 import org.openecomp.sdc.vendorsoftwareproduct.types.CompositionEntityResponse;
9 import org.openecomp.sdc.vendorsoftwareproduct.types.CompositionEntityValidationData;
10 import org.openecomp.sdc.vendorsoftwareproduct.types.composition.ComponentData;
11 import org.openecomp.sdc.versioning.dao.types.Version;
12 import org.openecomp.sdc.versioning.errors.VersioningErrorCodes;
13 import org.openecomp.core.utilities.CommonMethods;
14
15 import org.testng.Assert;
16 import org.testng.annotations.BeforeClass;
17 import org.testng.annotations.Test;
18
19 import java.util.Collection;
20
21 public class ComponentsTest {
22
23   private static final String USER1 = "componentsTestUser1";
24   private static final String USER2 = "componentsTestUser2";
25   private static final Version VERSION01 = new Version(0, 1);
26   private static final VendorSoftwareProductManager vendorSoftwareProductManager =
27       new VendorSoftwareProductManagerImpl();
28   private static final org.openecomp.sdc.vendorsoftwareproduct.dao.VendorSoftwareProductDao
29       vendorSoftwareProductDao =
30       VendorSoftwareProductDaoFactory.getInstance().createInterface();
31
32   private static String vsp1Id;
33   private static String vsp2Id;
34   private static String comp1Id = "1";
35   private static String comp2Id = "2";
36
37   static org.openecomp.sdc.vendorsoftwareproduct.dao.type.ComponentEntity createComponent(String vspId, Version version, String compId) {
38     org.openecomp.sdc.vendorsoftwareproduct.dao.type.ComponentEntity
39         componentEntity = new org.openecomp.sdc.vendorsoftwareproduct.dao.type.ComponentEntity(vspId, version, compId);
40     ComponentData compData = new ComponentData();
41     compData.setName(compId + " name");
42     compData.setDisplayName(compId + " display name");
43     compData.setDescription(compId + " desc");
44     componentEntity.setComponentCompositionData(compData);
45     vendorSoftwareProductDao.createComponent(componentEntity);
46     return componentEntity;
47   }
48
49   @BeforeClass
50   private void init() {
51     VspDetails vsp1 = vendorSoftwareProductManager.createNewVsp(VSPCommon
52         .createVspDetails(null, null, "VSP_" + CommonMethods.nextUuId(), "Test-vsp1", "vendorName",
53             "vlm1Id", "icon", "category", "subCategory", "123", null), USER1);
54     vsp1Id = vsp1.getId();
55
56     VspDetails vsp2 = vendorSoftwareProductManager.createNewVsp(VSPCommon
57         .createVspDetails(null, null, "VSP_" + CommonMethods.nextUuId(), "Test-vsp2", "vendorName",
58             "vlm1Id", "icon", "category", "subCategory", "123", null), USER1);
59     vsp2Id = vsp2.getId();
60   }
61
62   @Test
63   public void testListWhenNone() {
64     Collection<org.openecomp.sdc.vendorsoftwareproduct.dao.type.ComponentEntity> components =
65         vendorSoftwareProductManager.listComponents(vsp1Id, null, USER1);
66     Assert.assertEquals(components.size(), 0);
67   }
68
69   @Test
70   public void testCreateNonExistingVspId_negative() {
71     testCreate_negative(new org.openecomp.sdc.vendorsoftwareproduct.dao.type.ComponentEntity("non existing vsp id", null, null), USER1,
72         VersioningErrorCodes.VERSIONABLE_ENTITY_NOT_EXIST);
73   }
74
75   @Test
76   public void testCreateOnLockedVsp_negative() {
77     testCreate_negative(new org.openecomp.sdc.vendorsoftwareproduct.dao.type.ComponentEntity(vsp1Id, null, null), USER2,
78         VersioningErrorCodes.EDIT_ON_ENTITY_LOCKED_BY_OTHER_USER);
79   }
80
81 /*    @Test(dependsOnMethods = "testListWhenNone")
82     public void testCreate() {
83         comp1Id = testCreate(vsp1Id);
84     }
85
86     private String testCreate(String vspId) {
87         ComponentEntity expected = new ComponentEntity(vspId, null, null);
88         ComponentData compData = new ComponentData();
89         compData.setName("comp1 name");
90         compData.setDescription("comp1 desc");
91         expected.setComponentCompositionData(compData);
92
93         ComponentEntity created = vendorSoftwareProductManager.createComponent(expected, USER1);
94         Assert.assertNotNull(created);
95         expected.setId(created.getId());
96         expected.setVersion(VERSION01);
97
98         ComponentEntity actual = vendorSoftwareProductDao.getComponent(vspId, VERSION01, created.getId());
99
100         Assert.assertEquals(actual, expected);
101         return created.getId();
102     }*/
103
104 /*    @Test(dependsOnMethods = {"testCreate"})
105     public void testCreateWithExistingName_negative() {
106         ComponentEntity component = new ComponentEntity(vsp1Id, null, null);
107         ComponentData compData = new ComponentData();
108         compData.setName("comp1 name");
109         compData.setDescription("comp1 desc");
110         component.setComponentCompositionData(compData);
111         testCreate_negative(component, USER1, UniqueValueUtil.UNIQUE_VALUE_VIOLATION);
112     }*/
113
114 /*    @Test(dependsOnMethods = {"testCreate"})
115     public void testCreateWithExistingNameUnderOtherVsp() {
116         testCreate(vsp2Id);
117     }*/
118
119   @Test
120   public void testCreateOnUploadVsp_negative() {
121     testCreate_negative(new org.openecomp.sdc.vendorsoftwareproduct.dao.type.ComponentEntity(vsp1Id, null, null), USER1,
122         VendorSoftwareProductErrorCodes.VSP_COMPOSITION_EDIT_NOT_ALLOWED);
123   }
124
125   @Test
126   public void testGetNonExistingComponentId_negative() {
127     testGet_negative(vsp1Id, null, "non existing component id", USER1,
128         VersioningErrorCodes.VERSIONABLE_SUB_ENTITY_NOT_FOUND);
129   }
130
131   @Test
132   public void testGetNonExistingVspId_negative() {
133     testGet_negative("non existing vsp id", null, comp1Id, USER1,
134         VersioningErrorCodes.VERSIONABLE_ENTITY_NOT_EXIST);
135   }
136
137   @Test(dependsOnMethods = "testListWhenNone")//"testCreate")
138   public void testGet() {
139     org.openecomp.sdc.vendorsoftwareproduct.dao.type.ComponentEntity
140         expected = createComponent(vsp1Id, VERSION01, comp1Id);
141     testGet(vsp1Id, VERSION01, comp1Id, USER1, expected);
142   }
143
144   @Test
145   public void testUpdateNonExistingComponentId_negative() {
146     testUpdate_negative(vsp1Id, "non existing component id", USER1,
147         VersioningErrorCodes.VERSIONABLE_SUB_ENTITY_NOT_FOUND);
148   }
149
150   @Test
151   public void testUpdateNonExistingVspId_negative() {
152     testUpdate_negative("non existing vsp id", comp1Id, USER1,
153         VersioningErrorCodes.VERSIONABLE_ENTITY_NOT_EXIST);
154   }
155
156   @Test(dependsOnMethods = {"testGet"})
157   public void testUpdateOnUploadVsp() {
158     org.openecomp.sdc.vendorsoftwareproduct.dao.type.ComponentEntity
159         expected = new org.openecomp.sdc.vendorsoftwareproduct.dao.type.ComponentEntity(vsp1Id, null, comp1Id);
160     ComponentData compData = new ComponentData();
161     compData.setName(comp1Id + " name");                // no change
162     compData.setDisplayName(comp1Id + " display name"); // no change
163     compData.setDescription(comp1Id + " desc updated"); // allowed change
164     expected.setComponentCompositionData(compData);
165
166     CompositionEntityValidationData validationData =
167         vendorSoftwareProductManager.updateComponent(expected, USER1);
168     Assert.assertTrue(validationData == null || validationData.getErrors() == null);
169     expected.setVersion(VERSION01);
170
171     org.openecomp.sdc.vendorsoftwareproduct.dao.type.ComponentEntity actual = vendorSoftwareProductDao.getComponent(vsp1Id, VERSION01, comp1Id);
172     Assert.assertEquals(actual, expected);
173   }
174
175   @Test(dependsOnMethods = {"testUpdateOnUploadVsp"})
176   public void testIllegalUpdateOnUploadVsp() {
177     org.openecomp.sdc.vendorsoftwareproduct.dao.type.ComponentEntity
178         expected = new org.openecomp.sdc.vendorsoftwareproduct.dao.type.ComponentEntity(vsp1Id, null, comp1Id);
179     ComponentData compData = new ComponentData();
180     compData
181         .setName("comp1 name updated");         // not allowed: changed name + omitted display name
182     expected.setComponentCompositionData(compData);
183
184     CompositionEntityValidationData validationData =
185         vendorSoftwareProductManager.updateComponent(expected, USER1);
186     Assert.assertNotNull(validationData);
187     Assert.assertEquals(validationData.getErrors().size(), 2);
188   }
189
190   @Test
191   public void testListNonExistingVspId_negative() {
192     testList_negative("non existing vsp id", null, USER1,
193         VersioningErrorCodes.VERSIONABLE_ENTITY_NOT_EXIST);
194   }
195 /*
196     @Test(dependsOnMethods = {"testUpdateOnUploadVsp", "testList"})
197     public void testCreateWithERemovedName() {
198         testCreate(vsp1Id);
199     }
200
201     @Test(dependsOnMethods = "testList")
202     public void testDeleteNonExistingComponentId_negative() {
203         testDelete_negative(vsp1Id, "non existing component id", USER1, VersioningErrorCodes.VERSIONABLE_SUB_ENTITY_NOT_FOUND);
204     }*/
205
206   @Test(dependsOnMethods = {"testGet"})
207   public void testList() {
208     org.openecomp.sdc.vendorsoftwareproduct.dao.type.ComponentEntity
209         createdP2 = createComponent(vsp1Id, VERSION01, comp2Id);
210
211     Collection<org.openecomp.sdc.vendorsoftwareproduct.dao.type.ComponentEntity> actual =
212         vendorSoftwareProductManager.listComponents(vsp1Id, null, USER1);
213     Assert.assertEquals(actual.size(), 2);
214   }
215
216   @Test
217   public void testDeleteNonExistingVspId_negative() {
218     testDelete_negative("non existing vsp id", comp1Id, USER1,
219         VersioningErrorCodes.VERSIONABLE_ENTITY_NOT_EXIST);
220   }
221 /*
222     @Test(dependsOnMethods = "testList")
223     public void testDelete() {
224         vendorSoftwareProductManager.deleteComponent(vsp1Id, comp1Id, USER1);
225         ComponentEntity actual = vendorSoftwareProductDao.getComponent(vsp1Id, VERSION01, comp1Id);
226         Assert.assertNull(actual);
227     }*/
228
229   @Test
230   public void testDeleteOnUploadVsp_negative() {
231     testDelete_negative(vsp1Id, comp1Id, USER1,
232         VendorSoftwareProductErrorCodes.VSP_COMPOSITION_EDIT_NOT_ALLOWED);
233   }
234
235   @Test
236   public void testDeleteListNonExistingVspId_negative() {
237     testDeleteList_negative("non existing vsp id", USER1,
238         VersioningErrorCodes.VERSIONABLE_ENTITY_NOT_EXIST);
239   }
240 /*
241     @Test(dependsOnMethods = "testDelete")
242     public void testDeleteList() {
243         ComponentEntity comp3 = new ComponentEntity(vsp1Id, null, null);
244         comp3.setName("comp3 name");
245         comp3.setDescription("comp3 desc");
246         vendorSoftwareProductManager.createComponent(comp3, USER1);
247
248         vendorSoftwareProductManager.deleteComponents(vsp1Id, USER1);
249
250         Collection<ComponentEntity> actual = vendorSoftwareProductManager.listComponents(vsp1Id, null, USER1);
251         Assert.assertEquals(actual.size(), 0);
252     }*/
253
254   @Test
255   public void testDeleteListOnUploadVsp_negative() {
256     testDeleteList_negative(vsp1Id, USER1,
257         VendorSoftwareProductErrorCodes.VSP_COMPOSITION_EDIT_NOT_ALLOWED);
258   }
259
260   private void testGet(String vspId, Version version, String componentId, String user,
261                        org.openecomp.sdc.vendorsoftwareproduct.dao.type.ComponentEntity expected) {
262     CompositionEntityResponse<ComponentData> response =
263         vendorSoftwareProductManager.getComponent(vspId, null, componentId, user);
264     Assert.assertEquals(response.getId(), expected.getId());
265     Assert.assertEquals(response.getData(), expected.getComponentCompositionData());
266     Assert.assertNotNull(response.getSchema());
267   }
268
269   private void testCreate_negative(
270       org.openecomp.sdc.vendorsoftwareproduct.dao.type.ComponentEntity component, String user,
271       String expectedErrorCode) {
272     try {
273       vendorSoftwareProductManager.createComponent(component, user);
274       Assert.fail();
275     } catch (CoreException e) {
276       Assert.assertEquals(e.code().id(), expectedErrorCode);
277     }
278   }
279
280   private void testGet_negative(String vspId, Version version, String componentId, String user,
281                                 String expectedErrorCode) {
282     try {
283       vendorSoftwareProductManager.getComponent(vspId, version, componentId, user);
284       Assert.fail();
285     } catch (CoreException e) {
286       Assert.assertEquals(e.code().id(), expectedErrorCode);
287     }
288   }
289
290   private void testUpdate_negative(String vspId, String componentId, String user,
291                                    String expectedErrorCode) {
292     try {
293       vendorSoftwareProductManager
294           .updateComponent(new org.openecomp.sdc.vendorsoftwareproduct.dao.type.ComponentEntity(vspId, null, componentId), user);
295       Assert.fail();
296     } catch (CoreException e) {
297       Assert.assertEquals(e.code().id(), expectedErrorCode);
298     }
299   }
300
301   private void testList_negative(String vspId, Version version, String user,
302                                  String expectedErrorCode) {
303     try {
304       vendorSoftwareProductManager.listComponents(vspId, version, user);
305       Assert.fail();
306     } catch (CoreException e) {
307       Assert.assertEquals(e.code().id(), expectedErrorCode);
308     }
309   }
310
311   private void testDeleteList_negative(String vspId, String user, String expectedErrorCode) {
312     try {
313       vendorSoftwareProductManager.deleteComponents(vspId, user);
314       Assert.fail();
315     } catch (CoreException e) {
316       Assert.assertEquals(e.code().id(), expectedErrorCode);
317     }
318   }
319
320   private void testDelete_negative(String vspId, String componentId, String user,
321                                    String expectedErrorCode) {
322     try {
323       vendorSoftwareProductManager.deleteComponent(vspId, componentId, user);
324       Assert.fail();
325     } catch (CoreException e) {
326       Assert.assertEquals(e.code().id(), expectedErrorCode);
327     }
328   }
329 }