View Javadoc

1   /*
2    * Copyright 2011 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 1.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl1.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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   * @author nwright
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       * Subclasses may override this method to customize the location(s) of the Rice configuration.
70       * By default it is: classpath:META-INF/" + getModuleName().toLowerCase() + "-test-config.xml"
71       * @return List of config locations to add to this tests config location.
72       */
73      protected List<String> getConfigLocations() {
74          List<String> configLocations = new ArrayList<String>();
75  //        configLocations.add(getRiceMasterDefaultConfigFile());
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      * Test of validate method, of class RiceValidatorImpl.
127      * Because there is no constraintProcessors provided for DictionaryValidationService in rice all tests are just
128      * to validate the xml file syntax. Any element setting is not validated.
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         // basic validation test has all required fields
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         // check that type key is required
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         assertEquals("typeKey", result.get(0).getElement());
160         assertEquals(new Integer(2), result.get(0).getLevel());
161         assertEquals("error.required", result.get(0).getMessage());
162 */
163 
164         // check that empty string is same as null
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         assertEquals("typeKey", result.get(0).getElement());
176         assertEquals(new Integer(2), result.get(0).getLevel());
177         assertEquals("error.required", result.get(0).getMessage());
178 */
179 
180         // check that a single blank string is same as null
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         assertEquals("typeKey", result.get(0).getElement());
192         assertEquals(new Integer(2), result.get(0).getLevel());
193         assertEquals("error.required", result.get(0).getMessage());
194 */
195 
196         // check that a lots of blanks is same as null
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         assertEquals("typeKey", result.get(0).getElement());
208         assertEquals(new Integer(2), result.get(0).getLevel());
209         assertEquals("error.required", result.get(0).getMessage());
210 */
211 
212         // check that tabs and newlines count as all whitespace and is the same as null
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         assertEquals("typeKey", result.get(0).getElement());
224         assertEquals(new Integer(2), result.get(0).getLevel());
225         assertEquals("error.required", result.get(0).getMessage());
226 */
227 
228 
229         // check that we can skip the requiredness checks 
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         // check that valid chars catches that the name cannot have an embedded new line
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         assertEquals("name", result.get(0).getElement());
252         assertEquals(new Integer(2), result.get(0).getLevel());
253         assertEquals("error.invalidFormat", result.get(0).getMessage());
254 */
255 
256         // check that name cannot exceed 255
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         assertEquals("name", result.get(0).getElement());
270         assertEquals(new Integer(2), result.get(0).getLevel());
271         assertEquals("error.outOfRange", result.get(0).getMessage());
272 */
273 
274         // check that the name does not get trimmed before comparing it to not exceed 255
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         assertEquals("name", result.get(0).getElement());
288         assertEquals(new Integer(2), result.get(0).getLevel());
289         assertEquals("error.outOfRange", result.get(0).getMessage());
290 */
291 
292         // check reference to a complex sub-structure (descr)
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         // check reference to a complex sub-structure (descr) with bad data in it
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         // 2 because 1 for plain and 1 for formatted
313         assertEquals(0, result.size());
314 /*
315         assertEquals("descr.plain", result.get(0).getElement());
316         assertEquals(new Integer(2), result.get(0).getLevel());
317         assertEquals("error.invalidFormat", result.get(0).getMessage());
318         assertEquals("descr.formatted", result.get(1).getElement());
319         assertEquals(new Integer(2), result.get(1).getLevel());
320         assertEquals("error.invalidFormat", result.get(1).getMessage());
321 */
322 
323         // check start date required if official
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 }