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.ConfigurableApplicationContext;
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 DefaultValidatorImpl val = new DefaultValidatorImpl();
81 val.setValidatorFactory(new ValidatorFactory());
82 ProgramManagingBodiesValidator programManagingBodiesValidator = new ProgramManagingBodiesValidator();
83 MessageServiceMock messageServiceMock = new MessageServiceMock();
84 MessageInfo message = new MessageInfo();
85 message.setGroupName("validation");
86 LocaleInfo l = new LocaleInfo();
87 l.setLocaleLanguage("en");
88 l.setLocaleRegion("en");
89 l.setLocaleScript("en");
90 l.setLocaleVariant("en");
91
92 message.setLocale(l);
93 message.setMessageKey("validation.programManagingBodiesMatch");
94
95 message.setValue("validation.programManagingBodiesMatch");
96
97
98 try {
99 messageServiceMock.createMessage(l,message.getGroupName(), message.getMessageKey(), message, new ContextInfo());
100 } catch (DoesNotExistException e) {
101
102 e.printStackTrace();
103 } catch (InvalidParameterException e) {
104
105 e.printStackTrace();
106 } catch (MissingParameterException e) {
107
108 e.printStackTrace();
109 } catch (PermissionDeniedException e) {
110
111 e.printStackTrace();
112 }
113 programManagingBodiesValidator.setMessageService(messageServiceMock);
114 programManagingBodiesValidator.setSearchDispatcher(new MockSearchDispatcher());
115 List<Validator> validatorList = new ArrayList<Validator>();
116 validatorList.add(programManagingBodiesValidator);
117 val.getValidatorFactory().setValidatorList(validatorList);
118 val.setDateParser(new ServerDateParser());
119 val.setSearchDispatcher(new MockSearchDispatcher());
120 MajorDisciplineInfo info = new MajorDisciplineInfo();
121
122 ConfigurableApplicationContext ac = new ClassPathXmlApplicationContext(
123 "classpath:ks-programInfo-dictionary-context.xml");
124 ObjectStructureDefinition os = (ObjectStructureDefinition) ac.getBean(
125 info.getClass().getName());
126 ac.close();
127
128 List<ValidationResultInfo> validationResults = val.validateObject(info, os, contextInfo);
129 System.out.println("h2. with just a blank record");
130 for (ValidationResultInfo vr : validationResults) {
131 System.out.println(vr.getElement() + " " + vr.getMessage());
132 }
133
134
135
136
137
138 assertEquals(5, validationResults.size());
139
140 try {
141 info = new MajorDisciplineDataGenerator().getMajorDisciplineInfoTestData();
142 }
143 catch (Exception ex) {
144 throw new RuntimeException(ex);
145 }
146 validationResults = val.validateObject(info, os, contextInfo);
147 System.out.println("h2. with generated data");
148 for (ValidationResultInfo vr : validationResults) {
149 System.out.println(vr.getElement() + " " + vr.getMessage());
150 }
151 assertEquals(18, validationResults.size());
152 }
153 }