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