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.apache.log4j.Logger;
19  import org.junit.After;
20  import org.junit.Before;
21  import org.junit.Test;
22  import org.junit.runner.RunWith;
23  import org.kuali.student.r2.common.datadictionary.DataDictionaryValidator;
24  import org.kuali.student.r2.common.dto.ContextInfo;
25  import org.kuali.student.r2.common.dto.ValidationResultInfo;
26  import org.kuali.student.r2.common.infc.ValidationResult;
27  import org.kuali.student.r2.common.util.RichTextHelper;
28  import org.kuali.student.r2.core.atp.dto.AtpInfo;
29  import org.kuali.student.r2.core.constants.AtpServiceConstants;
30  import org.springframework.context.ApplicationContext;
31  import org.springframework.context.support.ClassPathXmlApplicationContext;
32  import org.springframework.test.context.ContextConfiguration;
33  import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
34  
35  import javax.annotation.Resource;
36  import java.text.DateFormat;
37  import java.text.ParseException;
38  import java.text.SimpleDateFormat;
39  import java.util.Date;
40  import java.util.List;
41  
42  import static org.junit.Assert.assertEquals;
43  
44  /**
45   *
46   * @author nwright
47   */
48  @RunWith(SpringJUnit4ClassRunner.class)
49  @ContextConfiguration(locations = {"classpath:co-test-with-class2-mock-context.xml"})
50  public class TestRiceDataDictionaryValidatorImplAgainstAtp {
51      private static final Logger log = Logger.getLogger(TestRiceDataDictionaryValidatorImplAgainstAtp.class);
52  
53      public ContextInfo callContext = null;
54  
55      @Resource
56      protected DataDictionaryValidator validator;
57  
58  
59      public TestRiceDataDictionaryValidatorImplAgainstAtp() {
60      }
61  
62      @Before
63      public void setup() throws Exception {
64          callContext = new ContextInfo();
65          callContext.setPrincipalId("principalId.1");
66      }
67  
68      @After
69      public void tearDown() throws Exception {
70      }
71  
72  
73      private Date parseDate(String str) {
74          DateFormat df = new SimpleDateFormat("yyyy-mm-dd");
75          Date date = null;
76          try {
77              date = df.parse(str);
78          } catch (ParseException ex) {
79              throw new IllegalArgumentException(str, ex);
80          }
81          return date;
82      }
83  
84      private AtpInfo getDefaultAtpInfo() {
85          AtpInfo atp = new AtpInfo();
86          atp.setId("org.kuali.test.atp");
87          atp.setName("test atp");
88          atp.setTypeKey(AtpServiceConstants.ATP_ACADEMIC_CALENDAR_TYPE_KEY);
89          atp.setStateKey(AtpServiceConstants.ATP_DRAFT_STATE_KEY);
90          atp.setStartDate(parseDate("2010-01-01"));
91          atp.setEndDate(parseDate("2010-06-30"));
92          return atp;
93      }
94  
95      /**
96       * Test of validate method, of class RiceValidatorImpl.
97       * Because there is no constraintProcessors provided for DictionaryValidationService in rice all tests are just
98       * to validate the xml file syntax. Any element setting is not validated.
99       */
100     @Test
101     public void testValidate() throws Exception {
102         // basic validation test has all required fields
103         DataDictionaryValidator.ValidationType validationType = DataDictionaryValidator.ValidationType.FULL_VALIDATION;
104         AtpInfo atp = this.getDefaultAtpInfo();
105 
106         List<ValidationResultInfo> result = validator.validate(validationType, atp, callContext);
107         assertEquals(0, result.size());
108 
109         // check that type key is required
110         atp.setTypeKey(null);
111         result = validator.validate(validationType, atp, callContext);
112         assertEquals(1, result.size());
113         assertEquals("typeKey", result.get(0).getElement());
114         assertEquals(ValidationResult.ErrorLevel.ERROR, result.get(0).getLevel());
115         assertEquals("error.required", result.get(0).getMessage());
116 
117         //test a config option
118         validationType = DataDictionaryValidator.ValidationType.SKIP_REQUREDNESS_VALIDATIONS;
119         result = validator.validate(validationType, atp, callContext);
120         assertEquals(0, result.size());
121     }
122 }