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.IOException;
|
9
|
import java.io.InputStream;
|
10
|
import java.util.List;
|
11
|
|
12
|
import javax.xml.parsers.ParserConfigurationException;
|
13
|
|
14
|
|
15
|
|
16
|
import org.apache.solr.client.solrj.SolrServer;
|
17
|
import org.apache.solr.client.solrj.SolrServerException;
|
18
|
import org.apache.solr.client.solrj.embedded.EmbeddedSolrServer;
|
19
|
import org.apache.solr.client.solrj.response.QueryResponse;
|
20
|
import org.apache.solr.common.params.SolrParams;
|
21
|
import org.apache.solr.core.CoreContainer;
|
22
|
import org.apache.solr.servlet.SolrRequestParsers;
|
23
|
import org.dataone.configuration.Settings;
|
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
|
import org.xml.sax.SAXException;
|
29
|
|
30
|
public class SolrIndexIT {
|
31
|
|
32
|
private static final String SYSTEMMETAFILEPATH = "src/test/resources/eml-system-meta-example.xml";
|
33
|
private static final String EMLFILEPATH = "src/test/resources/eml-example.xml";
|
34
|
private static final String SYSTEMMETAUPDATEFILEPATH = "src/test/resources/eml-updating-system-meta-example.xml";
|
35
|
private static final String EMLUPDATEFILEPATH = "src/test/resources/eml-updating-example.xml";
|
36
|
private static final String id = "urn:uuid:606a19dd-b531-4bf4-b5a5-6d06c3d39098";
|
37
|
private static final String newId = "urn:uuid:606a19dd-b531-4bf4-b5a5-6d06c3d39099";
|
38
|
private static String SOLRTESTHOMEPATH = null;
|
39
|
|
40
|
/**
|
41
|
*
|
42
|
*/
|
43
|
@Before
|
44
|
public void setUp() {
|
45
|
SOLRTESTHOMEPATH = Settings.getConfiguration().getString("solr.homeDir");
|
46
|
}
|
47
|
|
48
|
/**
|
49
|
* Test building index for an insert.
|
50
|
*/
|
51
|
@Test
|
52
|
public void testInsert() throws Exception {
|
53
|
try {
|
54
|
SolrIndex solrIndex = generateSolrIndex();
|
55
|
|
56
|
//InputStream systemInputStream = new FileInputStream(new File(SYSTEMMETAFILEPATH));
|
57
|
SystemMetadata systemMetadata = TypeMarshaller.unmarshalTypeFromFile(SystemMetadata.class, SYSTEMMETAFILEPATH);
|
58
|
InputStream emlInputStream = new FileInputStream(new File(EMLFILEPATH));
|
59
|
solrIndex.insert(id, systemMetadata, emlInputStream);
|
60
|
String result = doQuery(solrIndex.getSolrServer());
|
61
|
assertTrue(result.contains("version1"));
|
62
|
} catch (Exception e) {
|
63
|
e.printStackTrace();
|
64
|
}
|
65
|
}
|
66
|
|
67
|
/**
|
68
|
* Test building index for an insert.
|
69
|
*/
|
70
|
@Test
|
71
|
public void testUpdate() throws Exception {
|
72
|
SolrIndex solrIndex = generateSolrIndex();
|
73
|
//InputStream systemInputStream = new FileInputStream(new File(SYSTEMMETAFILEPATH));
|
74
|
SystemMetadata systemMetadata = TypeMarshaller.unmarshalTypeFromFile(SystemMetadata.class, SYSTEMMETAUPDATEFILEPATH);
|
75
|
InputStream emlInputStream = new FileInputStream(new File(EMLUPDATEFILEPATH));
|
76
|
solrIndex.update(newId, systemMetadata, emlInputStream);
|
77
|
String result = doQuery(solrIndex.getSolrServer());
|
78
|
assertTrue(result.contains("version2"));
|
79
|
}
|
80
|
|
81
|
/**
|
82
|
* Test building index for an insert.
|
83
|
*/
|
84
|
//@Test
|
85
|
/*public void testDelete() throws Exception {
|
86
|
SolrIndex solrIndex = generateSolrIndex();
|
87
|
//solrIndex.remove(newId);
|
88
|
//String result = doQuery(solrIndex.getSolrServer());
|
89
|
//assertTrue(!result.contains("version1"));
|
90
|
//assertTrue(!result.contains("version2"));
|
91
|
}*/
|
92
|
|
93
|
|
94
|
|
95
|
/*
|
96
|
* Do query
|
97
|
*/
|
98
|
private String doQuery(SolrServer server)
|
99
|
throws SolrServerException {
|
100
|
StringBuffer request = new StringBuffer();
|
101
|
request.append("q=" + "*:*");
|
102
|
SolrParams solrParams = SolrRequestParsers.parseQueryString(request
|
103
|
.toString());
|
104
|
QueryResponse reponse = server.query(solrParams);
|
105
|
System.out.println("**************************************************************************");
|
106
|
System.out.println("The query result:\n");
|
107
|
System.out.println(reponse.toString());
|
108
|
System.out.println("**************************************************************************");
|
109
|
return reponse.toString();
|
110
|
}
|
111
|
|
112
|
|
113
|
/*
|
114
|
* Print out the QueryResponse
|
115
|
*/
|
116
|
/*private void print(QueryResponse response) {
|
117
|
SolrDocumentList docs = response.getResults();
|
118
|
if (docs != null) {
|
119
|
System.out.println(docs.getNumFound() + " documents found, "
|
120
|
+ docs.size() + " returned : ");
|
121
|
for (int i = 0; i < docs.size(); i++) {
|
122
|
SolrDocument doc = docs.get(i);
|
123
|
System.out.println("\t" + doc.toString());
|
124
|
}
|
125
|
}
|
126
|
}*/
|
127
|
|
128
|
private SolrIndex generateSolrIndex() throws Exception {
|
129
|
ApplicationController controller = new ApplicationController();
|
130
|
List<SolrIndex> list = controller.getSolrIndexes();
|
131
|
SolrIndex[] solrIndexesarray = list.toArray(new SolrIndex[list.size()]);
|
132
|
SolrIndex index = solrIndexesarray[0];
|
133
|
index.setSolrServer(generateSolrServer());
|
134
|
return index;
|
135
|
}
|
136
|
|
137
|
/*
|
138
|
* Generate a test solr server
|
139
|
*/
|
140
|
private SolrServer generateSolrServer() throws IOException, ParserConfigurationException, SAXException {
|
141
|
//createSolrHome();
|
142
|
File configFile = new File(SOLRTESTHOMEPATH, "solr.xml");
|
143
|
CoreContainer c = new CoreContainer(SOLRTESTHOMEPATH, configFile);
|
144
|
c.load(SOLRTESTHOMEPATH, configFile);
|
145
|
SolrServer solrServer = new EmbeddedSolrServer(c, "collection1");
|
146
|
return solrServer;
|
147
|
}
|
148
|
|
149
|
}
|
150
|
|