View Javadoc
1   /**
2    * Copyright 2005-2014 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.test;
17  
18  import org.apache.commons.lang.exception.ExceptionUtils;
19  import org.junit.Assert;
20  import org.junit.Before;
21  import org.junit.Test;
22  import org.kuali.rice.kew.api.exception.WorkflowException;
23  import org.kuali.rice.krad.UserSession;
24  import org.kuali.rice.krad.maintenance.MaintenanceDocument;
25  import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
26  import org.kuali.rice.krad.util.GlobalVariables;
27  import org.kuali.rice.krad.util.KRADConstants;
28  
29  import static org.junit.Assert.fail;
30  
31  /**
32   *  BaseMaintenanceDocumentTest is a base class for testing maintenance documents
33   *
34   *  <p>It provides test methods for setting up, editing, saving and copying a maintenance document</p>
35   *
36   *  @author Kuali Rice Team (rice.collab@kuali.org)
37   */
38  public abstract class BaseMaintenanceDocumentTest extends KRADTestCase {
39      private MaintenanceDocument document;
40      private String documentTypeName;
41      private String initiatorPrincipalName;
42  
43      @Override
44      public void setUp() throws Exception {
45          super.setUp();
46          GlobalVariables.setUserSession(new UserSession(getInitiatorPrincipalName()));
47          try {
48              MaintenanceDocument maintenanceDocument =
49                  (MaintenanceDocument) KRADServiceLocatorWeb.getDocumentService().getNewDocument(getDocumentTypeName());
50              maintenanceDocument.getDocumentHeader().setDocumentDescription("test maintenance document");
51              setDocument(maintenanceDocument);
52          } catch (org.kuali.rice.krad.datadictionary.exception.UnknownDocumentTypeException udte) {
53              fail("CI failure - " + udte.getMessage() + " "  +ExceptionUtils.getStackTrace(udte));
54          }
55      }
56  
57      @Before
58      public void setUpBeforeTest() {
59          GlobalVariables.getMessageMap().clearErrorMessages();
60      }
61  
62      /**
63       * Override this method to provide different value
64       *
65       * @return the document type name to use
66       */
67      protected String getDocumentTypeName() {
68          return documentTypeName;
69      }
70  
71      /**
72       * Override this method to provide different initiator
73       *
74       * @return the principal name to use as the initiator for the specified document type
75       */
76      protected String getInitiatorPrincipalName() {
77          return initiatorPrincipalName;
78      }
79  
80      /**
81       *  setup a new maintenance document
82       *
83       * @param document - the maintenance document being tested
84       */
85      protected void setupNewAccountMaintDoc(MaintenanceDocument document) {
86  
87          Object am = getNewMaintainableObject();
88  
89          document.getOldMaintainableObject().setDataObject(null);
90          document.getOldMaintainableObject().setDataObjectClass(am.getClass());
91          document.getNewMaintainableObject().setDataObject(am);
92          document.getNewMaintainableObject().setDataObjectClass(am.getClass());
93  
94          document.getNewMaintainableObject().setMaintenanceAction(KRADConstants.MAINTENANCE_NEW_ACTION);
95      }
96  
97      /**
98       *
99       * @return an object to set as the new maintainable object
100      */
101     protected abstract Object getNewMaintainableObject();
102 
103     /**
104      *
105      * @return an object to set as the old maintainable object
106      */
107     protected abstract Object getOldMaintainableObject();
108 
109     /**
110      * populate maintenance document with objects for editing
111      *
112      * @param document - the maintenance document being tested
113      */
114     protected void setupEditAccountMaintDoc(MaintenanceDocument document) {
115 
116         Object newAm = getNewMaintainableObject();
117         Object oldAm = getOldMaintainableObject();
118 
119         document.getOldMaintainableObject().setDataObject(oldAm);
120         document.getOldMaintainableObject().setDataObjectClass(oldAm.getClass());
121         document.getNewMaintainableObject().setDataObject(newAm);
122         document.getNewMaintainableObject().setDataObjectClass(newAm.getClass());
123 
124         document.getNewMaintainableObject().setMaintenanceAction(KRADConstants.MAINTENANCE_EDIT_ACTION);
125     }
126 
127     @Test
128     /**
129      * test creating a new maintenance document
130      */
131     public void test_NewDoc() {
132 
133         setupNewAccountMaintDoc(getDocument());
134 
135         Assert.assertEquals("Document should indicate New.", true, getDocument().isNew());
136         Assert.assertEquals("Document should not indicate Edit.", false, getDocument().isEdit());
137         Assert.assertEquals("Old BO should not be present.", false, getDocument().isOldDataObjectInDocument());
138     }
139 
140     @Test
141     /**
142      * test editing a maintenance document
143      */
144     public void test_EditDoc() {
145 
146         setupEditAccountMaintDoc(getDocument());
147 
148         Assert.assertEquals("Document should not indicate New.", false, getDocument().isNew());
149         Assert.assertEquals("Document should indicate Edit.", true, getDocument().isEdit());
150         Assert.assertEquals("Old BO should be present.", true, getDocument().isOldDataObjectInDocument());
151 
152     }
153 
154     @Test
155     /**
156      * test copying a maintenance document
157      */
158     public void test_CopyDoc() {
159 
160         setupEditAccountMaintDoc(getDocument());
161         getDocument().getNewMaintainableObject().setMaintenanceAction(KRADConstants.MAINTENANCE_COPY_ACTION);
162 
163         Assert.assertEquals("Document should indicate New.", true, getDocument().isNew());
164         Assert.assertEquals("Document should not indicate Edit.", false, getDocument().isEdit());
165         Assert.assertEquals("Old BO should be present.", true, getDocument().isOldDataObjectInDocument());
166 
167     }
168 
169     @Test
170     /**
171      * test saving a maintenance document
172      */
173     public void test_SaveNewDoc() throws WorkflowException {
174         setupNewAccountMaintDoc(getDocument());
175         KRADServiceLocatorWeb.getDocumentService().saveDocument(getDocument());
176         Assert.assertTrue(getDocument().getDocumentHeader().getWorkflowDocument().isSaved());
177     }
178 
179     /**
180      * gets the maintenance document that is created in the constructor
181      *
182      * @return a maintenance document of the type returned by {@link #getDocumentTypeName()}
183      */
184     public MaintenanceDocument getDocument() {
185         return document;
186     }
187 
188     /**
189      * set the maintenance document to use in the test
190      * @param document - the maintenance document
191      */
192     public void setDocument(MaintenanceDocument document) {
193         this.document = document;
194     }
195 }