1 package org.kuali.student.krms.naturallanguage.config.context;
2
3 import org.junit.Assert;
4 import org.junit.Before;
5 import org.junit.Test;
6 import org.junit.runner.RunWith;
7 import org.kuali.rice.krms.api.repository.term.TermDefinitionContract;
8 import org.kuali.rice.krms.api.repository.term.TermParameterDefinitionContract;
9 import org.kuali.student.common.test.spring.AbstractServiceTest;
10 import org.kuali.student.common.test.util.AttributeTester;
11 import org.kuali.student.krms.naturallanguage.KRMSDataGenerator;
12 import org.kuali.student.r2.core.krms.naturallanguage.TermParameterTypes;
13 import org.kuali.student.r2.common.dto.ContextInfo;
14 import org.kuali.student.r2.common.exceptions.AlreadyExistsException;
15 import org.kuali.student.r2.common.exceptions.OperationFailedException;
16 import org.kuali.student.r2.common.util.ContextUtils;
17 import org.kuali.student.r2.common.util.RichTextHelper;
18 import org.kuali.student.r2.lum.lrc.dto.ResultScaleInfo;
19 import org.kuali.student.r2.lum.lrc.dto.ResultValueInfo;
20 import org.kuali.student.r2.lum.lrc.service.LRCService;
21 import org.kuali.student.r2.lum.util.constants.LrcServiceConstants;
22 import org.springframework.test.context.ContextConfiguration;
23 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
24 import org.springframework.test.context.transaction.TransactionConfiguration;
25
26 import javax.annotation.Resource;
27 import java.util.*;
28
29 @RunWith(SpringJUnit4ClassRunner.class)
30 @ContextConfiguration(locations = {"classpath:lrc-service-test-context.xml"})
31 @TransactionConfiguration(transactionManager = "JtaTxManager", defaultRollback = true)
32 public class LrcContextImplTest extends AbstractServiceTest {
33
34 @Resource
35 private LRCService lrcService;
36 private LrcContextImpl lrcContext = new LrcContextImpl();
37
38 private Map<String, Object> term;
39 private Map<String, Object> term2;
40
41
42 private void setupTerm1() {
43 Map<String, Object> parameters = new HashMap<String, Object>();
44 parameters.put(TermParameterTypes.GRADE_KEY.getId(),LrcServiceConstants.RESULT_VALUE_KEY_GRADE_LETTER_A);
45 parameters.put(TermParameterTypes.GRADE_TYPE_KEY.getId(),LrcServiceConstants.RESULT_SCALE_KEY_GRADE_LETTER);
46 term = parameters;
47 }
48
49 private void setupTerm2() {
50 Map<String, Object> parameters = new HashMap<String, Object>();
51 parameters.put(TermParameterTypes.GRADE_KEY.getId(),null);
52 parameters.put(TermParameterTypes.GRADE_TYPE_KEY.getId(),null);
53 term2 = parameters;
54 }
55
56 @Before
57 public void beforeMethod() {
58 lrcContext.setLrcService(lrcService);
59 ContextInfo context = new ContextInfo();
60 context.setPrincipalId("testUser1");
61 context.setCurrentDate(new Date());
62
63 ResultScaleInfo resScale = new ResultScaleInfo();
64 resScale.setKey(LrcServiceConstants.RESULT_SCALE_KEY_GRADE_LETTER);
65 resScale.setTypeKey(LrcServiceConstants.RESULT_SCALE_TYPE_KEY_GRADE);
66 resScale.setStateKey(LrcServiceConstants.RESULT_SCALE_STATE_APPROVED);
67 resScale.setEffectiveDate(new Date());
68 resScale.setExpirationDate(new Date(new Date().getTime() + 1000));
69 resScale.setName("A-F Letter Graded Scale");
70 resScale.setDescr(new RichTextHelper().fromPlain("Letter Graded Scale description"));
71 new AttributeTester().add2ForCreate(resScale.getAttributes());
72
73
74
75 ResultValueInfo reaValue = new ResultValueInfo();
76 reaValue.setKey(LrcServiceConstants.RESULT_VALUE_KEY_GRADE_LETTER_A);
77 reaValue.setTypeKey(LrcServiceConstants.RESULT_VALUE_TYPE_KEY_VALUE);
78 reaValue.setStateKey(LrcServiceConstants.RESULT_VALUE_STATE_APPROVED);
79 reaValue.setEffectiveDate(new Date());
80 reaValue.setExpirationDate(new Date(new Date().getTime() + 1000));
81 reaValue.setName("Grade A+");
82 reaValue.setDescr(new RichTextHelper().fromPlain("value A+ description"));
83 reaValue.setResultScaleKey(LrcServiceConstants.RESULT_SCALE_KEY_GRADE_LETTER);
84 reaValue.setValue("A+");
85 reaValue.setNumericValue("4.5");
86
87 try {
88 lrcService.createResultScale(resScale.getTypeKey(), resScale, context);
89 lrcService.createResultValue(reaValue.getResultScaleKey(), reaValue.getTypeKey(), reaValue,
90 context);
91 } catch (AlreadyExistsException e) {
92
93 } catch (Exception e) {
94 Assert.fail("Problem creating test data in the LRC Service");
95 }
96 setupTerm1();
97 setupTerm2();
98 }
99
100 @Test
101 public void testCreateContextMap_Lrc() throws OperationFailedException {
102 Map<String, Object> contextMap = lrcContext.createContextMap(term, ContextUtils.getContextInfo());
103 String grade = (String) contextMap.get(LrcContextImpl.GRADE_TOKEN);
104 ResultScaleInfo gradeType = (ResultScaleInfo) contextMap.get(LrcContextImpl.GRADE_TYPE_TOKEN);
105
106 Assert.assertNotNull(contextMap);
107 Assert.assertEquals("A+", grade);
108 Assert.assertEquals(LrcServiceConstants.RESULT_SCALE_KEY_GRADE_LETTER, gradeType.getKey());
109 }
110
111 @Test
112 public void testCreateContextMap_NullTokenValues() throws OperationFailedException {
113 Map<String, Object> contextMap = lrcContext.createContextMap(term2, ContextUtils.getContextInfo());
114 String grade = (String) contextMap.get(LrcContextImpl.GRADE_TOKEN);
115 ResultScaleInfo gradeType = (ResultScaleInfo) contextMap.get(LrcContextImpl.GRADE_TYPE_TOKEN);
116
117 Assert.assertNotNull(contextMap);
118 Assert.assertEquals(null, grade);
119 Assert.assertEquals(null, gradeType);
120
121 }
122
123 }