push addional code
[sdc.git] / openecomp-be / backend / openecomp-sdc-vendor-software-product-manager / src / test / java / org / openecomp / sdc / vendorsoftwareproduct / ProcessesTest.java
1 package org.openecomp.sdc.vendorsoftwareproduct;
2
3 import org.openecomp.sdc.common.errors.CoreException;
4 import org.openecomp.sdc.vendorsoftwareproduct.dao.VendorSoftwareProductDao;
5 import org.openecomp.sdc.vendorsoftwareproduct.dao.VendorSoftwareProductDaoFactory;
6 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.ProcessArtifactEntity;
7 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.VspDetails;
8 import org.openecomp.sdc.vendorsoftwareproduct.impl.VendorSoftwareProductManagerImpl;
9 import org.openecomp.sdc.versioning.dao.types.Version;
10 import org.openecomp.sdc.versioning.errors.VersioningErrorCodes;
11 import org.openecomp.core.util.UniqueValueUtil;
12 import org.openecomp.core.utilities.CommonMethods;
13
14 import org.testng.Assert;
15 import org.testng.annotations.BeforeClass;
16 import org.testng.annotations.Test;
17
18 import java.io.ByteArrayInputStream;
19 import java.io.File;
20 import java.util.Collection;
21
22 public class ProcessesTest {
23
24   protected static final String USER1 = "processesTestUser1";
25   protected static final VendorSoftwareProductManager vendorSoftwareProductManager =
26       new VendorSoftwareProductManagerImpl();
27   private static final String USER2 = "processesTestUser2";
28   private static final String ARTIFACT_NAME = "artifact.sh";
29   private static final Version VERSION01 = new Version(0, 1);
30   private static final VendorSoftwareProductDao vendorSoftwareProductDao =
31       VendorSoftwareProductDaoFactory.getInstance().createInterface();
32
33   protected String vsp1Id;
34   protected String vsp2Id;
35   protected String component11Id = VendorSoftwareProductConstants.GENERAL_COMPONENT_ID;
36   protected String component21Id = VendorSoftwareProductConstants.GENERAL_COMPONENT_ID;
37   private String p1Id;
38   private String p2Id;
39
40   @BeforeClass
41   protected void init() {
42     VspDetails vsp1 = vendorSoftwareProductManager.createNewVsp(VSPCommon
43         .createVspDetails(null, null, "VSP_" + CommonMethods.nextUuId(), "Test-vsp1", "vendorName1",
44             "vlm1Id", "icon", "category", "subCategory", "123", null), USER1);
45     vsp1Id = vsp1.getId();
46
47     VspDetails vsp2 = vendorSoftwareProductManager.createNewVsp(VSPCommon
48         .createVspDetails(null, null, "VSP_" + CommonMethods.nextUuId(), "Test-vsp2", "vendorName1",
49             "vlm1Id", "icon", "category", "subCategory", "123", null), USER1);
50     vsp2Id = vsp2.getId();
51   }
52
53   @Test
54   public void testListWhenNone() {
55     Collection<org.openecomp.sdc.vendorsoftwareproduct.dao.type.ProcessEntity> processes =
56         vendorSoftwareProductManager.listProcesses(vsp1Id, null, component11Id, USER1);
57     Assert.assertEquals(processes.size(), 0);
58   }
59
60   @Test
61   public void testCreateNonExistingComponentId_negative() {
62     testCreate_negative(new org.openecomp.sdc.vendorsoftwareproduct.dao.type.ProcessEntity(vsp1Id, null, "non existing component id", null), USER1,
63         VersioningErrorCodes.VERSIONABLE_SUB_ENTITY_NOT_FOUND);
64   }
65
66   @Test
67   public void testCreateNonExistingVspId_negative() {
68     testCreate_negative(new org.openecomp.sdc.vendorsoftwareproduct.dao.type.ProcessEntity("non existing vsp id", null, component11Id, null), USER1,
69         VersioningErrorCodes.VERSIONABLE_ENTITY_NOT_EXIST);
70   }
71
72   @Test
73   public void testCreateOnLockedVsp_negative() {
74     testCreate_negative(new org.openecomp.sdc.vendorsoftwareproduct.dao.type.ProcessEntity(vsp1Id, null, component11Id, null), USER2,
75         VersioningErrorCodes.EDIT_ON_ENTITY_LOCKED_BY_OTHER_USER);
76   }
77
78   @Test(dependsOnMethods = "testListWhenNone")
79   public void testCreate() {
80     p1Id = testCreate(vsp1Id, component11Id);
81   }
82
83   @Test(dependsOnMethods = {"testCreate"})
84   public void testCreateWithExistingName_negative() {
85     org.openecomp.sdc.vendorsoftwareproduct.dao.type.ProcessEntity
86         process = new org.openecomp.sdc.vendorsoftwareproduct.dao.type.ProcessEntity(vsp1Id, null, component11Id, null);
87     process.setName("p1 name");
88     testCreate_negative(process, USER1, UniqueValueUtil.UNIQUE_VALUE_VIOLATION);
89   }
90
91   @Test(dependsOnMethods = {"testCreate"})
92   public void testCreateWithExistingNameUnderOtherComponent() {
93     // This method is implemented in the sub class ComponentProcessesTest, it is here in order to keep the tests sequence down there (using @Test).
94   }
95
96   @Test(dependsOnMethods = {"testCreate"})
97   public void testCreateWithExistingNameUnderOtherVsp() {
98     testCreate(vsp2Id, component21Id);
99   }
100
101   @Test
102   public void testGetNonExistingProcessId_negative() {
103     testGet_negative(vsp1Id, null, component11Id, "non existing process id", USER1,
104         VersioningErrorCodes.VERSIONABLE_SUB_ENTITY_NOT_FOUND);
105   }
106
107   @Test(dependsOnMethods = "testCreate")
108   public void testGetNonExistingComponentId_negative() {
109     testGet_negative(vsp1Id, null, "non existing component id", p1Id, USER1,
110         VersioningErrorCodes.VERSIONABLE_SUB_ENTITY_NOT_FOUND);
111   }
112
113   @Test(dependsOnMethods = "testCreate")
114   public void testGetNonExistingVspId_negative() {
115     testGet_negative("non existing vsp id", null, component11Id, p1Id, USER1,
116         VersioningErrorCodes.VERSIONABLE_ENTITY_NOT_EXIST);
117   }
118
119   @Test(dependsOnMethods = "testCreate")
120   public void testGet() {
121     org.openecomp.sdc.vendorsoftwareproduct.dao.type.ProcessEntity
122         actual = testGet(vsp1Id, VERSION01, component11Id, p1Id, USER1);
123     Assert.assertNull(actual.getArtifactName());
124   }
125
126   @Test
127   public void testUpdateNonExistingProcessId_negative() {
128     testUpdate_negative(vsp1Id, component11Id, "non existing process id", USER1,
129         VersioningErrorCodes.VERSIONABLE_SUB_ENTITY_NOT_FOUND);
130   }
131
132   @Test(dependsOnMethods = "testCreate")
133   public void testUpdateNonExistingComponentId_negative() {
134     testUpdate_negative(vsp1Id, "non existing component id", p1Id, USER1,
135         VersioningErrorCodes.VERSIONABLE_SUB_ENTITY_NOT_FOUND);
136   }
137
138   @Test(dependsOnMethods = "testCreate")
139   public void testUpdateNonExistingVspId_negative() {
140     testUpdate_negative("non existing vsp id", component11Id, p1Id, USER1,
141         VersioningErrorCodes.VERSIONABLE_ENTITY_NOT_EXIST);
142   }
143
144   @Test(dependsOnMethods = {"testGet"})
145   public void testUpdate() {
146     org.openecomp.sdc.vendorsoftwareproduct.dao.type.ProcessEntity
147         expected = new org.openecomp.sdc.vendorsoftwareproduct.dao.type.ProcessEntity(vsp1Id, null, component11Id, p1Id);
148     expected.setName("p1 name updated");
149     expected.setDescription("p1 desc updated");
150
151     vendorSoftwareProductManager.updateProcess(expected, USER1);
152     expected.setVersion(VERSION01);
153
154     org.openecomp.sdc.vendorsoftwareproduct.dao.type.ProcessEntity actual =
155         vendorSoftwareProductDao.getProcess(vsp1Id, VERSION01, component11Id, p1Id);
156     Assert.assertEquals(actual, expected);
157   }
158
159   @Test
160   public void testListNonExistingComponentId_negative() {
161     testList_negative(vsp1Id, null, "non existing component id", USER1,
162         VersioningErrorCodes.VERSIONABLE_SUB_ENTITY_NOT_FOUND);
163   }
164
165   @Test
166   public void testListNonExistingVspId_negative() {
167     testList_negative("non existing vsp id", null, component11Id, USER1,
168         VersioningErrorCodes.VERSIONABLE_ENTITY_NOT_EXIST);
169   }
170
171   @Test(dependsOnMethods = {"testGet"})
172   public void testList() {
173     org.openecomp.sdc.vendorsoftwareproduct.dao.type.ProcessEntity
174         p2 = new org.openecomp.sdc.vendorsoftwareproduct.dao.type.ProcessEntity(vsp1Id, null, component11Id, null);
175     p2.setName("p2 name");
176     p2.setDescription("p2 desc");
177
178     org.openecomp.sdc.vendorsoftwareproduct.dao.type.ProcessEntity createdP2 = vendorSoftwareProductManager.createProcess(p2, USER1);
179     p2Id = createdP2.getId();
180
181     Collection<org.openecomp.sdc.vendorsoftwareproduct.dao.type.ProcessEntity> actual =
182         vendorSoftwareProductManager.listProcesses(vsp1Id, null, component11Id, USER1);
183     Collection<org.openecomp.sdc.vendorsoftwareproduct.dao.type.ProcessEntity> expected =
184         vendorSoftwareProductDao.listProcesses(vsp1Id, VERSION01, component11Id);
185     Assert.assertEquals(actual.size(), 2);
186     Assert.assertEquals(actual, expected);
187   }
188
189   @Test(dependsOnMethods = {"testUpdate", "testList"})
190   public void testCreateWithRemovedName() {
191     testCreate(vsp1Id, component11Id);
192   }
193
194   @Test
195   public void testDeleteNonExistingProcessId_negative() {
196     testDelete_negative(vsp1Id, component11Id, "non existing process id", USER1,
197         VersioningErrorCodes.VERSIONABLE_SUB_ENTITY_NOT_FOUND);
198   }
199
200   @Test(dependsOnMethods = "testList")
201   public void testDeleteNonExistingComponentId_negative() {
202     testDelete_negative(vsp1Id, "non existing component id", p1Id, USER1,
203         VersioningErrorCodes.VERSIONABLE_SUB_ENTITY_NOT_FOUND);
204   }
205
206   @Test(dependsOnMethods = "testList")
207   public void testDeleteNonExistingVspId_negative() {
208     testDelete_negative("non existing vsp id", component11Id, p1Id, USER1,
209         VersioningErrorCodes.VERSIONABLE_ENTITY_NOT_EXIST);
210   }
211
212   @Test(dependsOnMethods = "testList")
213   public void testDelete() {
214     vendorSoftwareProductManager.deleteProcess(vsp1Id, component11Id, p1Id, USER1);
215     org.openecomp.sdc.vendorsoftwareproduct.dao.type.ProcessEntity actual =
216         vendorSoftwareProductDao.getProcess(vsp1Id, VERSION01, component11Id, p1Id);
217     Assert.assertNull(actual);
218   }
219
220   @Test
221   public void testUploadFileNonExistingProcessId_negative() {
222     testUploadFile_negative(vsp1Id, component11Id, "non existing process id", USER1,
223         VersioningErrorCodes.VERSIONABLE_SUB_ENTITY_NOT_FOUND);
224   }
225
226   @Test(dependsOnMethods = "testList")
227   public void testUploadFileNonExistingComponentId_negative() {
228     testUploadFile_negative(vsp1Id, "non existing component id", p2Id, USER1,
229         VersioningErrorCodes.VERSIONABLE_SUB_ENTITY_NOT_FOUND);
230   }
231
232   @Test(dependsOnMethods = "testList")
233   public void testUploadFileNonExistingVspId_negative() {
234     testUploadFile_negative("non existing vsp id", component11Id, p2Id, USER1,
235         VersioningErrorCodes.VERSIONABLE_ENTITY_NOT_EXIST);
236   }
237
238   @Test(dependsOnMethods = "testList")
239   public void testGetFileWhenNone_negative() {
240     testGetFile_negative(vsp1Id, null, component11Id, p2Id, USER1,
241         VersioningErrorCodes.VERSIONABLE_SUB_ENTITY_NOT_FOUND);
242   }
243
244   @Test(dependsOnMethods = "testList")
245   public void testDeleteFileWhenNone_negative() {
246     testDeleteFile_negative(vsp1Id, component11Id, p2Id, USER1,
247         VersioningErrorCodes.VERSIONABLE_SUB_ENTITY_NOT_FOUND);
248   }
249
250   @Test(dependsOnMethods = {"testGetFileWhenNone_negative", "testDeleteFileWhenNone_negative"})
251   public void testUploadFile() {
252     vendorSoftwareProductManager
253         .uploadProcessArtifact(new ByteArrayInputStream("bla bla".getBytes()), ARTIFACT_NAME,
254             vsp1Id, component11Id, p2Id, USER1);
255     ProcessArtifactEntity actual =
256         vendorSoftwareProductDao.getProcessArtifact(vsp1Id, VERSION01, component11Id, p2Id);
257     Assert.assertNotNull(actual);
258     Assert.assertNotNull(actual.getArtifact());
259     Assert.assertEquals(actual.getArtifactName(), ARTIFACT_NAME);
260   }
261
262   @Test(dependsOnMethods = "testUploadFile")
263   public void testGetAfterUploadFile() {
264     org.openecomp.sdc.vendorsoftwareproduct.dao.type.ProcessEntity
265         actual = testGet(vsp1Id, VERSION01, component11Id, p2Id, USER1);
266     Assert.assertEquals(actual.getArtifactName(), ARTIFACT_NAME);
267   }
268
269   @Test
270   public void testGetFileNonExistingProcessId_negative() {
271     testGetFile_negative(vsp1Id, null, component11Id, "non existing process id", USER1,
272         VersioningErrorCodes.VERSIONABLE_SUB_ENTITY_NOT_FOUND);
273   }
274
275   @Test(dependsOnMethods = "testList")
276   public void testGetFileNonExistingComponentId_negative() {
277     testGetFile_negative(vsp1Id, null, "non existing component id", p2Id, USER1,
278         VersioningErrorCodes.VERSIONABLE_SUB_ENTITY_NOT_FOUND);
279   }
280
281   @Test(dependsOnMethods = "testList")
282   public void testGetFileNonExistingVspId_negative() {
283     testGetFile_negative("non existing vsp id", null, component11Id, p2Id, USER1,
284         VersioningErrorCodes.VERSIONABLE_ENTITY_NOT_EXIST);
285   }
286
287   @Test(dependsOnMethods = "testUploadFile")
288   public void testGetFile() {
289     File actual =
290         vendorSoftwareProductManager.getProcessArtifact(vsp1Id, null, component11Id, p2Id, USER1);
291     Assert.assertNotNull(actual);
292     ProcessArtifactEntity expected =
293         vendorSoftwareProductDao.getProcessArtifact(vsp1Id, VERSION01, component11Id, p2Id);
294     Assert.assertNotNull(expected);
295     Assert.assertNotNull(expected.getArtifact());
296   }
297
298   @Test
299   public void testDeleteFileNonExistingProcessId_negative() {
300     testDeleteFile_negative(vsp1Id, component11Id, "non existing process id", USER1,
301         VersioningErrorCodes.VERSIONABLE_SUB_ENTITY_NOT_FOUND);
302   }
303
304   @Test(dependsOnMethods = "testList")
305   public void testDeleteFileNonExistingComponentId_negative() {
306     testDeleteFile_negative(vsp1Id, "non existing component id", p2Id, USER1,
307         VersioningErrorCodes.VERSIONABLE_SUB_ENTITY_NOT_FOUND);
308   }
309
310   @Test(dependsOnMethods = "testList")
311   public void testDeleteFileNonExistingVspId_negative() {
312     testDeleteFile_negative("non existing vsp id", component11Id, p2Id, USER1,
313         VersioningErrorCodes.VERSIONABLE_ENTITY_NOT_EXIST);
314   }
315
316   @Test(dependsOnMethods = "testGetFile")
317   public void testDeleteFile() {
318     vendorSoftwareProductManager.deleteProcessArtifact(vsp1Id, component11Id, p2Id, USER1);
319     ProcessArtifactEntity expected =
320         vendorSoftwareProductDao.getProcessArtifact(vsp1Id, VERSION01, component11Id, p2Id);
321     Assert.assertNull(expected.getArtifact());
322   }
323
324   @Test
325   public void testDeleteListNonExistingComponentId_negative() {
326     testDeleteList_negative(vsp1Id, "non existing component id", USER1,
327         VersioningErrorCodes.VERSIONABLE_SUB_ENTITY_NOT_FOUND);
328   }
329
330   @Test
331   public void testDeleteListNonExistingVspId_negative() {
332     testDeleteList_negative("non existing vsp id", component11Id, USER1,
333         VersioningErrorCodes.VERSIONABLE_ENTITY_NOT_EXIST);
334   }
335
336   @Test(dependsOnMethods = {"testDeleteFile"})
337   public void testDeleteList() {
338     org.openecomp.sdc.vendorsoftwareproduct.dao.type.ProcessEntity
339         p3 = new org.openecomp.sdc.vendorsoftwareproduct.dao.type.ProcessEntity(vsp1Id, null, component11Id, null);
340     p3.setName("p3 name");
341     p3.setDescription("p3 desc");
342     vendorSoftwareProductManager.createProcess(p3, USER1);
343
344     vendorSoftwareProductManager.deleteProcesses(vsp1Id, component11Id, USER1);
345
346     Collection<org.openecomp.sdc.vendorsoftwareproduct.dao.type.ProcessEntity> actual =
347         vendorSoftwareProductManager.listProcesses(vsp1Id, null, component11Id, USER1);
348     Assert.assertEquals(actual.size(), 0);
349   }
350
351   protected String testCreate(String vspId, String componentId) {
352     org.openecomp.sdc.vendorsoftwareproduct.dao.type.ProcessEntity
353         expected = new org.openecomp.sdc.vendorsoftwareproduct.dao.type.ProcessEntity(vspId, null, componentId, null);
354     expected.setName("p1 name");
355     expected.setDescription("p1 desc");
356
357     org.openecomp.sdc.vendorsoftwareproduct.dao.type.ProcessEntity created = vendorSoftwareProductManager.createProcess(expected, USER1);
358     Assert.assertNotNull(created);
359     expected.setId(created.getId());
360     expected.setVersion(VERSION01);
361
362     org.openecomp.sdc.vendorsoftwareproduct.dao.type.ProcessEntity actual =
363         vendorSoftwareProductDao.getProcess(vspId, VERSION01, componentId, created.getId());
364
365     Assert.assertEquals(actual, expected);
366
367     return created.getId();
368   }
369
370   private org.openecomp.sdc.vendorsoftwareproduct.dao.type.ProcessEntity testGet(String vspId, Version version, String componentId, String processId,
371                                                                                  String user) {
372     org.openecomp.sdc.vendorsoftwareproduct.dao.type.ProcessEntity actual =
373         vendorSoftwareProductManager.getProcess(vspId, null, componentId, processId, user);
374     org.openecomp.sdc.vendorsoftwareproduct.dao.type.ProcessEntity expected =
375         vendorSoftwareProductDao.getProcess(vspId, version, componentId, processId);
376     Assert.assertEquals(actual, expected);
377     return actual;
378   }
379
380   private void testCreate_negative(
381       org.openecomp.sdc.vendorsoftwareproduct.dao.type.ProcessEntity process, String user, String expectedErrorCode) {
382     try {
383       vendorSoftwareProductManager.createProcess(process, user);
384       Assert.fail();
385     } catch (CoreException e) {
386       Assert.assertEquals(e.code().id(), expectedErrorCode);
387     }
388   }
389
390   private void testGet_negative(String vspId, Version version, String componentId, String processId,
391                                 String user, String expectedErrorCode) {
392     try {
393       vendorSoftwareProductManager.getProcess(vspId, version, componentId, processId, user);
394       Assert.fail();
395     } catch (CoreException e) {
396       Assert.assertEquals(e.code().id(), expectedErrorCode);
397     }
398   }
399
400   private void testUpdate_negative(String vspId, String componentId, String processId, String user,
401                                    String expectedErrorCode) {
402     try {
403       vendorSoftwareProductManager
404           .updateProcess(new org.openecomp.sdc.vendorsoftwareproduct.dao.type.ProcessEntity(vspId, null, componentId, processId), user);
405       Assert.fail();
406     } catch (CoreException e) {
407       Assert.assertEquals(e.code().id(), expectedErrorCode);
408     }
409   }
410
411   private void testList_negative(String vspId, Version version, String componentId, String user,
412                                  String expectedErrorCode) {
413     try {
414       vendorSoftwareProductManager.listProcesses(vspId, version, componentId, user);
415       Assert.fail();
416     } catch (CoreException e) {
417       Assert.assertEquals(e.code().id(), expectedErrorCode);
418     }
419   }
420
421   private void testDeleteList_negative(String vspId, String componentId, String user,
422                                        String expectedErrorCode) {
423     try {
424       vendorSoftwareProductManager.deleteProcesses(vspId, componentId, user);
425       Assert.fail();
426     } catch (CoreException e) {
427       Assert.assertEquals(e.code().id(), expectedErrorCode);
428     }
429   }
430
431   private void testDelete_negative(String vspId, String componentId, String processId, String user,
432                                    String expectedErrorCode) {
433     try {
434       vendorSoftwareProductManager.deleteProcess(vspId, componentId, processId, user);
435       Assert.fail();
436     } catch (CoreException e) {
437       Assert.assertEquals(e.code().id(), expectedErrorCode);
438     }
439   }
440
441   private void testGetFile_negative(String vspId, Version version, String componentId,
442                                     String processId, String user, String expectedErrorCode) {
443     try {
444       vendorSoftwareProductManager.getProcessArtifact(vspId, version, componentId, processId, user);
445       Assert.fail();
446     } catch (CoreException e) {
447       Assert.assertEquals(e.code().id(), expectedErrorCode);
448     }
449   }
450
451   private void testUploadFile_negative(String vspId, String componentId, String processId,
452                                        String user, String expectedErrorCode) {
453     try {
454       vendorSoftwareProductManager
455           .uploadProcessArtifact(new ByteArrayInputStream("bla bla".getBytes()), "artifact.sh",
456               vspId, componentId, processId, user);
457       Assert.fail();
458     } catch (CoreException e) {
459       Assert.assertEquals(e.code().id(), expectedErrorCode);
460     }
461   }
462
463   private void testDeleteFile_negative(String vspId, String componentId, String processId,
464                                        String user, String expectedErrorCode) {
465     try {
466       vendorSoftwareProductManager.deleteProcessArtifact(vspId, componentId, processId, user);
467       Assert.fail();
468     } catch (CoreException e) {
469       Assert.assertEquals(e.code().id(), expectedErrorCode);
470     }
471   }
472
473 }