View Javadoc
1   /**
2    * Copyright 2005-2016 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.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/ecl2.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.rice.krad.datadictionary.validation.constraint;
17  
18  
19  
20  import java.util.ArrayList;
21  import java.util.List;
22  import java.util.Properties;
23  
24  import org.junit.Assert;
25  import org.junit.Before;
26  import org.junit.Test;
27  import org.kuali.rice.core.api.CoreConstants;
28  import org.kuali.rice.core.api.config.property.Config;
29  import org.kuali.rice.core.api.config.property.ConfigContext;
30  import org.kuali.rice.core.framework.config.property.SimpleConfig;
31  import org.kuali.rice.core.framework.config.property.SimpleConfig;
32  import org.kuali.rice.krad.datadictionary.AttributeDefinition;
33  import org.kuali.rice.krad.datadictionary.validation.AttributeValueReader;
34  import org.kuali.rice.krad.datadictionary.validation.ErrorLevel;
35  import org.kuali.rice.krad.datadictionary.validation.SingleAttributeValueReader;
36  import org.kuali.rice.krad.datadictionary.validation.constraint.ValidCharactersConstraint;
37  import org.kuali.rice.krad.datadictionary.validation.processor.ValidCharactersConstraintProcessor;
38  import org.kuali.rice.krad.datadictionary.validation.result.ConstraintValidationResult;
39  import org.kuali.rice.krad.datadictionary.validation.result.DictionaryValidationResult;
40  
41  
42  /**
43   * Things this test should check:
44   *
45   * 1. empty value check. (failure) {@link #testValueInvalidDateEmpty()}
46   * 2. value with valid date. (success) {@link #testValueValidDate()}
47   * 3. value with valid date. (success) {@link #testValueValidDate1()}
48   * 4. value with invalid date. (failure) {@link #testValueInvalidDate()}
49   * 5. value with invalid date. (failure) {@link #testValueInvalidDate1()}
50   * 6. value with invalid date. (failure) {@link #testValueInvalidDate2()}
51   * 7. value with invalid date. (failure) {@link #testValueInvalidDate3()}
52   *
53   * @author Kuali Rice Team (rice.collab@kuali.org)
54   */
55  public class DatePatternConstraintTest {
56  
57  	private AttributeDefinition dateDefinition;
58  
59  	private DictionaryValidationResult dictionaryValidationResult;
60  
61  	private ValidCharactersConstraintProcessor processor;
62  
63  	private String validDate;
64  	private String validDate1;
65  	private String invalidDateEmpty;
66  	private String invalidDate;
67  	private String invalidDate1;
68  	private String invalidDate2;
69  	private String invalidDate3;
70  
71  	private DatePatternConstraint datePatternConstraint;
72  
73  	@Before
74  	public void setUp() throws Exception {
75  
76  		String formats = "MM/dd/yy;MM/dd/yyyy;MM-dd-yy;MM-dd-yyyy";
77  
78  		Properties props = new Properties();
79  		props.put(CoreConstants.STRING_TO_DATE_FORMATS, formats);
80  		Config config = new SimpleConfig(props);
81  
82  		ConfigContext.init(config);
83  		ConfigContext.getCurrentContextConfig().putProperty(CoreConstants.STRING_TO_DATE_FORMATS, formats);
84  
85  		processor = new ValidCharactersConstraintProcessor();
86  
87  		dictionaryValidationResult = new DictionaryValidationResult();
88  		dictionaryValidationResult.setErrorLevel(ErrorLevel.NOCONSTRAINT);
89  
90  		validDate = "07-28-2011";
91  		validDate1 = "12-31-83";
92  		invalidDateEmpty = "";
93  		invalidDate = "31-12-2010";
94  		invalidDate1 = "31-12-2010 12:30:45.456";
95  		invalidDate2 = "2011-07-28 IST";
96  		invalidDate3 = "12/31/2011";
97  
98  		List<String> allowedFormats = new ArrayList<String>();
99  		allowedFormats.add("MM-dd-yy");
100 		allowedFormats.add("MM-dd-yyyy");
101 
102 		datePatternConstraint = new DatePatternConstraint();
103         datePatternConstraint.setMessageKey("validate.dummykey");
104         datePatternConstraint.setValidationMessageParams( new ArrayList<String>());
105 		datePatternConstraint.setAllowedFormats(allowedFormats);
106 
107 		dateDefinition = new AttributeDefinition();
108 		dateDefinition.setName("date");
109 		dateDefinition.setValidCharactersConstraint(datePatternConstraint);
110 	}
111 
112 	@Test
113 	public void testValueInvalidDateEmpty() {
114 		ConstraintValidationResult result = process(invalidDateEmpty, "date", datePatternConstraint);
115 		Assert.assertEquals(0, dictionaryValidationResult.getNumberOfWarnings());
116 		Assert.assertEquals(0, dictionaryValidationResult.getNumberOfErrors());
117 		Assert.assertEquals(ErrorLevel.INAPPLICABLE, result.getStatus());
118 		Assert.assertEquals(new ValidCharactersConstraintProcessor().getName(), result.getConstraintName());
119 	}
120 
121 	@Test
122 	public void testValueValidDate() {
123 		ConstraintValidationResult result = process(validDate, "date", datePatternConstraint);
124 		Assert.assertEquals(0, dictionaryValidationResult.getNumberOfWarnings());
125 		Assert.assertEquals(0, dictionaryValidationResult.getNumberOfErrors());
126 		Assert.assertEquals(ErrorLevel.OK, result.getStatus());
127 		Assert.assertEquals(new ValidCharactersConstraintProcessor().getName(), result.getConstraintName());
128 	}
129 
130 	@Test
131 	public void testValueValidDate1() {
132 		ConstraintValidationResult result = process(validDate1, "Mdate", datePatternConstraint);
133 		Assert.assertEquals(0, dictionaryValidationResult.getNumberOfWarnings());
134 		Assert.assertEquals(0, dictionaryValidationResult.getNumberOfErrors());
135 		Assert.assertEquals(ErrorLevel.OK, result.getStatus());
136 		Assert.assertEquals(new ValidCharactersConstraintProcessor().getName(), result.getConstraintName());
137 	}
138 
139 	@Test
140 	public void testValueInvalidDate() {
141 		ConstraintValidationResult result = process(invalidDate, "date", datePatternConstraint);
142 		Assert.assertEquals(0, dictionaryValidationResult.getNumberOfWarnings());
143 		Assert.assertEquals(1, dictionaryValidationResult.getNumberOfErrors());
144 		Assert.assertEquals(ErrorLevel.ERROR, result.getStatus());
145 		Assert.assertEquals(new ValidCharactersConstraintProcessor().getName(), result.getConstraintName());
146 	}
147 
148 	@Test
149 	public void testValueInvalidDate1() {
150 		ConstraintValidationResult result = process(invalidDate1, "date", datePatternConstraint);
151 		Assert.assertEquals(0, dictionaryValidationResult.getNumberOfWarnings());
152 		Assert.assertEquals(1, dictionaryValidationResult.getNumberOfErrors());
153 		Assert.assertEquals(ErrorLevel.ERROR, result.getStatus());
154 		Assert.assertEquals(new ValidCharactersConstraintProcessor().getName(), result.getConstraintName());
155 	}
156 
157 	@Test
158 	public void testValueInvalidDate2() {
159 		ConstraintValidationResult result = process(invalidDate2, "date", datePatternConstraint);
160 		Assert.assertEquals(0, dictionaryValidationResult.getNumberOfWarnings());
161 		Assert.assertEquals(1, dictionaryValidationResult.getNumberOfErrors());
162 		Assert.assertEquals(ErrorLevel.ERROR, result.getStatus());
163 		Assert.assertEquals(new ValidCharactersConstraintProcessor().getName(), result.getConstraintName());
164 	}
165 
166 	@Test
167 	public void testValueInvalidDate3() {
168 		ConstraintValidationResult result = process(invalidDate3, "date", datePatternConstraint);
169 		Assert.assertEquals(0, dictionaryValidationResult.getNumberOfWarnings());
170 		Assert.assertEquals(1, dictionaryValidationResult.getNumberOfErrors());
171 		Assert.assertEquals(ErrorLevel.ERROR, result.getStatus());
172 		Assert.assertEquals(new ValidCharactersConstraintProcessor().getName(), result.getConstraintName());
173 	}
174 
175 	private ConstraintValidationResult process(Object object, String attributeName, ValidCharactersConstraint constraint) {
176 		AttributeValueReader attributeValueReader = new SingleAttributeValueReader(object, "org.kuali.rice.kns.datadictionary.validation.MockAddress", attributeName,  dateDefinition);
177 		Object value = attributeValueReader.getValue();
178 		return processor.process(dictionaryValidationResult, value, constraint, attributeValueReader).getFirstConstraintValidationResult();
179 	}
180 }