001: /*
002: * Copyright 2006-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.chart.rules;
017:
018: import static org.kuali.test.fixtures.UserNameFixture.KHUNTLEY;
019: import static org.kuali.test.util.KualiTestAssertionUtils.assertGlobalErrorMapEmpty;
020: import static org.kuali.test.util.KualiTestAssertionUtils.assertGlobalErrorMapSize;
021:
022: import org.kuali.core.bo.user.AuthenticationUserId;
023: import org.kuali.core.bo.user.UniversalUser;
024: import org.kuali.core.document.MaintenanceDocument;
025: import org.kuali.core.exceptions.UserNotFoundException;
026: import org.kuali.core.service.UniversalUserService;
027: import org.kuali.core.util.GlobalVariables;
028: import org.kuali.kfs.KFSKeyConstants;
029: import org.kuali.kfs.context.SpringContext;
030: import org.kuali.module.chart.bo.SubAccount;
031: import org.kuali.test.ConfigureContext;
032: import org.kuali.test.fixtures.SubAccountFixture;
033: import org.kuali.test.fixtures.UserNameFixture;
034:
035: @ConfigureContext(session=KHUNTLEY)
036: public class SubAccountRuleTest extends ChartRuleTestBase {
037:
038: protected static org.apache.log4j.Logger LOG = org.apache.log4j.Logger
039: .getLogger(SubAccountRuleTest.class);
040:
041: private static final String GOOD_CHART = "UA";
042: private static final String GOOD_ACCOUNT = "1912201";
043: private static final String NEW_SUBACCOUNT_NUMBER = "12345";
044: private static final String NEW_SUBACCOUNT_NAME = "A New SubAccount";
045:
046: // CG authorized test users
047: private static final UserNameFixture GOOD_CG_USERID = UserNameFixture.KCOPLEY;
048: private static final UserNameFixture BAD_CG_USERID = UserNameFixture.JHAVENS;
049:
050: SubAccount newSubAccount;
051: SubAccount oldSubAccount;
052: MaintenanceDocument maintDoc;
053:
054: /**
055: * This method creates a new SubAccount, and populates it with the data provided. No fields are required for this method, though
056: * some may be for the rules. This method calls subAccount.refresh() before returning it, so all sub-objects should be
057: * populated, if the keys match any records in the corresponding tables. This method does not populate anything in the contained
058: * A21SubAccount, though it does create a new A21SubAccount. So the A21SubAccount instance will be valid (ie, non-null), but all
059: * of its fields will be default or null.
060: *
061: * @param chartOfAccountsCode
062: * @param accountNumber
063: * @param subAccountNumber
064: * @param subAccountName
065: * @param subAccountActiveIndicator
066: * @param finReportChartCode
067: * @param finReportOrgCode
068: * @param finReportingCode
069: * @return returns a SubAccount instance populated with the data provided
070: */
071: private SubAccount newSubAccount(String chartOfAccountsCode,
072: String accountNumber, String subAccountNumber,
073: String subAccountName, boolean subAccountActiveIndicator,
074: String finReportChartCode, String finReportOrgCode,
075: String finReportingCode) {
076:
077: SubAccount subAccount = new SubAccount();
078:
079: subAccount.setChartOfAccountsCode(chartOfAccountsCode);
080: subAccount.setAccountNumber(accountNumber);
081: subAccount.setSubAccountNumber(subAccountNumber);
082: subAccount.setSubAccountName(subAccountName);
083: subAccount
084: .setSubAccountActiveIndicator(subAccountActiveIndicator);
085: subAccount.setFinancialReportChartCode(finReportChartCode);
086: subAccount.setFinReportOrganizationCode(finReportOrgCode);
087: subAccount.setFinancialReportingCode(finReportingCode);
088: subAccount.refresh();
089:
090: return subAccount;
091: }
092:
093: private UniversalUser createKualiUser(String userid) {
094: UniversalUser user = new UniversalUser();
095: try {
096: user = SpringContext.getBean(UniversalUserService.class)
097: .getUniversalUser(new AuthenticationUserId(userid));
098: } catch (Exception e) {
099: e.printStackTrace();
100: return null;
101: }
102: return user;
103: }
104:
105: public void testCheckForPartiallyEnteredReportingFields_nullChartAndAccount() {
106: SubAccountRule rule = new SubAccountRule();
107:
108: // setup rule, document, and bo
109: newSubAccount = newSubAccount(null, null,
110: NEW_SUBACCOUNT_NUMBER, NEW_SUBACCOUNT_NAME, true, null,
111: null, null);
112: rule = (SubAccountRule) setupMaintDocRule(newSubAccount, rule
113: .getClass());
114:
115: // confirm that there are no errors to begin with
116: assertGlobalErrorMapEmpty();
117:
118: // run the rule, should return true
119: assertEquals(true, rule
120: .checkForPartiallyEnteredReportingFields());
121: assertGlobalErrorMapEmpty();
122:
123: }
124:
125: public void testCheckForPartiallyEnteredReportingFields_goodChartNullAccount() {
126: SubAccountRule rule = new SubAccountRule();
127:
128: // setup rule, document, and bo
129: newSubAccount = newSubAccount(GOOD_CHART, null,
130: NEW_SUBACCOUNT_NUMBER, NEW_SUBACCOUNT_NAME, true, null,
131: null, null);
132: rule = (SubAccountRule) setupMaintDocRule(newSubAccount, rule
133: .getClass());
134:
135: // confirm that there are no errors to begin with
136: assertGlobalErrorMapEmpty();
137:
138: // run the rule, should return true
139: assertEquals(true, rule
140: .checkForPartiallyEnteredReportingFields());
141: assertGlobalErrorMapEmpty();
142:
143: }
144:
145: public void testCheckForPartiallyEnteredReportingFields_nullChartGoodAccount() {
146: SubAccountRule rule = new SubAccountRule();
147:
148: // setup rule, document, and bo
149: newSubAccount = newSubAccount(null, GOOD_ACCOUNT,
150: NEW_SUBACCOUNT_NUMBER, NEW_SUBACCOUNT_NAME, true, null,
151: null, null);
152: rule = (SubAccountRule) setupMaintDocRule(newSubAccount, rule
153: .getClass());
154:
155: // confirm that there are no errors to begin with
156: assertGlobalErrorMapEmpty();
157:
158: // run the rule, should return true
159: assertEquals(true, rule
160: .checkForPartiallyEnteredReportingFields());
161: assertGlobalErrorMapEmpty();
162:
163: }
164:
165: public void testCheckForPartiallyEnteredReportingFields_goodChartAndAccount() {
166: SubAccountRule rule = new SubAccountRule();
167:
168: // setup rule, document, and bo
169: newSubAccount = newSubAccount(GOOD_CHART, GOOD_ACCOUNT,
170: NEW_SUBACCOUNT_NUMBER, NEW_SUBACCOUNT_NAME, true, null,
171: null, null);
172: rule = (SubAccountRule) setupMaintDocRule(newSubAccount, rule
173: .getClass());
174:
175: // confirm that there are no errors to begin with
176: assertGlobalErrorMapEmpty();
177:
178: // run the rule, should return true
179: assertEquals(true, rule
180: .checkForPartiallyEnteredReportingFields());
181: assertGlobalErrorMapEmpty();
182:
183: }
184:
185: private void proveNotAllFinReportCodesEntered(SubAccount subAccount) {
186:
187: // setup the rule, and inject the subaccount
188: SubAccountRule rule = new SubAccountRule();
189: rule = (SubAccountRule) setupMaintDocRule(newSubAccount, rule
190: .getClass());
191:
192: // confirm that there are no errors to begin with
193: assertGlobalErrorMapEmpty();
194:
195: // run the rule, should return true
196: boolean result = rule.checkForPartiallyEnteredReportingFields();
197: assertEquals(false, result);
198: assertGlobalErrorMapSize(1);
199: assertGlobalErrorExists(KFSKeyConstants.ERROR_DOCUMENT_SUBACCTMAINT_RPTCODE_ALL_FIELDS_IF_ANY_FIELDS);
200: GlobalVariables.getErrorMap().clear();
201: }
202:
203: public void testCheckForPartiallyEnteredReportingFields_notAllFinReportCodesEntered() {
204:
205: // setup rule, document, and bo
206: newSubAccount = newSubAccount(null, null,
207: NEW_SUBACCOUNT_NUMBER, NEW_SUBACCOUNT_NAME, true, "UA",
208: null, null);
209: proveNotAllFinReportCodesEntered(newSubAccount);
210:
211: // setup rule, document, and bo
212: newSubAccount = newSubAccount(null, null,
213: NEW_SUBACCOUNT_NUMBER, NEW_SUBACCOUNT_NAME, true, null,
214: "KUL", null);
215: proveNotAllFinReportCodesEntered(newSubAccount);
216:
217: // setup rule, document, and bo
218: newSubAccount = newSubAccount(null, null,
219: NEW_SUBACCOUNT_NUMBER, NEW_SUBACCOUNT_NAME, true, null,
220: null, "KUL");
221: proveNotAllFinReportCodesEntered(newSubAccount);
222:
223: // setup rule, document, and bo
224: newSubAccount = newSubAccount(null, null,
225: NEW_SUBACCOUNT_NUMBER, NEW_SUBACCOUNT_NAME, true, "UA",
226: "KUL", null);
227: proveNotAllFinReportCodesEntered(newSubAccount);
228:
229: // setup rule, document, and bo
230: newSubAccount = newSubAccount(null, null,
231: NEW_SUBACCOUNT_NUMBER, NEW_SUBACCOUNT_NAME, true, "UA",
232: null, "KUL");
233: proveNotAllFinReportCodesEntered(newSubAccount);
234:
235: // setup rule, document, and bo
236: newSubAccount = newSubAccount(null, null,
237: NEW_SUBACCOUNT_NUMBER, NEW_SUBACCOUNT_NAME, true, null,
238: "KUL", "KUL");
239: proveNotAllFinReportCodesEntered(newSubAccount);
240:
241: }
242:
243: /**
244: * This method simulates a user that has permission to deal with CG accounts
245: */
246: public void testIsCgAuthorized_goodUser()
247: throws UserNotFoundException {
248: SubAccountRule rule = new SubAccountRule();
249: UniversalUser user = GOOD_CG_USERID.getUniversalUser();
250: // setup rule, document, and bo
251: newSubAccount = newSubAccount(GOOD_CHART, GOOD_ACCOUNT,
252: NEW_SUBACCOUNT_NUMBER, NEW_SUBACCOUNT_NAME, true, null,
253: null, null);
254: rule = (SubAccountRule) setupMaintDocRule(newSubAccount, rule
255: .getClass());
256:
257: // confirm that there are no errors to begin with
258: assertGlobalErrorMapEmpty();
259: assertEquals(true, rule.isCgAuthorized(user));
260:
261: }
262:
263: /**
264: * This method simulates a user that does not have permission to deal with CG accounts
265: */
266: public void testIsCgAuthorized_badUser()
267: throws UserNotFoundException {
268: SubAccountRule rule = new SubAccountRule();
269: UniversalUser user = BAD_CG_USERID.getUniversalUser();
270: // setup rule, document, and bo
271: newSubAccount = newSubAccount(GOOD_CHART, GOOD_ACCOUNT,
272: NEW_SUBACCOUNT_NUMBER, NEW_SUBACCOUNT_NAME, true, null,
273: null, null);
274: rule = (SubAccountRule) setupMaintDocRule(newSubAccount, rule
275: .getClass());
276:
277: // confirm that there are no errors to begin with
278: assertGlobalErrorMapEmpty();
279: assertEquals(false, rule.isCgAuthorized(user));
280: }
281:
282: public void testCheckCgRules_badFundGroup() {
283: SubAccountRule rule = new SubAccountRule();
284: // setup rule, document, and bo
285: newSubAccount = SubAccountFixture.SUB_ACCOUNT_WITH_BAD_CG_FUND_GROUP
286: .createSubAccount();
287: newSubAccount.refresh();
288: rule = (SubAccountRule) setupMaintDocRule(newSubAccount, rule
289: .getClass());
290:
291: // confirm that there are no errors to begin with
292: assertGlobalErrorMapEmpty();
293: assertEquals(true, rule.checkCgRules(maintDoc));
294: }
295:
296: public void testCheckCgRules_badA21SubAccountAccountType()
297: throws Exception {
298: SubAccountRule rule = new SubAccountRule();
299: // setup rule, document, and bo
300: newSubAccount = SubAccountFixture.A21_SUB_ACCOUNT_WITH_BAD_CG_ACCOUNT_TYPE
301: .createSubAccount();
302: newSubAccount.getA21SubAccount().refresh();
303: String fieldName = "a21SubAccount.subAccountTypeCode";
304: rule = (SubAccountRule) setupMaintDocRule(newSubAccount, rule
305: .getClass());
306: rule.setCgAuthorized(true);
307:
308: // confirm that there are no errors to begin with
309: assertGlobalErrorMapEmpty();
310: assertEquals(false, rule.checkCgRules(maintDoc));
311: assertFieldErrorExists(
312: fieldName,
313: KFSKeyConstants.ERROR_DOCUMENT_SUBACCTMAINT_INVALI_SUBACCOUNT_TYPE_CODES);
314: assertGlobalErrorMapSize(1);
315: }
316:
317: /**
318: * Incomplete TODO: Write tests for this method to accompany the testCheckCgRules
319: */
320: /*
321: * public void testCheckCgCostSharingRules() { }
322: */
323:
324: /**
325: * Incomplete TODO: Write tests for this method to accompany the testCheckCgRules
326: */
327: /*
328: * public void testCheckCgIcrRules() { }
329: */
330:
331: }
|