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.kfs.service;
017:
018: import java.io.InputStream;
019: import java.util.ArrayList;
020: import java.util.List;
021:
022: import org.apache.commons.io.IOUtils;
023: import org.kuali.kfs.batch.BatchInputFileType;
024: import org.kuali.kfs.context.KualiTestBase;
025: import org.kuali.kfs.context.SpringContext;
026: import org.kuali.kfs.exceptions.XMLParseException;
027: import org.kuali.module.financial.batch.pcard.ProcurementCardInputFileType;
028: import org.kuali.module.financial.bo.ProcurementCardTransaction;
029: import org.kuali.module.gl.batch.collector.CollectorBatch;
030: import org.kuali.module.gl.batch.collector.CollectorInputFileType;
031: import org.kuali.module.gl.bo.CollectorDetail;
032: import org.kuali.module.gl.bo.OriginEntryFull;
033: import org.kuali.test.ConfigureContext;
034:
035: /**
036: * Tests for the service parse method.
037: */
038: @ConfigureContext
039: public class BatchInputServiceParseTest extends KualiTestBase {
040: private static final String TEST_BATCH_XML_DIRECTORY = "org/kuali/kfs/batch/xml/";
041:
042: private BatchInputFileService batchInputFileService;
043:
044: private byte[] validPCDOFileContents;
045: private byte[] validCollectorFileContents;
046:
047: private BatchInputFileType pcdoBatchInputFileType;
048: private BatchInputFileType collectorBatchInputFileType;
049:
050: /**
051: * @see junit.framework.TestCase#setUp()
052: */
053: @Override
054: protected void setUp() throws Exception {
055: super .setUp();
056:
057: batchInputFileService = SpringContext
058: .getBean(BatchInputFileService.class);
059: pcdoBatchInputFileType = SpringContext
060: .getBean(ProcurementCardInputFileType.class);
061: collectorBatchInputFileType = SpringContext
062: .getBean(CollectorInputFileType.class);
063:
064: InputStream pcdoValidFileStream = ClassLoader
065: .getSystemResourceAsStream(TEST_BATCH_XML_DIRECTORY
066: + "BatchInputValidPCDO.xml");
067: validPCDOFileContents = IOUtils
068: .toByteArray(pcdoValidFileStream);
069: InputStream collectorValidFileStream = ClassLoader
070: .getSystemResourceAsStream(TEST_BATCH_XML_DIRECTORY
071: + "BatchInputValidCollector.xml");
072: validCollectorFileContents = IOUtils
073: .toByteArray(collectorValidFileStream);
074: }
075:
076: /**
077: * Verifies the correct object graph is being built from the pcdo file contents in the service parse method. The PCDO
078: * unmarshalling rules specify the result should be a ArrayList of ProcurementCardTransaction objects.
079: *
080: * @see org.kuali.module.financial.bo.ProcurementCardTransaction
081: */
082: public final void testParse_pcdoValidContents() throws Exception {
083: Object parsedObject = batchInputFileService.parse(
084: pcdoBatchInputFileType, validPCDOFileContents);
085:
086: assertNotNull("parsed object was null", parsedObject);
087: assertEquals("incorrect object type constructured from parse",
088: java.util.ArrayList.class, parsedObject.getClass());
089:
090: List parsedList = (ArrayList) parsedObject;
091: assertEquals("parsed object size was incorrect", 3, parsedList
092: .size());
093:
094: for (int i = 0; i < parsedList.size(); i++) {
095: Object parsedElement = parsedList.get(i);
096: assertEquals(
097: "list object type was incorrect on index " + i,
098: ProcurementCardTransaction.class, parsedElement
099: .getClass());
100: }
101: }
102:
103: /**
104: * Verifies the correct object graph is being built from the collector file contents in the service parse method. The Collector
105: * unmarshalling rules specify the result should be a populated CollectorBatch object.
106: *
107: * @see org.kuali.module.gl.collector.xml.CollectorBatch
108: */
109: public final void testParse_collectorValidContents()
110: throws Exception {
111: Object parsedObject = batchInputFileService
112: .parse(collectorBatchInputFileType,
113: validCollectorFileContents);
114:
115: assertNotNull("parsed object was null", parsedObject);
116: assertEquals("incorrect object type constructured from parse",
117: CollectorBatch.class, parsedObject.getClass());
118:
119: CollectorBatch collectorBatch = (CollectorBatch) parsedObject;
120:
121: // verify origin entries were populated
122: assertEquals(
123: "parsed object has incorrect number of origin entries",
124: 2, collectorBatch.getOriginEntries().size());
125:
126: for (int i = 0; i < collectorBatch.getOriginEntries().size(); i++) {
127: Object originEntry = collectorBatch.getOriginEntries().get(
128: i);
129:
130: assertNotNull("orgin entry record is null on index " + i,
131: originEntry);
132: assertEquals("object type was incorrect on index " + i,
133: OriginEntryFull.class, originEntry.getClass());
134: }
135:
136: // verify id billing entries were populated
137: assertEquals(
138: "parsed object has incorrect number of id billing entries",
139: 2, collectorBatch.getCollectorDetails().size());
140:
141: for (int i = 0; i < collectorBatch.getCollectorDetails().size(); i++) {
142: Object idBilling = collectorBatch.getCollectorDetails()
143: .get(i);
144:
145: assertNotNull("ID Billing record is null on index " + i,
146: idBilling);
147: assertEquals("object type was incorrect on index " + i,
148: CollectorDetail.class, idBilling.getClass());
149: }
150:
151: assertEquals(
152: "number of batch records does not match header count",
153: collectorBatch.getTotalRecords().intValue(),
154: collectorBatch.getOriginEntries().size()
155: + collectorBatch.getCollectorDetails().size());
156: }
157:
158: /**
159: * Verifies exception is thrown on parse method if file contents are empty.
160: */
161: public final void testParse_emptyFileContents() throws Exception {
162: byte[] emptyContents = null;
163:
164: boolean failedAsExpected = false;
165: try {
166: batchInputFileService.parse(pcdoBatchInputFileType,
167: emptyContents);
168: } catch (IllegalArgumentException e) {
169: failedAsExpected = true;
170: }
171: assertTrue(
172: "exception not thrown for null empty pcdo file contents",
173: failedAsExpected);
174:
175: failedAsExpected = false;
176: try {
177: batchInputFileService.parse(collectorBatchInputFileType,
178: emptyContents);
179: } catch (IllegalArgumentException e) {
180: failedAsExpected = true;
181: }
182: assertTrue(
183: "exception not thrown for null empty collector file contents",
184: failedAsExpected);
185: }
186:
187: /**
188: * Verifies error message occurs on parse when an invalid xml format is given.
189: */
190: public final void testParse_invalidTagOrder() throws Exception {
191: InputStream fileContents = ClassLoader
192: .getSystemResourceAsStream(TEST_BATCH_XML_DIRECTORY
193: + "BatchInputInvalidTagOrderPCDO.xml");
194: byte[] invalidTagOrderPCDOFileContents = IOUtils
195: .toByteArray(fileContents);
196:
197: boolean failedAsExpected = false;
198: try {
199: batchInputFileService.parse(pcdoBatchInputFileType,
200: invalidTagOrderPCDOFileContents);
201: } catch (XMLParseException e) {
202: failedAsExpected = true;
203: }
204:
205: assertTrue(
206: "parse exception not thrown for xml with invalid tag order",
207: failedAsExpected);
208: }
209:
210: /**
211: * Verifies error message occurs on parse when an invalid xml format is given.
212: */
213: public final void testParse_missingRequiredField() throws Exception {
214: InputStream fileContents = ClassLoader
215: .getSystemResourceAsStream(TEST_BATCH_XML_DIRECTORY
216: + "BatchInputMissingTagPCDO.xml");
217: byte[] missingTagPCDOFileContents = IOUtils
218: .toByteArray(fileContents);
219:
220: boolean failedAsExpected = false;
221: try {
222: batchInputFileService.parse(pcdoBatchInputFileType,
223: missingTagPCDOFileContents);
224: } catch (XMLParseException e) {
225: failedAsExpected = true;
226: }
227:
228: assertTrue(
229: "parse exception not thrown for xml missing a required field",
230: failedAsExpected);
231: }
232:
233: /**
234: * Verifies error occurs when an invalid tag is given.
235: */
236: public final void testParse_invalidTag() throws Exception {
237: InputStream fileContents = ClassLoader
238: .getSystemResourceAsStream(TEST_BATCH_XML_DIRECTORY
239: + "BatchInputInvalidTagCollector.xml");
240: byte[] invalidTagCollectorContents = IOUtils
241: .toByteArray(fileContents);
242:
243: boolean failedAsExpected = false;
244: try {
245: batchInputFileService.parse(collectorBatchInputFileType,
246: invalidTagCollectorContents);
247: } catch (XMLParseException e) {
248: failedAsExpected = true;
249: }
250:
251: assertTrue("parse exception not thrown for invalid tag",
252: failedAsExpected);
253: }
254: }
|