use of org.apache.solr.handler.loader.JsonLoader in project lucene-solr by apache.
the class JsonLoaderTest method testInvalidJsonProducesBadRequestSolrException.
@Test
public void testInvalidJsonProducesBadRequestSolrException() throws Exception {
SolrQueryResponse rsp = new SolrQueryResponse();
BufferingRequestProcessor p = new BufferingRequestProcessor(null);
JsonLoader loader = new JsonLoader();
String invalidJsonString = "}{";
try (SolrQueryRequest req = req()) {
try {
loader.load(req, rsp, new ContentStreamBase.StringStream(invalidJsonString), p);
fail("Expected invalid JSON to produce a SolrException.");
} catch (SolrException expectedException) {
assertEquals(SolrException.ErrorCode.BAD_REQUEST.code, expectedException.code());
assertTrue(expectedException.getMessage().contains("Cannot parse"));
assertTrue(expectedException.getMessage().contains("JSON"));
}
}
}
use of org.apache.solr.handler.loader.JsonLoader in project lucene-solr by apache.
the class JsonLoaderTest method testBigDecimalValuesInAdd.
@Test
public void testBigDecimalValuesInAdd() throws Exception {
String str = ("{'add':[{'id':'1','bd1':0.12345678901234567890123456789012345," + "'bd2':12345678901234567890.12345678901234567890,'bd3':0.012345678901234567890123456789012345," + "'bd3':123456789012345678900.012345678901234567890}]}").replace('\'', '"');
SolrQueryRequest req = req();
SolrQueryResponse rsp = new SolrQueryResponse();
BufferingRequestProcessor p = new BufferingRequestProcessor(null);
JsonLoader loader = new JsonLoader();
loader.load(req, rsp, new ContentStreamBase.StringStream(str), p);
assertEquals(1, p.addCommands.size());
AddUpdateCommand add = p.addCommands.get(0);
SolrInputDocument d = add.solrDoc;
SolrInputField f = d.getField("bd1");
assertTrue(f.getValue() instanceof String);
assertEquals("0.12345678901234567890123456789012345", f.getValue());
f = d.getField("bd2");
assertTrue(f.getValue() instanceof String);
assertEquals("12345678901234567890.12345678901234567890", f.getValue());
f = d.getField("bd3");
assertEquals(2, ((List) f.getValue()).size());
assertTrue(((List) f.getValue()).contains("0.012345678901234567890123456789012345"));
assertTrue(((List) f.getValue()).contains("123456789012345678900.012345678901234567890"));
req.close();
}
use of org.apache.solr.handler.loader.JsonLoader in project lucene-solr by apache.
the class JsonLoaderTest method testFewParentsJsonDoc.
public void testFewParentsJsonDoc() throws Exception {
String json = PARENT_TWO_CHILDREN_JSON;
SolrQueryRequest req;
SolrQueryResponse rsp;
BufferingRequestProcessor p;
JsonLoader loader;
{
//multichild test case
final boolean array = random().nextBoolean();
StringBuilder b = new StringBuilder();
if (array) {
b.append("[");
}
final int passes = atLeast(2);
for (int i = 1; i <= passes; i++) {
b.append(json.replace("1", "" + i));
if (array) {
b.append(i < passes ? "," : "]");
}
}
req = req(PARENT_TWO_CHILDREN_PARAMS);
req.getContext().put("path", "/update/json/docs");
rsp = new SolrQueryResponse();
p = new BufferingRequestProcessor(null);
loader = new JsonLoader();
loader.load(req, rsp, new ContentStreamBase.StringStream(b.toString()), p);
for (int i = 1; i <= passes; i++) {
final int ii = i;
UnaryOperator<String> s = (v) -> v.replace("1", "" + ii);
final SolrInputDocument parent = p.addCommands.get(i - 1).solrDoc;
assertOnlyValue(s.apply("1"), parent, "id");
assertOnlyValue("i am the parent", parent, "name");
assertOnlyValue("parent", parent, "cat");
assertEquals(2, parent.getChildDocuments().size());
{
final SolrInputDocument child1 = parent.getChildDocuments().get(0);
assertOnlyValue(s.apply("1.1"), child1, "id");
assertOnlyValue(s.apply("i am the 1st child"), child1, "name");
assertOnlyValue("child", child1, "cat");
}
{
final SolrInputDocument child2 = parent.getChildDocuments().get(1);
assertOnlyValue(s.apply("1.2"), child2, "id");
assertOnlyValue("i am the 2nd child", child2, "name");
assertOnlyValue("child", child2, "cat");
assertEquals(1, child2.getChildDocuments().size());
final SolrInputDocument grandChild = child2.getChildDocuments().get(0);
assertOnlyValue(s.apply("1.2.1"), grandChild, "id");
assertOnlyValue("i am the grandchild", grandChild, "name");
assertOnlyValue("grandchild", grandChild, "cat");
}
}
}
}
use of org.apache.solr.handler.loader.JsonLoader in project lucene-solr by apache.
the class JsonLoaderTest method checkFieldValueOrdering.
private void checkFieldValueOrdering(String rawJson, float fBoost) throws Exception {
SolrQueryRequest req = req();
SolrQueryResponse rsp = new SolrQueryResponse();
BufferingRequestProcessor p = new BufferingRequestProcessor(null);
JsonLoader loader = new JsonLoader();
loader.load(req, rsp, new ContentStreamBase.StringStream(rawJson), p);
assertEquals(2, p.addCommands.size());
SolrInputDocument d = p.addCommands.get(0).solrDoc;
assertEquals(2, d.getFieldNames().size());
assertEquals("1", d.getFieldValue("id"));
assertEquals(new Object[] { 45L, 67L, 89L }, d.getFieldValues("f").toArray());
d = p.addCommands.get(1).solrDoc;
assertEquals(1, d.getFieldNames().size());
assertEquals("2", d.getFieldValue("id"));
req.close();
}
Aggregations