1 | package eu.clarin.cmdi.vlo.importer; |
---|
2 | |
---|
3 | import eu.clarin.cmdi.vlo.FacetConstants; |
---|
4 | import eu.clarin.cmdi.vlo.config.DataRoot; |
---|
5 | import java.io.File; |
---|
6 | import java.io.IOException; |
---|
7 | import java.net.MalformedURLException; |
---|
8 | import java.util.ArrayList; |
---|
9 | import java.util.Collection; |
---|
10 | import java.util.Collections; |
---|
11 | import java.util.List; |
---|
12 | import java.util.logging.Level; |
---|
13 | import java.util.logging.Logger; |
---|
14 | import org.apache.solr.client.solrj.SolrServerException; |
---|
15 | import org.apache.solr.common.SolrInputDocument; |
---|
16 | import static org.junit.Assert.assertEquals; |
---|
17 | import org.junit.Test; |
---|
18 | |
---|
19 | public class MetadataImporterTest extends ImporterTestcase { |
---|
20 | |
---|
21 | @Test |
---|
22 | public void classPath(){ |
---|
23 | ResourceList.printClassPath(); |
---|
24 | } |
---|
25 | |
---|
26 | @Test |
---|
27 | public void testImporterSimple() throws Exception { |
---|
28 | String session = ""; |
---|
29 | session += "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"; |
---|
30 | session += "<CMD xmlns=\"http://www.clarin.eu/cmd/\">\n"; |
---|
31 | session += " <Header>\n"; |
---|
32 | session += " <MdCreationDate>2008-05-27</MdCreationDate>\n"; |
---|
33 | session += " <MdSelfLink> testID1Session</MdSelfLink>\n"; |
---|
34 | session += " <MdCollectionDisplayName>CollectionName</MdCollectionDisplayName>\n"; |
---|
35 | session += " <MdProfile>clarin.eu:cr1:p_1271859438204</MdProfile>\n"; |
---|
36 | session += " </Header>\n"; |
---|
37 | session += " <Resources>\n"; |
---|
38 | session += " <ResourceProxyList>\n"; |
---|
39 | session += " <ResourceProxy id=\"d314e408\">\n"; |
---|
40 | session += " <ResourceType mimetype=\"video/x-mpeg1\" >Resource</ResourceType>\n"; |
---|
41 | session += " <ResourceRef>../Media/elan-example1.mpg</ResourceRef>\n"; |
---|
42 | session += " </ResourceProxy>\n"; |
---|
43 | session += " </ResourceProxyList>\n"; |
---|
44 | session += " </Resources>\n"; |
---|
45 | session += " <Components>\n"; |
---|
46 | session += " <Session>\n"; |
---|
47 | session += " <Name>kleve-route</Name>\n"; |
---|
48 | session += " <Title>kleve-route-title</Title>\n"; |
---|
49 | session += " </Session>\n"; |
---|
50 | session += " </Components>\n"; |
---|
51 | session += "</CMD>\n"; |
---|
52 | File sessionFile = createCmdiFile("testSession", session); |
---|
53 | |
---|
54 | List<SolrInputDocument> docs = importData(sessionFile); |
---|
55 | assertEquals(1, docs.size()); |
---|
56 | SolrInputDocument doc = docs.get(0); |
---|
57 | assertEquals("testID1Session", getValue(doc, FacetConstants.FIELD_ID)); |
---|
58 | assertEquals("CollectionName", getValue(doc, FacetConstants.FIELD_COLLECTION)); |
---|
59 | assertEquals("testRoot", getValue(doc, FacetConstants.FIELD_DATA_PROVIDER)); |
---|
60 | assertEquals("kleve-route", getValue(doc, FacetConstants.FIELD_NAME)); |
---|
61 | assertEquals(sessionFile.getAbsolutePath(), getValue(doc, FacetConstants.FIELD_FILENAME)); |
---|
62 | assertEquals("video/x-mpeg1", getValue(doc, FacetConstants.FIELD_FORMAT)); |
---|
63 | assertEquals("video/x-mpeg1|../Media/elan-example1.mpg", getValue(doc, FacetConstants.FIELD_RESOURCE)); |
---|
64 | } |
---|
65 | |
---|
66 | @Test |
---|
67 | public void testImportWithMimeTypeOverride() throws Exception { |
---|
68 | String content = ""; |
---|
69 | content += "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"; |
---|
70 | content += "<CMD xmlns=\"http://www.clarin.eu/cmd/\">\n"; |
---|
71 | content += " <Header>\n"; |
---|
72 | content += " <MdSelfLink>testID2</MdSelfLink>\n"; |
---|
73 | content += " <MdProfile>clarin.eu:cr1:p_1289827960126</MdProfile>\n"; |
---|
74 | content += " </Header>\n"; |
---|
75 | content += " <Resources>\n"; |
---|
76 | content += " <ResourceProxyList>\n"; |
---|
77 | content += " <ResourceProxy id=\"refLink\">\n"; |
---|
78 | content += " <ResourceType>Resource</ResourceType>\n"; |
---|
79 | content += " <ResourceRef>http://terminotica.upf.es/CREL/LIC01.htm</ResourceRef>\n"; |
---|
80 | content += " </ResourceProxy>\n"; |
---|
81 | content += " <ResourceProxy id=\"refLink2\">\n"; |
---|
82 | content += " <ResourceType>Resource</ResourceType>\n"; |
---|
83 | content += " <ResourceRef>file://bla.resource2.txt</ResourceRef>\n"; |
---|
84 | content += " </ResourceProxy>\n"; |
---|
85 | content += " </ResourceProxyList>\n"; |
---|
86 | content += " </Resources>\n"; |
---|
87 | content += " <Components>\n"; |
---|
88 | content += " <LrtInventoryResource>\n"; |
---|
89 | content += " <LrtCommon>\n"; |
---|
90 | content += " <ResourceName>PALIC</ResourceName>\n"; |
---|
91 | content += " <ResourceType>Application / Tool</ResourceType>\n"; |
---|
92 | content += " <ResourceType>Text</ResourceType>\n"; |
---|
93 | content += " </LrtCommon>\n"; |
---|
94 | content += " </LrtInventoryResource>\n"; |
---|
95 | content += " </Components>\n"; |
---|
96 | content += "</CMD>\n"; |
---|
97 | File rootFile = createCmdiFile("rootFile", content); |
---|
98 | |
---|
99 | List<SolrInputDocument> docs = importData(rootFile); |
---|
100 | assertEquals(1, docs.size()); |
---|
101 | SolrInputDocument doc = docs.get(0); |
---|
102 | assertEquals("PALIC", getValue(doc, FacetConstants.FIELD_NAME)); |
---|
103 | Collection<Object> fieldValues = doc.getFieldValues(FacetConstants.FIELD_RESOURCE_CLASS); |
---|
104 | assertEquals(2, fieldValues.size()); |
---|
105 | List<String> values = new ArrayList(fieldValues); |
---|
106 | Collections.sort(values); |
---|
107 | assertEquals("Application / Tool", values.get(0)); |
---|
108 | assertEquals("plainText", values.get(1)); |
---|
109 | fieldValues = doc.getFieldValues(FacetConstants.FIELD_RESOURCE); |
---|
110 | assertEquals(2, fieldValues.size()); |
---|
111 | values = new ArrayList(fieldValues); |
---|
112 | Collections.sort(values); |
---|
113 | assertEquals("unknown type|file://bla.resource2.txt", values.get(0)); |
---|
114 | assertEquals("unknown type|http://terminotica.upf.es/CREL/LIC01.htm", values.get(1)); |
---|
115 | } |
---|
116 | |
---|
117 | @Test |
---|
118 | public void testImportWithNameSpaceGalore() throws Exception { |
---|
119 | String content = ""; |
---|
120 | content += "<cmdi:CMD CMDVersion=\"1.1\" xmlns:cmdi=\"http://www.clarin.eu/cmd/\"\n"; |
---|
121 | content += " xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://www.clarin.eu/cmd/ http://catalog.clarin.eu/ds/ComponentRegistry/rest/registry/profiles/clarin.eu:cr1:p_1290431694629/xsd\">\n"; |
---|
122 | content += " <cmdi:Header/>\n"; |
---|
123 | content += " <cmdi:Resources>\n"; |
---|
124 | content += " <cmdi:ResourceProxyList>\n"; |
---|
125 | content += " <cmdi:ResourceProxy id=\"TEI\">\n"; |
---|
126 | content += " <cmdi:ResourceType>Resource</cmdi:ResourceType>\n"; |
---|
127 | content += " <cmdi:ResourceRef>http://hdl.handle.net/11858/00-175C-0000-0000-E180-8?urlappend=/TEI</cmdi:ResourceRef>\n"; |
---|
128 | content += " </cmdi:ResourceProxy>\n"; |
---|
129 | content += " </cmdi:ResourceProxyList>\n"; |
---|
130 | content += " <cmdi:JournalFileProxyList/>\n"; |
---|
131 | content += " <cmdi:ResourceRelationList/>\n"; |
---|
132 | content += " </cmdi:Resources>\n"; |
---|
133 | content += " <cmdi:Components>\n"; |
---|
134 | content += " <cmdi:EastRepublican ref=\"TEI\">\n"; |
---|
135 | content += " <cmdi:GeneralInformation>\n"; |
---|
136 | content += " <cmdi:Identifier>hdl:11858/00-175C-0000-0000-E180-8</cmdi:Identifier>\n"; |
---|
137 | content += " <cmdi:Title>L'Est R\u00e9publicain : \u00e9dition du 17 mai 1999</cmdi:Title>\n"; |
---|
138 | content += " </cmdi:GeneralInformation>\n"; |
---|
139 | content += " </cmdi:EastRepublican>\n"; |
---|
140 | content += " </cmdi:Components>\n"; |
---|
141 | content += "</cmdi:CMD>\n"; |
---|
142 | |
---|
143 | File rootFile = createCmdiFile("rootFile", content); |
---|
144 | |
---|
145 | List<SolrInputDocument> docs = importData(rootFile); |
---|
146 | assertEquals(1, docs.size()); |
---|
147 | SolrInputDocument doc = docs.get(0); |
---|
148 | assertEquals("hdl_58_11858_47_00-175C-0000-0000-E180-8", getValue(doc, FacetConstants.FIELD_ID)); |
---|
149 | assertEquals("L'Est R\u00e9publicain : \u00e9dition du 17 mai 1999", getValue(doc, FacetConstants.FIELD_NAME)); |
---|
150 | assertEquals("unknown type|http://hdl.handle.net/11858/00-175C-0000-0000-E180-8?urlappend=/TEI", getValue(doc, |
---|
151 | FacetConstants.FIELD_RESOURCE)); |
---|
152 | } |
---|
153 | |
---|
154 | @Test |
---|
155 | public void testNoIdTakeFileName() throws Exception { |
---|
156 | String session = ""; |
---|
157 | session += "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"; |
---|
158 | session += "<CMD xmlns=\"http://www.clarin.eu/cmd/\">\n"; |
---|
159 | session += " <Header>\n"; |
---|
160 | session += " <MdProfile>clarin.eu:cr1:p_1271859438204</MdProfile>\n"; |
---|
161 | session += " </Header>\n"; |
---|
162 | session += " <Resources>\n"; |
---|
163 | session += " </Resources>\n"; |
---|
164 | session += " <Components>\n"; |
---|
165 | session += " <Session>\n"; |
---|
166 | session += " <Name>kleve-route</Name>\n"; |
---|
167 | session += " <Title>kleve-route-title</Title>\n"; |
---|
168 | session += " </Session>\n"; |
---|
169 | session += " </Components>\n"; |
---|
170 | session += "</CMD>\n"; |
---|
171 | File sessionFile = createCmdiFile("testSession", session); |
---|
172 | |
---|
173 | List<SolrInputDocument> docs = importData(sessionFile); |
---|
174 | assertEquals(1, docs.size()); |
---|
175 | SolrInputDocument doc = docs.get(0); |
---|
176 | assertEquals("testRoot/" + sessionFile.getName(), getValue(doc, FacetConstants.FIELD_ID)); |
---|
177 | } |
---|
178 | |
---|
179 | @Test |
---|
180 | public void testProjectName() throws Exception { |
---|
181 | String content = ""; |
---|
182 | content += "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"; |
---|
183 | content += "<CMD xmlns=\"http://www.clarin.eu/cmd/\">\n"; |
---|
184 | content += " <Header>\n"; |
---|
185 | content += " <MdProfile>clarin.eu:cr1:p_1280305685235</MdProfile>\n"; |
---|
186 | content += " </Header>\n"; |
---|
187 | content += " <Resources>\n"; |
---|
188 | content += " </Resources>\n"; |
---|
189 | content += " <Components>\n"; |
---|
190 | content += " <DynaSAND>\n"; |
---|
191 | content += " <Collection>\n"; |
---|
192 | content += " <GeneralInfo>\n"; |
---|
193 | content += " <Name>DiDDD</Name>\n"; |
---|
194 | content += " <ID>id1234</ID>\n"; |
---|
195 | content += " </GeneralInfo>\n"; |
---|
196 | content += " <Project>\n"; |
---|
197 | content += " <Name>DiDDD-project</Name>\n"; |
---|
198 | content += " </Project>\n"; |
---|
199 | content += " </Collection>\n"; |
---|
200 | content += " </DynaSAND>\n"; |
---|
201 | content += " </Components>\n"; |
---|
202 | content += "</CMD>\n"; |
---|
203 | File sessionFile = createCmdiFile("testSession", content); |
---|
204 | |
---|
205 | List<SolrInputDocument> docs = importData(sessionFile); |
---|
206 | assertEquals(1, docs.size()); |
---|
207 | SolrInputDocument doc = docs.get(0); |
---|
208 | assertEquals("testRoot", getValue(doc, FacetConstants.FIELD_COLLECTION)); |
---|
209 | assertEquals("DiDDD-project", getValue(doc, FacetConstants.FIELD_PROJECT_NAME)); |
---|
210 | } |
---|
211 | |
---|
212 | private Object getValue(SolrInputDocument doc, String field) { |
---|
213 | assertEquals(1, doc.getFieldValues(field).size()); |
---|
214 | return doc.getFieldValue(field); |
---|
215 | } |
---|
216 | |
---|
217 | private List<SolrInputDocument> importData(File rootFile) throws MalformedURLException { |
---|
218 | final List<SolrInputDocument> result = new ArrayList<SolrInputDocument>(); |
---|
219 | |
---|
220 | /* |
---|
221 | * Read configuration in ImporterTestCase.setup and change the setup to |
---|
222 | * suit the test. |
---|
223 | */ |
---|
224 | |
---|
225 | modifyConfig(rootFile); |
---|
226 | |
---|
227 | MetadataImporter importer; |
---|
228 | importer = new MetadataImporter() { |
---|
229 | /* |
---|
230 | * Because in the test, the solr server is not assumed to be |
---|
231 | * available, override the importer's class startImport method by |
---|
232 | * leaving out interaction with server. |
---|
233 | * |
---|
234 | * By invoking the processCmdi method, the class being defined here |
---|
235 | * needs to anticipate on an exception possibly thrown by the |
---|
236 | * processCmdi method invoking the sendDocs method. Please note |
---|
237 | * however, that the latter method is overriden, and the actual |
---|
238 | * database is being replaced by an array of documents. |
---|
239 | */ |
---|
240 | @Override |
---|
241 | void startImport() throws MalformedURLException { |
---|
242 | |
---|
243 | // make sure the mapping file for testing is used |
---|
244 | config.setFacetConceptsFile(getTestFacetConceptFilePath()); |
---|
245 | |
---|
246 | List<DataRoot> dataRoots = checkDataRoots(); |
---|
247 | long start = System.currentTimeMillis(); |
---|
248 | try { |
---|
249 | |
---|
250 | for (DataRoot dataRoot : dataRoots) { |
---|
251 | LOG.info("Start of processing: " + |
---|
252 | dataRoot.getOriginName()); |
---|
253 | CMDIDataProcessor processor = new |
---|
254 | CMDIParserVTDXML(POST_PROCESSORS, true); |
---|
255 | List<File> files = |
---|
256 | getFilesFromDataRoot(dataRoot.getRootFile()).get(0); |
---|
257 | for (File file : files) { |
---|
258 | if (config.getMaxFileSize () > 0 |
---|
259 | && file.length() > |
---|
260 | config.getMaxFileSize()) { |
---|
261 | LOG.info("Skipping " + file.getAbsolutePath() + |
---|
262 | " because it is too large."); |
---|
263 | } else { |
---|
264 | LOG.debug("PROCESSING FILE: {}", file.getAbsolutePath()); |
---|
265 | /* |
---|
266 | * Anticipate on the solr exception that will |
---|
267 | * never by raised because sendDocs is overriden |
---|
268 | * in a suitable way. |
---|
269 | */ |
---|
270 | try { |
---|
271 | processCmdi(file, dataRoot, processor); |
---|
272 | } catch (SolrServerException ex) { |
---|
273 | Logger.getLogger(MetadataImporterTest.class.getName()).log(Level.SEVERE, null, ex); |
---|
274 | } |
---|
275 | } |
---|
276 | } |
---|
277 | if (!docs.isEmpty()) { |
---|
278 | sendDocs(); |
---|
279 | } |
---|
280 | LOG.info("End of processing: " + |
---|
281 | dataRoot.getOriginName()); |
---|
282 | } |
---|
283 | |
---|
284 | } catch (IOException e) { |
---|
285 | LOG.error("error updating files:\n", e); |
---|
286 | } finally { |
---|
287 | |
---|
288 | } |
---|
289 | long took = (System.currentTimeMillis() - start) / 1000; |
---|
290 | LOG.info("Found " + nrOfFilesWithoutId + |
---|
291 | " file(s) without an id. (id is generated based on fileName but that may not be unique)"); |
---|
292 | LOG.info("Found " + nrOfFilesWithError + |
---|
293 | " file(s) with errors."); |
---|
294 | LOG.info("Found " + nrOfIgnoredFiles |
---|
295 | + " file(s) that where ignored (files without resources or any link to a search service or landing page are ignored)."); |
---|
296 | LOG.info("Update of " + nrOFDocumentsSend + " took " + took + |
---|
297 | " secs. Total nr of files analyzed " + nrOfFilesAnalyzed); |
---|
298 | } |
---|
299 | |
---|
300 | /* |
---|
301 | * Replace the server's database by a document array |
---|
302 | */ |
---|
303 | @Override |
---|
304 | protected void sendDocs() throws IOException { |
---|
305 | |
---|
306 | result.addAll(this.docs); |
---|
307 | docs = new ArrayList<SolrInputDocument>(); |
---|
308 | } |
---|
309 | }; |
---|
310 | importer.startImport(); |
---|
311 | return result; |
---|
312 | } |
---|
313 | |
---|
314 | private void modifyConfig(File rootFile) { |
---|
315 | DataRoot dataRoot = new DataRoot(); |
---|
316 | dataRoot.setDeleteFirst(false); // cannot delete becanot using real solrServer |
---|
317 | dataRoot.setOriginName("testRoot"); |
---|
318 | dataRoot.setRootFile(rootFile); |
---|
319 | dataRoot.setTostrip(""); |
---|
320 | dataRoot.setPrefix("http://example.com"); |
---|
321 | config.setDataRoots(Collections.singletonList(dataRoot)); |
---|
322 | } |
---|
323 | |
---|
324 | } |
---|