View Javadoc

1   /*
2    * Copyright 2006-2012 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  
17  package org.kuali.test;
18  
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  import org.kuali.test.KRADTestCase;
29  
30  import static org.junit.Assert.assertEquals;
31  
32  /**
33   *  BaseMaintenanceDocumentTest is a base class for testing maintenance documents
34   *
35   *  <p>It provides test methods for setting up, editing, saving and copying a maintenance document</p>
36   *
37   *  @author Kuali Rice Team (rice.collab@kuali.org)
38   */
39  public abstract class BaseMaintenanceDocumentTest extends KRADTestCase {
40      private MaintenanceDocument document;
41      private String documentTypeName;
42      private String initiatorPrincipalName;
43  
44      @Override
45      public void setUp() throws Exception {
46          super.setUp();
47          GlobalVariables.setUserSession(new UserSession(getInitiatorPrincipalName()));
48          MaintenanceDocument maintenanceDocument =
49                  (MaintenanceDocument) KRADServiceLocatorWeb.getDocumentService().getNewDocument(getDocumentTypeName());
50          maintenanceDocument.getDocumentHeader().setDocumentDescription("test maintenance document");
51          setDocument(maintenanceDocument);
52      }
53  
54      @Before
55      public void setUpBeforeTest() {
56          GlobalVariables.getMessageMap().clearErrorMessages();
57      }
58  
59      /**
60       * Override this method to provide different value
61       *
62       * @return the document type name to use
63       */
64      protected String getDocumentTypeName() {
65          return documentTypeName;
66      }
67  
68      /**
69       * Override this method to provide different initiator
70       *
71       * @return the principal name to use as the initiator for the specified document type
72       */
73      protected String getInitiatorPrincipalName() {
74          return initiatorPrincipalName;
75      }
76  
77      /**
78       *  setup a new maintenance document
79       *
80       * @param document - the maintenance document being tested
81       */
82      protected void setupNewAccountMaintDoc(MaintenanceDocument document) {
83  
84          Object am = getNewMaintainableObject();
85  
86          document.getOldMaintainableObject().setDataObject(null);
87          document.getOldMaintainableObject().setDataObjectClass(am.getClass());
88          document.getNewMaintainableObject().setDataObject(am);
89          document.getNewMaintainableObject().setDataObjectClass(am.getClass());
90  
91          document.getNewMaintainableObject().setMaintenanceAction(KRADConstants.MAINTENANCE_NEW_ACTION);
92      }
93  
94      /**
95       *
96       * @return an object to set as the new maintainable object
97       */
98      protected abstract Object getNewMaintainableObject();
99  
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 }