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