1 package org.kuali.student.lum.program.service.impl;
2
3 import static org.junit.Assert.assertEquals;
4 import static org.junit.Assert.fail;
5
6 import java.util.ArrayList;
7 import java.util.LinkedHashSet;
8 import java.util.List;
9 import java.util.Set;
10
11 import org.apache.cxf.service.model.MessageInfo;
12 import org.junit.Test;
13 import org.kuali.student.common.dictionary.dto.ObjectStructureDefinition;
14 import org.kuali.student.common.dictionary.service.impl.DictionaryTesterHelper;
15 import org.kuali.student.common.exceptions.OperationFailedException;
16 import org.kuali.student.common.messages.dto.LocaleKeyList;
17 import org.kuali.student.common.messages.dto.Message;
18 import org.kuali.student.common.messages.dto.MessageGroupKeyList;
19 import org.kuali.student.common.messages.dto.MessageList;
20 import org.kuali.student.common.messages.service.MessageService;
21 import org.kuali.student.common.validation.dto.ValidationResultInfo;
22 import org.kuali.student.common.validator.DefaultValidatorImpl;
23 import org.kuali.student.common.validator.ServerDateParser;
24 import org.kuali.student.common.validator.Validator;
25 import org.kuali.student.common.validator.ValidatorFactory;
26 import org.kuali.student.core.messages.service.impl.MessageServiceMock;
27 import org.kuali.student.lum.course.service.impl.MockSearchDispatcher;
28 import org.kuali.student.lum.program.dto.CoreProgramInfo;
29 import org.kuali.student.lum.program.dto.CredentialProgramInfo;
30 import org.kuali.student.lum.program.dto.MajorDisciplineInfo;
31 import org.kuali.student.lum.program.dto.MinorDisciplineInfo;
32 import org.kuali.student.lum.program.dto.ProgramRequirementInfo;
33 import org.kuali.student.lum.program.service.assembler.MajorDisciplineDataGenerator;
34 import org.kuali.student.lum.program.service.validation.ProgramManagingBodiesValidator;
35 import org.springframework.context.ApplicationContext;
36 import org.springframework.context.support.ClassPathXmlApplicationContext;
37
38 public class TestProgramInfoDictionary {
39
40 @Test
41 public void testLoadProgramInfoDictionary() {
42 Set<String> startingClasses = new LinkedHashSet();
43 startingClasses.add(MajorDisciplineInfo.class.getName ());
44 startingClasses.add(CoreProgramInfo.class.getName ());
45 startingClasses.add(MinorDisciplineInfo.class.getName ());
46 startingClasses.add(CredentialProgramInfo.class.getName ());
47 startingClasses.add(ProgramRequirementInfo.class.getName ());
48 String contextFile = "ks-programInfo-dictionary-context";
49 String outFile = "target/" + contextFile + ".txt";
50 DictionaryTesterHelper helper = new DictionaryTesterHelper(outFile,
51 startingClasses,
52 contextFile
53 + ".xml",
54 true);
55 List<String> errors = helper.doTest ();
56 if (errors.size () > 0)
57 {
58 fail ("failed dictionary validation:\n" + formatAsString (errors));
59 }
60 }
61
62 private String formatAsString (List<String> errors)
63 {
64 int i = 0;
65 StringBuilder builder = new StringBuilder ();
66 for (String error : errors)
67 {
68 i ++;
69 builder.append (i + ". " + error + "\n");
70 }
71 return builder.toString ();
72 }
73
74
75 @Test
76 public void testMajorDisciplineInfoValidation() throws
77 OperationFailedException {
78 System.out.println("h1. Validation results");
79 ApplicationContext ac = new ClassPathXmlApplicationContext(
80 "classpath:ks-programInfo-dictionary-context.xml");
81 DefaultValidatorImpl val = new DefaultValidatorImpl();
82 val.setValidatorFactory(new ValidatorFactory());
83 ProgramManagingBodiesValidator programManagingBodiesValidator = new ProgramManagingBodiesValidator();
84 MessageServiceMock messageServiceMock = new MessageServiceMock();
85 Message message = new Message();
86 message.setGroupName("validation");
87 message.setLocale("en");
88 message.setId("validation.programManagingBodiesMatch");
89 message.setValue("validation.programManagingBodiesMatch");
90 messageServiceMock.addMessage(message);
91 programManagingBodiesValidator.setMessageService(messageServiceMock);
92 programManagingBodiesValidator.setSearchDispatcher(new MockSearchDispatcher());
93 List<Validator> validatorList = new ArrayList<Validator>();
94 validatorList.add(programManagingBodiesValidator);
95 val.getValidatorFactory().setValidatorList(validatorList);
96 val.setDateParser(new ServerDateParser());
97 val.setSearchDispatcher(new MockSearchDispatcher());
98 MajorDisciplineInfo info = new MajorDisciplineInfo();
99 ObjectStructureDefinition os = (ObjectStructureDefinition) ac.getBean(
100 info.getClass().getName());
101 List<ValidationResultInfo> validationResults = val.validateObject(info, os);
102 System.out.println("h2. with just a blank record");
103 for (ValidationResultInfo vr : validationResults) {
104 System.out.println(vr.getElement() + " " + vr.getMessage());
105 }
106
107
108
109
110
111 assertEquals(5, validationResults.size());
112
113 try {
114 info =
115 new MajorDisciplineDataGenerator().getMajorDisciplineInfoTestData();
116 }
117 catch (Exception ex) {
118 throw new RuntimeException(ex);
119 }
120 validationResults = val.validateObject(info, os);
121 System.out.println("h2. with generated data");
122 for (ValidationResultInfo vr : validationResults) {
123 System.out.println(vr.getElement() + " " + vr.getMessage());
124 }
125
126 }
127 }