001/*
002 * To change this template, choose Tools | Templates
003 * and open the template in the editor.
004 */
005package org.kuali.rice.krms.impl.repository.mock;
006
007import java.util.ArrayList;
008import java.util.Date;
009import java.util.LinkedHashMap;
010import java.util.LinkedHashSet;
011import java.util.List;
012import java.util.Map;
013import java.util.Set;
014import org.junit.After;
015import org.junit.AfterClass;
016import org.junit.Before;
017import org.junit.BeforeClass;
018import org.junit.Test;
019import static org.junit.Assert.*;
020import org.junit.Ignore;
021import org.kuali.rice.krms.api.repository.RuleManagementService;
022import org.kuali.rice.krms.api.repository.language.NaturalLanguageTemplate;
023import org.kuali.rice.krms.api.repository.language.NaturalLanguageUsage;
024import org.kuali.rice.krms.api.repository.proposition.PropositionParameterType;
025import org.kuali.rice.krms.api.repository.term.TermRepositoryService;
026import org.kuali.rice.krms.api.repository.term.TermResolverDefinition;
027import org.kuali.rice.krms.api.repository.term.TermSpecificationDefinition;
028import org.kuali.rice.krms.api.repository.type.KrmsTypeDefinition;
029import org.kuali.rice.krms.api.repository.type.KrmsTypeRepositoryService;
030import org.kuali.rice.krms.impl.repository.language.SimpleNaturalLanguageTemplater;
031import org.kuali.student.r2.common.dto.ContextInfo;
032import org.kuali.student.r2.core.constants.KSKRMSServiceConstants;
033import org.kuali.student.r2.core.versionmanagement.dto.VersionDisplayInfo;
034import org.kuali.student.r2.lum.clu.dto.CluInfo;
035import org.kuali.student.r2.lum.clu.dto.CluSetInfo;
036import org.kuali.student.r2.lum.clu.service.CluService;
037import org.kuali.student.r2.lum.lu.service.impl.CluDataLoader;
038import org.kuali.student.r2.lum.lu.service.impl.CluServiceMockImpl;
039import org.kuali.student.r2.lum.util.constants.CluServiceConstants;
040
041/**
042 *
043 * @author nwright
044 */
045@Ignore
046public class RuleManagementServiceMockImplTest {
047
048    public RuleManagementServiceMockImplTest() {
049    }
050    private KrmsTypeRepositoryService krmsTypeRepositoryService = null;
051    private RuleManagementService ruleManagementService = null;
052    private TermRepositoryService termRepositoryService = null;
053    private CluService cluService = null;
054
055    @BeforeClass
056    public static void setUpClass() {
057    }
058
059    @AfterClass
060    public static void tearDownClass() {
061    }
062    private ContextInfo contextInfo;
063
064    @Before
065    public void setUp() {
066        contextInfo = new ContextInfo();
067        contextInfo.setPrincipalId("TESTUSER");
068        contextInfo.setCurrentDate(new Date());
069
070        this.krmsTypeRepositoryService = new KrmsTypeRepositoryServiceMockImpl();
071        this.termRepositoryService = new TermRepositoryServiceMockImpl();
072        this.ruleManagementService = new RuleManagementServiceMockImpl();
073        ((RuleManagementServiceMockImpl) this.ruleManagementService).setTemplater(new SimpleNaturalLanguageTemplater());
074        ((RuleManagementServiceMockImpl) this.ruleManagementService).setTermRepositoryService(termRepositoryService);
075        KrmsConfigurationLoader loader = new KrmsConfigurationLoader();
076        loader.setKrmsTypeRepositoryService(this.krmsTypeRepositoryService);
077        loader.setRuleManagementService(this.ruleManagementService);
078        loader.setTermRepositoryService(this.termRepositoryService);
079        loader.loadConfiguration();
080
081        this.cluService = new CluServiceMockImpl();
082        CluDataLoader cluDataLoader = new CluDataLoader();
083        cluDataLoader.setCluService(cluService);
084        cluDataLoader.setContextInfo(contextInfo);
085        cluDataLoader.load();
086
087    }
088
089    @After
090    public void tearDown() {
091    }
092
093    @Test
094    public void testStadardAuthoringUsageCase() {
095        System.out.println("testStadardAuthoringUsageCase");
096
097        List<KrmsTypeDefinition> types = null;
098        Set<String> expected = null;
099        String languageCode = KSKRMSServiceConstants.LANGUAGE_CODE_ENGLISH;
100
101        // check that we can get all the different context types
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//        expected.add(KsKrmsConstants.CONTEXT_TYPE_COURSE_OFFERING);
107        this.checkTypeNamesAnyOrder(expected, types);
108
109        // Typically the user does not actually select the context but it is
110        // hardwired into the program, so the "context" is taken from where 
111        // we are within the application, i.e. are we on the course requisites screen?
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        // Get all the agenda types for this course context type
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        // Get all the agenda types for the main agenda type
136        // Right now we don't do this we just have rule types so this should return an empty list
137        types = this.krmsTypeRepositoryService.findAgendaTypesForAgendaType(courseEligAgendaType.getId());
138        expected = new LinkedHashSet<String>();
139        this.checkTypeNamesOrdered(expected, types);
140
141        // Get all the RULE types for the main agenda type
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        // Get all the Proposition types for the rule type
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//                <entry key="kuali.krms.proposition.type.success.course.courseset.completed.nof">
186//                    <bean parent="TemplateInfo-parent"
187//                          p:termSpecName="NumberOfCompletedCourses" p:operator="&lt;=" p:value="n">
188//                        <property name="componentId" value="KRMS-MultiCourse-Section"/>
189//                        <property name="constantComponentId" value="KRMS-NumberOfCourses-ConstantValue"/>
190//                        <property name="componentBuilderClass" value="org.kuali.student.lum.lu.ui.krms.builder.MultiCourseComponentBuilder"/>
191//                    </bean>
192//                </entry>
193
194
195        // Get all the parameter types for the proposition type
196        types = this.krmsTypeRepositoryService.findPropositionParameterTypesForPropositionType(nOfCoursesPropositionType.getId());
197        expected = new LinkedHashSet<String>();
198        // for now we are not defining types for parameters
199//        expected.add(KsKrmsConstants.PROPOSITION_PARAMETER_TYPE_TERM_NUMBER_OF_COMPLETED_COURSES);
200//        expected.add(KsKrmsConstants.PROPOSITION_PARAMETER_TYPE_CONSTANT_VALUE_N);
201//        expected.add(KsKrmsConstants.PROPOSITION_PARAMETER_TYPE_OPERATOR_LESS_THAN_OR_EQUAL_TO);
202        this.checkTypeNamesOrdered(expected, types);
203
204        String termName = "NumberOfCompletedCourses";
205        String constantValue = "1";
206        String operator = "<=";
207
208//      TermSpec 
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        // ok now actually start creating rule
223        //
224
225//
226//        // this is the course to which we are going to attach the agenda
227//        // NOTE: this will be a COURSE not a CLU but I don't want to mock the course impl
228//        CluInfo anchorClu = this.getClu("COURSE1");
229//
230//        // find/create the context corresponding to this context type.
231//        // TODO: Confirm convention that we are creating a single context for all rules of that particular context type, right?
232//        ContextDefinition.Builder bldr = ContextDefinition.Builder.create(KsKrmsConstants.NAMESPACE_CODE, selectedContextType.getName());
233//        // TODO: find out what this typeId is really supposed to be.
234//        // I thought it would be the selected context type but in the existing configured data it has a value T1004?!? 
235//        bldr.setTypeId(selectedContextType.getId());
236//        bldr.setDescription(description); // set the description to be the template description of the type
237//        bldr.setActive(true);
238//        ContextDefinition context = this.ruleManagementService.findCreateContext(bldr.build());
239//        assertNotNull(context);
240//        assertEquals(context.getName(), selectedContextType.getName());
241//        assertEquals(context.getNamespace(), selectedContextType.getNamespace());
242//        // TODO: worry that this context could have been created with a different type and/or description
243//        assertEquals(context.getTypeId(), selectedContextType.getId());
244//        assertEquals(context.getDescription(), description);
245//
246//        // create the agenda
247//        String id = null; // set by service when create is called
248//        String name = courseEligAgendaType.getName() + " for " + anchorClu.getOfficialIdentifier().getCode() + " " + anchorClu.getId();
249//        String typeId = courseEligAgendaType.getId();
250//        String contextId = context.getId();
251//        AgendaDefinition.Builder agendaBldr = AgendaDefinition.Builder.create(id, name, typeId, contextId);
252//        agendaBldr.setActive(false);
253//        AgendaDefinition agenda = this.ruleManagementService.createAgenda(agendaBldr.build());
254//
255//        // create the rule
256//        String ruleId = null; // sevice sets id
257//        // TODO: find out if this really needs to be unique.. if not remove the actual internal Id of the course from the end 
258//        name = eligPrereqRuleType.getName() + " for " + anchorClu.getOfficialIdentifier().getCode() + " " + anchorClu.getId();
259//        String namespace = KsKrmsConstants.NAMESPACE_CODE;
260//        typeId = eligPrereqRuleType.getId();
261//        String propId = null;
262//        RuleDefinition.Builder ruleBldr = RuleDefinition.Builder.create(ruleId, name, namespace, typeId, propId);
263//        ruleBldr.setActive(true);
264//        RuleDefinition rule = this.ruleManagementService.createRule(ruleBldr.build());
265//
266//        // bind the rule to the agenda via the item
267//        id = null;
268//        String agendaId = agenda.getId();
269//        AgendaItemDefinition.Builder itemBldr = AgendaItemDefinition.Builder.create(id, agendaId);
270//        itemBldr.setRuleId(ruleId);
271//        AgendaItemDefinition item = this.ruleManagementService.createAgendaItem(itemBldr.build());
272//        // now go back and mark the agenda with the item and make it active
273//        agendaBldr = AgendaDefinition.Builder.create(agenda);
274//        agendaBldr.setActive(true);
275//        agendaBldr.setFirstItemId(item.getId());
276//        this.ruleManagementService.updateAgenda(agendaBldr.build());
277//        agenda = this.ruleManagementService.getAgenda(agenda.getId());
278//
279//        // create the cluset
280//        CluInfo courseClu2 = this.getClu("COURSE2");
281//        CluInfo courseClu3 = this.getClu("COURSE3");
282//        CluInfo courseClu4 = this.getClu("COURSE4");
283//        List<String> versionIndIds = this.getVersionIndIds(anchorClu, courseClu2, courseClu3);
284//        CluSetInfo cluSet = createCourseSet("Courses 1, 2, & 3", versionIndIds);
285//
286//        propId = null; // should be null until assigned by service
287//        String propTypeCode = PropositionType.SIMPLE.getCode();
288//        ruleId = rule.getId();
289//        typeId = nOfCoursesPropositionType.getId();
290//        List<PropositionParameter.Builder> parameters = new ArrayList<PropositionParameter.Builder>();
291//
292//        // do the term parameter first
293//        //        nOfCoursesTermType.getId();
294//        KrmsTypeDefinition type = nOfCoursesTermType;
295//        nlTemplate = this.getRuleEditUsageNaturalLanguageTemplate(type.getId(), languageCode);
296//        id = null; // should be set by service
297//        propId = null; // should also be set by the service when the create on the proposition happens
298//        String value = null;
299//        String parameterType = serviceName2PropositionParameterType(type.getServiceName()).getCode();
300//        Integer sequenceNumber = 1;
301//        PropositionParameter.Builder propParamBldr = PropositionParameter.Builder.create(id, propId, value, parameterType, sequenceNumber);
302//
303//        // now the parameter to the parameter! (actually the term parameter to the proposition parameter that is a term!
304//        id = null; // set by service
305//        TermSpecificationDefinition.Builder termSpecBldr = TermSpecificationDefinition.Builder.create(termSpec);
306//        List<TermParameterDefinition.Builder> termParameters = new ArrayList<TermParameterDefinition.Builder>();
307//        description = termSpec.getName() + " for " + cluSet.getId();
308//        TermDefinition.Builder termBldr = TermDefinition.Builder.create(id, termSpecBldr, termParameters);
309//        termBldr.setDescription(description);
310//
311//        id = null; //set by service
312//        typeId = cluSetIdType.getId();
313//        nlTemplate = this.getRuleEditUsageNaturalLanguageTemplate(typeId, languageCode);
314//        name = nlTemplate.getTemplate(); // this should be CourseSetId 
315//        value = cluSet.getId(); // this was created when the cluSet was built
316//        TermParameterDefinition.Builder termParamBldr = TermParameterDefinition.Builder.create(id, typeId, name, value);
317//        termParameters.add(termParamBldr);
318//        propParamBldr.setTermValue(termBldr.build());
319//        parameters.add(propParamBldr);
320//
321//        // do the 2nd parameter
322//        type = constantValueN;
323//        nlTemplate = this.getRuleEditUsageNaturalLanguageTemplate(type.getId(), languageCode);
324//        id = null; // should be set by service
325//        propId = null; // should also be set by the service when the create on the proposition happens
326//        value = nlTemplate.getTemplate(); // this should be default of 1 but use can change to be 2 or 3 or...
327//        parameterType = serviceName2PropositionParameterType(type.getServiceName()).getCode();
328//        sequenceNumber = 2;
329//        propParamBldr = PropositionParameter.Builder.create(id, propId, value, parameterType, sequenceNumber);
330//        parameters.add(propParamBldr);
331//
332//        // do the 3rd parameter
333//        type = lessThanEqualOperatorType;
334//        nlTemplate = this.getRuleEditUsageNaturalLanguageTemplate(type.getId(), languageCode);
335//        id = null; // should be set by service
336//        propId = null; // should also be set by the service when the create on the proposition happens
337//        value = nlTemplate.getTemplate(); // this should be default of <=..
338//        parameterType = serviceName2PropositionParameterType(type.getServiceName()).getCode();
339//        sequenceNumber = 3;
340//        propParamBldr = PropositionParameter.Builder.create(id, propId, value, parameterType, sequenceNumber);
341//        parameters.add(propParamBldr);
342//
343//        PropositionDefinition.Builder propBldr = PropositionDefinition.Builder.create(propId, propTypeCode, ruleId, typeId, parameters);
344//
345//        String enDescription = this.ruleManagementService.translateNaturalLanguageForProposition(getTypeDescriptionUsage ().getId(), propBldr.build(), languageCode);
346//        System.out.println ("description=" + enDescription);
347//        
348////        String enPreview = this.ruleManagementService.translateNaturalLanguageForProposition(getPreviewUsage ().getId(), propBldr.build(), languageCode);
349////        System.out.println (enPreview);
350//     
351//        
352//        PropositionDefinition propositon = this.ruleManagementService.createProposition(propBldr.build());
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    // cache
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        // get the usage
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        // check there is a corresponding template so we can display on the screen what kind of rule this is
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        // check there is a corresponding template so we can display on the screen what kind of rule this is
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}