001: /*
002: * Copyright 2005-2007 The Kuali Foundation.
003: *
004: * Licensed under the Educational Community License, Version 1.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/ecl1.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.module.financial.document;
017:
018: import static org.kuali.module.financial.document.AccountingDocumentTestUtils.testGetNewDocument_byDocumentClass;
019: import static org.kuali.test.fixtures.AccountingLineFixture.LINE5;
020: import static org.kuali.test.fixtures.UserNameFixture.DFOGLE;
021: import static org.kuali.test.util.KualiTestAssertionUtils.assertEquality;
022: import static org.kuali.test.util.KualiTestAssertionUtils.assertInequality;
023:
024: import java.util.ArrayList;
025: import java.util.List;
026:
027: import org.kuali.core.document.Copyable;
028: import org.kuali.core.document.Correctable;
029: import org.kuali.core.document.Document;
030: import org.kuali.core.service.DataDictionaryService;
031: import org.kuali.core.service.DocumentService;
032: import org.kuali.core.service.TransactionalDocumentDictionaryService;
033: import org.kuali.core.util.KualiDecimal;
034: import org.kuali.core.util.ObjectUtils;
035: import org.kuali.kfs.KFSConstants;
036: import org.kuali.kfs.bo.SourceAccountingLine;
037: import org.kuali.kfs.bo.TargetAccountingLine;
038: import org.kuali.kfs.context.KualiTestBase;
039: import org.kuali.kfs.context.SpringContext;
040: import org.kuali.kfs.document.AccountingDocument;
041: import org.kuali.module.chart.service.AccountingPeriodService;
042: import org.kuali.test.ConfigureContext;
043: import org.kuali.test.DocumentTestUtils;
044: import org.kuali.test.fixtures.AccountingLineFixture;
045: import org.kuali.test.monitor.ChangeMonitor;
046: import org.kuali.test.monitor.DocumentStatusMonitor;
047: import org.kuali.test.monitor.DocumentWorkflowStatusMonitor;
048: import org.kuali.workflow.WorkflowTestUtils;
049:
050: import edu.iu.uis.eden.EdenConstants;
051:
052: /**
053: * This class is used to test JournalVoucherDocument.
054: */
055: @ConfigureContext(session=DFOGLE)
056: public class JournalVoucherDocumentTest extends KualiTestBase {
057:
058: public static final Class<JournalVoucherDocument> DOCUMENT_CLASS = JournalVoucherDocument.class;
059:
060: private JournalVoucherDocument buildDocument() throws Exception {
061: // put accounting lines into document parameter for later
062: JournalVoucherDocument document = (JournalVoucherDocument) getDocumentParameterFixture();
063:
064: // set accountinglines to document
065: for (AccountingLineFixture sourceFixture : getSourceAccountingLineParametersFromFixtures()) {
066: sourceFixture.addAsSourceTo(document);
067: }
068:
069: for (AccountingLineFixture targetFixture : getTargetAccountingLineParametersFromFixtures()) {
070: targetFixture.addAsTargetTo(document);
071: }
072: document.setBalanceTypeCode(KFSConstants.BALANCE_TYPE_ACTUAL);
073: return document;
074: }
075:
076: /**
077: * Had to override b/c there are too many differences between the JV and the standard document structure (i.e. GLPEs generate
078: * differently, routing isn't standard, etc).
079: *
080: * @see org.kuali.core.document.AccountingDocumentTestBase#testConvertIntoCopy()
081: */
082: // @RelatesTo(JiraIssue.KULRNE4926)
083: @ConfigureContext(session=DFOGLE,shouldCommitTransactions=true)
084: public void testConvertIntoCopy() throws Exception {
085: // save the original doc, wait for status change
086: AccountingDocument document = buildDocument();
087: SpringContext.getBean(DocumentService.class).routeDocument(
088: document, "saving copy source document", null);
089: // collect some preCopy data
090: String preCopyId = document.getDocumentNumber();
091: String preCopyCopiedFromId = document.getDocumentHeader()
092: .getFinancialDocumentTemplateNumber();
093:
094: int preCopyPECount = document.getGeneralLedgerPendingEntries()
095: .size();
096: // int preCopyNoteCount = document.getDocumentHeader().getNotes().size();
097:
098: ArrayList preCopySourceLines = (ArrayList) ObjectUtils
099: .deepCopy((ArrayList) document
100: .getSourceAccountingLines());
101: ArrayList preCopyTargetLines = (ArrayList) ObjectUtils
102: .deepCopy((ArrayList) document
103: .getTargetAccountingLines());
104: // validate preCopy state
105: assertNotNull(preCopyId);
106: assertNull(preCopyCopiedFromId);
107:
108: assertEquals(1, preCopyPECount);
109: // assertEquals(0, preCopyNoteCount);
110:
111: // do the copy
112: ((Copyable) document).toCopy();
113: // compare to preCopy state
114:
115: String postCopyId = document.getDocumentNumber();
116: assertFalse(postCopyId.equals(preCopyId));
117: // verify that docStatus has changed
118: // pending entries should be cleared
119: int postCopyPECount = document.getGeneralLedgerPendingEntries()
120: .size();
121: assertEquals(0, postCopyPECount);
122: // TODO: revisit this is it still needed
123: // count 1 note, compare to "copied" text
124: // int postCopyNoteCount = document.getDocumentHeader().getNotes().size();
125: // assertEquals(1, postCopyNoteCount);
126: // DocumentNote note = document.getDocumentHeader().getNote(0);
127: // assertTrue(note.getFinancialDocumentNoteText().indexOf("copied from") != -1);
128: // copiedFrom should be equal to old id
129: String copiedFromId = document.getDocumentHeader()
130: .getFinancialDocumentTemplateNumber();
131: assertEquals(preCopyId, copiedFromId);
132: // accounting lines should be have different docHeaderIds but same amounts
133: List postCopySourceLines = document.getSourceAccountingLines();
134: assertEquals(preCopySourceLines.size(), postCopySourceLines
135: .size());
136: for (int i = 0; i < preCopySourceLines.size(); ++i) {
137: SourceAccountingLine preCopyLine = (SourceAccountingLine) preCopySourceLines
138: .get(i);
139: SourceAccountingLine postCopyLine = (SourceAccountingLine) postCopySourceLines
140: .get(i);
141:
142: assertInequality(preCopyLine.getDocumentNumber(),
143: postCopyLine.getDocumentNumber());
144: assertEquality(preCopyLine.getAmount(), postCopyLine
145: .getAmount());
146: }
147:
148: List postCopyTargetLines = document.getTargetAccountingLines();
149: assertEquals(preCopyTargetLines.size(), postCopyTargetLines
150: .size());
151: for (int i = 0; i < preCopyTargetLines.size(); ++i) {
152: TargetAccountingLine preCopyLine = (TargetAccountingLine) preCopyTargetLines
153: .get(i);
154: TargetAccountingLine postCopyLine = (TargetAccountingLine) postCopyTargetLines
155: .get(i);
156:
157: assertInequality(preCopyLine.getDocumentNumber(),
158: postCopyLine.getDocumentNumber());
159: assertEquality(preCopyLine.getAmount(), postCopyLine
160: .getAmount());
161: }
162: }
163:
164: /**
165: * Had to override b/c there are too many differences between the JV and the standard document structure (i.e. GLPEs generate
166: * differently, routing isn't standard, etc).
167: *
168: * @see org.kuali.core.document.AccountingDocumentTestBase#testConvertIntoErrorCorrection()
169: */
170: // @RelatesTo(JiraIssue.KULRNE4926)
171: @ConfigureContext(session=DFOGLE,shouldCommitTransactions=true)
172: public void testConvertIntoErrorCorrection() throws Exception {
173: AccountingDocument document = buildDocument();
174:
175: // replace the broken sourceLines with one that lets the test succeed
176: KualiDecimal balance = new KualiDecimal("21.12");
177: ArrayList sourceLines = new ArrayList();
178: {
179: SourceAccountingLine sourceLine = new SourceAccountingLine();
180: sourceLine.setDocumentNumber(document.getDocumentNumber());
181: sourceLine.setSequenceNumber(new Integer(0));
182: sourceLine.setChartOfAccountsCode("BL");
183: sourceLine.setAccountNumber("1031400");
184: sourceLine.setFinancialObjectCode("1663");
185: sourceLine.setAmount(balance);
186: sourceLine.setObjectTypeCode("AS");
187: sourceLine.setBalanceTypeCode("AC");
188: sourceLine.refresh();
189: sourceLines.add(sourceLine);
190: }
191: document.setSourceAccountingLines(sourceLines);
192:
193: String documentHeaderId = document.getDocumentNumber();
194: // route the original doc, wait for status change
195: SpringContext.getBean(DocumentService.class).routeDocument(
196: document, "saving errorCorrection source document",
197: null);
198: // jv docs go straight to final
199: DocumentWorkflowStatusMonitor routeMonitor = new DocumentWorkflowStatusMonitor(
200: SpringContext.getBean(DocumentService.class),
201: documentHeaderId, "F");
202: assertTrue(ChangeMonitor.waitUntilChange(routeMonitor, 240, 5));
203: document = (AccountingDocument) SpringContext.getBean(
204: DocumentService.class).getByDocumentHeaderId(
205: documentHeaderId);
206: // collect some preCorrect data
207: String preCorrectId = document.getDocumentNumber();
208: String preCorrectCorrectsId = document.getDocumentHeader()
209: .getFinancialDocumentInErrorNumber();
210:
211: int preCorrectPECount = document
212: .getGeneralLedgerPendingEntries().size();
213: // int preCorrectNoteCount = document.getDocumentHeader().getNotes().size();
214: String preCorrectStatus = document.getDocumentHeader()
215: .getWorkflowDocument().getRouteHeader()
216: .getDocRouteStatus();
217:
218: ArrayList preCorrectSourceLines = (ArrayList) ObjectUtils
219: .deepCopy(new ArrayList(document
220: .getSourceAccountingLines()));
221: ArrayList preCorrectTargetLines = (ArrayList) ObjectUtils
222: .deepCopy(new ArrayList(document
223: .getTargetAccountingLines()));
224: // validate preCorrect state
225: assertNotNull(preCorrectId);
226: assertNull(preCorrectCorrectsId);
227:
228: // assertEquals(0, preCorrectNoteCount);
229: assertEquals("F", preCorrectStatus);
230: // do the copy
231: ((Correctable) document).toErrorCorrection();
232: // compare to preCorrect state
233:
234: String postCorrectId = document.getDocumentNumber();
235: assertFalse(postCorrectId.equals(preCorrectId));
236: // pending entries should be cleared
237: int postCorrectPECount = document
238: .getGeneralLedgerPendingEntries().size();
239: assertEquals(0, postCorrectPECount);
240: // TODO: revisit this is it still needed
241: // count 1 note, compare to "correction" text
242: // int postCorrectNoteCount = document.getDocumentHeader().getNotes().size();
243: // assertEquals(1, postCorrectNoteCount);
244: // DocumentNote note = document.getDocumentHeader().getNote(0);
245: // assertTrue(note.getFinancialDocumentNoteText().indexOf("correction") != -1);
246: // correctsId should be equal to old id
247: String correctsId = document.getDocumentHeader()
248: .getFinancialDocumentInErrorNumber();
249: assertEquals(preCorrectId, correctsId);
250: // accounting lines should have sign reversed on amounts
251: List postCorrectSourceLines = document
252: .getSourceAccountingLines();
253: assertEquals(preCorrectSourceLines.size(),
254: postCorrectSourceLines.size());
255: for (int i = 0; i < preCorrectSourceLines.size(); ++i) {
256: SourceAccountingLine preCorrectLine = (SourceAccountingLine) preCorrectSourceLines
257: .get(i);
258: SourceAccountingLine postCorrectLine = (SourceAccountingLine) postCorrectSourceLines
259: .get(i);
260:
261: assertEquality(postCorrectId, postCorrectLine
262: .getDocumentNumber());
263: assertEquality(preCorrectLine.getAmount().negated(),
264: postCorrectLine.getAmount());
265: }
266:
267: List postCorrectTargetLines = document
268: .getTargetAccountingLines();
269: assertEquals(preCorrectTargetLines.size(),
270: postCorrectTargetLines.size());
271: for (int i = 0; i < preCorrectTargetLines.size(); ++i) {
272: TargetAccountingLine preCorrectLine = (TargetAccountingLine) preCorrectTargetLines
273: .get(i);
274: TargetAccountingLine postCorrectLine = (TargetAccountingLine) postCorrectTargetLines
275: .get(i);
276:
277: assertEquality(postCorrectId, postCorrectLine
278: .getDocumentNumber());
279: assertEquality(preCorrectLine.getAmount().negated(),
280: postCorrectLine.getAmount());
281: }
282: }
283:
284: /**
285: * Override b/c the status changing is flakey with this doc b/c routing is special (goes straight to final).
286: *
287: * @see org.kuali.core.document.DocumentTestBase#testRouteDocument()
288: */
289: // @RelatesTo(JiraIssue.KULRNE4926)
290: @ConfigureContext(session=DFOGLE,shouldCommitTransactions=true)
291: public void testRouteDocument() throws Exception {
292: // save the original doc, wait for status change
293: Document document = buildDocument();
294: assertFalse("R".equals(document.getDocumentHeader()
295: .getWorkflowDocument().getRouteHeader()
296: .getDocRouteStatus()));
297: SpringContext.getBean(DocumentService.class).routeDocument(
298: document, "saving copy source document", null);
299: // jv docs go straight to final
300: WorkflowTestUtils.waitForStatusChange(document
301: .getDocumentHeader().getWorkflowDocument(),
302: EdenConstants.ROUTE_HEADER_FINAL_CD);
303: // also check the Kuali (not Workflow) document status
304: DocumentStatusMonitor statusMonitor = new DocumentStatusMonitor(
305: SpringContext.getBean(DocumentService.class), document
306: .getDocumentHeader().getDocumentNumber(),
307: KFSConstants.DocumentStatusCodes.APPROVED);
308: assertTrue(ChangeMonitor.waitUntilChange(statusMonitor, 240, 5));
309: }
310:
311: private Document getDocumentParameterFixture() throws Exception {
312: return DocumentTestUtils.createDocument(SpringContext
313: .getBean(DocumentService.class),
314: JournalVoucherDocument.class);
315: }
316:
317: private List<AccountingLineFixture> getTargetAccountingLineParametersFromFixtures() {
318: ArrayList list = new ArrayList();
319: return list;
320: }
321:
322: private List<AccountingLineFixture> getSourceAccountingLineParametersFromFixtures() {
323: List<AccountingLineFixture> list = new ArrayList<AccountingLineFixture>();
324: list.add(LINE5);
325: return list;
326: }
327:
328: public final void testAddAccountingLine() throws Exception {
329: List<SourceAccountingLine> sourceLines = generateSouceAccountingLines();
330: List<TargetAccountingLine> targetLines = generateTargetAccountingLines();
331: int expectedSourceTotal = sourceLines.size();
332: int expectedTargetTotal = targetLines.size();
333: JournalVoucherDocument document = DocumentTestUtils
334: .createDocument(SpringContext
335: .getBean(DocumentService.class), DOCUMENT_CLASS);
336: document.setBalanceTypeCode(KFSConstants.BALANCE_TYPE_ACTUAL);
337:
338: AccountingDocumentTestUtils.testAddAccountingLine(document,
339: sourceLines, targetLines, expectedSourceTotal,
340: expectedTargetTotal);
341: }
342:
343: public final void testGetNewDocument() throws Exception {
344: testGetNewDocument_byDocumentClass(DOCUMENT_CLASS,
345: SpringContext.getBean(DocumentService.class));
346: }
347:
348: public final void testConvertIntoCopy_copyDisallowed()
349: throws Exception {
350: AccountingDocumentTestUtils.testConvertIntoCopy_copyDisallowed(
351: buildDocument(), SpringContext
352: .getBean(DataDictionaryService.class));
353:
354: }
355:
356: public final void testConvertIntoErrorCorrection_documentAlreadyCorrected()
357: throws Exception {
358: AccountingDocumentTestUtils
359: .testConvertIntoErrorCorrection_documentAlreadyCorrected(
360: buildDocument(),
361: SpringContext
362: .getBean(TransactionalDocumentDictionaryService.class));
363: }
364:
365: public final void testConvertIntoErrorCorrection_errorCorrectionDisallowed()
366: throws Exception {
367: AccountingDocumentTestUtils
368: .testConvertIntoErrorCorrection_errorCorrectionDisallowed(
369: buildDocument(), SpringContext
370: .getBean(DataDictionaryService.class));
371: }
372:
373: public final void testConvertIntoErrorCorrection_invalidYear()
374: throws Exception {
375: AccountingDocumentTestUtils
376: .testConvertIntoErrorCorrection_invalidYear(
377: buildDocument(),
378: SpringContext
379: .getBean(TransactionalDocumentDictionaryService.class),
380: SpringContext
381: .getBean(AccountingPeriodService.class));
382: }
383:
384: // @RelatesTo(JiraIssue.KULRNE4926)
385: @ConfigureContext(session=DFOGLE,shouldCommitTransactions=true)
386: public final void testSaveDocument() throws Exception {
387: AccountingDocumentTestUtils.testSaveDocument(buildDocument(),
388: SpringContext.getBean(DocumentService.class));
389: }
390:
391: // test util methods
392: private List<SourceAccountingLine> generateSouceAccountingLines()
393: throws Exception {
394: List<SourceAccountingLine> sourceLines = new ArrayList<SourceAccountingLine>();
395: // set accountinglines to document
396: for (AccountingLineFixture sourceFixture : getSourceAccountingLineParametersFromFixtures()) {
397: sourceLines.add(sourceFixture.createSourceAccountingLine());
398: }
399:
400: return sourceLines;
401: }
402:
403: private List<TargetAccountingLine> generateTargetAccountingLines()
404: throws Exception {
405: List<TargetAccountingLine> targetLines = new ArrayList<TargetAccountingLine>();
406: for (AccountingLineFixture targetFixture : getTargetAccountingLineParametersFromFixtures()) {
407: targetLines.add(targetFixture.createTargetAccountingLine());
408: }
409:
410: return targetLines;
411: }
412:
413: }
|