001    package org.kuali.student.lum.course.service.impl;
002    
003    import java.util.LinkedHashSet;
004    import java.util.List;
005    import java.util.Set;
006    import org.junit.Test;
007    import org.kuali.student.common.dictionary.dto.ObjectStructureDefinition;
008    import org.kuali.student.common.dictionary.service.impl.DictionaryTesterHelper;
009    import org.kuali.student.common.dto.AmountInfo;
010    import org.kuali.student.common.dto.TimeAmountInfo;
011    import org.kuali.student.common.exceptions.OperationFailedException;
012    import org.kuali.student.common.validation.dto.ValidationResultInfo;
013    import org.kuali.student.common.validator.DefaultValidatorImpl;
014    import org.kuali.student.common.validator.ServerDateParser;
015    import org.kuali.student.lum.lu.dto.CluIdentifierInfo;
016    import org.kuali.student.lum.lu.dto.CluInfo;
017    import org.springframework.context.ApplicationContext;
018    import org.springframework.context.support.ClassPathXmlApplicationContext;
019    import static org.junit.Assert.*;
020    
021    public class TestCluInfoDictionary
022    {
023    
024     @Test
025     public void testLoadCluInfoDictionary ()
026     {
027      Set<String> startingClasses = new LinkedHashSet ();
028      startingClasses.add (CluInfo.class.getName ());
029    //  startingClasses.add (CluCluRelationInfo.class.getName ());
030      String contextFile = "ks-cluInfo-dictionary-context";
031      String outFile = "target/" + contextFile + ".txt";
032      DictionaryTesterHelper helper = new DictionaryTesterHelper (outFile,
033                                                                  startingClasses,
034                                                                  contextFile
035                                                                  + ".xml",
036                                                                  false);
037      List<String> errors = helper.doTest ();
038      if (errors.size () > 0)
039      {
040       fail ("failed dictionary validation:\n" + formatAsString (errors));
041      }
042     }
043    
044     private String formatAsString (List<String> errors)
045     {
046      int i = 0;
047      StringBuilder builder = new StringBuilder ();
048      for (String error : errors)
049      {
050       i ++;
051       builder.append (i + ". " + error + "\n");
052      }
053      return builder.toString ();
054     }
055    
056    
057     @Test
058     public void testCluInfoValidation () throws OperationFailedException
059     {
060      ApplicationContext ac = new ClassPathXmlApplicationContext (
061        "classpath:ks-cluInfo-dictionary-context.xml");
062      System.out.println ("h2. Validation Test");
063      DefaultValidatorImpl val = new DefaultValidatorImpl ();
064      val.setDateParser (new ServerDateParser ());
065      val.setSearchDispatcher (new MockSearchDispatcher ());
066      CluInfo info = new CluInfo ();
067      ObjectStructureDefinition os = (ObjectStructureDefinition) ac.getBean (
068        info.getClass ().getName ());
069      List<ValidationResultInfo> validationResults = val.validateObject (info, os);
070      System.out.println ("h3. With just a blank CluInfo");
071    //  for (ValidationResultInfo vr : validationResults)
072    //  {
073    //   System.out.println (vr.getElement () + " " + vr.getMessage ());
074    //  }
075      assertEquals (2, validationResults.size ());
076    
077      // test that we validate substructures
078      info.setOfficialIdentifier (new CluIdentifierInfo ());
079      validationResults = val.validateObject (info, os);
080    //  for (ValidationResultInfo vr : validationResults)
081    //  {
082    //   System.out.println (vr.getElement () + " " + vr.getMessage ());
083    //  }
084      // should now require type and state of the identifier structure too
085      assertEquals (4, validationResults.size ());
086    
087    
088      // test that we can put completely blank timeAmountInfo structures
089      info.setStdDuration (new TimeAmountInfo ());
090      validationResults = val.validateObject (info, os);
091    //  for (ValidationResultInfo vr : validationResults)
092    //  {
093    //   System.out.println (vr.getElement () + " " + vr.getMessage ());
094    //  }
095      assertEquals (4, validationResults.size ());
096    
097      // test the requires constraint
098      // that requires a durationType if we have a timeQuantity
099      info.getStdDuration ().setTimeQuantity (1);
100      validationResults = val.validateObject (info, os);
101    //  for (ValidationResultInfo vr : validationResults)
102    //  {
103    //   System.out.println (vr.getElement () + " " + vr.getMessage ());
104    //  }
105      assertEquals (6, validationResults.size ());
106    
107      // test that we can put completely blank timeAmountInfo structures
108      info.setIntensity (new AmountInfo ());
109      validationResults = val.validateObject (info, os);
110    //  System.out.println ("validation results adding a blank CluIdentifierInfo");
111    //  for (ValidationResultInfo vr : validationResults)
112    //  {
113    //   System.out.println (vr.getElement () + " " + vr.getMessage ());
114    //  }
115      assertEquals (6, validationResults.size ());
116    
117      // test the requires constraint
118      // that requires a unity if we have a unitQuantity
119      info.getIntensity ().setUnitQuantity ("1");
120      validationResults = val.validateObject (info, os);
121    //  System.out.println ("validation results adding a blank CluIdentifierInfo");
122    //  for (ValidationResultInfo vr : validationResults)
123    //  {
124    //   System.out.println (vr.getElement () + " " + vr.getMessage ());
125    //  }
126      assertEquals (8, validationResults.size ());
127    
128    
129     }
130    }