KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > repo > action > ActionServiceImplTest


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.action;
18
19 import java.io.Serializable JavaDoc;
20 import java.util.HashMap JavaDoc;
21 import java.util.List JavaDoc;
22 import java.util.Map JavaDoc;
23
24 import org.alfresco.model.ContentModel;
25 import org.alfresco.repo.action.evaluator.ComparePropertyValueEvaluator;
26 import org.alfresco.repo.action.evaluator.InCategoryEvaluator;
27 import org.alfresco.repo.action.evaluator.NoConditionEvaluator;
28 import org.alfresco.repo.action.evaluator.compare.ComparePropertyValueOperation;
29 import org.alfresco.repo.action.executer.AddFeaturesActionExecuter;
30 import org.alfresco.repo.action.executer.CheckInActionExecuter;
31 import org.alfresco.repo.action.executer.CheckOutActionExecuter;
32 import org.alfresco.repo.action.executer.CompositeActionExecuter;
33 import org.alfresco.repo.action.executer.MoveActionExecuter;
34 import org.alfresco.repo.content.MimetypeMap;
35 import org.alfresco.repo.transaction.TransactionUtil;
36 import org.alfresco.service.cmr.action.Action;
37 import org.alfresco.service.cmr.action.ActionCondition;
38 import org.alfresco.service.cmr.action.ActionConditionDefinition;
39 import org.alfresco.service.cmr.action.ActionDefinition;
40 import org.alfresco.service.cmr.action.CompositeAction;
41 import org.alfresco.service.cmr.repository.ContentData;
42 import org.alfresco.service.cmr.repository.NodeRef;
43 import org.alfresco.service.cmr.repository.NodeService;
44 import org.alfresco.service.namespace.QName;
45 import org.alfresco.service.transaction.TransactionService;
46 import org.alfresco.util.BaseAlfrescoSpringTest;
47
48 /**
49  * Action service test
50  *
51  * @author Roy Wetherall
52  */

