1 17 package org.alfresco.repo.rule; 18 19 import java.io.File ; 20 import java.io.Serializable ; 21 import java.util.HashMap ; 22 import java.util.List ; 23 import java.util.Map ; 24 25 import org.alfresco.model.ContentModel; 26 import org.alfresco.repo.action.evaluator.ComparePropertyValueEvaluator; 27 import org.alfresco.repo.action.executer.ImageTransformActionExecuter; 28 import org.alfresco.repo.content.MimetypeMap; 29 import org.alfresco.repo.content.transform.AbstractContentTransformerTest; 30 import org.alfresco.service.cmr.action.Action; 31 import org.alfresco.service.cmr.action.ActionCondition; 32 import org.alfresco.service.cmr.repository.ContentWriter; 33 import org.alfresco.service.cmr.repository.CyclicChildRelationshipException; 34 import org.alfresco.service.cmr.repository.NodeRef; 35 import org.alfresco.service.cmr.rule.Rule; 36 import org.alfresco.service.cmr.rule.RuleType; 37 import org.alfresco.service.namespace.QName; 38 39 40 45 public class RuleServiceImplTest extends BaseRuleTest 46 { 47 48 51 public void testGetRuleType() 52 { 53 List <RuleType> ruleTypes = this.ruleService.getRuleTypes(); 54 assertNotNull(ruleTypes); 55 56 for (RuleType type : ruleTypes) 58 { 59 System.out.println(type.getDisplayLabel()); 60 } 61 } 62 63 66 public void testCreateRule() 67 { 68 Rule newRule = this.ruleService.createRule("ruleType1"); 69 assertNotNull(newRule); 70 assertNotNull(newRule.getId()); 71 assertEquals("ruleType1", newRule.getRuleTypeName()); 72 } 73 74 78 public void testAddRule() 79 { 80 Rule newRule = createTestRule(); 81 String ruleId = newRule.getId(); 82 this.ruleService.saveRule(this.nodeRef, newRule); 83 84 Rule savedRule = this.ruleService.getRule(this.nodeRef, ruleId); 85 assertNotNull(savedRule); 86 assertFalse(savedRule.isAppliedToChildren()); 87 88 savedRule.applyToChildren(true); 89 this.ruleService.saveRule(this.nodeRef, savedRule); 90 91 Rule savedRule2 = this.ruleService.getRule(this.nodeRef, ruleId); 92 assertNotNull(savedRule2); 93 assertTrue(savedRule2.isAppliedToChildren()); 94 } 95 96 public void testRemoveAllRules() 97 { 98 this.ruleService.removeAllRules(this.nodeRef); 99 List <Rule> rules1 = this.ruleService.getRules(this.nodeRef); 100 assertNotNull(rules1); 101 assertEquals(0, rules1.size()); 102 103 Rule newRule = this.ruleService.createRule(ruleType.getName()); 104 this.ruleService.saveRule(this.nodeRef, newRule); 105 Rule newRule2 = this.ruleService.createRule(ruleType.getName()); 106 this.ruleService.saveRule(this.nodeRef, newRule2); 107 108 List <Rule> rules2 = this.ruleService.getRules(this.nodeRef); 109 assertNotNull(rules2); 110 assertEquals(2, rules2.size()); 111 112 this.ruleService.removeAllRules(this.nodeRef); 113 114 List <Rule> rules3 = this.ruleService.getRules(this.nodeRef); 115 assertNotNull(rules3); 116 assertEquals(0, rules3.size()); 117 118 } 119 120 123 public void testGetRules() 124 { 125 List <Rule> noRules = this.ruleService.getRules(this.nodeRef); 127 assertNotNull(noRules); 128 assertEquals(0, noRules.size()); 129 130 List <Rule> noRulesAfterCache = this.ruleService.getRules(this.nodeRef); 133 assertNotNull(noRulesAfterCache); 134 assertEquals(0, noRulesAfterCache.size()); 135 136 testAddRule(); 138 139 List <Rule> rules = this.ruleService.getRules(this.nodeRef); 141 assertNotNull(rules); 142 assertEquals(1, rules.size()); 143 144 Rule rule = rules.get(0); 146 assertEquals("title", rule.getTitle()); 147 assertEquals("description", rule.getDescription()); 148 assertNotNull(rule.getCreatedDate()); 149 assertNotNull(rule.getModifiedDate()); 150 151 List <ActionCondition> conditions = rule.getActionConditions(); 153 assertNotNull(conditions); 154 assertEquals(1, conditions.size()); 155 List <Action> actions = rule.getActions(); 156 assertNotNull(actions); 157 assertEquals(1, actions.size()); 158 } 159 160 163 public void testRulesDisabled() 164 { 165 testAddRule(); 166 assertTrue(this.ruleService.rulesEnabled(this.nodeRef)); 167 this.ruleService.disableRules(this.nodeRef); 168 assertFalse(this.ruleService.rulesEnabled(this.nodeRef)); 169 this.ruleService.enableRules(this.nodeRef); 170 assertTrue(this.ruleService.rulesEnabled(this.nodeRef)); 171 } 172 173 179 private NodeRef createNewNode(NodeRef parent, boolean isActionable) 180 { 181 NodeRef newNodeRef = this.nodeService.createNode(parent, 182 ContentModel.ASSOC_CHILDREN, 183 QName.createQName("{test}testnode"), 184 ContentModel.TYPE_CONTAINER).getChildRef(); 185 return newNodeRef; 186 } 187 188 192 public void testRuleInheritance() 193 { 194 196 NodeRef rootWithRules = createNewNode(this.rootNodeRef, true); 197 Rule rule1 = createTestRule(); 198 this.ruleService.saveRule(rootWithRules, rule1); 199 Rule rule2 = createTestRule(true); 200 this.ruleService.saveRule(rootWithRules, rule2); 201 202 NodeRef nonActionableChild = createNewNode(rootWithRules, false); 203 204 NodeRef childWithRules = createNewNode(nonActionableChild, true); 205 Rule rule3 = createTestRule(); 206 this.ruleService.saveRule(childWithRules, rule3); 207 Rule rule4 = createTestRule(true); 208 this.ruleService.saveRule(childWithRules, rule4); 209 210 NodeRef rootWithRules2 = createNewNode(this.rootNodeRef, true); 211 this.nodeService.addChild( 212 rootWithRules2, 213 childWithRules, 214 ContentModel.ASSOC_CHILDREN, 215 QName.createQName("{test}testnode")); 216 Rule rule5 = createTestRule(); 217 this.ruleService.saveRule(rootWithRules2, rule5); 218 Rule rule6 = createTestRule(true); 219 this.ruleService.saveRule(rootWithRules2, rule6); 220 221 223 List <? extends Rule> allRules = this.ruleService.getRules(childWithRules, true); 224 assertNotNull(allRules); 225 assertEquals(4, allRules.size()); 226 assertTrue(allRules.contains(rule2)); 227 assertTrue(allRules.contains(rule3)); 228 assertTrue(allRules.contains(rule4)); 229 assertTrue(allRules.contains(rule6)); 230 231 int count = 0; 233 for (Rule rule : allRules) 234 { 235 if (rule.getOwningNodeRef() == childWithRules) 236 { 237 count++; 238 } 239 } 240 assertEquals(2, count); 241 242 List <? extends Rule> myRules = this.ruleService.getRules(childWithRules, false); 243 assertNotNull(myRules); 244 assertEquals(2, myRules.size()); 245 assertTrue(myRules.contains(rule3)); 246 assertTrue(myRules.contains(rule4)); 247 248 List <? extends Rule> allRules2 = this.ruleService.getRules(nonActionableChild, true); 249 assertNotNull(allRules2); 250 assertEquals(1, allRules2.size()); 251 assertTrue(allRules2.contains(rule2)); 252 253 List <? extends Rule> myRules2 = this.ruleService.getRules(nonActionableChild, false); 254 assertNotNull(myRules2); 255 assertEquals(0, myRules2.size()); 256 257 List <? extends Rule> allRules3 = this.ruleService.getRules(rootWithRules, true); 258 assertNotNull(allRules3); 259 assertEquals(2, allRules3.size()); 260 assertTrue(allRules3.contains(rule1)); 261 assertTrue(allRules3.contains(rule2)); 262 263 List <? extends Rule> myRules3 = this.ruleService.getRules(rootWithRules, false); 264 assertNotNull(myRules3); 265 assertEquals(2, myRules3.size()); 266 assertTrue(myRules3.contains(rule1)); 267 assertTrue(myRules3.contains(rule2)); 268 269 List <? extends Rule> allRules4 = this.ruleService.getRules(rootWithRules2, true); 270 assertNotNull(allRules4); 271 assertEquals(2, allRules4.size()); 272 assertTrue(allRules4.contains(rule5)); 273 assertTrue(allRules4.contains(rule6)); 274 275 List <? extends Rule> myRules4 = this.ruleService.getRules(rootWithRules2, false); 276 assertNotNull(myRules4); 277 assertEquals(2, myRules4.size()); 278 assertTrue(myRules4.contains(rule5)); 279 assertTrue(myRules4.contains(rule6)); 280 281 283 Rule rule7 = createTestRule(true); 284 this.ruleService.saveRule(rootWithRules, rule7); 285 286 List <? extends Rule> allRules5 = this.ruleService.getRules(childWithRules, true); 287 assertNotNull(allRules5); 288 assertEquals(5, allRules5.size()); 289 assertTrue(allRules5.contains(rule2)); 290 assertTrue(allRules5.contains(rule3)); 291 assertTrue(allRules5.contains(rule4)); 292 assertTrue(allRules5.contains(rule6)); 293 assertTrue(allRules5.contains(rule7)); 294 295 List <? extends Rule> allRules6 = this.ruleService.getRules(nonActionableChild, true); 296 assertNotNull(allRules6); 297 assertEquals(2, allRules6.size()); 298 assertTrue(allRules6.contains(rule2)); 299 assertTrue(allRules6.contains(rule7)); 300 301 List <? extends Rule> allRules7 = this.ruleService.getRules(rootWithRules, true); 302 assertNotNull(allRules7); 303 assertEquals(3, allRules7.size()); 304 assertTrue(allRules7.contains(rule1)); 305 assertTrue(allRules7.contains(rule2)); 306 assertTrue(allRules7.contains(rule7)); 307 308 List <? extends Rule> allRules8 = this.ruleService.getRules(rootWithRules2, true); 309 assertNotNull(allRules8); 310 assertEquals(2, allRules8.size()); 311 assertTrue(allRules8.contains(rule5)); 312 assertTrue(allRules8.contains(rule6)); 313 314 316 this.ruleService.removeRule(rootWithRules, rule7); 317 318 List <? extends Rule> allRules9 = this.ruleService.getRules(childWithRules, true); 319 assertNotNull(allRules9); 320 assertEquals(4, allRules9.size()); 321 assertTrue(allRules9.contains(rule2)); 322 assertTrue(allRules9.contains(rule3)); 323 assertTrue(allRules9.contains(rule4)); 324 assertTrue(allRules9.contains(rule6)); 325 326 List <? extends Rule> allRules10 = this.ruleService.getRules(nonActionableChild, true); 327 assertNotNull(allRules10); 328 assertEquals(1, allRules10.size()); 329 assertTrue(allRules10.contains(rule2)); 330 331 List <? extends Rule> allRules11 = this.ruleService.getRules(rootWithRules, true); 332 assertNotNull(allRules11); 333 assertEquals(2, allRules11.size()); 334 assertTrue(allRules11.contains(rule1)); 335 assertTrue(allRules11.contains(rule2)); 336 337 List <? extends Rule> allRules12 = this.ruleService.getRules(rootWithRules2, true); 338 assertNotNull(allRules12); 339 assertEquals(2, allRules12.size()); 340 assertTrue(allRules12.contains(rule5)); 341 assertTrue(allRules12.contains(rule6)); 342 343 345 this.nodeService.removeChild(rootWithRules2, childWithRules); 346 347 List <? extends Rule> allRules13 = this.ruleService.getRules(childWithRules, true); 348 assertNotNull(allRules13); 349 assertEquals(3, allRules13.size()); 350 assertTrue(allRules13.contains(rule2)); 351 assertTrue(allRules13.contains(rule3)); 352 assertTrue(allRules13.contains(rule4)); 353 354 List <? extends Rule> allRules14 = this.ruleService.getRules(nonActionableChild, true); 355 assertNotNull(allRules14); 356 assertEquals(1, allRules14.size()); 357 assertTrue(allRules14.contains(rule2)); 358 359 List <? extends Rule> allRules15 = this.ruleService.getRules(rootWithRules, true); 360 assertNotNull(allRules15); 361 assertEquals(2, allRules15.size()); 362 assertTrue(allRules15.contains(rule1)); 363 assertTrue(allRules15.contains(rule2)); 364 365 List <? extends Rule> allRules16 = this.ruleService.getRules(rootWithRules2, true); 366 assertNotNull(allRules16); 367 assertEquals(2, allRules16.size()); 368 assertTrue(allRules16.contains(rule5)); 369 assertTrue(allRules16.contains(rule6)); 370 371 this.ruleService.disableRules(rootWithRules2); 372 try 373 { 374 this.nodeService.addChild( 376 rootWithRules2, 377 childWithRules, 378 ContentModel.ASSOC_CHILDREN, 379 QName.createQName("{test}testnode")); 380 } 381 finally 382 { 383 this.ruleService.enableRules(rootWithRules2); 384 } 385 386 List <? extends Rule> allRules17 = this.ruleService.getRules(childWithRules, true); 387 assertNotNull(allRules17); 388 assertEquals(4, allRules17.size()); 389 assertTrue(allRules17.contains(rule2)); 390 assertTrue(allRules17.contains(rule3)); 391 assertTrue(allRules17.contains(rule4)); 392 assertTrue(allRules17.contains(rule6)); 393 394 List <? extends Rule> allRules18 = this.ruleService.getRules(nonActionableChild, true); 395 assertNotNull(allRules18); 396 assertEquals(1, allRules18.size()); 397 assertTrue(allRules18.contains(rule2)); 398 399 List <? extends Rule> allRules19 = this.ruleService.getRules(rootWithRules, true); 400 assertNotNull(allRules19); 401 assertEquals(2, allRules19.size()); 402 assertTrue(allRules19.contains(rule1)); 403 assertTrue(allRules19.contains(rule2)); 404 405 List <? extends Rule> allRules20 = this.ruleService.getRules(rootWithRules2, true); 406 assertNotNull(allRules20); 407 assertEquals(2, allRules20.size()); 408 assertTrue(allRules20.contains(rule5)); 409 assertTrue(allRules20.contains(rule6)); 410 411 413 this.nodeService.deleteNode(rootWithRules2); 414 415 List <? extends Rule> allRules21 = this.ruleService.getRules(childWithRules, true); 416 assertNotNull(allRules21); 417 assertEquals(3, allRules21.size()); 418 assertTrue(allRules21.contains(rule2)); 419 assertTrue(allRules21.contains(rule3)); 420 assertTrue(allRules21.contains(rule4)); 421 422 List <? extends Rule> allRules22 = this.ruleService.getRules(nonActionableChild, true); 423 assertNotNull(allRules22); 424 assertEquals(1, allRules22.size()); 425 assertTrue(allRules22.contains(rule2)); 426 427 List <? extends Rule> allRules23 = this.ruleService.getRules(rootWithRules, true); 428 assertNotNull(allRules23); 429 assertEquals(2, allRules23.size()); 430 assertTrue(allRules23.contains(rule1)); 431 assertTrue(allRules23.contains(rule2)); 432 } 433 434 439 public void testCyclicGraphWithInheritedRules() 440 throws Exception 441 { 442 NodeRef nodeRef1 = createNewNode(this.rootNodeRef, true); 443 NodeRef nodeRef2 = createNewNode(nodeRef1, true); 444 NodeRef nodeRef3 = createNewNode(nodeRef2, true); 445 try 446 { 447 this.nodeService.addChild(nodeRef3, nodeRef1, ContentModel.ASSOC_CHILDREN, QName.createQName("{test}loop")); 448 fail("Expected detection of cyclic relationship"); 449 } 450 catch (CyclicChildRelationshipException e) 451 { 452 } 455 456 Rule rule1 = createTestRule(true); 457 this.ruleService.saveRule(nodeRef1, rule1); 458 Rule rule2 = createTestRule(true); 459 this.ruleService.saveRule(nodeRef2, rule2); 460 Rule rule3 = createTestRule(true); 461 this.ruleService.saveRule(nodeRef3, rule3); 462 463 List <? extends Rule> allRules1 = this.ruleService.getRules(nodeRef1, true); 464 assertNotNull(allRules1); 465 assertEquals(3, allRules1.size()); 466 assertTrue(allRules1.contains(rule1)); 467 assertTrue(allRules1.contains(rule2)); 468 assertTrue(allRules1.contains(rule3)); 469 470 List <? extends Rule> allRules2 = this.ruleService.getRules(nodeRef2, true); 471 assertNotNull(allRules2); 472 assertEquals(3, allRules2.size()); 473 assertTrue(allRules2.contains(rule1)); 474 assertTrue(allRules2.contains(rule2)); 475 assertTrue(allRules2.contains(rule3)); 476 477 List <? extends Rule> allRules3 = this.ruleService.getRules(nodeRef3, true); 478 assertNotNull(allRules3); 479 assertEquals(3, allRules3.size()); 480 assertTrue(allRules3.contains(rule1)); 481 assertTrue(allRules3.contains(rule2)); 482 assertTrue(allRules3.contains(rule3)); 483 } 484 485 488 public void testRuleDuplication() 489 { 490 NodeRef nodeRef1 = createNewNode(this.rootNodeRef, true); 491 NodeRef nodeRef2 = createNewNode(nodeRef1, true); 492 NodeRef nodeRef3 = createNewNode(nodeRef2, true); 493 NodeRef nodeRef4 = createNewNode(nodeRef1, true); 494 this.nodeService.addChild(nodeRef4, nodeRef3, ContentModel.ASSOC_CHILDREN, QName.createQName("{test}test")); 495 496 Rule rule1 = createTestRule(true); 497 this.ruleService.saveRule(nodeRef1, rule1); 498 Rule rule2 = createTestRule(true); 499 this.ruleService.saveRule(nodeRef2, rule2); 500 Rule rule3 = createTestRule(true); 501 this.ruleService.saveRule(nodeRef3, rule3); 502 Rule rule4 = createTestRule(true); 503 this.ruleService.saveRule(nodeRef4, rule4); 504 505 List <? extends Rule> allRules1 = this.ruleService.getRules(nodeRef1, true); 506 assertNotNull(allRules1); 507 assertEquals(1, allRules1.size()); 508 assertTrue(allRules1.contains(rule1)); 509 510 List <? extends Rule> allRules2 = this.ruleService.getRules(nodeRef2, true); 511 assertNotNull(allRules2); 512 assertEquals(2, allRules2.size()); 513 assertTrue(allRules2.contains(rule1)); 514 assertTrue(allRules2.contains(rule2)); 515 516 List <? extends Rule> allRules3 = this.ruleService.getRules(nodeRef3, true); 517 assertNotNull(allRules3); 518 assertEquals(4, allRules3.size()); 519 assertTrue(allRules3.contains(rule1)); 520 assertTrue(allRules3.contains(rule2)); 521 assertTrue(allRules3.contains(rule3)); 522 assertTrue(allRules3.contains(rule4)); 523 524 List <? extends Rule> allRules4 = this.ruleService.getRules(nodeRef4, true); 525 assertNotNull(allRules4); 526 assertEquals(2, allRules4.size()); 527 assertTrue(allRules4.contains(rule1)); 528 assertTrue(allRules4.contains(rule4)); 529 } 530 531 public void testCyclicRules() 532 { 533 } 534 535 public void testCyclicAsyncRules() throws Exception 536 { 537 NodeRef nodeRef = createNewNode(this.rootNodeRef, true); 538 539 541 Map <String , Serializable > conditionProps = new HashMap <String , Serializable >(); 542 conditionProps.put(ComparePropertyValueEvaluator.PARAM_VALUE, "*.jpg"); 543 544 Map <String , Serializable > actionProps = new HashMap <String , Serializable >(); 545 actionProps.put(ImageTransformActionExecuter.PARAM_MIME_TYPE, MimetypeMap.MIMETYPE_IMAGE_GIF); 546 actionProps.put(ImageTransformActionExecuter.PARAM_DESTINATION_FOLDER, nodeRef); 547 actionProps.put(ImageTransformActionExecuter.PARAM_ASSOC_TYPE_QNAME, ContentModel.ASSOC_CHILDREN); 548 actionProps.put(ImageTransformActionExecuter.PARAM_ASSOC_QNAME, ContentModel.ASSOC_CHILDREN); 549 550 Rule rule = this.ruleService.createRule(this.ruleType.getName()); 551 rule.setTitle("Convert from *.jpg to *.gif"); 552 rule.setExecuteAsynchronously(true); 553 554 ActionCondition actionCondition = this.actionService.createActionCondition(ComparePropertyValueEvaluator.NAME); 555 actionCondition.setParameterValues(conditionProps); 556 rule.addActionCondition(actionCondition); 557 558 Action action = this.actionService.createAction(ImageTransformActionExecuter.NAME); 559 action.setParameterValues(actionProps); 560 rule.addAction(action); 561 562 564 Map <String , Serializable > conditionProps2 = new HashMap <String , Serializable >(); 565 conditionProps2.put(ComparePropertyValueEvaluator.PARAM_VALUE, "*.gif"); 566 567 Map <String , Serializable > actionProps2 = new HashMap <String , Serializable >(); 568 actionProps2.put(ImageTransformActionExecuter.PARAM_MIME_TYPE, MimetypeMap.MIMETYPE_IMAGE_JPEG); 569 actionProps2.put(ImageTransformActionExecuter.PARAM_DESTINATION_FOLDER, nodeRef); 570 actionProps2.put(ImageTransformActionExecuter.PARAM_ASSOC_QNAME, ContentModel.ASSOC_CHILDREN); 571 572 Rule rule2 = this.ruleService.createRule(this.ruleType.getName()); 573 rule2.setTitle("Convert from *.gif to *.jpg"); 574 rule2.setExecuteAsynchronously(true); 575 576 ActionCondition actionCondition2 = this.actionService.createActionCondition(ComparePropertyValueEvaluator.NAME); 577 actionCondition2.setParameterValues(conditionProps2); 578 rule2.addActionCondition(actionCondition2); 579 580 Action action2 = this.actionService.createAction(ImageTransformActionExecuter.NAME); 581 action2.setParameterValues(actionProps2); 582 rule2.addAction(action2); 583 584 this.ruleService.saveRule(nodeRef, rule); 586 this.ruleService.saveRule(nodeRef, rule); 587 588 NodeRef contentNode = this.nodeService.createNode(nodeRef, 590 ContentModel.ASSOC_CHILDREN, 591 QName.createQName("{test}testnode"), 592 ContentModel.TYPE_CONTENT).getChildRef(); 593 this.nodeService.setProperty(contentNode, ContentModel.PROP_NAME, "myFile.jpg"); 594 File file = AbstractContentTransformerTest.loadQuickTestFile("jpg"); 595 ContentWriter writer = this.contentService.getWriter(contentNode, ContentModel.PROP_CONTENT, true); 596 writer.setEncoding("UTF-8"); 597 writer.setMimetype(MimetypeMap.MIMETYPE_IMAGE_JPEG); 598 writer.putContent(file); 599 600 setComplete(); 601 endTransaction(); 602 603 605 } 625 } 626 | Popular Tags |