View Javadoc

1   /**
2    * Copyright 2005-2013 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.rice.kew.batch;
17  
18  import org.apache.commons.io.FileUtils;
19  import org.junit.Assert;
20  import org.junit.Ignore;
21  import org.junit.Test;
22  import org.kuali.rice.core.api.CoreApiServiceLocator;
23  import org.kuali.rice.core.api.impex.xml.FileXmlDocCollection;
24  import org.kuali.rice.core.api.impex.xml.XmlDocCollection;
25  import org.kuali.rice.core.api.util.ClasspathOrFileResourceLoader;
26  import org.kuali.rice.edl.impl.bo.EDocLiteAssociation;
27  import org.kuali.rice.edl.impl.service.EdlServiceLocator;
28  import org.kuali.rice.edl.impl.xml.export.EdlExportDataSet;
29  import org.kuali.rice.kew.test.KEWTestCase;
30  import org.springframework.core.io.Resource;
31  import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
32  import org.springframework.core.io.support.ResourcePatternResolver;
33  import org.springframework.util.FileCopyUtils;
34  
35  import java.io.File;
36  import java.io.IOException;
37  import java.util.ArrayList;
38  import java.util.Collection;
39  import java.util.Iterator;
40  import java.util.LinkedList;
41  import java.util.List;
42  import java.util.Map;
43  import java.util.Properties;
44  
45  import static org.junit.Assert.assertFalse;
46  import static org.junit.Assert.assertTrue;
47  
48  /**
49   * Tests XML "ingestion" pipeline
50   *
51   * @author Kuali Rice Team (rice.collab@kuali.org)
52   */
53  public class XmlIngestionTest extends KEWTestCase {
54  
55      private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(XmlIngestionTest.class);
56  
57      private static final File TMP_DIR = new File(System.getProperty("java.io.tmpdir"), "XmlIngestionTest_dir");
58      private static final File PENDING_DIR = new File(TMP_DIR, "pending");
59      private static final File LOADED_DIR = new File(TMP_DIR, "loaded");
60      private static final File PROBLEM_DIR = new File(TMP_DIR, "problem");
61  
62      public void setUp() throws Exception {
63          super.setUp();
64          deleteDirectories();
65          TMP_DIR.mkdirs();
66          PENDING_DIR.mkdirs();
67          LOADED_DIR.mkdirs();
68          PROBLEM_DIR.mkdirs();
69      }
70  
71      private void deleteContentsOfDir(File dir, int depth) {
72          File[] files = dir.listFiles();
73          if (files == null) return;
74          for (File file : files) {
75              if (file.isDirectory() && depth > 0) {
76                  // decrement depth
77                  // to avoid the possibility of inadvertent
78                  // recursive delete!
79                  deleteContentsOfDir(file, depth - 1);
80              }
81              boolean success = file.delete();
82              LOG.info("deleting: " + file + "..." + (success ? "succeeded" : "failed"));
83          }
84      }
85  
86      public void tearDown() throws Exception {
87          try {
88              deleteDirectories();
89          } finally {
90              super.tearDown();
91          }
92      }
93  
94      protected void deleteDirectories() {
95          deleteContentsOfDir(PENDING_DIR, 0);
96          deleteContentsOfDir(LOADED_DIR, 2);
97          deleteContentsOfDir(PROBLEM_DIR, 2);
98          deleteContentsOfDir(TMP_DIR, 0);
99          TMP_DIR.delete();
100     }
101 
102     protected boolean verifyFileExists(File dir, File file) throws IOException {
103         ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
104         Resource[] resources = resolver.getResources(dir.toURL() + "/**/" + file.getName());
105         if (resources == null) {
106             return false;
107         }
108         for (int i = 0; i < resources.length; i++) {
109             if (resources[i].exists()) {
110                 return true;
111             }
112         }
113         return false;
114     }
115     
116 
117     @Ignore
118     public void testXmlReIngestion() throws Exception {
119 
120         // Define the path for the test environment
121         String filePath = "org/kuali/rice/kew/batch/data/widgetsTest.xml";
122         File ingestedFile = new ClasspathOrFileResourceLoader().getResource(filePath).getFile();
123         List<XmlDocCollection> collections = new ArrayList<XmlDocCollection>();
124         XmlDocCollection fileDoc = new FileXmlDocCollection(ingestedFile);
125         collections.add(fileDoc);
126         // ingest the collection and save it to the database
127         Collection<XmlDocCollection> ingestedXmlFile = null;
128         try {
129             ingestedXmlFile = CoreApiServiceLocator.getXmlIngesterService().ingest(collections);
130         } catch (Exception e) {
131             LOG.error("Error ingesting data", e);
132             //throw new RuntimeException(e);
133         }
134 
135         EdlExportDataSet dataSet = new EdlExportDataSet();
136 
137         //Cast this for now
138         List<EDocLiteAssociation> edla = EdlServiceLocator.getEDocLiteService().getEDocLiteAssociations();     
139         String style = null;
140         for (EDocLiteAssociation edl : edla) {
141             if (edl != null) {
142                 style = edl.getStyle();
143                 if ("widgetsTest".equals(style)) {
144                     dataSet.getEdocLites().add(edl);
145                 }
146             }
147         }
148         
149         byte[] xmlBytes = CoreApiServiceLocator.getXmlExporterService().export(dataSet.createExportDataSet());
150         // now export that xml into a file
151         File reingestFile = File.createTempFile("widgetsTestOutput", ".xml");		
152         FileUtils.writeByteArrayToFile(reingestFile, xmlBytes);
153         String ingestedString = FileUtils.readFileToString(ingestedFile);
154         String reingestedString = FileUtils.readFileToString(reingestFile);
155         //assertTrue(FileUtils.contentEquals(ingestedFile, reingestFile));
156     }
157 
158 
159     /**
160      * TODO: beef this up
161      * need a reliable way to test if the file arrived in the right date-stamped
162      * subdirectory (maybe just pick the last, or first directory?)
163      *
164      * @throws java.io.IOException
165      */
166     @Test
167     public void testXmlIngestion() throws IOException {
168         XmlPollerServiceImpl poller = new XmlPollerServiceImpl();
169         poller.setPollIntervalSecs(1);
170         poller.setXmlParentDirectory(TMP_DIR.toString());
171         poller.setXmlPendingLocation(PENDING_DIR.toString());
172         poller.setXmlCompletedLocation(LOADED_DIR.toString());
173         poller.setXmlProblemLocation(PROBLEM_DIR.toString());
174 
175         Properties filesToIngest = new Properties();
176         filesToIngest.load(getClass().getResourceAsStream("XmlIngestionTest.txt"));
177         List<File> pendingFiles = new LinkedList<File>();
178         List<File> shouldPass = new LinkedList<File>();
179         List<File> shouldFail = new LinkedList<File>();
180         Iterator<Map.Entry<Object, Object>> entries = filesToIngest.entrySet().iterator();
181         int i = 0;
182         while (entries.hasNext()) {
183             Map.Entry<?, ?> entry = entries.next();
184             String filePath = entry.getKey().toString();
185             File testFile = new ClasspathOrFileResourceLoader().getResource(filePath).getFile();
186             File pendingDir = new File(PENDING_DIR + "/TestDoc-" + i);
187             Assert.assertTrue(pendingDir.mkdirs());
188             assertTrue(pendingDir.isDirectory());
189             File pending = new File(pendingDir, testFile.getName());
190             pendingFiles.add(pending);
191             if (Boolean.valueOf(entry.getValue().toString())) {
192                 shouldPass.add(pending);
193             } else {
194                 shouldFail.add(pending);
195             }
196             FileCopyUtils.copy(testFile, pending);
197             LOG.info("created: " + pending);
198             i++;
199         }
200 
201         // poller should not throw exceptions
202         poller.run();
203 
204         // check that all files have been processed
205         Iterator<File> it = pendingFiles.iterator();
206         while (it.hasNext()) {
207             File pending = it.next();
208             assertTrue(!pending.isFile());
209         }
210 
211         // check that they landed in the appropriate location
212 
213         // loaded files should be in the loaded dir...
214         it = shouldPass.iterator();
215         while (it.hasNext()) {
216             File file = it.next();
217             assertTrue("Loaded file " + file + " was not moved to loaded directory " + LOADED_DIR, verifyFileExists(LOADED_DIR, file));
218             assertFalse("Loaded file " + file + " was moved to problem directory " + PROBLEM_DIR, verifyFileExists(PROBLEM_DIR, file));
219         }
220         // and problem files should be in the problem dir...
221         it = shouldFail.iterator();
222         while (it.hasNext()) {
223             File file = it.next();
224             assertTrue("Problem file " + file + " was not moved to problem directory" + PROBLEM_DIR, verifyFileExists(PROBLEM_DIR, file));
225             assertFalse("Problem file " + file + " was moved to loaded directory" + LOADED_DIR, verifyFileExists(LOADED_DIR, file));
226         }
227     }
228 }