1 | package eu.clarin.cmdi.vlo.importer; |
---|
2 | |
---|
3 | import static org.junit.Assert.assertEquals; |
---|
4 | |
---|
5 | import java.io.File; |
---|
6 | import java.io.IOException; |
---|
7 | import java.net.MalformedURLException; |
---|
8 | import java.util.ArrayList; |
---|
9 | import java.util.Collections; |
---|
10 | import java.util.List; |
---|
11 | |
---|
12 | import org.apache.solr.client.solrj.SolrServerException; |
---|
13 | import org.apache.solr.common.SolrInputDocument; |
---|
14 | import org.junit.Test; |
---|
15 | |
---|
16 | import eu.clarin.cmdi.vlo.FacetConstants; |
---|
17 | |
---|
18 | public class MetadataImporterTest extends ImporterTestcase { |
---|
19 | |
---|
20 | @Test |
---|
21 | public void testImporterSimple() throws Exception { |
---|
22 | String session = ""; |
---|
23 | session += "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"; |
---|
24 | session += "<CMD>\n"; |
---|
25 | session += " <Header>\n"; |
---|
26 | session += " <MdCreationDate>2008-05-27</MdCreationDate>\n"; |
---|
27 | session += " <MdSelfLink>testID1Session</MdSelfLink>\n"; |
---|
28 | session += " <MdProfile>clarin.eu:cr1:p_1271859438204</MdProfile>\n"; |
---|
29 | session += " </Header>\n"; |
---|
30 | session += " <Resources>\n"; |
---|
31 | session += " <ResourceProxyList>\n"; |
---|
32 | session += " <ResourceProxy id=\"d314e408\">\n"; |
---|
33 | session += " <ResourceType mimetype=\"video/x-mpeg1\" >Resource</ResourceType>\n"; |
---|
34 | session += " <ResourceRef>../Media/elan-example1.mpg</ResourceRef>\n"; |
---|
35 | session += " </ResourceProxy>\n"; |
---|
36 | session += " </ResourceProxyList>\n"; |
---|
37 | session += " </Resources>\n"; |
---|
38 | session += " <Components>\n"; |
---|
39 | session += " <Session>\n"; |
---|
40 | session += " <Name>kleve-route</Name>\n"; |
---|
41 | session += " </Session>\n"; |
---|
42 | session += " </Components>\n"; |
---|
43 | session += "</CMD>\n"; |
---|
44 | File sessionFile = createCmdiFile("testSession", session); |
---|
45 | |
---|
46 | String content = ""; |
---|
47 | content += "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"; |
---|
48 | content += "<CMD>\n"; |
---|
49 | content += " <Header>\n"; |
---|
50 | content += " <MdSelfLink>testID2</MdSelfLink>\n"; |
---|
51 | content += " </Header>\n"; |
---|
52 | content += " <Resources>\n"; |
---|
53 | content += " <ResourceProxyList>\n"; |
---|
54 | content += " <ResourceProxy id=\"d28635e19\">\n"; |
---|
55 | content += " <ResourceType>Metadata</ResourceType>\n"; |
---|
56 | content += " <ResourceRef>" + sessionFile.getName() + "</ResourceRef>\n"; |
---|
57 | content += " </ResourceProxy>\n"; |
---|
58 | content += " </ResourceProxyList>\n"; |
---|
59 | content += " </Resources>\n"; |
---|
60 | content += " <Components>\n"; |
---|
61 | content += " <imdi-corpus>\n"; |
---|
62 | content += " <Corpus>\n"; |
---|
63 | content += " <Name>MPI corpora</Name>\n"; |
---|
64 | content += " </Corpus>\n"; |
---|
65 | content += " </imdi-corpus>\n"; |
---|
66 | content += " </Components>\n"; |
---|
67 | content += "</CMD>\n"; |
---|
68 | File rootFile = createCmdiFile("rootFile", content); |
---|
69 | |
---|
70 | List<SolrInputDocument> docs = importData(rootFile); |
---|
71 | assertEquals(1, docs.size()); |
---|
72 | SolrInputDocument doc = docs.get(0); |
---|
73 | assertEquals("testID1Session", getValue(doc, FacetConstants.FIELD_ID)); |
---|
74 | assertEquals("testRoot", getValue(doc, FacetConstants.FIELD_ORIGIN)); //TODO PD make _dataRoot and origin facet, make all none showable fields start with _ |
---|
75 | assertEquals("kleve-route", getValue(doc, FacetConstants.FIELD_NAME)); |
---|
76 | assertEquals(sessionFile.getAbsolutePath(), getValue(doc, FacetConstants.FIELD_FILENAME)); |
---|
77 | assertEquals("video", getValue(doc, FacetConstants.FIELD_RESOURCE_TYPE)); |
---|
78 | assertEquals("video/x-mpeg1,../Media/elan-example1.mpg", getValue(doc, FacetConstants.FIELD_RESOURCE)); |
---|
79 | } |
---|
80 | |
---|
81 | private Object getValue(SolrInputDocument doc, String field) { |
---|
82 | assertEquals(1, doc.getFieldValues(field).size()); |
---|
83 | return doc.getFieldValue(field); |
---|
84 | } |
---|
85 | |
---|
86 | private List<SolrInputDocument> importData(File rootFile) throws MalformedURLException { |
---|
87 | final List<SolrInputDocument> result = new ArrayList<SolrInputDocument>(); |
---|
88 | ImporterConfig config = createConfig(rootFile); |
---|
89 | MetadataImporter importer = new MetadataImporter(config) { |
---|
90 | @Override |
---|
91 | protected void initSolrServer() throws MalformedURLException { |
---|
92 | //do nothing no solrserver in test |
---|
93 | } |
---|
94 | |
---|
95 | @Override |
---|
96 | protected void sendDocs() throws SolrServerException, IOException { |
---|
97 | //overriding here so we can test the docs |
---|
98 | result.addAll(this.docs); |
---|
99 | docs = new ArrayList<SolrInputDocument>(); |
---|
100 | } |
---|
101 | }; |
---|
102 | importer.startImport(); |
---|
103 | return result; |
---|
104 | } |
---|
105 | |
---|
106 | private ImporterConfig createConfig(File rootFile) { |
---|
107 | ImporterConfig config = new ImporterConfig(); |
---|
108 | FacetMapping facetMapping = getIMDIFacetMap(); |
---|
109 | DataRoot dataRoot = new DataRoot(); |
---|
110 | dataRoot.setFacetMapping(facetMapping); |
---|
111 | dataRoot.setDeleteFirst(false);//cannot delete not using real solrServer |
---|
112 | dataRoot.setOriginName("testRoot"); |
---|
113 | dataRoot.setRootFile(rootFile); |
---|
114 | config.setDataRoots(Collections.singletonList(dataRoot)); |
---|
115 | return config; |
---|
116 | } |
---|
117 | |
---|
118 | } |
---|