1
|
package edu.ucsb.nceas.metacat.index;
|
2
|
|
3
|
import static org.junit.Assert.assertEquals;
|
4
|
import static org.junit.Assert.assertTrue;
|
5
|
|
6
|
import java.io.File;
|
7
|
import java.io.FileInputStream;
|
8
|
import java.io.InputStream;
|
9
|
import java.util.List;
|
10
|
|
11
|
|
12
|
|
13
|
import org.apache.solr.client.solrj.SolrServer;
|
14
|
import org.apache.solr.client.solrj.SolrServerException;
|
15
|
import org.apache.solr.client.solrj.response.QueryResponse;
|
16
|
import org.apache.solr.common.SolrDocument;
|
17
|
import org.apache.solr.common.SolrDocumentList;
|
18
|
import org.apache.solr.common.params.SolrParams;
|
19
|
import org.apache.solr.servlet.SolrRequestParsers;
|
20
|
import org.dataone.service.types.v1.SystemMetadata;
|
21
|
import org.dataone.service.util.TypeMarshaller;
|
22
|
import org.junit.Test;
|
23
|
|
24
|
public class SolrIndexTest {
|
25
|
|
26
|
private static final String SYSTEMMETAFILEPATH = "src/test/resources/eml-system-meta-example.xml";
|
27
|
private static final String EMLFILEPATH = "src/test/resources/eml-example.xml";
|
28
|
private static final String id = "urn:uuid:606a19dd-b531-4bf4-b5a5-6d06c3d39098";
|
29
|
|
30
|
/**
|
31
|
* Test building index for an insert.
|
32
|
*/
|
33
|
@Test
|
34
|
public void testInsert() throws Exception {
|
35
|
SolrIndex solrIndex = generateSolrIndex();
|
36
|
//InputStream systemInputStream = new FileInputStream(new File(SYSTEMMETAFILEPATH));
|
37
|
SystemMetadata systemMetadata = TypeMarshaller.unmarshalTypeFromFile(SystemMetadata.class, SYSTEMMETAFILEPATH);
|
38
|
InputStream emlInputStream = new FileInputStream(new File(EMLFILEPATH));
|
39
|
solrIndex.insert(id, systemMetadata, emlInputStream);
|
40
|
doQuery(solrIndex.getSolrServer());
|
41
|
}
|
42
|
|
43
|
/**
|
44
|
* Test building index for an insert.
|
45
|
*/
|
46
|
@Test
|
47
|
public void testDelete() throws Exception {
|
48
|
//SolrIndex solrIndex = generateSolrIndex();
|
49
|
//solrIndex.remove(id);
|
50
|
|
51
|
}
|
52
|
|
53
|
private SolrIndex generateSolrIndex() throws Exception {
|
54
|
ApplicationController controller = new ApplicationController();
|
55
|
List<SolrIndex> list = controller.getSolrIndexes();
|
56
|
SolrIndex[] solrIndexesarray = list.toArray(new SolrIndex[list.size()]);
|
57
|
SolrIndex index = solrIndexesarray[0];
|
58
|
return index;
|
59
|
}
|
60
|
|
61
|
/*
|
62
|
* Do query
|
63
|
*/
|
64
|
private void doQuery(SolrServer server)
|
65
|
throws SolrServerException {
|
66
|
StringBuffer request = new StringBuffer();
|
67
|
request.append("q=" + "*:*");
|
68
|
SolrParams solrParams = SolrRequestParsers.parseQueryString(request
|
69
|
.toString());
|
70
|
QueryResponse reponse = server.query(solrParams);
|
71
|
System.out.println(reponse.toString());
|
72
|
}
|
73
|
|
74
|
|
75
|
/*
|
76
|
* Print out the QueryResponse
|
77
|
*/
|
78
|
private void print(QueryResponse response) {
|
79
|
SolrDocumentList docs = response.getResults();
|
80
|
if (docs != null) {
|
81
|
System.out.println(docs.getNumFound() + " documents found, "
|
82
|
+ docs.size() + " returned : ");
|
83
|
for (int i = 0; i < docs.size(); i++) {
|
84
|
SolrDocument doc = docs.get(i);
|
85
|
System.out.println("\t" + doc.toString());
|
86
|
}
|
87
|
}
|
88
|
}
|
89
|
}
|
90
|
|