1 17 package org.alfresco.repo.action; 18 19 import java.io.Serializable ; 20 import java.util.HashMap ; 21 import java.util.List ; 22 import java.util.Map ; 23 24 import org.alfresco.model.ContentModel; 25 import org.alfresco.repo.action.evaluator.ComparePropertyValueEvaluator; 26 import org.alfresco.repo.action.evaluator.InCategoryEvaluator; 27 import org.alfresco.repo.action.evaluator.NoConditionEvaluator; 28 import org.alfresco.repo.action.evaluator.compare.ComparePropertyValueOperation; 29 import org.alfresco.repo.action.executer.AddFeaturesActionExecuter; 30 import org.alfresco.repo.action.executer.CheckInActionExecuter; 31 import org.alfresco.repo.action.executer.CheckOutActionExecuter; 32 import org.alfresco.repo.action.executer.CompositeActionExecuter; 33 import org.alfresco.repo.action.executer.MoveActionExecuter; 34 import org.alfresco.repo.content.MimetypeMap; 35 import org.alfresco.repo.transaction.TransactionUtil; 36 import org.alfresco.service.cmr.action.Action; 37 import org.alfresco.service.cmr.action.ActionCondition; 38 import org.alfresco.service.cmr.action.ActionConditionDefinition; 39 import org.alfresco.service.cmr.action.ActionDefinition; 40 import org.alfresco.service.cmr.action.CompositeAction; 41 import org.alfresco.service.cmr.repository.ContentData; 42 import org.alfresco.service.cmr.repository.NodeRef; 43 import org.alfresco.service.cmr.repository.NodeService; 44 import org.alfresco.service.namespace.QName; 45 import org.alfresco.service.transaction.TransactionService; 46 import org.alfresco.util.BaseAlfrescoSpringTest; 47 48 53 public class ActionServiceImplTest extends BaseAlfrescoSpringTest 54 { 55 private static final String BAD_NAME = "badName"; 56 57 private NodeRef nodeRef; 58 59 @Override 60 protected void onSetUpInTransaction() throws Exception 61 { 62 super.onSetUpInTransaction(); 63 64 this.nodeRef = this.nodeService.createNode( 66 this.rootNodeRef, 67 ContentModel.ASSOC_CHILDREN, 68 QName.createQName("{test}testnode"), 69 ContentModel.TYPE_CONTENT).getChildRef(); 70 this.nodeService.setProperty( 71 this.nodeRef, 72 ContentModel.PROP_CONTENT, 73 new ContentData(null, MimetypeMap.MIMETYPE_TEXT_PLAIN, 0L, null)); 74 } 75 76 79 public void testGetActionDefinition() 80 { 81 ActionDefinition action = actionService.getActionDefinition(AddFeaturesActionExecuter.NAME); 82 assertNotNull(action); 83 assertEquals(AddFeaturesActionExecuter.NAME, action.getName()); 84 85 ActionConditionDefinition nullCondition = this.actionService.getActionConditionDefinition(BAD_NAME); 86 assertNull(nullCondition); 87 } 88 89 92 public void testGetActionDefinitions() 93 { 94 List <ActionDefinition> defintions = this.actionService.getActionDefinitions(); 95 assertNotNull(defintions); 96 assertFalse(defintions.isEmpty()); 97 98 for (ActionDefinition definition : defintions) 99 { 100 System.out.println(definition.getTitle()); 101 } 102 } 103 104 107 public void testGetActionConditionDefinition() 108 { 109 ActionConditionDefinition condition = this.actionService.getActionConditionDefinition(NoConditionEvaluator.NAME); 110 assertNotNull(condition); 111 assertEquals(NoConditionEvaluator.NAME, condition.getName()); 112 113 ActionConditionDefinition nullCondition = this.actionService.getActionConditionDefinition(BAD_NAME); 114 assertNull(nullCondition); 115 } 116 117 121 public void testGetActionConditionDefinitions() 122 { 123 List <ActionConditionDefinition> defintions = this.actionService.getActionConditionDefinitions(); 124 assertNotNull(defintions); 125 assertFalse(defintions.isEmpty()); 126 127 for (ActionConditionDefinition definition : defintions) 128 { 129 System.out.println(definition.getTitle()); 130 } 131 } 132 133 136 public void testCreateActionCondition() 137 { 138 ActionCondition condition = this.actionService.createActionCondition(NoConditionEvaluator.NAME); 139 assertNotNull(condition); 140 assertEquals(NoConditionEvaluator.NAME, condition.getActionConditionDefinitionName()); 141 } 142 143 146 public void testCreateAction() 147 { 148 Action action = this.actionService.createAction(AddFeaturesActionExecuter.NAME); 149 assertNotNull(action); 150 assertEquals(AddFeaturesActionExecuter.NAME, action.getActionDefinitionName()); 151 } 152 153 156 public void testCreateCompositeAction() 157 { 158 CompositeAction action = this.actionService.createCompositeAction(); 159 assertNotNull(action); 160 assertEquals(CompositeActionExecuter.NAME, action.getActionDefinitionName()); 161 } 162 163 166 public void testEvaluateAction() 167 { 168 Action action = this.actionService.createAction(AddFeaturesActionExecuter.NAME); 169 assertTrue(this.actionService.evaluateAction(action, this.nodeRef)); 170 171 ActionCondition condition = this.actionService.createActionCondition(ComparePropertyValueEvaluator.NAME); 172 condition.setParameterValue(ComparePropertyValueEvaluator.PARAM_VALUE, "*.doc"); 173 action.addActionCondition(condition); 174 175 assertFalse(this.actionService.evaluateAction(action, this.nodeRef)); 176 this.nodeService.setProperty(this.nodeRef, ContentModel.PROP_NAME, "myDocument.doc"); 177 assertTrue(this.actionService.evaluateAction(action, this.nodeRef)); 178 179 ActionCondition condition2 = this.actionService.createActionCondition(ComparePropertyValueEvaluator.NAME); 180 condition2.setParameterValue(ComparePropertyValueEvaluator.PARAM_VALUE, "my"); 181 action.addActionCondition(condition2); 182 assertTrue(this.actionService.evaluateAction(action, this.nodeRef)); 183 184 this.nodeService.setProperty(this.nodeRef, ContentModel.PROP_NAME, "document.doc"); 185 assertFalse(this.actionService.evaluateAction(action, this.nodeRef)); 186 } 187 188 191 public void testEvaluateActionCondition() 192 { 193 ActionCondition condition = this.actionService.createActionCondition(ComparePropertyValueEvaluator.NAME); 194 condition.setParameterValue(ComparePropertyValueEvaluator.PARAM_VALUE, "*.doc"); 195 196 assertFalse(this.actionService.evaluateActionCondition(condition, this.nodeRef)); 197 this.nodeService.setProperty(this.nodeRef, ContentModel.PROP_NAME, "myDocument.doc"); 198 assertTrue(this.actionService.evaluateActionCondition(condition, this.nodeRef)); 199 200 condition.setInvertCondition(true); 202 assertFalse(this.actionService.evaluateActionCondition(condition, this.nodeRef)); 203 } 204 205 208 public void testExecuteAction() 209 { 210 assertFalse(this.nodeService.hasAspect(this.nodeRef, ContentModel.ASPECT_VERSIONABLE)); 211 212 Action action = this.actionService.createAction(AddFeaturesActionExecuter.NAME); 213 action.setParameterValue(AddFeaturesActionExecuter.PARAM_ASPECT_NAME, ContentModel.ASPECT_VERSIONABLE); 214 215 this.actionService.executeAction(action, this.nodeRef); 216 assertTrue(this.nodeService.hasAspect(this.nodeRef, ContentModel.ASPECT_VERSIONABLE)); 217 218 this.nodeService.removeAspect(this.nodeRef, ContentModel.ASPECT_VERSIONABLE); 219 assertFalse(this.nodeService.hasAspect(this.nodeRef, ContentModel.ASPECT_VERSIONABLE)); 220 221 ActionCondition condition = this.actionService.createActionCondition(ComparePropertyValueEvaluator.NAME); 222 condition.setParameterValue(ComparePropertyValueEvaluator.PARAM_VALUE, "*.doc"); 223 action.addActionCondition(condition); 224 225 this.actionService.executeAction(action, this.nodeRef); 226 assertFalse(this.nodeService.hasAspect(this.nodeRef, ContentModel.ASPECT_VERSIONABLE)); 227 228 this.actionService.executeAction(action, this.nodeRef, true); 229 assertFalse(this.nodeService.hasAspect(this.nodeRef, ContentModel.ASPECT_VERSIONABLE)); 230 231 this.actionService.executeAction(action, this.nodeRef, false); 232 assertTrue(this.nodeService.hasAspect(this.nodeRef, ContentModel.ASPECT_VERSIONABLE)); 233 234 this.nodeService.removeAspect(this.nodeRef, ContentModel.ASPECT_VERSIONABLE); 235 assertFalse(this.nodeService.hasAspect(this.nodeRef, ContentModel.ASPECT_VERSIONABLE)); 236 237 this.nodeService.setProperty(this.nodeRef, ContentModel.PROP_NAME, "myDocument.doc"); 238 this.actionService.executeAction(action, this.nodeRef); 239 assertTrue(this.nodeService.hasAspect(this.nodeRef, ContentModel.ASPECT_VERSIONABLE)); 240 241 this.nodeService.removeAspect(this.nodeRef, ContentModel.ASPECT_VERSIONABLE); 242 assertFalse(this.nodeService.hasAspect(this.nodeRef, ContentModel.ASPECT_VERSIONABLE)); 243 244 this.nodeService.removeAspect(this.nodeRef, ContentModel.ASPECT_VERSIONABLE); 245 assertFalse(this.nodeService.hasAspect(this.nodeRef, ContentModel.ASPECT_VERSIONABLE)); 246 247 Action action1 = this.actionService.createAction(AddFeaturesActionExecuter.NAME); 249 action1.setParameterValue(AddFeaturesActionExecuter.PARAM_ASPECT_NAME, ContentModel.ASPECT_LOCKABLE); 250 Action action2 = this.actionService.createAction(AddFeaturesActionExecuter.NAME); 251 action2.setParameterValue(AddFeaturesActionExecuter.PARAM_ASPECT_NAME, ContentModel.ASPECT_VERSIONABLE); 252 CompositeAction compAction = this.actionService.createCompositeAction(); 253 compAction.setTitle("title"); 254 compAction.setDescription("description"); 255 compAction.addAction(action1); 256 compAction.addAction(action2); 257 258 this.actionService.executeAction(compAction, this.nodeRef); 260 261 assertTrue(this.nodeService.hasAspect(this.nodeRef, ContentModel.ASPECT_LOCKABLE)); 262 assertTrue(this.nodeService.hasAspect(this.nodeRef, ContentModel.ASPECT_VERSIONABLE)); 263 } 264 265 public void testGetAndGetAllWithNoActions() 266 { 267 assertNull(this.actionService.getAction(this.nodeRef, AddFeaturesActionExecuter.NAME)); 268 List <Action> actions = this.actionService.getActions(this.nodeRef); 269 assertNotNull(actions); 270 assertEquals(0, actions.size()); 271 } 272 273 277 public void testSaveActionNoCondition() 278 { 279 Action action = this.actionService.createAction(AddFeaturesActionExecuter.NAME); 281 String actionId = action.getId(); 282 283 action.setParameterValue(AddFeaturesActionExecuter.PARAM_ASPECT_NAME, ContentModel.ASPECT_VERSIONABLE); 285 286 action.setTitle("title"); 288 action.setDescription("description"); 289 action.setExecuteAsynchronously(true); 290 291 this.actionService.saveAction(this.nodeRef, action); 293 294 Action savedAction = this.actionService.getAction(this.nodeRef, actionId); 296 297 assertEquals(action.getId(), savedAction.getId()); 299 assertEquals(action.getActionDefinitionName(), savedAction.getActionDefinitionName()); 300 301 assertEquals("title", savedAction.getTitle()); 303 assertEquals("description", savedAction.getDescription()); 304 assertTrue(savedAction.getExecuteAsychronously()); 305 306 assertNull(savedAction.getCompensatingAction()); 308 309 assertEquals(1, savedAction.getParameterValues().size()); 311 assertEquals(ContentModel.ASPECT_VERSIONABLE, savedAction.getParameterValue(AddFeaturesActionExecuter.PARAM_ASPECT_NAME)); 312 313 assertNotNull(savedAction.getActionConditions()); 315 assertEquals(0, savedAction.getActionConditions().size()); 316 317 Map <QName, Serializable > properties = new HashMap <QName, Serializable >(1); 319 properties.put(ContentModel.PROP_NAME, "testName"); 320 action.setParameterValue(AddFeaturesActionExecuter.PARAM_ASPECT_PROPERTIES, (Serializable )properties); 321 action.setParameterValue(AddFeaturesActionExecuter.PARAM_ASPECT_NAME, ContentModel.ASPECT_AUDITABLE); 322 323 Action compensatingAction = this.actionService.createAction(AddFeaturesActionExecuter.NAME); 325 compensatingAction.setParameterValue(AddFeaturesActionExecuter.PARAM_ASPECT_NAME, ContentModel.ASPECT_VERSIONABLE); 326 action.setCompensatingAction(compensatingAction); 327 328 this.actionService.saveAction(this.nodeRef, action); 329 Action savedAction2 = this.actionService.getAction(this.nodeRef, actionId); 330 331 assertEquals(2, savedAction2.getParameterValues().size()); 333 assertEquals(ContentModel.ASPECT_AUDITABLE, savedAction2.getParameterValue(AddFeaturesActionExecuter.PARAM_ASPECT_NAME)); 334 Map <QName, Serializable > temp = (Map <QName, Serializable >)savedAction2.getParameterValue(AddFeaturesActionExecuter.PARAM_ASPECT_PROPERTIES); 335 assertNotNull(temp); 336 assertEquals(1, temp.size()); 337 assertEquals("testName", temp.get(ContentModel.PROP_NAME)); 338 339 Action savedCompensatingAction = savedAction2.getCompensatingAction(); 341 assertNotNull(savedCompensatingAction); 342 assertEquals(compensatingAction, savedCompensatingAction); 343 assertEquals(AddFeaturesActionExecuter.NAME, savedCompensatingAction.getActionDefinitionName()); 344 assertEquals(ContentModel.ASPECT_VERSIONABLE, savedCompensatingAction.getParameterValue(AddFeaturesActionExecuter.PARAM_ASPECT_NAME)); 345 346 compensatingAction.setParameterValue(AddFeaturesActionExecuter.PARAM_ASPECT_NAME, ContentModel.ASPECT_CLASSIFIABLE); 348 this.actionService.saveAction(this.nodeRef, action); 349 Action savedAction3 = this.actionService.getAction(this.nodeRef, actionId); 350 Action savedCompensatingAction2 = savedAction3.getCompensatingAction(); 351 assertNotNull(savedCompensatingAction2); 352 assertEquals(compensatingAction, savedCompensatingAction2); 353 assertEquals(AddFeaturesActionExecuter.NAME, savedCompensatingAction2.getActionDefinitionName()); 354 assertEquals(ContentModel.ASPECT_CLASSIFIABLE, savedCompensatingAction2.getParameterValue(AddFeaturesActionExecuter.PARAM_ASPECT_NAME)); 355 action.setCompensatingAction(null); 356 this.actionService.saveAction(this.nodeRef, action); 357 Action savedAction4 = this.actionService.getAction(this.nodeRef, actionId); 358 assertNull(savedAction4.getCompensatingAction()); 359 360 } 362 363 public void testOwningNodeRef() 364 { 365 Action action = this.actionService.createAction(AddFeaturesActionExecuter.NAME); 367 String actionId = action.getId(); 368 369 action.setParameterValue(AddFeaturesActionExecuter.PARAM_ASPECT_NAME, ContentModel.ASPECT_VERSIONABLE); 371 372 action.setTitle("title"); 374 action.setDescription("description"); 375 action.setExecuteAsynchronously(true); 376 377 assertNull(action.getOwningNodeRef()); 379 380 this.actionService.saveAction(this.nodeRef, action); 382 383 assertEquals(this.nodeRef, action.getOwningNodeRef()); 385 386 Action savedAction = this.actionService.getAction(this.nodeRef, actionId); 388 389 assertEquals(this.nodeRef, savedAction.getOwningNodeRef());; 391 } 392 393 396 public void testSaveActionWithConditions() 397 { 398 Action action = this.actionService.createAction(AddFeaturesActionExecuter.NAME); 400 String actionId = action.getId(); 401 402 action.setParameterValue(AddFeaturesActionExecuter.PARAM_ASPECT_NAME, ContentModel.ASPECT_VERSIONABLE); 404 Map <QName, Serializable > properties = new HashMap <QName, Serializable >(1); 405 properties.put(ContentModel.PROP_NAME, "testName"); 406 action.setParameterValue(AddFeaturesActionExecuter.PARAM_ASPECT_PROPERTIES, (Serializable )properties); 407 408 ActionCondition actionCondition = this.actionService.createActionCondition(NoConditionEvaluator.NAME); 410 actionCondition.setInvertCondition(true); 411 ActionCondition actionCondition2 = this.actionService.createActionCondition(ComparePropertyValueEvaluator.NAME); 412 actionCondition2.setParameterValue(ComparePropertyValueEvaluator.PARAM_VALUE, "*.doc"); 413 action.addActionCondition(actionCondition); 414 action.addActionCondition(actionCondition2); 415 416 this.actionService.saveAction(this.nodeRef, action); 418 419 Action savedAction = this.actionService.getAction(this.nodeRef, actionId); 421 422 assertEquals(action.getId(), savedAction.getId()); 424 assertEquals(action.getActionDefinitionName(), savedAction.getActionDefinitionName()); 425 426 assertEquals(action.getParameterValues().size(), savedAction.getParameterValues().size()); 428 assertEquals(ContentModel.ASPECT_VERSIONABLE, savedAction.getParameterValue(AddFeaturesActionExecuter.PARAM_ASPECT_NAME)); 429 Map <QName, Serializable > temp = (Map <QName, Serializable >)savedAction.getParameterValue(AddFeaturesActionExecuter.PARAM_ASPECT_PROPERTIES); 430 assertNotNull(temp); 431 assertEquals(1, temp.size()); 432 assertEquals("testName", temp.get(ContentModel.PROP_NAME)); 433 434 assertNotNull(savedAction.getActionConditions()); 436 assertEquals(2, savedAction.getActionConditions().size()); 437 for (ActionCondition savedCondition : savedAction.getActionConditions()) 438 { 439 if (savedCondition.getActionConditionDefinitionName().equals(NoConditionEvaluator.NAME) == true) 440 { 441 assertEquals(0, savedCondition.getParameterValues().size()); 442 assertTrue(savedCondition.getInvertCondition()); 443 } 444 else if (savedCondition.getActionConditionDefinitionName().equals(ComparePropertyValueEvaluator.NAME) == true) 445 { 446 assertEquals(1, savedCondition.getParameterValues().size()); 447 assertEquals("*.doc", savedCondition.getParameterValue(ComparePropertyValueEvaluator.PARAM_VALUE)); 448 assertFalse(savedCondition.getInvertCondition()); 449 } 450 else 451 { 452 fail("There is a condition here that we are not expecting."); 453 } 454 } 455 456 ActionCondition actionCondition3 = this.actionService.createActionCondition(InCategoryEvaluator.NAME); 458 actionCondition3.setParameterValue(InCategoryEvaluator.PARAM_CATEGORY_ASPECT, ContentModel.ASPECT_OWNABLE); 459 action.addActionCondition(actionCondition3); 460 action.removeActionCondition(actionCondition); 461 actionCondition2.setParameterValue(ComparePropertyValueEvaluator.PARAM_VALUE, "*.exe"); 462 actionCondition2.setParameterValue(ComparePropertyValueEvaluator.PARAM_OPERATION, ComparePropertyValueOperation.EQUALS); 463 464 this.actionService.saveAction(this.nodeRef, action); 465 Action savedAction2 = this.actionService.getAction(this.nodeRef, actionId); 466 467 assertNotNull(savedAction2.getActionConditions()); 469 assertEquals(2, savedAction2.getActionConditions().size()); 470 for (ActionCondition savedCondition : savedAction2.getActionConditions()) 471 { 472 if (savedCondition.getActionConditionDefinitionName().equals(InCategoryEvaluator.NAME) == true) 473 { 474 assertEquals(1, savedCondition.getParameterValues().size()); 475 assertEquals(ContentModel.ASPECT_OWNABLE, savedCondition.getParameterValue(InCategoryEvaluator.PARAM_CATEGORY_ASPECT)); 476 } 477 else if (savedCondition.getActionConditionDefinitionName().equals(ComparePropertyValueEvaluator.NAME) == true) 478 { 479 assertEquals(2, savedCondition.getParameterValues().size()); 480 assertEquals("*.exe", savedCondition.getParameterValue(ComparePropertyValueEvaluator.PARAM_VALUE)); 481 assertEquals(ComparePropertyValueOperation.EQUALS, savedCondition.getParameterValue(ComparePropertyValueEvaluator.PARAM_OPERATION)); 482 } 483 else 484 { 485 fail("There is a condition here that we are not expecting."); 486 } 487 } 488 489 } 491 492 495 public void testSaveCompositeAction() 496 { 497 Action action1 = this.actionService.createAction(AddFeaturesActionExecuter.NAME); 498 Action action2 = this.actionService.createAction(CheckInActionExecuter.NAME); 499 500 CompositeAction compositeAction = this.actionService.createCompositeAction(); 501 String actionId = compositeAction.getId(); 502 compositeAction.addAction(action1); 503 compositeAction.addAction(action2); 504 505 this.actionService.saveAction(this.nodeRef, compositeAction); 506 assertEquals(1, this.actionService.getActions(this.nodeRef).size()); 507 CompositeAction savedCompositeAction = (CompositeAction)this.actionService.getAction(this.nodeRef, actionId); 508 509 assertEquals(2, savedCompositeAction.getActions().size()); 511 for (Action action : savedCompositeAction.getActions()) 512 { 513 if (action.getActionDefinitionName().equals(AddFeaturesActionExecuter.NAME) == true) 514 { 515 assertEquals(action, action1); 516 } 517 else if (action.getActionDefinitionName().equals(CheckInActionExecuter.NAME) == true) 518 { 519 assertEquals(action, action2); 520 } 521 else 522 { 523 fail("We have an action here we are not expecting."); 524 } 525 } 526 527 compositeAction.removeAction(action1); 529 Action action3 = this.actionService.createAction(CheckOutActionExecuter.NAME); 530 compositeAction.addAction(action3); 531 action2.setParameterValue(CheckInActionExecuter.PARAM_DESCRIPTION, "description"); 532 533 this.actionService.saveAction(this.nodeRef, compositeAction); 534 assertEquals(1, this.actionService.getActions(this.nodeRef).size()); 535 CompositeAction savedCompositeAction2 = (CompositeAction)this.actionService.getAction(this.nodeRef, actionId); 536 537 assertEquals(2, savedCompositeAction2.getActions().size()); 538 for (Action action : savedCompositeAction2.getActions()) 539 { 540 if (action.getActionDefinitionName().equals(CheckOutActionExecuter.NAME) == true) 541 { 542 assertEquals(action, action3); 543 } 544 else if (action.getActionDefinitionName().equals(CheckInActionExecuter.NAME) == true) 545 { 546 assertEquals(action, action2); 547 assertEquals("description", action2.getParameterValue(CheckInActionExecuter.PARAM_DESCRIPTION)); 548 } 549 else 550 { 551 fail("We have an action here we are not expecting."); 552 } 553 } 554 } 555 556 559 public void testRemove() 560 { 561 assertEquals(0, this.actionService.getActions(this.nodeRef).size()); 562 563 Action action1 = this.actionService.createAction(AddFeaturesActionExecuter.NAME); 564 this.actionService.saveAction(this.nodeRef, action1); 565 Action action2 = this.actionService.createAction(CheckInActionExecuter.NAME); 566 this.actionService.saveAction(this.nodeRef, action2); 567 assertEquals(2, this.actionService.getActions(this.nodeRef).size()); 568 569 this.actionService.removeAction(this.nodeRef, action1); 570 assertEquals(1, this.actionService.getActions(this.nodeRef).size()); 571 572 this.actionService.removeAllActions(this.nodeRef); 573 assertEquals(0, this.actionService.getActions(this.nodeRef).size()); 574 } 575 576 public void testConditionOrder() 577 { 578 Action action = this.actionService.createAction(AddFeaturesActionExecuter.NAME); 579 String actionId = action.getId(); 580 581 ActionCondition condition1 = this.actionService.createActionCondition(NoConditionEvaluator.NAME); 582 ActionCondition condition2 = this.actionService.createActionCondition(NoConditionEvaluator.NAME); 583 584 action.addActionCondition(condition1); 585 action.addActionCondition(condition2); 586 587 this.actionService.saveAction(this.nodeRef, action); 588 Action savedAction = this.actionService.getAction(this.nodeRef, actionId); 589 590 assertNotNull(savedAction); 592 assertEquals(condition1, savedAction.getActionCondition(0)); 593 assertEquals(condition2, savedAction.getActionCondition(1)); 594 595 ActionCondition condition3 = this.actionService.createActionCondition(NoConditionEvaluator.NAME); 596 ActionCondition condition4 = this.actionService.createActionCondition(NoConditionEvaluator.NAME); 597 598 savedAction.removeActionCondition(condition1); 600 savedAction.addActionCondition(condition3); 601 savedAction.addActionCondition(condition4); 602 603 this.actionService.saveAction(this.nodeRef, savedAction); 604 Action savedAction2 = this.actionService.getAction(this.nodeRef, actionId); 605 606 assertNotNull(savedAction2); 608 assertEquals(condition2, savedAction2.getActionCondition(0)); 609 assertEquals(condition3, savedAction2.getActionCondition(1)); 610 assertEquals(condition4, savedAction2.getActionCondition(2)); 611 } 612 613 public void testActionOrder() 614 { 615 CompositeAction action = this.actionService.createCompositeAction(); 616 String actionId = action.getId(); 617 618 Action action1 = this.actionService.createAction(AddFeaturesActionExecuter.NAME); 619 Action action2 = this.actionService.createAction(AddFeaturesActionExecuter.NAME); 620 621 action.addAction(action1); 622 action.addAction(action2); 623 624 this.actionService.saveAction(this.nodeRef, action); 625 CompositeAction savedAction = (CompositeAction)this.actionService.getAction(this.nodeRef, actionId); 626 627 assertNotNull(savedAction); 629 assertEquals(action1, savedAction.getAction(0)); 630 assertEquals(action2, savedAction.getAction(1)); 631 632 Action action3 = this.actionService.createAction(AddFeaturesActionExecuter.NAME); 633 Action action4 = this.actionService.createAction(AddFeaturesActionExecuter.NAME); 634 635 savedAction.removeAction(action1); 637 savedAction.addAction(action3); 638 savedAction.addAction(action4); 639 640 this.actionService.saveAction(this.nodeRef, savedAction); 641 CompositeAction savedAction2 = (CompositeAction)this.actionService.getAction(this.nodeRef, actionId); 642 643 assertNotNull(savedAction2); 645 assertEquals(action2, savedAction2.getAction(0)); 646 assertEquals(action3, savedAction2.getAction(1)); 647 assertEquals(action4, savedAction2.getAction(2)); 648 } 649 650 653 654 657 public void testAsyncExecuteAction() 658 { 659 assertFalse(this.nodeService.hasAspect(this.nodeRef, ContentModel.ASPECT_VERSIONABLE)); 660 661 Action action = this.actionService.createAction(AddFeaturesActionExecuter.NAME); 662 action.setParameterValue(AddFeaturesActionExecuter.PARAM_ASPECT_NAME, ContentModel.ASPECT_VERSIONABLE); 663 action.setExecuteAsynchronously(true); 664 665 this.actionService.executeAction(action, this.nodeRef); 666 667 setComplete(); 668 endTransaction(); 669 670 final NodeService finalNodeService = this.nodeService; 671 final NodeRef finalNodeRef = this.nodeRef; 672 673 postAsyncActionTest( 674 this.transactionService, 675 1000, 676 10, 677 new AsyncTest() 678 { 679 public boolean executeTest() 680 { 681 return ( 682 finalNodeService.hasAspect(finalNodeRef, ContentModel.ASPECT_VERSIONABLE)); 683 }; 684 }); 685 } 686 687 688 689 692 public void testAsyncCompositeActionExecute() 693 { 694 Action action1 = this.actionService.createAction(AddFeaturesActionExecuter.NAME); 696 action1.setParameterValue(AddFeaturesActionExecuter.PARAM_ASPECT_NAME, ContentModel.ASPECT_LOCKABLE); 697 Action action2 = this.actionService.createAction(AddFeaturesActionExecuter.NAME); 698 action2.setParameterValue(AddFeaturesActionExecuter.PARAM_ASPECT_NAME, ContentModel.ASPECT_VERSIONABLE); 699 CompositeAction compAction = this.actionService.createCompositeAction(); 700 compAction.setTitle("title"); 701 compAction.setDescription("description"); 702 compAction.addAction(action1); 703 compAction.addAction(action2); 704 compAction.setExecuteAsynchronously(true); 705 706 this.actionService.executeAction(compAction, this.nodeRef); 708 709 setComplete(); 710 endTransaction(); 711 712 final NodeService finalNodeService = this.nodeService; 713 final NodeRef finalNodeRef = this.nodeRef; 714 715 postAsyncActionTest( 716 this.transactionService, 717 1000, 718 10, 719 new AsyncTest() 720 { 721 public boolean executeTest() 722 { 723 return ( 724 finalNodeService.hasAspect(finalNodeRef, ContentModel.ASPECT_VERSIONABLE) && 725 finalNodeService.hasAspect(finalNodeRef, ContentModel.ASPECT_LOCKABLE)); 726 }; 727 }); 728 } 729 730 public void xtestAsyncLoadTest() 731 { 732 734 Action action = this.actionService.createAction(AddFeaturesActionExecuter.NAME); 735 action.setParameterValue(AddFeaturesActionExecuter.PARAM_ASPECT_NAME, ContentModel.ASPECT_VERSIONABLE); 736 action.setExecuteAsynchronously(true); 737 738 for (int i = 0; i < 1000; i++) 739 { 740 this.actionService.executeAction(action, this.nodeRef); 741 } 742 743 setComplete(); 744 endTransaction(); 745 746 } 748 749 756 public static void postAsyncActionTest( 757 TransactionService transactionService, 758 final long sleepTime, 759 final int maxTries, 760 final AsyncTest test) 761 { 762 try 763 { 764 int tries = 0; 765 boolean done = false; 766 while (done == false && tries < maxTries) 767 { 768 try 769 { 770 tries++; 772 773 Thread.sleep(sleepTime); 775 776 done = (TransactionUtil.executeInUserTransaction( 777 transactionService, 778 new TransactionUtil.TransactionWork<Boolean >() 779 { 780 public Boolean doWork() 781 { 782 boolean done = test.executeTest(); 784 return done; 785 } 786 })).booleanValue(); 787 } 788 catch (InterruptedException e) 789 { 790 e.printStackTrace(); 792 } 793 } 794 795 if (done == false) 796 { 797 throw new RuntimeException ("Asynchronous action was not executed."); 798 } 799 } 800 catch (Throwable exception) 801 { 802 exception.printStackTrace(); 803 fail("An exception was encountered whilst checking the async action was executed: " + exception.getMessage()); 804 } 805 } 806 807 810 public interface AsyncTest 811 { 812 boolean executeTest(); 813 } 814 815 818 819 822 public void testSyncFailureBehaviour() 823 { 824 Action action = this.actionService.createAction(MoveActionExecuter.NAME); 826 action.setParameterValue(MoveActionExecuter.PARAM_ASSOC_TYPE_QNAME, ContentModel.ASSOC_CHILDREN); 827 action.setParameterValue(MoveActionExecuter.PARAM_ASSOC_QNAME, ContentModel.ASSOC_CHILDREN); 828 NodeRef badNodeRef = new NodeRef(this.storeRef, "123123"); 830 action.setParameterValue(MoveActionExecuter.PARAM_DESTINATION_FOLDER, badNodeRef); 831 832 try 833 { 834 this.actionService.executeAction(action, this.nodeRef); 835 836 fail("An exception should have been raised."); 838 } 839 catch (RuntimeException exception) 840 { 841 } 843 844 Action action1 = this.actionService.createAction(AddFeaturesActionExecuter.NAME); 847 action1.setParameterValue(AddFeaturesActionExecuter.PARAM_ASPECT_NAME, ContentModel.ASPECT_LOCKABLE); 848 Action action2 = this.actionService.createAction(AddFeaturesActionExecuter.NAME); 849 action2.setParameterValue(AddFeaturesActionExecuter.PARAM_ASPECT_NAME, QName.createQName("{test}badDogAspect")); 850 CompositeAction compAction = this.actionService.createCompositeAction(); 851 compAction.setTitle("title"); 852 compAction.setDescription("description"); 853 compAction.addAction(action1); 854 compAction.addAction(action2); 855 856 try 857 { 858 this.actionService.executeAction(compAction, this.nodeRef); 860 861 fail("An exception should have been raised here !!"); 862 } 863 catch (RuntimeException runtimeException) 864 { 865 } 867 } 868 869 872 public void testCompensatingAction() 873 { 874 final Action action = this.actionService.createAction(MoveActionExecuter.NAME); 876 action.setParameterValue(MoveActionExecuter.PARAM_ASSOC_TYPE_QNAME, ContentModel.ASSOC_CHILDREN); 877 action.setParameterValue(MoveActionExecuter.PARAM_ASSOC_QNAME, ContentModel.ASSOC_CHILDREN); 878 NodeRef badNodeRef = new NodeRef(this.storeRef, "123123"); 880 action.setParameterValue(MoveActionExecuter.PARAM_DESTINATION_FOLDER, badNodeRef); 881 action.setTitle("title"); 882 883 Action compensatingAction = actionService.createAction(AddFeaturesActionExecuter.NAME); 885 compensatingAction.setParameterValue(AddFeaturesActionExecuter.PARAM_ASPECT_NAME, ContentModel.ASPECT_CLASSIFIABLE); 886 compensatingAction.setTitle("title"); 887 action.setCompensatingAction(compensatingAction); 888 889 action.setExecuteAsynchronously(true); 891 892 this.actionService.executeAction(action, this.nodeRef); 893 894 setComplete(); 895 endTransaction(); 896 897 postAsyncActionTest( 898 this.transactionService, 899 1000, 900 10, 901 new AsyncTest() 902 { 903 public boolean executeTest() 904 { 905 return ( 906 ActionServiceImplTest.this.nodeService.hasAspect(nodeRef, ContentModel.ASPECT_CLASSIFIABLE)); 907 }; 908 }); 909 910 compensatingAction.setParameterValue(AddFeaturesActionExecuter.PARAM_ASPECT_NAME, QName.createQName("{test}badAspect")); 912 913 TransactionUtil.executeInUserTransaction( 914 this.transactionService, 915 new TransactionUtil.TransactionWork<Object >() 916 { 917 public Object doWork() 918 { 919 try 920 { 921 ActionServiceImplTest.this.actionService.executeAction(action, ActionServiceImplTest.this.nodeRef); 922 } 923 catch (RuntimeException exception) 924 { 925 exception.printStackTrace(); 927 fail("An exception should not have been raised here."); 928 } 929 return null; 930 } 931 932 }); 933 934 } 935 } | Popular Tags |