0001: /*
0002: * Copyright 2006-2007 The Kuali Foundation.
0003: *
0004: * Licensed under the Educational Community License, Version 1.0 (the "License");
0005: * you may not use this file except in compliance with the License.
0006: * You may obtain a copy of the License at
0007: *
0008: * http://www.opensource.org/licenses/ecl1.php
0009: *
0010: * Unless required by applicable law or agreed to in writing, software
0011: * distributed under the License is distributed on an "AS IS" BASIS,
0012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0013: * See the License for the specific language governing permissions and
0014: * limitations under the License.
0015: */
0016: package org.kuali.module.financial.service;
0017:
0018: import static org.kuali.test.fixtures.UserNameFixture.TWATSON;
0019:
0020: import java.security.GeneralSecurityException;
0021: import java.util.ArrayList;
0022: import java.util.HashMap;
0023: import java.util.Iterator;
0024: import java.util.List;
0025: import java.util.Map;
0026:
0027: import org.kuali.core.document.Document;
0028: import org.kuali.core.exceptions.ValidationException;
0029: import org.kuali.core.service.BusinessObjectService;
0030: import org.kuali.core.service.DocumentService;
0031: import org.kuali.core.util.GlobalVariables;
0032: import org.kuali.core.util.KualiDecimal;
0033: import org.kuali.kfs.KFSConstants;
0034: import org.kuali.kfs.KFSPropertyConstants;
0035: import org.kuali.kfs.context.KualiTestBase;
0036: import org.kuali.kfs.context.SpringContext;
0037: import org.kuali.module.financial.bo.BankAccount;
0038: import org.kuali.module.financial.bo.CashDrawer;
0039: import org.kuali.module.financial.bo.CashReceiptHeader;
0040: import org.kuali.module.financial.bo.Deposit;
0041: import org.kuali.module.financial.document.CashManagementDocument;
0042: import org.kuali.module.financial.document.CashReceiptDocument;
0043: import org.kuali.module.financial.exceptions.CashDrawerStateException;
0044: import org.kuali.module.financial.exceptions.InvalidCashReceiptState;
0045: import org.kuali.module.financial.util.CashReceiptFamilyTestUtil;
0046: import org.kuali.test.ConfigureContext;
0047: import org.kuali.test.fixtures.UserNameFixture;
0048:
0049: import edu.iu.uis.eden.exception.WorkflowException;
0050:
0051: @ConfigureContext(session=TWATSON)
0052: public class CashManagementServiceTest extends KualiTestBase {
0053: static final String CMST_WORKGROUP = "KUALI_BRSR_KO";
0054:
0055: final public void testCreateCashManagementDocument_blankUnitName()
0056: throws Exception {
0057: boolean failedAsExpected = false;
0058:
0059: try {
0060: SpringContext.getBean(CashManagementService.class)
0061: .createCashManagementDocument(" ", "foo",
0062: "cmst1");
0063: } catch (IllegalArgumentException e) {
0064: failedAsExpected = true;
0065: }
0066:
0067: assertTrue(failedAsExpected);
0068: }
0069:
0070: final public void testCreateCashManagementDocument_blankDocDescription()
0071: throws Exception {
0072: boolean failedAsExpected = false;
0073:
0074: try {
0075: SpringContext.getBean(CashManagementService.class)
0076: .createCashManagementDocument(CMST_WORKGROUP, null,
0077: "cmst2");
0078: } catch (IllegalArgumentException e) {
0079: failedAsExpected = true;
0080: }
0081:
0082: assertTrue(failedAsExpected);
0083: }
0084:
0085: @ConfigureContext(session=TWATSON,shouldCommitTransactions=true)
0086: final public void testCreateCashManagementDocument_valid()
0087: throws Exception {
0088: String testDocumentId = null;
0089:
0090: try {
0091: deleteIfExists(CMST_WORKGROUP);
0092: CashDrawer preDocCD = SpringContext.getBean(
0093: CashDrawerService.class).getByWorkgroupName(
0094: CMST_WORKGROUP, true);
0095: assertTrue(preDocCD.isClosed());
0096:
0097: CashManagementDocument createdDoc = SpringContext.getBean(
0098: CashManagementService.class)
0099: .createCashManagementDocument(CMST_WORKGROUP,
0100: "CMST_testCreate_valid", "cmst3");
0101: assertNotNull(createdDoc);
0102: testDocumentId = createdDoc.getDocumentNumber();
0103:
0104: // save it separately
0105: SpringContext.getBean(CashDrawerService.class)
0106: .openCashDrawer(createdDoc.getCashDrawer(),
0107: testDocumentId);
0108: saveDocument(createdDoc);
0109:
0110: // verify that the doc was saved
0111: CashManagementDocument retrievedDoc = (CashManagementDocument) SpringContext
0112: .getBean(DocumentService.class)
0113: .getByDocumentHeaderId(testDocumentId);
0114: assertEquals("S", retrievedDoc.getDocumentHeader()
0115: .getWorkflowDocument().getRouteHeader()
0116: .getDocRouteStatus());
0117: } finally {
0118: // cancel the document
0119: cleanupCancel(testDocumentId);
0120:
0121: // delete the cashDrawer which was created as a side-effect above
0122: deleteIfExists(CMST_WORKGROUP);
0123: }
0124: }
0125:
0126: @ConfigureContext(session=TWATSON,shouldCommitTransactions=true)
0127: final public void testCreateCashManagementDocument_cashDrawerAlreadyOpen()
0128: throws Exception {
0129:
0130: String testDocumentId = null;
0131: try {
0132: deleteIfExists(CMST_WORKGROUP);
0133: CashDrawer preDocCD = SpringContext.getBean(
0134: CashDrawerService.class).getByWorkgroupName(
0135: CMST_WORKGROUP, true);
0136: assertTrue(preDocCD.isClosed());
0137:
0138: CashManagementDocument createdDoc = SpringContext.getBean(
0139: CashManagementService.class)
0140: .createCashManagementDocument(CMST_WORKGROUP,
0141: "CMST_testCreate_cashDrawerAlreadyOpen",
0142: "cmst3");
0143: assertNotNull(createdDoc);
0144: testDocumentId = createdDoc.getDocumentNumber();
0145:
0146: // force the drawer open
0147: SpringContext.getBean(CashDrawerService.class)
0148: .openCashDrawer(createdDoc.getCashDrawer(),
0149: testDocumentId);
0150: saveDocument(createdDoc);
0151:
0152: boolean failedAsExpected = false;
0153: // fail creating the document since the CashDrawer is already open
0154: try {
0155: SpringContext.getBean(CashManagementService.class)
0156: .createCashManagementDocument(CMST_WORKGROUP,
0157: "CMST_testCreate_validCollision 2",
0158: "cmst5");
0159: fail("created a CMD while the CashDrawer is already open");
0160: } catch (CashDrawerStateException e) {
0161: // good
0162: failedAsExpected = true;
0163: }
0164:
0165: assertEquals(failedAsExpected, true);
0166:
0167: //
0168: // cancel empty CMDoc
0169: //
0170: SpringContext.getBean(CashManagementService.class)
0171: .cancelCashManagementDocument(createdDoc);
0172:
0173: } finally {
0174: // cancel the document
0175: cleanupCancel(testDocumentId);
0176:
0177: // delete the cashDrawer you created
0178: deleteIfExists(CMST_WORKGROUP);
0179: }
0180: }
0181:
0182: @ConfigureContext(session=TWATSON,shouldCommitTransactions=true)
0183: final public void testCancelCashManagementDocument_validEmpty()
0184: throws Exception {
0185: String testDocumentId = null;
0186:
0187: try {
0188: //
0189: // create empty CMDoc
0190: //
0191: deleteIfExists(CMST_WORKGROUP);
0192: CashDrawer preDocCD = SpringContext.getBean(
0193: CashDrawerService.class).getByWorkgroupName(
0194: CMST_WORKGROUP, false);
0195: assertNull(preDocCD);
0196:
0197: CashManagementDocument createdDoc = SpringContext.getBean(
0198: CashManagementService.class)
0199: .createCashManagementDocument(CMST_WORKGROUP,
0200: "CMST_testCreate_valid", "cmst3");
0201: assertNotNull(createdDoc);
0202: testDocumentId = createdDoc.getDocumentNumber();
0203:
0204: // save it separately
0205: SpringContext.getBean(CashDrawerService.class)
0206: .openCashDrawer(createdDoc.getCashDrawer(),
0207: testDocumentId);
0208: saveDocument(createdDoc);
0209:
0210: // verify it actually got saved
0211: CashManagementDocument retrievedDoc = (CashManagementDocument) SpringContext
0212: .getBean(DocumentService.class)
0213: .getByDocumentHeaderId(testDocumentId);
0214: assertEquals("S", retrievedDoc.getDocumentHeader()
0215: .getWorkflowDocument().getRouteHeader()
0216: .getDocRouteStatus());
0217:
0218: //
0219: // cancel empty CMDoc
0220: //
0221: SpringContext.getBean(CashManagementService.class)
0222: .cancelCashManagementDocument(createdDoc);
0223:
0224: // verify that the cancellation closed the cash drawer
0225: CashDrawer postCancelCD = SpringContext.getBean(
0226: CashDrawerService.class).getByWorkgroupName(
0227: CMST_WORKGROUP, false);
0228: assertNotNull(postCancelCD);
0229: assertEquals(postCancelCD.getStatusCode(),
0230: KFSConstants.CashDrawerConstants.STATUS_CLOSED);
0231: } finally {
0232: // cancel the document
0233: cleanupCancel(testDocumentId);
0234:
0235: // delete the cashDrawer which was created as a side-effect above
0236: deleteIfExists(CMST_WORKGROUP);
0237: }
0238: }
0239:
0240: @ConfigureContext(session=TWATSON,shouldCommitTransactions=true)
0241: final public void testCancelCashManagementDocument_valid_interimOnly()
0242: throws Exception {
0243: String testDocumentId = null;
0244:
0245: try {
0246: //
0247: // create a valid, empty CashManagementDocument
0248: deleteIfExists(CMST_WORKGROUP);
0249: CashDrawer preDocCD = SpringContext.getBean(
0250: CashDrawerService.class).getByWorkgroupName(
0251: CMST_WORKGROUP, true);
0252: assertTrue(preDocCD.isClosed());
0253:
0254: CashManagementDocument createdDoc = SpringContext.getBean(
0255: CashManagementService.class)
0256: .createCashManagementDocument(CMST_WORKGROUP,
0257: "CMST_testAddID_valid", null);
0258: testDocumentId = createdDoc.getDocumentNumber();
0259:
0260: // save it separately
0261: SpringContext.getBean(CashDrawerService.class)
0262: .openCashDrawer(createdDoc.getCashDrawer(),
0263: testDocumentId);
0264: saveDocument(createdDoc);
0265:
0266: //
0267: // create Interim Deposit
0268:
0269: // create CashReceipts
0270: changeCurrentUser(UserNameFixture.INEFF);
0271: CashReceiptDocument cr1 = buildCashReceiptDoc(
0272: CMST_WORKGROUP,
0273: "CMST CR1",
0274: KFSConstants.DocumentStatusCodes.CashReceipt.VERIFIED,
0275: new KualiDecimal("25.00"));
0276: CashReceiptDocument cr2 = buildCashReceiptDoc(
0277: CMST_WORKGROUP,
0278: "CMST CR2",
0279: KFSConstants.DocumentStatusCodes.CashReceipt.VERIFIED,
0280: new KualiDecimal("25.00"));
0281: CashReceiptDocument cr3 = buildCashReceiptDoc(
0282: CMST_WORKGROUP,
0283: "CMST CR3",
0284: KFSConstants.DocumentStatusCodes.CashReceipt.VERIFIED,
0285: new KualiDecimal("23.00"));
0286:
0287: List crList = new ArrayList();
0288: crList.add(cr1);
0289: crList.add(cr2);
0290: crList.add(cr3);
0291:
0292: // add interim deposit
0293: changeCurrentUser(TWATSON);
0294: CashManagementDocument interimDoc = (CashManagementDocument) SpringContext
0295: .getBean(DocumentService.class)
0296: .getByDocumentHeaderId(testDocumentId);
0297: SpringContext.getBean(CashManagementService.class)
0298: .addDeposit(interimDoc, VALID_DEPOSIT_TICKET,
0299: lookupBankAccount(), crList,
0300: new ArrayList(), false);
0301:
0302: //
0303: // verify addition
0304: CashManagementDocument depositedDoc = (CashManagementDocument) SpringContext
0305: .getBean(DocumentService.class)
0306: .getByDocumentHeaderId(testDocumentId);
0307: {
0308:
0309: // 1 deposit in document
0310: List deposits = depositedDoc.getDeposits();
0311: assertEquals(1, deposits.size());
0312:
0313: // deposit exists in database
0314: Map depositPK = new HashMap();
0315: depositPK.put(KFSPropertyConstants.DOCUMENT_NUMBER,
0316: testDocumentId);
0317: depositPK.put("financialDocumentDepositLineNumber",
0318: new Integer(0));
0319:
0320: assertEquals(1, SpringContext.getBean(
0321: BusinessObjectService.class).countMatching(
0322: Deposit.class, depositPK));
0323:
0324: // deposit contains 3 CRs
0325: Deposit deposit = depositedDoc.getDeposit(0);
0326: List depositedReceiptControls = deposit
0327: .getDepositCashReceiptControl();
0328: assertEquals(3, depositedReceiptControls.size());
0329:
0330: // CRs are in appropriate state
0331: assertEquals(
0332: KFSConstants.DocumentStatusCodes.CashReceipt.INTERIM,
0333: lookupCR(cr1.getDocumentNumber())
0334: .getDocumentHeader()
0335: .getFinancialDocumentStatusCode());
0336: assertEquals(
0337: KFSConstants.DocumentStatusCodes.CashReceipt.INTERIM,
0338: lookupCR(cr2.getDocumentNumber())
0339: .getDocumentHeader()
0340: .getFinancialDocumentStatusCode());
0341: assertEquals(
0342: KFSConstants.DocumentStatusCodes.CashReceipt.INTERIM,
0343: lookupCR(cr3.getDocumentNumber())
0344: .getDocumentHeader()
0345: .getFinancialDocumentStatusCode());
0346: }
0347:
0348: } finally {
0349:
0350: // clean up CRdoc
0351: denatureCashReceipts(CMST_WORKGROUP);
0352:
0353: // cancel CMDoc; now we can
0354: cleanupCancel(testDocumentId);
0355:
0356: // delete the cashDrawer which was created as a side-effect above
0357: deleteIfExists(CMST_WORKGROUP);
0358: }
0359: }
0360:
0361: private static final String VALID_DEPOSIT_TICKET = "0 0 0 destruct 0";
0362:
0363: final public void testAddInterimDeposit_nullDoc() throws Exception {
0364: boolean failedAsExpected = false;
0365:
0366: try {
0367: SpringContext.getBean(CashManagementService.class)
0368: .addDeposit(null, VALID_DEPOSIT_TICKET,
0369: lookupBankAccount(), null, null, false);
0370: } catch (IllegalArgumentException e) {
0371: failedAsExpected = true;
0372: }
0373:
0374: assertTrue(failedAsExpected);
0375: }
0376:
0377: @ConfigureContext(session=TWATSON,shouldCommitTransactions=true)
0378: final public void testAddInterimDeposit_nullBank() throws Exception {
0379: boolean failedAsExpected = false;
0380:
0381: String docId = null;
0382: try {
0383: CashManagementDocument createdDoc = SpringContext.getBean(
0384: CashManagementService.class)
0385: .createCashManagementDocument(CMST_WORKGROUP,
0386: "CMST_testAddID_eCRL", null);
0387: docId = createdDoc.getDocumentNumber();
0388:
0389: SpringContext.getBean(CashDrawerService.class)
0390: .openCashDrawer(createdDoc.getCashDrawer(), docId);
0391: saveDocument(createdDoc);
0392:
0393: SpringContext.getBean(CashManagementService.class)
0394: .addDeposit(createdDoc, VALID_DEPOSIT_TICKET, null,
0395: new ArrayList(), new ArrayList(), false);
0396: } catch (IllegalArgumentException e) {
0397: failedAsExpected = true;
0398: } finally {
0399: // clean up CRdoc
0400: denatureCashReceipts(CMST_WORKGROUP);
0401:
0402: if (docId != null) {
0403: Document testDoc = SpringContext.getBean(
0404: DocumentService.class).getByDocumentHeaderId(
0405: docId);
0406: SpringContext.getBean(DocumentService.class)
0407: .cancelDocument(testDoc, "CMST cleanup");
0408: }
0409:
0410: // delete the cashDrawer which was created as a side-effect above
0411: deleteIfExists(CMST_WORKGROUP);
0412: }
0413:
0414: assertTrue(failedAsExpected);
0415: }
0416:
0417: @ConfigureContext(session=TWATSON,shouldCommitTransactions=true)
0418: final public void testAddInterimDeposit_nonverifiedCashReceipt()
0419: throws Exception {
0420: boolean failedAsExpected = false;
0421:
0422: String testDocumentId = null;
0423:
0424: try {
0425: //
0426: // create a valid, empty CashManagementDocument
0427: deleteIfExists(CMST_WORKGROUP);
0428: CashDrawer preDocCD = SpringContext.getBean(
0429: CashDrawerService.class).getByWorkgroupName(
0430: CMST_WORKGROUP, true);
0431: assertTrue(preDocCD.isClosed());
0432:
0433: CashManagementDocument createdDoc = SpringContext.getBean(
0434: CashManagementService.class)
0435: .createCashManagementDocument(CMST_WORKGROUP,
0436: "CMST_testAddID_nonverified", null);
0437: testDocumentId = createdDoc.getDocumentNumber();
0438:
0439: SpringContext.getBean(CashDrawerService.class)
0440: .openCashDrawer(createdDoc.getCashDrawer(),
0441: testDocumentId);
0442: saveDocument(createdDoc);
0443:
0444: // retrieve the document, for future use
0445: CashManagementDocument retrievedDoc = (CashManagementDocument) SpringContext
0446: .getBean(DocumentService.class)
0447: .getByDocumentHeaderId(testDocumentId);
0448:
0449: //
0450: // create Interim Deposit
0451:
0452: // create CashReceipt
0453: changeCurrentUser(UserNameFixture.INEFF);
0454: CashReceiptDocument cr = buildCashReceiptDoc(
0455: CMST_WORKGROUP,
0456: "CMST nonverified CR",
0457: KFSConstants.DocumentStatusCodes.CashReceipt.INTERIM,
0458: new KualiDecimal("75.00"));
0459: changeCurrentUser(TWATSON);
0460:
0461: List crList = new ArrayList();
0462: crList.add(cr);
0463:
0464: // add invalid interim deposit
0465: SpringContext.getBean(CashManagementService.class)
0466: .addDeposit(retrievedDoc, VALID_DEPOSIT_TICKET,
0467: lookupBankAccount(), crList,
0468: new ArrayList(), false);
0469: } catch (InvalidCashReceiptState e) {
0470: failedAsExpected = true;
0471: } finally {
0472: // clean up CRdoc
0473: denatureCashReceipts(CMST_WORKGROUP);
0474:
0475: // cancel CMDoc
0476: cleanupCancel(testDocumentId);
0477:
0478: // delete the cashDrawer which was created as a side-effect above
0479: deleteIfExists(CMST_WORKGROUP);
0480: }
0481:
0482: assertTrue(failedAsExpected);
0483: }
0484:
0485: @ConfigureContext(session=TWATSON,shouldCommitTransactions=true)
0486: final public void testAddInterimDeposit_unsavedCMDoc()
0487: throws Exception {
0488: boolean failedAsExpected = false;
0489:
0490: String testDocumentId = null;
0491:
0492: try {
0493: //
0494: // create a valid, empty CashManagementDocument
0495: deleteIfExists(CMST_WORKGROUP);
0496: CashDrawer preDocCD = SpringContext.getBean(
0497: CashDrawerService.class).getByWorkgroupName(
0498: CMST_WORKGROUP, true);
0499: assertTrue(preDocCD.isClosed());
0500:
0501: changeCurrentUser(TWATSON);
0502: CashManagementDocument createdDoc = SpringContext.getBean(
0503: CashManagementService.class)
0504: .createCashManagementDocument(CMST_WORKGROUP,
0505: "CMST_testAddID_nonverified", null);
0506: testDocumentId = createdDoc.getDocumentNumber();
0507:
0508: //
0509: // create Interim Deposit
0510:
0511: // create CashReceipt
0512: changeCurrentUser(UserNameFixture.INEFF);
0513: CashReceiptDocument cr = buildCashReceiptDoc(
0514: CMST_WORKGROUP,
0515: "CMST noncheck CR",
0516: KFSConstants.DocumentStatusCodes.CashReceipt.VERIFIED,
0517: new KualiDecimal("25.00"));
0518: changeCurrentUser(TWATSON);
0519: changeCurrentUser(TWATSON);
0520:
0521: List crList = new ArrayList();
0522: crList.add(cr);
0523:
0524: // add interim deposit
0525: SpringContext.getBean(CashManagementService.class)
0526: .addDeposit(createdDoc, VALID_DEPOSIT_TICKET,
0527: lookupBankAccount(), crList,
0528: new ArrayList(), false);
0529: } catch (IllegalStateException e) {
0530: failedAsExpected = true;
0531: } finally {
0532: // clean up CRdoc
0533: denatureCashReceipts(CMST_WORKGROUP);
0534:
0535: // delete the cashDrawer which was created as a side-effect above
0536: deleteIfExists(CMST_WORKGROUP);
0537: }
0538:
0539: assertTrue(failedAsExpected);
0540: }
0541:
0542: @ConfigureContext(session=TWATSON,shouldCommitTransactions=true)
0543: final public void testAddInterimDeposit_valid() throws Exception {
0544: String testDocumentId = null;
0545:
0546: try {
0547: //
0548: // create a valid, empty CashManagementDocument
0549: deleteIfExists(CMST_WORKGROUP);
0550: CashDrawer preDocCD = SpringContext.getBean(
0551: CashDrawerService.class).getByWorkgroupName(
0552: CMST_WORKGROUP, true);
0553: assertTrue(preDocCD.isClosed());
0554:
0555: CashManagementDocument createdDoc = SpringContext.getBean(
0556: CashManagementService.class)
0557: .createCashManagementDocument(CMST_WORKGROUP,
0558: "CMST_testAddID_valid", null);
0559: testDocumentId = createdDoc.getDocumentNumber();
0560:
0561: // save it
0562: SpringContext.getBean(CashDrawerService.class)
0563: .openCashDrawer(createdDoc.getCashDrawer(),
0564: testDocumentId);
0565: saveDocument(createdDoc);
0566:
0567: // retrieve the document, for future use
0568: CashManagementDocument retrievedDoc = (CashManagementDocument) SpringContext
0569: .getBean(DocumentService.class)
0570: .getByDocumentHeaderId(testDocumentId);
0571:
0572: //
0573: // create Interim Deposit
0574:
0575: // create CashReceipts
0576: changeCurrentUser(UserNameFixture.INEFF);
0577: CashReceiptDocument cr1 = buildCashReceiptDoc(
0578: CMST_WORKGROUP,
0579: "CMST CR1",
0580: KFSConstants.DocumentStatusCodes.CashReceipt.VERIFIED,
0581: new KualiDecimal("10.00"));
0582: CashReceiptDocument cr2 = buildCashReceiptDoc(
0583: CMST_WORKGROUP,
0584: "CMST CR2",
0585: KFSConstants.DocumentStatusCodes.CashReceipt.VERIFIED,
0586: new KualiDecimal("25.00"));
0587: CashReceiptDocument cr3 = buildCashReceiptDoc(
0588: CMST_WORKGROUP,
0589: "CMST CR3",
0590: KFSConstants.DocumentStatusCodes.CashReceipt.VERIFIED,
0591: new KualiDecimal("23.00"));
0592:
0593: List crList = new ArrayList();
0594: crList.add(cr1);
0595: crList.add(cr2);
0596: crList.add(cr3);
0597:
0598: // add interim deposit
0599: changeCurrentUser(TWATSON);
0600: SpringContext.getBean(CashManagementService.class)
0601: .addDeposit(retrievedDoc, VALID_DEPOSIT_TICKET,
0602: lookupBankAccount(), crList,
0603: new ArrayList(), false);
0604:
0605: //
0606: // validate results
0607: CashManagementDocument depositedDoc = (CashManagementDocument) SpringContext
0608: .getBean(DocumentService.class)
0609: .getByDocumentHeaderId(testDocumentId);
0610:
0611: // 1 deposit
0612: List deposits = depositedDoc.getDeposits();
0613: assertEquals(1, deposits.size());
0614:
0615: // deposit exists in database
0616: Map depositPK = new HashMap();
0617: depositPK.put(KFSPropertyConstants.DOCUMENT_NUMBER,
0618: testDocumentId);
0619: depositPK.put("financialDocumentDepositLineNumber",
0620: new Integer(0));
0621:
0622: assertEquals(1, SpringContext.getBean(
0623: BusinessObjectService.class).countMatching(
0624: Deposit.class, depositPK));
0625:
0626: // deposit is interim, not final
0627: Deposit deposit = (Deposit) deposits.get(0);
0628: assertEquals(
0629: KFSConstants.DepositConstants.DEPOSIT_TYPE_INTERIM,
0630: deposit.getDepositTypeCode());
0631:
0632: // deposit contains 3 CRs
0633: List depositedReceiptControls = deposit
0634: .getDepositCashReceiptControl();
0635: assertEquals(3, depositedReceiptControls.size());
0636:
0637: // CRs are in appropriate state
0638: assertEquals(
0639: KFSConstants.DocumentStatusCodes.CashReceipt.INTERIM,
0640: lookupCR(cr1.getDocumentNumber())
0641: .getDocumentHeader()
0642: .getFinancialDocumentStatusCode());
0643: assertEquals(
0644: KFSConstants.DocumentStatusCodes.CashReceipt.INTERIM,
0645: lookupCR(cr2.getDocumentNumber())
0646: .getDocumentHeader()
0647: .getFinancialDocumentStatusCode());
0648: assertEquals(
0649: KFSConstants.DocumentStatusCodes.CashReceipt.INTERIM,
0650: lookupCR(cr3.getDocumentNumber())
0651: .getDocumentHeader()
0652: .getFinancialDocumentStatusCode());
0653:
0654: // total value of the deposit is the sum of the values of the 3 CRs
0655: assertEquals(new KualiDecimal("58.00"), deposit
0656: .getDepositAmount());
0657: } finally {
0658: // clean up CRdoc
0659: denatureCashReceipts(CMST_WORKGROUP);
0660:
0661: // cancel CMDoc
0662: cleanupCancel(testDocumentId);
0663:
0664: // delete the cashDrawer which was created as a side-effect above
0665: deleteIfExists(CMST_WORKGROUP);
0666: }
0667: }
0668:
0669: public void testCancelDeposit_nullDeposit() throws Exception {
0670: boolean failedAsExpected = false;
0671:
0672: try {
0673: SpringContext.getBean(CashManagementService.class)
0674: .cancelDeposit(null);
0675: } catch (IllegalArgumentException e) {
0676: failedAsExpected = true;
0677: }
0678:
0679: assertTrue(failedAsExpected);
0680: }
0681:
0682: @ConfigureContext(session=TWATSON,shouldCommitTransactions=true)
0683: public void testCancelDeposit_cancelSingleInterim()
0684: throws Exception {
0685: String testDocumentId = null;
0686:
0687: try {
0688: //
0689: // create a valid, empty CashManagementDocument
0690: deleteIfExists(CMST_WORKGROUP);
0691: CashDrawer preDocCD = SpringContext.getBean(
0692: CashDrawerService.class).getByWorkgroupName(
0693: CMST_WORKGROUP, true);
0694: assertTrue(preDocCD.isClosed());
0695:
0696: CashManagementDocument createdDoc = SpringContext.getBean(
0697: CashManagementService.class)
0698: .createCashManagementDocument(CMST_WORKGROUP,
0699: "CMST_testAddID_valid", null);
0700: testDocumentId = createdDoc.getDocumentNumber();
0701:
0702: // save it
0703: SpringContext.getBean(CashDrawerService.class)
0704: .openCashDrawer(createdDoc.getCashDrawer(),
0705: testDocumentId);
0706: saveDocument(createdDoc);
0707:
0708: //
0709: // add Interim Deposit
0710:
0711: // create CashReceipts
0712: changeCurrentUser(UserNameFixture.INEFF);
0713: CashReceiptDocument cr1 = buildCashReceiptDoc(
0714: CMST_WORKGROUP,
0715: "CMST CR1",
0716: KFSConstants.DocumentStatusCodes.CashReceipt.VERIFIED,
0717: new KualiDecimal("25.00"));
0718: CashReceiptDocument cr2 = buildCashReceiptDoc(
0719: CMST_WORKGROUP,
0720: "CMST CR2",
0721: KFSConstants.DocumentStatusCodes.CashReceipt.VERIFIED,
0722: new KualiDecimal("25.00"));
0723: CashReceiptDocument cr3 = buildCashReceiptDoc(
0724: CMST_WORKGROUP,
0725: "CMST CR3",
0726: KFSConstants.DocumentStatusCodes.CashReceipt.VERIFIED,
0727: new KualiDecimal("25.00"));
0728:
0729: List crList = new ArrayList();
0730: crList.add(cr1);
0731: crList.add(cr2);
0732: crList.add(cr3);
0733:
0734: // add interim deposit
0735: changeCurrentUser(TWATSON);
0736: CashManagementDocument interimDoc = (CashManagementDocument) SpringContext
0737: .getBean(DocumentService.class)
0738: .getByDocumentHeaderId(testDocumentId);
0739: SpringContext.getBean(CashManagementService.class)
0740: .addDeposit(interimDoc, VALID_DEPOSIT_TICKET,
0741: lookupBankAccount(), crList,
0742: new ArrayList(), false);
0743:
0744: //
0745: // verify addition
0746:
0747: CashManagementDocument depositedDoc = (CashManagementDocument) SpringContext
0748: .getBean(DocumentService.class)
0749: .getByDocumentHeaderId(testDocumentId);
0750: {
0751: // 1 deposit in document
0752: List deposits = depositedDoc.getDeposits();
0753: assertEquals(1, deposits.size());
0754:
0755: // deposit exists in database
0756: Map depositPK = new HashMap();
0757: depositPK.put(KFSPropertyConstants.DOCUMENT_NUMBER,
0758: testDocumentId);
0759: depositPK.put("financialDocumentDepositLineNumber",
0760: new Integer(0));
0761:
0762: assertEquals(1, SpringContext.getBean(
0763: BusinessObjectService.class).countMatching(
0764: Deposit.class, depositPK));
0765:
0766: // deposit contains 3 CRs
0767: Deposit deposit = depositedDoc.getDeposit(0);
0768: List depositedReceiptControls = deposit
0769: .getDepositCashReceiptControl();
0770: assertEquals(3, depositedReceiptControls.size());
0771:
0772: // CRs are in appropriate state
0773: assertEquals(
0774: KFSConstants.DocumentStatusCodes.CashReceipt.INTERIM,
0775: lookupCR(cr1.getDocumentNumber())
0776: .getDocumentHeader()
0777: .getFinancialDocumentStatusCode());
0778: assertEquals(
0779: KFSConstants.DocumentStatusCodes.CashReceipt.INTERIM,
0780: lookupCR(cr2.getDocumentNumber())
0781: .getDocumentHeader()
0782: .getFinancialDocumentStatusCode());
0783: assertEquals(
0784: KFSConstants.DocumentStatusCodes.CashReceipt.INTERIM,
0785: lookupCR(cr3.getDocumentNumber())
0786: .getDocumentHeader()
0787: .getFinancialDocumentStatusCode());
0788: }
0789:
0790: {
0791: //
0792: // cancel deposit
0793: Deposit deposit = depositedDoc.getDeposit(0);
0794: SpringContext.getBean(CashManagementService.class)
0795: .cancelDeposit(deposit);
0796:
0797: //
0798: // verify cancellation
0799: CashManagementDocument postCanceledDoc = (CashManagementDocument) SpringContext
0800: .getBean(DocumentService.class)
0801: .getByDocumentHeaderId(testDocumentId);
0802:
0803: // 0 deposits in document
0804: List deposits = postCanceledDoc.getDeposits();
0805: assertEquals(0, deposits.size());
0806:
0807: // deposit doesn't exist in database
0808: Map depositPK = new HashMap();
0809: depositPK.put(KFSPropertyConstants.DOCUMENT_NUMBER,
0810: testDocumentId);
0811: depositPK.put("financialDocumentDepositLineNumber",
0812: new Integer(0));
0813:
0814: assertEquals(0, SpringContext.getBean(
0815: BusinessObjectService.class).countMatching(
0816: Deposit.class, depositPK));
0817:
0818: // cash receipts have been restored to appropriate state
0819: assertEquals(
0820: KFSConstants.DocumentStatusCodes.CashReceipt.VERIFIED,
0821: lookupCR(cr1.getDocumentNumber())
0822: .getDocumentHeader()
0823: .getFinancialDocumentStatusCode());
0824: assertEquals(
0825: KFSConstants.DocumentStatusCodes.CashReceipt.VERIFIED,
0826: lookupCR(cr2.getDocumentNumber())
0827: .getDocumentHeader()
0828: .getFinancialDocumentStatusCode());
0829: assertEquals(
0830: KFSConstants.DocumentStatusCodes.CashReceipt.VERIFIED,
0831: lookupCR(cr3.getDocumentNumber())
0832: .getDocumentHeader()
0833: .getFinancialDocumentStatusCode());
0834: }
0835: } finally {
0836: // clean up CRdoc
0837: denatureCashReceipts(CMST_WORKGROUP);
0838:
0839: // cancel CMDoc
0840: cleanupCancel(testDocumentId);
0841:
0842: // delete the cashDrawer which was created as a side-effect above
0843: deleteIfExists(CMST_WORKGROUP);
0844: }
0845: }
0846:
0847: public void testKULEDOCS_1475_nullDocument() {
0848: try {
0849: // open the Cash Drawer for a null documentId
0850: deleteIfExists(CMST_WORKGROUP);
0851:
0852: CashDrawer forcedOpen = SpringContext.getBean(
0853: CashDrawerService.class).getByWorkgroupName(
0854: CMST_WORKGROUP, true);
0855: forcedOpen
0856: .setStatusCode(KFSConstants.CashDrawerConstants.STATUS_OPEN);
0857: forcedOpen.setReferenceFinancialDocumentNumber(null);
0858: SpringContext.getBean(BusinessObjectService.class).save(
0859: forcedOpen);
0860:
0861: // try create a new CM doc
0862: CashManagementDocument createdDoc = SpringContext.getBean(
0863: CashManagementService.class)
0864: .createCashManagementDocument(CMST_WORKGROUP,
0865: "CMST_testAddID_valid", null);
0866: assertNotNull(createdDoc);
0867: } finally {
0868: deleteIfExists(CMST_WORKGROUP);
0869: }
0870: }
0871:
0872: public void testKULEDOCS_1475_nonexistentDocument() {
0873: try {
0874: // open the Cash Drawer for a nonexistent documentId
0875: deleteIfExists(CMST_WORKGROUP);
0876:
0877: CashDrawer forcedLocked = SpringContext.getBean(
0878: CashDrawerService.class).getByWorkgroupName(
0879: CMST_WORKGROUP, true);
0880: forcedLocked
0881: .setStatusCode(KFSConstants.CashDrawerConstants.STATUS_LOCKED);
0882: forcedLocked.setReferenceFinancialDocumentNumber("0");
0883: SpringContext.getBean(BusinessObjectService.class).save(
0884: forcedLocked);
0885:
0886: // try create a new CM doc
0887: CashManagementDocument createdDoc = SpringContext.getBean(
0888: CashManagementService.class)
0889: .createCashManagementDocument(CMST_WORKGROUP,
0890: "CMST_testAddID_valid", null);
0891: assertNotNull(createdDoc);
0892: } finally {
0893: deleteIfExists(CMST_WORKGROUP);
0894: }
0895: }
0896:
0897: @ConfigureContext(session=TWATSON,shouldCommitTransactions=true)
0898: public void testKULEDOCS_1475_existentDocument() throws Exception {
0899: boolean failedAsExpected = false;
0900:
0901: String testDocumentId = null;
0902: try {
0903:
0904: //
0905: // create a valid, empty CashManagementDocument
0906: deleteIfExists(CMST_WORKGROUP);
0907: CashDrawer preDocCD = SpringContext.getBean(
0908: CashDrawerService.class).getByWorkgroupName(
0909: CMST_WORKGROUP, true);
0910: assertTrue(preDocCD.isClosed());
0911:
0912: CashManagementDocument createdDoc = SpringContext.getBean(
0913: CashManagementService.class)
0914: .createCashManagementDocument(CMST_WORKGROUP,
0915: "CMST_testAddID_valid", null);
0916: testDocumentId = createdDoc.getDocumentNumber();
0917:
0918: // save it
0919: SpringContext.getBean(CashDrawerService.class)
0920: .openCashDrawer(createdDoc.getCashDrawer(),
0921: testDocumentId);
0922: saveDocument(createdDoc);
0923:
0924: // try create a new CM doc
0925: SpringContext.getBean(CashManagementService.class)
0926: .createCashManagementDocument(CMST_WORKGROUP,
0927: "CMST_testAddID_valid", null);
0928: } catch (CashDrawerStateException e) {
0929: failedAsExpected = true;
0930: } finally {
0931: // cancel CMDoc
0932: cleanupCancel(testDocumentId);
0933:
0934: deleteIfExists(CMST_WORKGROUP);
0935: }
0936:
0937: assertTrue(failedAsExpected);
0938: }
0939:
0940: private CashReceiptDocument lookupCR(String documentId)
0941: throws WorkflowException {
0942: CashReceiptDocument crDoc = (CashReceiptDocument) SpringContext
0943: .getBean(DocumentService.class).getByDocumentHeaderId(
0944: documentId);
0945:
0946: return crDoc;
0947: }
0948:
0949: private void deleteIfExists(String workgroupName) {
0950: Map deleteCriteria = new HashMap();
0951: deleteCriteria.put("workgroupName", workgroupName);
0952: SpringContext.getBean(BusinessObjectService.class)
0953: .deleteMatching(CashDrawer.class, deleteCriteria);
0954: }
0955:
0956: private static final String[] BOTH_STATII = {
0957: KFSConstants.DocumentStatusCodes.CashReceipt.VERIFIED,
0958: KFSConstants.DocumentStatusCodes.CashReceipt.INTERIM };
0959:
0960: private void denatureCashReceipts(String workgroupName) {
0961: List verifiedReceipts = SpringContext.getBean(
0962: CashReceiptService.class).getCashReceipts(
0963: workgroupName, BOTH_STATII);
0964:
0965: for (Iterator i = verifiedReceipts.iterator(); i.hasNext();) {
0966: CashReceiptDocument receipt = (CashReceiptDocument) i
0967: .next();
0968: receipt.getDocumentHeader().setFinancialDocumentStatusCode(
0969: "Z");
0970: SpringContext.getBean(DocumentService.class)
0971: .updateDocument(receipt);
0972: }
0973: }
0974:
0975: private CashReceiptDocument buildCashReceiptDoc(
0976: String workgroupName, String description, String status,
0977: KualiDecimal checkAmount) throws WorkflowException {
0978: CashReceiptDocument crDoc = (CashReceiptDocument) SpringContext
0979: .getBean(DocumentService.class).getNewDocument(
0980: CashReceiptDocument.class);
0981:
0982: crDoc.getDocumentHeader().setFinancialDocumentDescription(
0983: description);
0984: crDoc.getDocumentHeader()
0985: .setFinancialDocumentStatusCode(status);
0986:
0987: crDoc.setCheckEntryMode(CashReceiptDocument.CHECK_ENTRY_TOTAL);
0988: crDoc.setTotalCashAmount(KualiDecimal.ZERO); // cash amounts are now calculated differently
0989: crDoc.setTotalCheckAmount(checkAmount);
0990:
0991: crDoc.setCashReceiptHeader(new CashReceiptHeader());
0992: crDoc.getCashReceiptHeader().setDocumentNumber(
0993: crDoc.getDocumentNumber());
0994: crDoc.getCashReceiptHeader().setWorkgroupName(workgroupName);
0995:
0996: crDoc.setCampusLocationCode(SpringContext.getBean(
0997: CashReceiptService.class)
0998: .getCampusCodeForCashReceiptVerificationUnit(
0999: workgroupName));
1000:
1001: crDoc.addSourceAccountingLine(CashReceiptFamilyTestUtil
1002: .buildSourceAccountingLine(crDoc.getDocumentNumber(),
1003: crDoc.getPostingYear(), crDoc
1004: .getNextSourceLineNumber()));
1005: saveDocument(crDoc);
1006:
1007: CashReceiptDocument persistedDoc = (CashReceiptDocument) SpringContext
1008: .getBean(DocumentService.class).getByDocumentHeaderId(
1009: crDoc.getDocumentNumber());
1010: return persistedDoc;
1011: }
1012:
1013: private void saveDocument(Document doc) throws WorkflowException {
1014: try {
1015: SpringContext.getBean(DocumentService.class).saveDocument(
1016: doc);
1017: } catch (ValidationException e) {
1018: // If the business rule evaluation fails then give us more info for debugging this test.
1019: fail(e.getMessage() + ", " + GlobalVariables.getErrorMap());
1020: }
1021: }
1022:
1023: private BankAccount lookupBankAccount()
1024: throws GeneralSecurityException {
1025: Map keyMap = new HashMap();
1026: keyMap.put("financialDocumentBankCode", "TEST");
1027: keyMap.put("finDocumentBankAccountNumber", "1111");
1028:
1029: BankAccount bankAccount = (BankAccount) SpringContext.getBean(
1030: BusinessObjectService.class).findByPrimaryKey(
1031: BankAccount.class, keyMap);
1032:
1033: assertNotNull("invalid bank account for test", bankAccount);
1034: return bankAccount;
1035: }
1036:
1037: private void cleanupCancel(String documentId) throws Exception {
1038: if (documentId != null) {
1039: Document testDoc = SpringContext.getBean(
1040: DocumentService.class).getByDocumentHeaderId(
1041: documentId);
1042:
1043: if (!testDoc.getDocumentHeader().getWorkflowDocument()
1044: .stateIsCanceled()) {
1045: final String initiatorNetworkId = testDoc
1046: .getDocumentHeader().getWorkflowDocument()
1047: .getInitiatorNetworkId();
1048: final String previousNetworkId = GlobalVariables
1049: .getUserSession().getNetworkId();
1050: if (!previousNetworkId.equals(initiatorNetworkId)) {
1051: changeCurrentUser(UserNameFixture
1052: .valueOf(initiatorNetworkId.toUpperCase()));
1053: // Only the initiator can cancel an initiated or saved document.
1054: testDoc = SpringContext.getBean(
1055: DocumentService.class)
1056: .getByDocumentHeaderId(documentId);
1057: }
1058: SpringContext.getBean(DocumentService.class)
1059: .cancelDocument(testDoc, "CMST cleanup cancel");
1060: changeCurrentUser(UserNameFixture
1061: .valueOf(previousNetworkId.toUpperCase()));
1062: }
1063: }
1064: }
1065: }
|