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