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    public abstract class BaseMaintenanceDocumentTest extends KRADTestCase {
033        private MaintenanceDocument document;
034        private String documentTypeName;
035        private String initiatorPrincipalName;
036    
037        @Override
038        public void setUp() throws Exception {
039            super.setUp();
040            GlobalVariables.setUserSession(new UserSession(getInitiatorPrincipalName()));
041            MaintenanceDocument maintenanceDocument =
042                    (MaintenanceDocument) KRADServiceLocatorWeb.getDocumentService().getNewDocument(getDocumentTypeName());
043            maintenanceDocument.getDocumentHeader().setDocumentDescription("test maintenance document");
044            setDocument(maintenanceDocument);
045        }
046    
047        @Before
048        public void setUpBeforeTest() {
049            GlobalVariables.getMessageMap().clearErrorMessages();
050        }
051    
052        /**
053         * Override this method to provide different value
054         *
055         * @return the document type name to use
056         */
057        protected String getDocumentTypeName() {
058            return documentTypeName;
059        }
060    
061        /**
062         * Override this method to provide different initiator
063         *
064         * @return the principal name to use as the initiator for the specified document type
065         */
066        protected String getInitiatorPrincipalName() {
067            return initiatorPrincipalName;
068        }
069    
070        protected void setupNewAccountMaintDoc(MaintenanceDocument document) {
071    
072            Object am = getNewMaintainableObject();
073    
074            document.getOldMaintainableObject().setDataObject(null);
075            document.getOldMaintainableObject().setDataObjectClass(am.getClass());
076            document.getNewMaintainableObject().setDataObject(am);
077            document.getNewMaintainableObject().setDataObjectClass(am.getClass());
078    
079            document.getNewMaintainableObject().setMaintenanceAction(KRADConstants.MAINTENANCE_NEW_ACTION);
080        }
081    
082        /**
083         *
084         * @return an object to set as the new maintainable object
085         */
086        protected abstract Object getNewMaintainableObject();
087    
088        /**
089         *
090         * @return an object to set as the old maintainable object
091         */
092        protected abstract Object getOldMaintainableObject();
093    
094        /**
095         * populate maintenance document with objects for editing
096         *
097         * @param document - the maintenance document being tested
098         */
099        protected void setupEditAccountMaintDoc(MaintenanceDocument document) {
100    
101            Object newAm = getNewMaintainableObject();
102            Object oldAm = getOldMaintainableObject();
103    
104            document.getOldMaintainableObject().setDataObject(oldAm);
105            document.getOldMaintainableObject().setDataObjectClass(oldAm.getClass());
106            document.getNewMaintainableObject().setDataObject(newAm);
107            document.getNewMaintainableObject().setDataObjectClass(newAm.getClass());
108    
109            document.getNewMaintainableObject().setMaintenanceAction(KRADConstants.MAINTENANCE_EDIT_ACTION);
110        }
111    
112        @Test
113        /**
114         * test creating a new maintenance document
115         */
116        public void test_NewDoc() {
117    
118            setupNewAccountMaintDoc(getDocument());
119    
120            Assert.assertEquals("Document should indicate New.", true, getDocument().isNew());
121            Assert.assertEquals("Document should not indicate Edit.", false, getDocument().isEdit());
122            Assert.assertEquals("Old BO should not be present.", false, getDocument().isOldDataObjectInDocument());
123        }
124    
125        @Test
126        /**
127         * test editing
128         */
129        public void test_EditDoc() {
130    
131            setupEditAccountMaintDoc(getDocument());
132    
133            Assert.assertEquals("Document should not indicate New.", false, getDocument().isNew());
134            Assert.assertEquals("Document should indicate Edit.", true, getDocument().isEdit());
135            Assert.assertEquals("Old BO should be present.", true, getDocument().isOldDataObjectInDocument());
136    
137        }
138    
139        @Test
140        /**
141         * test copying
142         */
143        public void test_CopyDoc() {
144    
145            setupEditAccountMaintDoc(getDocument());
146            getDocument().getNewMaintainableObject().setMaintenanceAction(KRADConstants.MAINTENANCE_COPY_ACTION);
147    
148            Assert.assertEquals("Document should indicate New.", true, getDocument().isNew());
149            Assert.assertEquals("Document should not indicate Edit.", false, getDocument().isEdit());
150            Assert.assertEquals("Old BO should be present.", true, getDocument().isOldDataObjectInDocument());
151    
152        }
153    
154        @Test
155        /**
156         * test saving
157         */
158        public void test_SaveNewDoc() throws WorkflowException {
159            setupNewAccountMaintDoc(getDocument());
160            KRADServiceLocatorWeb.getDocumentService().saveDocument(getDocument());
161            Assert.assertTrue(getDocument().getDocumentHeader().getWorkflowDocument().isSaved());
162        }
163    
164        public MaintenanceDocument getDocument() {
165            return document;
166        }
167    
168        public void setDocument(MaintenanceDocument document) {
169            this.document = document;
170        }
171    }