KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > repo > rule > RuleServiceCoverageTest


1 /*
2  * Copyright (C) 2005 Alfresco, Inc.
3  *
4  * Licensed under the Mozilla Public License version 1.1
5  * with a permitted attribution clause. You may obtain a
6  * copy of the License at
7  *
8  * http://www.alfresco.org/legal/license.txt
9  *
10  * Unless required by applicable law or agreed to in writing,
11  * software distributed under the License is distributed on an
12  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13  * either express or implied. See the License for the specific
14  * language governing permissions and limitations under the
15  * License.
16  */

17 package org.alfresco.repo.rule;
18
19 import java.io.File JavaDoc;
20 import java.io.Serializable JavaDoc;
21 import java.util.ArrayList JavaDoc;
22 import java.util.HashMap JavaDoc;
23 import java.util.List JavaDoc;
24 import java.util.Map JavaDoc;
25
26 import javax.transaction.UserTransaction JavaDoc;
27
28 import junit.framework.TestCase;
29
30 import org.alfresco.model.ContentModel;
31 import org.alfresco.repo.action.ActionServiceImplTest;
32 import org.alfresco.repo.action.ActionServiceImplTest.AsyncTest;
33 import org.alfresco.repo.action.evaluator.ComparePropertyValueEvaluator;
34 import org.alfresco.repo.action.evaluator.InCategoryEvaluator;
35 import org.alfresco.repo.action.evaluator.NoConditionEvaluator;
36 import org.alfresco.repo.action.executer.AddFeaturesActionExecuter;
37 import org.alfresco.repo.action.executer.CheckInActionExecuter;
38 import org.alfresco.repo.action.executer.CheckOutActionExecuter;
39 import org.alfresco.repo.action.executer.CopyActionExecuter;
40 import org.alfresco.repo.action.executer.ImageTransformActionExecuter;
41 import org.alfresco.repo.action.executer.LinkCategoryActionExecuter;
42 import org.alfresco.repo.action.executer.MailActionExecuter;
43 import org.alfresco.repo.action.executer.MoveActionExecuter;
44 import org.alfresco.repo.action.executer.SimpleWorkflowActionExecuter;
45 import org.alfresco.repo.action.executer.TransformActionExecuter;
46 import org.alfresco.repo.content.MimetypeMap;
47 import org.alfresco.repo.content.transform.AbstractContentTransformerTest;
48 import org.alfresco.repo.content.transform.ContentTransformerRegistry;
49 import org.alfresco.repo.dictionary.DictionaryDAO;
50 import org.alfresco.repo.dictionary.M2Aspect;
51 import org.alfresco.repo.dictionary.M2Model;
52 import org.alfresco.repo.dictionary.M2Property;
53 import org.alfresco.repo.security.authentication.AuthenticationComponent;
54 import org.alfresco.repo.transaction.TransactionUtil;
55 import org.alfresco.service.ServiceRegistry;
56 import org.alfresco.service.cmr.action.Action;
57 import org.alfresco.service.cmr.action.ActionCondition;
58 import org.alfresco.service.cmr.action.ActionService;
59 import org.alfresco.service.cmr.coci.CheckOutCheckInService;
60 import org.alfresco.service.cmr.dictionary.DataTypeDefinition;
61 import org.alfresco.service.cmr.lock.LockService;
62 import org.alfresco.service.cmr.lock.LockStatus;
63 import org.alfresco.service.cmr.repository.ChildAssociationRef;
64 import org.alfresco.service.cmr.repository.ContentData;
65 import org.alfresco.service.cmr.repository.ContentService;
66 import org.alfresco.service.cmr.repository.ContentWriter;
67 import org.alfresco.service.cmr.repository.CopyService;
68 import org.alfresco.service.cmr.repository.NodeRef;
69 import org.alfresco.service.cmr.repository.NodeService;
70 import org.alfresco.service.cmr.repository.StoreRef;
71 import org.alfresco.service.cmr.rule.Rule;
72 import org.alfresco.service.cmr.rule.RuleService;
73 import org.alfresco.service.cmr.rule.RuleServiceException;
74 import org.alfresco.service.cmr.rule.RuleType;
75 import org.alfresco.service.namespace.NamespaceService;
76 import org.alfresco.service.namespace.QName;
77 import org.alfresco.service.namespace.RegexQNamePattern;
78 import org.alfresco.service.transaction.TransactionService;
79 import org.springframework.context.ApplicationContext;
80 import org.springframework.context.support.ClassPathXmlApplicationContext;
81 import org.springframework.util.StopWatch;
82
83 /**
84  * @author Roy Wetherall
85  */

