1
2
3
4
5
6
7
8
9
10
11
12
13
14
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.Test;
21 import org.kuali.rice.core.api.CoreApiServiceLocator;
22 import org.kuali.rice.core.api.impex.xml.FileXmlDocCollection;
23 import org.kuali.rice.core.api.impex.xml.XmlDocCollection;
24 import org.kuali.rice.core.api.util.ClasspathOrFileResourceLoader;
25 import org.kuali.rice.edl.impl.bo.EDocLiteAssociation;
26 import org.kuali.rice.edl.impl.service.EdlServiceLocator;
27 import org.kuali.rice.edl.impl.xml.export.EdlExportDataSet;
28 import org.kuali.rice.kew.test.KEWTestCase;
29 import org.springframework.core.io.Resource;
30 import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
31 import org.springframework.core.io.support.ResourcePatternResolver;
32 import org.springframework.util.FileCopyUtils;
33
34 import java.io.File;
35 import java.io.IOException;
36 import java.util.ArrayList;
37 import java.util.Collection;
38 import java.util.Iterator;
39 import java.util.LinkedList;
40 import java.util.List;
41 import java.util.Map;
42 import java.util.Properties;
43
44 import static org.junit.Assert.assertFalse;
45 import static org.junit.Assert.assertTrue;
46
47
48
49
50
51
52 public class XmlIngestionTest extends KEWTestCase {
53
54 private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(XmlIngestionTest.class);
55
56 private static final File TMP_DIR = new File(System.getProperty("java.io.tmpdir"), "XmlIngestionTest_dir");
57 private static final File PENDING_DIR = new File(TMP_DIR, "pending");
58 private static final File LOADED_DIR = new File(TMP_DIR, "loaded");
59 private static final File PROBLEM_DIR = new File(TMP_DIR, "problem");
60
61 public void setUp() throws Exception {
62 super.setUp();
63 deleteDirectories();
64 TMP_DIR.mkdirs();
65 PENDING_DIR.mkdirs();
66 LOADED_DIR.mkdirs();
67 PROBLEM_DIR.mkdirs();
68 }
69
70 private void deleteContentsOfDir(File dir, int depth) {
71 File[] files = dir.listFiles();
72 if (files == null) return;
73 for (File file : files) {
74 if (file.isDirectory() && depth > 0) {
75
76
77
78 deleteContentsOfDir(file, depth - 1);
79 }
80 boolean success = file.delete();
81 LOG.info("deleting: " + file + "..." + (success ? "succeeded" : "failed"));
82 }
83 }
84
85 public void tearDown() throws Exception {
86 try {
87 deleteDirectories();
88 } finally {
89 super.tearDown();
90 }
91 }
92
93 protected void deleteDirectories() {
94 deleteContentsOfDir(PENDING_DIR, 0);
95 deleteContentsOfDir(LOADED_DIR, 2);
96 deleteContentsOfDir(PROBLEM_DIR, 2);
97 deleteContentsOfDir(TMP_DIR, 0);
98 TMP_DIR.delete();
99 }
100
101 protected boolean verifyFileExists(File dir, File file) throws IOException {
102 ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
103 Resource[] resources = resolver.getResources(dir.toURL() + "/**/" + file.getName());
104 if (resources == null) {
105 return false;
106 }
107 for (int i = 0; i < resources.length; i++) {
108 if (resources[i].exists()) {
109 return true;
110 }
111 }
112 return false;
113 }
114
115
116 public void testXmlReIngestion() throws Exception {
117
118
119 String filePath = "org/kuali/rice/kew/batch/data/widgetsTest.xml";
120 File ingestedFile = new ClasspathOrFileResourceLoader().getResource(filePath).getFile();
121 List<XmlDocCollection> collections = new ArrayList<XmlDocCollection>();
122 XmlDocCollection fileDoc = new FileXmlDocCollection(ingestedFile);
123 collections.add(fileDoc);
124
125 Collection<XmlDocCollection> ingestedXmlFile = null;
126 try {
127 ingestedXmlFile = CoreApiServiceLocator.getXmlIngesterService().ingest(collections);
128 } catch (Exception e) {
129 LOG.error("Error ingesting data", e);
130
131 }
132
133 EdlExportDataSet dataSet = new EdlExportDataSet();
134
135
136 List<EDocLiteAssociation> edla = EdlServiceLocator.getEDocLiteService().getEDocLiteAssociations();
137 String style = null;
138 for (EDocLiteAssociation edl : edla) {
139 if (edl != null) {
140 style = edl.getStyle();
141 if ("widgetsTest".equals(style)) {
142 dataSet.getEdocLites().add(edl);
143 }
144 }
145 }
146
147 byte[] xmlBytes = CoreApiServiceLocator.getXmlExporterService().export(dataSet.createExportDataSet());
148
149 File reingestFile = File.createTempFile("widgetsTestOutput", ".xml");
150 FileUtils.writeByteArrayToFile(reingestFile, xmlBytes);
151 String ingestedString = FileUtils.readFileToString(ingestedFile);
152 String reingestedString = FileUtils.readFileToString(reingestFile);
153
154 }
155
156
157
158
159
160
161
162
163
164 @Test
165 public void testXmlIngestion() throws IOException {
166 XmlPollerServiceImpl poller = new XmlPollerServiceImpl();
167 poller.setPollIntervalSecs(1);
168 poller.setXmlParentDirectory(TMP_DIR.toString());
169 poller.setXmlPendingLocation(PENDING_DIR.toString());
170 poller.setXmlCompletedLocation(LOADED_DIR.toString());
171 poller.setXmlProblemLocation(PROBLEM_DIR.toString());
172
173 Properties filesToIngest = new Properties();
174 filesToIngest.load(getClass().getResourceAsStream("XmlIngestionTest.txt"));
175 List<File> pendingFiles = new LinkedList<File>();
176 List<File> shouldPass = new LinkedList<File>();
177 List<File> shouldFail = new LinkedList<File>();
178 Iterator<Map.Entry<Object, Object>> entries = filesToIngest.entrySet().iterator();
179 int i = 0;
180 while (entries.hasNext()) {
181 Map.Entry<?, ?> entry = entries.next();
182 String filePath = entry.getKey().toString();
183 File testFile = new ClasspathOrFileResourceLoader().getResource(filePath).getFile();
184 File pendingDir = new File(PENDING_DIR + "/TestDoc-" + i);
185 Assert.assertTrue(pendingDir.mkdirs());
186 assertTrue(pendingDir.isDirectory());
187 File pending = new File(pendingDir, testFile.getName());
188 pendingFiles.add(pending);
189 if (Boolean.valueOf(entry.getValue().toString())) {
190 shouldPass.add(pending);
191 } else {
192 shouldFail.add(pending);
193 }
194 FileCopyUtils.copy(testFile, pending);
195 LOG.info("created: " + pending);
196 i++;
197 }
198
199
200 poller.run();
201
202
203 Iterator<File> it = pendingFiles.iterator();
204 while (it.hasNext()) {
205 File pending = it.next();
206 assertTrue(!pending.isFile());
207 }
208
209
210
211
212 it = shouldPass.iterator();
213 while (it.hasNext()) {
214 File file = it.next();
215 assertTrue("Loaded file " + file + " was not moved to loaded directory " + LOADED_DIR, verifyFileExists(LOADED_DIR, file));
216 assertFalse("Loaded file " + file + " was moved to problem directory " + PROBLEM_DIR, verifyFileExists(PROBLEM_DIR, file));
217 }
218
219 it = shouldFail.iterator();
220 while (it.hasNext()) {
221 File file = it.next();
222 assertTrue("Problem file " + file + " was not moved to problem directory" + PROBLEM_DIR, verifyFileExists(PROBLEM_DIR, file));
223 assertFalse("Problem file " + file + " was moved to loaded directory" + LOADED_DIR, verifyFileExists(LOADED_DIR, file));
224 }
225 }
226 }