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.atp.dto.AtpInfo;
27  import org.kuali.student.r2.core.constants.AtpServiceConstants;
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 TestRiceDataDictionaryValidatorImplAgainstAtp {
47      public ContextInfo callContext = null;
48  
49      @Resource
50      protected DataDictionaryValidator validator;
51  
52  
53      public TestRiceDataDictionaryValidatorImplAgainstAtp() {
54      }
55  
56      @Before
57      public void setup() throws Exception {
58          callContext = new ContextInfo();
59          callContext.setPrincipalId("principalId.1");
60      }
61  
62      @After
63      public void tearDown() throws Exception {
64      }
65  
66  
67      private Date parseDate(String str) throws Exception {
68          DateFormat df = new SimpleDateFormat("yyyy-mm-dd");
69          return df.parse(str);
70      }
71  
72      private AtpInfo getDefaultAtpInfo() throws Exception {
73          AtpInfo atp = new AtpInfo();
74          atp.setId("org.kuali.test.atp");
75          atp.setName("test atp");
76          atp.setTypeKey(AtpServiceConstants.ATP_ACADEMIC_CALENDAR_TYPE_KEY);
77          atp.setStateKey(AtpServiceConstants.ATP_DRAFT_STATE_KEY);
78          atp.setStartDate(parseDate("2010-01-01"));
79          atp.setEndDate(parseDate("2010-06-30"));
80          return atp;
81      }
82  
83      /**
84       * Test of validate method, of class RiceValidatorImpl.
85       * Because there is no constraintProcessors provided for DictionaryValidationService in rice all tests are just
86       * to validate the xml file syntax. Any element setting is not validated.
87       */
88      @Test
89      public void testValidate() throws Exception {
90          // basic validation test has all required fields
91          DataDictionaryValidator.ValidationType validationType = DataDictionaryValidator.ValidationType.FULL_VALIDATION;
92          AtpInfo atp = this.getDefaultAtpInfo();
93  
94          List<ValidationResultInfo> result = validator.validate(validationType, atp, callContext);
95          assertTrue(result.isEmpty());
96  
97          // check that type key is required
98          atp.setTypeKey(null);
99          result = validator.validate(validationType, atp, callContext);
100         assertEquals(1, result.size());
101         assertEquals("typeKey", result.get(0).getElement());
102         assertEquals(ValidationResult.ErrorLevel.ERROR, result.get(0).getLevel());
103         assertEquals("error.required", result.get(0).getMessage());
104 
105         //test a config option
106         validationType = DataDictionaryValidator.ValidationType.SKIP_REQUREDNESS_VALIDATIONS;
107         result = validator.validate(validationType, atp, callContext);
108         assertTrue(result.isEmpty());
109     }
110 }