use of org.apache.solr.client.solrj.impl.NoOpResponseParser in project lucene-solr by apache.
the class SolrExampleTests method testRawFields.
@Test
public void testRawFields() throws Exception {
String rawJson = "{ \"raw\": 1.234, \"id\":\"111\" }";
String rawXml = "<hello>this is <some/><xml/></hello>";
SolrClient client = getSolrClient();
// Empty the database...
// delete everything!
client.deleteByQuery("*:*");
// Now add something...
SolrInputDocument doc = new SolrInputDocument();
doc.addField("id", "111");
doc.addField("name", "doc1");
doc.addField("json_s", rawJson);
doc.addField("xml_s", rawXml);
client.add(doc);
// make sure this gets in first
client.commit();
SolrQuery query = new SolrQuery();
query.setQuery("*:*");
query.set(CommonParams.FL, "id,json_s:[json],xml_s:[xml]");
QueryRequest req = new QueryRequest(query);
req.setResponseParser(new BinaryResponseParser());
QueryResponse rsp = req.process(client);
SolrDocumentList out = rsp.getResults();
assertEquals(1, out.getNumFound());
SolrDocument out1 = out.get(0);
assertEquals("111", out1.getFieldValue("id"));
// Check that the 'raw' fields are unchanged using the standard formats
assertEquals(rawJson, out1.get("json_s"));
assertEquals(rawXml, out1.get("xml_s"));
if (client instanceof EmbeddedSolrServer) {
// the EmbeddedSolrServer ignores the configured parser
return;
}
// Check raw JSON Output
query.set("fl", "id,json_s:[json],xml_s:[xml]");
query.set(CommonParams.WT, "json");
req = new QueryRequest(query);
req.setResponseParser(new NoOpResponseParser("json"));
NamedList<Object> resp = client.request(req);
String raw = (String) resp.get("response");
// Check that the response parses as JSON
JSONParser parser = new JSONParser(raw);
int evt = parser.nextEvent();
while (evt != JSONParser.EOF) {
evt = parser.nextEvent();
}
// no escaping
assertTrue(raw.indexOf(rawJson) > 0);
// quoted xml
assertTrue(raw.indexOf('"' + rawXml + '"') > 0);
// Check raw XML Output
req.setResponseParser(new NoOpResponseParser("xml"));
query.set("fl", "id,json_s:[json],xml_s:[xml]");
query.set(CommonParams.WT, "xml");
req = new QueryRequest(query);
req.setResponseParser(new NoOpResponseParser("xml"));
resp = client.request(req);
raw = (String) resp.get("response");
// Check that we get raw xml and json is escaped
// escaped
assertTrue(raw.indexOf('>' + rawJson + '<') > 0);
// raw xml
assertTrue(raw.indexOf(rawXml) > 0);
}
use of org.apache.solr.client.solrj.impl.NoOpResponseParser in project lucene-solr by apache.
the class NoOpResponseParserTest method testInputStreamResponse.
/**
* Parse response from java.io.InputStream.
*/
@Test
public void testInputStreamResponse() throws Exception {
NoOpResponseParser parser = new NoOpResponseParser();
try (final InputStream is = getResponse()) {
assertNotNull(is);
NamedList<Object> response = parser.processResponse(is, "UTF-8");
assertNotNull(response.get("response"));
String expectedResponse = IOUtils.toString(getResponse(), "UTF-8");
assertEquals(expectedResponse, response.get("response"));
}
}
use of org.apache.solr.client.solrj.impl.NoOpResponseParser in project lucene-solr by apache.
the class NoOpResponseParserTest method testReaderResponse.
/**
* Parse response from java.io.Reader.
*/
@Test
public void testReaderResponse() throws Exception {
NoOpResponseParser parser = new NoOpResponseParser();
try (final InputStream is = getResponse()) {
assertNotNull(is);
Reader in = new InputStreamReader(is, StandardCharsets.UTF_8);
NamedList<Object> response = parser.processResponse(in);
assertNotNull(response.get("response"));
String expectedResponse = IOUtils.toString(getResponse(), "UTF-8");
assertEquals(expectedResponse, response.get("response"));
}
}
use of org.apache.solr.client.solrj.impl.NoOpResponseParser in project lucene-solr by apache.
the class NoOpResponseParserTest method testQueryParse.
/**
* Parse response from query using NoOpResponseParser.
*/
@Test
public void testQueryParse() throws Exception {
try (HttpSolrClient client = (HttpSolrClient) createNewSolrClient()) {
SolrQuery query = new SolrQuery("id:1234");
QueryRequest req = new QueryRequest(query);
client.setParser(new NoOpResponseParser());
NamedList<Object> resp = client.request(req);
String responseString = (String) resp.get("response");
assertResponse(responseString);
}
}
use of org.apache.solr.client.solrj.impl.NoOpResponseParser in project lucene-solr by apache.
the class SolrTestCaseHS method getQueryResponse.
public static String getQueryResponse(SolrClient client, String wt, SolrParams params) throws Exception {
if (client == null) {
return getQueryResponse(wt, params);
}
ModifiableSolrParams p = new ModifiableSolrParams(params);
p.set("wt", wt);
String path = p.get("qt");
p.remove("qt");
p.set("indent", "true");
QueryRequest query = new QueryRequest(p);
if (path != null) {
query.setPath(path);
}
query.setResponseParser(new NoOpResponseParser(wt));
NamedList<Object> rsp = client.request(query);
String raw = (String) rsp.get("response");
return raw;
}
Aggregations