View Javadoc

1   /**
2    * Copyright 2005-2014 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.io.IOException;
21  import java.io.InputStream;
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.krad.datadictionary.AttributeDefinition;
28  import org.kuali.rice.krad.datadictionary.validation.AttributeValueReader;
29  import org.kuali.rice.krad.datadictionary.validation.ErrorLevel;
30  import org.kuali.rice.krad.datadictionary.validation.SingleAttributeValueReader;
31  import org.kuali.rice.krad.datadictionary.validation.constraint.ValidCharactersConstraint;
32  import org.kuali.rice.krad.datadictionary.validation.processor.ValidCharactersConstraintProcessor;
33  import org.kuali.rice.krad.datadictionary.validation.result.ConstraintValidationResult;
34  import org.kuali.rice.krad.datadictionary.validation.result.DictionaryValidationResult;
35  
36  
37  /**
38   * Things this test should check:
39   * 
40   * 1. empty value check. (failure) {@link #testValueInvalidTimestampEmpty()}
41   * 2. value with valid timestamp. (success) {@link #testValueValidTimestamp()}
42   * 3. value with valid timestamp. (success) {@link #testValueValidTimestamp1()}
43   * 4. value with invalid timestamp. (failure) {@link testValueInvalidTimestamp()}
44   * 5. value with invalid timestamp. (failure) {@link testValueInvalidTimestamp1()}
45   * 6. value with invalid timestamp. (failure) {@link testValueInvalidTimestamp2()}
46   * 7. value with invalid timestamp. (failure) {@link testValueInvalidTimestamp3()}  
47   * 
48   * @author Kuali Rice Team (rice.collab@kuali.org) 
49   */
50  public class TimestampPatternConstraintTest {
51  	
52  	private final String PATTERN_CONSTRAINT = "validationPatternRegex.timestamp";
53  
54  	private AttributeDefinition timestampDefinition;
55  	
56  	private DictionaryValidationResult dictionaryValidationResult;
57  	
58  	private ValidCharactersConstraintProcessor processor;
59  	
60  	private String validTimestamp;
61  	private String validTimestamp1;
62  	private String invalidTimestampEmpty;
63  	private String invalidTimestamp;
64  	private String invalidTimestamp1;
65  	private String invalidTimestamp2;
66  	private String invalidTimestamp3;
67  		
68  	private ConfigurationBasedRegexPatternConstraint timestampPatternConstraint;		
69  	
70  	@Before
71  	public void setUp() throws Exception {			
72  		
73  		String regex = getProperty(PATTERN_CONSTRAINT);
74  		
75  		processor = new ValidCharactersConstraintProcessor();
76  		
77  		dictionaryValidationResult = new DictionaryValidationResult();
78  		dictionaryValidationResult.setErrorLevel(ErrorLevel.NOCONSTRAINT);
79  		
80  		validTimestamp = "2011-07-28 15:10:36.300";
81  		validTimestamp1 = "1936-07-28 15:10:36.9999999";		
82  		invalidTimestampEmpty = "";
83  		invalidTimestamp = "2011/07/28 15:10:36.300";
84  		invalidTimestamp1 = "2011-07-28 15:10:36.300 IST";
85  		invalidTimestamp2 = "2011-07-28";
86  		invalidTimestamp3 = "28-07-2011 15:10:36.300";
87  					
88  		timestampPatternConstraint = new ConfigurationBasedRegexPatternConstraint();
89  		timestampPatternConstraint.setValue(regex);
90  
91  		timestampDefinition = new AttributeDefinition();
92  		timestampDefinition.setName("timestamp");
93  		timestampDefinition.setValidCharactersConstraint(timestampPatternConstraint);												
94  	}
95  	
96  	@Test
97  	public void testValueInvalidTimestampEmpty() {
98  		ConstraintValidationResult result = process(invalidTimestampEmpty, "timestamp", timestampPatternConstraint);
99  		Assert.assertEquals(0, dictionaryValidationResult.getNumberOfWarnings());
100 		Assert.assertEquals(0, dictionaryValidationResult.getNumberOfErrors());
101 		Assert.assertEquals(ErrorLevel.INAPPLICABLE, result.getStatus());
102 		Assert.assertEquals(new ValidCharactersConstraintProcessor().getName(), result.getConstraintName());
103 	}
104 	
105 	@Test
106 	public void testValueValidTimestamp() {
107 		ConstraintValidationResult result = process(validTimestamp, "timestamp", timestampPatternConstraint);
108 		Assert.assertEquals(0, dictionaryValidationResult.getNumberOfWarnings());
109 		Assert.assertEquals(0, dictionaryValidationResult.getNumberOfErrors());
110 		Assert.assertEquals(ErrorLevel.OK, result.getStatus());
111 		Assert.assertEquals(new ValidCharactersConstraintProcessor().getName(), result.getConstraintName());
112 	}
113 	
114 	@Test
115 	public void testValueValidTimestamp1() {
116 		ConstraintValidationResult result = process(validTimestamp1, "Mtimestamp", timestampPatternConstraint);
117 		Assert.assertEquals(0, dictionaryValidationResult.getNumberOfWarnings());
118 		Assert.assertEquals(0, dictionaryValidationResult.getNumberOfErrors());
119 		Assert.assertEquals(ErrorLevel.OK, result.getStatus());
120 		Assert.assertEquals(new ValidCharactersConstraintProcessor().getName(), result.getConstraintName());
121 	}				
122 	
123 	@Test
124 	public void testValueInvalidTimestamp() {
125 		ConstraintValidationResult result = process(invalidTimestamp, "timestamp", timestampPatternConstraint);
126 		Assert.assertEquals(0, dictionaryValidationResult.getNumberOfWarnings());
127 		Assert.assertEquals(1, dictionaryValidationResult.getNumberOfErrors());
128 		Assert.assertEquals(ErrorLevel.ERROR, result.getStatus());
129 		Assert.assertEquals(new ValidCharactersConstraintProcessor().getName(), result.getConstraintName());
130 	}
131 	
132 	@Test
133 	public void testValueInvalidTimestamp1() {
134 		ConstraintValidationResult result = process(invalidTimestamp1, "timestamp", timestampPatternConstraint);
135 		Assert.assertEquals(0, dictionaryValidationResult.getNumberOfWarnings());
136 		Assert.assertEquals(1, dictionaryValidationResult.getNumberOfErrors());
137 		Assert.assertEquals(ErrorLevel.ERROR, result.getStatus());
138 		Assert.assertEquals(new ValidCharactersConstraintProcessor().getName(), result.getConstraintName());
139 	}
140 	
141 	@Test
142 	public void testValueInvalidTimestamp2() {
143 		ConstraintValidationResult result = process(invalidTimestamp2, "timestamp", timestampPatternConstraint);
144 		Assert.assertEquals(0, dictionaryValidationResult.getNumberOfWarnings());
145 		Assert.assertEquals(1, dictionaryValidationResult.getNumberOfErrors());
146 		Assert.assertEquals(ErrorLevel.ERROR, result.getStatus());
147 		Assert.assertEquals(new ValidCharactersConstraintProcessor().getName(), result.getConstraintName());
148 	}
149 	
150 	@Test
151 	public void testValueInvalidTimestamp3() {
152 		ConstraintValidationResult result = process(invalidTimestamp3, "timestamp", timestampPatternConstraint);
153 		Assert.assertEquals(0, dictionaryValidationResult.getNumberOfWarnings());
154 		Assert.assertEquals(1, dictionaryValidationResult.getNumberOfErrors());
155 		Assert.assertEquals(ErrorLevel.ERROR, result.getStatus());
156 		Assert.assertEquals(new ValidCharactersConstraintProcessor().getName(), result.getConstraintName());
157 	}		
158 	
159 	private ConstraintValidationResult process(Object object, String attributeName, ValidCharactersConstraint constraint) {
160 		AttributeValueReader attributeValueReader = new SingleAttributeValueReader(object, "org.kuali.rice.kns.datadictionary.validation.MockAddress", attributeName,  timestampDefinition);		
161 		Object value = attributeValueReader.getValue();
162 		return processor.process(dictionaryValidationResult, value, constraint, attributeValueReader).getFirstConstraintValidationResult();
163 	}
164 	
165 	private String getProperty(String key) {
166 		String value = null;
167 		String filePath = "org/kuali/rice/krad/ApplicationResources.properties";
168 		Properties properties = new Properties();
169 		try {			
170 			InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream(filePath);
171 			properties.load(in);
172 			value = properties.getProperty(key);			
173 		} catch (IOException e) {		
174 		}
175 		return value;	
176 	}
177 }