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.File;
019: import java.io.FileNotFoundException;
020: import java.io.InputStream;
021: import java.util.ArrayList;
022: import java.util.List;
023:
024: import org.apache.commons.lang.StringUtils;
025: import org.apache.commons.lang.math.RandomUtils;
026: import org.kuali.core.bo.user.UniversalUser;
027: import org.kuali.core.exceptions.AuthorizationException;
028: import org.kuali.core.service.UniversalUserService;
029: import org.kuali.kfs.batch.BatchInputFileType;
030: import org.kuali.kfs.context.KualiTestBase;
031: import org.kuali.kfs.context.SpringContext;
032: import org.kuali.kfs.exceptions.FileStorageException;
033: import org.kuali.module.financial.batch.pcard.ProcurementCardInputFileType;
034: import org.kuali.module.gl.batch.collector.CollectorInputFileType;
035: import org.kuali.test.ConfigureContext;
036: import org.kuali.test.KualiTestConstants.TestConstants.Data4;
037:
038: /**
039: * Tests the BatchInputFileService. TEST DEPENDENCIES The following are external configurations not setup by the test case that are
040: * necessary for the test to run correctly: 1) User identified by the constant Data4_USER_ID2 must be a member of the workgroup
041: * given by constant Data2.KUALI_FMSOPS. 2) User identified by the constant Data4_USER_ID1 must NOT be a member of the workgroup
042: * given by constant Data2.KUALI_FMSOPS. 3) Five xml files must exist in the PCDO staging directory. Name of files is not important.
043: * 4) One Collector files must exist in Collector staging directory. File name should not contain the usernames given by constants
044: * Data4_USER_ID2 and Data4_USER_ID1. Note the files in #3 & #4 are created by the project build from files located in project
045: * folder buld/configurationFiles/externalConfigDirectory/static/staging/
046: *
047: * @see org.kuali.kfs.service.BatchInputFileService Unit tests for this service are also in:
048: * @see org.kuali.kfs.service.BatchInputServiceParseTest
049: * @see org.kuali.kfs.service.BatchInputServiceSystemParametersTest
050: */
051: @ConfigureContext
052: public class BatchInputFileServiceTest extends KualiTestBase {
053: private static final String TEST_BATCH_XML_DIRECTORY = "org/kuali/kfs/batch/xml/";
054:
055: private BatchInputFileService batchInputFileService;
056:
057: private String testFileIdentifier;
058: private InputStream validPCDOFileContents;
059: private InputStream validCollectorFileContents;
060:
061: private BatchInputFileType pcdoBatchInputFileType;
062: private BatchInputFileType collectorBatchInputFileType;
063:
064: private UniversalUser validWorkgroupUser;
065: private UniversalUser invalidWorkgroupUser;
066:
067: private List<File> createdTestFiles;
068:
069: /**
070: * @see junit.framework.TestCase#setUp()
071: */
072: @Override
073: protected void setUp() throws Exception {
074: super .setUp();
075:
076: batchInputFileService = SpringContext
077: .getBean(BatchInputFileService.class);
078: pcdoBatchInputFileType = SpringContext
079: .getBean(ProcurementCardInputFileType.class);
080: collectorBatchInputFileType = SpringContext
081: .getBean(CollectorInputFileType.class);
082:
083: testFileIdentifier = "junit" + RandomUtils.nextInt();
084: validPCDOFileContents = ClassLoader
085: .getSystemResourceAsStream(TEST_BATCH_XML_DIRECTORY
086: + "BatchInputValidPCDO.xml");
087: validCollectorFileContents = ClassLoader
088: .getSystemResourceAsStream(TEST_BATCH_XML_DIRECTORY
089: + "BatchInputValidCollector.xml");
090:
091: validWorkgroupUser = SpringContext.getBean(
092: UniversalUserService.class)
093: .getUniversalUserByAuthenticationUserId(Data4.USER_ID2);
094: invalidWorkgroupUser = SpringContext.getBean(
095: UniversalUserService.class)
096: .getUniversalUserByAuthenticationUserId(Data4.USER_ID1);
097:
098: createdTestFiles = new ArrayList();
099: }
100:
101: /**
102: * Clean up any files created during test methods that were not removed (possibly because of a failure).
103: *
104: * @see junit.framework.TestCase#tearDown()
105: */
106: @Override
107: protected void tearDown() throws Exception {
108: super .tearDown();
109:
110: if (createdTestFiles != null) {
111: for (File createdFile : createdTestFiles) {
112: if (createdFile.exists()) {
113: createdFile.delete();
114: }
115: String doneFileName = StringUtils.substringBeforeLast(
116: createdFile.getPath(), ".")
117: + ".done";
118: File doneFile = new File(doneFileName);
119: if (doneFile.exists()) {
120: doneFile.delete();
121: }
122: }
123: }
124: }
125:
126: /**
127: * Checks file was created succesfully for valid call to save method.
128: */
129: public final void testSave_valid() throws Exception {
130: String savedFileName = batchInputFileService.save(
131: validWorkgroupUser, pcdoBatchInputFileType,
132: testFileIdentifier, validPCDOFileContents,
133: new ArrayList());
134:
135: File expectedFile = new File(savedFileName);
136: createdTestFiles.add(expectedFile);
137:
138: assertTrue("uploaded pcdo file not found", expectedFile
139: .exists());
140: assertTrue("uploaded pcdo file is empty",
141: expectedFile.length() > 0);
142:
143: checkForDoneFile(expectedFile);
144:
145: // remove file so we can test collector upload
146: expectedFile.delete();
147:
148: savedFileName = batchInputFileService.save(validWorkgroupUser,
149: collectorBatchInputFileType, testFileIdentifier,
150: validCollectorFileContents, new MockCollectorBatch());
151:
152: expectedFile = new File(savedFileName);
153: createdTestFiles.add(expectedFile);
154:
155: assertTrue("uploaded collector file not found", expectedFile
156: .exists());
157: assertTrue("uploaded collector file is empty", expectedFile
158: .length() > 0);
159:
160: checkForDoneFile(expectedFile);
161: }
162:
163: /**
164: * Checks for a done file with the same name as the given batch file.
165: */
166: private final void checkForDoneFile(File batchFile) {
167: String doneFileName = StringUtils.substringBeforeLast(batchFile
168: .getPath(), ".")
169: + ".done";
170: File doneFile = new File(doneFileName);
171:
172: assertTrue("done file " + doneFile.getPath()
173: + " does not exist", doneFile.exists());
174: }
175:
176: /**
177: * Assures AuthorizationException is being thrown when the user does not have permissions on the given batch input type.
178: */
179: public final void testSave_incorrectUserPermissions()
180: throws Exception {
181: boolean failedAsExpected = false;
182: try {
183: batchInputFileService.save(invalidWorkgroupUser,
184: pcdoBatchInputFileType, testFileIdentifier,
185: validPCDOFileContents, new ArrayList());
186: } catch (AuthorizationException e) {
187: failedAsExpected = true;
188: }
189:
190: assertTrue(
191: "authorization exception not thrown for user with invalid permissions on pcdo batch type",
192: failedAsExpected);
193:
194: failedAsExpected = false;
195: try {
196: batchInputFileService.save(invalidWorkgroupUser,
197: collectorBatchInputFileType, testFileIdentifier,
198: validCollectorFileContents,
199: new MockCollectorBatch());
200: } catch (AuthorizationException e) {
201: failedAsExpected = true;
202: }
203:
204: assertTrue(
205: "authorization exception not thrown for user with invalid permissions on collector batch type",
206: failedAsExpected);
207: }
208:
209: /**
210: * Assures FileStorageException is thrown when a the requested file name is already used by another file on the server.
211: */
212: public final void DISABLED_testSave_saveFileNameExists()
213: throws Exception {
214: String savedFileName = batchInputFileService.save(
215: validWorkgroupUser, pcdoBatchInputFileType,
216: testFileIdentifier, validPCDOFileContents,
217: new ArrayList());
218:
219: File expectedFile = new File(savedFileName);
220: createdTestFiles.add(expectedFile);
221:
222: // assure first attempt was completed successfully
223: assertTrue("uploaded pcdo file not found", expectedFile
224: .exists());
225: long lastModifiedTime = expectedFile.lastModified();
226:
227: // no attemp to a file with the same name
228: boolean failedAsExpected = false;
229: try {
230: batchInputFileService.save(validWorkgroupUser,
231: pcdoBatchInputFileType, testFileIdentifier,
232: validPCDOFileContents, new ArrayList());
233: } catch (FileStorageException e) {
234: failedAsExpected = true;
235: }
236:
237: assertTrue(
238: "exception not thrown for attempt to save a file with existing name",
239: failedAsExpected);
240: assertEquals(
241: "file was modified even though storage exception was thrown",
242: lastModifiedTime, expectedFile.lastModified());
243: }
244:
245: /**
246: * Checks existing file was removed by delete method.
247: */
248: public final void testDelete_valid() throws Exception {
249: checkDelete_valid(pcdoBatchInputFileType,
250: validPCDOFileContents, new ArrayList());
251: checkDelete_valid(collectorBatchInputFileType,
252: validCollectorFileContents, new MockCollectorBatch());
253: }
254:
255: private final void checkDelete_valid(
256: BatchInputFileType batchInputFileType,
257: InputStream fileContents, Object parsedObject)
258: throws Exception {
259: String savedFileName = batchInputFileService.save(
260: validWorkgroupUser, batchInputFileType,
261: testFileIdentifier, fileContents, parsedObject);
262:
263: File expectedFile = new File(savedFileName);
264: createdTestFiles.add(expectedFile);
265: assertTrue("uploaded "
266: + batchInputFileType.getFileTypeIdentifer()
267: + " file not found", expectedFile.exists());
268:
269: String fileNameOnly = expectedFile.getName();
270: batchInputFileService.delete(validWorkgroupUser,
271: batchInputFileType, fileNameOnly);
272: assertFalse("file still exists", expectedFile.exists());
273:
274: String doneFileName = StringUtils.substringBeforeLast(
275: expectedFile.getPath(), ".")
276: + ".done";
277: File doneFile = new File(doneFileName);
278: assertFalse("done file " + doneFileName + " not removed",
279: doneFile.exists());
280: }
281:
282: /**
283: * Assures AuthorizationException is thrown when the user does not have permissions to delete a file with the given batch input
284: * type.
285: */
286: public final void testDelete_incorrectUserPermissions()
287: throws Exception {
288: checkDelete_incorrectUserPermissions(pcdoBatchInputFileType,
289: validPCDOFileContents, new ArrayList());
290: checkDelete_incorrectUserPermissions(
291: collectorBatchInputFileType,
292: validCollectorFileContents, new MockCollectorBatch());
293: }
294:
295: private final void checkDelete_incorrectUserPermissions(
296: BatchInputFileType batchInputFileType,
297: InputStream fileContents, Object parsedObject)
298: throws Exception {
299: String savedFileName = batchInputFileService.save(
300: validWorkgroupUser, batchInputFileType,
301: testFileIdentifier, fileContents, parsedObject);
302: createdTestFiles.add(new File(savedFileName));
303:
304: boolean failedAsExpected = false;
305: try {
306: batchInputFileService.delete(invalidWorkgroupUser,
307: batchInputFileType, testFileIdentifier);
308: } catch (AuthorizationException e) {
309: failedAsExpected = true;
310: }
311:
312: assertTrue(
313: "authorization exception not thrown for user with invalid permissions on "
314: + batchInputFileType.getFileTypeIdentifer()
315: + " batch type", failedAsExpected);
316: }
317:
318: /**
319: * Assures FileNotFound exception is thrown when an invalid file name to delete is given.
320: */
321: public final void testDelete_fileNotFound() throws Exception {
322: checkDelete_fileNotFound(pcdoBatchInputFileType,
323: validPCDOFileContents);
324: checkDelete_fileNotFound(collectorBatchInputFileType,
325: validCollectorFileContents);
326: }
327:
328: private final void checkDelete_fileNotFound(
329: BatchInputFileType batchInputFileType,
330: InputStream fileContents) throws Exception {
331: boolean failedAsExpected = false;
332: try {
333: batchInputFileService.delete(validWorkgroupUser,
334: batchInputFileType, "foo.xml");
335: } catch (FileNotFoundException e) {
336: failedAsExpected = true;
337: }
338:
339: assertTrue(
340: "FileNotFound exception not thrown for user invalid file name on "
341: + batchInputFileType.getFileTypeIdentifer()
342: + " batch type", failedAsExpected);
343: }
344:
345: }
|