001    /**
002     * Copyright 2005-2013 The Kuali Foundation
003     *
004     * Licensed under the Educational Community License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     * http://www.opensource.org/licenses/ecl2.php
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    package org.kuali.rice.krad.service;
017    
018    import org.junit.Test;
019    import org.kuali.rice.krad.UserSession;
020    import org.kuali.rice.krad.test.document.AccountRequestDocument;
021    import org.kuali.rice.krad.test.document.AccountRequestDocumentWithCyclicalReference;
022    import org.kuali.rice.krad.util.GlobalVariables;
023    import org.kuali.rice.krad.util.MessageMap;
024    import org.kuali.rice.krad.test.KRADTestCase;
025    
026    import static org.junit.Assert.assertEquals;
027    
028    /**
029     * This class tests the DictionaryValidationService (currently only recursive validation is tested).
030     *
031     * @author Kuali Rice Team (rice.collab@kuali.org)
032     *
033     */
034    public class DictionaryValidationServiceTest extends KRADTestCase {
035    
036        public DictionaryValidationServiceTest() {
037        }
038    
039        @Override
040        public void setUp() throws Exception {
041            super.setUp();
042            GlobalVariables.setMessageMap(new MessageMap());
043            GlobalVariables.setUserSession(new UserSession("quickstart"));
044        }
045    
046        @Override
047        public void tearDown() throws Exception {
048            GlobalVariables.setMessageMap(new MessageMap());
049            GlobalVariables.setUserSession(null);
050            super.tearDown();
051        }
052    
053        /**
054         * This method tests recursive validation at a depth of zero
055         *
056         * @throws Exception
057         */
058        @Test public void testRecursiveValidation() throws Exception {
059            AccountRequestDocument travelDocument = (AccountRequestDocument) KRADServiceLocatorWeb.getDocumentService().getNewDocument("AccountRequest");
060            // set all required fields except 1
061            travelDocument.getDocumentHeader().setDocumentDescription("test document");
062            travelDocument.setReason1("reason1");
063            travelDocument.setReason2("reason2");
064            travelDocument.setRequester("requester");
065    
066            GlobalVariables.setMessageMap(new MessageMap());
067            KRADServiceLocatorWeb.getDictionaryValidationService().validateDocumentAndUpdatableReferencesRecursively(travelDocument, 0, true);
068            MessageMap errorMap = GlobalVariables.getMessageMap();
069            int recursiveZeroMessageMapSize = errorMap.getNumberOfPropertiesWithErrors();
070    
071            // errors should be 'account type code' and 'request type' both being required
072            assertEquals("Number of errors found is incorrect", 2, recursiveZeroMessageMapSize);
073        }
074    
075        /**
076         * This method tests recursive validation comparing multiple levels of recursion
077         *
078         * @throws Exception
079         */
080        @Test public void testRecursiveValidationMultiple() throws Exception {
081            AccountRequestDocument travelDocument = (AccountRequestDocument) KRADServiceLocatorWeb.getDocumentService().getNewDocument("AccountRequest");
082            // set all required fields except 1
083            travelDocument.getDocumentHeader().setDocumentDescription("test document");
084            travelDocument.setReason1("reason1");
085            travelDocument.setReason2("reason2");
086            travelDocument.setRequester("requester");
087    
088            GlobalVariables.setMessageMap(new MessageMap());
089            KRADServiceLocatorWeb.getDictionaryValidationService().validateDocumentAndUpdatableReferencesRecursively(travelDocument, 0, true);
090            MessageMap errorMap = GlobalVariables.getMessageMap();
091            int recursiveZeroMessageMapSize = errorMap.getNumberOfPropertiesWithErrors();
092    
093            GlobalVariables.setMessageMap(new MessageMap());
094            KRADServiceLocatorWeb.getDictionaryValidationService().validateDocumentAndUpdatableReferencesRecursively(travelDocument, 5, true);
095            MessageMap errorMap2 = GlobalVariables.getMessageMap();
096            int recursiveFiveMessageMapSize = errorMap2.getNumberOfPropertiesWithErrors();
097    
098            assertEquals("We should get the same number of errors no matter how deeply we recursively validate for this document", recursiveZeroMessageMapSize, recursiveFiveMessageMapSize);
099        }
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    }