86 public class RuleServiceCoverageTest extends TestCase
87 {
88     //private static final ContentData CONTENT_DATA_TEXT = new ContentData(null, MimetypeMap.MIMETYPE_TEXT_PLAIN, 0L, "UTF-8");
89

90     /**
91      * Application context used during the test
92      */

93     static ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:alfresco/application-context.xml");
94     
95     /**
96      * Services used during the tests
97      */

98     private TransactionService transactionService;
99     private RuleService ruleService;
100     private NodeService nodeService;
101     private StoreRef testStoreRef;
102     private NodeRef rootNodeRef;
103     private NodeRef nodeRef;
104     private CheckOutCheckInService cociService;
105     private LockService lockService;
106     private ContentService contentService;
107     private ServiceRegistry serviceRegistry;
108     private DictionaryDAO dictionaryDAO;
109     private ActionService actionService;
110     private ContentTransformerRegistry transformerRegistry;
111     private CopyService copyService;
112     
113     /**
114      * Category related values
115      */

116     private static final String JavaDoc TEST_NAMESPACE = "http://www.alfresco.org/test/rulesystemtest";
117     private static final QName CAT_PROP_QNAME = QName.createQName(TEST_NAMESPACE, "region");
118     private QName regionCategorisationQName;
119     private NodeRef catContainer;
120     private NodeRef catRoot;
121     private NodeRef catRBase;
122     private NodeRef catROne;
123     private NodeRef catRTwo;
124     @SuppressWarnings JavaDoc("unused")
125     private NodeRef catRThree;
126     
127     /**
128      * Standard content text
129      */

130     private static final String JavaDoc STANDARD_TEXT_CONTENT = "standardTextContent";
131     
132     /**
133      * Setup method
134      */

135     @Override JavaDoc
136     protected void setUp() throws Exception JavaDoc
137     {
138         // Get the required services
139
this.serviceRegistry = (ServiceRegistry)applicationContext.getBean(ServiceRegistry.SERVICE_REGISTRY);
140         this.nodeService = serviceRegistry.getNodeService();
141         this.ruleService = serviceRegistry.getRuleService();
142         this.cociService = serviceRegistry.getCheckOutCheckInService();
143         this.lockService = serviceRegistry.getLockService();
144         this.copyService = serviceRegistry.getCopyService();
145         this.contentService = serviceRegistry.getContentService();
146         this.dictionaryDAO = (DictionaryDAO)applicationContext.getBean("dictionaryDAO");
147         this.actionService = serviceRegistry.getActionService();
148         this.transactionService = serviceRegistry.getTransactionService();
149         this.transformerRegistry = (ContentTransformerRegistry)applicationContext.getBean("contentTransformerRegistry");
150         
151         AuthenticationComponent authenticationComponent = (AuthenticationComponent)applicationContext.getBean("authenticationComponent");
152         authenticationComponent.setCurrentUser(authenticationComponent.getSystemUserName());
153             
154         this.testStoreRef = this.nodeService.createStore(StoreRef.PROTOCOL_WORKSPACE, "Test_" + System.currentTimeMillis());
155         this.rootNodeRef = this.nodeService.getRootNode(this.testStoreRef);
156         
157         // Create the node used for tests
158
this.nodeRef = this.nodeService.createNode(
159                 this.rootNodeRef,
160                 ContentModel.ASSOC_CHILDREN,
161                 ContentModel.ASSOC_CHILDREN,
162                 ContentModel.TYPE_CONTAINER).getChildRef();
163         
164         // Create and authenticate the user used in the tests
165
//TestWithUserUtils.createUser(USER_NAME, PWD, this.rootNodeRef, this.nodeService, this.authenticationService);
166
//TestWithUserUtils.authenticateUser(USER_NAME, PWD, this.rootNodeRef, this.authenticationService);
167
}
168     
169     private Rule createRule(
170             String JavaDoc ruleTypeName,
171             String JavaDoc actionName,
172             Map JavaDoc<String JavaDoc, Serializable JavaDoc> actionParams,
173             String JavaDoc conditionName,
174             Map JavaDoc<String JavaDoc, Serializable JavaDoc> conditionParams)
175     {
176         Rule rule = this.ruleService.createRule(ruleTypeName);
177         ActionCondition condition = this.actionService.createActionCondition(conditionName, conditionParams);
178         rule.addActionCondition(condition);
179         Action action = this.actionService.createAction(actionName, actionParams);
180         rule.addAction(action);
181         return rule;
182     }
183     
184     /**
185      * Create the categories used in the tests
186      */

187     private void createTestCategories()
188     {
189         // Create the test model
190
M2Model model = M2Model.createModel("test:rulecategory");
191         model.createNamespace(TEST_NAMESPACE, "test");
192         model.createImport(NamespaceService.DICTIONARY_MODEL_1_0_URI, "d");
193         model.createImport(NamespaceService.CONTENT_MODEL_1_0_URI, NamespaceService.CONTENT_MODEL_PREFIX);
194         
195         // Create the region category
196
regionCategorisationQName = QName.createQName(TEST_NAMESPACE, "Region");
197         M2Aspect generalCategorisation = model.createAspect("test:" + regionCategorisationQName.getLocalName());
198         generalCategorisation.setParentName("cm:" + ContentModel.ASPECT_CLASSIFIABLE.getLocalName());
199         M2Property genCatProp = generalCategorisation.createProperty("test:region");
200         genCatProp.setIndexed(true);
201         genCatProp.setIndexedAtomically(true);
202         genCatProp.setMandatory(true);
203         genCatProp.setMultiValued(false);
204         genCatProp.setStoredInIndex(true);
205         genCatProp.setTokenisedInIndex(true);
206         genCatProp.setType("d:" + DataTypeDefinition.CATEGORY.getLocalName());
207
208         // Save the mode
209
dictionaryDAO.putModel(model);
210         
211         // Create the category value container and root
212
catContainer = nodeService.createNode(rootNodeRef, ContentModel.ASSOC_CHILDREN, QName.createQName(TEST_NAMESPACE, "categoryContainer"), ContentModel.TYPE_CONTAINER).getChildRef();
213         catRoot = nodeService.createNode(catContainer, ContentModel.ASSOC_CHILDREN, QName.createQName(TEST_NAMESPACE, "categoryRoot"), ContentModel.TYPE_CATEGORYROOT).getChildRef();
214
215         // Create the category values
216
catRBase = nodeService.createNode(catRoot, ContentModel.ASSOC_CATEGORIES, QName.createQName(TEST_NAMESPACE, "Region"), ContentModel.TYPE_CATEGORY).getChildRef();
217         catROne = nodeService.createNode(catRBase, ContentModel.ASSOC_SUBCATEGORIES, QName.createQName(TEST_NAMESPACE, "Europe"), ContentModel.TYPE_CATEGORY).getChildRef();
218         catRTwo = nodeService.createNode(catRBase, ContentModel.ASSOC_SUBCATEGORIES, QName.createQName(TEST_NAMESPACE, "RestOfWorld"), ContentModel.TYPE_CATEGORY).getChildRef();
219         catRThree = nodeService.createNode(catRTwo, ContentModel.ASSOC_SUBCATEGORIES, QName.createQName(TEST_NAMESPACE, "US"), ContentModel.TYPE_CATEGORY).getChildRef();
220     }
221     
222     /**
223      * Asynchronous rule tests
224      */

225     
226     /**
227      * Check async rule execution
228      */

229     public void testAsyncRuleExecution()
230     {
231         final NodeRef newNodeRef = TransactionUtil.executeInUserTransaction(
232                 this.transactionService,
233                 new TransactionUtil.TransactionWork<NodeRef>()
234                 {
235                     public NodeRef doWork()
236                     {
237                         RuleServiceCoverageTest.this.nodeService.addAspect(
238                                 RuleServiceCoverageTest.this.nodeRef,
239                                 ContentModel.ASPECT_LOCKABLE,
240                                 null);
241                         
242                         Map JavaDoc<String JavaDoc, Serializable JavaDoc> params = new HashMap JavaDoc<String JavaDoc, Serializable JavaDoc>(1);
243                         params.put("aspect-name", ContentModel.ASPECT_VERSIONABLE);
244                         
245                         Rule rule = createRule(
246                                 RuleType.INBOUND,
247                                 AddFeaturesActionExecuter.NAME,
248                                 params,
249                                 NoConditionEvaluator.NAME,
250                                 null);
251                         rule.setExecuteAsynchronously(true);
252                         
253                         RuleServiceCoverageTest.this.ruleService.saveRule(RuleServiceCoverageTest.this.nodeRef, rule);
254
255                         NodeRef newNodeRef = RuleServiceCoverageTest.this.nodeService.createNode(
256                                 RuleServiceCoverageTest.this.nodeRef,
257                                 ContentModel.ASSOC_CHILDREN,
258                                 QName.createQName(TEST_NAMESPACE, "children"),
259                                 ContentModel.TYPE_CONTENT,
260                                 getContentProperties()).getChildRef();
261                         addContentToNode(newNodeRef);
262                         
263                         return newNodeRef;
264                     }
265                 });
266         
267         ActionServiceImplTest.postAsyncActionTest(
268                 this.transactionService,
269                 1000,
270                 10,
271                 new AsyncTest()
272                 {
273                     public boolean executeTest()
274                     {
275                         return RuleServiceCoverageTest.this.nodeService.hasAspect(
276                                 newNodeRef,
277                                 ContentModel.ASPECT_VERSIONABLE);
278                     };
279                 });
280     }
281     
282     // TODO check compensating action execution
283

284     /**
285      * Standard rule coverage tests
286      */

287
288     /**
289      * Test:
290      * rule type: inbound
291      * condition: no-condition()
292      * action: add-features(
293      * aspect-name = versionable)
294      */

295     public void testAddFeaturesAction()
296     {
297         this.nodeService.addAspect(this.nodeRef, ContentModel.ASPECT_LOCKABLE, null);
298         
299         Map JavaDoc<String JavaDoc, Serializable JavaDoc> params = new HashMap JavaDoc<String JavaDoc, Serializable JavaDoc>(1);
300         params.put("aspect-name", ContentModel.ASPECT_VERSIONABLE);
301         
302         Rule rule = createRule(
303                 RuleType.INBOUND,
304                 AddFeaturesActionExecuter.NAME,
305                 params,
306                 NoConditionEvaluator.NAME,
307                 null);
308         
309         this.ruleService.saveRule(this.nodeRef, rule);
310
311         NodeRef newNodeRef = this.nodeService.createNode(
312                 this.nodeRef,
313                 ContentModel.ASSOC_CHILDREN,
314                 QName.createQName(TEST_NAMESPACE, "children"),
315                 ContentModel.TYPE_CONTENT,
316                 getContentProperties()).getChildRef();
317         addContentToNode(newNodeRef);
318         assertTrue(this.nodeService.hasAspect(newNodeRef, ContentModel.ASPECT_VERSIONABLE));
319         
320         Map JavaDoc<String JavaDoc, Serializable JavaDoc> params2 = new HashMap JavaDoc<String JavaDoc, Serializable JavaDoc>(2);
321         params2.put(AddFeaturesActionExecuter.PARAM_ASPECT_NAME, ContentModel.ASPECT_SIMPLE_WORKFLOW);
322         params2.put(ContentModel.PROP_APPROVE_STEP.toString(), "approveStep");
323         params2.put(ContentModel.PROP_APPROVE_MOVE.toString(), false);
324         
325         // Test that rule can be updated and execute correctly
326
rule.removeAllActions();
327         Action action2 = this.actionService.createAction(AddFeaturesActionExecuter.NAME, params2);
328         rule.addAction(action2);
329         this.ruleService.saveRule(this.nodeRef, rule);
330         
331         NodeRef newNodeRef2 = this.nodeService.createNode(
332                 this.nodeRef,
333                 ContentModel.ASSOC_CHILDREN,
334                 QName.createQName(TEST_NAMESPACE, "children"),
335                 ContentModel.TYPE_CONTENT,
336                 getContentProperties()).getChildRef();
337         addContentToNode(newNodeRef2);
338         assertTrue(this.nodeService.hasAspect(newNodeRef2, ContentModel.ASPECT_SIMPLE_WORKFLOW));
339         assertEquals("approveStep", this.nodeService.getProperty(newNodeRef2, ContentModel.PROP_APPROVE_STEP));
340         assertEquals(false, this.nodeService.getProperty(newNodeRef2, ContentModel.PROP_APPROVE_MOVE));
341         
342         // System.out.println(NodeStoreInspector.dumpNodeStore(this.nodeService, this.testStoreRef));
343
}
344     
345     public void testDisableRule()
346     {
347         this.nodeService.addAspect(this.nodeRef, ContentModel.ASPECT_LOCKABLE, null);
348         
349         Map JavaDoc<String JavaDoc, Serializable JavaDoc> params = new HashMap JavaDoc<String JavaDoc, Serializable JavaDoc>(1);
350         params.put("aspect-name", ContentModel.ASPECT_VERSIONABLE);
351         
352         Rule rule = createRule(
353                 RuleType.INBOUND,
354                 AddFeaturesActionExecuter.NAME,
355                 params,
356                 NoConditionEvaluator.NAME,
357                 null);
358         
359         this.ruleService.saveRule(this.nodeRef, rule);
360         this.ruleService.disableRule(rule);
361         
362         NodeRef newNodeRef = this.nodeService.createNode(
363                 this.nodeRef,
364                 ContentModel.ASSOC_CHILDREN,
365                 QName.createQName(TEST_NAMESPACE, "children"),
366                 ContentModel.TYPE_CONTENT,
367                 getContentProperties()).getChildRef();
368         addContentToNode(newNodeRef);
369         assertFalse(this.nodeService.hasAspect(newNodeRef, ContentModel.ASPECT_VERSIONABLE));
370         
371         this.ruleService.enableRule(rule);
372         
373         NodeRef newNodeRef2 = this.nodeService.createNode(
374                 this.nodeRef,
375                 ContentModel.ASSOC_CHILDREN,
376                 QName.createQName(TEST_NAMESPACE, "children"),
377                 ContentModel.TYPE_CONTENT,
378                 getContentProperties()).getChildRef();
379         addContentToNode(newNodeRef2);
380         assertTrue(this.nodeService.hasAspect(newNodeRef2, ContentModel.ASPECT_VERSIONABLE));
381         
382     }
383     
384     public void testAddFeaturesToAFolder()
385     {
386         Map JavaDoc<String JavaDoc, Serializable JavaDoc> params = new HashMap JavaDoc<String JavaDoc, Serializable JavaDoc>(1);
387         params.put("aspect-name", ContentModel.ASPECT_TEMPLATABLE);
388         
389         Rule rule = createRule(
390                 RuleType.INBOUND,
391                 AddFeaturesActionExecuter.NAME,
392                 params,
393                 NoConditionEvaluator.NAME,
394                 null);
395         
396         this.ruleService.saveRule(this.nodeRef, rule);
397
398         NodeRef newNodeRef = this.nodeService.createNode(
399                 this.nodeRef,
400                 ContentModel.ASSOC_CHILDREN,
401                 QName.createQName(TEST_NAMESPACE, "children"),
402                 ContentModel.TYPE_FOLDER).getChildRef();
403         
404         assertTrue(this.nodeService.hasAspect(newNodeRef, ContentModel.ASPECT_TEMPLATABLE));
405         
406         // System.out.println(NodeStoreInspector.dumpNodeStore(this.nodeService, this.testStoreRef));
407
}
408     
409     public void testCopyFolderToTriggerRules()
410     {
411         // Create the folders and content
412
NodeRef copyToFolder = this.nodeService.createNode(
413                 this.rootNodeRef,
414                 ContentModel.ASSOC_CHILDREN,
415                 QName.createQName("{test}copyToFolder"),
416                 ContentModel.TYPE_FOLDER).getChildRef();
417         NodeRef folderToCopy = this.nodeService.createNode(
418                 this.rootNodeRef,
419                 ContentModel.ASSOC_CHILDREN,
420                 QName.createQName("{test}folderToCopy"),
421                 ContentModel.TYPE_FOLDER).getChildRef();
422         NodeRef contentToCopy = this.nodeService.createNode(
423                 folderToCopy,
424                 ContentModel.ASSOC_CONTAINS,
425                 QName.createQName("{test}contentToCopy"),
426                 ContentModel.TYPE_CONTENT,
427                 getContentProperties()).getChildRef();
428         addContentToNode(contentToCopy);
429         
430         // Create the rule and add to folder
431
Map JavaDoc<String JavaDoc, Serializable JavaDoc> params = new HashMap JavaDoc<String JavaDoc, Serializable JavaDoc>(1);
432         params.put("aspect-name", ContentModel.ASPECT_TEMPLATABLE);
433         Rule rule = createRule(
434                 RuleType.INBOUND,
435                 AddFeaturesActionExecuter.NAME,
436                 params,
437                 NoConditionEvaluator.NAME,
438                 null);
439         rule.applyToChildren(true);
440         this.ruleService.saveRule(copyToFolder, rule);
441         
442         // Copy the folder in order to try and trigger the rule
443
NodeRef copiedFolder = this.copyService.copy(folderToCopy, copyToFolder, ContentModel.ASSOC_CONTAINS, QName.createQName("{test}coppiedFolder"), true);
444         assertNotNull(copiedFolder);
445         
446         // Check that the rule has been applied to the copied folder and content
447
assertTrue(this.nodeService.hasAspect(copiedFolder, ContentModel.ASPECT_TEMPLATABLE));
448         for (ChildAssociationRef childAssoc : this.nodeService.getChildAssocs(copiedFolder))
449         {
450             assertTrue(this.nodeService.hasAspect(childAssoc.getChildRef(), ContentModel.ASPECT_TEMPLATABLE));
451         }
452         
453         //System.out.println(NodeStoreInspector.dumpNodeStore(this.nodeService, this.testStoreRef));
454
}
455     
456     private Map JavaDoc<QName, Serializable JavaDoc> getContentProperties()
457     {
458    // Map<QName, Serializable> properties = new HashMap<QName, Serializable>(1);
459
// properties.put(ContentModel.PROP_CONTENT, CONTENT_DATA_TEXT);
460
return null;
461     }
462
463     /**
464      * Test:
465      * rule type: inbound
466      * condition: no-condition
467      * action: simple-workflow
468      */

469     public void testSimpleWorkflowAction()
470     {
471         this.nodeService.addAspect(this.nodeRef, ContentModel.ASPECT_LOCKABLE, null);
472         
473         Map JavaDoc<String JavaDoc, Serializable JavaDoc> params = new HashMap JavaDoc<String JavaDoc, Serializable JavaDoc>(1);
474         params.put(SimpleWorkflowActionExecuter.PARAM_APPROVE_STEP, "approveStep");
475         params.put(SimpleWorkflowActionExecuter.PARAM_APPROVE_FOLDER, this.rootNodeRef);
476         params.put(SimpleWorkflowActionExecuter.PARAM_APPROVE_MOVE, true);
477         params.put(SimpleWorkflowActionExecuter.PARAM_REJECT_STEP, "rejectStep");
478         params.put(SimpleWorkflowActionExecuter.PARAM_REJECT_FOLDER, this.rootNodeRef);
479         params.put(SimpleWorkflowActionExecuter.PARAM_REJECT_MOVE, false);
480         
481         Rule rule = createRule(
482                 RuleType.INBOUND,
483                 SimpleWorkflowActionExecuter.NAME,
484                 params,
485                 NoConditionEvaluator.NAME,
486                 null);
487
488         this.ruleService.saveRule(this.nodeRef, rule);
489                 
490         NodeRef newNodeRef = this.nodeService.createNode(
491                 this.nodeRef,
492                 ContentModel.ASSOC_CHILDREN,
493                 QName.createQName(TEST_NAMESPACE, "children"),
494                 ContentModel.TYPE_CONTENT,
495                 getContentProperties()).getChildRef();
496         addContentToNode(newNodeRef);
497         
498         assertTrue(this.nodeService.hasAspect(newNodeRef, ContentModel.ASPECT_SIMPLE_WORKFLOW));
499         assertEquals("approveStep", this.nodeService.getProperty(newNodeRef, ContentModel.PROP_APPROVE_STEP));
500         assertEquals(this.rootNodeRef, this.nodeService.getProperty(newNodeRef, ContentModel.PROP_APPROVE_FOLDER));
501         assertTrue(((Boolean JavaDoc)this.nodeService.getProperty(newNodeRef, ContentModel.PROP_APPROVE_MOVE)).booleanValue());
502         assertTrue(this.nodeService.hasAspect(newNodeRef, ContentModel.ASPECT_SIMPLE_WORKFLOW));
503         assertEquals("rejectStep", this.nodeService.getProperty(newNodeRef, ContentModel.PROP_REJECT_STEP));
504         assertEquals(this.rootNodeRef, this.nodeService.getProperty(newNodeRef, ContentModel.PROP_REJECT_FOLDER));
505         assertFalse(((Boolean JavaDoc)this.nodeService.getProperty(newNodeRef, ContentModel.PROP_REJECT_MOVE)).booleanValue());
506         
507         // System.out.println(NodeStoreInspector.dumpNodeStore(this.nodeService, this.testStoreRef));
508
}
509     
510     /**
511      * Test:
512      * rule type: inbound
513      * condition: in-category
514      * action: add-feature
515      */

516     public void testInCategoryCondition()
517     {
518         // Create categories used in tests
519
createTestCategories();
520         
521         try
522         {
523             Map JavaDoc<String JavaDoc, Serializable JavaDoc> params = new HashMap JavaDoc<String JavaDoc, Serializable JavaDoc>(1);
524             params.put(InCategoryEvaluator.PARAM_CATEGORY_ASPECT, this.regionCategorisationQName);
525             params.put(InCategoryEvaluator.PARAM_CATEGORY_VALUE, this.catROne);
526             
527             Map JavaDoc<String JavaDoc, Serializable JavaDoc> params2 = new HashMap JavaDoc<String JavaDoc, Serializable JavaDoc>(1);
528             params2.put("aspect-name", ContentModel.ASPECT_VERSIONABLE);
529             
530             Rule rule = createRule(
531                     RuleType.INBOUND,
532                     AddFeaturesActionExecuter.NAME,
533                     params2,
534                     InCategoryEvaluator.NAME,
535                     params);
536             
537             this.ruleService.saveRule(this.nodeRef, rule);
538                     
539             // Check rule does not get fired when a node without the aspect is added
540
NodeRef newNodeRef2 = this.nodeService.createNode(
541                     this.nodeRef,
542                     ContentModel.ASSOC_CHILDREN,
543                     QName.createQName(TEST_NAMESPACE, "noAspect"),
544                     ContentModel.TYPE_CONTENT,
545                     getContentProperties()).getChildRef();
546             addContentToNode(newNodeRef2);
547             assertFalse(this.nodeService.hasAspect(newNodeRef2, ContentModel.ASPECT_VERSIONABLE));
548             
549             // Check rule gets fired when node contains category value
550
UserTransaction JavaDoc tx = transactionService.getUserTransaction();
551             tx.begin();
552             NodeRef newNodeRef = this.nodeService.createNode(
553                     this.nodeRef,
554                     ContentModel.ASSOC_CHILDREN,
555                     QName.createQName(TEST_NAMESPACE, "hasAspectAndValue"),
556                     ContentModel.TYPE_CONTENT,
557                     getContentProperties()).getChildRef();
558             addContentToNode(newNodeRef);
559             Map JavaDoc<QName, Serializable JavaDoc> catProps = new HashMap JavaDoc<QName, Serializable JavaDoc>();
560             catProps.put(CAT_PROP_QNAME, this.catROne);
561             this.nodeService.addAspect(newNodeRef, this.regionCategorisationQName, catProps);
562             tx.commit();
563             assertTrue(this.nodeService.hasAspect(newNodeRef, ContentModel.ASPECT_VERSIONABLE));
564             
565             // Check rule does not get fired when the node has the incorrect category value
566
UserTransaction JavaDoc tx3 = transactionService.getUserTransaction();
567             tx3.begin();
568             NodeRef newNodeRef3 = this.nodeService.createNode(
569                     this.nodeRef,
570                     ContentModel.ASSOC_CHILDREN,
571                     QName.createQName(TEST_NAMESPACE, "hasAspectAndValue"),
572                     ContentModel.TYPE_CONTENT,
573                     getContentProperties()).getChildRef();
574             addContentToNode(newNodeRef3);
575             Map JavaDoc<QName, Serializable JavaDoc> catProps3 = new HashMap JavaDoc<QName, Serializable JavaDoc>();
576             catProps3.put(CAT_PROP_QNAME, this.catRTwo);
577             this.nodeService.addAspect(newNodeRef3, this.regionCategorisationQName, catProps3);
578             tx3.commit();
579             assertFalse(this.nodeService.hasAspect(newNodeRef3, ContentModel.ASPECT_VERSIONABLE));
580             
581             //System.out.println(NodeStoreInspector.dumpNodeStore(this.nodeService, this.testStoreRef));
582
}
583         catch (Exception JavaDoc exception)
584         {
585             throw new RuntimeException JavaDoc(exception);
586         }
587     }
588     
589     /**
590      * Test:
591      * rule type: inbound
592      * condition: no-condition
593      * action: link-category
594      */

595     public void testLinkCategoryAction()
596     {
597         // Create categories used in tests
598
createTestCategories();
599         
600         Map JavaDoc<String JavaDoc, Serializable JavaDoc> params = new HashMap JavaDoc<String JavaDoc, Serializable JavaDoc>(1);
601         params.put(LinkCategoryActionExecuter.PARAM_CATEGORY_ASPECT, this.regionCategorisationQName);
602         params.put(LinkCategoryActionExecuter.PARAM_CATEGORY_VALUE, this.catROne);
603         
604         Rule rule = createRule(
605                 RuleType.INBOUND,
606                 LinkCategoryActionExecuter.NAME,
607                 params,
608                 NoConditionEvaluator.NAME,
609                 null);
610         
611         this.ruleService.saveRule(this.nodeRef, rule);
612                 
613         NodeRef newNodeRef2 = this.nodeService.createNode(
614                 this.nodeRef,
615                 ContentModel.ASSOC_CHILDREN,
616                 QName.createQName(TEST_NAMESPACE, "noAspect"),
617                 ContentModel.TYPE_CONTENT,
618                 getContentProperties()).getChildRef();
619         addContentToNode(newNodeRef2);
620         
621         // Check that the category value has been set
622
NodeRef setValue = (NodeRef)this.nodeService.getProperty(newNodeRef2, CAT_PROP_QNAME);
623         assertNotNull(setValue);
624         assertEquals(this.catROne, setValue);
625 }
626         
627     
628     /**
629      * Test:
630      * rule type: inbound
631      * condition: no-condition
632      * action: mail
633      *
634      * Note: this test will be removed from the standard list since it is not currently automated
635      */

636     public void xtestMailAction()
637     {
638         this.nodeService.addAspect(this.nodeRef, ContentModel.ASPECT_LOCKABLE, null);
639         
640         Map JavaDoc<String JavaDoc, Serializable JavaDoc> params = new HashMap JavaDoc<String JavaDoc, Serializable JavaDoc>(1);
641         params.put(MailActionExecuter.PARAM_TO, "alfresco.test@gmail.com");
642         params.put(MailActionExecuter.PARAM_SUBJECT, "Unit test");
643         params.put(MailActionExecuter.PARAM_TEXT, "This is a test to check that the mail action is working.");
644         
645         Rule rule = createRule(
646                 RuleType.INBOUND,
647                 MailActionExecuter.NAME,
648                 params,
649                 NoConditionEvaluator.NAME,
650                 null);
651         
652         this.ruleService.saveRule(this.nodeRef, rule);
653                 
654         this.nodeService.createNode(
655                 this.nodeRef,
656                 ContentModel.ASSOC_CHILDREN,
657                 QName.createQName(TEST_NAMESPACE, "children"),
658                 ContentModel.TYPE_CONTENT,
659                 getContentProperties()).getChildRef();
660         
661         // An email should appear in the recipients email
662

663         // System.out.println(NodeStoreInspector.dumpNodeStore(this.nodeService, this.testStoreRef));
664
}
665     
666     /**
667      * Test:
668      * rule type: inbound
669      * condition: no-condition()
670      * action: copy()
671      */

672     public void testCopyAction()
673     {
674         Map JavaDoc<String JavaDoc, Serializable JavaDoc> params = new HashMap JavaDoc<String JavaDoc, Serializable JavaDoc>(1);
675         params.put(MoveActionExecuter.PARAM_DESTINATION_FOLDER, this.rootNodeRef);
676         params.put(MoveActionExecuter.PARAM_ASSOC_TYPE_QNAME, ContentModel.ASSOC_CHILDREN);
677         params.put(MoveActionExecuter.PARAM_ASSOC_QNAME, QName.createQName(TEST_NAMESPACE, "copy"));
678         
679         Rule rule = createRule(
680                 RuleType.INBOUND,
681                 CopyActionExecuter.NAME,
682                 params,
683                 NoConditionEvaluator.NAME,
684                 null);
685         
686         this.ruleService.saveRule(this.nodeRef, rule);
687
688         NodeRef newNodeRef = this.nodeService.createNode(
689                 this.nodeRef,
690                 ContentModel.ASSOC_CHILDREN,
691                 QName.createQName(TEST_NAMESPACE, "origional"),
692                 ContentModel.TYPE_CONTENT,
693                 getContentProperties()).getChildRef();
694         addContentToNode(newNodeRef);
695         
696         //System.out.println(NodeStoreInspector.dumpNodeStore(this.nodeService, this.testStoreRef));
697

698         // Check that the created node is still there
699
List JavaDoc<ChildAssociationRef> origRefs = this.nodeService.getChildAssocs(
700                 this.nodeRef,
701                 RegexQNamePattern.MATCH_ALL,
702                 QName.createQName(TEST_NAMESPACE, "origional"));
703         assertNotNull(origRefs);
704         assertEquals(1, origRefs.size());
705         NodeRef origNodeRef = origRefs.get(0).getChildRef();
706         assertEquals(newNodeRef, origNodeRef);
707
708         // Check that the created node has been copied
709
List JavaDoc<ChildAssociationRef> copyChildAssocRefs = this.nodeService.getChildAssocs(
710                                                     this.rootNodeRef,
711                                                     RegexQNamePattern.MATCH_ALL, QName.createQName(TEST_NAMESPACE, "copy"));
712         assertNotNull(copyChildAssocRefs);
713         
714         // **********************************
715
// NOTE: Changed expected result to get build running
716
// **********************************
717
assertEquals(1, copyChildAssocRefs.size());
718         
719         NodeRef copyNodeRef = copyChildAssocRefs.get(0).getChildRef();
720         assertTrue(this.nodeService.hasAspect(copyNodeRef, ContentModel.ASPECT_COPIEDFROM));
721         NodeRef source = (NodeRef)this.nodeService.getProperty(copyNodeRef, ContentModel.PROP_COPY_REFERENCE);
722         assertEquals(newNodeRef, source);
723         
724         // TODO test deep copy !!
725
}
726     
727     /**
728      * Test:
729      * rule type: inbound
730      * condition: no-condition()
731      * action: transform()
732      */

733     public void testTransformAction()
734     {
735         if (this.transformerRegistry.getTransformer(MimetypeMap.MIMETYPE_EXCEL, MimetypeMap.MIMETYPE_TEXT_PLAIN) != null)
736         {
737             try
738             {
739                 Map JavaDoc<String JavaDoc, Serializable JavaDoc> params = new HashMap JavaDoc<String JavaDoc, Serializable JavaDoc>(1);
740                 params.put(TransformActionExecuter.PARAM_MIME_TYPE, MimetypeMap.MIMETYPE_TEXT_PLAIN);
741                 params.put(TransformActionExecuter.PARAM_DESTINATION_FOLDER, this.rootNodeRef);
742                 params.put(TransformActionExecuter.PARAM_ASSOC_TYPE_QNAME, ContentModel.ASSOC_CHILDREN);
743                 params.put(TransformActionExecuter.PARAM_ASSOC_QNAME, QName.createQName(TEST_NAMESPACE, "transformed"));
744                 
745                 Rule rule = createRule(
746                         RuleType.INBOUND,
747                         TransformActionExecuter.NAME,
748                         params,
749                         NoConditionEvaluator.NAME,
750                         null);
751                 
752                 this.ruleService.saveRule(this.nodeRef, rule);
753         
754                 UserTransaction JavaDoc tx = transactionService.getUserTransaction();
755                 tx.begin();
756                 
757                 Map JavaDoc<QName, Serializable JavaDoc> props =new HashMap JavaDoc<QName, Serializable JavaDoc>(1);
758                 props.put(ContentModel.PROP_NAME, "test.xls");
759                 
760                 // Create the node at the root
761
NodeRef newNodeRef = this.nodeService.createNode(
762                         this.nodeRef,
763                         ContentModel.ASSOC_CHILDREN,
764                         QName.createQName(TEST_NAMESPACE, "origional"),
765                         ContentModel.TYPE_CONTENT,
766                         props).getChildRef();
767                 
768                 // Set some content on the origional
769
ContentWriter contentWriter = this.contentService.getWriter(newNodeRef, ContentModel.PROP_CONTENT, true);
770                 contentWriter.setMimetype(MimetypeMap.MIMETYPE_EXCEL);
771                 File JavaDoc testFile = AbstractContentTransformerTest.loadQuickTestFile("xls");
772                 contentWriter.putContent(testFile);
773                 
774                 tx.commit();
775                 
776                 //System.out.println(NodeStoreInspector.dumpNodeStore(this.nodeService, this.testStoreRef));
777

778                 AuthenticationComponent authenticationComponent = (AuthenticationComponent)applicationContext.getBean("authenticationComponent");
779                 authenticationComponent.setCurrentUser(authenticationComponent.getSystemUserName());
780                 
781                 // Check that the created node is still there
782
List JavaDoc<ChildAssociationRef> origRefs = this.nodeService.getChildAssocs(
783                         this.nodeRef,
784                         RegexQNamePattern.MATCH_ALL, QName.createQName(TEST_NAMESPACE, "origional"));
785                 assertNotNull(origRefs);
786                 assertEquals(1, origRefs.size());
787                 NodeRef origNodeRef = origRefs.get(0).getChildRef();
788                 assertEquals(newNodeRef, origNodeRef);
789         
790                 // Check that the created node has been copied
791
List JavaDoc<ChildAssociationRef> copyChildAssocRefs = this.nodeService.getChildAssocs(
792                                                             this.rootNodeRef,
793                                                             RegexQNamePattern.MATCH_ALL, QName.createQName(TEST_NAMESPACE, "transformed"));
794                 assertNotNull(copyChildAssocRefs);
795                 assertEquals(1, copyChildAssocRefs.size());
796                 NodeRef copyNodeRef = copyChildAssocRefs.get(0).getChildRef();
797                 assertTrue(this.nodeService.hasAspect(copyNodeRef, ContentModel.ASPECT_COPIEDFROM));
798                 NodeRef source = (NodeRef)this.nodeService.getProperty(copyNodeRef, ContentModel.PROP_COPY_REFERENCE);
799                 assertEquals(newNodeRef, source);
800                 
801                 // Check the transformed content
802
ContentData contentData = (ContentData) nodeService.getProperty(copyNodeRef, ContentModel.PROP_CONTENT);
803                 assertEquals(MimetypeMap.MIMETYPE_TEXT_PLAIN, contentData.getMimetype());
804                 
805             }
806             catch (Exception JavaDoc exception)
807             {
808                 throw new RuntimeException JavaDoc(exception);
809             }
810         }
811     }
812     
813     /**
814      * Test image transformation
815      *
816      */

817     public void testImageTransformAction()
818     {
819         if (this.transformerRegistry.getTransformer(MimetypeMap.MIMETYPE_IMAGE_GIF, MimetypeMap.MIMETYPE_IMAGE_JPEG) != null)
820         {
821             try
822             {
823                 Map JavaDoc<String JavaDoc, Serializable JavaDoc> params = new HashMap JavaDoc<String JavaDoc, Serializable JavaDoc>(1);
824                 params.put(ImageTransformActionExecuter.PARAM_DESTINATION_FOLDER, this.rootNodeRef);
825                 params.put(ImageTransformActionExecuter.PARAM_ASSOC_TYPE_QNAME, ContentModel.ASSOC_CHILDREN);
826                 params.put(TransformActionExecuter.PARAM_MIME_TYPE, MimetypeMap.MIMETYPE_IMAGE_JPEG);
827                 params.put(ImageTransformActionExecuter.PARAM_ASSOC_QNAME, QName.createQName(TEST_NAMESPACE, "transformed"));
828                 params.put(ImageTransformActionExecuter.PARAM_CONVERT_COMMAND, "-negate");
829                 
830                 Rule rule = createRule(
831                         RuleType.INBOUND,
832                         ImageTransformActionExecuter.NAME,
833                         params,
834                         NoConditionEvaluator.NAME,
835                         null);
836                 
837                 this.ruleService.saveRule(this.nodeRef, rule);
838         
839                 UserTransaction JavaDoc tx = transactionService.getUserTransaction();
840                 tx.begin();
841                 
842                 Map JavaDoc<QName, Serializable JavaDoc> props =new HashMap JavaDoc<QName, Serializable JavaDoc>(1);
843                 props.put(ContentModel.PROP_NAME, "test.gif");
844                 
845                 // Create the node at the root
846
NodeRef newNodeRef = this.nodeService.createNode(
847                         this.nodeRef,
848                         ContentModel.ASSOC_CHILDREN,
849                         QName.createQName(TEST_NAMESPACE, "origional"),
850                         ContentModel.TYPE_CONTENT,
851                         props).getChildRef();
852                 
853                 // Set some content on the origional
854
ContentWriter contentWriter = this.contentService.getWriter(newNodeRef, ContentModel.PROP_CONTENT, true);
855                 contentWriter.setMimetype(MimetypeMap.MIMETYPE_IMAGE_GIF);
856                 File JavaDoc testFile = AbstractContentTransformerTest.loadQuickTestFile("gif");
857                 contentWriter.putContent(testFile);
858                 
859                 tx.commit();
860                 
861                 //System.out.println(NodeStoreInspector.dumpNodeStore(this.nodeService, this.testStoreRef));
862

863                 // Check that the created node is still there
864
List JavaDoc<ChildAssociationRef> origRefs = this.nodeService.getChildAssocs(
865                         this.nodeRef,
866                         RegexQNamePattern.MATCH_ALL, QName.createQName(TEST_NAMESPACE, "origional"));
867                 assertNotNull(origRefs);
868                 assertEquals(1, origRefs.size());
869                 NodeRef origNodeRef = origRefs.get(0).getChildRef();
870                 assertEquals(newNodeRef, origNodeRef);
871         
872                 // Check that the created node has been copied
873
List JavaDoc<ChildAssociationRef> copyChildAssocRefs = this.nodeService.getChildAssocs(
874                                                             this.rootNodeRef,
875                                                             RegexQNamePattern.MATCH_ALL, QName.createQName(TEST_NAMESPACE, "transformed"));
876                 assertNotNull(copyChildAssocRefs);
877                 assertEquals(1, copyChildAssocRefs.size());
878                 NodeRef copyNodeRef = copyChildAssocRefs.get(0).getChildRef();
879                 assertTrue(this.nodeService.hasAspect(copyNodeRef, ContentModel.ASPECT_COPIEDFROM));
880                 NodeRef source = (NodeRef)this.nodeService.getProperty(copyNodeRef, ContentModel.PROP_COPY_REFERENCE);
881                 assertEquals(newNodeRef, source);
882             }
883             catch (Exception JavaDoc exception)
884             {
885                 throw new RuntimeException JavaDoc(exception);
886             }
887         }
888     }
889     
890     /**
891      * Test:
892      * rule type: inbound
893      * condition: no-condition()
894      * action: move()
895      */

896     public void testMoveAction()
897     {
898         Map JavaDoc<String JavaDoc, Serializable JavaDoc> params = new HashMap JavaDoc<String JavaDoc, Serializable JavaDoc>(1);
899         params.put(MoveActionExecuter.PARAM_DESTINATION_FOLDER, this.rootNodeRef);
900         params.put(MoveActionExecuter.PARAM_ASSOC_TYPE_QNAME, ContentModel.ASSOC_CHILDREN);
901         params.put(MoveActionExecuter.PARAM_ASSOC_QNAME, QName.createQName(TEST_NAMESPACE, "copy"));
902         
903         Rule rule = createRule(
904                 RuleType.INBOUND,
905                 MoveActionExecuter.NAME,
906                 params,
907                 NoConditionEvaluator.NAME,
908                 null);
909         
910         this.ruleService.saveRule(this.nodeRef, rule);
911                 
912         NodeRef newNodeRef = this.nodeService.createNode(
913                 this.nodeRef,
914                 ContentModel.ASSOC_CHILDREN,
915                 QName.createQName(TEST_NAMESPACE, "origional"),
916                 ContentModel.TYPE_CONTENT,
917                 getContentProperties()).getChildRef();
918         addContentToNode(newNodeRef);
919         
920         //System.out.println(NodeStoreInspector.dumpNodeStore(this.nodeService, this.testStoreRef));
921

922         // Check that the created node has been moved
923
List JavaDoc<ChildAssociationRef> origRefs = this.nodeService.getChildAssocs(
924                 this.nodeRef,
925                 RegexQNamePattern.MATCH_ALL, QName.createQName(TEST_NAMESPACE, "origional"));
926         assertNotNull(origRefs);
927         assertEquals(0, origRefs.size());
928
929         // Check that the created node is in the new location
930
List JavaDoc<ChildAssociationRef> copyChildAssocRefs = this.nodeService.getChildAssocs(
931                                                     this.rootNodeRef,
932                                                     RegexQNamePattern.MATCH_ALL, QName.createQName(TEST_NAMESPACE, "copy"));
933         assertNotNull(copyChildAssocRefs);
934         assertEquals(1, copyChildAssocRefs.size());
935         NodeRef movedNodeRef = copyChildAssocRefs.get(0).getChildRef();
936         assertEquals(newNodeRef, movedNodeRef);
937     }
938     
939     /**
940      * Test:
941      * rule type: inbound
942      * condition: no-condition()
943      * action: checkout()
944      */

945     public void testCheckOutAction()
946     {
947         Rule rule = createRule(
948                 RuleType.INBOUND,
949                 CheckOutActionExecuter.NAME,
950                 null,
951                 NoConditionEvaluator.NAME,
952                 null);
953         
954         this.ruleService.saveRule(this.nodeRef, rule);
955          
956         NodeRef newNodeRef = null;
957         UserTransaction JavaDoc tx = this.transactionService.getUserTransaction();
958         try
959         {
960             tx.begin();
961             
962             // Create a new node
963
newNodeRef = this.nodeService.createNode(
964                     this.nodeRef,
965                     ContentModel.ASSOC_CHILDREN,
966                     QName.createQName(TEST_NAMESPACE, "checkout"),
967                     ContentModel.TYPE_CONTENT,
968                     getContentProperties()).getChildRef();
969             addContentToNode(newNodeRef);
970             
971             tx.commit();
972         }
973         catch (Exception JavaDoc exception)
974         {
975             throw new RuntimeException JavaDoc(exception);
976         }
977         
978         //System.out.println(NodeStoreInspector.dumpNodeStore(this.nodeService, this.testStoreRef));
979

980         // Check that the new node has been checked out
981
List JavaDoc<ChildAssociationRef> children = this.nodeService.getChildAssocs(this.nodeRef);
982         assertNotNull(children);
983         assertEquals(3, children.size()); // includes rule folder
984
for (ChildAssociationRef child : children)
985         {
986             NodeRef childNodeRef = child.getChildRef();
987             if (childNodeRef.equals(newNodeRef) == true)
988             {
989                 // check that the node has been locked
990
LockStatus lockStatus = this.lockService.getLockStatus(childNodeRef);
991                 assertEquals(LockStatus.LOCK_OWNER, lockStatus);
992             }
993             else if (this.nodeService.hasAspect(childNodeRef, ContentModel.ASPECT_WORKING_COPY) == true)
994             {
995                 // assert that it is the working copy that relates to the origional node
996
NodeRef copiedFromNodeRef = (NodeRef)this.nodeService.getProperty(childNodeRef, ContentModel.PROP_COPY_REFERENCE);
997                 assertEquals(newNodeRef, copiedFromNodeRef);
998             }
999         }
1000    }
1001    
1002    /**
1003     * Test:
1004     * rule type: inbound
1005     * condition: no-condition()
1006     * action: checkin()
1007     */

1008    @SuppressWarnings JavaDoc("unchecked")
1009    public void testCheckInAction()
1010    {
1011        Map JavaDoc<String JavaDoc, Serializable JavaDoc> params = new HashMap JavaDoc<String JavaDoc, Serializable JavaDoc>(1);
1012        params.put(CheckInActionExecuter.PARAM_DESCRIPTION, "The version description.");
1013        
1014        Rule rule = createRule(
1015                RuleType.INBOUND,
1016                CheckInActionExecuter.NAME,
1017                params,
1018                NoConditionEvaluator.NAME,
1019                null);
1020        
1021        this.ruleService.saveRule(this.nodeRef, rule);
1022         
1023        List JavaDoc<NodeRef> list = TransactionUtil.executeInUserTransaction(
1024                this.transactionService,
1025                new TransactionUtil.TransactionWork<List JavaDoc<NodeRef>>()
1026                {
1027                    public List JavaDoc<NodeRef> doWork()
1028                    {
1029                        // Create a new node and check-it out
1030
NodeRef newNodeRef = RuleServiceCoverageTest.this.nodeService.createNode(
1031                                RuleServiceCoverageTest.this.rootNodeRef,
1032                                ContentModel.ASSOC_CHILDREN,
1033                                QName.createQName(TEST_NAMESPACE, "origional"),
1034                                ContentModel.TYPE_CONTENT,
1035                                getContentProperties()).getChildRef();
1036                        NodeRef workingCopy = RuleServiceCoverageTest.this.cociService.checkout(newNodeRef);
1037                        
1038                        // Move the working copy into the actionable folder
1039
RuleServiceCoverageTest.this.nodeService.moveNode(
1040                                workingCopy,
1041                                RuleServiceCoverageTest.this.nodeRef,
1042                                ContentModel.ASSOC_CHILDREN,
1043                                QName.createQName(TEST_NAMESPACE, "moved"));
1044                        
1045                        List JavaDoc<NodeRef> result = new ArrayList JavaDoc<NodeRef>();
1046                        result.add(newNodeRef);
1047                        result.add(workingCopy);
1048                        return result;
1049                    }
1050                    
1051                });
1052        
1053        // Check that the working copy has been removed
1054
assertFalse(this.nodeService.exists(list.get(1)));
1055        
1056        // Check that the origional is no longer locked
1057
assertEquals(LockStatus.NO_LOCK, this.lockService.getLockStatus(list.get(0)));
1058        
1059        //System.out.println(NodeStoreInspector.dumpNodeStore(this.nodeService, this.testStoreRef));
1060
}
1061    
1062    /**
1063     * Check that the rules can be enabled and disabled
1064     */

1065    public void testRulesDisabled()
1066    {
1067        Map JavaDoc<String JavaDoc, Serializable JavaDoc> actionParams = new HashMap JavaDoc<String JavaDoc, Serializable JavaDoc>(1);
1068        actionParams.put("aspect-name", ContentModel.ASPECT_VERSIONABLE);
1069        
1070        Rule rule = createRule(
1071                RuleType.INBOUND,
1072                AddFeaturesActionExecuter.NAME,
1073                actionParams,
1074                NoConditionEvaluator.NAME,
1075                null);
1076        
1077        this.ruleService.saveRule(this.nodeRef, rule);
1078        this.ruleService.disableRules(this.nodeRef);
1079        
1080        NodeRef newNodeRef = this.nodeService.createNode(
1081                this.nodeRef,
1082                ContentModel.ASSOC_CHILDREN,
1083                QName.createQName(TEST_NAMESPACE, "children"),
1084                ContentModel.TYPE_CONTENT,
1085                getContentProperties()).getChildRef();
1086        addContentToNode(newNodeRef);
1087        assertFalse(this.nodeService.hasAspect(newNodeRef, ContentModel.ASPECT_VERSIONABLE));
1088        
1089        this.ruleService.enableRules(this.nodeRef);
1090        
1091        NodeRef newNodeRef2 = this.nodeService.createNode(
1092                this.nodeRef,
1093                ContentModel.ASSOC_CHILDREN,
1094                QName.createQName(TEST_NAMESPACE, "children"),
1095                ContentModel.TYPE_CONTENT,
1096                getContentProperties()).getChildRef();
1097        addContentToNode(newNodeRef2);
1098        assertTrue(this.nodeService.hasAspect(newNodeRef2, ContentModel.ASPECT_VERSIONABLE));
1099    }
1100    
1101    /**
1102     * Adds content to a given node.
1103     * <p>
1104     * Used to trigger rules of type of incomming.
1105     *
1106     * @param nodeRef the node reference
1107     */

1108    private void addContentToNode(NodeRef nodeRef)
1109    {
1110        ContentWriter contentWriter = this.contentService.getWriter(nodeRef, ContentModel.PROP_CONTENT, true);
1111        contentWriter.setMimetype(MimetypeMap.MIMETYPE_TEXT_PLAIN);
1112        contentWriter.setEncoding("UTF-8");
1113        assertNotNull(contentWriter);
1114        contentWriter.putContent(STANDARD_TEXT_CONTENT + System.currentTimeMillis());
1115    }
1116    
1117    /**
1118     * Test checkMandatoryProperties method
1119     */

1120    public void testCheckMandatoryProperties()
1121    {
1122        Map JavaDoc<String JavaDoc, Serializable JavaDoc> actionParams = new HashMap JavaDoc<String JavaDoc, Serializable JavaDoc>(1);
1123        actionParams.put("aspect-name", ContentModel.ASPECT_VERSIONABLE);
1124        
1125        Map JavaDoc<String JavaDoc, Serializable JavaDoc> condParams = new HashMap JavaDoc<String JavaDoc, Serializable JavaDoc>(1);
1126        // should be setting the condition parameter here
1127

1128        Rule rule = createRule(
1129                RuleType.INBOUND,
1130                AddFeaturesActionExecuter.NAME,
1131                actionParams,
1132                ComparePropertyValueEvaluator.NAME,
1133                condParams);
1134        
1135        this.ruleService.saveRule(this.nodeRef, rule);
1136        
1137        try
1138        {
1139            // Try and create a node .. should fail since the rule is invalid
1140
Map JavaDoc<QName, Serializable JavaDoc> props2 = getContentProperties();
1141            props2.put(ContentModel.PROP_NAME, "bobbins.doc");
1142            NodeRef newNodeRef2 = this.nodeService.createNode(
1143                    this.nodeRef,
1144                    ContentModel.ASSOC_CHILDREN,
1145                    QName.createQName(TEST_NAMESPACE, "children"),
1146                    ContentModel.TYPE_CONTENT,
1147                    props2).getChildRef();
1148            addContentToNode(newNodeRef2);
1149            fail("An exception should have been thrown since a mandatory parameter was missing from the condition.");
1150        }
1151        catch (Throwable JavaDoc ruleServiceException)
1152        {
1153            // Success since we where expecting the exception
1154
}
1155    }
1156    
1157    /**
1158     * Test:
1159     * rule type: inbound
1160     * condition: match-text(
1161     * text = .doc,
1162     * operation = CONTAINS)
1163     * action: add-features(
1164     * aspect-name = versionable)
1165     */

1166    public void testContainsTextCondition()
1167    {
1168        Map JavaDoc<String JavaDoc, Serializable JavaDoc> actionParams = new HashMap JavaDoc<String JavaDoc, Serializable JavaDoc>(1);
1169        actionParams.put("aspect-name", ContentModel.ASPECT_VERSIONABLE);
1170        
1171        // ActionCondition parameter's
1172
Map JavaDoc<String JavaDoc, Serializable JavaDoc> condParams = new HashMap JavaDoc<String JavaDoc, Serializable JavaDoc>(1);
1173        condParams.put(ComparePropertyValueEvaluator.PARAM_VALUE, ".doc");
1174        
1175        Rule rule = createRule(
1176                RuleType.INBOUND,
1177                AddFeaturesActionExecuter.NAME,
1178                actionParams,
1179                ComparePropertyValueEvaluator.NAME,
1180                condParams);
1181        
1182        this.ruleService.saveRule(this.nodeRef, rule);
1183        
1184        // Test condition failure
1185
Map JavaDoc<QName, Serializable JavaDoc> props1 = new HashMap JavaDoc<QName, Serializable JavaDoc>();
1186        props1.put(ContentModel.PROP_NAME, "bobbins.txt");
1187       // props1.put(ContentModel.PROP_CONTENT, CONTENT_DATA_TEXT);
1188
NodeRef newNodeRef = this.nodeService.createNode(
1189                this.nodeRef,
1190                ContentModel.ASSOC_CHILDREN,
1191                QName.createQName(TEST_NAMESPACE, "children"),
1192                ContentModel.TYPE_CONTENT,
1193                props1).getChildRef();
1194        addContentToNode(newNodeRef);
1195        
1196        //Map<QName, Serializable> map = this.nodeService.getProperties(newNodeRef);
1197
//String value = (String)this.nodeService.getProperty(newNodeRef, ContentModel.PROP_NAME);
1198

1199        assertFalse(this.nodeService.hasAspect(newNodeRef, ContentModel.ASPECT_VERSIONABLE));
1200        
1201        // Test condition success
1202
Map JavaDoc<QName, Serializable JavaDoc> props2 = new HashMap JavaDoc<QName, Serializable JavaDoc>();
1203        props2.put(ContentModel.PROP_NAME, "bobbins.doc");
1204        //props2.put(ContentModel.PROP_CONTENT, CONTENT_DATA_TEXT);
1205
NodeRef newNodeRef2 = this.nodeService.createNode(
1206                this.nodeRef,
1207                ContentModel.ASSOC_CHILDREN,
1208                QName.createQName(TEST_NAMESPACE, "children"),
1209                ContentModel.TYPE_CONTENT,
1210                props2).getChildRef();
1211        addContentToNode(newNodeRef2);
1212        assertTrue(this.nodeService.hasAspect(
1213                newNodeRef2,
1214                ContentModel.ASPECT_VERSIONABLE));
1215        
1216        try
1217        {
1218            // Test name not set
1219
NodeRef newNodeRef3 = this.nodeService.createNode(
1220                    this.nodeRef,
1221                    ContentModel.ASSOC_CHILDREN,
1222                    QName.createQName(TEST_NAMESPACE, "children"),
1223                    ContentModel.TYPE_CONTENT,
1224                    getContentProperties()).getChildRef();
1225            addContentToNode(newNodeRef3);
1226        }
1227        catch (RuleServiceException exception)
1228        {
1229            // Correct since text-match is a mandatory property
1230
}
1231        
1232        // Test begins with
1233
Map JavaDoc<String JavaDoc, Serializable JavaDoc> condParamsBegins = new HashMap JavaDoc<String JavaDoc, Serializable JavaDoc>(1);
1234        condParamsBegins.put(ComparePropertyValueEvaluator.PARAM_VALUE, "bob*");
1235        rule.removeAllActionConditions();
1236        ActionCondition condition1 = this.actionService.createActionCondition(ComparePropertyValueEvaluator.NAME, condParamsBegins);
1237        rule.addActionCondition(condition1);
1238        this.ruleService.saveRule(this.nodeRef, rule);
1239        Map JavaDoc<QName, Serializable JavaDoc> propsx = new HashMap JavaDoc<QName, Serializable JavaDoc>();
1240        propsx.put(ContentModel.PROP_NAME, "mybobbins.doc");
1241        //propsx.put(ContentModel.PROP_CONTENT, CONTENT_DATA_TEXT);
1242
NodeRef newNodeRefx = this.nodeService.createNode(
1243                this.nodeRef,
1244                ContentModel.ASSOC_CHILDREN,
1245                QName.createQName(TEST_NAMESPACE, "children"),
1246                ContentModel.TYPE_CONTENT,
1247                propsx).getChildRef();
1248        addContentToNode(newNodeRefx);
1249        assertFalse(this.nodeService.hasAspect(newNodeRefx, ContentModel.ASPECT_VERSIONABLE));
1250        Map JavaDoc<QName, Serializable JavaDoc> propsy = new HashMap JavaDoc<QName, Serializable JavaDoc>();
1251        propsy.put(ContentModel.PROP_NAME, "bobbins.doc");
1252        //propsy.put(ContentModel.PROP_CONTENT, CONTENT_DATA_TEXT);
1253
NodeRef newNodeRefy = this.nodeService.createNode(
1254                this.nodeRef,
1255                ContentModel.ASSOC_CHILDREN,
1256                QName.createQName(TEST_NAMESPACE, "children"),
1257                ContentModel.TYPE_CONTENT,
1258                propsy).getChildRef();
1259        addContentToNode(newNodeRefy);
1260        assertTrue(this.nodeService.hasAspect(
1261                newNodeRefy,
1262                ContentModel.ASPECT_VERSIONABLE));
1263        
1264        // Test ends with
1265
Map JavaDoc<String JavaDoc, Serializable JavaDoc> condParamsEnds = new HashMap JavaDoc<String JavaDoc, Serializable JavaDoc>(1);
1266        condParamsEnds.put(ComparePropertyValueEvaluator.PARAM_VALUE, "*s.doc");
1267        rule.removeAllActionConditions();
1268        ActionCondition condition2 = this.actionService.createActionCondition(ComparePropertyValueEvaluator.NAME, condParamsEnds);
1269        rule.addActionCondition(condition2);
1270        this.ruleService.saveRule(this.nodeRef, rule);
1271        Map JavaDoc<QName, Serializable JavaDoc> propsa = new HashMap JavaDoc<QName, Serializable JavaDoc>();
1272        propsa.put(ContentModel.PROP_NAME, "bobbins.document");
1273       // propsa.put(ContentModel.PROP_CONTENT, CONTENT_DATA_TEXT);
1274
NodeRef newNodeRefa = this.nodeService.createNode(
1275                this.nodeRef,
1276                ContentModel.ASSOC_CHILDREN,
1277                QName.createQName(TEST_NAMESPACE, "children"),
1278                ContentModel.TYPE_CONTENT,
1279                propsa).getChildRef();
1280        addContentToNode(newNodeRefa);
1281        assertFalse(this.nodeService.hasAspect(newNodeRefa, ContentModel.ASPECT_VERSIONABLE));
1282        Map JavaDoc<QName, Serializable JavaDoc> propsb = new HashMap JavaDoc<QName, Serializable JavaDoc>();
1283        propsb.put(ContentModel.PROP_NAME, "bobbins.doc");
1284        //propsb.put(ContentModel.PROP_CONTENT, CONTENT_DATA_TEXT);
1285
NodeRef newNodeRefb = this.nodeService.createNode(
1286                this.nodeRef,
1287                ContentModel.ASSOC_CHILDREN,
1288                QName.createQName(TEST_NAMESPACE, "children"),
1289                ContentModel.TYPE_CONTENT,
1290                propsb).getChildRef();
1291        addContentToNode(newNodeRefb);
1292        assertTrue(this.nodeService.hasAspect(
1293                newNodeRefb,
1294                ContentModel.ASPECT_VERSIONABLE));
1295    }
1296    
1297    /**
1298     * Test:
1299     * rule type: outbound
1300     * condition: no-condition()
1301     * action: add-features(
1302     * aspect-name = versionable)
1303     */

1304    public void testOutboundRuleType()
1305    {
1306        this.nodeService.addAspect(this.nodeRef, ContentModel.ASPECT_LOCKABLE, null);
1307        
1308        Map JavaDoc<String JavaDoc, Serializable JavaDoc> params = new HashMap JavaDoc<String JavaDoc, Serializable JavaDoc>(1);
1309        params.put("aspect-name", ContentModel.ASPECT_VERSIONABLE);
1310        
1311        Rule rule = createRule(
1312                "outbound",
1313                AddFeaturesActionExecuter.NAME,
1314                params,
1315                NoConditionEvaluator.NAME,
1316                null);
1317        
1318        this.ruleService.saveRule(this.nodeRef, rule);
1319        
1320        // Create a node
1321
NodeRef newNodeRef = this.nodeService.createNode(
1322                this.nodeRef,
1323                ContentModel.ASSOC_CHILDREN,
1324                QName.createQName(TEST_NAMESPACE, "children"),
1325                ContentModel.TYPE_CONTAINER).getChildRef();
1326        assertFalse(this.nodeService.hasAspect(newNodeRef, ContentModel.ASPECT_VERSIONABLE));
1327        
1328        // Move the node out of the actionable folder
1329
this.nodeService.moveNode(
1330                newNodeRef,
1331                this.rootNodeRef,
1332                ContentModel.ASSOC_CHILDREN,
1333                QName.createQName(TEST_NAMESPACE, "children"));
1334        assertTrue(this.nodeService.hasAspect(newNodeRef, ContentModel.ASPECT_VERSIONABLE));
1335        
1336        // Check the deletion of a node
1337

1338        //System.out.println(NodeStoreInspector.dumpNodeStore(this.nodeService, this.testStoreRef));
1339
NodeRef newNodeRef2 = this.nodeService.createNode(
1340                this.nodeRef,
1341                ContentModel.ASSOC_CHILDREN,
1342                QName.createQName(TEST_NAMESPACE, "children"),
1343                ContentModel.TYPE_CONTAINER).getChildRef();
1344        this.nodeService.deleteNode(newNodeRef2);
1345    }
1346    
1347    /**
1348     * Performance guideline test
1349     *
1350     */

1351    public void xtestPerformanceOfRuleExecution()
1352    {
1353        try
1354        {
1355            StopWatch sw = new StopWatch();
1356            
1357            // Create actionable nodes
1358
sw.start("create nodes with no rule executed");
1359            UserTransaction JavaDoc userTransaction1 = this.transactionService.getUserTransaction();
1360            userTransaction1.begin();
1361            
1362            for (int i = 0; i < 100; i++)
1363            {
1364                this.nodeService.createNode(
1365                        this.nodeRef,
1366                        ContentModel.ASSOC_CONTAINS,
1367                        ContentModel.ASSOC_CONTAINS,
1368                        ContentModel.TYPE_CONTAINER).getChildRef();
1369                assertFalse(this.nodeService.hasAspect(nodeRef, ContentModel.ASPECT_VERSIONABLE));
1370            }
1371                
1372            userTransaction1.commit();
1373            sw.stop();
1374            
1375            Map JavaDoc<String JavaDoc, Serializable JavaDoc> params = new HashMap JavaDoc<String JavaDoc, Serializable JavaDoc>(1);
1376            params.put("aspect-name", ContentModel.ASPECT_VERSIONABLE);
1377            
1378            Rule rule = createRule(
1379                    RuleType.INBOUND,
1380                    AddFeaturesActionExecuter.NAME,
1381                    params,
1382                    NoConditionEvaluator.NAME,
1383                    null);
1384            
1385            this.ruleService.saveRule(this.nodeRef, rule);
1386            
1387            sw.start("create nodes with one rule run (apply versionable aspect)");
1388            UserTransaction JavaDoc userTransaction2 = this.transactionService.getUserTransaction();
1389            userTransaction2.begin();
1390            
1391            NodeRef[] nodeRefs = new NodeRef[100];
1392            for (int i = 0; i < 100; i++)
1393            {
1394                NodeRef nodeRef = this.nodeService.createNode(
1395                        this.nodeRef,
1396                        ContentModel.ASSOC_CHILDREN,
1397                        QName.createQName(TEST_NAMESPACE, "children"),
1398                        ContentModel.TYPE_CONTAINER).getChildRef();
1399                addContentToNode(nodeRef);
1400                nodeRefs[i] = nodeRef;
1401                
1402                // Check that the versionable aspect has not yet been applied
1403
assertFalse(this.nodeService.hasAspect(nodeRef, ContentModel.ASPECT_VERSIONABLE));
1404            }
1405            
1406            userTransaction2.commit();
1407            sw.stop();
1408            
1409            // Check that the versionable aspect has been applied to all the created nodes
1410
for (NodeRef ref : nodeRefs)
1411            {
1412                assertTrue(this.nodeService.hasAspect(ref, ContentModel.ASPECT_VERSIONABLE));
1413            }
1414            
1415            System.out.println(sw.prettyPrint());
1416        }
1417        catch (Exception JavaDoc exception)
1418        {
1419            throw new RuntimeException JavaDoc(exception);
1420        }
1421    }
1422}
1423
Popular Tags