View Javadoc

1   /**
2    * Copyright 2005-2013 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              if (udte.getMessage().contains("AccountManagerMaintenanceDocument")) {
54                  fail("CI failure - https://jira.kuali.org/browse/KULRICE-9285 " + udte.getMessage() + " "  +ExceptionUtils.getStackTrace(udte));
55              }
56          }
57      }
58  
59      @Before
60      public void setUpBeforeTest() {
61          GlobalVariables.getMessageMap().clearErrorMessages();
62      }
63  
64      /**
65       * Override this method to provide different value
66       *
67       * @return the document type name to use
68       */
69      protected String getDocumentTypeName() {
70          return documentTypeName;
71      }
72  
73      /**
74       * Override this method to provide different initiator
75       *
76       * @return the principal name to use as the initiator for the specified document type
77       */
78      protected String getInitiatorPrincipalName() {
79          return initiatorPrincipalName;
80      }
81  
82      /**
83       *  setup a new maintenance document
84       *
85       * @param document - the maintenance document being tested
86       */
87      protected void setupNewAccountMaintDoc(MaintenanceDocument document) {
88  
89          Object am = getNewMaintainableObject();
90  
91          document.getOldMaintainableObject().setDataObject(null);
92          document.getOldMaintainableObject().setDataObjectClass(am.getClass());
93          document.getNewMaintainableObject().setDataObject(am);
94          document.getNewMaintainableObject().setDataObjectClass(am.getClass());
95  
96          document.getNewMaintainableObject().setMaintenanceAction(KRADConstants.MAINTENANCE_NEW_ACTION);
97      }
98  
99      /**
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 }