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.financial.service;
017:
018: import java.util.HashMap;
019: import java.util.Map;
020:
021: import org.kuali.core.service.BusinessObjectService;
022: import org.kuali.core.service.DateTimeService;
023: import org.kuali.kfs.KFSConstants;
024: import org.kuali.kfs.context.KualiTestBase;
025: import org.kuali.kfs.context.SpringContext;
026: import org.kuali.module.financial.bo.CashDrawer;
027: import org.kuali.test.ConfigureContext;
028:
029: /**
030: * This class tests the Check service.
031: */
032: @ConfigureContext
033: public class CashDrawerServiceTest extends KualiTestBase {
034: private static final String BLANK_WORKGROUP_NAME = "";
035: private static final String VALID_WORKGROUP_NAME = "testWorkgroup";
036: private static final String BLANK_DOC_ID = " ";
037: private static final String VALID_DOC_ID = "1234";
038: private static final String OTHER_DOC_ID = "4321";
039:
040: /**
041: *
042: * This method tests that calling the openCashDrawer method on a CashDrawerService with a blank workgroup name
043: * generates an error.
044: */
045: public final void testOpenCashDrawer_blankWorkgroup() {
046: boolean failedAsExpected = false;
047:
048: try {
049: SpringContext.getBean(CashDrawerService.class)
050: .openCashDrawer(BLANK_WORKGROUP_NAME, VALID_DOC_ID);
051: } catch (IllegalArgumentException e) {
052: failedAsExpected = true;
053: }
054:
055: assertTrue(failedAsExpected);
056: }
057:
058: /**
059: *
060: * This method tests that calling the openCashDrawer method on a CashDrawerService with a blank document id generates
061: * an error.
062: */
063: public final void testOpenCashDrawer_blankDocId() {
064: boolean failedAsExpected = false;
065:
066: try {
067: SpringContext.getBean(CashDrawerService.class)
068: .openCashDrawer(VALID_WORKGROUP_NAME, BLANK_DOC_ID);
069: } catch (IllegalArgumentException e) {
070: failedAsExpected = true;
071: }
072:
073: assertTrue(failedAsExpected);
074: }
075:
076: /**
077: *
078: * This method tests that calling the openCashDrawer method on a CashDrawerService, when the cash drawer doesn't exist,
079: * will create a new cash drawer and open it.
080: */
081: public final void testOpenCashDrawer_nonexistent() {
082: final String workgroup = VALID_WORKGROUP_NAME;
083:
084: // make sure it doesn't exist
085: deleteCashDrawer(workgroup);
086:
087: // open it
088: SpringContext.getBean(CashDrawerService.class).openCashDrawer(
089: workgroup, VALID_DOC_ID);
090:
091: // verify that it is open
092: CashDrawer drawer = SpringContext.getBean(
093: CashDrawerService.class).getByWorkgroupName(workgroup,
094: false);
095: assertEquals(KFSConstants.CashDrawerConstants.STATUS_OPEN,
096: drawer.getStatusCode());
097: assertEquals(VALID_DOC_ID, drawer
098: .getReferenceFinancialDocumentNumber());
099: }
100:
101: /**
102: *
103: * This method tests the openCashDrawer method under valid conditions.
104: */
105: public final void testOpenCashDrawer() {
106: final String workgroup = VALID_WORKGROUP_NAME;
107:
108: // make sure it exists
109: createCashDrawer(workgroup);
110:
111: // open it
112: SpringContext.getBean(CashDrawerService.class).openCashDrawer(
113: workgroup, VALID_DOC_ID);
114:
115: // make sure it is open
116: CashDrawer drawer = SpringContext.getBean(
117: CashDrawerService.class).getByWorkgroupName(workgroup,
118: false);
119: assertEquals(KFSConstants.CashDrawerConstants.STATUS_OPEN,
120: drawer.getStatusCode());
121: assertEquals(VALID_DOC_ID, drawer
122: .getReferenceFinancialDocumentNumber());
123: }
124:
125: /**
126: *
127: * This method tests that calling the closeCashDrawer method on a CashDrawerService with a blank workgroup name
128: * generates an error.
129: */
130: public final void testCloseCashDrawer_blankWorkgroup() {
131: boolean failedAsExpected = false;
132:
133: try {
134: SpringContext.getBean(CashDrawerService.class)
135: .closeCashDrawer(BLANK_WORKGROUP_NAME);
136: } catch (IllegalArgumentException e) {
137: failedAsExpected = true;
138: }
139:
140: assertTrue(failedAsExpected);
141: }
142:
143: /**
144: *
145: * This method tests that calling the closeCashDrawer method on a CashDrawerService, when the cash drawer doesn't exist,
146: * will create a new cash drawer and closes it.
147: */
148: public final void testCloseCashDrawer_nonexistent() {
149: final String workgroup = VALID_WORKGROUP_NAME;
150:
151: // make sure it doesn't exist
152: deleteCashDrawer(workgroup);
153:
154: // close it
155: SpringContext.getBean(CashDrawerService.class).closeCashDrawer(
156: workgroup);
157:
158: // verify that it is closed
159: CashDrawer drawer = SpringContext.getBean(
160: CashDrawerService.class).getByWorkgroupName(workgroup,
161: false);
162: assertEquals(KFSConstants.CashDrawerConstants.STATUS_CLOSED,
163: drawer.getStatusCode());
164: assertNull(drawer.getReferenceFinancialDocumentNumber());
165: }
166:
167: /**
168: *
169: * This method tests the closeCashDrawer method under valid conditions.
170: */
171: public final void testCloseCashDrawer_existent() {
172: final String workgroup = VALID_WORKGROUP_NAME;
173:
174: // make sure it exists
175: createCashDrawer(workgroup);
176:
177: // close it
178: SpringContext.getBean(CashDrawerService.class).closeCashDrawer(
179: workgroup);
180:
181: // make sure it is closed
182: CashDrawer drawer = SpringContext.getBean(
183: CashDrawerService.class).getByWorkgroupName(workgroup,
184: false);
185: assertEquals(KFSConstants.CashDrawerConstants.STATUS_CLOSED,
186: drawer.getStatusCode());
187: assertNull(drawer.getReferenceFinancialDocumentNumber());
188: }
189:
190: /**
191: *
192: * This method tests that calling the lockCashDrawer method on a CashDrawerService with a blank workgroup name
193: * generates an error.
194: */
195: public final void testLockCashDrawer_blankWorkgroup() {
196: boolean failedAsExpected = false;
197:
198: try {
199: SpringContext.getBean(CashDrawerService.class)
200: .lockCashDrawer(BLANK_WORKGROUP_NAME, VALID_DOC_ID);
201: } catch (IllegalArgumentException e) {
202: failedAsExpected = true;
203: }
204:
205: assertTrue(failedAsExpected);
206: }
207:
208: /**
209: *
210: * This method tests that calling the lockCashDrawer method on a CashDrawerService, when the cash drawer doesn't exist,
211: * an error is generated.
212: */
213: public final void testLockCashDrawer_nonexistent() {
214: final String workgroup = VALID_WORKGROUP_NAME;
215:
216: // make sure it doesn't exist
217: deleteCashDrawer(workgroup);
218:
219: // lock it
220: boolean failedAsExpected = false;
221: try {
222: SpringContext.getBean(CashDrawerService.class)
223: .lockCashDrawer(workgroup, VALID_DOC_ID);
224: } catch (IllegalStateException e) {
225: failedAsExpected = true;
226: }
227:
228: assertTrue(failedAsExpected);
229: }
230:
231: /**
232: *
233: * This method tests that calling the lockCashDrawer method on a CashDrawerService, when the cash drawer
234: * is closed, an error is generated.
235: */
236: public final void testLockCashDrawer_closed() {
237: final String workgroup = VALID_WORKGROUP_NAME;
238:
239: // close it
240: SpringContext.getBean(CashDrawerService.class).closeCashDrawer(
241: workgroup);
242:
243: // lock it
244: boolean failedAsExpected = false;
245: try {
246: SpringContext.getBean(CashDrawerService.class)
247: .lockCashDrawer(workgroup, VALID_DOC_ID);
248: } catch (IllegalStateException e) {
249: failedAsExpected = true;
250: }
251:
252: assertTrue(failedAsExpected);
253: }
254:
255: /**
256: *
257: * This method tests that calling the lockCashDrawer method on a CashDrawerService, when the cash drawer
258: * is already locked, an error is generated.
259: */
260: public final void testLockCashDrawer_alreadyLocked() {
261: final String workgroup = VALID_WORKGROUP_NAME;
262:
263: // open it
264: SpringContext.getBean(CashDrawerService.class).openCashDrawer(
265: workgroup, VALID_DOC_ID);
266:
267: // lock it twice
268: SpringContext.getBean(CashDrawerService.class).lockCashDrawer(
269: workgroup, VALID_DOC_ID);
270:
271: boolean failedAsExpected = false;
272: try {
273: SpringContext.getBean(CashDrawerService.class)
274: .lockCashDrawer(workgroup, VALID_DOC_ID);
275: } catch (IllegalStateException e) {
276: failedAsExpected = true;
277: }
278:
279: assertTrue(failedAsExpected);
280: }
281:
282: /**
283: *
284: * This method tests that calling the lockCashDrawer method on a CashDrawerService, when the cash drawer
285: * is already opened by another document, an error is generated.
286: */
287: public final void testLockCashDrawer_openedByDifferentDocument() {
288: final String workgroup = VALID_WORKGROUP_NAME;
289:
290: // open it
291: SpringContext.getBean(CashDrawerService.class).openCashDrawer(
292: workgroup, OTHER_DOC_ID);
293:
294: // lock it
295: boolean failedAsExpected = false;
296: try {
297: SpringContext.getBean(CashDrawerService.class)
298: .lockCashDrawer(workgroup, VALID_DOC_ID);
299: } catch (IllegalStateException e) {
300: failedAsExpected = true;
301: }
302:
303: assertTrue(failedAsExpected);
304: }
305:
306: /**
307: *
308: * This method tests the lockCashDrawer method under valid conditions.
309: */
310: public final void testLockCashDrawer_open() {
311: final String workgroup = VALID_WORKGROUP_NAME;
312:
313: // open it
314: SpringContext.getBean(CashDrawerService.class).openCashDrawer(
315: workgroup, VALID_DOC_ID);
316:
317: // lock it
318: SpringContext.getBean(CashDrawerService.class).lockCashDrawer(
319: workgroup, VALID_DOC_ID);
320:
321: // verify that it is locked
322: CashDrawer drawer = SpringContext.getBean(
323: CashDrawerService.class).getByWorkgroupName(workgroup,
324: false);
325: assertEquals(KFSConstants.CashDrawerConstants.STATUS_LOCKED,
326: drawer.getStatusCode());
327: assertEquals(VALID_DOC_ID, drawer
328: .getReferenceFinancialDocumentNumber());
329: }
330:
331: /**
332: *
333: * This method tests that calling the unlockCashDrawer method on a CashDrawerService with a blank workgroup name
334: * generates an error.
335: */
336: public final void testUnlockCashDrawer_blankWorkgroup() {
337: boolean failedAsExpected = false;
338:
339: try {
340: SpringContext.getBean(CashDrawerService.class)
341: .unlockCashDrawer(BLANK_WORKGROUP_NAME,
342: VALID_DOC_ID);
343: } catch (IllegalArgumentException e) {
344: failedAsExpected = true;
345: }
346:
347: assertTrue(failedAsExpected);
348: }
349:
350: /**
351: *
352: * This method tests that calling the unlockCashDrawer method on a CashDrawerService, when the cash drawer doesn't exist,
353: * an error is generated.
354: */
355: public final void testUnlockCashDrawer_nonexistent() {
356: final String workgroup = VALID_WORKGROUP_NAME;
357:
358: // make sure it doesn't exist
359: deleteCashDrawer(workgroup);
360:
361: // lock it
362: boolean failedAsExpected = false;
363: try {
364: SpringContext.getBean(CashDrawerService.class)
365: .unlockCashDrawer(workgroup, VALID_DOC_ID);
366: } catch (IllegalStateException e) {
367: failedAsExpected = true;
368: }
369:
370: assertTrue(failedAsExpected);
371: }
372:
373: /**
374: *
375: * This method tests that calling the lockCashDrawer method on a CashDrawerService, when the cash drawer is already open,
376: * an error is generated.
377: */
378: public final void testUnlockCashDrawer_open() {
379: final String workgroup = VALID_WORKGROUP_NAME;
380:
381: // open it
382: SpringContext.getBean(CashDrawerService.class).openCashDrawer(
383: workgroup, VALID_DOC_ID);
384:
385: // unlock it
386: boolean failedAsExpected = false;
387: try {
388: SpringContext.getBean(CashDrawerService.class)
389: .unlockCashDrawer(workgroup, VALID_DOC_ID);
390: } catch (IllegalStateException e) {
391: failedAsExpected = true;
392: }
393:
394: assertTrue(failedAsExpected);
395: }
396:
397: /**
398: *
399: * This method tests the unlockCashDrawer method under valid conditions.
400: */
401: public final void testUnlockCashDrawer_locked() {
402: final String workgroup = VALID_WORKGROUP_NAME;
403:
404: // lock it
405: SpringContext.getBean(CashDrawerService.class).openCashDrawer(
406: workgroup, VALID_DOC_ID);
407: SpringContext.getBean(CashDrawerService.class).lockCashDrawer(
408: workgroup, VALID_DOC_ID);
409:
410: // unlock it
411: SpringContext.getBean(CashDrawerService.class)
412: .unlockCashDrawer(workgroup, VALID_DOC_ID);
413:
414: // verify that it is unlocked
415: CashDrawer drawer = SpringContext.getBean(
416: CashDrawerService.class).getByWorkgroupName(workgroup,
417: false);
418: assertEquals(KFSConstants.CashDrawerConstants.STATUS_OPEN,
419: drawer.getStatusCode());
420: assertEquals(VALID_DOC_ID, drawer
421: .getReferenceFinancialDocumentNumber());
422: }
423:
424: /**
425: *
426: * This method tests that calling the unlockCashDrawer method on a CashDrawerService, when the cash drawer
427: * is already locked by another document, an error is generated.
428: */
429: public final void testUnlockCashDrawer_lockedByDifferentDocumentId() {
430: final String workgroup = VALID_WORKGROUP_NAME;
431:
432: // lock it
433: SpringContext.getBean(CashDrawerService.class).openCashDrawer(
434: workgroup, OTHER_DOC_ID);
435: SpringContext.getBean(CashDrawerService.class).lockCashDrawer(
436: workgroup, OTHER_DOC_ID);
437:
438: // unlock it
439: boolean failedAsExpected = false;
440: try {
441: SpringContext.getBean(CashDrawerService.class)
442: .unlockCashDrawer(workgroup, VALID_DOC_ID);
443: } catch (IllegalStateException e) {
444: failedAsExpected = true;
445: }
446:
447: assertTrue(failedAsExpected);
448: }
449:
450: /**
451: *
452: * This method tests that trying to retrieve a cash drawer by a workgroup name, when the workgroup name provided is
453: * blank, will generate an error.
454: */
455: public final void testGetByWorkgroupName_blankWorkgroup() {
456: boolean failedAsExpected = false;
457:
458: try {
459: SpringContext.getBean(CashDrawerService.class)
460: .getByWorkgroupName(" ", false);
461: } catch (IllegalArgumentException e) {
462: failedAsExpected = true;
463: }
464:
465: assertTrue(failedAsExpected);
466: }
467:
468: /**
469: *
470: * This method tests that trying to retrieve a cash drawer by a workgroup name, when the workgroup does not exist,
471: * will generate an error.
472: */
473: public final void testGetByWorkgroupName_nonexistentWorkgroup() {
474: CashDrawer d = SpringContext.getBean(CashDrawerService.class)
475: .getByWorkgroupName("foo", false);
476:
477: assertNull(d);
478: }
479:
480: /**
481: *
482: * This method tests the getByWorkgroupName method under valid conditions.
483: */
484: public final void testGetByWorkgroupName_existingWorkgroup() {
485: final String workgroup = VALID_WORKGROUP_NAME;
486:
487: createCashDrawer(VALID_WORKGROUP_NAME);
488:
489: CashDrawer d = SpringContext.getBean(CashDrawerService.class)
490: .getByWorkgroupName(workgroup, false);
491:
492: assertNotNull(d);
493: assertEquals(workgroup, d.getWorkgroupName());
494: assertEquals(KFSConstants.CashDrawerConstants.STATUS_CLOSED, d
495: .getStatusCode());
496: }
497:
498: /**
499: *
500: * This method tests that all the possible steps during the lifecycle of a cash drawer function properly and are
501: * achievable with the expected results.
502: */
503: public final void testLifeCycle() {
504: final String RANDOM_WORKGROUP_NAME = "testWorkgroup-"
505: + SpringContext.getBean(DateTimeService.class)
506: .getCurrentDate().getTime();
507:
508: boolean deleteSucceeded = false;
509:
510: CashDrawer preExisting = SpringContext.getBean(
511: CashDrawerService.class).getByWorkgroupName(
512: RANDOM_WORKGROUP_NAME, false);
513: assertNull(preExisting);
514:
515: CashDrawer created = new CashDrawer();
516: created.setWorkgroupName(RANDOM_WORKGROUP_NAME);
517: created
518: .setStatusCode(KFSConstants.CashDrawerConstants.STATUS_CLOSED);
519:
520: CashDrawer retrieved = null;
521: try {
522: SpringContext.getBean(BusinessObjectService.class).save(
523: created);
524:
525: retrieved = SpringContext.getBean(CashDrawerService.class)
526: .getByWorkgroupName(RANDOM_WORKGROUP_NAME, false);
527: assertNotNull(retrieved);
528:
529: // compare
530: assertEquals(created.getWorkgroupName(), retrieved
531: .getWorkgroupName());
532: assertEquals(created.getStatusCode(), retrieved
533: .getStatusCode());
534: assertNull(retrieved.getReferenceFinancialDocumentNumber());
535: } finally {
536: // delete it
537: if (retrieved != null) {
538: SpringContext.getBean(BusinessObjectService.class)
539: .delete(retrieved);
540: }
541: }
542:
543: // verify that the delete succeeded
544: retrieved = SpringContext.getBean(CashDrawerService.class)
545: .getByWorkgroupName(RANDOM_WORKGROUP_NAME, false);
546: assertNull(retrieved);
547: }
548:
549: // utility methods
550:
551: /**
552: * This method performs the necessary steps to delete a cash drawer from the database for the given workgroup name.
553: * @param workgroupName The name of the workgroup of the cash drawer to be deleted.
554: */
555: private void deleteCashDrawer(String workgroupName) {
556: Map deleteCriteria = new HashMap();
557: deleteCriteria.put("workgroupName", workgroupName);
558: SpringContext.getBean(BusinessObjectService.class)
559: .deleteMatching(CashDrawer.class, deleteCriteria);
560: }
561:
562: /**
563: *
564: * This method performs the necessary steps to create a new cash drawer in the database for the given workgroup name.
565: * @param workgroupName The name of the workgroup of the cash drawer being created.
566: */
567: private void createCashDrawer(String workgroupName) {
568: deleteCashDrawer(workgroupName);
569:
570: CashDrawer cd = new CashDrawer();
571: cd.setWorkgroupName(workgroupName);
572: cd
573: .setStatusCode(KFSConstants.CashDrawerConstants.STATUS_CLOSED);
574: SpringContext.getBean(BusinessObjectService.class).save(cd);
575: }
576: }
|