View Javadoc

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