1   
2   
3   
4   
5   package org.kuali.rice.krms.impl.repository.mock;
6   
7   import java.util.ArrayList;
8   import java.util.Date;
9   import java.util.LinkedHashMap;
10  import java.util.LinkedHashSet;
11  import java.util.List;
12  import java.util.Map;
13  import java.util.Set;
14  import org.junit.After;
15  import org.junit.AfterClass;
16  import org.junit.Before;
17  import org.junit.BeforeClass;
18  import org.junit.Test;
19  import static org.junit.Assert.*;
20  import org.junit.Ignore;
21  import org.kuali.rice.krms.api.repository.RuleManagementService;
22  import org.kuali.rice.krms.api.repository.language.NaturalLanguageTemplate;
23  import org.kuali.rice.krms.api.repository.language.NaturalLanguageUsage;
24  import org.kuali.rice.krms.api.repository.proposition.PropositionParameterType;
25  import org.kuali.rice.krms.api.repository.term.TermRepositoryService;
26  import org.kuali.rice.krms.api.repository.term.TermResolverDefinition;
27  import org.kuali.rice.krms.api.repository.term.TermSpecificationDefinition;
28  import org.kuali.rice.krms.api.repository.type.KrmsTypeDefinition;
29  import org.kuali.rice.krms.api.repository.type.KrmsTypeRepositoryService;
30  import org.kuali.rice.krms.impl.repository.language.SimpleNaturalLanguageTemplater;
31  import org.kuali.student.r2.common.dto.ContextInfo;
32  import org.kuali.student.r2.core.constants.KSKRMSServiceConstants;
33  import org.kuali.student.r2.core.versionmanagement.dto.VersionDisplayInfo;
34  import org.kuali.student.r2.lum.clu.dto.CluInfo;
35  import org.kuali.student.r2.lum.clu.dto.CluSetInfo;
36  import org.kuali.student.r2.lum.clu.service.CluService;
37  import org.kuali.student.r2.lum.lu.service.impl.CluDataLoader;
38  import org.kuali.student.r2.lum.lu.service.impl.CluServiceMockImpl;
39  import org.kuali.student.r2.lum.util.constants.CluServiceConstants;
40  
41  
42  
43  
44  
45  @Ignore
46  public class RuleManagementServiceMockImplTest {
47  
48      public RuleManagementServiceMockImplTest() {
49      }
50      private KrmsTypeRepositoryService krmsTypeRepositoryService = null;
51      private RuleManagementService ruleManagementService = null;
52      private TermRepositoryService termRepositoryService = null;
53      private CluService cluService = null;
54  
55      @BeforeClass
56      public static void setUpClass() {
57      }
58  
59      @AfterClass
60      public static void tearDownClass() {
61      }
62      private ContextInfo contextInfo;
63  
64      @Before
65      public void setUp() {
66          contextInfo = new ContextInfo();
67          contextInfo.setPrincipalId("TESTUSER");
68          contextInfo.setCurrentDate(new Date());
69  
70          this.krmsTypeRepositoryService = new KrmsTypeRepositoryServiceMockImpl();
71          this.termRepositoryService = new TermRepositoryServiceMockImpl();
72          this.ruleManagementService = new RuleManagementServiceMockImpl();
73          ((RuleManagementServiceMockImpl) this.ruleManagementService).setTemplater(new SimpleNaturalLanguageTemplater());
74          ((RuleManagementServiceMockImpl) this.ruleManagementService).setTermRepositoryService(termRepositoryService);
75          KrmsConfigurationLoader loader = new KrmsConfigurationLoader();
76          loader.setKrmsTypeRepositoryService(this.krmsTypeRepositoryService);
77          loader.setRuleManagementService(this.ruleManagementService);
78          loader.setTermRepositoryService(this.termRepositoryService);
79          loader.loadConfiguration();
80  
81          this.cluService = new CluServiceMockImpl();
82          CluDataLoader cluDataLoader = new CluDataLoader();
83          cluDataLoader.setCluService(cluService);
84          cluDataLoader.setContextInfo(contextInfo);
85          cluDataLoader.load();
86  
87      }
88  
89      @After
90      public void tearDown() {
91      }
92  
93      @Test
94      public void testStadardAuthoringUsageCase() {
95          System.out.println("testStadardAuthoringUsageCase");
96  
97          List<KrmsTypeDefinition> types = null;
98          Set<String> expected = null;
99          String languageCode = KSKRMSServiceConstants.LANGUAGE_CODE_ENGLISH;
100 
101         
102         types = this.krmsTypeRepositoryService.findAllContextTypes();
103         expected = new LinkedHashSet<String>();
104         expected.add(KSKRMSServiceConstants.AGENDA_TYPE_COURSE);
105         expected.add(KSKRMSServiceConstants.AGENDA_TYPE_PROGRAM);
106 
107         this.checkTypeNamesAnyOrder(expected, types);
108 
109         
110         
111         
112         System.out.println("Please choose which context of rules that you want to work on:");
113         for (KrmsTypeDefinition type : types) {
114             System.out.println("     " + this.getScreenDescription(type.getId(), languageCode));
115         }
116         System.out.print("==> Choose: ");
117         String selectedId = this.simulateUserChoosingMainTypeId();
118         System.out.println(selectedId);
119         KrmsTypeDefinition selectedContextType = this.krmsTypeRepositoryService.getTypeById(selectedId);
120         String description = this.getScreenDescription(selectedContextType.getId(), languageCode);
121         System.out.println("Editing Rules for Context: " + description);
122 
123 
124         
125         types = this.krmsTypeRepositoryService.findAgendaTypesForContextType(selectedContextType.getId());
126         expected = new LinkedHashSet<String>();
127         expected.add(KSKRMSServiceConstants.AGENDA_TYPE_COURSE_ENROLLMENTELIGIBILITY);
128         expected.add(KSKRMSServiceConstants.AGENDA_TYPE_COURSE_CREDITCONSTRAINTS);
129         this.checkTypeNamesOrdered(expected, types);
130 
131         String title = "Please choose which type of rule you want to work on:";
132         int defaultIndex = 0;
133         KrmsTypeDefinition courseEligAgendaType = this.simulateUserChoosingType(title, types, languageCode, defaultIndex);
134 
135         
136         
137         types = this.krmsTypeRepositoryService.findAgendaTypesForAgendaType(courseEligAgendaType.getId());
138         expected = new LinkedHashSet<String>();
139         this.checkTypeNamesOrdered(expected, types);
140 
141         
142         types = this.krmsTypeRepositoryService.findRuleTypesForAgendaType(courseEligAgendaType.getId());
143         expected = new LinkedHashSet<String>();
144         expected.add(KSKRMSServiceConstants.RULE_TYPE_COURSE_ACADEMICREADINESS_STUDENTELIGIBILITYPREREQ);
145         expected.add(KSKRMSServiceConstants.RULE_TYPE_COURSE_ACADEMICREADINESS_COREQ);
146         expected.add(KSKRMSServiceConstants.RULE_TYPE_COURSE_RECOMMENDEDPREPARATION);
147         expected.add(KSKRMSServiceConstants.RULE_TYPE_COURSE_ACADEMICREADINESS_ANTIREQ);
148         this.checkTypeNamesOrdered(expected, types);
149 
150         title = "Please choose which type of rule you want to work on:";
151         defaultIndex = 0;
152         KrmsTypeDefinition eligPrereqRuleType = this.simulateUserChoosingType(title, types, languageCode, defaultIndex);
153 
154         
155         types = this.krmsTypeRepositoryService.findPropositionTypesForRuleType(eligPrereqRuleType.getId());
156         expected = new LinkedHashSet<String>();
157         expected.add(KSKRMSServiceConstants.PROPOSITION_TYPE_FREEFORM_TEXT);
158         expected.add(KSKRMSServiceConstants.PROPOSITION_TYPE_SUCCESS_COMPL_COURSE);
159         expected.add(KSKRMSServiceConstants.PROPOSITION_TYPE_CUMULATIVE_GPA_MIN);
160         expected.add(KSKRMSServiceConstants.PROPOSITION_TYPE_ADMITTED_TO_PROGRAM);
161         expected.add(KSKRMSServiceConstants.PROPOSITION_TYPE_SUCCESS_CREDIT_COURSESET_COMPLETED_NOF);
162         expected.add(KSKRMSServiceConstants.PROPOSITION_TYPE_SUCCESS_CREDITS_COURSESET_COMPLETED_NOF_ORG);
163         expected.add(KSKRMSServiceConstants.PROPOSITION_TYPE_SUCCESS_COURSE_COURSESET_COMPLETED_ALL);
164         expected.add(KSKRMSServiceConstants.PROPOSITION_TYPE_SUCCESS_COURSE_COURSESET_COMPLETED_NOF);
165         expected.add(KSKRMSServiceConstants.PROPOSITION_TYPE_COURSE_COURSESET_GPA_MIN);
166         expected.add(KSKRMSServiceConstants.PROPOSITION_TYPE_COURSE_COURSESET_GRADE_MIN);
167         expected.add(KSKRMSServiceConstants.PROPOSITION_TYPE_COURSE_COURSESET_NOF_GRADE_MIN);
168         expected.add(KSKRMSServiceConstants.PROPOSITION_TYPE_COURSE_COURSESET_GRADE_MAX);
169         expected.add(KSKRMSServiceConstants.PROPOSITION_TYPE_ADMITTED_TO_PROGRAM_CAMPUS);
170         expected.add(KSKRMSServiceConstants.PROPOSITION_TYPE_NOTADMITTED_TO_PROGRAM);
171         expected.add(KSKRMSServiceConstants.PROPOSITION_TYPE_PERMISSION_INSTRUCTOR_REQUIRED);
172         expected.add(KSKRMSServiceConstants.PROPOSITION_TYPE_PERMISSION_ADMIN_ORG);
173         expected.add(KSKRMSServiceConstants.PROPOSITION_TYPE_COURSE_TEST_SCORE_MIN);
174         expected.add(KSKRMSServiceConstants.PROPOSITION_TYPE_TEST_SCORE_BETWEEN_VALUES);
175         expected.add(KSKRMSServiceConstants.PROPOSITION_TYPE_TEST_SCORE);
176         this.checkTypeNamesOrdered(expected, types);
177 
178         title = "Please choose which type of rule you want to work on:";
179         defaultIndex = 7;
180         KrmsTypeDefinition nOfCoursesPropositionType = this.simulateUserChoosingType(title, types, languageCode, defaultIndex);
181         NaturalLanguageTemplate nlTemplate = this.getRuleEditUsageNaturalLanguageTemplate(nOfCoursesPropositionType.getId(), languageCode);
182         assertNotNull(nlTemplate);
183         assertEquals("#if($intValue == 1 && $courseCluSet.getCluList().size() == 1)Must have successfully completed $courseCluSet.getCluSetAsCode()#{else}Must have successfully completed a minimum of $intValue $NLHelper.getProperGrammar($intValue, \"course\") from $courseCluSet.getCluSetAsCode()#end", nlTemplate.getTemplate());
184 
185 
186 
187 
188 
189 
190 
191 
192 
193 
194 
195         
196         types = this.krmsTypeRepositoryService.findPropositionParameterTypesForPropositionType(nOfCoursesPropositionType.getId());
197         expected = new LinkedHashSet<String>();
198         
199 
200 
201 
202         this.checkTypeNamesOrdered(expected, types);
203 
204         String termName = "NumberOfCompletedCourses";
205         String constantValue = "1";
206         String operator = "<=";
207 
208 
209         TermSpecificationDefinition termSpec =
210                 this.termRepositoryService.getTermSpecificationByNameAndNamespace(termName,
211                         KSKRMSServiceConstants.NAMESPACE_CODE);
212         assertNotNull(termSpec);
213         assertEquals (termName, termSpec.getName());
214 
215         TermResolverDefinition termResolver =
216                 this.termRepositoryService.getTermResolverByNameAndNamespace(termName,
217                         KSKRMSServiceConstants.NAMESPACE_CODE);
218         assertNotNull(termResolver);
219         assertEquals (termName, termResolver.getName());
220 
221         
222         
223         
224 
225 
226 
227 
228 
229 
230 
231 
232 
233 
234 
235 
236 
237 
238 
239 
240 
241 
242 
243 
244 
245 
246 
247 
248 
249 
250 
251 
252 
253 
254 
255 
256 
257 
258 
259 
260 
261 
262 
263 
264 
265 
266 
267 
268 
269 
270 
271 
272 
273 
274 
275 
276 
277 
278 
279 
280 
281 
282 
283 
284 
285 
286 
287 
288 
289 
290 
291 
292 
293 
294 
295 
296 
297 
298 
299 
300 
301 
302 
303 
304 
305 
306 
307 
308 
309 
310 
311 
312 
313 
314 
315 
316 
317 
318 
319 
320 
321 
322 
323 
324 
325 
326 
327 
328 
329 
330 
331 
332 
333 
334 
335 
336 
337 
338 
339 
340 
341 
342 
343 
344 
345 
346 
347 
348 
349 
350 
351 
352 
353 
354 
355     }
356 
357     
358     
359     
360     
361     
362     private PropositionParameterType serviceName2PropositionParameterType(String serviceTypeName) {
363         if (serviceTypeName.equals(KrmsTypeRepositoryService.TERM_PROPOSITION_PARAMETER_SERVICE_NAME)) {
364             return PropositionParameterType.TERM;
365         }
366         if (serviceTypeName.equals(KrmsTypeRepositoryService.CONSTANT_VALUE_PROPOSITION_PARAMETER_SERVICE_NAME)) {
367             return PropositionParameterType.CONSTANT;
368         }
369         if (serviceTypeName.equals(KrmsTypeRepositoryService.OPERATOR_PROPOSITION_PARAMETER_SERVICE_NAME)) {
370             return PropositionParameterType.OPERATOR;
371         }
372         if (serviceTypeName.equals(KrmsTypeRepositoryService.FUNCTION_PROPOSITION_PARAMETER_SERVICE_NAME)) {
373             return PropositionParameterType.FUNCTION;
374         }
375         throw new IllegalArgumentException(serviceTypeName);
376     }
377 
378     public CluSetInfo createCourseSet(String name, List<String> versionIndIds) {
379         CluSetInfo cluSetInfo = new CluSetInfo();
380         cluSetInfo.setTypeKey(CluServiceConstants.CLUSET_TYPE_CREDIT_COURSE);
381         cluSetInfo.setStateKey("Active");
382         cluSetInfo.setName(name);
383         cluSetInfo.setEffectiveDate(new Date());
384         cluSetInfo.setIsReferenceable(Boolean.TRUE);
385         cluSetInfo.setIsReusable(Boolean.FALSE);
386         cluSetInfo.setCluIds(versionIndIds);
387         try {
388             cluSetInfo = this.cluService.createCluSet(cluSetInfo.getTypeKey(), cluSetInfo, contextInfo);
389         } catch (Exception ex) {
390             throw new IllegalArgumentException(ex);
391         }
392         return cluSetInfo;
393     }
394 
395     private List<String> getVersionIndIds(CluInfo... clus) {
396         List<String> list = new ArrayList<String>();
397         for (CluInfo clu : clus) {
398             list.add(clu.getVersion().getVersionIndId());
399         }
400         return list;
401     }
402 
403     private CluInfo getClu(String id) {
404         try {
405             return this.cluService.getClu(id, contextInfo);
406         } catch (Exception ex) {
407             throw new RuntimeException(ex);
408         }
409     }
410     
411     private transient Map<String, NaturalLanguageUsage> name2NaturalLanguageUsageCache = null;
412 
413     private NaturalLanguageUsage getNaturalLanguageUsage(String name, String namespace) {
414         if (name2NaturalLanguageUsageCache == null) {
415             name2NaturalLanguageUsageCache = new LinkedHashMap<String, NaturalLanguageUsage>();
416         }
417         String key = namespace + ":" + name;
418         NaturalLanguageUsage usage = name2NaturalLanguageUsageCache.get(key);
419         if (usage != null) {
420             return usage;
421         }
422         
423         usage = this.ruleManagementService.getNaturalLanguageUsageByNameAndNamespace(name, namespace);
424         assertNotNull(usage);
425         assertEquals(name, usage.getName());
426         assertEquals(namespace, usage.getNamespace());
427         name2NaturalLanguageUsageCache.put(key, usage);
428         return usage;
429     }
430 
431     private NaturalLanguageUsage getTypeDescriptionUsage() {
432         return this.getNaturalLanguageUsage(KSKRMSServiceConstants.KRMS_NL_TYPE_DESCRIPTION, KSKRMSServiceConstants.NAMESPACE_CODE);
433     }
434 
435     private NaturalLanguageUsage getRuleEditUsage() {
436         return this.getNaturalLanguageUsage(KSKRMSServiceConstants.KRMS_NL_RULE_EDIT, KSKRMSServiceConstants.NAMESPACE_CODE);
437     }
438 
439     private NaturalLanguageUsage getPreviewUsage() {
440         return this.getNaturalLanguageUsage(KSKRMSServiceConstants.KRMS_NL_PREVIEW, KSKRMSServiceConstants.NAMESPACE_CODE);
441     }
442 
443     private String simulateUserChoosingMainTypeId() {
444         KrmsTypeDefinition type = this.krmsTypeRepositoryService.getTypeByName(KSKRMSServiceConstants.NAMESPACE_CODE, KSKRMSServiceConstants.AGENDA_TYPE_COURSE);
445         assertNotNull(type);
446         assertEquals(KSKRMSServiceConstants.AGENDA_TYPE_COURSE, type.getName());
447         assertEquals(KSKRMSServiceConstants.NAMESPACE_CODE, type.getNamespace());
448         return type.getId();
449     }
450 
451     private KrmsTypeDefinition simulateUserChoosingType(String title, List<KrmsTypeDefinition> types, String languageCode, int defaultIndex) {
452         System.out.println(title);
453         this.displayScreenDescriptions(types, languageCode);
454         System.out.print("==> Choose: ");
455         KrmsTypeDefinition selected = types.get(defaultIndex);
456         System.out.println(selected.getName());
457         return selected;
458     }
459 
460     private void displayScreenDescriptions(List<KrmsTypeDefinition> types, String languageCode) {
461         for (KrmsTypeDefinition type : types) {
462             System.out.println(this.getScreenDescription(type.getId(), languageCode));
463         }
464     }
465 
466     private String getScreenDescription(String typeId, String languageCode) {
467         
468         NaturalLanguageTemplate template =
469                 this.ruleManagementService.findNaturalLanguageTemplateByLanguageCodeTypeIdAndNluId(languageCode,
470                 typeId,
471                 this.getTypeDescriptionUsage().getId());
472         if (template == null) {
473             System.out.println("could not find template for " + typeId + " with language " + languageCode + " and for type description usage");
474         }
475         assertNotNull(template);
476         assertEquals(languageCode, template.getLanguageCode());
477         assertEquals(typeId, template.getTypeId());
478         assertEquals(getTypeDescriptionUsage().getId(), template.getNaturalLanguageUsageId());
479         return template.getTemplate();
480     }
481 
482     private String getComponentId(NaturalLanguageTemplate template) {
483         return template.getAttributes().get(KSKRMSServiceConstants.ATTRIBUTE_COMPONENT_ID);
484     }
485 
486     private String getComponentBuilderClass(NaturalLanguageTemplate template) {
487         return template.getAttributes().get(KSKRMSServiceConstants.ATTRIBUTE_COMPONENT_BUILDER_CLASS);
488     }
489 
490     private NaturalLanguageTemplate getRuleEditUsageNaturalLanguageTemplate(String typeId, String languageCode) {
491         
492         NaturalLanguageTemplate template =
493                 this.ruleManagementService.findNaturalLanguageTemplateByLanguageCodeTypeIdAndNluId(languageCode,
494                 typeId,
495                 this.getRuleEditUsage().getId());
496         if (template == null) {
497             System.out.println("could not find template for " + typeId + " with language " + languageCode + " and for rule edit usage");
498         }
499         assertNotNull(template);
500         assertEquals(languageCode, template.getLanguageCode());
501         assertEquals(typeId, template.getTypeId());
502         assertEquals(getRuleEditUsage().getId(), template.getNaturalLanguageUsageId());
503         return template;
504     }
505 
506     private void checkTypeNamesAnyOrder(Set<String> expected, List<KrmsTypeDefinition> types) {
507         List<String> unexpected = new ArrayList<String>();
508         for (KrmsTypeDefinition type : types) {
509             if (!expected.remove(type.getName())) {
510                 unexpected.add(type.getName());
511             }
512         }
513         if (!expected.isEmpty() || !unexpected.isEmpty()) {
514             fail(expected.size() + " types expected that were not found " + expected
515                     + " and "
516                     + unexpected.size() + " types were found but not expected " + unexpected);
517         }
518     }
519 
520     private void checkTypeNamesOrdered(Set<String> expected, List<KrmsTypeDefinition> types) {
521         List<String> expectedOrdered = new ArrayList(expected);
522         this.checkTypeNamesAnyOrder(expected, types);
523         for (int i = 0; i < types.size(); i++) {
524             if (!expectedOrdered.get(i).equals(types.get(i).getName())) {
525                 fail("Expected " + i + "th position to have " + expectedOrdered.get(i) + " but found " + types.get(i).getName());
526             }
527         }
528     }
529 
530     private CluInfo getCurrentCluInfoByVersionIndId(String id, ContextInfo contextInfo) {
531         VersionDisplayInfo versionDisplayInfo = null;
532         try {
533             versionDisplayInfo = this.cluService.getCurrentVersion(CluServiceConstants.CLU_NAMESPACE_URI, id, contextInfo);
534         } catch (Exception ex) {
535             throw new IllegalArgumentException("Unexpected", ex);
536         }
537         try {
538             return this.cluService.getClu(versionDisplayInfo.getId(), contextInfo);
539         } catch (Exception ex) {
540             throw new IllegalArgumentException("Unexpected", ex);
541         }
542     }
543 }