1
|
package edu.ucsb.nceas.metacat.index;
|
2
|
|
3
|
import static org.junit.Assert.assertTrue;
|
4
|
|
5
|
import java.io.File;
|
6
|
import java.io.FileInputStream;
|
7
|
import java.io.IOException;
|
8
|
import java.io.InputStream;
|
9
|
import java.io.StringWriter;
|
10
|
import java.io.Writer;
|
11
|
import java.util.List;
|
12
|
|
13
|
import org.apache.solr.client.solrj.SolrServer;
|
14
|
import org.apache.solr.client.solrj.SolrServerException;
|
15
|
import org.apache.solr.client.solrj.embedded.EmbeddedSolrServer;
|
16
|
import org.apache.solr.client.solrj.response.QueryResponse;
|
17
|
import org.apache.solr.common.params.SolrParams;
|
18
|
import org.apache.solr.core.CoreContainer;
|
19
|
import org.apache.solr.core.SolrCore;
|
20
|
import org.apache.solr.request.LocalSolrQueryRequest;
|
21
|
import org.apache.solr.response.SolrQueryResponse;
|
22
|
import org.apache.solr.response.XMLResponseWriter;
|
23
|
import org.apache.solr.servlet.SolrRequestParsers;
|
24
|
import org.dataone.service.types.v1.SystemMetadata;
|
25
|
import org.dataone.service.util.TypeMarshaller;
|
26
|
import org.junit.Before;
|
27
|
import org.junit.Test;
|
28
|
|
29
|
public class SolrIndexIT {
|
30
|
|
31
|
private static final String SYSTEMMETAFILEPATH = "src/test/resources/eml-system-meta-example.xml";
|
32
|
private static final String EMLFILEPATH = "src/test/resources/eml-example.xml";
|
33
|
private static final String SYSTEMMETAUPDATEFILEPATH = "src/test/resources/eml-updating-system-meta-example.xml";
|
34
|
private static final String EMLUPDATEFILEPATH = "src/test/resources/eml-updating-example.xml";
|
35
|
private static final String id = "urn:uuid:606a19dd-b531-4bf4-b5a5-6d06c3d39098";
|
36
|
private static final String newId = "urn:uuid:606a19dd-b531-4bf4-b5a5-6d06c3d39099";
|
37
|
|
38
|
private SolrIndex solrIndex = null;
|
39
|
|
40
|
|
41
|
@Before
|
42
|
public void setUp() throws Exception {
|
43
|
solrIndex = generateSolrIndex();
|
44
|
}
|
45
|
|
46
|
private SolrIndex generateSolrIndex() throws Exception {
|
47
|
ApplicationController controller = new ApplicationController();
|
48
|
List<SolrIndex> list = controller.getSolrIndexes();
|
49
|
SolrIndex[] solrIndexesarray = list.toArray(new SolrIndex[list.size()]);
|
50
|
SolrIndex index = solrIndexesarray[0];
|
51
|
SolrServer solrServer = SolrServerFactory.createSolrServer();
|
52
|
index.setSolrServer(solrServer);
|
53
|
return index;
|
54
|
}
|
55
|
|
56
|
/**
|
57
|
* Test building index for an insert.
|
58
|
*/
|
59
|
@Test
|
60
|
public void testInsert() throws Exception {
|
61
|
|
62
|
|
63
|
//InputStream systemInputStream = new FileInputStream(new File(SYSTEMMETAFILEPATH));
|
64
|
SystemMetadata systemMetadata = TypeMarshaller.unmarshalTypeFromFile(SystemMetadata.class, SYSTEMMETAFILEPATH);
|
65
|
InputStream emlInputStream = new FileInputStream(new File(EMLFILEPATH));
|
66
|
solrIndex.insert(id, systemMetadata, emlInputStream);
|
67
|
String result = doQuery(solrIndex.getSolrServer());
|
68
|
assertTrue(result.contains("version1"));
|
69
|
|
70
|
}
|
71
|
|
72
|
/**
|
73
|
* Test building index for an insert.
|
74
|
*/
|
75
|
@Test
|
76
|
public void testUpdate() throws Exception {
|
77
|
//InputStream systemInputStream = new FileInputStream(new File(SYSTEMMETAFILEPATH));
|
78
|
SystemMetadata systemMetadata = TypeMarshaller.unmarshalTypeFromFile(SystemMetadata.class, SYSTEMMETAUPDATEFILEPATH);
|
79
|
InputStream emlInputStream = new FileInputStream(new File(EMLUPDATEFILEPATH));
|
80
|
solrIndex.update(newId, systemMetadata, emlInputStream);
|
81
|
String result = doQuery(solrIndex.getSolrServer());
|
82
|
assertTrue(result.contains("version2"));
|
83
|
}
|
84
|
|
85
|
/**
|
86
|
* Test building index for an insert.
|
87
|
*/
|
88
|
@Test
|
89
|
public void testDelete() throws Exception {
|
90
|
SolrIndex solrIndex = generateSolrIndex();
|
91
|
solrIndex.remove(newId);
|
92
|
String result = doQuery(solrIndex.getSolrServer());
|
93
|
assertTrue(!result.contains("version1"));
|
94
|
assertTrue(!result.contains("version2"));
|
95
|
}
|
96
|
|
97
|
|
98
|
/*
|
99
|
* Do query
|
100
|
*/
|
101
|
private String doQuery(SolrServer server)
|
102
|
throws SolrServerException {
|
103
|
StringBuffer request = new StringBuffer();
|
104
|
request.append("q=" + "*:*");
|
105
|
SolrParams solrParams = SolrRequestParsers.parseQueryString(request
|
106
|
.toString());
|
107
|
QueryResponse reponse = server.query(solrParams);
|
108
|
String result = toXML(solrParams, reponse);
|
109
|
System.out.println("**************************************************************************");
|
110
|
System.out.println("The query result:\n");
|
111
|
System.out.println(result);
|
112
|
System.out.println("**************************************************************************");
|
113
|
return result;
|
114
|
}
|
115
|
|
116
|
/*
|
117
|
* Transform the query response to the xml format.
|
118
|
*/
|
119
|
private String toXML(SolrParams request, QueryResponse response) {
|
120
|
XMLResponseWriter xmlWriter = new XMLResponseWriter();
|
121
|
Writer w = new StringWriter();
|
122
|
SolrQueryResponse sResponse = new SolrQueryResponse();
|
123
|
sResponse.setAllValues(response.getResponse());
|
124
|
try {
|
125
|
SolrCore core = null;
|
126
|
CoreContainer container = SolrServerFactory.getCoreContainer();
|
127
|
core = container.getCore("collection1");
|
128
|
xmlWriter.write(w, new LocalSolrQueryRequest(core, request), sResponse);
|
129
|
} catch (IOException e) {
|
130
|
throw new RuntimeException("Unable to convert Solr response into XML", e);
|
131
|
}
|
132
|
return w.toString();
|
133
|
}
|
134
|
|
135
|
|
136
|
|
137
|
|
138
|
}
|
139
|
|