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.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.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 @Ignore
117 public void testXmlReIngestion() throws Exception {
118
119
120 String relativeFolder = "/src/test/resources/org/kuali/rice/kew/batch/data/";
121 String filePath = getBaseDir() + relativeFolder + "widgetsTest.xml";
122 File ingestedFile = new File(filePath);
123 List<XmlDocCollection> collections = new ArrayList<XmlDocCollection>();
124 XmlDocCollection fileDoc = new FileXmlDocCollection(ingestedFile);
125 collections.add(fileDoc);
126
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
133 }
134
135 EdlExportDataSet dataSet = new EdlExportDataSet();
136
137
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
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
156 }
157
158
159
160
161
162
163
164
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 filePath = filePath.replace("${basedir}", getBaseDir());
186 File testFile = new File(filePath);
187 File pendingDir = new File(PENDING_DIR + "/TestDoc-" + i);
188 Assert.assertTrue(pendingDir.mkdirs());
189 assertTrue(pendingDir.isDirectory());
190 File pending = new File(pendingDir, testFile.getName());
191 pendingFiles.add(pending);
192 if (Boolean.valueOf(entry.getValue().toString())) {
193 shouldPass.add(pending);
194 } else {
195 shouldFail.add(pending);
196 }
197 FileCopyUtils.copy(testFile, pending);
198 LOG.info("created: " + pending);
199 i++;
200 }
201
202
203 poller.run();
204
205
206 Iterator<File> it = pendingFiles.iterator();
207 while (it.hasNext()) {
208 File pending = it.next();
209 assertTrue(!pending.isFile());
210 }
211
212
213
214
215 it = shouldPass.iterator();
216 while (it.hasNext()) {
217 File file = it.next();
218 assertTrue("Loaded file " + file + " was not moved to loaded directory " + LOADED_DIR, verifyFileExists(LOADED_DIR, file));
219 assertFalse("Loaded file " + file + " was moved to problem directory " + PROBLEM_DIR, verifyFileExists(PROBLEM_DIR, file));
220 }
221
222 it = shouldFail.iterator();
223 while (it.hasNext()) {
224 File file = it.next();
225 assertTrue("Problem file " + file + " was not moved to problem directory" + PROBLEM_DIR, verifyFileExists(PROBLEM_DIR, file));
226 assertFalse("Problem file " + file + " was moved to loaded directory" + LOADED_DIR, verifyFileExists(LOADED_DIR, file));
227 }
228 }
229 }