001 /**
002 * Copyright 2005-2014 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 package org.kuali.rice.krad.test;
017
018 import org.apache.commons.lang.exception.ExceptionUtils;
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
029 import static org.junit.Assert.fail;
030
031 /**
032 * BaseMaintenanceDocumentTest is a base class for testing maintenance documents
033 *
034 * <p>It provides test methods for setting up, editing, saving and copying a maintenance document</p>
035 *
036 * @author Kuali Rice Team (rice.collab@kuali.org)
037 */
038 public abstract class BaseMaintenanceDocumentTest extends KRADTestCase {
039 private MaintenanceDocument document;
040 private String documentTypeName;
041 private String initiatorPrincipalName;
042
043 @Override
044 public void setUp() throws Exception {
045 super.setUp();
046 GlobalVariables.setUserSession(new UserSession(getInitiatorPrincipalName()));
047 try {
048 MaintenanceDocument maintenanceDocument =
049 (MaintenanceDocument) KRADServiceLocatorWeb.getDocumentService().getNewDocument(getDocumentTypeName());
050 maintenanceDocument.getDocumentHeader().setDocumentDescription("test maintenance document");
051 setDocument(maintenanceDocument);
052 } catch (org.kuali.rice.krad.datadictionary.exception.UnknownDocumentTypeException udte) {
053 fail("CI failure - " + udte.getMessage() + " " +ExceptionUtils.getStackTrace(udte));
054 }
055 }
056
057 @Before
058 public void setUpBeforeTest() {
059 GlobalVariables.getMessageMap().clearErrorMessages();
060 }
061
062 /**
063 * Override this method to provide different value
064 *
065 * @return the document type name to use
066 */
067 protected String getDocumentTypeName() {
068 return documentTypeName;
069 }
070
071 /**
072 * Override this method to provide different initiator
073 *
074 * @return the principal name to use as the initiator for the specified document type
075 */
076 protected String getInitiatorPrincipalName() {
077 return initiatorPrincipalName;
078 }
079
080 /**
081 * setup a new maintenance document
082 *
083 * @param document - the maintenance document being tested
084 */
085 protected void setupNewAccountMaintDoc(MaintenanceDocument document) {
086
087 Object am = getNewMaintainableObject();
088
089 document.getOldMaintainableObject().setDataObject(null);
090 document.getOldMaintainableObject().setDataObjectClass(am.getClass());
091 document.getNewMaintainableObject().setDataObject(am);
092 document.getNewMaintainableObject().setDataObjectClass(am.getClass());
093
094 document.getNewMaintainableObject().setMaintenanceAction(KRADConstants.MAINTENANCE_NEW_ACTION);
095 }
096
097 /**
098 *
099 * @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 }