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.enrollment.class2.courseoffering.service.impl;
17  
18  import org.junit.After;
19  import org.junit.Before;
20  import org.junit.Test;
21  import org.junit.runner.RunWith;
22  import org.kuali.student.r2.common.datadictionary.DataDictionaryValidator;
23  import org.kuali.student.r2.common.dto.ContextInfo;
24  import org.kuali.student.r2.common.dto.ValidationResultInfo;
25  import org.kuali.student.r2.common.infc.ValidationResult;
26  import org.kuali.student.r2.core.acal.dto.AcademicCalendarInfo;
27  import org.kuali.student.r2.core.constants.AcademicCalendarServiceConstants;
28  import org.springframework.test.context.ContextConfiguration;
29  import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
30  
31  import javax.annotation.Resource;
32  import java.text.DateFormat;
33  import java.text.SimpleDateFormat;
34  import java.util.Date;
35  import java.util.List;
36  
37  import static org.junit.Assert.assertEquals;
38  import static org.junit.Assert.assertTrue;
39  
40  /**
41   *
42   * @author nwright
43   */
44  @RunWith(SpringJUnit4ClassRunner.class)
45  @ContextConfiguration(locations = {"classpath:co-test-with-class2-mock-context.xml"})
46  public class TestRiceDataDictionaryValidatorImplAgainstAcal {
47  
48      public ContextInfo callContext = null;
49  
50      @Resource
51      protected DataDictionaryValidator validator;
52  
53  
54      public TestRiceDataDictionaryValidatorImplAgainstAcal() {
55      }
56  
57      @Before
58      public void setup() throws Exception {
59          callContext = new ContextInfo();
60          callContext.setPrincipalId("principalId.1");
61      }
62  
63      @After
64      public void tearDown() throws Exception {
65      }
66  
67  
68      private Date parseDate(String str) throws Exception {
69          DateFormat df = new SimpleDateFormat("yyyy-mm-dd");
70          return df.parse(str);
71      }
72  
73      private AcademicCalendarInfo getDefaultAcademicCalendarInfo() throws Exception {
74          AcademicCalendarInfo acal = new AcademicCalendarInfo();
75          acal.setId("org.kuali.test.acal");
76          acal.setName("test acal");
77          acal.setTypeKey(AcademicCalendarServiceConstants.ACADEMIC_CALENDAR_TYPE_KEY);
78          acal.setStateKey(AcademicCalendarServiceConstants.ACADEMIC_CALENDAR_DRAFT_STATE_KEY);
79          acal.setStartDate(parseDate("2010-01-01"));
80          acal.setEndDate(parseDate("2010-06-30"));
81          return acal;
82      }
83  
84      /**
85       * Test of validate method, of class RiceValidatorImpl.
86       */
87      @Test
88      public void testValidate1() throws Exception {
89          System.out.println("basic validation test has all required fields");
90  
91          callContext = new ContextInfo();
92          callContext.setPrincipalId("principalId");
93  
94          DataDictionaryValidator.ValidationType validationType = DataDictionaryValidator.ValidationType.FULL_VALIDATION;
95          AcademicCalendarInfo acal = this.getDefaultAcademicCalendarInfo();
96  
97          List<ValidationResultInfo> result = validator.validate(validationType, acal, callContext);
98          assertTrue(result.isEmpty());
99      }
100 
101     /**
102      * Test of validate method, of class RiceValidatorImpl.
103      */
104     @Test
105     public void testValidate2() throws Exception {
106         System.out.println("check that type key is required");
107 
108         DataDictionaryValidator.ValidationType validationType = DataDictionaryValidator.ValidationType.FULL_VALIDATION;
109         AcademicCalendarInfo acal = this.getDefaultAcademicCalendarInfo();
110         acal.setTypeKey(null);
111 
112         List<ValidationResultInfo> result = validator.validate(validationType, acal, callContext);
113         assertEquals(1, result.size());
114         for (ValidationResult vri : result) {
115             System.out.println(vri.getElement() + " " + vri.getLevel() + " " + vri.getMessage());
116             assertEquals("typeKey", vri.getElement());
117         }
118 
119         System.out.println("check that empty string is same as null");
120         acal.setTypeKey("");
121         result = validator.validate(validationType, acal, callContext);
122         assertEquals(1, result.size());
123         for (ValidationResult vri : result) {
124             System.out.println(vri.getElement() + " " + vri.getLevel() + " " + vri.getMessage());
125             assertEquals("typeKey", vri.getElement());
126         }
127 
128         System.out.println("check that a single blank string is same as null");
129         acal.setTypeKey(" ");
130         result = validator.validate(validationType, acal, callContext);
131         assertEquals(1, result.size());
132         for (ValidationResult vri : result) {
133             System.out.println(vri.getElement() + " " + vri.getLevel() + " " + vri.getMessage());
134             assertEquals("typeKey", vri.getElement());
135         }
136 
137         System.out.println("check that a lots of blanks is same as null");
138         acal.setTypeKey("         ");
139         result = validator.validate(validationType, acal, callContext);
140         assertEquals(1, result.size());
141         for (ValidationResult vri : result) {
142             System.out.println(vri.getElement() + " " + vri.getLevel() + " " + vri.getMessage());
143             assertEquals("typeKey", vri.getElement());
144         }
145 
146         System.out.println("check that tabs and newlines count as all whitespace and is the same as null");
147         acal.setTypeKey("\n\t\r");
148         result = validator.validate(validationType, acal, callContext);
149         assertEquals(1, result.size());
150         for (ValidationResult vri : result) {
151             System.out.println(vri.getElement() + " " + vri.getLevel() + " " + vri.getMessage());
152             assertEquals("typeKey", vri.getElement());
153         }
154 
155         System.out.println("check that we can skip the requiredness checks");
156         validationType = DataDictionaryValidator.ValidationType.SKIP_REQUREDNESS_VALIDATIONS;
157         result = validator.validate(validationType, acal, callContext);
158         assertTrue(result.isEmpty());
159     }
160 
161 }