1 17 package org.alfresco.repo.rule.ruletrigger; 18 19 import org.alfresco.model.ContentModel; 20 import org.alfresco.repo.content.MimetypeMap; 21 import org.alfresco.service.cmr.repository.ContentService; 22 import org.alfresco.service.cmr.repository.ContentWriter; 23 import org.alfresco.service.cmr.repository.NodeRef; 24 import org.alfresco.service.cmr.repository.NodeService; 25 import org.alfresco.service.cmr.repository.StoreRef; 26 import org.alfresco.service.cmr.rule.RuleType; 27 import org.alfresco.util.BaseSpringTest; 28 29 34 public class RuleTriggerTest extends BaseSpringTest 35 { 36 private static final String ON_CREATE_NODE_TRIGGER = "on-create-node-trigger"; 37 private static final String ON_UPDATE_NODE_TRIGGER = "on-update-node-trigger"; 38 private static final String ON_DELETE_NODE_TRIGGER = "on-delete-node-trigger"; 39 private static final String ON_CREATE_CHILD_ASSOCIATION_TRIGGER = "on-create-child-association-trigger"; 40 private static final String ON_DELETE_CHILD_ASSOCIATION_TRIGGER = "on-delete-child-association-trigger"; 41 private static final String ON_CREATE_ASSOCIATION_TRIGGER = "on-create-association-trigger"; 42 private static final String ON_DELETE_ASSOCIATION_TRIGGER = "on-delete-association-trigger"; 43 private static final String ON_CONTENT_UPDATE_TRIGGER = "on-content-update-trigger"; 44 private static final String ON_CONTENT_CREATE_TRIGGER = "on-content-create-trigger"; 45 46 private NodeService nodeService; 47 private ContentService contentService; 48 49 private StoreRef testStoreRef; 50 private NodeRef rootNodeRef; 51 52 @Override 53 protected void onSetUpInTransaction() throws Exception 54 { 55 this.nodeService = (NodeService)this.applicationContext.getBean("nodeService"); 56 this.contentService = (ContentService)this.applicationContext.getBean("contentService"); 57 58 this.testStoreRef = this.nodeService.createStore(StoreRef.PROTOCOL_WORKSPACE, "Test_" + System.currentTimeMillis()); 59 this.rootNodeRef = this.nodeService.getRootNode(this.testStoreRef); 60 } 61 62 public void testOnCreateNodeTrigger() 63 { 64 TestRuleType ruleType = createTestRuleType(ON_CREATE_NODE_TRIGGER); 65 assertFalse(ruleType.rulesTriggered); 66 67 this.nodeService.createNode( 69 this.rootNodeRef, 70 ContentModel.ASSOC_CHILDREN, 71 ContentModel.ASSOC_CHILDREN, 72 ContentModel.TYPE_CONTAINER); 73 74 assertTrue(ruleType.rulesTriggered); 76 } 77 78 public void testOnUpdateNodeTrigger() 79 { 80 NodeRef nodeRef = this.nodeService.createNode( 81 this.rootNodeRef, 82 ContentModel.ASSOC_CHILDREN, 83 ContentModel.ASSOC_CHILDREN, 84 ContentModel.TYPE_CONTAINER).getChildRef(); 85 86 TestRuleType ruleType = createTestRuleType(ON_UPDATE_NODE_TRIGGER); 87 assertFalse(ruleType.rulesTriggered); 88 89 this.nodeService.setProperty(nodeRef, ContentModel.PROP_NAME, "nameChanged"); 91 92 assertTrue(ruleType.rulesTriggered); 94 } 95 96 public void testOnDeleteNodeTrigger() 97 { 98 NodeRef nodeRef = this.nodeService.createNode( 99 this.rootNodeRef, 100 ContentModel.ASSOC_CHILDREN, 101 ContentModel.ASSOC_CHILDREN, 102 ContentModel.TYPE_CONTAINER).getChildRef(); 103 104 TestRuleType ruleType = createTestRuleType(ON_DELETE_NODE_TRIGGER); 105 assertFalse(ruleType.rulesTriggered); 106 107 this.nodeService.deleteNode(nodeRef); 109 110 assertTrue(ruleType.rulesTriggered); 112 } 113 114 public void testOnCreateChildAssociationTrigger() 115 { 116 NodeRef nodeRef = this.nodeService.createNode( 117 this.rootNodeRef, 118 ContentModel.ASSOC_CHILDREN, 119 ContentModel.ASSOC_CHILDREN, 120 ContentModel.TYPE_CONTAINER).getChildRef(); 121 NodeRef nodeRef2 = this.nodeService.createNode( 122 this.rootNodeRef, 123 ContentModel.ASSOC_CHILDREN, 124 ContentModel.ASSOC_CHILDREN, 125 ContentModel.TYPE_CONTAINER).getChildRef(); 126 127 TestRuleType ruleType = createTestRuleType(ON_CREATE_CHILD_ASSOCIATION_TRIGGER); 128 assertFalse(ruleType.rulesTriggered); 129 130 this.nodeService.addChild( 132 nodeRef, 133 nodeRef2, 134 ContentModel.ASSOC_CHILDREN, 135 ContentModel.ASSOC_CHILDREN); 136 137 assertTrue(ruleType.rulesTriggered); 139 } 140 141 public void testOnDeleteChildAssociationTrigger() 142 { 143 NodeRef nodeRef = this.nodeService.createNode( 144 this.rootNodeRef, 145 ContentModel.ASSOC_CHILDREN, 146 ContentModel.ASSOC_CHILDREN, 147 ContentModel.TYPE_CONTAINER).getChildRef(); 148 NodeRef nodeRef2 = this.nodeService.createNode( 149 this.rootNodeRef, 150 ContentModel.ASSOC_CHILDREN, 151 ContentModel.ASSOC_CHILDREN, 152 ContentModel.TYPE_CONTAINER).getChildRef(); 153 this.nodeService.addChild( 154 nodeRef, 155 nodeRef2, 156 ContentModel.ASSOC_CHILDREN, 157 ContentModel.ASSOC_CHILDREN); 158 159 TestRuleType ruleType = createTestRuleType(ON_DELETE_CHILD_ASSOCIATION_TRIGGER); 160 assertFalse(ruleType.rulesTriggered); 161 162 this.nodeService.removeChild(nodeRef, nodeRef2); 164 165 assertTrue(ruleType.rulesTriggered); 167 } 168 169 public void testOnCreateAssociationTrigger() 170 { 171 NodeRef nodeRef = this.nodeService.createNode( 172 this.rootNodeRef, 173 ContentModel.ASSOC_CHILDREN, 174 ContentModel.ASSOC_CHILDREN, 175 ContentModel.TYPE_CONTAINER).getChildRef(); 176 NodeRef nodeRef2 = this.nodeService.createNode( 177 this.rootNodeRef, 178 ContentModel.ASSOC_CHILDREN, 179 ContentModel.ASSOC_CHILDREN, 180 ContentModel.TYPE_CONTAINER).getChildRef(); 181 182 TestRuleType ruleType = createTestRuleType(ON_CREATE_ASSOCIATION_TRIGGER); 183 assertFalse(ruleType.rulesTriggered); 184 185 this.nodeService.createAssociation(nodeRef, nodeRef2, ContentModel.ASSOC_CHILDREN); 187 188 assertTrue(ruleType.rulesTriggered); 190 } 191 192 public void testOnDeleteAssociationTrigger() 193 { 194 NodeRef nodeRef = this.nodeService.createNode( 195 this.rootNodeRef, 196 ContentModel.ASSOC_CHILDREN, 197 ContentModel.ASSOC_CHILDREN, 198 ContentModel.TYPE_CONTAINER).getChildRef(); 199 NodeRef nodeRef2 = this.nodeService.createNode( 200 this.rootNodeRef, 201 ContentModel.ASSOC_CHILDREN, 202 ContentModel.ASSOC_CHILDREN, 203 ContentModel.TYPE_CONTAINER).getChildRef(); 204 this.nodeService.createAssociation(nodeRef, nodeRef2, ContentModel.ASSOC_CHILDREN); 205 206 TestRuleType ruleType = createTestRuleType(ON_DELETE_ASSOCIATION_TRIGGER); 207 assertFalse(ruleType.rulesTriggered); 208 209 this.nodeService.removeAssociation(nodeRef, nodeRef2, ContentModel.ASSOC_CHILDREN); 211 212 assertTrue(ruleType.rulesTriggered); 214 } 215 216 public void testOnContentCreateTrigger() 217 { 218 NodeRef nodeRef = this.nodeService.createNode( 219 this.rootNodeRef, 220 ContentModel.ASSOC_CHILDREN, 221 ContentModel.ASSOC_CHILDREN, 222 ContentModel.TYPE_CONTENT).getChildRef(); 223 224 TestRuleType contentCreate = createTestRuleType(ON_CONTENT_CREATE_TRIGGER); 225 assertFalse(contentCreate.rulesTriggered); 226 227 ContentWriter contentWriter = this.contentService.getWriter(nodeRef, ContentModel.PROP_CONTENT, true); 229 contentWriter.setMimetype(MimetypeMap.MIMETYPE_TEXT_PLAIN); 230 contentWriter.setEncoding("UTF-8"); 231 contentWriter.putContent("some content"); 232 233 assertTrue(contentCreate.rulesTriggered); 235 } 236 237 public void testOnContentUpdateTrigger() 238 { 239 NodeRef nodeRef = this.nodeService.createNode( 240 this.rootNodeRef, 241 ContentModel.ASSOC_CHILDREN, 242 ContentModel.ASSOC_CHILDREN, 243 ContentModel.TYPE_CONTENT).getChildRef(); 244 245 TestRuleType contentUpdate = createTestRuleType(ON_CONTENT_UPDATE_TRIGGER); 246 assertFalse(contentUpdate.rulesTriggered); 247 248 ContentWriter contentWriter = this.contentService.getWriter(nodeRef, ContentModel.PROP_CONTENT, true); 250 contentWriter.setMimetype(MimetypeMap.MIMETYPE_TEXT_PLAIN); 251 contentWriter.setEncoding("UTF-8"); 252 contentWriter.putContent("some content"); 253 254 assertFalse(contentUpdate.rulesTriggered); 256 257 ContentWriter contentWriter2 = this.contentService.getWriter(nodeRef, ContentModel.PROP_CONTENT, true); 259 contentWriter2.setMimetype(MimetypeMap.MIMETYPE_TEXT_PLAIN); 260 contentWriter2.setEncoding("UTF-8"); 261 contentWriter2.putContent("more content some content"); 262 263 assertTrue(contentUpdate.rulesTriggered); 265 } 266 267 private TestRuleType createTestRuleType(String ruleTriggerName) 268 { 269 RuleTrigger ruleTrigger = (RuleTrigger)this.applicationContext.getBean(ruleTriggerName); 270 assertNotNull(ruleTrigger); 271 TestRuleType ruleType = new TestRuleType(); 272 ruleTrigger.registerRuleType(ruleType); 273 return ruleType; 274 } 275 276 private class TestRuleType implements RuleType 277 { 278 public boolean rulesTriggered = false; 279 280 public String getName() 281 { 282 return "testRuleType"; 283 } 284 285 public String getDisplayLabel() 286 { 287 return "displayLabel"; 288 } 289 290 public void triggerRuleType(NodeRef nodeRef, NodeRef actionedUponNodeRef) 291 { 292 this.rulesTriggered = true; 294 } 295 } 296 } 297 | Popular Tags |