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  public abstract class BaseMaintenanceDocumentTest extends KRADTestCase {
33      private MaintenanceDocument document;
34      private String documentTypeName;
35      private String initiatorPrincipalName;
36  
37      @Override
38      public void setUp() throws Exception {
39          super.setUp();
40          GlobalVariables.setUserSession(new UserSession(getInitiatorPrincipalName()));
41          MaintenanceDocument maintenanceDocument =
42                  (MaintenanceDocument) KRADServiceLocatorWeb.getDocumentService().getNewDocument(getDocumentTypeName());
43          maintenanceDocument.getDocumentHeader().setDocumentDescription("test maintenance document");
44          setDocument(maintenanceDocument);
45      }
46  
47      @Before
48      public void setUpBeforeTest() {
49          GlobalVariables.getMessageMap().clearErrorMessages();
50      }
51  
52      /**
53       * Override this method to provide different value
54       *
55       * @return the document type name to use
56       */
57      protected String getDocumentTypeName() {
58          return documentTypeName;
59      }
60  
61      /**
62       * Override this method to provide different initiator
63       *
64       * @return the principal name to use as the initiator for the specified document type
65       */
66      protected String getInitiatorPrincipalName() {
67          return initiatorPrincipalName;
68      }
69  
70      protected void setupNewAccountMaintDoc(MaintenanceDocument document) {
71  
72          Object am = getNewMaintainableObject();
73  
74          document.getOldMaintainableObject().setDataObject(null);
75          document.getOldMaintainableObject().setDataObjectClass(am.getClass());
76          document.getNewMaintainableObject().setDataObject(am);
77          document.getNewMaintainableObject().setDataObjectClass(am.getClass());
78  
79          document.getNewMaintainableObject().setMaintenanceAction(KRADConstants.MAINTENANCE_NEW_ACTION);
80      }
81  
82      /**
83       *
84       * @return an object to set as the new maintainable object
85       */
86      protected abstract Object getNewMaintainableObject();
87  
88      /**
89       *
90       * @return an object to set as the old maintainable object
91       */
92      protected abstract Object getOldMaintainableObject();
93  
94      /**
95       * populate maintenance document with objects for editing
96       *
97       * @param document - the maintenance document being tested
98       */
99      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 }