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.core.service;
017:
018: import java.math.BigDecimal;
019: import java.util.HashMap;
020: import java.util.Map;
021:
022: import org.kuali.core.exceptions.ObjectNotABusinessObjectRuntimeException;
023: import org.kuali.core.exceptions.ReferenceAttributeDoesntExistException;
024: import org.kuali.core.util.GlobalVariables;
025: import org.kuali.kfs.KFSKeyConstants;
026: import org.kuali.kfs.bo.SourceAccountingLine;
027: import org.kuali.kfs.context.KualiTestBase;
028: import org.kuali.kfs.context.SpringContext;
029: import org.kuali.module.chart.bo.Account;
030: import org.kuali.module.chart.bo.ProjectCode;
031: import org.kuali.module.financial.bo.PaymentReasonCode;
032: import org.kuali.test.ConfigureContext;
033:
034: /**
035: * This class tests the DictionaryValidation service.
036: */
037: @ConfigureContext
038: public class DictionaryValidationServiceTest extends KualiTestBase {
039:
040: private final static String GOOD_CHART_CODE = "UA";
041: private final static String GOOD_ACCOUNT_NBR = "1912201";
042: private final static String BAD_CHART_CODE = "ZZ";
043: private final static String BAD_ACCOUNT_NBR = "0000000";
044:
045: /**
046: * Test validation of attribute values against the restrictions defined in the data dictionary.
047: */
048: public final void testValidateAttributeFormat1() {
049: String value = "99999";
050: SpringContext.getBean(DictionaryValidationService.class)
051: .validateAttributeFormat(Account.class.getName(),
052: "accountNumber", value, "accountNumber");
053:
054: assertEquals(1, GlobalVariables.getErrorMap().size());
055: assertTrue(GlobalVariables.getErrorMap().fieldHasMessage(
056: "accountNumber", KFSKeyConstants.ERROR_INVALID_FORMAT));
057: }
058:
059: public final void testValidateAttributeFormat2() {
060: String value = "99999999";
061: SpringContext.getBean(DictionaryValidationService.class)
062: .validateAttributeFormat(Account.class.getName(),
063: "accountNumber", value, "accountNumber");
064:
065: assertEquals(1, GlobalVariables.getErrorMap().size());
066: assertTrue(GlobalVariables.getErrorMap().fieldHasMessage(
067: "accountNumber", KFSKeyConstants.ERROR_MAX_LENGTH));
068: }
069:
070: public final void testValidateAttributeFormat3() {
071: String value = "9999999";
072: SpringContext.getBean(DictionaryValidationService.class)
073: .validateAttributeFormat(Account.class.getName(),
074: "accountNumber", value, "accountNumber");
075:
076: assertEquals(0, GlobalVariables.getErrorMap().size());
077: }
078:
079: // this used to expect a failure, but as a result of KULCOA-292, this is
080: // changed to allow alpha characters in the Account Number field.
081: public final void testValidateAttributeFormat4() {
082: String value = "b999999";
083: SpringContext.getBean(DictionaryValidationService.class)
084: .validateAttributeFormat(Account.class.getName(),
085: "accountNumber", value, "accountNumber");
086:
087: assertEquals(0, GlobalVariables.getErrorMap().size());
088: }
089:
090: public final void testValidateAttributeFormat5() {
091: String value = "999 999";
092: SpringContext.getBean(DictionaryValidationService.class)
093: .validateAttributeFormat(Account.class.getName(),
094: "accountNumber", value, "accountNumber");
095:
096: assertEquals(1, GlobalVariables.getErrorMap().size());
097: assertTrue(GlobalVariables.getErrorMap().fieldHasMessage(
098: "accountNumber", KFSKeyConstants.ERROR_INVALID_FORMAT));
099: }
100:
101: public final void testValidateAttributeFormat6() {
102: String value = "1024 12th St.";
103: SpringContext.getBean(DictionaryValidationService.class)
104: .validateAttributeFormat(Account.class.getName(),
105: "accountStreetAddress", value,
106: "accountStreetAddress");
107:
108: assertEquals(0, GlobalVariables.getErrorMap().size());
109: }
110:
111: /**
112: * Test required validtion
113: */
114: public final void testValidateAttributeRequired1() {
115: SpringContext
116: .getBean(DictionaryValidationService.class)
117: .validateAttributeRequired(
118: SourceAccountingLine.class.getName(),
119: "accountNumber", null, Boolean.FALSE, "account");
120:
121: assertEquals(1, GlobalVariables.getErrorMap().size());
122: assertTrue(GlobalVariables.getErrorMap().fieldHasMessage(
123: "account", KFSKeyConstants.ERROR_REQUIRED));
124: }
125:
126: public final void testValidateAttributeRequired2() {
127: SpringContext.getBean(DictionaryValidationService.class)
128: .validateAttributeRequired(
129: SourceAccountingLine.class.getName(),
130: "accountNumber", new Account(), Boolean.FALSE,
131: "account");
132:
133: assertEquals(0, GlobalVariables.getErrorMap().size());
134: }
135:
136: public final void testValidateAttributeRequired3() {
137: SpringContext.getBean(DictionaryValidationService.class)
138: .validateAttributeRequired(
139: SourceAccountingLine.class.getName(),
140: "chartOfAccountsCode", null, Boolean.FALSE,
141: "account");
142:
143: assertEquals(1, GlobalVariables.getErrorMap().size());
144: assertTrue(GlobalVariables.getErrorMap().fieldHasMessage(
145: "account", KFSKeyConstants.ERROR_REQUIRED));
146: }
147:
148: public final void testValidateAttributeRequired4() {
149: SpringContext.getBean(DictionaryValidationService.class)
150: .validateAttributeRequired(
151: SourceAccountingLine.class.getName(), "amount",
152: null, Boolean.FALSE, "amount");
153:
154: assertEquals(1, GlobalVariables.getErrorMap().size());
155: assertTrue(GlobalVariables.getErrorMap().fieldHasMessage(
156: "amount", KFSKeyConstants.ERROR_REQUIRED));
157: }
158:
159: public final void testValidateAttributeRequired5() {
160: SpringContext.getBean(DictionaryValidationService.class)
161: .validateAttributeRequired(
162: SourceAccountingLine.class.getName(), "amount",
163: new BigDecimal(10.00), Boolean.FALSE, "amount");
164:
165: assertEquals(0, GlobalVariables.getErrorMap().size());
166: }
167:
168: /**
169: * Test validtion on a business object
170: */
171: public final void testValidateBusinessObject1() {
172: // test a simple object first (no references)
173: PaymentReasonCode dvPaymentReason = new PaymentReasonCode();
174: dvPaymentReason.setCode("TP");
175:
176: SpringContext
177: .getBean(DictionaryValidationService.class)
178: .validateBusinessObjectsRecursively(dvPaymentReason, -1);
179:
180: assertEquals(1, GlobalVariables.getErrorMap().size());
181: assertTrue(GlobalVariables.getErrorMap().fieldHasMessage(
182: "code", KFSKeyConstants.ERROR_MAX_LENGTH));
183: }
184:
185: public final void testValidateBusinessObject2() {
186: PaymentReasonCode dvPaymentReason = new PaymentReasonCode();
187: dvPaymentReason.setCode("-");
188:
189: SpringContext
190: .getBean(DictionaryValidationService.class)
191: .validateBusinessObjectsRecursively(dvPaymentReason, -1);
192:
193: assertEquals(1, GlobalVariables.getErrorMap().size());
194: assertTrue(GlobalVariables.getErrorMap().fieldHasMessage(
195: "code", KFSKeyConstants.ERROR_INVALID_FORMAT));
196: }
197:
198: public final void testValidateBusinessObject3() {
199: PaymentReasonCode dvPaymentReason = new PaymentReasonCode();
200: dvPaymentReason.setCode(" ");
201:
202: SpringContext
203: .getBean(DictionaryValidationService.class)
204: .validateBusinessObjectsRecursively(dvPaymentReason, -1);
205:
206: assertEquals(1, GlobalVariables.getErrorMap().size());
207: assertTrue(GlobalVariables.getErrorMap().fieldHasMessage(
208: "code", KFSKeyConstants.ERROR_REQUIRED));
209: }
210:
211: public final void testValidateBusinessObject4() {
212: // test object with references
213: SourceAccountingLine line = new SourceAccountingLine();
214: line.setPostingYear(new Integer(2005));
215: line.getChart().setChartOfAccountsCode("BAB");
216:
217: SpringContext.getBean(DictionaryValidationService.class)
218: .validateBusinessObjectsRecursively(line, -1);
219:
220: assertTrue(GlobalVariables.getErrorMap().fieldHasMessage(
221: "chart.chartOfAccountsCode",
222: KFSKeyConstants.ERROR_MAX_LENGTH));
223: assertTrue(GlobalVariables.getErrorMap().fieldHasMessage(
224: "chartOfAccountsCode", KFSKeyConstants.ERROR_REQUIRED));
225: }
226:
227: public final void testValidateReferenceExists_NullArguments1() {
228:
229: try {
230: SpringContext.getBean(DictionaryValidationService.class)
231: .validateReferenceExists(null, "test");
232: } catch (IllegalArgumentException e) {
233: assertTrue(
234: "An IllegalArgumentException should have been thrown.",
235: true);
236: return;
237: } catch (Exception e) {
238: assertTrue(
239: "An IllegalArgumentException should have been thrown.",
240: false);
241: }
242: assertTrue(
243: "An IllegalArgumentException should have been thrown.",
244: false);
245:
246: }
247:
248: public final void testValidateReferenceExists_NullArguments2() {
249:
250: try {
251: SpringContext.getBean(DictionaryValidationService.class)
252: .validateReferenceExists(new ProjectCode(),
253: (String) null);
254: } catch (IllegalArgumentException e) {
255: assertTrue(
256: "An IllegalArgumentException should have been thrown.",
257: true);
258: return;
259: } catch (Exception e) {
260: assertTrue(
261: "An IllegalArgumentException should have been thrown.",
262: false);
263: }
264: assertTrue(
265: "An IllegalArgumentException should have been thrown.",
266: false);
267:
268: }
269:
270: public final void testValidateReferenceExists_NullArguments3() {
271:
272: try {
273: SpringContext.getBean(DictionaryValidationService.class)
274: .validateReferenceExists(null, (String) null);
275: } catch (IllegalArgumentException e) {
276: assertTrue(
277: "An IllegalArgumentException should have been thrown.",
278: true);
279: return;
280: } catch (Exception e) {
281: assertTrue(
282: "An IllegalArgumentException should have been thrown.",
283: false);
284: }
285: assertTrue(
286: "An IllegalArgumentException should have been thrown.",
287: false);
288:
289: }
290:
291: public final void testValidateReferenceExists_ReferenceNameDoesntExist() {
292:
293: try {
294: SpringContext.getBean(DictionaryValidationService.class)
295: .validateReferenceExists(new Account(),
296: "gobbledyGook");
297: } catch (ReferenceAttributeDoesntExistException e) {
298: assertTrue(
299: "An ReferenceAttributeDoesntExistException should have been thrown.",
300: true);
301: return;
302: } catch (Exception e) {
303: assertTrue(
304: "An ReferenceAttributeDoesntExistException should have been thrown.",
305: false);
306: }
307: assertTrue(
308: "An ReferenceAttributeDoesntExistException should have been thrown.",
309: false);
310:
311: }
312:
313: public final void testValidateReferenceExists_ReferenceClassIsNotBoDescendent() {
314:
315: try {
316: SpringContext.getBean(DictionaryValidationService.class)
317: .validateReferenceExists(new Account(),
318: "accountNumber");
319: } catch (ObjectNotABusinessObjectRuntimeException e) {
320: assertTrue(
321: "An ObjectNotABusinessObjectRuntimeException should have been thrown.",
322: true);
323: return;
324: } catch (Exception e) {
325: assertTrue(
326: "An ObjectNotABusinessObjectRuntimeException should have been thrown.",
327: false);
328: }
329: assertTrue(
330: "An ObjectNotABusinessObjectRuntimeException should have been thrown.",
331: false);
332:
333: }
334:
335: public final void testValidateReferenceExists_AllFkeysAreNull() {
336:
337: boolean result;
338: Account account = new Account();
339:
340: result = SpringContext
341: .getBean(DictionaryValidationService.class)
342: .validateReferenceExists(account, "continuationAccount");
343: assertFalse(
344: "Return should be False when all foreign-key fields are null.",
345: result);
346:
347: account.setContinuationFinChrtOfAcctCd("");
348: account.setContinuationAccountNumber("");
349: result = SpringContext
350: .getBean(DictionaryValidationService.class)
351: .validateReferenceExists(account, "continuationAccount");
352: assertFalse(
353: "Return should be False when all foreign-key fields are empty-strings.",
354: result);
355:
356: }
357:
358: public final void testValidateReferenceExists_SomeFkeysAreNull() {
359:
360: boolean result;
361: Account account;
362:
363: account = new Account();
364: account.setContinuationAccountNumber(GOOD_ACCOUNT_NBR);
365: result = SpringContext
366: .getBean(DictionaryValidationService.class)
367: .validateReferenceExists(account, "continuationAccount");
368: assertFalse(
369: "Return should be False when any foreign-key fields are null.",
370: result);
371:
372: account = new Account();
373: account.setContinuationFinChrtOfAcctCd("");
374: account.setContinuationAccountNumber(GOOD_ACCOUNT_NBR);
375: result = SpringContext
376: .getBean(DictionaryValidationService.class)
377: .validateReferenceExists(account, "continuationAccount");
378: assertFalse(
379: "Return should be False when any foreign-key fields are empty-strings.",
380: result);
381:
382: }
383:
384: private void assertGoodCodesExist() {
385: Map pkMap = new HashMap();
386: pkMap.put("chartOfAccountsCode", GOOD_CHART_CODE);
387: pkMap.put("accountNumber", GOOD_ACCOUNT_NBR);
388: Account account = (Account) SpringContext.getBean(
389: BusinessObjectService.class).findByPrimaryKey(
390: Account.class, pkMap);
391: assertNotNull(
392: "If this fails, the rest of the test is meaningless, as the known good codes are no longer good",
393: account);
394: }
395:
396: private void assertBadCodesDontExist() {
397: Map pkMap = new HashMap();
398: pkMap.put("chartOfAccountsCode", BAD_CHART_CODE);
399: pkMap.put("accountNumber", BAD_ACCOUNT_NBR);
400: Account account = (Account) SpringContext.getBean(
401: BusinessObjectService.class).findByPrimaryKey(
402: Account.class, pkMap);
403: assertNull(
404: "If this fails, the rest of the test is meaningless, as the known bad codes are no longer bad",
405: account);
406: }
407:
408: public final void testValidateReferenceExists_AllFkeysPopulated_ObjectDoesntExist() {
409:
410: assertBadCodesDontExist();
411:
412: boolean result;
413: Account account;
414:
415: account = new Account();
416: account.setContinuationFinChrtOfAcctCd(BAD_CHART_CODE);
417: account.setContinuationAccountNumber(BAD_ACCOUNT_NBR);
418: result = SpringContext
419: .getBean(DictionaryValidationService.class)
420: .validateReferenceExists(account, "continuationAccount");
421: assertFalse("Return should be False on known-bad codes.",
422: result);
423:
424: }
425:
426: public final void testValidateReferenceExists_AllFkeysPopulated_ObjectDoesExist() {
427:
428: assertGoodCodesExist();
429:
430: boolean result;
431: Account account;
432:
433: account = new Account();
434: account.setContinuationFinChrtOfAcctCd(GOOD_CHART_CODE);
435: account.setContinuationAccountNumber(GOOD_ACCOUNT_NBR);
436: result = SpringContext
437: .getBean(DictionaryValidationService.class)
438: .validateReferenceExists(account, "continuationAccount");
439: assertTrue("Return should be True on known-good codes.", result);
440:
441: }
442:
443: }
|