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