1 package org.kuali.student.core.dictionary.service; 2 3 import java.util.Arrays; 4 import java.util.LinkedHashSet; 5 import java.util.List; 6 import java.util.Set; 7 import org.junit.Test; 8 import org.kuali.student.common.dictionary.dto.ObjectStructureDefinition; 9 import org.kuali.student.common.dictionary.service.impl.DictionaryTesterHelper; 10 import org.kuali.student.common.exceptions.OperationFailedException; 11 import org.kuali.student.common.validation.dto.ValidationResultInfo; 12 import org.kuali.student.common.validator.DefaultValidatorImpl; 13 import org.kuali.student.common.validator.ServerDateParser; 14 import org.kuali.student.core.statement.dto.ReqCompFieldInfo; 15 import org.kuali.student.core.statement.dto.ReqComponentInfo; 16 import org.kuali.student.core.statement.dto.StatementInfo; 17 import org.kuali.student.core.statement.dto.StatementTreeViewInfo; 18 import org.springframework.context.ApplicationContext; 19 import org.springframework.context.support.ClassPathXmlApplicationContext; 20 import static org.junit.Assert.*; 21 22 public class TestStatementDictionary { 23 24 @Test 25 public void testLoadStatementInfoDictionary() { 26 System.out.println ("testing statement dictionary"); 27 Set<String> startingClasses = new LinkedHashSet(); 28 startingClasses.add(StatementInfo.class.getName ()); 29 startingClasses.add(ReqComponentInfo.class.getName ()); 30 startingClasses.add(StatementTreeViewInfo.class.getName ()); 31 String contextFile = "ks-statement-dictionary-context"; 32 String outFile = "target/" + contextFile + ".txt"; 33 DictionaryTesterHelper helper = new DictionaryTesterHelper(outFile, 34 startingClasses, contextFile + ".xml", false); 35 List<String> errors = helper.doTest (); 36 if (errors.size () > 0) 37 { 38 fail ("failed dictionary validation:\n" + formatAsString (errors)); 39 } 40 } 41 42 private String formatAsString (List<String> errors) 43 { 44 int i = 0; 45 StringBuilder builder = new StringBuilder (); 46 for (String error : errors) 47 { 48 i ++; 49 builder.append (i + ". " + error + "\n"); 50 } 51 return builder.toString (); 52 } 53 54 55 @Test 56 public void testStatementInfoValidation() throws OperationFailedException { 57 ApplicationContext ac = new ClassPathXmlApplicationContext("classpath:ks-statement-dictionary-context.xml"); 58 System.out.println("h2. Validation Test"); 59 DefaultValidatorImpl val = new DefaultValidatorImpl(); 60 val.setDateParser(new ServerDateParser()); 61 val.setSearchDispatcher(new MockSearchDispatcher()); 62 StatementInfo info = new StatementInfo(); 63 ObjectStructureDefinition os = (ObjectStructureDefinition) ac.getBean(info.getClass().getName()); 64 List<ValidationResultInfo> validationResults = val.validateObject(info, os); 65 System.out.println("h3. With just a blank StatementInfo"); 66 for (ValidationResultInfo vr : validationResults) 67 { 68 System.out.println (vr.getElement () + " " + vr.getMessage ()); 69 } 70 assertEquals(1, validationResults.size()); 71 } 72 73 @Test 74 public void testRequirementComponentInfoValidation() throws OperationFailedException { 75 ApplicationContext ac = new ClassPathXmlApplicationContext("classpath:ks-statement-dictionary-context.xml"); 76 System.out.println("h2. Validation Test"); 77 DefaultValidatorImpl val = new DefaultValidatorImpl(); 78 val.setDateParser(new ServerDateParser()); 79 val.setSearchDispatcher(new MockSearchDispatcher()); 80 ReqComponentInfo info = new ReqComponentInfo(); 81 ObjectStructureDefinition os = (ObjectStructureDefinition) ac.getBean(info.getClass().getName()); 82 List<ValidationResultInfo> validationResults = val.validateObject(info, os); 83 System.out.println("h3. With just a blank ReqComponentInfo"); 84 for (ValidationResultInfo vr : validationResults) 85 { 86 System.out.println (vr.getElement () + " " + vr.getMessage ()); 87 } 88 assertEquals(0, validationResults.size()); 89 90 ReqCompFieldInfo fieldInfo = new ReqCompFieldInfo (); 91 fieldInfo.setType ("kuali.reqComponent.field.type.gradeType.id"); 92 fieldInfo.setValue ("kuali.resultComponent.grade.letter"); 93 info.setReqCompFields (Arrays.asList (fieldInfo)); 94 validationResults = val.validateObject(info, os); 95 System.out.println("h3. With just a blank ReqComponentInfo"); 96 for (ValidationResultInfo vr : validationResults) 97 { 98 System.out.println (vr.getElement () + " " + vr.getMessage ()); 99 } 100 assertEquals(0, validationResults.size()); 101 102 fieldInfo = new ReqCompFieldInfo (); 103 fieldInfo.setType ("kuali.reqComponent.field.type.gradeType.id"); 104 fieldInfo.setValue ("bad with an embedded space in value"); 105 info.setReqCompFields (Arrays.asList (fieldInfo)); 106 validationResults = val.validateObject(info, os); 107 System.out.println("h3. With just a blank ReqComponentInfo"); 108 for (ValidationResultInfo vr : validationResults) 109 { 110 System.out.println (vr.getElement () + " " + vr.getMessage ()); 111 } 112 assertEquals(1, validationResults.size()); 113 114 } 115 }