/krad/krad-web-framework/src/test/java/org/kuali/rice/krad/datadictionary/validation/processor/CollectionSizeConstraintProcessorTest.java

https://github.com/sbower/kuali-rice-1 · Java · 307 lines · 219 code · 53 blank · 35 comment · 0 complexity · 557af8678c02cc844ae17b0a1803685c MD5 · raw file

  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.rice.krad.datadictionary.validation.processor;
  17. import org.junit.Assert;
  18. import org.junit.Before;
  19. import org.junit.Test;
  20. import org.kuali.rice.krad.datadictionary.BusinessObjectEntry;
  21. import org.kuali.rice.krad.datadictionary.CollectionDefinition;
  22. import org.kuali.rice.krad.datadictionary.validation.Address;
  23. import org.kuali.rice.krad.datadictionary.validation.AttributeValueReader;
  24. import org.kuali.rice.krad.datadictionary.validation.Company;
  25. import org.kuali.rice.krad.datadictionary.validation.DictionaryObjectAttributeValueReader;
  26. import org.kuali.rice.krad.datadictionary.validation.Employee;
  27. import org.kuali.rice.krad.datadictionary.validation.ErrorLevel;
  28. import org.kuali.rice.krad.datadictionary.validation.result.ConstraintValidationResult;
  29. import org.kuali.rice.krad.datadictionary.validation.result.DictionaryValidationResult;
  30. import java.util.ArrayList;
  31. import java.util.Collection;
  32. import java.util.Collections;
  33. import java.util.List;
  34. /**
  35. * Things this test should check:
  36. *
  37. * 1. collection size within range (success) {@link #testSimpleCollectionSizeWithinRangeSuccess()}
  38. * 2. collection size at top of range (success) {@link #testSimpleCollectionSizeAtTopOfRangeSuccess()}
  39. * 3. collection size at bottom of range (success) {@link #testSimpleCollectionSizeAtBottomOfRangeSuccess()}
  40. * 4. collection size below range (failure) {@link #testSimpleCollectionSizeBelowRangeFailure()}
  41. * 5. collection size above range (failure) {@link #testSimpleCollectionSizeAboveRangeFailure()}
  42. * 6. no range constraints defined (success) {@link #testSimpleCollectionSizeUnconstrainedSuccess()}
  43. *
  44. * @author Kuali Rice Team (rice.collab@kuali.org)
  45. */
  46. public class CollectionSizeConstraintProcessorTest {
  47. private CollectionSizeConstraintProcessor processor;
  48. private CollectionDefinition constrained0to2;
  49. private CollectionDefinition constrained0to3;
  50. private CollectionDefinition constrained2to4;
  51. private CollectionDefinition constrained3to6;
  52. private CollectionDefinition constrained5to12;
  53. private CollectionDefinition unconstrained;
  54. private Company companyWithThreeAddressesAndThreeEmployees;
  55. @Before
  56. public void setUp() throws Exception {
  57. processor = new CollectionSizeConstraintProcessor();
  58. companyWithThreeAddressesAndThreeEmployees = new Company("3M");
  59. List<Address> addresses = new ArrayList<Address>();
  60. addresses.add(new Address("123 Broadway", "Suite 1200", "New York", "NY", "10005", "USA", null));
  61. addresses.add(new Address("124 Broadway", "Suite 1300", "New York", "NY", "10005", "USA", null));
  62. addresses.add(new Address("125 Broadway", "Suite 1400", "New York", "NY", "10005", "USA", null));
  63. companyWithThreeAddressesAndThreeEmployees.setLocations(addresses);
  64. List<Employee> employees = new ArrayList<Employee>();
  65. employees.add(new Employee());
  66. employees.add(new Employee());
  67. employees.add(new Employee());
  68. companyWithThreeAddressesAndThreeEmployees.setEmployees(employees);
  69. constrained0to2 = new CollectionDefinition() {
  70. @Override
  71. public String getLabel() {
  72. return "Employees(s)";
  73. }
  74. @Override
  75. public String getName() {
  76. return "employees";
  77. }
  78. @Override
  79. public Integer getMaximumNumberOfElements() {
  80. return Integer.valueOf(2);
  81. }
  82. @Override
  83. public Integer getMinimumNumberOfElements() {
  84. return Integer.valueOf(0);
  85. }
  86. };
  87. constrained0to3 = new CollectionDefinition() {
  88. @Override
  89. public String getLabel() {
  90. return "Employee(s)";
  91. }
  92. @Override
  93. public String getName() {
  94. return "employees";
  95. }
  96. @Override
  97. public Integer getMaximumNumberOfElements() {
  98. return Integer.valueOf(3);
  99. }
  100. @Override
  101. public Integer getMinimumNumberOfElements() {
  102. return Integer.valueOf(0);
  103. }
  104. };
  105. constrained2to4 = new CollectionDefinition() {
  106. @Override
  107. public String getLabel() {
  108. return "Employees";
  109. }
  110. @Override
  111. public String getName() {
  112. return "employees";
  113. }
  114. @Override
  115. public Integer getMaximumNumberOfElements() {
  116. return Integer.valueOf(4);
  117. }
  118. @Override
  119. public Integer getMinimumNumberOfElements() {
  120. return Integer.valueOf(2);
  121. }
  122. };
  123. constrained3to6 = new CollectionDefinition() {
  124. @Override
  125. public String getLabel() {
  126. return "Employees";
  127. }
  128. @Override
  129. public String getName() {
  130. return "employees";
  131. }
  132. @Override
  133. public Integer getMaximumNumberOfElements() {
  134. return Integer.valueOf(6);
  135. }
  136. @Override
  137. public Integer getMinimumNumberOfElements() {
  138. return Integer.valueOf(3);
  139. }
  140. };
  141. constrained5to12 = new CollectionDefinition() {
  142. @Override
  143. public String getLabel() {
  144. return "Employee(s)";
  145. }
  146. @Override
  147. public String getName() {
  148. return "employees";
  149. }
  150. @Override
  151. public Integer getMaximumNumberOfElements() {
  152. return Integer.valueOf(12);
  153. }
  154. @Override
  155. public Integer getMinimumNumberOfElements() {
  156. return Integer.valueOf(5);
  157. }
  158. };
  159. unconstrained = new CollectionDefinition() {
  160. @Override
  161. public String getLabel() {
  162. return "Empployee(s)";
  163. }
  164. @Override
  165. public String getName() {
  166. return "employees";
  167. }
  168. @Override
  169. public Integer getMaximumNumberOfElements() {
  170. return null;
  171. }
  172. @Override
  173. public Integer getMinimumNumberOfElements() {
  174. return null;
  175. }
  176. };
  177. }
  178. @Test
  179. public void testSimpleCollectionSizeWithinRangeSuccess() {
  180. DictionaryValidationResult dictionaryValidationResult = new DictionaryValidationResult();
  181. dictionaryValidationResult.setErrorLevel(ErrorLevel.NOCONSTRAINT);
  182. ConstraintValidationResult result = process(dictionaryValidationResult, companyWithThreeAddressesAndThreeEmployees, constrained2to4, "employees");
  183. Assert.assertEquals(0, dictionaryValidationResult.getNumberOfWarnings());
  184. Assert.assertEquals(0, dictionaryValidationResult.getNumberOfErrors());
  185. Assert.assertEquals(ErrorLevel.OK, result.getStatus());
  186. Assert.assertEquals(new CollectionSizeConstraintProcessor().getName(), result.getConstraintName());
  187. }
  188. @Test
  189. public void testSimpleCollectionSizeAtTopOfRangeSuccess() {
  190. DictionaryValidationResult dictionaryValidationResult = new DictionaryValidationResult();
  191. dictionaryValidationResult.setErrorLevel(ErrorLevel.NOCONSTRAINT);
  192. ConstraintValidationResult result = process(dictionaryValidationResult, companyWithThreeAddressesAndThreeEmployees, constrained0to3, "employees");
  193. Assert.assertEquals(0, dictionaryValidationResult.getNumberOfWarnings());
  194. Assert.assertEquals(0, dictionaryValidationResult.getNumberOfErrors());
  195. Assert.assertEquals(ErrorLevel.OK, result.getStatus());
  196. Assert.assertEquals(new CollectionSizeConstraintProcessor().getName(), result.getConstraintName());
  197. }
  198. @Test
  199. public void testSimpleCollectionSizeAtBottomOfRangeSuccess() {
  200. DictionaryValidationResult dictionaryValidationResult = new DictionaryValidationResult();
  201. dictionaryValidationResult.setErrorLevel(ErrorLevel.NOCONSTRAINT);
  202. ConstraintValidationResult result = process(dictionaryValidationResult, companyWithThreeAddressesAndThreeEmployees, constrained3to6, "employees");
  203. Assert.assertEquals(0, dictionaryValidationResult.getNumberOfWarnings());
  204. Assert.assertEquals(0, dictionaryValidationResult.getNumberOfErrors());
  205. Assert.assertEquals(ErrorLevel.OK, result.getStatus());
  206. Assert.assertEquals(new CollectionSizeConstraintProcessor().getName(), result.getConstraintName());
  207. }
  208. /*
  209. * Verifies that a company object with a collection attribute 'contactEmails' that has 3 elements returns a validation error when the collection
  210. * size is constrained to be between 5 and 12 elements
  211. */
  212. @Test
  213. public void testSimpleCollectionSizeBelowRangeFailure() {
  214. DictionaryValidationResult dictionaryValidationResult = new DictionaryValidationResult();
  215. dictionaryValidationResult.setErrorLevel(ErrorLevel.NOCONSTRAINT);
  216. ConstraintValidationResult result = process(dictionaryValidationResult, companyWithThreeAddressesAndThreeEmployees, constrained5to12, "employees");
  217. Assert.assertEquals(0, dictionaryValidationResult.getNumberOfWarnings());
  218. Assert.assertEquals(1, dictionaryValidationResult.getNumberOfErrors());
  219. Assert.assertEquals(ErrorLevel.ERROR, result.getStatus());
  220. Assert.assertEquals(new CollectionSizeConstraintProcessor().getName(), result.getConstraintName());
  221. }
  222. /*
  223. * Verifies that a company object with a collection attribute 'contactEmails' that has 3 elements returns a validation error when the collection
  224. * size is constrained to be between 0 and 2 elements
  225. */
  226. @Test
  227. public void testSimpleCollectionSizeAboveRangeFailure() {
  228. DictionaryValidationResult dictionaryValidationResult = new DictionaryValidationResult();
  229. dictionaryValidationResult.setErrorLevel(ErrorLevel.NOCONSTRAINT);
  230. ConstraintValidationResult result = process(dictionaryValidationResult, companyWithThreeAddressesAndThreeEmployees, constrained0to2, "employees");
  231. Assert.assertEquals(0, dictionaryValidationResult.getNumberOfWarnings());
  232. Assert.assertEquals(1, dictionaryValidationResult.getNumberOfErrors());
  233. Assert.assertEquals(ErrorLevel.ERROR, result.getStatus());
  234. Assert.assertEquals(new CollectionSizeConstraintProcessor().getName(), result.getConstraintName());
  235. }
  236. @Test
  237. public void testSimpleCollectionSizeUnconstrainedSuccess() {
  238. DictionaryValidationResult dictionaryValidationResult = new DictionaryValidationResult();
  239. dictionaryValidationResult.setErrorLevel(ErrorLevel.NOCONSTRAINT);
  240. ConstraintValidationResult result = process(dictionaryValidationResult, companyWithThreeAddressesAndThreeEmployees, unconstrained, "employees");
  241. Assert.assertEquals(0, dictionaryValidationResult.getNumberOfWarnings());
  242. Assert.assertEquals(0, dictionaryValidationResult.getNumberOfErrors());
  243. Assert.assertEquals(ErrorLevel.NOCONSTRAINT, result.getStatus());
  244. Assert.assertEquals(new CollectionSizeConstraintProcessor().getName(), result.getConstraintName());
  245. }
  246. private ConstraintValidationResult process(DictionaryValidationResult dictionaryValidationResult, Object object, CollectionDefinition definition, String attributeName) {
  247. BusinessObjectEntry entry = new BusinessObjectEntry();
  248. entry.setCollections(Collections.singletonList((CollectionDefinition)definition));
  249. AttributeValueReader attributeValueReader = new DictionaryObjectAttributeValueReader(object, "org.kuali.rice.kns.datadictionary.validation.Company", entry);
  250. attributeValueReader.setAttributeName(attributeName);
  251. Collection<?> value = (Collection<?>)attributeValueReader.getValue();
  252. return processor.process(dictionaryValidationResult, value, definition, attributeValueReader).getFirstConstraintValidationResult();
  253. }
  254. }