push addional code
[sdc.git] / openecomp-be / backend / openecomp-sdc-action-manager / src / test / java / org / openecomp / sdc / action / ActionTest.java
1 package org.openecomp.sdc.action;
2
3 import org.openecomp.sdc.action.dao.ActionDao;
4 import org.openecomp.sdc.action.dao.ActionDaoFactory;
5 import org.openecomp.sdc.action.dao.types.ActionEntity;
6 import org.openecomp.sdc.action.errors.ActionErrorConstants;
7 import org.openecomp.sdc.action.errors.ActionException;
8 import org.openecomp.sdc.action.impl.ActionManagerImpl;
9
10 import org.openecomp.sdc.versioning.dao.types.Version;
11 import org.openecomp.core.nosqldb.api.NoSqlDb;
12 import org.openecomp.core.nosqldb.factory.NoSqlDbFactory;
13 import org.openecomp.core.utilities.json.JsonUtil;
14
15 import org.openecomp.sdc.action.types.Action;
16 import org.openecomp.sdc.action.types.ActionArtifact;
17 import org.openecomp.sdc.action.types.ActionArtifactProtection;
18 import org.openecomp.sdc.action.types.ActionStatus;
19 import org.openecomp.sdc.action.types.EcompComponent;
20 import org.testng.Assert;
21 import org.testng.annotations.BeforeTest;
22 import org.testng.annotations.Test;
23
24 import java.io.File;
25 import java.io.FileInputStream;
26 import java.io.IOException;
27 import java.util.*;
28
29
30 @SuppressWarnings("Duplicates")
31 public class ActionTest {
32   private static final Version VERSION01 = new Version(0, 1);
33   private static final String USER1 = "actionTestUser1";
34   private static final String USER2 = "actionTestUser2";
35   private static final String ACTION_1 =
36       "{\"name\":\"Test_Action1_name\", \"endpointUri\":\"/test/action/uri\"}";
37   private static final String ACTION_2 =
38       "{\"name\":\"Test_Action2_list\", \"endpointUri\":\"/test/action/uri\", \"categoryList\":[\"Cat-test\", \"Cat-2\"], \"supportedModels\":[{\"versionId\" : \"Model-test\"}], \"supportedComponents\":[{\"Id\":\"APP-C\"}]}";
39   private static final String ACTION_3 =
40       "{\"name\":\"Test_Action3_list\", \"endpointUri\":\"/test/action/uri\", \"vendorList\":[\"Vendor-test\", \"Vendor-2\"], \"supportedModels\":[{\"versionId\" : \"Model-2\"}], \"supportedComponents\":[{\"Id\":\"MSO\"}]}";
41   private static final String ACTION_4 =
42       "{\"name\":\"Test_Action4_list\", \"endpointUri\":\"/test/action/uri\", \"categoryList\":[\"Cat-test\", \"Cat-2\"], \"supportedModels\":[{\"versionId\" : \"Model-test\"}], \"supportedComponents\":[{\"Id\":\"APP-C\"}]}";
43   private static final String ACTION_5 =
44       "{\"name\":\"Test_Action5_list\", \"endpointUri\":\"/test/action/uri\", \"vendorList\":[\"Vendor-test\", \"Vendor-2\"], \"supportedModels\":[{\"versionId\" : \"Model-2\"}], \"supportedComponents\":[{\"Id\":\"MSO\"}]}";
45   private static final String ACTION_6 =
46       "{\"name\":\"Test_Action6_name\", \"endpointUri\":\"/test/action/uri\"}";
47   private static final String ARTIFACT_TEST_ACTION =
48       "{\"name\":\"Test_Artifact_Action\", \"endpointUri\":\"/test/artifact/action/uri\", \"vendorList\":[\"Vendor-test\", \"Vendor-2\"], \"supportedModels\":[{\"versionId\" : \"Model-2\"}], \"supportedComponents\":[{\"Id\":\"MSO\"}]}";
49   private static final String ACTION_TEST_DELETE =
50       "{\"name\":\"Test_Delete_Action\", \"endpointUri\":\"/test/delete/action/uri\", \"categoryList\":[\"Cat-Delete-test\"], \"vendorList\":[\"Vendor-Delete\"], \"supportedModels\":[{\"versionId\" : \"Model-Delete\"}], \"supportedComponents\":[{\"Id\":\"MSO-Delete\"}]}";
51   private static final String ACTION_TEST_ARTIFACT_FILE_NAME = "test_artifact_file.txt";
52   private static final String ACTION_TEST_UPDATE_ARTIFACT_FILE_NAME =
53       "test_artifact_update_file.txt";
54   private static ActionManager actionManager = new ActionManagerImpl();
55   private static ActionDao actionDao = ActionDaoFactory.getInstance().createInterface();
56
57   private static NoSqlDb noSqlDb;
58
59   private static String action1Id;
60   private static String action2Id;
61
62   private static String actionUUId;
63   private static Action testArtifactAction;
64   private static String expectedArtifactUUID;
65   private static ActionArtifact actionArtifact;
66   private Action deleteAction;
67
68   private static String testCreate() {
69     Action action1 = createAction(ACTION_1);
70     Action actionCreated = actionManager.createAction(action1, USER1);
71     action1Id = actionCreated.getActionInvariantUuId();
72     actionUUId = actionCreated.getActionUuId();
73     action1.setVersion(VERSION01.toString());
74     ActionEntity loadedAction = actionDao.get(action1.toEntity());
75     assertActionEquals(actionCreated, loadedAction.toDto());
76     return action1Id;
77   }
78
79   private static Action createAction(String requestJSON) {
80     Action action = JsonUtil.json2Object(requestJSON, Action.class);
81     action.setData(requestJSON);
82     return action;
83   }
84
85   private static void assertActionEquals(Action actual, Action expected) {
86     Assert.assertEquals(actual.getActionUuId(), expected.getActionUuId());
87     Assert.assertEquals(actual.getVersion(), expected.getVersion());
88     Assert.assertEquals(actual.getName(), expected.getName());
89     //Assert.assertEquals(actual.getDescription(), expected.getDescription());
90     Assert.assertEquals(actual.getData(), expected.getData());
91     Assert.assertEquals(actual.getActionInvariantUuId(), expected.getActionInvariantUuId());
92     //Assert.assertEquals(actual.getEndpointUri(), expected.getEndpointUri());
93     Assert.assertEquals(actual.getStatus(), expected.getStatus());
94     Assert.assertEquals(actual.getSupportedComponents(), expected.getSupportedComponents());
95     Assert.assertEquals(actual.getSupportedModels(), expected.getSupportedModels());
96   }
97
98   @BeforeTest
99   private void init() {
100     this.noSqlDb = NoSqlDbFactory.getInstance().createInterface();
101     this.noSqlDb.execute("TRUNCATE dox.action;");
102     this.noSqlDb.execute("TRUNCATE dox.ecompcomponent;");
103     this.noSqlDb.execute("TRUNCATE dox.unique_value;");
104     this.noSqlDb.execute("TRUNCATE dox.action_artifact;");
105     this.noSqlDb.execute("insert into dox.ecompcomponent(id, name) values ('COMP-1','MSO');");
106     this.noSqlDb.execute("insert into dox.ecompcomponent(id, name) values ('COMP-2','APP-C');");
107   }
108
109   @Test
110   public void createTest() {
111     action1Id = testCreate();
112   }
113
114   @Test
115   public void testGetByInvIdOnCreate() {
116     String input =
117         "{\"name\":\"Action_2.0\",\"endpointUri\":\"new/action/uri\",\"categoryList\":[\"Cat-1\", \"Cat-2\"],\"displayName\":\"Updated Action\",\"vendorList\":[\"Vendor-1\", \"Vendor-2\"]," +
118             "\"supportedModels\":[{\"versionId\":\"AA56B177-9383-4934-8543-0F91A7A04971\"," +
119             "\"invariantID\":\"CC87B177-9383-4934-8543-0F91A7A07193\", \"name\":\"vABC\"," +
120             "\"version\":\"2.1\",\"vendor\":\"cisco\"}]," +
121             "\"supportedComponents\":[{\"Id\":\"BB47B177-9383-4934-8543-0F91A7A06448\", \"name\":\"appc\"}]}";
122     Action action1 = createAction(input);
123     Action action = actionManager.createAction(action1, USER1);
124     action2Id = action.getActionInvariantUuId();
125     List<Action> actions =
126         actionManager.getActionsByActionInvariantUuId(action.getActionInvariantUuId());
127     Assert.assertEquals(1, actions.size());
128     Assert.assertEquals("0.1", actions.get(0).getVersion());
129   }
130
131   @Test(dependsOnMethods = {"testGetByInvIdOnCreate"})
132   public void testGetByIgnoreCaseName() {
133     List<Action> actions =
134         actionManager.getFilteredActions(ActionConstants.FILTER_TYPE_NAME, "acTion_2.0");
135     List<String> actualVersionList = new ArrayList<String>();
136     List<String> expectedVersionList = new ArrayList<String>();
137     expectedVersionList.add("0.1");
138     for (Action action : actions) {
139       System.out.println("action by testGetByIgnoreCaseName is::::");
140       System.out.println(action.getActionInvariantUuId() + " " + action.getVersion());
141       actualVersionList.add(action.getVersion());
142     }
143     Assert.assertEquals(1, actions.size());
144     Assert.assertEquals(expectedVersionList, actualVersionList);
145   }
146
147   @Test(dependsOnMethods = {"testGetByInvIdOnCreate"})
148   public void testGetByInvIdManyVersionWithoutSubmit() {
149     for (int i = 0; i < 11; i++) {
150       actionManager.checkin(action2Id, USER1);
151       actionManager.checkout(action2Id, USER1);
152     }
153
154     List<Action> actions = actionManager.getActionsByActionInvariantUuId(action2Id);
155     List<String> actualVersionList = new ArrayList<String>();
156     List<String> expectedVersionList = new ArrayList<String>();
157     expectedVersionList.add("0.11");
158     expectedVersionList.add("0.12");
159     System.out.println(actions.size());
160     for (Action action : actions) {
161       System.out.println("testGetByInvIdManyVersionWithoutSubmit is::::");
162       System.out.println(action.getActionInvariantUuId() + " " + action.getVersion());
163       actualVersionList.add(action.getVersion());
164     }
165     Assert.assertEquals(2, actions.size());
166     Assert.assertEquals(expectedVersionList, actualVersionList);
167   }
168
169   @Test(dependsOnMethods = {"testGetByInvIdManyVersionWithoutSubmit"})
170   public void testGetByInvIdManyVersionWithFirstSubmit() {
171     actionManager.checkin(action2Id, USER1);//Checkin 0.12
172     actionManager.submit(action2Id, USER1); //1.0
173     for (int i = 0; i < 11; i++) {
174       actionManager.checkout(action2Id, USER1);
175       actionManager.checkin(action2Id, USER1);
176     }
177
178     List<Action> actions = actionManager.getActionsByActionInvariantUuId(action2Id);
179     List<String> actualVersionList = new ArrayList<String>();
180     List<String> expectedVersionList = new ArrayList<String>();
181     expectedVersionList.add("1.0");
182     expectedVersionList.add("1.11");
183     System.out.println(actions.size());
184     for (Action action : actions) {
185       System.out.println("testGetByInvIdManyVersionWithFirstSubmit is::::");
186       System.out.println(action.getActionInvariantUuId() + " " + action.getVersion());
187       actualVersionList.add(action.getVersion());
188     }
189     Assert.assertEquals(2, actions.size());
190     Assert.assertEquals(expectedVersionList, actualVersionList);
191   }
192
193   @Test(dependsOnMethods = {"testGetByInvIdManyVersionWithFirstSubmit"})
194   public void testGetByInvIdManyVersionWithMultSubmit() {
195     actionManager.submit(action2Id, USER1); //2.0
196     for (int i = 0; i < 11; i++) {
197       actionManager.checkout(action2Id, USER1);
198       actionManager.checkin(action2Id, USER1);
199     }
200     actionManager.checkout(action2Id, USER1); //2.12
201
202     List<Action> actions = actionManager.getActionsByActionInvariantUuId(action2Id);
203     List<String> actualVersionList = new ArrayList<String>();
204     List<String> expectedVersionList = new ArrayList<String>();
205     expectedVersionList.add("1.0");
206     expectedVersionList.add("2.0");
207     expectedVersionList.add("2.11");
208     expectedVersionList.add("2.12");
209     System.out.println(actions.size());
210     for (Action action : actions) {
211       System.out.println("testGetByInvIdManyVersionWithMultSubmit is::::");
212       System.out.println(action.getActionInvariantUuId() + " " + action.getVersion());
213       actualVersionList.add(action.getVersion());
214     }
215     Assert.assertEquals(4, actions.size());
216     Assert.assertEquals(expectedVersionList, actualVersionList);
217   }
218
219   @Test(dependsOnMethods = {"testGetByInvIdManyVersionWithMultSubmit"})
220   public void testGetByInvIdOnName() {
221     for (int i = 0; i < 9; i++) {
222       actionManager.checkin(action2Id, USER1);
223       actionManager.checkout(action2Id, USER1); //2.21
224     }
225
226     List<Action> actions =
227         actionManager.getFilteredActions(ActionConstants.FILTER_TYPE_NAME, "Action_2.0");
228     List<String> actualVersionList = new ArrayList<String>();
229     List<String> expectedVersionList = new ArrayList<String>();
230     expectedVersionList.add("1.0");
231     expectedVersionList.add("2.0");
232     expectedVersionList.add("2.20");
233     expectedVersionList.add("2.21");
234     for (Action action : actions) {
235       System.out.println("action by testGetByInvIdOnName is::::");
236       System.out.println(action.getActionInvariantUuId() + " " + action.getVersion());
237       actualVersionList.add(action.getVersion());
238     }
239     Assert.assertEquals(4, actions.size());
240     Assert.assertEquals(expectedVersionList, actualVersionList);
241   }
242
243   @Test(dependsOnMethods = {"createTest"})
244   public void testCreateWithExistingActionName_negative() {
245     try {
246       actionManager.createAction(createAction(ACTION_1), USER1);
247       Assert.fail();
248     } catch (ActionException e) {
249       Assert.assertEquals(e.getErrorCode(), ActionErrorConstants.ACTION_ENTITY_UNIQUE_VALUE_ERROR);
250     }
251   }
252
253   @Test(groups = "updateTestGroup",
254       dependsOnMethods = {"testCreateWithExistingActionName_negative", "createTest"})
255   public void updateTest() {
256     List<String> newSupportedComponents = new LinkedList<>();
257     newSupportedComponents.add("Updated MSO");
258     newSupportedComponents.add("Updated APPC");
259
260     List<String> newSupportedModels = new LinkedList<>();
261     newSupportedModels.add("Updated Model-1");
262     newSupportedModels.add("Updated Model-2");
263
264     Action action = new Action();
265     action.setActionInvariantUuId(action1Id);
266     action.setVersion(VERSION01.toString());
267     ActionEntity existingActionEntity = actionDao.get(action.toEntity());
268     existingActionEntity
269         .setSupportedComponents(newSupportedComponents);    //Updating Supported components
270     existingActionEntity.setSupportedModels(newSupportedModels);    //Updating supported models
271     //Persisting the updated entity
272     Action updatedAction = actionManager.updateAction(existingActionEntity.toDto(), USER1);
273
274     //Create expected response template
275     ActionEntity expectedActionEntity = new ActionEntity(action1Id, VERSION01);
276     expectedActionEntity.setName(existingActionEntity.getName());
277     expectedActionEntity.setActionUuId(existingActionEntity.getActionUuId());
278     expectedActionEntity.setActionInvariantUuId(existingActionEntity.getActionInvariantUuId());
279     expectedActionEntity.setData(existingActionEntity.getData());
280     expectedActionEntity.setStatus(ActionStatus.Locked.name());
281     expectedActionEntity.setSupportedComponents(newSupportedComponents);
282     expectedActionEntity.setSupportedModels(newSupportedModels);
283     Action expectedAction = updateData(expectedActionEntity.toDto());
284
285     assertActionEquals(updatedAction, expectedAction);
286   }
287
288   @Test(groups = "updateTestGroup", dependsOnMethods = {"updateTest"})
289   public void testUpdateName_negative() {
290     try {
291       Action action = new Action();
292       action.setActionInvariantUuId(action1Id);
293       action.setVersion(VERSION01.toString());
294       ActionEntity existingActionEntity = actionDao.get(action.toEntity());
295       action = existingActionEntity.toDto();
296       action.setName("Update - New Action Name");
297       //Persisting the updated entity
298       actionManager.updateAction(action, USER1);
299       Assert.fail();
300     } catch (ActionException e) {
301       Assert
302           .assertEquals(e.getErrorCode(), ActionErrorConstants.ACTION_UPDATE_NOT_ALLOWED_CODE_NAME);
303     }
304   }
305
306   @Test(groups = "updateTestGroup", dependsOnMethods = {"updateTest"})
307   public void testUpdateVersion_negative() {
308     try {
309       Action action = new Action();
310       action.setActionInvariantUuId(action1Id);
311       action.setVersion(VERSION01.toString());
312       ActionEntity existingActionEntity = actionDao.get(action.toEntity());
313       action = existingActionEntity.toDto();
314       action.setVersion("0.3");
315       //Persisting the updated entity
316       actionManager.updateAction(action, USER1);
317       Assert.fail();
318     } catch (ActionException e) {
319       Assert.assertEquals(e.getErrorCode(), ActionErrorConstants.ACTION_UPDATE_INVALID_VERSION);
320     }
321   }
322
323   @Test(groups = "updateTestGroup", dependsOnMethods = {"updateTest"})
324   public void testUpdateInvalidVersion_negative() {
325     try {
326       Action action = new Action();
327       action.setActionInvariantUuId(action1Id);
328       action.setVersion(VERSION01.toString());
329       ActionEntity existingActionEntity = actionDao.get(action.toEntity());
330       //existingActionEntity.setDisplayName("Display Name Updated");
331       Action updatedAction = existingActionEntity.toDto();
332       updatedAction.setVersion("invalid_version_format");
333       //Persisting the updated entity
334       actionManager.updateAction(updatedAction, USER1);
335       Assert.fail();
336     } catch (ActionException e) {
337       Assert.assertEquals(e.getErrorCode(), ActionErrorConstants.ACTION_UPDATE_NOT_ALLOWED_CODE);
338     }
339   }
340
341     /*@Test(groups = "updateTestGroup", dependsOnMethods = {"updateTest"})
342     public void testUpdateStatusInvalidEnum_negative() {
343         try {
344             Action action = new Action();
345             action.setActionInvariantUuId(action1Id);
346             action.setVersion(VERSION01.toString());
347             ActionEntity existingActionEntity = actionDao.get(action.toEntity());
348             existingActionEntity.setStatus("invalid_status_string");
349             //Persisting the updated entity
350             actionManager.updateAction(existingActionEntity.toDto(),USER1);
351             Assert.fail();
352         } catch (ActionException e) {
353             Assert.assertEquals(e.getErrorCode(), ActionErrorConstants.ACTION_UPDATE_NOT_ALLOWED_CODE);
354         } catch (IllegalArgumentException ie){
355             String message = ie.getMessage();
356             boolean result = message.contains("No enum constant");
357             Assert.assertEquals(true, result);
358         }
359     }*/
360
361   @Test(groups = "updateTestGroup", dependsOnMethods = {"updateTest"})
362   public void testUpdateInvariantId_negative() {
363     try {
364       Action action = new Action();
365       action.setActionInvariantUuId(action1Id);
366       action.setVersion(VERSION01.toString());
367       ActionEntity existingActionEntity = actionDao.get(action.toEntity());
368       action = existingActionEntity.toDto();
369       action.setActionInvariantUuId(UUID.randomUUID().toString());
370       //Persisting the updated entity
371       actionManager.updateAction(action, USER1);
372       Assert.fail();
373     } catch (ActionException e) {
374       Assert.assertEquals(e.getErrorCode(), ActionErrorConstants.ACTION_ENTITY_NOT_EXIST_CODE);
375     }
376   }
377
378   @Test(groups = "updateTestGroup", dependsOnMethods = {"updateTest"})
379   public void testUpdateUniqueId_negative() {
380     try {
381       Action action = new Action();
382       action.setActionInvariantUuId(action1Id);
383       action.setVersion(VERSION01.toString());
384       ActionEntity existingActionEntity = actionDao.get(action.toEntity());
385       //existingActionEntity.setActionUuId(UUID.randomUUID().toString());
386
387       action = existingActionEntity.toDto();
388       action.setActionUuId(UUID.randomUUID().toString());
389       //Persisting the updated entity
390       //actionManager.updateAction(existingActionEntity.toDto(),USER1);
391       actionManager.updateAction(action, USER1);
392       Assert.fail();
393     } catch (ActionException e) {
394       Assert.assertEquals(e.getErrorCode(), ActionErrorConstants.ACTION_UPDATE_NOT_ALLOWED_CODE);
395     }
396   }
397
398   @Test(groups = "updateTestGroup", dependsOnMethods = {"updateTest"})
399   public void testUpdateStatus_negative() {
400     try {
401       Action action = new Action();
402       action.setActionInvariantUuId(action1Id);
403       action.setVersion(VERSION01.toString());
404       ActionEntity existingActionEntity = actionDao.get(action.toEntity());
405       action = existingActionEntity.toDto();
406       action.setStatus(ActionStatus.Final);
407       //Persisting the updated entity
408       actionManager.updateAction(action, USER1);
409       Assert.fail();
410     } catch (ActionException e) {
411       Assert.assertEquals(e.getErrorCode(), ActionErrorConstants.ACTION_UPDATE_NOT_ALLOWED_CODE);
412     } catch (IllegalArgumentException ie) {
413       String message = ie.getMessage();
414       boolean result = message.contains("No enum constant");
415       Assert.assertEquals(true, result);
416     }
417   }
418
419   @Test(groups = "updateTestGroup", dependsOnMethods = {"updateTest"})
420   public void testUpdateOtherUser_negative() {
421     try {
422       Action action = new Action();
423       action.setActionInvariantUuId(action1Id);
424       action.setVersion(VERSION01.toString());
425       ActionEntity existingActionEntity = actionDao.get(action.toEntity());
426       action = existingActionEntity.toDto();
427       //existingActionEntity.setDescription("Testing Update using other user");
428       //Persisting the updated entity
429       actionManager.updateAction(action, USER2);
430       Assert.fail();
431     } catch (ActionException e) {
432       Assert.assertEquals(e.getErrorCode(),
433           ActionErrorConstants.ACTION_EDIT_ON_ENTITY_LOCKED_BY_OTHER_USER);
434     }
435   }
436
437   @Test(dependsOnMethods = {"createTest"})
438   public void testCheckOutOnCheckOut() {
439     try {
440       actionManager.checkout(action1Id, USER1);
441     } catch (ActionException wae) {
442       Assert
443           .assertEquals(wae.getErrorCode(), ActionErrorConstants.ACTION_CHECKOUT_ON_LOCKED_ENTITY);
444       Assert.assertEquals(wae.getDescription(),
445           "Can not check out versionable entity Action with id " + action1Id +
446               " since it is checked out by other user: " + USER1 + ".");
447     }
448   }
449
450   @Test(dependsOnMethods = {"createTest"})
451   public void testCheckOutOnCheckOutWithOtherUser() {
452     try {
453       actionManager.checkout(action1Id, "invlaiduser");
454     } catch (ActionException wae) {
455       Assert.assertEquals(wae.getErrorCode(),
456           ActionErrorConstants.ACTION_CHECKOUT_ON_LOCKED_ENTITY_OTHER_USER);
457       Assert.assertEquals(wae.getDescription(),
458           "Can not check out versionable entity Action with id " + action1Id +
459               " since it is checked out by other user: " + USER1 + ".");
460     }
461   }
462
463   @Test(dependsOnGroups = {"updateTestGroup"})
464   public void testCheckIn() {
465     Action action = actionManager.checkin(action1Id, USER1);
466     Assert.assertEquals(action.getActionInvariantUuId(), action1Id);
467     Assert.assertEquals(action.getStatus(), ActionStatus.Available);
468     Assert.assertEquals(action.getVersion(), VERSION01.toString());
469     Assert.assertNotNull(action.getActionUuId());
470   }
471
472   @Test(dependsOnMethods = {"testCheckIn"})
473   public void testUpdateOnCheckedInAction_negative() {
474     try {
475       Action action = new Action();
476       action.setActionInvariantUuId(action1Id);
477       action.setVersion(VERSION01.toString());
478       ActionEntity existingActionEntity = actionDao.get(action.toEntity());
479       //existingActionEntity.setDescription("Testing Update On Checked In Action");
480       //Persisting the updated entity
481       actionManager.updateAction(existingActionEntity.toDto(), USER1);
482       Assert.fail();
483     } catch (ActionException e) {
484       Assert.assertEquals(e.getErrorCode(), ActionErrorConstants.ACTION_UPDATE_ON_UNLOCKED_ENTITY);
485     }
486   }
487
488   @Test(dependsOnMethods = {"testUpdateOnCheckedInAction_negative"})
489   public void testSubmit() {
490     Action action = actionManager.submit(action1Id, USER1);
491     ActionEntity loadedAction = actionDao.get(action.toEntity());
492     assertActionEquals(action, loadedAction.toDto());
493   }
494
495   @Test(dependsOnMethods = {"testSubmit"})
496   public void testCheckInWithoutCheckout() {
497     try {
498       actionManager.checkin(action1Id, "invaliduser");
499     } catch (ActionException wae) {
500       Assert
501           .assertEquals(wae.getErrorCode(), ActionErrorConstants.ACTION_CHECKIN_ON_UNLOCKED_ENTITY);
502       Assert.assertEquals(wae.getDescription(),
503           "Can not check in versionable entity Action with id " + action1Id +
504               " since it is not checked out.");
505     }
506   }
507
508   @Test(dependsOnMethods = {"testSubmit"})
509   public void testCheckOut() {
510     final Version VERSION02 = new Version(1, 1);
511     Action action = null;
512     action = actionManager.checkout(action1Id, USER1);
513     ActionEntity loadedAction = actionDao.get(action.toEntity());
514     assertActionEquals(action, loadedAction.toDto());
515   }
516
517   @Test(dependsOnMethods = {"testCheckOut"})
518   public void testCheckInWithOtherUser() {
519     try {
520       actionManager.checkin(action1Id, "invaliduser");
521     } catch (ActionException wae) {
522       Assert.assertEquals(wae.getErrorCode(),
523           ActionErrorConstants.ACTION_CHECKIN_ON_ENTITY_LOCKED_BY_OTHER_USER);
524       Assert.assertEquals(wae.getDescription(),
525           "Can not check in versionable entity Action with id " + action1Id +
526               " since it is checked out by other user: " + USER1 + ".");
527     }
528   }
529
530   @Test(dependsOnMethods = {"testCheckOut"})
531   public void testSubmitOnCheckout() {
532     try {
533       actionManager.submit(action1Id, USER1);
534     } catch (ActionException wae) {
535       Assert.assertEquals(wae.getErrorCode(),
536           ActionErrorConstants.ACTION_SUBMIT_LOCKED_ENTITY_NOT_ALLOWED);
537       Assert.assertEquals(wae.getDescription(), "Versionable entity Action with id " + action1Id +
538           " can not be submitted since it is currently locked by user " + USER1 + ".");
539     }
540   }
541
542   @Test(dependsOnMethods = {"testCheckOut"})
543   public void testUndoCheckout() {
544     final Version VERSION11 = new Version(1, 1);
545     actionManager.undoCheckout(action1Id, USER1);
546     Action action = new Action();
547     action.setActionInvariantUuId(action1Id);
548     action.setVersion(VERSION11.toString());
549     ActionEntity existingActionEntity = actionDao.get(action.toEntity());
550     Assert.assertNull(existingActionEntity);
551   }
552
553   @Test
554   public void testUndoCheckoutOnCreate() {
555     Action action = actionManager.createAction(createAction(ACTION_6), USER1);
556     actionManager.undoCheckout(action.getActionInvariantUuId(), USER1);
557     ActionEntity existingActionEntity = actionDao.get(action.toEntity());
558     Assert.assertNull(existingActionEntity);
559   }
560
561   @Test
562   public void testGetECOMPComponents() {
563     List<EcompComponent> componentList = actionManager.getEcompComponents();
564     List<EcompComponent> expectedComponentList = new ArrayList<>();
565     expectedComponentList.add(new EcompComponent("MSO", "COMP-1"));
566     expectedComponentList.add(new EcompComponent("APP-C", "COMP-2"));
567     for (EcompComponent e : componentList) {
568       boolean res = expectedComponentList.contains(e);
569       Assert.assertEquals(res, true);
570     }
571   }
572
573   @Test
574   public void testgetActionsByActionUUID_Negative() {
575     try {
576       Action action = actionManager.getActionsByActionUuId("");
577       Assert.fail();
578     } catch (ActionException e) {
579       Assert.assertEquals(e.getErrorCode(), ActionErrorConstants.ACTION_ENTITY_NOT_EXIST_CODE);
580     }
581   }
582
583   @Test(dependsOnMethods = {"createTest"})
584   public void testgetActionsByActionUUID() {
585     Action action = actionManager.getActionsByActionUuId(actionUUId);
586     Assert.assertNotNull(action.getData());
587   }
588
589   @Test
590   public void testGetByCategory() {
591     createActionVersions(ACTION_2);
592     createActionVersions(ACTION_3);
593     createActionVersions(ACTION_4);
594     createActionVersions(ACTION_5);
595     List<Action> actions =
596         actionManager.getFilteredActions(ActionConstants.FILTER_TYPE_CATEGORY, "CAT-teSt");
597
598     List<String> actualNameVersionList = new ArrayList<String>();
599     List<String> expectedNameVersionList = new ArrayList<String>();
600     expectedNameVersionList.add("Test_Action4_list:2.2");
601     expectedNameVersionList.add("Test_Action4_list:2.0");
602     expectedNameVersionList.add("Test_Action2_list:2.2");
603     expectedNameVersionList.add("Test_Action2_list:2.0");
604     for (Action action : actions) {
605       System.out.println("action by category is::::");
606       System.out.println(action.getName() + " " + action.getVersion());
607       actualNameVersionList.add(action.getName() + ":" + action.getVersion());
608     }
609     Assert.assertEquals(4, actions.size());
610     Assert.assertEquals(expectedNameVersionList, actualNameVersionList);
611   }
612
613   @Test(dependsOnMethods = {"testGetByCategory"})
614   public void testGetByVendor() {
615     List<Action> actions =
616         actionManager.getFilteredActions(ActionConstants.FILTER_TYPE_VENDOR, "VendOr-tESt");
617
618     List<String> actualNameVersionList = new ArrayList<String>();
619     List<String> expectedNameVersionList = new ArrayList<String>();
620     expectedNameVersionList.add("Test_Action5_list:2.2");
621     expectedNameVersionList.add("Test_Action5_list:2.0");
622     expectedNameVersionList.add("Test_Action3_list:2.2");
623     expectedNameVersionList.add("Test_Action3_list:2.0");
624     for (Action action : actions) {
625       System.out.println("action by category is::::");
626       System.out.println(action.getName() + " " + action.getVersion());
627       actualNameVersionList.add(action.getName() + ":" + action.getVersion());
628     }
629     Assert.assertEquals(4, actions.size());
630     Assert.assertEquals(expectedNameVersionList, actualNameVersionList);
631   }
632
633   @Test(dependsOnMethods = {"testGetByCategory"})
634   public void testGetBySupportedModel() {
635     List<Action> actions =
636         actionManager.getFilteredActions(ActionConstants.FILTER_TYPE_MODEL, "MODEL-tEst");
637
638     List<String> actualNameVersionList = new ArrayList<>();
639     List<String> expectedNameVersionList = new ArrayList<>();
640     expectedNameVersionList.add("Test_Action4_list:2.2");
641     expectedNameVersionList.add("Test_Action4_list:2.0");
642     expectedNameVersionList.add("Test_Action2_list:2.2");
643     expectedNameVersionList.add("Test_Action2_list:2.0");
644     for (Action action : actions) {
645       actualNameVersionList.add(action.getName() + ":" + action.getVersion());
646     }
647     Assert.assertEquals(4, actions.size());
648     Assert.assertEquals(expectedNameVersionList, actualNameVersionList);
649   }
650
651   @Test(dependsOnMethods = {"testGetByCategory"})
652   public void testGetBySupportedComponent() {
653     List<Action> actions =
654         actionManager.getFilteredActions(ActionConstants.FILTER_TYPE_ECOMP_COMPONENT, "mso");
655
656     List<String> actualNameVersionList = new ArrayList<>();
657     List<String> expectedNameVersionList = new ArrayList<>();
658     expectedNameVersionList.add("Test_Action5_list:2.2");
659     expectedNameVersionList.add("Test_Action5_list:2.0");
660     expectedNameVersionList.add("Test_Action3_list:2.2");
661     expectedNameVersionList.add("Test_Action3_list:2.0");
662     for (Action action : actions) {
663       actualNameVersionList.add(action.getName() + ":" + action.getVersion());
664     }
665     Assert.assertEquals(4, actions.size());
666     Assert.assertEquals(expectedNameVersionList, actualNameVersionList);
667   }
668
669   @Test(dependsOnMethods = {"testGetByCategory"})
670   public void testGetAllActions() {
671     List<Action> actions =
672         actionManager.getFilteredActions(ActionConstants.FILTER_TYPE_NONE, "MSO");
673
674     List<String> actualNameVersionList = new ArrayList<>();
675     List<String> expectedNameVersionList = new ArrayList<>();
676
677     expectedNameVersionList.add("Test_Action5_list:2.2");
678     expectedNameVersionList.add("Test_Action5_list:2.0");
679     expectedNameVersionList.add("Test_Action3_list:2.2");
680     expectedNameVersionList.add("Test_Action3_list:2.0");
681     expectedNameVersionList.add("Test_Action4_list:2.2");
682     expectedNameVersionList.add("Test_Action4_list:2.0");
683     expectedNameVersionList.add("Test_Action2_list:2.2");
684     expectedNameVersionList.add("Test_Action2_list:2.0");
685     for (Action action : actions) {
686       actualNameVersionList.add(action.getName() + ":" + action.getVersion());
687     }
688     Assert.assertEquals(8, actions.size());
689
690     for (String s : actualNameVersionList) {
691       boolean res = expectedNameVersionList.contains(s);
692       Assert.assertEquals(res, true);
693     }
694   }
695
696   @Test(dependsOnMethods = {"testGetAllActions"})
697   public void testDeleteCheckedOutAction_Negative() {
698     try {
699       initDeleteActionTest();
700       String deleteActionInvariantId = deleteAction.getActionInvariantUuId();
701       actionManager.deleteAction(deleteActionInvariantId, USER1);
702       Assert.fail();
703     } catch (ActionException e) {
704       Assert.assertEquals(e.getErrorCode(), ActionErrorConstants.ACTION_DELETE_ON_LOCKED_ENTITY_CODE);
705       Assert.assertEquals(e.getDescription(), String.format(
706           "Can not delete versionable entity Action with id %s since it is checked out by other user: %s",
707           deleteAction.getActionInvariantUuId(), USER1 + "."));
708     }
709   }
710
711   @Test(dependsOnMethods = {"testDeleteCheckedOutAction_Negative"})
712   public void testDeleteAction() {
713     try {
714       String deleteActionInvariantId = deleteAction.getActionInvariantUuId();
715       actionManager.checkin(deleteActionInvariantId, USER1);
716       actionManager.deleteAction(deleteActionInvariantId, USER1);
717     } catch (ActionException e) {
718       Assert.fail("Delete action test failed with exception : " + e.getDescription());
719     }
720   }
721
722   @Test(dependsOnMethods = {"testDeleteAction"})
723   public void testDeletedActionVersioningOperations_Negative() {
724     String deleteActionInvariantId = deleteAction.getActionInvariantUuId();
725     try {
726       actionManager.checkout(deleteActionInvariantId, USER1);
727       Assert.fail();
728     } catch (ActionException e) {
729       Assert.assertEquals(e.getErrorCode(), ActionErrorConstants.ACTION_ENTITY_NOT_EXIST_CODE);
730       Assert.assertEquals(e.getDescription(), ActionErrorConstants.ACTION_ENTITY_NOT_EXIST);
731     }
732     try {
733       actionManager.checkin(deleteActionInvariantId, USER1);
734       Assert.fail();
735     } catch (ActionException e) {
736       Assert.assertEquals(e.getErrorCode(), ActionErrorConstants.ACTION_ENTITY_NOT_EXIST_CODE);
737       Assert.assertEquals(e.getDescription(), ActionErrorConstants.ACTION_ENTITY_NOT_EXIST);
738     }
739     try {
740       actionManager.submit(deleteActionInvariantId, USER1);
741       Assert.fail();
742     } catch (ActionException e) {
743       Assert.assertEquals(e.getErrorCode(), ActionErrorConstants.ACTION_ENTITY_NOT_EXIST_CODE);
744       Assert.assertEquals(e.getDescription(), ActionErrorConstants.ACTION_ENTITY_NOT_EXIST);
745     }
746     try {
747       actionManager.undoCheckout(deleteActionInvariantId, USER1);
748       Assert.fail();
749     } catch (ActionException e) {
750       Assert.assertEquals(e.getErrorCode(), ActionErrorConstants.ACTION_ENTITY_NOT_EXIST_CODE);
751       Assert.assertEquals(e.getDescription(), ActionErrorConstants.ACTION_ENTITY_NOT_EXIST);
752     }
753     try {
754       actionManager.deleteAction(deleteActionInvariantId, USER1);
755       Assert.fail();
756     } catch (ActionException e) {
757       Assert.assertEquals(e.getErrorCode(), ActionErrorConstants.ACTION_ENTITY_NOT_EXIST_CODE);
758       Assert.assertEquals(e.getDescription(), ActionErrorConstants.ACTION_ENTITY_NOT_EXIST);
759     }
760   }
761
762   @Test(dependsOnMethods = {"testDeleteAction"})
763   public void testCreateActionWithDeletedActionName_Negative() {
764     try {
765       actionManager.createAction(createAction(ACTION_TEST_DELETE), USER1);
766       Assert.fail();
767     } catch (ActionException e) {
768       Assert.assertEquals(e.getErrorCode(), ActionErrorConstants.ACTION_ENTITY_UNIQUE_VALUE_ERROR);
769       Assert.assertEquals(e.getDescription(), String
770           .format(ActionErrorConstants.ACTION_ENTITY_UNIQUE_VALUE_MSG, ActionConstants.UniqueValues.ACTION_NAME,
771               deleteAction.getName()));
772     }
773   }
774
775   @Test(dependsOnMethods = {"testDeleteAction"})
776   public void testDeletedActionGetQueries() {
777     String deleteActionInvariantId = deleteAction.getActionInvariantUuId();
778     List<Action> invariantFetchResults =
779         actionManager.getActionsByActionInvariantUuId(deleteActionInvariantId);
780     Assert.assertEquals(invariantFetchResults.size(), 3);
781     for (Action a : invariantFetchResults) {
782       Assert.assertEquals(a.getStatus(), ActionStatus.Deleted);
783     }
784
785     Action actionUUIDFetchResult =
786         actionManager.getActionsByActionUuId(deleteAction.getActionUuId());
787     Assert.assertEquals(actionUUIDFetchResult.getStatus(), ActionStatus.Deleted);
788
789     List<Action> nameFetchResults =
790         actionManager.getFilteredActions(ActionConstants.FILTER_TYPE_NAME, "Test_Delete_Action");
791     Assert.assertEquals(nameFetchResults.size(), 3);
792     for (Action a : nameFetchResults) {
793       Assert.assertEquals(a.getStatus(), ActionStatus.Deleted);
794     }
795
796     List<Action> filteredActions =
797         actionManager.getFilteredActions(ActionConstants.FILTER_TYPE_VENDOR, "Vendor-Delete");
798     Assert.assertEquals(filteredActions.size(), 0);
799     filteredActions =
800         actionManager.getFilteredActions(ActionConstants.FILTER_TYPE_CATEGORY, "Cat-Delete-test");
801     Assert.assertEquals(filteredActions.size(), 0);
802     filteredActions =
803         actionManager.getFilteredActions(ActionConstants.FILTER_TYPE_ECOMP_COMPONENT, "MSO-delete");
804     Assert.assertEquals(filteredActions.size(), 0);
805     filteredActions =
806         actionManager.getFilteredActions(ActionConstants.FILTER_TYPE_MODEL, "Model-Delete");
807     Assert.assertEquals(filteredActions.size(), 0);
808   }
809
810   /***
811    * ACTION ARTIFACT OPERATION TEST CASES
812    ***/
813
814   @Test
815   public void testUploadArtifact() {
816     actionArtifact = new ActionArtifact();
817     File resourceFile = new File(
818         this.getClass().getClassLoader().getResource(ACTION_TEST_ARTIFACT_FILE_NAME).getPath());
819     FileInputStream fileInputStream;
820     //Create payload from the test resource file
821     byte[] payload = new byte[(int) resourceFile.length()];
822     try {
823       fileInputStream = new FileInputStream(resourceFile);
824       fileInputStream.read(payload);
825       fileInputStream.close();
826       actionArtifact.setArtifact(payload);
827       actionArtifact.setArtifactName(ACTION_TEST_ARTIFACT_FILE_NAME);
828       actionArtifact.setArtifactLabel("Test Artifact Label");
829       actionArtifact.setArtifactDescription("Test Artifact Description");
830       actionArtifact.setArtifactProtection(ActionArtifactProtection.readWrite.name());
831     } catch (IOException e) {
832       e.printStackTrace();
833     }
834
835     //Create action for artifact upload test
836     testArtifactAction = actionManager.createAction(createAction(ARTIFACT_TEST_ACTION), USER1);
837     //Generate Expected artifact UUID
838     expectedArtifactUUID =
839         generateActionArtifactUUID(testArtifactAction, ACTION_TEST_ARTIFACT_FILE_NAME);
840     //Upload the artifact
841     ActionArtifact response = actionManager
842         .uploadArtifact(actionArtifact, testArtifactAction.getActionInvariantUuId(), USER1);
843     //Validate if generated and the expected artifact UUID is same
844     Assert.assertEquals(expectedArtifactUUID, response.getArtifactUuId());
845     //Fetch the data field of the updated action version
846     Action updatedAction = actionManager.getActionsByActionUuId(testArtifactAction.getActionUuId());
847     List<ActionArtifact> updatedArtifactList = updatedAction.getArtifacts();
848     for (ActionArtifact artifact : updatedArtifactList) {
849       //Validate the artifact metadata
850       Assert.assertEquals(artifact.getArtifactName(), actionArtifact.getArtifactName());
851       Assert.assertEquals(artifact.getArtifactLabel(), actionArtifact.getArtifactLabel());
852       Assert
853           .assertEquals(artifact.getArtifactDescription(), actionArtifact.getArtifactDescription());
854       Assert.assertEquals(artifact.getArtifactProtection(), actionArtifact.getArtifactProtection());
855     }
856   }
857
858   @Test(dependsOnMethods = {"testUploadArtifact"})
859   public void testUploadArtifactInvalidActionInvId_negative() {
860     ActionArtifact testArtifact = new ActionArtifact();
861     testArtifact.setArtifact("testData".getBytes());
862     testArtifact.setArtifactName(ACTION_TEST_ARTIFACT_FILE_NAME);
863     try {
864       actionManager.uploadArtifact(testArtifact, "INVALID_UUID", USER1);
865     } catch (ActionException ae) {
866       Assert.assertEquals(ae.getErrorCode(), ActionErrorConstants.ACTION_ENTITY_NOT_EXIST_CODE);
867       Assert.assertEquals(ae.getDescription(), ActionErrorConstants.ACTION_ENTITY_NOT_EXIST);
868     }
869   }
870
871   @Test(dependsOnMethods = {"testUploadArtifact"})
872   public void testUploadArtifactSameName_negative() {
873     try {
874       actionManager
875           .uploadArtifact(actionArtifact, testArtifactAction.getActionInvariantUuId(), USER1);
876     } catch (ActionException ae) {
877       Assert.assertEquals(ae.getErrorCode(), ActionErrorConstants.ACTION_ARTIFACT_ALREADY_EXISTS_CODE);
878       Assert.assertEquals(ae.getDescription(), String
879           .format(ActionErrorConstants.ACTION_ARTIFACT_ALREADY_EXISTS, testArtifactAction.getActionInvariantUuId()));
880     }
881   }
882
883   @Test(dependsOnMethods = {"testUploadArtifact"})
884   public void testUploadArtifactCheckedOutOtherUser_negative() {
885     try {
886       actionManager
887           .uploadArtifact(actionArtifact, testArtifactAction.getActionInvariantUuId(), USER2);
888     } catch (ActionException ae) {
889       Assert.assertEquals(ae.getErrorCode(), ActionErrorConstants.ACTION_EDIT_ON_ENTITY_LOCKED_BY_OTHER_USER);
890       Assert.assertEquals(ae.getDescription(),
891           "Versionable entity Action with id " + testArtifactAction.getActionInvariantUuId() +
892               " can not be updated since it is locked by other user " + USER1 + ".");
893     }
894   }
895
896   @Test(dependsOnMethods = {"testUploadArtifact"})
897   public void testUploadArtifactUnlockedAction_negative() {
898     try {
899       testArtifactAction =
900           actionManager.checkin(testArtifactAction.getActionInvariantUuId(), USER1);
901       actionManager
902           .uploadArtifact(actionArtifact, testArtifactAction.getActionInvariantUuId(), USER1);
903     } catch (ActionException ae) {
904       Assert.assertEquals(ae.getErrorCode(), ActionErrorConstants.ACTION_UPDATE_ON_UNLOCKED_ENTITY);
905       Assert.assertEquals(ae.getDescription(), "Can not update versionable entity Action with id " +
906           testArtifactAction.getActionInvariantUuId() + " since it is not checked out.");
907     }
908   }
909
910   @Test(dependsOnMethods = {"testUploadArtifact"})
911   public void testDownloadArtifact() {
912     String actionUUID = testArtifactAction.getActionUuId();
913     ActionArtifact response = actionManager.downloadArtifact(actionUUID, expectedArtifactUUID);
914     Assert.assertEquals(actionArtifact.getArtifactName(), response.getArtifactName());
915     Assert.assertEquals(actionArtifact.getArtifact(), response.getArtifact());
916   }
917
918   @Test(dependsOnMethods = {"testUploadArtifact"})
919   public void testDownloadArtifactNegativeInvalidArtifact() {
920     String actionUUID = testArtifactAction.getActionUuId();
921     String artifactUUID = "negativeArtifact";
922     try {
923       ActionArtifact response = actionManager.downloadArtifact(actionUUID, artifactUUID);
924     } catch (ActionException ae) {
925       Assert.assertEquals(ae.getErrorCode(), ActionErrorConstants.ACTION_ARTIFACT_ENTITY_NOT_EXIST_CODE);
926     }
927
928   }
929
930   @Test
931   public void testDownloadArtifactNegativeInvalidAction() {
932     String actionUUID = "NegativeAction";
933     try {
934       ActionArtifact response = actionManager.downloadArtifact(actionUUID, expectedArtifactUUID);
935     } catch (ActionException ae) {
936       Assert.assertEquals(ae.getErrorCode(), ActionErrorConstants.ACTION_ENTITY_NOT_EXIST_CODE);
937     }
938
939   }
940
941   @Test
942   public void testDeleteArtifactInvalidActInvId() {
943     try {
944       actionManager.deleteArtifact("action2Id", "1234", USER1);
945     } catch (ActionException e) {
946       Assert.assertEquals(e.getErrorCode(), ActionErrorConstants.ACTION_ENTITY_NOT_EXIST_CODE);
947       Assert.assertEquals(e.getDescription(), ActionErrorConstants.ACTION_ENTITY_NOT_EXIST);
948     }
949   }
950
951   @Test(dependsOnMethods = {"testGetByInvIdOnCreate"})
952   public void testDeleteArtifactInvalidArtifactUUID() {
953     try {
954       actionManager.deleteArtifact(action2Id, "1234", USER1);
955     } catch (ActionException e) {
956       Assert.assertEquals(e.getErrorCode(),
957           ActionErrorConstants.ACTION_ARTIFACT_ENTITY_NOT_EXIST_CODE);
958       Assert
959           .assertEquals(e.getDescription(), ActionErrorConstants.ACTION_ARTIFACT_ENTITY_NOT_EXIST);
960     }
961   }
962
963   @Test(dependsOnMethods = {"testUploadArtifact"})
964   public void testDeleteReadOnlyArtifact() {
965     ActionArtifact testArtifact = null;
966     String artifactUUID = null;
967     try {
968       testArtifact = new ActionArtifact();
969       testArtifact.setArtifact("testData".getBytes());
970       testArtifact.setArtifactProtection(ActionArtifactProtection.readOnly.name());
971       testArtifact.setArtifactName("TestRO.txt");
972       actionManager
973           .uploadArtifact(testArtifact, testArtifactAction.getActionInvariantUuId(), USER1);
974       artifactUUID = testArtifact.getArtifactUuId();
975       actionManager.deleteArtifact(testArtifactAction.getActionInvariantUuId(),
976           testArtifact.getArtifactUuId(), USER1);
977
978     } catch (ActionException e) {
979       Assert.assertEquals(e.getErrorCode(), ActionErrorConstants.ACTION_ARTIFACT_DELETE_READ_ONLY);
980       Assert.assertEquals(e.getDescription(),
981           ActionErrorConstants.ACTION_ARTIFACT_DELETE_READ_ONLY_MSG);
982     }
983
984     //cleanup uploaded document after test
985     testArtifact = new ActionArtifact();
986     testArtifact.setArtifactUuId(artifactUUID);
987     testArtifact.setArtifactProtection(ActionArtifactProtection.readWrite.name());
988     actionManager.updateArtifact(testArtifact, testArtifactAction.getActionInvariantUuId(), USER1);
989     actionManager
990         .deleteArtifact(testArtifactAction.getActionInvariantUuId(), testArtifact.getArtifactUuId(),
991             USER1);
992   }
993
994   @Test(dependsOnMethods = {"testUploadArtifact"})
995   public void testDeleteArtifactLockedByOtherUser() {
996     try {
997       actionManager.deleteArtifact(testArtifactAction.getActionInvariantUuId(),
998           actionArtifact.getArtifactUuId(), USER2);
999     } catch (ActionException ae) {
1000       Assert.assertEquals(ae.getErrorCode(), ActionErrorConstants.ACTION_ARTIFACT_DEL_LOCKED_OTHER_USER_CODE);
1001       Assert.assertEquals(ae.getDescription(),
1002           String.format(ActionErrorConstants.ACTION_ARTIFACT_DEL_LOCKED_OTHER_USER, USER1));
1003     }
1004   }
1005
1006   @Test(dependsOnMethods = {"testUploadArtifactUnlockedAction_negative"})
1007   public void testDeleteArtifactOnUnlockedAction() {
1008     try {
1009       actionManager.deleteArtifact(testArtifactAction.getActionInvariantUuId(),
1010           actionArtifact.getArtifactUuId(), USER1);
1011     } catch (ActionException ae) {
1012       Assert.assertEquals(ae.getErrorCode(), ActionErrorConstants.ACTION_NOT_LOCKED_CODE);
1013       Assert.assertEquals(ae.getDescription(), ActionErrorConstants.ACTION_NOT_LOCKED_MSG);
1014     }
1015   }
1016
1017   @Test(dependsOnMethods = {"testUploadArtifact"})
1018   public void testDeleteArtifact() {
1019     try {
1020       ActionArtifact testArtifact = new ActionArtifact();
1021       testArtifact.setArtifact("testData".getBytes());
1022       testArtifact.setArtifactName("Test_ToBeDel.txt");
1023       testArtifact.setArtifactProtection(ActionArtifactProtection.readWrite.name());
1024       actionManager
1025           .uploadArtifact(testArtifact, testArtifactAction.getActionInvariantUuId(), USER1);
1026       actionManager.deleteArtifact(testArtifactAction.getActionInvariantUuId(),
1027           testArtifact.getArtifactUuId(), USER1);
1028       ActionArtifact response = actionManager
1029           .downloadArtifact(testArtifactAction.getActionUuId(), testArtifact.getArtifactUuId());
1030     } catch (ActionException e) {
1031       Assert.assertEquals(e.getErrorCode(),
1032           ActionErrorConstants.ACTION_ARTIFACT_ENTITY_NOT_EXIST_CODE);
1033       Assert
1034           .assertEquals(e.getDescription(), ActionErrorConstants.ACTION_ARTIFACT_ENTITY_NOT_EXIST);
1035     }
1036   }
1037
1038   @Test(dependsOnMethods = {"testUploadArtifact"})
1039   public void testUpdateArtifact() {
1040     ActionArtifact updatedArtifact = new ActionArtifact();
1041     File resourceFile = new File(
1042         this.getClass().getClassLoader().getResource(ACTION_TEST_UPDATE_ARTIFACT_FILE_NAME)
1043             .getPath());
1044     FileInputStream fileInputStream;
1045     //Create payload from the test resource file
1046     byte[] payload = new byte[(int) resourceFile.length()];
1047     try {
1048       fileInputStream = new FileInputStream(resourceFile);
1049       fileInputStream.read(payload);
1050       fileInputStream.close();
1051       updatedArtifact.setArtifactUuId(
1052           generateActionArtifactUUID(testArtifactAction, ACTION_TEST_ARTIFACT_FILE_NAME));
1053       updatedArtifact.setArtifact(payload);
1054       updatedArtifact.setArtifactName(ACTION_TEST_ARTIFACT_FILE_NAME);
1055       updatedArtifact.setArtifactLabel("Test Artifact Update Label");
1056       updatedArtifact.setArtifactDescription("Test Artifact Update Description");
1057       updatedArtifact.setArtifactProtection(ActionArtifactProtection.readWrite.name());
1058     } catch (IOException e) {
1059       e.printStackTrace();
1060     }
1061
1062     String actionInvarientUUID = testArtifactAction.getActionInvariantUuId();
1063     actionManager.updateArtifact(updatedArtifact, actionInvarientUUID, USER1);
1064
1065     String actionUUID = testArtifactAction.getActionUuId();
1066     Action action = actionManager.getActionsByActionUuId(actionUUID);
1067     List<ActionArtifact> artifacts = action.getArtifacts();
1068     for (ActionArtifact actionArtifact : artifacts) {
1069       Assert.assertEquals(actionArtifact.getArtifactName(), updatedArtifact.getArtifactName());
1070       Assert.assertEquals(actionArtifact.getArtifactLabel(), updatedArtifact.getArtifactLabel());
1071       Assert.assertEquals(actionArtifact.getArtifactDescription(),
1072           updatedArtifact.getArtifactDescription());
1073       Assert.assertEquals(actionArtifact.getArtifactProtection(),
1074           updatedArtifact.getArtifactProtection());
1075     }
1076   }
1077
1078   @Test(dependsOnMethods = {"testUploadArtifact"})
1079   public void testUpdateArtifact_ArtifactNotPresent_Negative() {
1080     ActionArtifact invalidActionArtifact = new ActionArtifact();
1081     String artifactUUID = generateActionArtifactUUID(testArtifactAction, "ArtifactNotPresent");
1082     invalidActionArtifact.setArtifactUuId(artifactUUID);
1083     try {
1084       actionManager
1085           .updateArtifact(invalidActionArtifact, testArtifactAction.getActionInvariantUuId(),
1086               USER1);
1087     } catch (ActionException actionException) {
1088       Assert.assertEquals(actionException.getDescription(), ActionErrorConstants.ACTION_ARTIFACT_ENTITY_NOT_EXIST);
1089     }
1090   }
1091
1092   @Test(dependsOnMethods = {"testUploadArtifact"})
1093   public void testUpdateArtifact_ArtifactNameUpdate_Negative() {
1094     String invariantUUID = testArtifactAction.getActionInvariantUuId();
1095     ActionArtifact artifactToUpdate = new ActionArtifact();
1096     artifactToUpdate.setArtifactUuId(actionArtifact.getArtifactUuId());
1097     artifactToUpdate.setArtifactName("UpdatingName");
1098
1099     try {
1100       actionManager.updateArtifact(artifactToUpdate, invariantUUID, USER1);
1101     } catch (ActionException actionException) {
1102       Assert.assertEquals(actionException.getDescription(), ActionErrorConstants.ACTION_ARTIFACT_UPDATE_NAME_INVALID);
1103     }
1104   }
1105
1106   @Test(dependsOnMethods = {"testUploadArtifact"})
1107   void testUpdateArtifact_CheckoutByOtherUser_Negative() {
1108     String invariantUUID = testArtifactAction.getActionInvariantUuId();
1109     ActionArtifact artifactToUpdate = new ActionArtifact();
1110     artifactToUpdate.setArtifactUuId(actionArtifact.getArtifactUuId());
1111     artifactToUpdate.setArtifactLabel("CheckoutbyOtherUser label");
1112
1113     try {
1114       actionManager.updateArtifact(artifactToUpdate, invariantUUID, USER2);
1115     } catch (ActionException actionException) {
1116       Assert
1117           .assertEquals(actionException.getErrorCode(), ActionErrorConstants.ACTION_EDIT_ON_ENTITY_LOCKED_BY_OTHER_USER);
1118       Assert.assertEquals(actionException.getDescription(),
1119           "Versionable entity Action with id " + invariantUUID +
1120               " can not be updated since it is locked by other user " + USER1 + ".");
1121     }
1122     System.out.println("asdf");
1123   }
1124
1125   @Test(dependsOnMethods = {"testUploadArtifact"})
1126   void testUpdateArtifact_ArtifactProtectionReadOnly_CanNotUpdate_Negative() {
1127     String invariantUUID = testArtifactAction.getActionInvariantUuId();
1128     ActionArtifact artifactToUpdate = new ActionArtifact();
1129     artifactToUpdate.setArtifactUuId(actionArtifact.getArtifactUuId());
1130     artifactToUpdate.setArtifactProtection(ActionArtifactProtection.readOnly.name());
1131     actionManager.updateArtifact(artifactToUpdate, invariantUUID, USER1);
1132
1133     artifactToUpdate.setArtifactLabel("test label");
1134     artifactToUpdate.setArtifactDescription("test description");
1135     artifactToUpdate.setArtifactProtection(ActionArtifactProtection.readWrite.name());
1136     try {
1137       actionManager.updateArtifact(artifactToUpdate, invariantUUID, USER1);
1138     } catch (ActionException actionExecption) {
1139       Assert.assertEquals(actionExecption.getDescription(), ActionErrorConstants.ACTION_ARTIFACT_UPDATE_READ_ONLY_MSG);
1140     }
1141   }
1142
1143   // Function which will take action as input string and create action
1144   // After create multiple versions of same action
1145   // Final versions :1.0, 2.0
1146   // Last minor version :2.2
1147   // Candidate version :2.3
1148   private void createActionVersions(String input) {
1149     Action action1 = createAction(input);
1150     Action action = actionManager.createAction(action1, USER1);
1151     String Id = action.getActionInvariantUuId();
1152
1153     actionManager.checkin(Id, USER1);
1154     actionManager.submit(Id, USER1); // 1.0
1155     actionManager.checkout(Id, USER1);
1156     actionManager.checkin(Id, USER1);
1157     actionManager.submit(Id, USER1);//2.0
1158     actionManager.checkout(Id, USER1);
1159     actionManager.checkin(Id, USER1);
1160     actionManager.checkout(Id, USER1);
1161     actionManager.checkin(Id, USER1); //2.2
1162     actionManager.checkout(Id, USER1); //2.3 candidate
1163   }
1164
1165   private Action updateData(Action action) {
1166     Map<String, String> dataMap = new LinkedHashMap<>();
1167     dataMap.put(ActionConstants.UNIQUE_ID, action.getActionUuId());
1168     dataMap.put(ActionConstants.VERSION, action.getVersion());
1169     dataMap.put(ActionConstants.INVARIANTUUID, action.getActionInvariantUuId());
1170     dataMap.put(ActionConstants.STATUS, ActionStatus.Locked.name());
1171
1172     String data = action.getData();
1173     Map<String, String> currentDataMap = JsonUtil.json2Object(data, LinkedHashMap.class);
1174     dataMap.putAll(currentDataMap);
1175     data = JsonUtil.object2Json(dataMap);
1176     action.setData(data);
1177     return action;
1178   }
1179
1180   private void initDeleteActionTest() {
1181     deleteAction = actionManager.createAction(createAction(ACTION_TEST_DELETE), USER1);
1182     String deleteActionInvariantId = deleteAction.getActionInvariantUuId();
1183     actionManager.checkin(deleteActionInvariantId, USER1);
1184     actionManager.submit(deleteActionInvariantId, USER1); // 1.0
1185     actionManager.checkout(deleteActionInvariantId, USER1);
1186     actionManager.checkin(deleteActionInvariantId, USER1);
1187     actionManager.submit(deleteActionInvariantId, USER1);//2.0
1188     actionManager.checkout(deleteActionInvariantId, USER1);
1189   }
1190
1191   private int getEffectiveVersion(String actionVersion) {
1192     Version version = Version.valueOf(actionVersion);
1193     return version.getMajor() * 10000 + version.getMinor();
1194   }
1195
1196   private String generateActionArtifactUUID(Action action, String artifactName) {
1197     int effectiveVersion = getEffectiveVersion(action.getVersion());
1198     //Upper case for maintaining case-insensitive behavior for the artifact names
1199     String artifactUUIDString =
1200         action.getName().toUpperCase() + effectiveVersion + artifactName.toUpperCase();
1201     String generateArtifactUUID =
1202         UUID.nameUUIDFromBytes((artifactUUIDString).getBytes()).toString();
1203     String artifactUUID = generateArtifactUUID.replace("-", "");
1204     return artifactUUID.toUpperCase();
1205   }
1206
1207 }