53 public class ActionServiceImplTest extends BaseAlfrescoSpringTest
54 {
55     private static final String JavaDoc BAD_NAME = "badName";
56     
57     private NodeRef nodeRef;
58     
59     @Override JavaDoc
60     protected void onSetUpInTransaction() throws Exception JavaDoc
61     {
62         super.onSetUpInTransaction();
63
64         // Create the node used for tests
65
this.nodeRef = this.nodeService.createNode(
66                 this.rootNodeRef,
67                 ContentModel.ASSOC_CHILDREN,
68                 QName.createQName("{test}testnode"),
69                 ContentModel.TYPE_CONTENT).getChildRef();
70         this.nodeService.setProperty(
71                 this.nodeRef,
72                 ContentModel.PROP_CONTENT,
73                 new ContentData(null, MimetypeMap.MIMETYPE_TEXT_PLAIN, 0L, null));
74     }
75     
76     /**
77      * Test getActionDefinition
78      */

79     public void testGetActionDefinition()
80     {
81         ActionDefinition action = actionService.getActionDefinition(AddFeaturesActionExecuter.NAME);
82         assertNotNull(action);
83         assertEquals(AddFeaturesActionExecuter.NAME, action.getName());
84         
85         ActionConditionDefinition nullCondition = this.actionService.getActionConditionDefinition(BAD_NAME);
86         assertNull(nullCondition);
87     }
88     
89     /**
90      * Test getActionDefintions
91      */

92     public void testGetActionDefinitions()
93     {
94         List JavaDoc<ActionDefinition> defintions = this.actionService.getActionDefinitions();
95         assertNotNull(defintions);
96         assertFalse(defintions.isEmpty());
97         
98         for (ActionDefinition definition : defintions)
99         {
100             System.out.println(definition.getTitle());
101         }
102     }
103
104     /**
105      * Test getActionConditionDefinition
106      */

107     public void testGetActionConditionDefinition()
108     {
109         ActionConditionDefinition condition = this.actionService.getActionConditionDefinition(NoConditionEvaluator.NAME);
110         assertNotNull(condition);
111         assertEquals(NoConditionEvaluator.NAME, condition.getName());
112         
113         ActionConditionDefinition nullCondition = this.actionService.getActionConditionDefinition(BAD_NAME);
114         assertNull(nullCondition);
115     }
116
117     /**
118      * Test getActionConditionDefinitions
119      *
120      */

121     public void testGetActionConditionDefinitions()
122     {
123         List JavaDoc<ActionConditionDefinition> defintions = this.actionService.getActionConditionDefinitions();
124         assertNotNull(defintions);
125         assertFalse(defintions.isEmpty());
126         
127         for (ActionConditionDefinition definition : defintions)
128         {
129             System.out.println(definition.getTitle());
130         }
131     }
132
133     /**
134      * Test create action condition
135      */

136     public void testCreateActionCondition()
137     {
138         ActionCondition condition = this.actionService.createActionCondition(NoConditionEvaluator.NAME);
139         assertNotNull(condition);
140         assertEquals(NoConditionEvaluator.NAME, condition.getActionConditionDefinitionName());
141     }
142
143     /**
144      * Test createAction
145      */

146     public void testCreateAction()
147     {
148         Action action = this.actionService.createAction(AddFeaturesActionExecuter.NAME);
149         assertNotNull(action);
150         assertEquals(AddFeaturesActionExecuter.NAME, action.getActionDefinitionName());
151     }
152
153     /**
154      * Test createCompositeAction
155      */

156     public void testCreateCompositeAction()
157     {
158         CompositeAction action = this.actionService.createCompositeAction();
159         assertNotNull(action);
160         assertEquals(CompositeActionExecuter.NAME, action.getActionDefinitionName());
161     }
162
163     /**
164      * Evaluate action
165      */

166     public void testEvaluateAction()
167     {
168         Action action = this.actionService.createAction(AddFeaturesActionExecuter.NAME);
169         assertTrue(this.actionService.evaluateAction(action, this.nodeRef));
170         
171         ActionCondition condition = this.actionService.createActionCondition(ComparePropertyValueEvaluator.NAME);
172         condition.setParameterValue(ComparePropertyValueEvaluator.PARAM_VALUE, "*.doc");
173         action.addActionCondition(condition);
174         
175         assertFalse(this.actionService.evaluateAction(action, this.nodeRef));
176         this.nodeService.setProperty(this.nodeRef, ContentModel.PROP_NAME, "myDocument.doc");
177         assertTrue(this.actionService.evaluateAction(action, this.nodeRef));
178         
179         ActionCondition condition2 = this.actionService.createActionCondition(ComparePropertyValueEvaluator.NAME);
180         condition2.setParameterValue(ComparePropertyValueEvaluator.PARAM_VALUE, "my");
181         action.addActionCondition(condition2);
182         assertTrue(this.actionService.evaluateAction(action, this.nodeRef));
183         
184         this.nodeService.setProperty(this.nodeRef, ContentModel.PROP_NAME, "document.doc");
185         assertFalse(this.actionService.evaluateAction(action, this.nodeRef));
186     }
187     
188     /**
189      * Test evaluate action condition
190      */

191     public void testEvaluateActionCondition()
192     {
193         ActionCondition condition = this.actionService.createActionCondition(ComparePropertyValueEvaluator.NAME);
194         condition.setParameterValue(ComparePropertyValueEvaluator.PARAM_VALUE, "*.doc");
195         
196         assertFalse(this.actionService.evaluateActionCondition(condition, this.nodeRef));
197         this.nodeService.setProperty(this.nodeRef, ContentModel.PROP_NAME, "myDocument.doc");
198         assertTrue(this.actionService.evaluateActionCondition(condition, this.nodeRef));
199         
200         // Check that inverting the condition has the correct effect
201
condition.setInvertCondition(true);
202         assertFalse(this.actionService.evaluateActionCondition(condition, this.nodeRef));
203     }
204     
205     /**
206      * Test execute action
207      */

208     public void testExecuteAction()
209     {
210         assertFalse(this.nodeService.hasAspect(this.nodeRef, ContentModel.ASPECT_VERSIONABLE));
211         
212         Action action = this.actionService.createAction(AddFeaturesActionExecuter.NAME);
213         action.setParameterValue(AddFeaturesActionExecuter.PARAM_ASPECT_NAME, ContentModel.ASPECT_VERSIONABLE);
214         
215         this.actionService.executeAction(action, this.nodeRef);
216         assertTrue(this.nodeService.hasAspect(this.nodeRef, ContentModel.ASPECT_VERSIONABLE));
217         
218         this.nodeService.removeAspect(this.nodeRef, ContentModel.ASPECT_VERSIONABLE);
219         assertFalse(this.nodeService.hasAspect(this.nodeRef, ContentModel.ASPECT_VERSIONABLE));
220         
221         ActionCondition condition = this.actionService.createActionCondition(ComparePropertyValueEvaluator.NAME);
222         condition.setParameterValue(ComparePropertyValueEvaluator.PARAM_VALUE, "*.doc");
223         action.addActionCondition(condition);
224                 
225         this.actionService.executeAction(action, this.nodeRef);
226         assertFalse(this.nodeService.hasAspect(this.nodeRef, ContentModel.ASPECT_VERSIONABLE));
227         
228         this.actionService.executeAction(action, this.nodeRef, true);
229         assertFalse(this.nodeService.hasAspect(this.nodeRef, ContentModel.ASPECT_VERSIONABLE));
230         
231         this.actionService.executeAction(action, this.nodeRef, false);
232         assertTrue(this.nodeService.hasAspect(this.nodeRef, ContentModel.ASPECT_VERSIONABLE));
233         
234         this.nodeService.removeAspect(this.nodeRef, ContentModel.ASPECT_VERSIONABLE);
235         assertFalse(this.nodeService.hasAspect(this.nodeRef, ContentModel.ASPECT_VERSIONABLE));
236         
237         this.nodeService.setProperty(this.nodeRef, ContentModel.PROP_NAME, "myDocument.doc");
238         this.actionService.executeAction(action, this.nodeRef);
239         assertTrue(this.nodeService.hasAspect(this.nodeRef, ContentModel.ASPECT_VERSIONABLE));
240         
241         this.nodeService.removeAspect(this.nodeRef, ContentModel.ASPECT_VERSIONABLE);
242         assertFalse(this.nodeService.hasAspect(this.nodeRef, ContentModel.ASPECT_VERSIONABLE));
243         
244         this.nodeService.removeAspect(this.nodeRef, ContentModel.ASPECT_VERSIONABLE);
245         assertFalse(this.nodeService.hasAspect(this.nodeRef, ContentModel.ASPECT_VERSIONABLE));
246         
247         // Create the composite action
248
Action action1 = this.actionService.createAction(AddFeaturesActionExecuter.NAME);
249         action1.setParameterValue(AddFeaturesActionExecuter.PARAM_ASPECT_NAME, ContentModel.ASPECT_LOCKABLE);
250         Action action2 = this.actionService.createAction(AddFeaturesActionExecuter.NAME);
251         action2.setParameterValue(AddFeaturesActionExecuter.PARAM_ASPECT_NAME, ContentModel.ASPECT_VERSIONABLE);
252         CompositeAction compAction = this.actionService.createCompositeAction();
253         compAction.setTitle("title");
254         compAction.setDescription("description");
255         compAction.addAction(action1);
256         compAction.addAction(action2);
257         
258         // Execute the composite action
259
this.actionService.executeAction(compAction, this.nodeRef);
260         
261         assertTrue(this.nodeService.hasAspect(this.nodeRef, ContentModel.ASPECT_LOCKABLE));
262         assertTrue(this.nodeService.hasAspect(this.nodeRef, ContentModel.ASPECT_VERSIONABLE));
263     }
264     
265     public void testGetAndGetAllWithNoActions()
266     {
267         assertNull(this.actionService.getAction(this.nodeRef, AddFeaturesActionExecuter.NAME));
268         List JavaDoc<Action> actions = this.actionService.getActions(this.nodeRef);
269         assertNotNull(actions);
270         assertEquals(0, actions.size());
271     }
272     
273     /**
274      * Test saving an action with no conditions. Includes testing storage and retrieval
275      * of compensating actions.
276      */

277     public void testSaveActionNoCondition()
278     {
279         // Create the action
280
Action action = this.actionService.createAction(AddFeaturesActionExecuter.NAME);
281         String JavaDoc actionId = action.getId();
282         
283         // Set the parameters of the action
284
action.setParameterValue(AddFeaturesActionExecuter.PARAM_ASPECT_NAME, ContentModel.ASPECT_VERSIONABLE);
285         
286         // Set the title and description of the action
287
action.setTitle("title");
288         action.setDescription("description");
289         action.setExecuteAsynchronously(true);
290                 
291         // Save the action
292
this.actionService.saveAction(this.nodeRef, action);
293         
294         // Get the action
295
Action savedAction = this.actionService.getAction(this.nodeRef, actionId);
296         
297         // Check the action
298
assertEquals(action.getId(), savedAction.getId());
299         assertEquals(action.getActionDefinitionName(), savedAction.getActionDefinitionName());
300         
301         // Check the properties
302
assertEquals("title", savedAction.getTitle());
303         assertEquals("description", savedAction.getDescription());
304         assertTrue(savedAction.getExecuteAsychronously());
305         
306         // Check that the compensating action has not been set
307
assertNull(savedAction.getCompensatingAction());
308         
309         // Check the properties
310
assertEquals(1, savedAction.getParameterValues().size());
311         assertEquals(ContentModel.ASPECT_VERSIONABLE, savedAction.getParameterValue(AddFeaturesActionExecuter.PARAM_ASPECT_NAME));
312                 
313         // Check the conditions
314
assertNotNull(savedAction.getActionConditions());
315         assertEquals(0, savedAction.getActionConditions().size());
316         
317         // Edit the properties of the action
318
Map JavaDoc<QName, Serializable JavaDoc> properties = new HashMap JavaDoc<QName, Serializable JavaDoc>(1);
319         properties.put(ContentModel.PROP_NAME, "testName");
320         action.setParameterValue(AddFeaturesActionExecuter.PARAM_ASPECT_PROPERTIES, (Serializable JavaDoc)properties);
321         action.setParameterValue(AddFeaturesActionExecuter.PARAM_ASPECT_NAME, ContentModel.ASPECT_AUDITABLE);
322         
323         // Set the compensating action
324
Action compensatingAction = this.actionService.createAction(AddFeaturesActionExecuter.NAME);
325         compensatingAction.setParameterValue(AddFeaturesActionExecuter.PARAM_ASPECT_NAME, ContentModel.ASPECT_VERSIONABLE);
326         action.setCompensatingAction(compensatingAction);
327         
328         this.actionService.saveAction(this.nodeRef, action);
329         Action savedAction2 = this.actionService.getAction(this.nodeRef, actionId);
330         
331         // Check the updated properties
332
assertEquals(2, savedAction2.getParameterValues().size());
333         assertEquals(ContentModel.ASPECT_AUDITABLE, savedAction2.getParameterValue(AddFeaturesActionExecuter.PARAM_ASPECT_NAME));
334         Map JavaDoc<QName, Serializable JavaDoc> temp = (Map JavaDoc<QName, Serializable JavaDoc>)savedAction2.getParameterValue(AddFeaturesActionExecuter.PARAM_ASPECT_PROPERTIES);
335         assertNotNull(temp);
336         assertEquals(1, temp.size());
337         assertEquals("testName", temp.get(ContentModel.PROP_NAME));
338         
339         // Check the compensating action
340
Action savedCompensatingAction = savedAction2.getCompensatingAction();
341         assertNotNull(savedCompensatingAction);
342         assertEquals(compensatingAction, savedCompensatingAction);
343         assertEquals(AddFeaturesActionExecuter.NAME, savedCompensatingAction.getActionDefinitionName());
344         assertEquals(ContentModel.ASPECT_VERSIONABLE, savedCompensatingAction.getParameterValue(AddFeaturesActionExecuter.PARAM_ASPECT_NAME));
345         
346         // Change the details of the compensating action (edit and remove)
347
compensatingAction.setParameterValue(AddFeaturesActionExecuter.PARAM_ASPECT_NAME, ContentModel.ASPECT_CLASSIFIABLE);
348         this.actionService.saveAction(this.nodeRef, action);
349         Action savedAction3 = this.actionService.getAction(this.nodeRef, actionId);
350         Action savedCompensatingAction2 = savedAction3.getCompensatingAction();
351         assertNotNull(savedCompensatingAction2);
352         assertEquals(compensatingAction, savedCompensatingAction2);
353         assertEquals(AddFeaturesActionExecuter.NAME, savedCompensatingAction2.getActionDefinitionName());
354         assertEquals(ContentModel.ASPECT_CLASSIFIABLE, savedCompensatingAction2.getParameterValue(AddFeaturesActionExecuter.PARAM_ASPECT_NAME));
355         action.setCompensatingAction(null);
356         this.actionService.saveAction(this.nodeRef, action);
357         Action savedAction4 = this.actionService.getAction(this.nodeRef, actionId);
358         assertNull(savedAction4.getCompensatingAction());
359         
360         //System.out.println(NodeStoreInspector.dumpNodeStore(this.nodeService, this.testStoreRef));
361
}
362
363     public void testOwningNodeRef()
364     {
365         // Create the action
366
Action action = this.actionService.createAction(AddFeaturesActionExecuter.NAME);
367         String JavaDoc actionId = action.getId();
368         
369         // Set the parameters of the action
370
action.setParameterValue(AddFeaturesActionExecuter.PARAM_ASPECT_NAME, ContentModel.ASPECT_VERSIONABLE);
371         
372         // Set the title and description of the action
373
action.setTitle("title");
374         action.setDescription("description");
375         action.setExecuteAsynchronously(true);
376         
377         // Check the owning node ref
378
assertNull(action.getOwningNodeRef());
379                 
380         // Save the action
381
this.actionService.saveAction(this.nodeRef, action);
382         
383         // Check the owning node ref
384
assertEquals(this.nodeRef, action.getOwningNodeRef());
385         
386         // Get the action
387
Action savedAction = this.actionService.getAction(this.nodeRef, actionId);
388         
389         // Check the owning node ref
390
assertEquals(this.nodeRef, savedAction.getOwningNodeRef());;
391     }
392
393     /**
394      * Test saving an action with conditions
395      */

396     public void testSaveActionWithConditions()
397     {
398         // Create the action
399
Action action = this.actionService.createAction(AddFeaturesActionExecuter.NAME);
400         String JavaDoc actionId = action.getId();
401         
402         // Set the parameters of the action
403
action.setParameterValue(AddFeaturesActionExecuter.PARAM_ASPECT_NAME, ContentModel.ASPECT_VERSIONABLE);
404         Map JavaDoc<QName, Serializable JavaDoc> properties = new HashMap JavaDoc<QName, Serializable JavaDoc>(1);
405         properties.put(ContentModel.PROP_NAME, "testName");
406         action.setParameterValue(AddFeaturesActionExecuter.PARAM_ASPECT_PROPERTIES, (Serializable JavaDoc)properties);
407         
408         // Set the conditions of the action
409
ActionCondition actionCondition = this.actionService.createActionCondition(NoConditionEvaluator.NAME);
410         actionCondition.setInvertCondition(true);
411         ActionCondition actionCondition2 = this.actionService.createActionCondition(ComparePropertyValueEvaluator.NAME);
412         actionCondition2.setParameterValue(ComparePropertyValueEvaluator.PARAM_VALUE, "*.doc");
413         action.addActionCondition(actionCondition);
414         action.addActionCondition(actionCondition2);
415         
416         // Save the action
417
this.actionService.saveAction(this.nodeRef, action);
418         
419         // Get the action
420
Action savedAction = this.actionService.getAction(this.nodeRef, actionId);
421         
422         // Check the action
423
assertEquals(action.getId(), savedAction.getId());
424         assertEquals(action.getActionDefinitionName(), savedAction.getActionDefinitionName());
425         
426         // Check the properties
427
assertEquals(action.getParameterValues().size(), savedAction.getParameterValues().size());
428         assertEquals(ContentModel.ASPECT_VERSIONABLE, savedAction.getParameterValue(AddFeaturesActionExecuter.PARAM_ASPECT_NAME));
429         Map JavaDoc<QName, Serializable JavaDoc> temp = (Map JavaDoc<QName, Serializable JavaDoc>)savedAction.getParameterValue(AddFeaturesActionExecuter.PARAM_ASPECT_PROPERTIES);
430         assertNotNull(temp);
431         assertEquals(1, temp.size());
432         assertEquals("testName", temp.get(ContentModel.PROP_NAME));
433         
434         // Check the conditions
435
assertNotNull(savedAction.getActionConditions());
436         assertEquals(2, savedAction.getActionConditions().size());
437         for (ActionCondition savedCondition : savedAction.getActionConditions())
438         {
439             if (savedCondition.getActionConditionDefinitionName().equals(NoConditionEvaluator.NAME) == true)
440             {
441                 assertEquals(0, savedCondition.getParameterValues().size());
442                 assertTrue(savedCondition.getInvertCondition());
443             }
444             else if (savedCondition.getActionConditionDefinitionName().equals(ComparePropertyValueEvaluator.NAME) == true)
445             {
446                 assertEquals(1, savedCondition.getParameterValues().size());
447                 assertEquals("*.doc", savedCondition.getParameterValue(ComparePropertyValueEvaluator.PARAM_VALUE));
448                 assertFalse(savedCondition.getInvertCondition());
449             }
450             else
451             {
452                 fail("There is a condition here that we are not expecting.");
453             }
454         }
455         
456         // Modify the conditions of the action
457
ActionCondition actionCondition3 = this.actionService.createActionCondition(InCategoryEvaluator.NAME);
458         actionCondition3.setParameterValue(InCategoryEvaluator.PARAM_CATEGORY_ASPECT, ContentModel.ASPECT_OWNABLE);
459         action.addActionCondition(actionCondition3);
460         action.removeActionCondition(actionCondition);
461         actionCondition2.setParameterValue(ComparePropertyValueEvaluator.PARAM_VALUE, "*.exe");
462         actionCondition2.setParameterValue(ComparePropertyValueEvaluator.PARAM_OPERATION, ComparePropertyValueOperation.EQUALS);
463         
464         this.actionService.saveAction(this.nodeRef, action);
465         Action savedAction2 = this.actionService.getAction(this.nodeRef, actionId);
466         
467         // Check that the conditions have been updated correctly
468
assertNotNull(savedAction2.getActionConditions());
469         assertEquals(2, savedAction2.getActionConditions().size());
470         for (ActionCondition savedCondition : savedAction2.getActionConditions())
471         {
472             if (savedCondition.getActionConditionDefinitionName().equals(InCategoryEvaluator.NAME) == true)
473             {
474                 assertEquals(1, savedCondition.getParameterValues().size());
475                 assertEquals(ContentModel.ASPECT_OWNABLE, savedCondition.getParameterValue(InCategoryEvaluator.PARAM_CATEGORY_ASPECT));
476             }
477             else if (savedCondition.getActionConditionDefinitionName().equals(ComparePropertyValueEvaluator.NAME) == true)
478             {
479                 assertEquals(2, savedCondition.getParameterValues().size());
480                 assertEquals("*.exe", savedCondition.getParameterValue(ComparePropertyValueEvaluator.PARAM_VALUE));
481                 assertEquals(ComparePropertyValueOperation.EQUALS, savedCondition.getParameterValue(ComparePropertyValueEvaluator.PARAM_OPERATION));
482             }
483             else
484             {
485                 fail("There is a condition here that we are not expecting.");
486             }
487         }
488         
489         //System.out.println(NodeStoreInspector.dumpNodeStore(this.nodeService, this.testStoreRef));
490
}
491     
492     /**
493      * Test saving a composite action
494      */

495     public void testSaveCompositeAction()
496     {
497         Action action1 = this.actionService.createAction(AddFeaturesActionExecuter.NAME);
498         Action action2 = this.actionService.createAction(CheckInActionExecuter.NAME);
499         
500         CompositeAction compositeAction = this.actionService.createCompositeAction();
501         String JavaDoc actionId = compositeAction.getId();
502         compositeAction.addAction(action1);
503         compositeAction.addAction(action2);
504         
505         this.actionService.saveAction(this.nodeRef, compositeAction);
506         assertEquals(1, this.actionService.getActions(this.nodeRef).size());
507         CompositeAction savedCompositeAction = (CompositeAction)this.actionService.getAction(this.nodeRef, actionId);
508         
509         // Check the saved composite action
510
assertEquals(2, savedCompositeAction.getActions().size());
511         for (Action action : savedCompositeAction.getActions())
512         {
513             if (action.getActionDefinitionName().equals(AddFeaturesActionExecuter.NAME) == true)
514             {
515                 assertEquals(action, action1);
516             }
517             else if (action.getActionDefinitionName().equals(CheckInActionExecuter.NAME) == true)
518             {
519                 assertEquals(action, action2);
520             }
521             else
522             {
523                 fail("We have an action here we are not expecting.");
524             }
525         }
526         
527         // Change the actions and re-save
528
compositeAction.removeAction(action1);
529         Action action3 = this.actionService.createAction(CheckOutActionExecuter.NAME);
530         compositeAction.addAction(action3);
531         action2.setParameterValue(CheckInActionExecuter.PARAM_DESCRIPTION, "description");
532         
533         this.actionService.saveAction(this.nodeRef, compositeAction);
534         assertEquals(1, this.actionService.getActions(this.nodeRef).size());
535         CompositeAction savedCompositeAction2 = (CompositeAction)this.actionService.getAction(this.nodeRef, actionId);
536         
537         assertEquals(2, savedCompositeAction2.getActions().size());
538         for (Action action : savedCompositeAction2.getActions())
539         {
540             if (action.getActionDefinitionName().equals(CheckOutActionExecuter.NAME) == true)
541             {
542                 assertEquals(action, action3);
543             }
544             else if (action.getActionDefinitionName().equals(CheckInActionExecuter.NAME) == true)
545             {
546                 assertEquals(action, action2);
547                 assertEquals("description", action2.getParameterValue(CheckInActionExecuter.PARAM_DESCRIPTION));
548             }
549             else
550             {
551                 fail("We have an action here we are not expecting.");
552             }
553         }
554     }
555     
556     /**
557      * Test remove action
558      */

559     public void testRemove()
560     {
561         assertEquals(0, this.actionService.getActions(this.nodeRef).size());
562         
563         Action action1 = this.actionService.createAction(AddFeaturesActionExecuter.NAME);
564         this.actionService.saveAction(this.nodeRef, action1);
565         Action action2 = this.actionService.createAction(CheckInActionExecuter.NAME);
566         this.actionService.saveAction(this.nodeRef, action2);
567         assertEquals(2, this.actionService.getActions(this.nodeRef).size());
568         
569         this.actionService.removeAction(this.nodeRef, action1);
570         assertEquals(1, this.actionService.getActions(this.nodeRef).size());
571         
572         this.actionService.removeAllActions(this.nodeRef);
573         assertEquals(0, this.actionService.getActions(this.nodeRef).size());
574     }
575     
576     public void testConditionOrder()
577     {
578         Action action = this.actionService.createAction(AddFeaturesActionExecuter.NAME);
579         String JavaDoc actionId = action.getId();
580         
581         ActionCondition condition1 = this.actionService.createActionCondition(NoConditionEvaluator.NAME);
582         ActionCondition condition2 = this.actionService.createActionCondition(NoConditionEvaluator.NAME);
583         
584         action.addActionCondition(condition1);
585         action.addActionCondition(condition2);
586         
587         this.actionService.saveAction(this.nodeRef, action);
588         Action savedAction = this.actionService.getAction(this.nodeRef, actionId);
589         
590         // Check that the conditions have been retrieved in the correct order
591
assertNotNull(savedAction);
592         assertEquals(condition1, savedAction.getActionCondition(0));
593         assertEquals(condition2, savedAction.getActionCondition(1));
594         
595         ActionCondition condition3 = this.actionService.createActionCondition(NoConditionEvaluator.NAME);
596         ActionCondition condition4 = this.actionService.createActionCondition(NoConditionEvaluator.NAME);
597         
598         // Update the conditions on the action
599
savedAction.removeActionCondition(condition1);
600         savedAction.addActionCondition(condition3);
601         savedAction.addActionCondition(condition4);
602         
603         this.actionService.saveAction(this.nodeRef, savedAction);
604         Action savedAction2 = this.actionService.getAction(this.nodeRef, actionId);
605         
606         // Check that the conditions are still in the correct order
607
assertNotNull(savedAction2);
608         assertEquals(condition2, savedAction2.getActionCondition(0));
609         assertEquals(condition3, savedAction2.getActionCondition(1));
610         assertEquals(condition4, savedAction2.getActionCondition(2));
611     }
612     
613     public void testActionOrder()
614     {
615         CompositeAction action = this.actionService.createCompositeAction();
616         String JavaDoc actionId = action.getId();
617         
618         Action action1 = this.actionService.createAction(AddFeaturesActionExecuter.NAME);
619         Action action2 = this.actionService.createAction(AddFeaturesActionExecuter.NAME);
620         
621         action.addAction(action1);
622         action.addAction(action2);
623         
624         this.actionService.saveAction(this.nodeRef, action);
625         CompositeAction savedAction = (CompositeAction)this.actionService.getAction(this.nodeRef, actionId);
626         
627         // Check that the conditions have been retrieved in the correct order
628
assertNotNull(savedAction);
629         assertEquals(action1, savedAction.getAction(0));
630         assertEquals(action2, savedAction.getAction(1));
631         
632         Action action3 = this.actionService.createAction(AddFeaturesActionExecuter.NAME);
633         Action action4 = this.actionService.createAction(AddFeaturesActionExecuter.NAME);
634         
635         // Update the conditions on the action
636
savedAction.removeAction(action1);
637         savedAction.addAction(action3);
638         savedAction.addAction(action4);
639         
640         this.actionService.saveAction(this.nodeRef, savedAction);
641         CompositeAction savedAction2 = (CompositeAction)this.actionService.getAction(this.nodeRef, actionId);
642         
643         // Check that the conditions are still in the correct order
644
assertNotNull(savedAction2);
645         assertEquals(action2, savedAction2.getAction(0));
646         assertEquals(action3, savedAction2.getAction(1));
647         assertEquals(action4, savedAction2.getAction(2));
648     }
649     
650     /** ===================================================================================
651      * Test asynchronous actions
652      */

653     
654     /**
655      * Test asynchronous execute action
656      */

657     public void testAsyncExecuteAction()
658     {
659         assertFalse(this.nodeService.hasAspect(this.nodeRef, ContentModel.ASPECT_VERSIONABLE));
660         
661         Action action = this.actionService.createAction(AddFeaturesActionExecuter.NAME);
662         action.setParameterValue(AddFeaturesActionExecuter.PARAM_ASPECT_NAME, ContentModel.ASPECT_VERSIONABLE);
663         action.setExecuteAsynchronously(true);
664         
665         this.actionService.executeAction(action, this.nodeRef);
666         
667         setComplete();
668         endTransaction();
669         
670         final NodeService finalNodeService = this.nodeService;
671         final NodeRef finalNodeRef = this.nodeRef;
672         
673         postAsyncActionTest(
674                 this.transactionService,
675                 1000,
676                 10,
677                 new AsyncTest()
678                 {
679                     public boolean executeTest()
680                     {
681                         return (
682                             finalNodeService.hasAspect(finalNodeRef, ContentModel.ASPECT_VERSIONABLE));
683                     };
684                 });
685     }
686     
687     
688     
689     /**
690      * Test async composite action execution
691      */

692     public void testAsyncCompositeActionExecute()
693     {
694         // Create the composite action
695
Action action1 = this.actionService.createAction(AddFeaturesActionExecuter.NAME);
696         action1.setParameterValue(AddFeaturesActionExecuter.PARAM_ASPECT_NAME, ContentModel.ASPECT_LOCKABLE);
697         Action action2 = this.actionService.createAction(AddFeaturesActionExecuter.NAME);
698         action2.setParameterValue(AddFeaturesActionExecuter.PARAM_ASPECT_NAME, ContentModel.ASPECT_VERSIONABLE);
699         CompositeAction compAction = this.actionService.createCompositeAction();
700         compAction.setTitle("title");
701         compAction.setDescription("description");
702         compAction.addAction(action1);
703         compAction.addAction(action2);
704         compAction.setExecuteAsynchronously(true);
705         
706         // Execute the composite action
707
this.actionService.executeAction(compAction, this.nodeRef);
708         
709         setComplete();
710         endTransaction();
711         
712         final NodeService finalNodeService = this.nodeService;
713         final NodeRef finalNodeRef = this.nodeRef;
714         
715         postAsyncActionTest(
716                 this.transactionService,
717                 1000,
718                 10,
719                 new AsyncTest()
720                 {
721                     public boolean executeTest()
722                     {
723                         return (
724                             finalNodeService.hasAspect(finalNodeRef, ContentModel.ASPECT_VERSIONABLE) &&
725                             finalNodeService.hasAspect(finalNodeRef, ContentModel.ASPECT_LOCKABLE));
726                     };
727                 });
728     }
729     
730     public void xtestAsyncLoadTest()
731     {
732         // TODO this is very weak .. how do we improve this ???
733

734         Action action = this.actionService.createAction(AddFeaturesActionExecuter.NAME);
735         action.setParameterValue(AddFeaturesActionExecuter.PARAM_ASPECT_NAME, ContentModel.ASPECT_VERSIONABLE);
736         action.setExecuteAsynchronously(true);
737         
738         for (int i = 0; i < 1000; i++)
739         {
740             this.actionService.executeAction(action, this.nodeRef);
741         }
742         
743         setComplete();
744         endTransaction();
745         
746         // TODO how do we assess whether the large number of actions stacked cause a problem ??
747
}
748     
749     /**
750      *
751      * @param sleepTime
752      * @param maxTries
753      * @param test
754      * @param context
755      */

756     public static void postAsyncActionTest(
757             TransactionService transactionService,
758             final long sleepTime,
759             final int maxTries,
760             final AsyncTest test)
761     {
762         try
763         {
764             int tries = 0;
765             boolean done = false;
766             while (done == false && tries < maxTries)
767             {
768                 try
769                 {
770                     // Increment the tries counter
771
tries++;
772                     
773                     // Sleep for a bit
774
Thread.sleep(sleepTime);
775                     
776                     done = (TransactionUtil.executeInUserTransaction(
777                                 transactionService,
778                                 new TransactionUtil.TransactionWork<Boolean JavaDoc>()
779                                 {
780                                     public Boolean JavaDoc doWork()
781                                     {
782                                         // See if the action has been performed
783
boolean done = test.executeTest();
784                                         return done;
785                                     }
786                                 })).booleanValue();
787                 }
788                 catch (InterruptedException JavaDoc e)
789                 {
790                     // Do nothing
791
e.printStackTrace();
792                 }
793             }
794             
795             if (done == false)
796             {
797                 throw new RuntimeException JavaDoc("Asynchronous action was not executed.");
798             }
799         }
800         catch (Throwable JavaDoc exception)
801         {
802             exception.printStackTrace();
803             fail("An exception was encountered whilst checking the async action was executed: " + exception.getMessage());
804         }
805     }
806     
807     /**
808      * Async test interface
809      */

810     public interface AsyncTest
811     {
812         boolean executeTest();
813     }
814         
815     /** ===================================================================================
816      * Test failure behaviour
817      */

818     
819     /**
820      * Test sync failure behaviour
821      */

822     public void testSyncFailureBehaviour()
823     {
824         // Create an action that is going to fail
825
Action action = this.actionService.createAction(MoveActionExecuter.NAME);
826         action.setParameterValue(MoveActionExecuter.PARAM_ASSOC_TYPE_QNAME, ContentModel.ASSOC_CHILDREN);
827         action.setParameterValue(MoveActionExecuter.PARAM_ASSOC_QNAME, ContentModel.ASSOC_CHILDREN);
828         // Create a bad node ref
829
NodeRef badNodeRef = new NodeRef(this.storeRef, "123123");
830         action.setParameterValue(MoveActionExecuter.PARAM_DESTINATION_FOLDER, badNodeRef);
831         
832         try
833         {
834             this.actionService.executeAction(action, this.nodeRef);
835             
836             // Fail if we get there since the exception should have been raised
837
fail("An exception should have been raised.");
838         }
839         catch (RuntimeException JavaDoc exception)
840         {
841             // Good! The exception was raised correctly
842
}
843         
844         // Test what happens when a element of a composite action fails (should raise and bubble up to parent bahviour)
845
// Create the composite action
846
Action action1 = this.actionService.createAction(AddFeaturesActionExecuter.NAME);
847         action1.setParameterValue(AddFeaturesActionExecuter.PARAM_ASPECT_NAME, ContentModel.ASPECT_LOCKABLE);
848         Action action2 = this.actionService.createAction(AddFeaturesActionExecuter.NAME);
849         action2.setParameterValue(AddFeaturesActionExecuter.PARAM_ASPECT_NAME, QName.createQName("{test}badDogAspect"));
850         CompositeAction compAction = this.actionService.createCompositeAction();
851         compAction.setTitle("title");
852         compAction.setDescription("description");
853         compAction.addAction(action1);
854         compAction.addAction(action2);
855         
856         try
857         {
858             // Execute the composite action
859
this.actionService.executeAction(compAction, this.nodeRef);
860             
861             fail("An exception should have been raised here !!");
862         }
863         catch (RuntimeException JavaDoc runtimeException)
864         {
865             // Good! The exception was raised
866
}
867     }
868     
869     /**
870      * Test the compensating action
871      */

872     public void testCompensatingAction()
873     {
874         // Create an action that is going to fail
875
final Action action = this.actionService.createAction(MoveActionExecuter.NAME);
876         action.setParameterValue(MoveActionExecuter.PARAM_ASSOC_TYPE_QNAME, ContentModel.ASSOC_CHILDREN);
877         action.setParameterValue(MoveActionExecuter.PARAM_ASSOC_QNAME, ContentModel.ASSOC_CHILDREN);
878         // Create a bad node ref
879
NodeRef badNodeRef = new NodeRef(this.storeRef, "123123");
880         action.setParameterValue(MoveActionExecuter.PARAM_DESTINATION_FOLDER, badNodeRef);
881         action.setTitle("title");
882         
883         // Create the compensating action
884
Action compensatingAction = actionService.createAction(AddFeaturesActionExecuter.NAME);
885         compensatingAction.setParameterValue(AddFeaturesActionExecuter.PARAM_ASPECT_NAME, ContentModel.ASPECT_CLASSIFIABLE);
886         compensatingAction.setTitle("title");
887         action.setCompensatingAction(compensatingAction);
888         
889         // Set the action to execute asynchronously
890
action.setExecuteAsynchronously(true);
891         
892         this.actionService.executeAction(action, this.nodeRef);
893         
894         setComplete();
895         endTransaction();
896         
897         postAsyncActionTest(
898                 this.transactionService,
899                 1000,
900                 10,
901                 new AsyncTest()
902                 {
903                     public boolean executeTest()
904                     {
905                         return (
906                             ActionServiceImplTest.this.nodeService.hasAspect(nodeRef, ContentModel.ASPECT_CLASSIFIABLE));
907                     };
908                 });
909         
910         // Modify the compensating action so that it will also fail
911
compensatingAction.setParameterValue(AddFeaturesActionExecuter.PARAM_ASPECT_NAME, QName.createQName("{test}badAspect"));
912         
913         TransactionUtil.executeInUserTransaction(
914                 this.transactionService,
915                 new TransactionUtil.TransactionWork<Object JavaDoc>()
916                 {
917                     public Object JavaDoc doWork()
918                     {
919                         try
920                         {
921                             ActionServiceImplTest.this.actionService.executeAction(action, ActionServiceImplTest.this.nodeRef);
922                         }
923                         catch (RuntimeException JavaDoc exception)
924                         {
925                             // The exception should have been ignored and execution continued
926
exception.printStackTrace();
927                             fail("An exception should not have been raised here.");
928                         }
929                         return null;
930                     }
931                     
932                 });
933         
934     }
935 }
Popular Tags