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