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