1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.student.krms;
17
18 import org.junit.Before;
19 import org.junit.Ignore;
20 import org.junit.Test;
21 import org.kuali.rice.core.api.lifecycle.Lifecycle;
22 import org.kuali.rice.core.framework.resourceloader.SpringResourceLoader;
23 import org.kuali.rice.krad.service.BusinessObjectService;
24 import org.kuali.rice.krad.service.KRADServiceLocator;
25 import org.kuali.rice.krms.api.KrmsApiServiceLocator;
26 import org.kuali.rice.krms.api.engine.*;
27 import org.kuali.rice.krms.api.repository.agenda.AgendaDefinition;
28 import org.kuali.rice.krms.api.repository.context.ContextDefinition;
29 import org.kuali.rice.krms.api.repository.term.TermDefinition;
30 import org.kuali.rice.krms.api.repository.type.KrmsTypeRepositoryService;
31 import org.kuali.rice.krms.framework.engine.BasicRule;
32 import org.kuali.rice.krms.framework.type.ValidationActionTypeService;
33 import org.kuali.rice.krms.impl.repository.*;
34 import org.kuali.rice.test.BaselineTestCase.BaselineMode;
35 import org.kuali.rice.test.BaselineTestCase.Mode;
36
37 import javax.xml.namespace.QName;
38 import java.util.HashMap;
39 import java.util.List;
40 import java.util.Map;
41
42 import static junit.framework.Assert.assertNotNull;
43
44 @Ignore
45 public class TestKRMSAgendasExecution extends KSKRMSTestCase {
46
47 public TestKRMSAgendasExecution() {
48 super();
49 this.setClearTables(false);
50 }
51
52 static final String KSNAMESPACE = "KR-RULE-TEST";
53 protected ContextBoService contextRepository;
54 protected KrmsTypeRepositoryService krmsTypeRepository;
55 private AgendaBoService agendaBoService;
56 private RuleBoService ruleBoService;
57 private FunctionBoServiceImpl functionBoService;
58
59
60 private TermBoService termBoService;
61 private SpringResourceLoader krmsTestResourceLoader;
62
63
64 @Before
65 public void setup() {
66 getLoadApplicationLifecycle();
67 termBoService = KrmsRepositoryServiceLocator.getTermBoService();
68 agendaBoService = KrmsRepositoryServiceLocator.getAgendaBoService();
69 contextRepository = KrmsRepositoryServiceLocator.getContextBoService();
70 ruleBoService = KrmsRepositoryServiceLocator.getRuleBoService();
71 krmsTypeRepository = KrmsRepositoryServiceLocator
72 .getKrmsTypeRepositoryService();
73 functionBoService = KrmsRepositoryServiceLocator
74 .getBean("functionRepositoryService");
75
76 }
77
78 @Override
79 protected List<String> getPerTestTablesNotToClear() {
80 List<String> tablesNotToClear = super.getPerTestTablesNotToClear();
81
82 return tablesNotToClear;
83 }
84
85 @Override
86 protected Lifecycle getLoadApplicationLifecycle() {
87 if (krmsTestResourceLoader == null) {
88 krmsTestResourceLoader = new SpringResourceLoader(new QName(
89 "KRMSTestHarnessApplicationResourceLoader"),
90 "classpath:KRMSTestHarnessSpringBeans.xml", null);
91 krmsTestResourceLoader
92 .setParentSpringResourceLoader(getTestHarnessSpringResourceLoader());
93 getTestHarnessSpringResourceLoader().addResourceLoader(
94 krmsTestResourceLoader);
95 }
96 return krmsTestResourceLoader;
97 }
98
99 public TermDefinition krmsTermLookup(String termName) {
100
101 Map<String, String> queryArgs = new HashMap<String, String>();
102 queryArgs.put("desc_txt", termName);
103 TermBo termBo = getBoService()
104 .findByPrimaryKey(TermBo.class, queryArgs);
105 if (termBo != null) {
106 return TermBo.to(termBo);
107 }
108 return null;
109 }
110
111 protected BusinessObjectService getBoService() {
112 return KRADServiceLocator.getBusinessObjectService();
113 }
114
115 @Test
116 public void testHello() {
117 System.out.println("Test hello");
118 }
119
120 @Test
121 public void testAllRulesKSAgenda() {
122
123
124
125
126
127
128
129
130
131
132 ContextDefinition contextDef = getKRMSContext(KSKRMSConstants.CONTEXT_STUD_ELIGIBILITY);
133 AgendaDefinition adgendaDef = getKRMSAgenda(KSKRMSConstants.AGENDA1, contextDef);
134 assertNotNull(contextDef);
135 assertNotNull(adgendaDef);
136
137
138
139
140
141
142
143 }
144
145 public AgendaDefinition getKRMSAgenda(String agendaName, ContextDefinition contextDef) {
146 AgendaDefinition agendaDef = agendaBoService
147 .getAgendaByNameAndContextId(agendaName,
148 contextDef.getId());
149 return agendaDef;
150 }
151
152 public ContextDefinition getKRMSContext(String context) {
153 return contextRepository.getContextByNameAndNamespace(
154 context, KSKRMSConstants.KSNAMESPACE);
155 }
156
157
158 public void populateContextQualifiers(Map<String, String> qualifiers) {
159 qualifiers.put("namespaceCode", KSKRMSConstants.KSNAMESPACE);
160 qualifiers.put("name", KSKRMSConstants.CONTEXT_STUD_ELIGIBILITY);
161 }
162
163 public void populateAgendaQualifiers(Map<String,String> agendaQualifiers) {
164 agendaQualifiers.put("name", KSKRMSConstants.AGENDA1);
165 }
166
167 public void addFacts(Facts.Builder factsBuilder) {
168
169
170 }
171
172
173 @Test
174 public void loadAndisRuleValid() {
175
176
177 Map <String, Boolean> ruleResults = new HashMap<String, Boolean>();
178
179
180 Map<String, String> contextQualifiers = new HashMap<String, String>();
181 populateContextQualifiers(contextQualifiers);
182
183 Map<String,String> agendaQualifiers = new HashMap<String,String>();
184
185
186 String namespace = null;
187 String contextKey = null;
188
189
190
191
192
193
194
195
196
197
198 SelectionCriteria selectionCriteria = SelectionCriteria.createCriteria(null, contextQualifiers, agendaQualifiers);
199
200 Facts.Builder factsBuilder = Facts.Builder.create();
201
202 addFacts(factsBuilder);
203
204 ExecutionOptions xOptions = new ExecutionOptions();
205 xOptions.setFlag(ExecutionFlag.LOG_EXECUTION, true);
206
207 EngineResults results = KrmsApiServiceLocator.getEngine().execute(selectionCriteria, factsBuilder.build(), xOptions);
208
209 String errors = (String) results.getAttribute(ValidationActionTypeService.VALIDATIONS_ACTION_ATTRIBUTE);
210 boolean isValid = false;
211 if (results.getResultsOfType(ResultEvent.RULE_EVALUATED) != null && results.getResultsOfType(ResultEvent.RULE_EVALUATED).size() > 0) {
212
213 for (ResultEvent resultEvent : results.getResultsOfType(ResultEvent.RULE_EVALUATED)) {
214 ruleResults.put(((BasicRule)resultEvent.getSource()).getName(), resultEvent.getResult());
215 System.out.println(resultEvent.getDescription() + ' ' + resultEvent.getResult());
216
217
218
219 }
220
221
222
223
224 }
225
226
227
228
229
230
231
232
233
234
235 }
236 }