View Javadoc
1   /*
2    * The Kuali Financial System, a comprehensive financial management system for higher education.
3    * 
4    * Copyright 2005-2014 The Kuali Foundation
5    * 
6    * This program is free software: you can redistribute it and/or modify
7    * it under the terms of the GNU Affero General Public License as
8    * published by the Free Software Foundation, either version 3 of the
9    * License, or (at your option) any later version.
10   * 
11   * This program is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   * GNU Affero General Public License for more details.
15   * 
16   * You should have received a copy of the GNU Affero General Public License
17   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18   */
19  package org.kuali.kfs.sys.batch;
20  
21  import java.io.InputStream;
22  import java.util.ArrayList;
23  import java.util.List;
24  
25  import org.apache.commons.io.IOUtils;
26  import org.kuali.kfs.fp.batch.ProcurementCardInputFileType;
27  import org.kuali.kfs.fp.businessobject.ProcurementCardTransaction;
28  import org.kuali.kfs.gl.batch.CollectorXmlInputFileType;
29  import org.kuali.kfs.sys.ConfigureContext;
30  import org.kuali.kfs.sys.batch.service.BatchInputFileService;
31  import org.kuali.kfs.sys.businessobject.HeaderLine;
32  import org.kuali.kfs.sys.context.KualiTestBase;
33  import org.kuali.kfs.sys.context.SpringContext;
34  import org.kuali.kfs.sys.exception.ParseException;
35  
36  /**
37   * Tests the flatFileParser.
38   * 
39   * @see org.kuali.kfs.sys.batch.BatchInputFileType
40   */
41  @ConfigureContext
42  public class FlatFileParserTest extends KualiTestBase {
43      private static final String TEST_BATCH_XML_DIRECTORY = "org/kuali/kfs/sys/batch/fixture/";
44      private BatchInputFileService batchInputFileService;
45      private BatchInputFileType sampleBatchInputFileType;
46      private byte[] sampleValidFileContents;
47      private byte[] sampleInvalidPropertyFileContents;
48      private byte[] sampleNoHeaderFileContents;
49      
50      /**
51       * @see junit.framework.TestCase#setUp()
52       */
53      @Override
54      protected void setUp() throws Exception {
55          super.setUp();
56          
57          batchInputFileService = SpringContext.getBean(BatchInputFileService.class);
58          sampleBatchInputFileType = SpringContext.getBean(BatchInputFileType.class,"sampleTestFlatFileInputFileType");
59          
60  
61          InputStream validFileStream = FlatFileParserTest.class.getClassLoader().getResourceAsStream(TEST_BATCH_XML_DIRECTORY + "SampleValidFlatFile.txt");
62          sampleValidFileContents = IOUtils.toByteArray(validFileStream);
63          
64          InputStream sampleInvalidPropertyFileStream = FlatFileParserTest.class.getClassLoader().getResourceAsStream(TEST_BATCH_XML_DIRECTORY + "SampleInValidPropertyFlatFile.txt");
65          sampleInvalidPropertyFileContents = IOUtils.toByteArray(sampleInvalidPropertyFileStream);
66          
67          InputStream sampleNoHeaderFileStream = FlatFileParserTest.class.getClassLoader().getResourceAsStream(TEST_BATCH_XML_DIRECTORY + "SampleNoHeaderFlatFile.txt");
68          sampleNoHeaderFileContents = IOUtils.toByteArray(sampleNoHeaderFileStream);
69          
70      }
71      
72      
73      /**
74       * Verifies the correct object graph is being built from the sample valid file contents 
75       * 
76       */
77      public final void testParseSampleValidContents() throws Exception {
78          Object parsedObject = batchInputFileService.parse(sampleBatchInputFileType, sampleValidFileContents);
79  
80          assertNotNull("parsed object was null", parsedObject);
81          assertEquals("incorrect object type constructured from parse", java.util.ArrayList.class, parsedObject.getClass());
82  
83          List parsedList = (ArrayList) parsedObject;
84          assertEquals("parsed object size was incorrect", 1, parsedList.size());
85  
86          for (int i = 0; i < parsedList.size(); i++) {
87              Object parsedElement = parsedList.get(i);
88              assertEquals("list object type was incorrect on index " + i, HeaderLine.class, parsedElement.getClass());
89          }
90      }
91      
92      /**
93       * Verifies exception is thrown on parse method if invalid property found in the file contents .
94       */
95      public final void testParseInvalidPropertyFileContents() throws Exception {
96      
97          boolean failedAsExpected = false;
98          String errorMessage = "";
99          try {
100             Object parsedObject =  batchInputFileService.parse(sampleBatchInputFileType, sampleInvalidPropertyFileContents);
101         }
102         catch (ParseException e) {
103             errorMessage = e.getStackTrace().toString();
104             failedAsExpected = true;
105         }
106         assertTrue("exception thrown for invalid property " , failedAsExpected);
107        
108     }
109     
110     /**
111      * Verifies exception is thrown on parse method if invalid property found in the file contents .
112      */
113     public final void testNoHeaderFileContents() throws Exception {
114     
115         boolean failedAsExpected = false;
116         try {
117             batchInputFileService.parse(sampleBatchInputFileType, sampleNoHeaderFileContents);
118         }
119         catch (ParseException e) {
120             failedAsExpected = true;
121         }
122         assertTrue("exception thrown for the file with no header " , failedAsExpected);
123        
124     }
125     
126     
127 }