KickJava   Java API By Example, From Geeks To Geeks.

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


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.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.executer.AddFeaturesActionExecuter;
27 import org.alfresco.repo.configuration.ConfigurableService;
28 import org.alfresco.repo.security.authentication.AuthenticationComponent;
29 import org.alfresco.service.cmr.action.Action;
30 import org.alfresco.service.cmr.action.ActionCondition;
31 import org.alfresco.service.cmr.action.ActionService;
32 import org.alfresco.service.cmr.repository.ContentService;
33 import org.alfresco.service.cmr.repository.NodeRef;
34 import org.alfresco.service.cmr.repository.NodeService;
35 import org.alfresco.service.cmr.repository.StoreRef;
36 import org.alfresco.service.cmr.rule.Rule;
37 import org.alfresco.service.cmr.rule.RuleService;
38 import org.alfresco.service.cmr.rule.RuleType;
39 import org.alfresco.service.namespace.QName;
40 import org.alfresco.service.transaction.TransactionService;
41 import org.alfresco.util.BaseSpringTest;
42
43 /**
44  * Base class for rule service test.
45  * <p>
46  * This file contains a number of helpers to reduce the duplication in tests.
47  *
48  * @author Roy Wetherall
49  */

50 public class BaseRuleTest extends BaseSpringTest
51 {
52     /**
53      * Data used in the tests
54      */

55     protected static final String JavaDoc RULE_TYPE_NAME = RuleType.INBOUND;
56
57     /**
58      * Action used in tests
59      */

60     protected static final String JavaDoc ACTION_DEF_NAME = AddFeaturesActionExecuter.NAME;
61     protected static final String JavaDoc ACTION_PROP_NAME_1 = AddFeaturesActionExecuter.PARAM_ASPECT_NAME;
62     protected static final QName ACTION_PROP_VALUE_1 = ContentModel.ASPECT_LOCKABLE;
63
64     /**
65      * ActionCondition used in tests
66      */

67     protected static final String JavaDoc CONDITION_DEF_NAME = ComparePropertyValueEvaluator.NAME;
68     protected static final String JavaDoc COND_PROP_NAME_1 = ComparePropertyValueEvaluator.PARAM_VALUE;
69     protected static final String JavaDoc COND_PROP_VALUE_1 = ".doc";
70
71     /**
72      * Rule values used in tests
73      */

74     protected static final String JavaDoc TITLE = "title";
75     protected static final String JavaDoc DESCRIPTION = "description";
76
77     /**
78      * Services
79      */

80     protected NodeService nodeService;
81     protected ContentService contentService;
82     protected RuleService ruleService;
83     protected ConfigurableService configService;
84     protected AuthenticationComponent authenticationComponent;
85
86     /**
87      * Rule type used in tests
88      */

89     protected RuleType ruleType;
90
91     /**
92      * Store and node references
93      */

94     protected StoreRef testStoreRef;
95     protected NodeRef rootNodeRef;
96     protected NodeRef nodeRef;
97     protected NodeRef configFolder;
98     protected ActionService actionService;
99     protected TransactionService transactionService;
100
101     /**
102      * onSetUpInTransaction implementation
103      */

104     @Override JavaDoc
105     protected void onSetUpInTransaction() throws Exception JavaDoc
106     {
107         // Get the services
108
this.nodeService = (NodeService) this.applicationContext
109                 .getBean("nodeService");
110         this.contentService = (ContentService) this.applicationContext
111                 .getBean("contentService");
112         this.ruleService = (RuleService) this.applicationContext
113                 .getBean("ruleService");
114         this.configService = (ConfigurableService)this.applicationContext
115                 .getBean("configurableService");
116         this.actionService = (ActionService)this.applicationContext.getBean("actionService");
117         this.transactionService = (TransactionService)this.applicationContext.getBean("transactionComponent");
118         this.authenticationComponent = (AuthenticationComponent)this.applicationContext.getBean("authenticationComponent");
119
120         authenticationComponent.setSystemUserAsCurrentUser();
121         
122         // Get the rule type
123
this.ruleType = this.ruleService.getRuleType(RULE_TYPE_NAME);
124
125         // Create the store and get the root node
126
this.testStoreRef = this.nodeService.createStore(
127                 StoreRef.PROTOCOL_WORKSPACE, "Test_"
128                         + System.currentTimeMillis());
129         this.rootNodeRef = this.nodeService.getRootNode(this.testStoreRef);
130
131         // Create the node used for tests
132
this.nodeRef = this.nodeService.createNode(rootNodeRef,
133                 ContentModel.ASSOC_CHILDREN,
134                 QName.createQName("{test}testnode"),
135                 ContentModel.TYPE_CONTAINER).getChildRef();
136     }
137     
138     @Override JavaDoc
139     protected void onTearDownInTransaction() throws Exception JavaDoc
140     {
141         authenticationComponent.clearCurrentSecurityContext();
142         super.onTearDownInTransaction();
143     }
144
145     protected void addRulesAspect()
146     {
147         // Make the node actionable
148
this.configService.makeConfigurable(this.nodeRef);
149         this.nodeService.addAspect(this.nodeRef, RuleModel.ASPECT_RULES, null);
150     }
151
152     protected Rule createTestRule()
153     {
154         return createTestRule(false);
155     }
156     
157     protected Rule createTestRule(boolean isAppliedToChildren)
158     {
159         // Rule properties
160
Map JavaDoc<String JavaDoc, Serializable JavaDoc> conditionProps = new HashMap JavaDoc<String JavaDoc, Serializable JavaDoc>();
161         conditionProps.put(COND_PROP_NAME_1, COND_PROP_VALUE_1);
162
163         Map JavaDoc<String JavaDoc, Serializable JavaDoc> actionProps = new HashMap JavaDoc<String JavaDoc, Serializable JavaDoc>();
164         actionProps.put(ACTION_PROP_NAME_1, ACTION_PROP_VALUE_1);
165         
166         // Create the rule
167
Rule rule = this.ruleService.createRule(this.ruleType.getName());
168         rule.setTitle(TITLE);
169         rule.setDescription(DESCRIPTION);
170         rule.applyToChildren(isAppliedToChildren);
171         
172         ActionCondition actionCondition = this.actionService.createActionCondition(CONDITION_DEF_NAME);
173         actionCondition.setParameterValues(conditionProps);
174         rule.addActionCondition(actionCondition);
175         
176         Action action = this.actionService.createAction(CONDITION_DEF_NAME);
177         action.setParameterValues(conditionProps);
178         rule.addAction(action);
179
180         return rule;
181     }
182
183     protected void checkRule(RuleImpl rule, String JavaDoc id)
184     {
185         // Check the basic details of the rule
186
assertEquals(id, rule.getId());
187         assertEquals(this.ruleType.getName(), rule.getRuleTypeName());
188         assertEquals(TITLE, rule.getTitle());
189         assertEquals(DESCRIPTION, rule.getDescription());
190
191         // Check conditions
192
List JavaDoc<ActionCondition> ruleConditions = rule.getActionConditions();
193         assertNotNull(ruleConditions);
194         assertEquals(1, ruleConditions.size());
195         assertEquals(CONDITION_DEF_NAME, ruleConditions.get(0)
196                 .getActionConditionDefinitionName());
197         Map JavaDoc<String JavaDoc, Serializable JavaDoc> condParams = ruleConditions.get(0)
198                 .getParameterValues();
199         assertNotNull(condParams);
200         assertEquals(1, condParams.size());
201         assertTrue(condParams.containsKey(COND_PROP_NAME_1));
202         assertEquals(COND_PROP_VALUE_1, condParams.get(COND_PROP_NAME_1));
203
204         // Check the actions
205
List JavaDoc<Action> ruleActions = rule.getActions();
206         assertNotNull(ruleActions);
207         assertEquals(1, ruleActions.size());
208         assertEquals(ACTION_DEF_NAME, ruleActions.get(0).getActionDefinitionName());
209         Map JavaDoc<String JavaDoc, Serializable JavaDoc> actionParams = ruleActions.get(0).getParameterValues();
210         assertNotNull(actionParams);
211         assertEquals(1, actionParams.size());
212         assertTrue(actionParams.containsKey(ACTION_PROP_NAME_1));
213         assertEquals(ACTION_PROP_VALUE_1, actionParams.get(ACTION_PROP_NAME_1));
214     }
215 }
216
Popular Tags