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