View Javadoc

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