View Javadoc

1   /**
2    * Copyright 2005-2011 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.rice.krms.test;
17  
18  import org.apache.commons.lang.builder.ToStringBuilder;
19  import org.apache.commons.lang.builder.ToStringStyle;
20  import org.joda.time.DateTime;
21  import org.junit.Before;
22  import org.junit.Test;
23  import org.kuali.rice.krms.api.KrmsApiServiceLocator;
24  import org.kuali.rice.krms.api.KrmsConstants;
25  import org.kuali.rice.krms.api.engine.EngineResults;
26  import org.kuali.rice.krms.api.engine.ExecutionFlag;
27  import org.kuali.rice.krms.api.engine.ExecutionOptions;
28  import org.kuali.rice.krms.api.engine.Facts;
29  import org.kuali.rice.krms.api.engine.SelectionCriteria;
30  import org.kuali.rice.krms.api.repository.action.ActionDefinition;
31  import org.kuali.rice.krms.api.repository.agenda.AgendaDefinition;
32  import org.kuali.rice.krms.api.repository.agenda.AgendaItem;
33  import org.kuali.rice.krms.api.repository.context.ContextDefinition;
34  import org.kuali.rice.krms.api.repository.proposition.PropositionDefinition;
35  import org.kuali.rice.krms.api.repository.proposition.PropositionParameter;
36  import org.kuali.rice.krms.api.repository.proposition.PropositionParameterType;
37  import org.kuali.rice.krms.api.repository.proposition.PropositionType;
38  import org.kuali.rice.krms.api.repository.rule.RuleDefinition;
39  import org.kuali.rice.krms.api.repository.term.TermDefinition;
40  import org.kuali.rice.krms.api.repository.term.TermSpecificationDefinition;
41  import org.kuali.rice.krms.api.repository.type.KrmsAttributeDefinition;
42  import org.kuali.rice.krms.api.repository.type.KrmsTypeAttribute;
43  import org.kuali.rice.krms.api.repository.type.KrmsTypeDefinition;
44  import org.kuali.rice.krms.api.repository.type.KrmsTypeRepositoryService;
45  import org.kuali.rice.krms.framework.type.ValidationActionType;
46  import org.kuali.rice.krms.framework.type.ValidationActionTypeService;
47  import org.kuali.rice.krms.framework.type.ValidationRuleType;
48  import org.kuali.rice.krms.framework.type.ValidationRuleTypeService;
49  import org.kuali.rice.krms.impl.repository.ActionAttributeBo;
50  import org.kuali.rice.krms.impl.repository.ActionBo;
51  import org.kuali.rice.krms.impl.repository.ActionBoService;
52  import org.kuali.rice.krms.impl.repository.ActionBoServiceImpl;
53  import org.kuali.rice.krms.impl.repository.AgendaAttributeBo;
54  import org.kuali.rice.krms.impl.repository.AgendaBo;
55  import org.kuali.rice.krms.impl.repository.AgendaBoService;
56  import org.kuali.rice.krms.impl.repository.AgendaBoServiceImpl;
57  import org.kuali.rice.krms.impl.repository.AgendaItemBo;
58  import org.kuali.rice.krms.impl.repository.ContextAttributeBo;
59  import org.kuali.rice.krms.impl.repository.ContextBo;
60  import org.kuali.rice.krms.impl.repository.ContextBoService;
61  import org.kuali.rice.krms.impl.repository.ContextBoServiceImpl;
62  import org.kuali.rice.krms.impl.repository.KrmsAttributeDefinitionBo;
63  import org.kuali.rice.krms.impl.repository.KrmsAttributeDefinitionService;
64  import org.kuali.rice.krms.impl.repository.KrmsRepositoryServiceLocator;
65  import org.kuali.rice.krms.impl.repository.KrmsTypeBoServiceImpl;
66  import org.kuali.rice.krms.impl.repository.PropositionBoService;
67  import org.kuali.rice.krms.impl.repository.PropositionBoServiceImpl;
68  import org.kuali.rice.krms.impl.repository.RuleAttributeBo;
69  import org.kuali.rice.krms.impl.repository.RuleBo;
70  import org.kuali.rice.krms.impl.repository.RuleBoService;
71  import org.kuali.rice.krms.impl.repository.RuleBoServiceImpl;
72  import org.kuali.rice.krms.impl.repository.TermBoService;
73  import org.kuali.rice.krms.impl.repository.TermBoServiceImpl;
74  import org.kuali.rice.krms.impl.util.KRMSServiceLocatorInternal;
75  import org.kuali.rice.test.BaselineTestCase.BaselineMode;
76  import org.kuali.rice.test.BaselineTestCase.Mode;
77  import org.springframework.transaction.annotation.Transactional;
78  
79  import java.util.ArrayList;
80  import java.util.Collections;
81  import java.util.HashMap;
82  import java.util.HashSet;
83  import java.util.List;
84  import java.util.Map;
85  import java.util.Set;
86  
87  import static org.junit.Assert.assertEquals;
88  import static org.junit.Assert.assertNotNull;
89  import static org.junit.Assert.assertTrue;
90  
91  /**
92   * Validation Integration Test
93   * @author Kuali Rice Team (rice.collab@kuali.org)
94   */
95  @BaselineMode(Mode.CLEAR_DB)
96  public class ValidationIntegrationTest extends AbstractBoTest {
97  
98      private static final String EVENT_ATTRIBUTE = "Event";
99  //    private static final String TERM_NAME = "totalProposalDollarAmount";
100     private static final String TERM_NAME = "campusCodeTermSpec";
101 
102     private static final String CONTEXT_NAME = "ValidationITContext";
103     private static final String WARNING_MESSAGE = "Warning Message.";
104     private static final String ERROR_MESSAGE = "Error Message.";
105     private static final String VALIDATION_ACTION_TYPE_SERVICE = "validationActionTypeService";
106     private static final String VALIDATION_RULE_TYPE_SERVICE = "validationRuleTypeService";
107 
108     private KrmsTypeRepositoryService krmsTypeRepositoryService;
109     private KrmsAttributeDefinitionService krmsAttributeDefinitionService;
110     private PropositionBoService propositionBoService;
111     private TermBoService termBoService;
112     private ContextBoService contextRepository;
113     private AgendaBoService agendaBoService;
114     private RuleBoService ruleBoService;
115     private ActionBoService actionBoService;
116 
117 
118 	@Before
119 	public void setup() {
120 
121         krmsAttributeDefinitionService = KrmsRepositoryServiceLocator.getKrmsAttributeDefinitionService();
122         krmsTypeRepositoryService = new KrmsTypeBoServiceImpl();
123         ((KrmsTypeBoServiceImpl)krmsTypeRepositoryService).setBusinessObjectService(getBoService());
124 
125         // like RepositoryCreateAndExecuteIntegrationTest
126         propositionBoService = new PropositionBoServiceImpl();
127         ((PropositionBoServiceImpl)propositionBoService).setBusinessObjectService(getBoService());
128         termBoService = new TermBoServiceImpl();
129         ((TermBoServiceImpl)termBoService).setBusinessObjectService(getBoService());
130         contextRepository = new ContextBoServiceImpl();
131         ((ContextBoServiceImpl)contextRepository).setBusinessObjectService(getBoService());
132         agendaBoService = new AgendaBoServiceImpl();
133         ((AgendaBoServiceImpl)agendaBoService).setBusinessObjectService(getBoService());
134         ((AgendaBoServiceImpl)agendaBoService).setAttributeDefinitionService(krmsAttributeDefinitionService);
135         ruleBoService = new RuleBoServiceImpl();
136         ((RuleBoServiceImpl)ruleBoService).setBusinessObjectService(getBoService());
137         actionBoService = new ActionBoServiceImpl();
138         ((ActionBoServiceImpl)actionBoService).setBusinessObjectService(getBoService());
139     }
140 
141     @Transactional
142     @Test
143     public void testValidWarning() {
144         KrmsAttributeTypeDefinitionAndBuilders ruleDefs = createKrmsAttributeTypeDefinitionAndBuilders(
145                 ValidationRuleTypeService.VALIDATIONS_RULE_TYPE_CODE_ATTRIBUTE, KrmsConstants.KRMS_NAMESPACE,
146                 ValidationRuleType.VALID.toString(), true, ValidationRuleType.VALID.toString(),
147                 ValidationRuleType.VALID.getCode(), VALIDATION_RULE_TYPE_SERVICE, krmsTypeRepositoryService, 1);
148         KrmsAttributeTypeDefinitionAndBuilders actionDefs = createKrmsAttributeTypeDefinitionAndBuilders(
149                 ValidationActionTypeService.VALIDATIONS_ACTION_TYPE_CODE_ATTRIBUTE, KrmsConstants.KRMS_NAMESPACE,
150                 ValidationActionType.WARNING.toString(), true, ValidationActionType.WARNING.toString(),
151                 ValidationActionType.WARNING.getCode(), VALIDATION_ACTION_TYPE_SERVICE, krmsTypeRepositoryService, 1);
152         ContextBo contextBo = createContext();
153         RuleBo ruleBo = createRuleWithAction(ruleDefs, actionDefs, contextBo, WARNING_MESSAGE);
154         createAgenda(ruleBo, contextBo, createEventAttributeDefinition());
155 
156         EngineResults results = engineExecute();
157         assertTrue(results.getAttribute(ValidationActionTypeService.VALIDATIONS_ACTION_ATTRIBUTE) == null);
158     }
159 
160     @Transactional
161     @Test
162     public void testInvalidWarning() {
163         KrmsAttributeTypeDefinitionAndBuilders ruleDefs = createKrmsAttributeTypeDefinitionAndBuilders(
164                 ValidationRuleTypeService.VALIDATIONS_RULE_TYPE_CODE_ATTRIBUTE, KrmsConstants.KRMS_NAMESPACE,
165                 ValidationRuleType.INVALID.toString(), true, ValidationRuleType.INVALID.toString(),
166                 ValidationRuleType.INVALID.getCode(), VALIDATION_RULE_TYPE_SERVICE, krmsTypeRepositoryService, 1);
167         KrmsAttributeTypeDefinitionAndBuilders actionDefs = createKrmsAttributeTypeDefinitionAndBuilders(
168                 ValidationActionTypeService.VALIDATIONS_ACTION_TYPE_CODE_ATTRIBUTE, KrmsConstants.KRMS_NAMESPACE,
169                 ValidationActionType.WARNING.toString(), true, ValidationActionType.WARNING.toString(),
170                 ValidationActionType.WARNING.getCode(), VALIDATION_ACTION_TYPE_SERVICE, krmsTypeRepositoryService, 1);
171 
172         ContextBo contextBo = createContext();
173         RuleBo ruleBo = createRuleWithAction(ruleDefs, actionDefs, contextBo, WARNING_MESSAGE);
174         createAgenda(ruleBo, contextBo, createEventAttributeDefinition());
175 
176         EngineResults results = engineExecute();
177         assertNotNull(results.getAttribute(ValidationActionTypeService.VALIDATIONS_ACTION_ATTRIBUTE));
178         assertEquals(ValidationActionType.WARNING.getCode() + ":" + WARNING_MESSAGE,
179                 results.getAttribute(ValidationActionTypeService.VALIDATIONS_ACTION_ATTRIBUTE));
180     }
181 
182     @Transactional
183     @Test
184     public void testValidError() {
185         KrmsAttributeTypeDefinitionAndBuilders ruleDefs = createKrmsAttributeTypeDefinitionAndBuilders(
186                 ValidationRuleTypeService.VALIDATIONS_RULE_TYPE_CODE_ATTRIBUTE, KrmsConstants.KRMS_NAMESPACE,
187                 ValidationRuleType.VALID.toString(), true, ValidationRuleType.VALID.toString(),
188                 ValidationRuleType.VALID.getCode(), VALIDATION_RULE_TYPE_SERVICE, krmsTypeRepositoryService, 1);
189         KrmsAttributeTypeDefinitionAndBuilders actionDefs = createKrmsAttributeTypeDefinitionAndBuilders(
190                 ValidationActionTypeService.VALIDATIONS_ACTION_TYPE_CODE_ATTRIBUTE, KrmsConstants.KRMS_NAMESPACE,
191                 ValidationActionType.ERROR.toString(), true, ValidationActionType.ERROR.toString(),
192                 ValidationActionType.ERROR.getCode(), VALIDATION_ACTION_TYPE_SERVICE, krmsTypeRepositoryService, 1);
193 
194 
195         ContextBo contextBo = createContext();
196         RuleBo ruleBo = createRuleWithAction(ruleDefs, actionDefs, contextBo, ERROR_MESSAGE);
197         createAgenda(ruleBo, contextBo, createEventAttributeDefinition());
198 
199         EngineResults results = engineExecute();
200         assertTrue(results.getAttribute(ValidationActionTypeService.VALIDATIONS_ACTION_ATTRIBUTE) == null);
201     }
202 
203     @Transactional
204     @Test
205     public void testInvalidError() {
206         KrmsAttributeTypeDefinitionAndBuilders ruleDefs = createKrmsAttributeTypeDefinitionAndBuilders(
207                 ValidationRuleTypeService.VALIDATIONS_RULE_TYPE_CODE_ATTRIBUTE, KrmsConstants.KRMS_NAMESPACE,
208                 ValidationRuleType.INVALID.toString(), true, ValidationRuleType.INVALID.toString(),
209                 ValidationRuleType.INVALID.getCode(), VALIDATION_RULE_TYPE_SERVICE, krmsTypeRepositoryService, 1);
210         KrmsAttributeTypeDefinitionAndBuilders actionDefs = createKrmsAttributeTypeDefinitionAndBuilders(
211                 ValidationActionTypeService.VALIDATIONS_ACTION_TYPE_CODE_ATTRIBUTE, KrmsConstants.KRMS_NAMESPACE,
212                 ValidationActionType.ERROR.toString(), true, ValidationActionType.ERROR.toString(),
213                 ValidationActionType.ERROR.getCode(), VALIDATION_ACTION_TYPE_SERVICE, krmsTypeRepositoryService, 1);
214 
215         ContextBo contextBo = createContext();
216         RuleBo ruleBo = createRuleWithAction(ruleDefs, actionDefs, contextBo, ERROR_MESSAGE);
217         createAgenda(ruleBo, contextBo, createEventAttributeDefinition());
218 
219         EngineResults results = engineExecute();
220         assertNotNull(results.getAttribute(ValidationActionTypeService.VALIDATIONS_ACTION_ATTRIBUTE));
221         assertEquals(ValidationActionType.ERROR.getCode() + ":" + ERROR_MESSAGE, results.getAttribute(
222                 ValidationActionTypeService.VALIDATIONS_ACTION_ATTRIBUTE));
223     }
224 
225     @Transactional
226     @Test
227     public void testDef() {
228         ContextDefinition contextDefinition = createContextDefinition(KrmsConstants.KRMS_NAMESPACE);
229         createAgendaDefinition(contextDefinition, "ValidationIntegration", KrmsConstants.KRMS_NAMESPACE);
230 
231         engineExecute();
232     }
233 
234     private ContextDefinition createContextDefinition(String nameSpace) {
235         // Attribute Defn for context;
236         KrmsAttributeDefinition.Builder contextTypeAttributeDefnBuilder = KrmsAttributeDefinition.Builder.create(null, "Context1Qualifier", nameSpace);
237         contextTypeAttributeDefnBuilder.setLabel("Context 1 Qualifier");
238         KrmsAttributeDefinition contextTypeAttributeDefinition = krmsAttributeDefinitionService.createAttributeDefinition(contextTypeAttributeDefnBuilder.build());
239 
240         // Attr for context;
241         KrmsTypeAttribute.Builder krmsTypeAttrBuilder = KrmsTypeAttribute.Builder.create(null, contextTypeAttributeDefinition.getId(), 1);
242 
243         // KrmsType for context
244         KrmsTypeDefinition.Builder krmsContextTypeDefnBuilder = KrmsTypeDefinition.Builder.create("KrmsTestContextType", nameSpace);
245         krmsContextTypeDefnBuilder.setAttributes(Collections.singletonList(krmsTypeAttrBuilder));
246         KrmsTypeDefinition krmsContextTypeDefinition = krmsContextTypeDefnBuilder.build();
247         krmsContextTypeDefinition = krmsTypeRepositoryService.createKrmsType(krmsContextTypeDefinition);
248 
249         // Context
250         ContextDefinition.Builder contextBuilder = ContextDefinition.Builder.create(nameSpace, CONTEXT_NAME);
251         contextBuilder.setTypeId(krmsContextTypeDefinition.getId());
252         ContextDefinition contextDefinition = contextBuilder.build();
253         contextDefinition = contextRepository.createContext(contextDefinition);
254 
255         // Context Attribute
256         // TODO: do this fur eel
257         ContextAttributeBo contextAttribute = new ContextAttributeBo();
258         contextAttribute.setAttributeDefinitionId(contextTypeAttributeDefinition.getId());
259         contextAttribute.setContextId(contextDefinition.getId());
260         contextAttribute.setValue("BLAH");
261         getBoService().save(contextAttribute);
262 
263         return contextDefinition;
264     }
265 
266     private void createAgendaDefinition(ContextDefinition contextDefinition, String eventName, String nameSpace ) {
267         KrmsTypeDefinition krmsGenericTypeDefinition = createKrmsGenericTypeDefinition(nameSpace);
268 
269         AgendaDefinition agendaDef =
270             AgendaDefinition.Builder.create(null, "testAgenda", krmsGenericTypeDefinition.getId(), contextDefinition.getId()).build();
271         agendaDef = agendaBoService.createAgenda(agendaDef);
272 
273         AgendaItem.Builder agendaItemBuilder1 = AgendaItem.Builder.create(null, agendaDef.getId());
274         agendaItemBuilder1.setRuleId(createRuleDefinition1(contextDefinition, nameSpace).getId());
275 
276         AgendaItem agendaItem1 = agendaBoService.createAgendaItem(agendaItemBuilder1.build());
277 
278         AgendaDefinition.Builder agendaDefBuilder1 = AgendaDefinition.Builder.create(agendaDef);
279         agendaDefBuilder1.setAttributes(Collections.singletonMap("Event", eventName));
280         agendaDefBuilder1.setFirstItemId(agendaItem1.getId());
281         agendaDef = agendaDefBuilder1.build();
282 
283         agendaBoService.updateAgenda(agendaDef);
284     }
285 
286     private KrmsTypeDefinition createKrmsGenericTypeDefinition(String nameSpace) {
287 	    // Attribute Defn for generic type;
288         KrmsAttributeDefinition.Builder genericTypeAttributeDefnBuilder = KrmsAttributeDefinition.Builder.create(null, "Event", nameSpace);
289         genericTypeAttributeDefnBuilder.setLabel("event name");
290         KrmsAttributeDefinition genericTypeAttributeDefinition1 = krmsAttributeDefinitionService.createAttributeDefinition(genericTypeAttributeDefnBuilder.build());
291 
292         // Attr for generic type;
293         KrmsTypeAttribute.Builder genericTypeAttrBuilder = KrmsTypeAttribute.Builder.create(null, genericTypeAttributeDefinition1.getId(), 1);
294 
295 		// Can use this generic type for KRMS bits that don't actually rely on services on the bus at this point in time
296 	    KrmsTypeDefinition.Builder krmsGenericTypeDefnBuilder = KrmsTypeDefinition.Builder.create("KrmsTestGenericType", nameSpace);
297 	    krmsGenericTypeDefnBuilder.setAttributes(Collections.singletonList(genericTypeAttrBuilder));
298 	    KrmsTypeDefinition krmsGenericTypeDefinition = krmsTypeRepositoryService.createKrmsType(krmsGenericTypeDefnBuilder.build());
299 
300         return krmsGenericTypeDefinition;
301     }
302 
303     private RuleDefinition createRuleDefinition1(ContextDefinition contextDefinition, String nameSpace) {
304         // Rule 1
305         RuleDefinition.Builder ruleDefBuilder1 =
306             RuleDefinition.Builder.create(null, "Rule1", nameSpace, createKrmsCampusTypeDefinition(nameSpace).getId(), null);
307         RuleDefinition ruleDef1 = ruleBoService.createRule(ruleDefBuilder1.build());
308 
309 
310         ruleDefBuilder1 = RuleDefinition.Builder.create(ruleDef1);
311         ruleDefBuilder1.setProposition(createCompoundProposition(contextDefinition, ruleDef1));
312         ruleDef1 = ruleDefBuilder1.build();
313         ruleBoService.updateRule(ruleDef1);
314 
315         // Action
316         ActionDefinition.Builder actionDefBuilder1 = ActionDefinition.Builder.create(null, "testAction1", nameSpace, createKrmsActionTypeDefinition(nameSpace).getId(), ruleDef1.getId(), 1);
317         ActionDefinition actionDef1 = actionBoService.createAction(actionDefBuilder1.build());
318 
319         return ruleDef1;
320     }
321     
322 
323     private KrmsTypeDefinition createKrmsCampusTypeDefinition(String nameSpace) {
324 	    // KrmsType for campus svc
325         KrmsTypeDefinition.Builder krmsCampusTypeDefnBuilder = KrmsTypeDefinition.Builder.create("CAMPUS", nameSpace);
326         KrmsTypeDefinition krmsCampusTypeDefinition = krmsTypeRepositoryService.createKrmsType(krmsCampusTypeDefnBuilder.build());
327         return krmsCampusTypeDefinition;
328     }
329 
330     private KrmsTypeDefinition createKrmsActionTypeDefinition(String nameSpace) {
331         KrmsTypeDefinition.Builder krmsActionTypeDefnBuilder = KrmsTypeDefinition.Builder.create("KrmsActionResolverType", nameSpace);
332         krmsActionTypeDefnBuilder.setServiceName("testActionTypeService");
333         KrmsTypeDefinition krmsActionTypeDefinition = krmsTypeRepositoryService.createKrmsType(krmsActionTypeDefnBuilder.build());
334 
335         return krmsActionTypeDefinition;
336     }
337 
338 
339     private EngineResults engineExecute() {
340         Map<String, String> contextQualifiers = new HashMap<String, String>();
341         contextQualifiers.put("name", CONTEXT_NAME);
342         contextQualifiers.put("namespaceCode", KrmsConstants.KRMS_NAMESPACE);
343 
344         SelectionCriteria sc1 = SelectionCriteria.createCriteria(new DateTime(),
345                 contextQualifiers, Collections.singletonMap(AgendaDefinition.Constants.EVENT, EVENT_ATTRIBUTE));
346 
347         Facts.Builder factsBuilder1 = Facts.Builder.create();
348 //        factsBuilder1.addFact(TERM_NAME, 49999);
349         factsBuilder1.addFact(TERM_NAME, "BL");
350 
351         ExecutionOptions xOptions1 = new ExecutionOptions();
352         xOptions1.setFlag(ExecutionFlag.LOG_EXECUTION, true);
353 
354         EngineResults engineResults = KrmsApiServiceLocator.getEngine().execute(sc1, factsBuilder1.build(), xOptions1);
355         assertNotNull(engineResults);
356         assertTrue(engineResults.getAllResults().size() > 0);
357         print(engineResults);
358         return engineResults;
359     }
360 
361     private void print(EngineResults engineResults) {
362         System.out.println(ToStringBuilder.reflectionToString(engineResults, ToStringStyle.MULTI_LINE_STYLE));
363     }
364 
365     private ContextBo createContext() {
366         KrmsAttributeTypeDefinitionAndBuilders defs = createKrmsAttributeTypeDefinitionAndBuilders(
367                 "ContextAttributeName", KrmsConstants.KRMS_NAMESPACE, "ContextLabel", true, "ContextTypeName",
368                 "ContextTypeId", "ContextServiceName", krmsTypeRepositoryService, 1);
369 
370         ContextBo contextBo = new ContextBo();
371         contextBo.setNamespace(KrmsConstants.KRMS_NAMESPACE);
372         contextBo.setName(CONTEXT_NAME);
373         contextBo.setTypeId(defs.typeDef.getId());
374         return (ContextBo)getBoService().save(contextBo);
375     }
376 
377     private KrmsAttributeTypeDefinitionAndBuilders createKrmsAttributeTypeDefinitionAndBuilders(String attributeName,
378             String namespace, String label, boolean active, String typeName, String typeId, String serviceName,
379             KrmsTypeRepositoryService krmsTypeRepositoryService, Integer sequenceNumber) {
380         KrmsAttributeDefinitionBo attributeDefinitionBo = new KrmsAttributeDefinitionBo();
381         attributeDefinitionBo.setNamespace(namespace);
382         attributeDefinitionBo.setName(attributeName);
383         attributeDefinitionBo.setLabel(label);
384         attributeDefinitionBo.setActive(active);
385         attributeDefinitionBo = (KrmsAttributeDefinitionBo)getBoService().save(attributeDefinitionBo);
386         assertNotNull(attributeDefinitionBo.getId());
387         KrmsAttributeDefinition attribDef = KrmsAttributeDefinitionBo.to(attributeDefinitionBo);
388 
389         KrmsTypeDefinition.Builder typeDefinition = KrmsTypeDefinition.Builder.create(
390             typeName, namespace);
391         typeDefinition.setServiceName(serviceName);
392         KrmsTypeAttribute.Builder attribDefinitionBuilder = KrmsTypeAttribute.Builder.create(typeId, attribDef.getId(), sequenceNumber);
393         typeDefinition.getAttributes().add(attribDefinitionBuilder);
394         KrmsTypeDefinition typeDef = krmsTypeRepositoryService.createKrmsType(typeDefinition.build());
395         assertNotNull(typeDef);
396 
397         return new KrmsAttributeTypeDefinitionAndBuilders(attribDef, attribDefinitionBuilder, typeDef, typeDefinition);
398     }
399 
400     private RuleBo createRuleWithAction(KrmsAttributeTypeDefinitionAndBuilders ruleBits,
401             KrmsAttributeTypeDefinitionAndBuilders actionBits, ContextBo contextBo, String message) {
402 
403         RuleBo rule = new RuleBo();
404         rule.setTypeId(ruleBits.typeDef.getId());
405         rule.setNamespace(ruleBits.typeDef.getNamespace());
406         rule.setName(ruleBits.typeDef.getName());
407         List<RuleAttributeBo> ruleAttributes = new ArrayList<RuleAttributeBo>();
408         rule.setAttributeBos(ruleAttributes);
409         RuleAttributeBo ruleType = new RuleAttributeBo();
410         ruleAttributes.add(ruleType);
411         ruleType.setAttributeDefinitionId(ruleBits.attribDef.getId());
412         ruleType.setAttributeDefinition(KrmsAttributeDefinitionBo.from(ruleBits.attribDef));
413         ruleType.setValue(ruleBits.typeAttribBuilder.getTypeId());
414         ruleType.setRuleId(rule.getId());
415 
416         List<ActionBo> actions = new ArrayList<ActionBo>();
417         ActionBo action = new ActionBo();
418         action.setTypeId(actionBits.typeDef.getId());
419         action.setDescription("Description of validation action for message " + message);
420         actions.add(action);
421         action.setNamespace(actionBits.typeDef.getNamespace());
422         action.setName(actionBits.typeDef.getName());
423         action.setSequenceNumber(actionBits.typeAttribBuilder.getSequenceNumber());
424         Set<ActionAttributeBo> actionAttributes = new HashSet<ActionAttributeBo>();
425         action.setAttributeBos(actionAttributes);
426 
427         ActionAttributeBo actionAttribute = new ActionAttributeBo();
428         actionAttributes.add(actionAttribute);
429         actionAttribute.setAttributeDefinitionId(actionBits.attribDef.getId());
430         actionAttribute.setAttributeDefinition(KrmsAttributeDefinitionBo.from(actionBits.attribDef));
431         actionAttribute.setValue(actionBits.typeAttribBuilder.getTypeId());
432 
433         createActionAttributeBo(actionBits.attribDef.getNamespace(), "Action Message", "Action Message", actionBits.attribDef.isActive(), "Action Message", message, actionAttributes);
434 
435         rule = (RuleBo) getBoService().save(rule);
436         RuleDefinition ruleDef = RuleBo.to(rule);
437         
438         PropositionDefinition propDef = createPropositionDefinition1(ContextBo.to(contextBo), ruleDef).build();
439         propDef = propositionBoService.createProposition(propDef);
440         rule.setPropId(propDef.getId());
441         rule.setActions(actions);
442         rule = (RuleBo) getBoService().save(rule);
443 
444         assertNotNull(rule.getId());
445         assertNotNull(rule.getPropId());
446         assertEquals(1, rule.getActions().size());
447         assertNotNull(rule.getActions().get(0).getId());
448         assertEquals(2, rule.getActions().get(0).getAttributeBos().size());
449         return rule;
450     }
451 
452     private void createActionAttributeBo(String namespace, String attributeName, String label, boolean active,
453             String actionAttribDefId, String value, Set<ActionAttributeBo> actionAttributes) {
454         KrmsAttributeDefinitionBo attributeDefinitionBo = new KrmsAttributeDefinitionBo();
455         attributeDefinitionBo.setNamespace(namespace);
456         attributeDefinitionBo.setName(attributeName);
457         attributeDefinitionBo.setLabel(label);
458         attributeDefinitionBo.setActive(active);
459         attributeDefinitionBo = (KrmsAttributeDefinitionBo)getBoService().save(attributeDefinitionBo);
460         assertNotNull(attributeDefinitionBo.getId());
461         KrmsAttributeDefinition attribDef = KrmsAttributeDefinitionBo.to(attributeDefinitionBo);
462 
463         ActionAttributeBo actionAttribute = new ActionAttributeBo();
464         actionAttributes.add(actionAttribute);
465         actionAttribute.setAttributeDefinitionId(attribDef.getId());
466         actionAttribute.setAttributeDefinition(KrmsAttributeDefinitionBo.from(attribDef));
467         actionAttribute.setValue(value);
468     }
469 
470     private PropositionDefinition.Builder createPropositionDefinition1(ContextDefinition contextDefinition, RuleDefinition ruleDef1) {
471         // Proposition for rule 1
472         PropositionDefinition.Builder propositionDefBuilder1 =
473             PropositionDefinition.Builder.create(null, PropositionType.SIMPLE.getCode(), ruleDef1.getId(), null /* type code is only for custom props */, Collections.<PropositionParameter.Builder>emptyList());
474         propositionDefBuilder1.setDescription("is campus bloomington");
475 
476         // PropositionParams for rule 1
477         List<PropositionParameter.Builder> propositionParams1 = new ArrayList<PropositionParameter.Builder>();
478         propositionParams1.add(
479                 PropositionParameter.Builder.create(null, null, createTermDefinition1(contextDefinition).getId(), PropositionParameterType.TERM.getCode(), 1)
480         );
481         propositionParams1.add(
482                 PropositionParameter.Builder.create(null, null, "BL", PropositionParameterType.CONSTANT.getCode(), 2)
483         );
484         propositionParams1.add(
485                 PropositionParameter.Builder.create(null, null, "=", PropositionParameterType.OPERATOR.getCode(), 3)
486         );
487 
488         // set the parent proposition so the builder will not puke
489         for (PropositionParameter.Builder propositionParamBuilder : propositionParams1) {
490             propositionParamBuilder.setProposition(propositionDefBuilder1);
491         }
492 
493         propositionDefBuilder1.setParameters(propositionParams1);
494 
495         return propositionDefBuilder1;
496     }
497 
498 
499     private PropositionDefinition.Builder createCompoundProposition(ContextDefinition contextDefinition,
500             RuleDefinition ruleDef1) {
501         // Proposition for rule 1
502         List<PropositionParameter.Builder> propositionParameterBuilderList = new ArrayList<PropositionParameter.Builder>();
503         propositionParameterBuilderList.add(PropositionParameter.Builder.create(null, null, createTermDefinition1(contextDefinition).getId(),
504                 PropositionParameterType.TERM.getCode(), 1)
505         );
506         PropositionDefinition.Builder propositionDefBuilder1 =
507             PropositionDefinition.Builder.create(null, PropositionType.COMPOUND.getCode(), ruleDef1.getId(), null /* type code is only for custom props */, propositionParameterBuilderList);
508         propositionDefBuilder1.setDescription("propositionDefBuilder1 Description");
509 
510         // PropositionParams for rule 1
511 //        List<PropositionParameter.Builder> propositionParams1 = new ArrayList<PropositionParameter.Builder>();
512 //        propositionParams1.add(
513 //                PropositionParameter.Builder.create(null, null, createTermDefinition1(contextDefinition).getId(), PropositionParameterType
514 //                        .TERM.getCode(), 1)
515 //        );
516 
517         // set the parent proposition so the builder will not puke
518         for (PropositionParameter.Builder propositionParamBuilder : propositionParameterBuilderList) {
519             propositionParamBuilder.setProposition(propositionDefBuilder1);
520         }
521 
522 //        propositionDefBuilder1.setParameters(propositionParams1);
523 
524         return propositionDefBuilder1;
525     }
526 
527     private TermDefinition createTermDefinition1(ContextDefinition contextDefinition) {
528         // campusCode TermSpec
529         TermSpecificationDefinition campusCodeTermSpec =
530             TermSpecificationDefinition.Builder.create(null, "campusCodeTermSpec", contextDefinition.getId(),
531                     "java.lang.String").build();
532         campusCodeTermSpec = termBoService.createTermSpecification(campusCodeTermSpec);
533 
534         // Term 1
535         TermDefinition termDefinition1 =
536             TermDefinition.Builder.create(null, TermSpecificationDefinition.Builder.create(campusCodeTermSpec), null).build();
537         termDefinition1 = termBoService.createTermDefinition(termDefinition1);
538 
539         return termDefinition1;
540     }
541 
542     private TermDefinition createTermDefinitionInteger(ContextDefinition contextDefinition) {
543         // campusCode TermSpec
544         TermSpecificationDefinition termSpec =
545             TermSpecificationDefinition.Builder.create(null, TERM_NAME, contextDefinition.getId(),
546                     "java.lang.Integer").build();
547         termSpec = termBoService.createTermSpecification(termSpec);
548 
549         // Term 1
550         TermDefinition termDefinition1 =
551             TermDefinition.Builder.create(null, TermSpecificationDefinition.Builder.create(termSpec), null).build();
552         termDefinition1 = termBoService.createTermDefinition(termDefinition1);
553 
554         return termDefinition1;
555     }
556 
557 
558     private KrmsAttributeDefinitionBo createEventAttributeDefinition() {
559         KrmsAttributeDefinitionService service = KRMSServiceLocatorInternal.getService("krmsAttributeDefinitionService");
560         assertNotNull(service);
561         KrmsAttributeDefinitionBo attributeDefinitionBo = new KrmsAttributeDefinitionBo();
562         attributeDefinitionBo.setNamespace(KrmsConstants.KRMS_NAMESPACE);
563         attributeDefinitionBo.setName(EVENT_ATTRIBUTE);
564         attributeDefinitionBo.setLabel("Event");
565         attributeDefinitionBo.setActive(true);
566         attributeDefinitionBo = (KrmsAttributeDefinitionBo) getBoService().save(attributeDefinitionBo);
567         assertNotNull(attributeDefinitionBo.getId());
568         return attributeDefinitionBo;
569     }
570 
571     private AgendaBo createAgenda(RuleBo ruleBo, ContextBo contextBo, KrmsAttributeDefinitionBo eventAttributeDefinition) {
572         KrmsAttributeTypeDefinitionAndBuilders defs = createKrmsAttributeTypeDefinitionAndBuilders(
573                 "AgendaAttributeName", KrmsConstants.KRMS_NAMESPACE, "AgendaLabel", true, "AgendaTypeName",
574                 "AgendaTypeId", "AgendaServiceName", krmsTypeRepositoryService, 1);
575 
576         AgendaBo agendaBo = new AgendaBo();
577         agendaBo.setActive(defs.typeDef.isActive());
578         agendaBo.setContextId(contextBo.getId());
579         agendaBo.setName("MyAgenda");
580         agendaBo.setTypeId(defs.typeDef.getId());
581         agendaBo = (AgendaBo)getBoService().save(agendaBo);
582 
583         agendaBo.setFirstItemId(ruleBo.getId());
584         AgendaItemBo agendaItemBo = new AgendaItemBo();
585         agendaItemBo.setRule(ruleBo);
586         agendaItemBo.setAgendaId(agendaBo.getId());
587         agendaItemBo = (AgendaItemBo)getBoService().save(agendaItemBo);
588 
589         List<AgendaItemBo> agendaItems = new ArrayList<AgendaItemBo>();
590         agendaItems.add(agendaItemBo);
591         agendaBo.setItems(agendaItems);
592         agendaBo.setFirstItemId(agendaItemBo.getId());
593 
594         // also add attribute to the agenda to store event
595         Set<AgendaAttributeBo> agendaAttributes = new HashSet<AgendaAttributeBo>();
596         agendaBo.setAttributeBos(agendaAttributes);
597         AgendaAttributeBo agendaAttribute = new AgendaAttributeBo();
598         agendaAttributes.add(agendaAttribute);
599         agendaAttribute.setAttributeDefinitionId(eventAttributeDefinition.getId());
600         agendaAttribute.setAttributeDefinition(eventAttributeDefinition);
601         agendaAttribute.setValue(EVENT_ATTRIBUTE);
602 //        agendaAttribute.setValue("workflow");
603         agendaBo = (AgendaBo)getBoService().save(agendaBo);
604 
605         contextBo.getAgendas().add(agendaBo);
606 
607         return agendaBo;
608     }
609 
610     class KrmsAttributeTypeDefinitionAndBuilders {
611         KrmsTypeDefinition typeDef;
612         KrmsTypeDefinition.Builder typeDefBuilder;
613         KrmsAttributeDefinition attribDef;
614         KrmsTypeAttribute.Builder typeAttribBuilder;
615         KrmsAttributeTypeDefinitionAndBuilders(KrmsAttributeDefinition krmsAttributeDefinition, KrmsTypeAttribute.Builder krmsAttributeDefinitionBuilder,
616                 KrmsTypeDefinition krmsTypeDefinition, KrmsTypeDefinition.Builder krmsTypeDefinitionBuilder) {
617             this.typeDef = krmsTypeDefinition;
618             this.typeDefBuilder = krmsTypeDefinitionBuilder;
619             this.attribDef = krmsAttributeDefinition;
620             this.typeAttribBuilder = krmsAttributeDefinitionBuilder;
621         }
622     }
623 }