1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.student.r2.common.datadictionary;
17
18 import org.junit.*;
19 import org.kuali.rice.core.api.config.property.Config;
20 import org.kuali.rice.core.api.config.property.ConfigContext;
21 import org.kuali.rice.core.impl.config.property.JAXBConfigImpl;
22 import org.kuali.student.r2.common.dto.ContextInfo;
23 import org.kuali.student.r2.common.dto.ValidationResultInfo;
24 import org.kuali.student.r2.common.infc.ValidationResult;
25 import org.kuali.student.r2.common.util.RichTextHelper;
26 import org.kuali.student.r2.core.atp.dto.AtpInfo;
27 import org.kuali.student.r2.core.constants.AtpServiceConstants;
28 import org.springframework.context.ApplicationContext;
29 import org.springframework.context.support.ClassPathXmlApplicationContext;
30
31 import java.io.IOException;
32 import java.text.DateFormat;
33 import java.text.ParseException;
34 import java.text.SimpleDateFormat;
35 import java.util.ArrayList;
36 import java.util.Date;
37 import java.util.List;
38
39 import static org.junit.Assert.assertEquals;
40
41
42
43
44
45 public class TestRiceDataDictionaryValidatorImplAgainstAtp {
46
47 public TestRiceDataDictionaryValidatorImplAgainstAtp() {
48 }
49
50 @BeforeClass
51 public static void setUpClass() throws Exception {
52 }
53
54 @AfterClass
55 public static void tearDownClass() throws Exception {
56 }
57
58 protected Config getTestHarnessConfig() {
59 Config config = new JAXBConfigImpl(getConfigLocations(), System.getProperties());
60 try {
61 config.parseConfig();
62 } catch (IOException ex) {
63 throw new RuntimeException(ex);
64 }
65 return config;
66 }
67
68
69
70
71
72
73 protected List<String> getConfigLocations() {
74 List<String> configLocations = new ArrayList<String>();
75
76 return configLocations;
77 }
78
79 @Before
80 public void setUp() {
81 Config config = getTestHarnessConfig();
82 ConfigContext.init(config);
83 }
84
85 @After
86 public void tearDown() {
87 }
88
89 private Date parseDate(String str) {
90 DateFormat df = new SimpleDateFormat("yyyy-mm-dd");
91 Date date = null;
92 try {
93 date = df.parse(str);
94 } catch (ParseException ex) {
95 throw new IllegalArgumentException(str, ex);
96 }
97 return date;
98 }
99
100 private ContextInfo getContext1() {
101 return ContextInfo.getInstance("principalId.1", "en", "us");
102 }
103 private DataDictionaryValidator validator;
104
105 public DataDictionaryValidator getValidator() {
106 if (validator == null) {
107 ApplicationContext appContext =
108 new ClassPathXmlApplicationContext(new String[]{"classpath:testContext.xml"});
109 this.validator = (DataDictionaryValidator) appContext.getBean("testValidator");
110 }
111 return validator;
112 }
113
114 private AtpInfo getDefaultAtpInfo() {
115 AtpInfo atp = new AtpInfo();
116 atp.setId("org.kuali.test.atp");
117 atp.setName("test atp");
118 atp.setTypeKey(AtpServiceConstants.ATP_ACADEMIC_CALENDAR_TYPE_KEY);
119 atp.setStateKey(AtpServiceConstants.ATP_DRAFT_STATE_KEY);
120 atp.setStartDate(parseDate("2010-01-01"));
121 atp.setEndDate(parseDate("2010-06-30"));
122 return atp;
123 }
124
125
126
127
128
129
130 @Test
131 public void testValidate() throws Exception {
132 System.out.println("validate ATP");
133 DataDictionaryValidator.ValidationType validationType = null;
134 AtpInfo atp = null;
135 ContextInfo context = null;
136
137
138 DataDictionaryValidator intstance = this.getValidator();
139 List<ValidationResultInfo> result = null;
140
141
142 validationType = DataDictionaryValidator.ValidationType.FULL_VALIDATION;
143 atp = this.getDefaultAtpInfo();
144 context = getContext1();
145 result = intstance.validate(validationType, atp, context);
146 assertEquals(0, result.size());
147
148
149 validationType = DataDictionaryValidator.ValidationType.FULL_VALIDATION;
150 atp = this.getDefaultAtpInfo();
151 context = getContext1();
152 atp.setTypeKey(null);
153 result = intstance.validate(validationType, atp, context);
154 for (ValidationResult vri : result) {
155 System.out.println(vri.getElement() + " " + vri.getLevel() + " " + vri.getMessage());
156 }
157 assertEquals(0, result.size());
158
159
160
161
162
163
164
165 validationType = DataDictionaryValidator.ValidationType.FULL_VALIDATION;
166 atp = this.getDefaultAtpInfo();
167 context = getContext1();
168 atp.setTypeKey("");
169 result = intstance.validate(validationType, atp, context);
170 for (ValidationResult vri : result) {
171 System.out.println(vri.getElement() + " " + vri.getLevel() + " " + vri.getMessage());
172 }
173 assertEquals(0, result.size());
174
175
176
177
178
179
180
181 validationType = DataDictionaryValidator.ValidationType.FULL_VALIDATION;
182 atp = this.getDefaultAtpInfo();
183 context = getContext1();
184 atp.setTypeKey(" ");
185 result = intstance.validate(validationType, atp, context);
186 for (ValidationResult vri : result) {
187 System.out.println(vri.getElement() + " " + vri.getLevel() + " " + vri.getMessage());
188 }
189 assertEquals(0, result.size());
190
191
192
193
194
195
196
197 validationType = DataDictionaryValidator.ValidationType.FULL_VALIDATION;
198 atp = this.getDefaultAtpInfo();
199 context = getContext1();
200 atp.setTypeKey(" ");
201 result = intstance.validate(validationType, atp, context);
202 for (ValidationResult vri : result) {
203 System.out.println(vri.getElement() + " " + vri.getLevel() + " " + vri.getMessage());
204 }
205 assertEquals(0, result.size());
206
207
208
209
210
211
212
213 validationType = DataDictionaryValidator.ValidationType.FULL_VALIDATION;
214 atp = this.getDefaultAtpInfo();
215 context = getContext1();
216 atp.setTypeKey(" \n\t\r ");
217 result = intstance.validate(validationType, atp, context);
218 for (ValidationResult vri : result) {
219 System.out.println(vri.getElement() + " " + vri.getLevel() + " " + vri.getMessage());
220 }
221 assertEquals(0, result.size());
222
223
224
225
226
227
228
229
230 validationType = DataDictionaryValidator.ValidationType.SKIP_REQUREDNESS_VALIDATIONS;
231 atp = this.getDefaultAtpInfo();
232 context = getContext1();
233 atp.setTypeKey(null);
234 result = intstance.validate(validationType, atp, context);
235 for (ValidationResult vri : result) {
236 System.out.println(vri.getElement() + " " + vri.getLevel() + " " + vri.getMessage());
237 }
238 assertEquals(0, result.size());
239
240
241 validationType = DataDictionaryValidator.ValidationType.FULL_VALIDATION;
242 atp = this.getDefaultAtpInfo();
243 context = getContext1();
244 atp.setName("this has \n an embedded return");
245 result = intstance.validate(validationType, atp, context);
246 for (ValidationResult vri : result) {
247 System.out.println(vri.getElement() + " " + vri.getLevel() + " " + vri.getMessage());
248 }
249 assertEquals(0, result.size());
250
251
252
253
254
255
256
257 validationType = DataDictionaryValidator.ValidationType.FULL_VALIDATION;
258 atp = this.getDefaultAtpInfo();
259 context = getContext1();
260 atp.setName(
261 "This has is a really long name in fact it is so long that it goes on and on and on and on sometimes I think it will go on forever"
262 + " but not really because the limit should be at 255 becaue that is the default in the dictionary and now I think I should stop typing");
263 result = intstance.validate(validationType, atp, context);
264 for (ValidationResult vri : result) {
265 System.out.println(vri.getElement() + " " + vri.getLevel() + " " + vri.getMessage());
266 }
267 assertEquals(0, result.size());
268
269
270
271
272
273
274
275 validationType = DataDictionaryValidator.ValidationType.FULL_VALIDATION;
276 atp = this.getDefaultAtpInfo();
277 context = getContext1();
278 atp.setName(
279 "test atp "
280 + " ");
281 result = intstance.validate(validationType, atp, context);
282 for (ValidationResult vri : result) {
283 System.out.println(vri.getElement() + " " + vri.getLevel() + " " + vri.getMessage());
284 }
285 assertEquals(0, result.size());
286
287
288
289
290
291
292
293 validationType = DataDictionaryValidator.ValidationType.FULL_VALIDATION;
294 atp = this.getDefaultAtpInfo();
295 context = getContext1();
296 atp.setDescr(new RichTextHelper().fromPlain("test atp description\n that is ok"));
297 result = intstance.validate(validationType, atp, context);
298 for (ValidationResult vri : result) {
299 System.out.println(vri.getElement() + " " + vri.getLevel() + " " + vri.getMessage());
300 }
301 assertEquals(0, result.size());
302
303
304 validationType = DataDictionaryValidator.ValidationType.FULL_VALIDATION;
305 atp = this.getDefaultAtpInfo();
306 context = getContext1();
307 atp.setDescr(new RichTextHelper().fromPlain("test atp description \nwith an invalid character tilde ~ in it"));
308 result = intstance.validate(validationType, atp, context);
309 for (ValidationResult vri : result) {
310 System.out.println(vri.getElement() + " " + vri.getLevel() + " " + vri.getMessage());
311 }
312
313 assertEquals(0, result.size());
314
315
316
317
318
319
320
321
322
323
324 validationType = DataDictionaryValidator.ValidationType.FULL_VALIDATION;
325 atp = this.getDefaultAtpInfo();
326 atp.setStateKey(AtpServiceConstants.ATP_OFFICIAL_STATE_KEY);
327 atp.setStartDate(null);
328 context = getContext1();
329 result = intstance.validate(validationType, atp, context);
330 for (ValidationResult vri : result) {
331 System.out.println(vri.getElement() + " " + vri.getLevel() + " " + vri.getMessage());
332 }
333 assertEquals(0, result.size());
334 }
335 }