1 |
7547
|
tao
|
package edu.ucsb.nceas.metacat.index;
|
2 |
|
|
|
3 |
|
|
import static org.junit.Assert.assertTrue;
|
4 |
|
|
|
5 |
7631
|
tao
|
import edu.ucsb.nceas.metacat.common.SolrServerFactory;
|
6 |
7547
|
tao
|
import java.io.File;
|
7 |
|
|
import java.io.FileInputStream;
|
8 |
7592
|
tao
|
import java.io.IOException;
|
9 |
7547
|
tao
|
import java.io.InputStream;
|
10 |
7592
|
tao
|
import java.io.StringWriter;
|
11 |
|
|
import java.io.Writer;
|
12 |
7603
|
tao
|
import java.util.ArrayList;
|
13 |
7547
|
tao
|
import java.util.List;
|
14 |
|
|
|
15 |
7569
|
tao
|
import org.apache.solr.client.solrj.SolrServer;
|
16 |
|
|
import org.apache.solr.client.solrj.SolrServerException;
|
17 |
7592
|
tao
|
import org.apache.solr.client.solrj.embedded.EmbeddedSolrServer;
|
18 |
7569
|
tao
|
import org.apache.solr.client.solrj.response.QueryResponse;
|
19 |
|
|
import org.apache.solr.common.params.SolrParams;
|
20 |
7592
|
tao
|
import org.apache.solr.core.CoreContainer;
|
21 |
|
|
import org.apache.solr.core.SolrCore;
|
22 |
|
|
import org.apache.solr.request.LocalSolrQueryRequest;
|
23 |
|
|
import org.apache.solr.response.SolrQueryResponse;
|
24 |
|
|
import org.apache.solr.response.XMLResponseWriter;
|
25 |
7569
|
tao
|
import org.apache.solr.servlet.SolrRequestParsers;
|
26 |
7555
|
tao
|
import org.dataone.service.types.v1.SystemMetadata;
|
27 |
|
|
import org.dataone.service.util.TypeMarshaller;
|
28 |
7590
|
leinfelder
|
import org.junit.Before;
|
29 |
7547
|
tao
|
import org.junit.Test;
|
30 |
|
|
|
31 |
7582
|
leinfelder
|
public class SolrIndexIT {
|
32 |
7547
|
tao
|
|
33 |
|
|
private static final String SYSTEMMETAFILEPATH = "src/test/resources/eml-system-meta-example.xml";
|
34 |
|
|
private static final String EMLFILEPATH = "src/test/resources/eml-example.xml";
|
35 |
7578
|
tao
|
private static final String SYSTEMMETAUPDATEFILEPATH = "src/test/resources/eml-updating-system-meta-example.xml";
|
36 |
|
|
private static final String EMLUPDATEFILEPATH = "src/test/resources/eml-updating-example.xml";
|
37 |
7627
|
tao
|
private static final String SYSTEMMETAARCHIVEFILEPATH = "src/test/resources/eml-archive-system-meta-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 |
7547
|
tao
|
|
41 |
7591
|
leinfelder
|
private SolrIndex solrIndex = null;
|
42 |
|
|
|
43 |
|
|
|
44 |
7590
|
leinfelder
|
@Before
|
45 |
7591
|
leinfelder
|
public void setUp() throws Exception {
|
46 |
|
|
solrIndex = generateSolrIndex();
|
47 |
7590
|
leinfelder
|
}
|
48 |
7573
|
tao
|
|
49 |
7591
|
leinfelder
|
private SolrIndex generateSolrIndex() throws Exception {
|
50 |
7784
|
tao
|
String springConfigFile = "src/main/resources/index-processor-context.xml";
|
51 |
|
|
String metacatPropertyFile = null; //in this test, we use the test.properties file rather than metacat.properties file. so set it to be null.
|
52 |
|
|
ApplicationController controller = new ApplicationController(springConfigFile, metacatPropertyFile);
|
53 |
|
|
controller.initialize();
|
54 |
7591
|
leinfelder
|
List<SolrIndex> list = controller.getSolrIndexes();
|
55 |
|
|
SolrIndex[] solrIndexesarray = list.toArray(new SolrIndex[list.size()]);
|
56 |
|
|
SolrIndex index = solrIndexesarray[0];
|
57 |
7593
|
tao
|
//SolrServer solrServer = SolrServerFactory.createSolrServer();
|
58 |
|
|
//index.setSolrServer(solrServer);
|
59 |
7591
|
leinfelder
|
return index;
|
60 |
|
|
}
|
61 |
|
|
|
62 |
7547
|
tao
|
/**
|
63 |
|
|
* Test building index for an insert.
|
64 |
|
|
*/
|
65 |
|
|
@Test
|
66 |
|
|
public void testInsert() throws Exception {
|
67 |
7590
|
leinfelder
|
|
68 |
7591
|
leinfelder
|
|
69 |
7555
|
tao
|
//InputStream systemInputStream = new FileInputStream(new File(SYSTEMMETAFILEPATH));
|
70 |
|
|
SystemMetadata systemMetadata = TypeMarshaller.unmarshalTypeFromFile(SystemMetadata.class, SYSTEMMETAFILEPATH);
|
71 |
7627
|
tao
|
InputStream emlInputStream = new FileInputStream(new File(EMLFILEPATH));
|
72 |
|
|
List<String> chain = null;
|
73 |
|
|
solrIndex.update(id, chain, systemMetadata, emlInputStream);
|
74 |
7578
|
tao
|
String result = doQuery(solrIndex.getSolrServer());
|
75 |
7604
|
tao
|
List<String> ids = solrIndex.getSolrIds();
|
76 |
7627
|
tao
|
//assertTrue(ids.size() == 1);
|
77 |
|
|
boolean foundId = false;
|
78 |
7604
|
tao
|
for(String identifiers :ids) {
|
79 |
7627
|
tao
|
if(id.equals(identifiers)) {
|
80 |
|
|
foundId = true;
|
81 |
|
|
}
|
82 |
7604
|
tao
|
}
|
83 |
7627
|
tao
|
assertTrue(foundId);
|
84 |
7578
|
tao
|
assertTrue(result.contains("version1"));
|
85 |
7591
|
leinfelder
|
|
86 |
7547
|
tao
|
}
|
87 |
|
|
|
88 |
7548
|
tao
|
/**
|
89 |
|
|
* Test building index for an insert.
|
90 |
|
|
*/
|
91 |
|
|
@Test
|
92 |
7578
|
tao
|
public void testUpdate() throws Exception {
|
93 |
|
|
//InputStream systemInputStream = new FileInputStream(new File(SYSTEMMETAFILEPATH));
|
94 |
|
|
SystemMetadata systemMetadata = TypeMarshaller.unmarshalTypeFromFile(SystemMetadata.class, SYSTEMMETAUPDATEFILEPATH);
|
95 |
7603
|
tao
|
InputStream emlInputStream = new FileInputStream(new File(EMLUPDATEFILEPATH));
|
96 |
|
|
ArrayList<String> obsoletes = new ArrayList<String>();
|
97 |
|
|
obsoletes.add(id);
|
98 |
|
|
obsoletes.add("tao");
|
99 |
|
|
solrIndex.update(newId, obsoletes, systemMetadata, emlInputStream);
|
100 |
7578
|
tao
|
String result = doQuery(solrIndex.getSolrServer());
|
101 |
|
|
assertTrue(result.contains("version2"));
|
102 |
|
|
}
|
103 |
|
|
|
104 |
|
|
/**
|
105 |
|
|
* Test building index for an insert.
|
106 |
|
|
*/
|
107 |
7592
|
tao
|
@Test
|
108 |
7627
|
tao
|
public void testArchive() throws Exception {
|
109 |
7573
|
tao
|
SolrIndex solrIndex = generateSolrIndex();
|
110 |
7627
|
tao
|
//InputStream systemInputStream = new FileInputStream(new File(SYSTEMMETAFILEPATH));
|
111 |
|
|
//System metadata's archive is true.
|
112 |
|
|
SystemMetadata systemMetadata = TypeMarshaller.unmarshalTypeFromFile(SystemMetadata.class, SYSTEMMETAARCHIVEFILEPATH);
|
113 |
|
|
InputStream emlInputStream = new FileInputStream(new File(EMLUPDATEFILEPATH));
|
114 |
|
|
ArrayList<String> obsoletes = new ArrayList<String>();
|
115 |
|
|
obsoletes.add(id);
|
116 |
|
|
obsoletes.add("tao");
|
117 |
|
|
solrIndex.update(newId, obsoletes, systemMetadata, emlInputStream);
|
118 |
7592
|
tao
|
String result = doQuery(solrIndex.getSolrServer());
|
119 |
|
|
assertTrue(!result.contains("version1"));
|
120 |
|
|
assertTrue(!result.contains("version2"));
|
121 |
|
|
}
|
122 |
7548
|
tao
|
|
123 |
7569
|
tao
|
|
124 |
|
|
/*
|
125 |
|
|
* Do query
|
126 |
|
|
*/
|
127 |
7607
|
tao
|
public static String doQuery(SolrServer server)
|
128 |
7569
|
tao
|
throws SolrServerException {
|
129 |
|
|
StringBuffer request = new StringBuffer();
|
130 |
|
|
request.append("q=" + "*:*");
|
131 |
|
|
SolrParams solrParams = SolrRequestParsers.parseQueryString(request
|
132 |
|
|
.toString());
|
133 |
|
|
QueryResponse reponse = server.query(solrParams);
|
134 |
7592
|
tao
|
String result = toXML(solrParams, reponse);
|
135 |
7573
|
tao
|
System.out.println("**************************************************************************");
|
136 |
|
|
System.out.println("The query result:\n");
|
137 |
7592
|
tao
|
System.out.println(result);
|
138 |
7573
|
tao
|
System.out.println("**************************************************************************");
|
139 |
7592
|
tao
|
return result;
|
140 |
7569
|
tao
|
}
|
141 |
|
|
|
142 |
|
|
/*
|
143 |
7592
|
tao
|
* Transform the query response to the xml format.
|
144 |
7569
|
tao
|
*/
|
145 |
7607
|
tao
|
private static String toXML(SolrParams request, QueryResponse response) {
|
146 |
7592
|
tao
|
XMLResponseWriter xmlWriter = new XMLResponseWriter();
|
147 |
|
|
Writer w = new StringWriter();
|
148 |
|
|
SolrQueryResponse sResponse = new SolrQueryResponse();
|
149 |
|
|
sResponse.setAllValues(response.getResponse());
|
150 |
|
|
try {
|
151 |
|
|
SolrCore core = null;
|
152 |
|
|
CoreContainer container = SolrServerFactory.getCoreContainer();
|
153 |
|
|
core = container.getCore("collection1");
|
154 |
|
|
xmlWriter.write(w, new LocalSolrQueryRequest(core, request), sResponse);
|
155 |
7658
|
tao
|
} catch (Exception e) {
|
156 |
7592
|
tao
|
throw new RuntimeException("Unable to convert Solr response into XML", e);
|
157 |
7569
|
tao
|
}
|
158 |
7592
|
tao
|
return w.toString();
|
159 |
|
|
}
|
160 |
|
|
|
161 |
|
|
|
162 |
7573
|
tao
|
|
163 |
|
|
|
164 |
7547
|
tao
|
}
|