001    /*
002     * Copyright 2006-2012 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    
017    package org.kuali.test;
018    
019    import org.junit.Assert;
020    import org.junit.Before;
021    import org.junit.Test;
022    import org.kuali.rice.kew.api.exception.WorkflowException;
023    import org.kuali.rice.krad.UserSession;
024    import org.kuali.rice.krad.maintenance.MaintenanceDocument;
025    import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
026    import org.kuali.rice.krad.util.GlobalVariables;
027    import org.kuali.rice.krad.util.KRADConstants;
028    import org.kuali.test.KRADTestCase;
029    
030    import static org.junit.Assert.assertEquals;
031    
032    /**
033     *  BaseMaintenanceDocumentTest is a base class for testing maintenance documents
034     *
035     *  <p>It provides test methods for setting up, editing, saving and copying a maintenance document</p>
036     *
037     *  @author Kuali Rice Team (rice.collab@kuali.org)
038     */
039    public abstract class BaseMaintenanceDocumentTest extends KRADTestCase {
040        private MaintenanceDocument document;
041        private String documentTypeName;
042        private String initiatorPrincipalName;
043    
044        @Override
045        public void setUp() throws Exception {
046            super.setUp();
047            GlobalVariables.setUserSession(new UserSession(getInitiatorPrincipalName()));
048            MaintenanceDocument maintenanceDocument =
049                    (MaintenanceDocument) KRADServiceLocatorWeb.getDocumentService().getNewDocument(getDocumentTypeName());
050            maintenanceDocument.getDocumentHeader().setDocumentDescription("test maintenance document");
051            setDocument(maintenanceDocument);
052        }
053    
054        @Before
055        public void setUpBeforeTest() {
056            GlobalVariables.getMessageMap().clearErrorMessages();
057        }
058    
059        /**
060         * Override this method to provide different value
061         *
062         * @return the document type name to use
063         */
064        protected String getDocumentTypeName() {
065            return documentTypeName;
066        }
067    
068        /**
069         * Override this method to provide different initiator
070         *
071         * @return the principal name to use as the initiator for the specified document type
072         */
073        protected String getInitiatorPrincipalName() {
074            return initiatorPrincipalName;
075        }
076    
077        /**
078         *  setup a new maintenance document
079         *
080         * @param document - the maintenance document being tested
081         */
082        protected void setupNewAccountMaintDoc(MaintenanceDocument document) {
083    
084            Object am = getNewMaintainableObject();
085    
086            document.getOldMaintainableObject().setDataObject(null);
087            document.getOldMaintainableObject().setDataObjectClass(am.getClass());
088            document.getNewMaintainableObject().setDataObject(am);
089            document.getNewMaintainableObject().setDataObjectClass(am.getClass());
090    
091            document.getNewMaintainableObject().setMaintenanceAction(KRADConstants.MAINTENANCE_NEW_ACTION);
092        }
093    
094        /**
095         *
096         * @return an object to set as the new maintainable object
097         */
098        protected abstract Object getNewMaintainableObject();
099    
100        /**
101         *
102         * @return an object to set as the old maintainable object
103         */
104        protected abstract Object getOldMaintainableObject();
105    
106        /**
107         * populate maintenance document with objects for editing
108         *
109         * @param document - the maintenance document being tested
110         */
111        protected void setupEditAccountMaintDoc(MaintenanceDocument document) {
112    
113            Object newAm = getNewMaintainableObject();
114            Object oldAm = getOldMaintainableObject();
115    
116            document.getOldMaintainableObject().setDataObject(oldAm);
117            document.getOldMaintainableObject().setDataObjectClass(oldAm.getClass());
118            document.getNewMaintainableObject().setDataObject(newAm);
119            document.getNewMaintainableObject().setDataObjectClass(newAm.getClass());
120    
121            document.getNewMaintainableObject().setMaintenanceAction(KRADConstants.MAINTENANCE_EDIT_ACTION);
122        }
123    
124        @Test
125        /**
126         * test creating a new maintenance document
127         */
128        public void test_NewDoc() {
129    
130            setupNewAccountMaintDoc(getDocument());
131    
132            Assert.assertEquals("Document should indicate New.", true, getDocument().isNew());
133            Assert.assertEquals("Document should not indicate Edit.", false, getDocument().isEdit());
134            Assert.assertEquals("Old BO should not be present.", false, getDocument().isOldDataObjectInDocument());
135        }
136    
137        @Test
138        /**
139         * test editing a maintenance document
140         */
141        public void test_EditDoc() {
142    
143            setupEditAccountMaintDoc(getDocument());
144    
145            Assert.assertEquals("Document should not indicate New.", false, getDocument().isNew());
146            Assert.assertEquals("Document should indicate Edit.", true, getDocument().isEdit());
147            Assert.assertEquals("Old BO should be present.", true, getDocument().isOldDataObjectInDocument());
148    
149        }
150    
151        @Test
152        /**
153         * test copying a maintenance document
154         */
155        public void test_CopyDoc() {
156    
157            setupEditAccountMaintDoc(getDocument());
158            getDocument().getNewMaintainableObject().setMaintenanceAction(KRADConstants.MAINTENANCE_COPY_ACTION);
159    
160            Assert.assertEquals("Document should indicate New.", true, getDocument().isNew());
161            Assert.assertEquals("Document should not indicate Edit.", false, getDocument().isEdit());
162            Assert.assertEquals("Old BO should be present.", true, getDocument().isOldDataObjectInDocument());
163    
164        }
165    
166        @Test
167        /**
168         * test saving a maintenance document
169         */
170        public void test_SaveNewDoc() throws WorkflowException {
171            setupNewAccountMaintDoc(getDocument());
172            KRADServiceLocatorWeb.getDocumentService().saveDocument(getDocument());
173            Assert.assertTrue(getDocument().getDocumentHeader().getWorkflowDocument().isSaved());
174        }
175    
176        /**
177         * gets the maintenance document that is created in the constructor
178         *
179         * @return a maintenance document of the type returned by {@link #getDocumentTypeName()}
180         */
181        public MaintenanceDocument getDocument() {
182            return document;
183        }
184    
185        /**
186         * set the maintenance document to use in the test
187         * @param document - the maintenance document
188         */
189        public void setDocument(MaintenanceDocument document) {
190            this.document = document;
191        }
192    }