001: /*
002: * Copyright 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.rules;
017:
018: import static org.kuali.test.util.KualiTestAssertionUtils.assertGlobalErrorMapEmpty;
019: import static org.kuali.test.util.KualiTestAssertionUtils.assertSparselyEqualBean;
020:
021: import java.util.ArrayList;
022: import java.util.List;
023:
024: import org.kuali.core.rule.BusinessRule;
025: import org.kuali.core.rule.RouteDocumentRule;
026: import org.kuali.core.rule.SaveDocumentRule;
027: import org.kuali.core.service.DataDictionaryService;
028: import org.kuali.core.util.GeneralLedgerPendingEntrySequenceHelper;
029: import org.kuali.kfs.bo.AccountingLine;
030: import org.kuali.kfs.context.KualiTestBase;
031: import org.kuali.kfs.context.SpringContext;
032: import org.kuali.kfs.document.AccountingDocument;
033: import org.kuali.kfs.rule.AddAccountingLineRule;
034: import org.kuali.kfs.rule.GenerateGeneralLedgerPendingEntriesRule;
035: import org.kuali.test.fixtures.GeneralLedgerPendingEntryFixture;
036:
037: public abstract class AccountingDocumentRuleTestUtils extends
038: KualiTestBase {
039:
040: // test methods
041: public static void testAddAccountingLineRule_IsObjectTypeAllowed(
042: Class<? extends AccountingDocument> documentClass,
043: AccountingLine line, boolean expected) throws Exception {
044: AddAccountingLineRule rule = getBusinessRule(documentClass,
045: AddAccountingLineRule.class);
046: assertEquals(expected, rule.isObjectTypeAllowed(documentClass,
047: line));
048: }
049:
050: public static void testAddAccountingLineRule_IsObjectCodeAllowed(
051: Class<? extends AccountingDocument> documentClass,
052: AccountingLine line, boolean expected) throws Exception {
053: AddAccountingLineRule rule = getBusinessRule(documentClass,
054: AddAccountingLineRule.class);
055: assertEquals(expected, rule.isObjectCodeAllowed(documentClass,
056: line));
057: }
058:
059: public static void testAddAccountingLine_IsObjectSubTypeAllowed(
060: Class<? extends AccountingDocument> documentClass,
061: AccountingLine line, boolean expected) throws Exception {
062: AddAccountingLineRule rule = getBusinessRule(documentClass,
063: AddAccountingLineRule.class);
064: assertEquals(expected, rule.isObjectSubTypeAllowed(
065: documentClass, line));
066: }
067:
068: public static <T extends AccountingDocument> void testAddAccountingLineRule_ProcessAddAccountingLineBusinessRules(
069: T document, boolean expected) throws Exception {
070: // Check business rules
071: List<? extends AccountingLine> allLines = new ArrayList<AccountingLine>();
072: allLines.addAll(document.getSourceAccountingLines());
073: allLines.addAll(document.getTargetAccountingLines());
074:
075: assertGlobalErrorMapEmpty();
076: AddAccountingLineRule rule = getBusinessRule(document
077: .getClass(), AddAccountingLineRule.class);
078: for (AccountingLine accountingLine : allLines) {
079: boolean ruleResult = rule
080: .processAddAccountingLineBusinessRules(document,
081: accountingLine);
082: if (expected) {
083: assertGlobalErrorMapEmpty(accountingLine.toString());
084: }
085: assertEquals(expected, ruleResult);
086: }
087: }
088:
089: public static <T extends AccountingDocument> void testSaveDocumentRule_ProcessSaveDocument(
090: T document, boolean expected) throws Exception {
091: SaveDocumentRule rule = getBusinessRule(document.getClass(),
092: SaveDocumentRule.class);
093: boolean rulePassed = rule.processSaveDocument(document);
094: if (expected) {
095: assertGlobalErrorMapEmpty();
096: }
097: assertEquals(expected, rulePassed);
098: }
099:
100: public static <T extends AccountingDocument> void testRouteDocumentRule_processRouteDocument(
101: T document, boolean expected) throws Exception {
102: RouteDocumentRule rule = getBusinessRule(document.getClass(),
103: RouteDocumentRule.class);
104: boolean rulePassed = rule.processRouteDocument(document);
105: if (expected) {
106: assertGlobalErrorMapEmpty();
107: }
108: assertEquals(expected, rulePassed);
109: }
110:
111: public static boolean testGenerateGeneralLedgerPendingEntriesRule_ProcessGenerateGeneralLedgerPendingEntries(
112: AccountingDocument document, AccountingLine line,
113: GeneralLedgerPendingEntryFixture expectedExplicitFixture,
114: GeneralLedgerPendingEntryFixture expectedOffsetFixture)
115: throws Exception {
116: assertEquals(0, document.getGeneralLedgerPendingEntries()
117: .size());
118: GenerateGeneralLedgerPendingEntriesRule rule = getBusinessRule(
119: document.getClass(),
120: GenerateGeneralLedgerPendingEntriesRule.class);
121: boolean result = rule
122: .processGenerateGeneralLedgerPendingEntries(document,
123: line,
124: new GeneralLedgerPendingEntrySequenceHelper());
125: assertEquals(expectedOffsetFixture == null ? 1 : 2, document
126: .getGeneralLedgerPendingEntries().size());
127: assertSparselyEqualBean(expectedExplicitFixture
128: .createGeneralLedgerPendingEntry(), document
129: .getGeneralLedgerPendingEntry(0));
130: if (expectedOffsetFixture != null) {
131: assertSparselyEqualBean(expectedOffsetFixture
132: .createGeneralLedgerPendingEntry(), document
133: .getGeneralLedgerPendingEntry(1));
134: }
135: return result;
136: }
137:
138: // helper methods
139: /**
140: * retrieves a rule instance for a given document
141: *
142: * @param <T>
143: * @param documentClass the type of document to retrieve the rule for
144: * @param businessRuleClass the type of rule to create
145: * @return an instance of a BusinessRule of the same type as the businessRuleClass parameter
146: * @throws Exception
147: */
148: public static <T extends BusinessRule> T getBusinessRule(
149: Class<? extends AccountingDocument> documentClass,
150: Class<T> businessRuleClass) throws Exception {
151: DataDictionaryService dataDictionaryService = SpringContext
152: .getBean(DataDictionaryService.class);
153: final String documentTypeName = dataDictionaryService
154: .getDocumentTypeNameByClass(documentClass);
155: T businessRule = (T) dataDictionaryService.getDataDictionary()
156: .getDocumentEntry(documentTypeName)
157: .getBusinessRulesClass().newInstance();
158: return businessRule;
159: }
160: }
|