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