1 17 package org.alfresco.repo.rule; 18 19 import java.io.Serializable ; 20 import java.util.HashMap ; 21 import java.util.List ; 22 import java.util.Map ; 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 50 public class BaseRuleTest extends BaseSpringTest 51 { 52 55 protected static final String RULE_TYPE_NAME = RuleType.INBOUND; 56 57 60 protected static final String ACTION_DEF_NAME = AddFeaturesActionExecuter.NAME; 61 protected static final String ACTION_PROP_NAME_1 = AddFeaturesActionExecuter.PARAM_ASPECT_NAME; 62 protected static final QName ACTION_PROP_VALUE_1 = ContentModel.ASPECT_LOCKABLE; 63 64 67 protected static final String CONDITION_DEF_NAME = ComparePropertyValueEvaluator.NAME; 68 protected static final String COND_PROP_NAME_1 = ComparePropertyValueEvaluator.PARAM_VALUE; 69 protected static final String COND_PROP_VALUE_1 = ".doc"; 70 71 74 protected static final String TITLE = "title"; 75 protected static final String DESCRIPTION = "description"; 76 77 80 protected NodeService nodeService; 81 protected ContentService contentService; 82 protected RuleService ruleService; 83 protected ConfigurableService configService; 84 protected AuthenticationComponent authenticationComponent; 85 86 89 protected RuleType ruleType; 90 91 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 104 @Override 105 protected void onSetUpInTransaction() throws Exception 106 { 107 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 this.ruleType = this.ruleService.getRuleType(RULE_TYPE_NAME); 124 125 this.testStoreRef = this.nodeService.createStore( 127 StoreRef.PROTOCOL_WORKSPACE, "Test_" 128 + System.currentTimeMillis()); 129 this.rootNodeRef = this.nodeService.getRootNode(this.testStoreRef); 130 131 this.nodeRef = this.nodeService.createNode(rootNodeRef, 133 ContentModel.ASSOC_CHILDREN, 134 QName.createQName("{test}testnode"), 135 ContentModel.TYPE_CONTAINER).getChildRef(); 136 } 137 138 @Override 139 protected void onTearDownInTransaction() throws Exception 140 { 141 authenticationComponent.clearCurrentSecurityContext(); 142 super.onTearDownInTransaction(); 143 } 144 145 protected void addRulesAspect() 146 { 147 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 Map <String , Serializable > conditionProps = new HashMap <String , Serializable >(); 161 conditionProps.put(COND_PROP_NAME_1, COND_PROP_VALUE_1); 162 163 Map <String , Serializable > actionProps = new HashMap <String , Serializable >(); 164 actionProps.put(ACTION_PROP_NAME_1, ACTION_PROP_VALUE_1); 165 166 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 id) 184 { 185 assertEquals(id, rule.getId()); 187 assertEquals(this.ruleType.getName(), rule.getRuleTypeName()); 188 assertEquals(TITLE, rule.getTitle()); 189 assertEquals(DESCRIPTION, rule.getDescription()); 190 191 List <ActionCondition> ruleConditions = rule.getActionConditions(); 193 assertNotNull(ruleConditions); 194 assertEquals(1, ruleConditions.size()); 195 assertEquals(CONDITION_DEF_NAME, ruleConditions.get(0) 196 .getActionConditionDefinitionName()); 197 Map <String , Serializable > 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 List <Action> ruleActions = rule.getActions(); 206 assertNotNull(ruleActions); 207 assertEquals(1, ruleActions.size()); 208 assertEquals(ACTION_DEF_NAME, ruleActions.get(0).getActionDefinitionName()); 209 Map <String , Serializable > 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 |