View Javadoc

1   package org.kuali.student.lum.course.service.impl;
2   
3   import org.junit.Test;
4   import org.kuali.student.common.test.util.ContextInfoTestUtility;
5   import org.kuali.student.r1.common.dictionary.dto.ObjectStructureDefinition;
6   import org.kuali.student.r1.common.dictionary.service.impl.DictionaryTesterHelper;
7   import org.kuali.student.r1.common.validator.ServerDateParser;
8   import org.kuali.student.r2.common.dto.AmountInfo;
9   import org.kuali.student.r2.common.dto.ContextInfo;
10  import org.kuali.student.r2.common.dto.TimeAmountInfo;
11  import org.kuali.student.r2.common.dto.ValidationResultInfo;
12  import org.kuali.student.r2.common.exceptions.OperationFailedException;
13  import org.kuali.student.r2.common.validator.DefaultValidatorImpl;
14  import org.kuali.student.r2.lum.clu.dto.CluIdentifierInfo;
15  import org.kuali.student.r2.lum.clu.dto.CluInfo;
16  import org.springframework.context.ApplicationContext;
17  import org.springframework.context.support.ClassPathXmlApplicationContext;
18  
19  import java.util.LinkedHashSet;
20  import java.util.List;
21  import java.util.Set;
22  
23  import static org.junit.Assert.assertEquals;
24  import static org.junit.Assert.fail;
25  
26  public class TestCluInfoDictionary {
27  
28      ContextInfo contextInfo = ContextInfoTestUtility.getEnglishContextInfo();
29  
30      @Test
31      public void testLoadCluInfoDictionary() {
32          Set<String> startingClasses = new LinkedHashSet();
33          startingClasses.add(CluInfo.class.getName());
34          // startingClasses.add (CluCluRelationInfo.class.getName ());
35          String contextFile = "ks-cluInfo-dictionary-context";
36          String outFile = "target/" + contextFile + ".txt";
37          DictionaryTesterHelper helper = new DictionaryTesterHelper(outFile, startingClasses, contextFile + ".xml", false);
38          List<String> errors = helper.doTest();
39          if (errors.size() > 0) {
40              fail("failed dictionary validation:\n" + formatAsString(errors));
41          }
42      }
43  
44      private String formatAsString(List<String> errors) {
45          int i = 0;
46          StringBuilder builder = new StringBuilder();
47          for (String error : errors) {
48              i++;
49              builder.append(i + ". " + error + "\n");
50          }
51          return builder.toString();
52      }
53  
54      @Test
55      public void testCluInfoValidation() throws OperationFailedException {
56          ApplicationContext ac = new ClassPathXmlApplicationContext("classpath:ks-cluInfo-dictionary-context.xml");
57          System.out.println("h2. Validation Test");
58          DefaultValidatorImpl val = new DefaultValidatorImpl();
59          val.setDateParser(new ServerDateParser());
60          val.setSearchDispatcher(new MockSearchDispatcher());
61          CluInfo info = new CluInfo();
62          ObjectStructureDefinition os = (ObjectStructureDefinition) ac.getBean(info.getClass().getName());
63  
64          List<ValidationResultInfo> validationResults = val.validateObject(info, os, contextInfo);
65          System.out.println("h3. With just a blank CluInfo");
66           for (ValidationResultInfo vr : validationResults)
67           {
68           System.out.println (vr.getElement () + " " + vr.getMessage ());
69           }
70          assertEquals(3, validationResults.size());
71  
72          // test that we validate substructures
73          info.setOfficialIdentifier(new CluIdentifierInfo());
74          validationResults = val.validateObject(info, os, contextInfo);
75          for (ValidationResultInfo vr : validationResults)
76          {
77          System.out.println (vr.getElement () + " " + vr.getMessage ());
78          }
79          // should now require type and state of the identifier structure too
80          assertEquals(5, validationResults.size());
81  
82          // test that we can put completely blank timeAmountInfo structures
83          info.setStdDuration(new TimeAmountInfo());
84          validationResults = val.validateObject(info, os, contextInfo);
85          for (ValidationResultInfo vr : validationResults)
86          {
87          System.out.println (vr.getElement () + " " + vr.getMessage ());
88          }
89          assertEquals(5, validationResults.size());
90  
91          // test the requires constraint
92          // that requires a durationType if we have a timeQuantity
93          info.getStdDuration().setTimeQuantity(1);
94          validationResults = val.validateObject(info, os, contextInfo);
95          for (ValidationResultInfo vr : validationResults)
96          {
97          System.out.println (vr.getElement () + " " + vr.getMessage ());
98          }
99          assertEquals(7, validationResults.size());
100 
101         // test that we can put completely blank timeAmountInfo structures
102         info.setIntensity(new AmountInfo());
103         validationResults = val.validateObject(info, os, contextInfo);
104         System.out.println ("validation results adding a blank CluIdentifierInfo");
105         for (ValidationResultInfo vr : validationResults)
106         {
107         System.out.println (vr.getElement () + " " + vr.getMessage ());
108         }
109         assertEquals(7, validationResults.size());
110 
111         // test the requires constraint
112         // that requires a unity if we have a unitQuantity
113         info.getIntensity().setUnitQuantity("1");
114         validationResults = val.validateObject(info, os, contextInfo);
115         System.out.println ("validation results adding a blank CluIdentifierInfo");
116         for (ValidationResultInfo vr : validationResults)
117         {
118         System.out.println (vr.getElement () + " " + vr.getMessage ());
119         }
120         assertEquals(9, validationResults.size());
121 
122     }
123 }