View Javadoc

1   /*
2    * Copyright 2007-2008 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.service;
17  
18  import org.junit.Test;
19  import org.kuali.rice.krad.UserSession;
20  import org.kuali.rice.krad.test.document.AccountRequestDocument;
21  import org.kuali.rice.krad.test.document.AccountRequestDocumentWithCyclicalReference;
22  import org.kuali.rice.krad.util.GlobalVariables;
23  import org.kuali.rice.krad.util.MessageMap;
24  import org.kuali.test.KRADTestCase;
25  
26  import static org.junit.Assert.assertEquals;
27  
28  /**
29   * This class tests the DictionaryValidationService (currently only recursive validation is tested).
30   *
31   * @author Kuali Rice Team (rice.collab@kuali.org)
32   *
33   */
34  public class DictionaryValidationServiceTest extends KRADTestCase {
35  
36      public DictionaryValidationServiceTest() {
37      }
38  
39      @Override
40      public void setUp() throws Exception {
41          super.setUp();
42          GlobalVariables.setMessageMap(new MessageMap());
43          GlobalVariables.setUserSession(new UserSession("quickstart"));
44      }
45  
46      @Override
47      public void tearDown() throws Exception {
48          GlobalVariables.setMessageMap(new MessageMap());
49          GlobalVariables.setUserSession(null);
50          super.tearDown();
51      }
52  
53      /**
54       * This method tests recursive validation at a depth of zero
55       *
56       * @throws Exception
57       */
58      @Test public void testRecursiveValidation() throws Exception {
59          AccountRequestDocument travelDocument = (AccountRequestDocument) KRADServiceLocatorWeb.getDocumentService().getNewDocument("AccountRequest");
60          // set all required fields except 1
61          travelDocument.getDocumentHeader().setDocumentDescription("test document");
62          travelDocument.setReason1("reason1");
63          travelDocument.setReason2("reason2");
64          travelDocument.setRequester("requester");
65  
66          GlobalVariables.setMessageMap(new MessageMap());
67          KRADServiceLocatorWeb.getDictionaryValidationService().validateDocumentAndUpdatableReferencesRecursively(travelDocument, 0, true);
68          MessageMap errorMap = GlobalVariables.getMessageMap();
69          int recursiveZeroMessageMapSize = errorMap.getNumberOfPropertiesWithErrors();
70  
71          // errors should be 'account type code' and 'request type' both being required
72          assertEquals("Number of errors found is incorrect", 2, recursiveZeroMessageMapSize);
73      }
74  
75      /**
76       * This method tests recursive validation comparing multiple levels of recursion
77       *
78       * @throws Exception
79       */
80      @Test public void testRecursiveValidationMultiple() throws Exception {
81          AccountRequestDocument travelDocument = (AccountRequestDocument) KRADServiceLocatorWeb.getDocumentService().getNewDocument("AccountRequest");
82          // set all required fields except 1
83          travelDocument.getDocumentHeader().setDocumentDescription("test document");
84          travelDocument.setReason1("reason1");
85          travelDocument.setReason2("reason2");
86          travelDocument.setRequester("requester");
87  
88          GlobalVariables.setMessageMap(new MessageMap());
89          KRADServiceLocatorWeb.getDictionaryValidationService().validateDocumentAndUpdatableReferencesRecursively(travelDocument, 0, true);
90          MessageMap errorMap = GlobalVariables.getMessageMap();
91          int recursiveZeroMessageMapSize = errorMap.getNumberOfPropertiesWithErrors();
92  
93          GlobalVariables.setMessageMap(new MessageMap());
94          KRADServiceLocatorWeb.getDictionaryValidationService().validateDocumentAndUpdatableReferencesRecursively(travelDocument, 5, true);
95          MessageMap errorMap2 = GlobalVariables.getMessageMap();
96          int recursiveFiveMessageMapSize = errorMap2.getNumberOfPropertiesWithErrors();
97  
98          assertEquals("We should get the same number of errors no matter how deeply we recursively validate for this document", recursiveZeroMessageMapSize, recursiveFiveMessageMapSize);
99      }
100 
101     @Test public void testRecursiveValidationParentChildLoop() throws Exception {
102         AccountRequestDocumentWithCyclicalReference doc1 = (AccountRequestDocumentWithCyclicalReference) KRADServiceLocatorWeb.getDocumentService().getNewDocument("AccountRequest3");
103         // set all required fields except 1
104         doc1.getDocumentHeader().setDocumentDescription("test document 1");
105         doc1.setReason1("reason1");
106         doc1.setReason2("reason2");
107         doc1.setRequester("requester");
108 
109         AccountRequestDocumentWithCyclicalReference doc2 = (AccountRequestDocumentWithCyclicalReference) KRADServiceLocatorWeb.getDocumentService().getNewDocument("AccountRequest3");
110         doc2.getDocumentHeader().setDocumentDescription("test document 2");
111         doc2.setReason1("reason1a");
112         doc2.setReason2("reason2a");
113         doc2.setRequester("requester2");
114         doc2.setParent(doc1);
115         doc1.setChild(doc2);
116 
117         GlobalVariables.setMessageMap(new MessageMap());
118         KRADServiceLocatorWeb.getDictionaryValidationService().validateDocumentAndUpdatableReferencesRecursively(doc1, 5, true);
119         MessageMap errorMap = GlobalVariables.getMessageMap();
120         int recursiveFiveMessageMapSize = errorMap.getNumberOfPropertiesWithErrors();
121 
122         GlobalVariables.setMessageMap(new MessageMap());
123         KRADServiceLocatorWeb.getDictionaryValidationService().validateDocumentAndUpdatableReferencesRecursively(doc1, 10, true);
124         MessageMap errorMap2 = GlobalVariables.getMessageMap();
125         int recursiveTenMessageMapSize = errorMap2.getNumberOfPropertiesWithErrors();
126 
127         assertEquals("We should get the same number of errors no matter how deeply we recursively validate for this document", recursiveFiveMessageMapSize, recursiveTenMessageMapSize);
128     }
129     
130 }