diff options
Diffstat (limited to 'container-search')
7 files changed, 238 insertions, 231 deletions
diff --git a/container-search/pom.xml b/container-search/pom.xml index 014b7dda14f..074f5827122 100644 --- a/container-search/pom.xml +++ b/container-search/pom.xml @@ -150,11 +150,6 @@ <version>${project.version}</version> <scope>test</scope> </dependency> - <dependency> - <groupId>org.assertj</groupId> - <artifactId>assertj-core</artifactId> - <scope>test</scope> - </dependency> </dependencies> <build> <plugins> diff --git a/container-search/src/main/java/com/yahoo/prelude/hitfield/JSONString.java b/container-search/src/main/java/com/yahoo/prelude/hitfield/JSONString.java index 55438aa35ba..209bfd08e6b 100644 --- a/container-search/src/main/java/com/yahoo/prelude/hitfield/JSONString.java +++ b/container-search/src/main/java/com/yahoo/prelude/hitfield/JSONString.java @@ -21,7 +21,6 @@ import java.util.Iterator; * * @author Steinar Knutsen */ -// TODO Vespa 8: remove methods leaking org.json types (replace with Slime equivalent?) public class JSONString implements Inspectable { private Inspector value; @@ -437,8 +436,6 @@ public class JSONString implements Inspectable { return content; } - /** @deprecated Use {@link #getContent()} instead and parse content yourself */ - @Deprecated(forRemoval = true, since = "7") public Object getParsedJSON() { initContent(); if (parsedJSON == null) { @@ -447,7 +444,6 @@ public class JSONString implements Inspectable { return parsedJSON; } - @Deprecated(forRemoval = true, since = "7") public void setParsedJSON(Object parsedJSON) { this.parsedJSON = parsedJSON; } diff --git a/container-search/src/main/java/com/yahoo/search/rendering/JsonRenderer.java b/container-search/src/main/java/com/yahoo/search/rendering/JsonRenderer.java index c4f850307ae..31f8194b3b7 100644 --- a/container-search/src/main/java/com/yahoo/search/rendering/JsonRenderer.java +++ b/container-search/src/main/java/com/yahoo/search/rendering/JsonRenderer.java @@ -45,6 +45,8 @@ import com.yahoo.search.result.Hit; import com.yahoo.search.result.HitGroup; import com.yahoo.search.result.NanNumber; import com.yahoo.tensor.Tensor; +import org.json.JSONArray; +import org.json.JSONObject; import java.io.IOException; import java.io.OutputStream; @@ -669,6 +671,14 @@ public class JsonRenderer extends AsynchronousSectionedRenderer<Result> { } else if (field instanceof FieldValue) { // the null below is the field which has already been written ((FieldValue) field).serialize(null, new JsonWriter(generator)); + } else if (field instanceof JSONArray || field instanceof JSONObject) { + // org.json returns null if the object would not result in syntactically correct JSON + String s = field.toString(); + if (s == null) { + generator.writeNull(); + } else { + generator.writeRawValue(s); + } } else { generator.writeString(field.toString()); } diff --git a/container-search/src/test/java/com/yahoo/prelude/fastsearch/SlimeSummaryTestCase.java b/container-search/src/test/java/com/yahoo/prelude/fastsearch/SlimeSummaryTestCase.java index 49df321e581..d1399cabc75 100644 --- a/container-search/src/test/java/com/yahoo/prelude/fastsearch/SlimeSummaryTestCase.java +++ b/container-search/src/test/java/com/yahoo/prelude/fastsearch/SlimeSummaryTestCase.java @@ -4,21 +4,16 @@ package com.yahoo.prelude.fastsearch; import com.google.common.collect.ImmutableSet; import com.yahoo.config.subscription.ConfigGetter; import com.yahoo.data.access.slime.SlimeAdapter; -import com.yahoo.prelude.hitfield.JSONString; import com.yahoo.prelude.hitfield.RawData; import com.yahoo.prelude.hitfield.XMLString; +import com.yahoo.prelude.hitfield.JSONString; import com.yahoo.search.result.FeatureData; import com.yahoo.search.result.Hit; +import com.yahoo.search.result.NanNumber; import com.yahoo.search.result.StructuredData; -import com.yahoo.slime.BinaryFormat; -import com.yahoo.slime.Cursor; -import com.yahoo.slime.Slime; -import com.yahoo.tensor.Tensor; -import com.yahoo.tensor.serialization.TypedBinaryFormat; -import org.junit.Test; - import java.nio.ByteBuffer; import java.nio.ByteOrder; + import java.nio.charset.StandardCharsets; import java.util.HashMap; import java.util.Iterator; @@ -26,6 +21,14 @@ import java.util.Map; import java.util.Set; import java.util.stream.Collectors; +import com.yahoo.slime.BinaryFormat; +import com.yahoo.slime.Cursor; +import com.yahoo.slime.Inspector; +import com.yahoo.slime.Slime; +import com.yahoo.tensor.Tensor; +import com.yahoo.tensor.serialization.TypedBinaryFormat; +import org.junit.Test; + import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; @@ -99,7 +102,7 @@ public class SlimeSummaryTestCase { if (hit.getField("jsonstring_field") instanceof JSONString) { JSONString jstr = (JSONString) hit.getField("jsonstring_field"); assertEquals("{\"foo\":1,\"bar\":2}", jstr.getContent()); - assertNotNull(getParsedJSON(jstr)); + assertNotNull(jstr.getParsedJSON()); com.yahoo.data.access.Inspector value = jstr.inspect(); assertEquals(1L, value.field("foo").asLong()); @@ -123,8 +126,6 @@ public class SlimeSummaryTestCase { assertEquals(tensor2, featureData.getTensor("tensor2_feature")); } - @SuppressWarnings("removal") private static Object getParsedJSON(JSONString jstr) { return jstr.getParsedJSON(); } - @Test public void testFieldAccessAPI() { DocsumDefinitionSet partialDocsum1 = createDocsumDefinitionSet(partial_summary1_cf); diff --git a/container-search/src/test/java/com/yahoo/search/handler/test/JSONSearchHandlerTestCase.java b/container-search/src/test/java/com/yahoo/search/handler/test/JSONSearchHandlerTestCase.java index 80e629ca4cb..3cca053d0e5 100644 --- a/container-search/src/test/java/com/yahoo/search/handler/test/JSONSearchHandlerTestCase.java +++ b/container-search/src/test/java/com/yahoo/search/handler/test/JSONSearchHandlerTestCase.java @@ -1,13 +1,10 @@ // Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. package com.yahoo.search.handler.test; -import com.fasterxml.jackson.databind.JsonNode; -import com.fasterxml.jackson.databind.ObjectMapper; -import com.fasterxml.jackson.databind.node.ArrayNode; -import com.fasterxml.jackson.databind.node.ObjectNode; import com.yahoo.container.Container; import com.yahoo.container.core.config.testutil.HandlersConfigurerTestWrapper; import com.yahoo.container.jdisc.HttpRequest; + import com.yahoo.container.jdisc.RequestHandlerTestDriver; import com.yahoo.container.protect.Error; import com.yahoo.io.IOUtils; @@ -16,8 +13,8 @@ import com.yahoo.search.handler.SearchHandler; import com.yahoo.search.searchchain.config.test.SearchChainConfigurerTestCase; import com.yahoo.slime.Inspector; import com.yahoo.slime.SlimeUtils; -import com.yahoo.test.json.JsonTestHelper; -import org.assertj.core.api.Assertions; +import org.json.JSONArray; +import org.json.JSONObject; import org.junit.After; import org.junit.Before; import org.junit.Rule; @@ -26,19 +23,13 @@ import org.junit.rules.TemporaryFolder; import java.io.File; import java.io.IOException; -import java.nio.charset.StandardCharsets; -import java.util.HashMap; import java.util.Map; +import java.util.HashMap; import static com.yahoo.jdisc.http.HttpRequest.Method.GET; import static org.hamcrest.CoreMatchers.containsString; import static org.hamcrest.CoreMatchers.is; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertNotSame; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertThat; -import static org.junit.Assert.assertTrue; +import static org.junit.Assert.*; /** * Tests submitting the query as JSON. @@ -47,8 +38,6 @@ import static org.junit.Assert.assertTrue; */ public class JSONSearchHandlerTestCase { - private static final ObjectMapper jsonMapper = new ObjectMapper(); - private static final String testDir = "src/test/java/com/yahoo/search/handler/test/config"; private static final String myHostnameHeader = "my-hostname-header"; private static final String selfHostname = HostName.getLocalhost(); @@ -108,8 +97,8 @@ public class JSONSearchHandlerTestCase { } @Test - public void testFailing() { - ObjectNode json = jsonMapper.createObjectNode(); + public void testFailing() throws Exception { + JSONObject json = new JSONObject(); json.put("query", "test"); json.put("searchChain", "classLoadingError"); assertTrue(driver.sendRequest(uri, com.yahoo.jdisc.http.HttpRequest.Method.POST, json.toString(), JSON_CONTENT_TYPE).readAll().contains("NoClassDefFoundError")); @@ -117,16 +106,16 @@ public class JSONSearchHandlerTestCase { @Test - public synchronized void testPluginError() { - ObjectNode json = jsonMapper.createObjectNode(); + public synchronized void testPluginError() throws Exception { + JSONObject json = new JSONObject(); json.put("query", "test"); json.put("searchChain", "exceptionInPlugin"); assertTrue(driver.sendRequest(uri, com.yahoo.jdisc.http.HttpRequest.Method.POST, json.toString(), JSON_CONTENT_TYPE).readAll().contains("NullPointerException")); } @Test - public synchronized void testWorkingReconfiguration() throws IOException { - ObjectNode json = jsonMapper.createObjectNode(); + public synchronized void testWorkingReconfiguration() throws Exception { + JSONObject json = new JSONObject(); json.put("query", "abc"); assertJsonResult(json, driver); @@ -146,7 +135,7 @@ public class JSONSearchHandlerTestCase { } @Test - public void testInvalidYqlQuery() throws IOException { + public void testInvalidYqlQuery() throws Exception { IOUtils.copyDirectory(new File(testDir, "config_yql"), new File(tempDir), 1); generateComponentsConfigForActive(); configurer.reloadConfig(); @@ -154,7 +143,7 @@ public class JSONSearchHandlerTestCase { SearchHandler newSearchHandler = fetchSearchHandler(configurer); assertTrue("Do I have a new instance of the search handler?", searchHandler != newSearchHandler); try (RequestHandlerTestDriver newDriver = new RequestHandlerTestDriver(newSearchHandler)) { - ObjectNode json = jsonMapper.createObjectNode(); + JSONObject json = new JSONObject(); json.put("yql", "select * from foo where bar > 1453501295"); RequestHandlerTestDriver.MockResponseHandler responseHandler = newDriver.sendRequest(uri, com.yahoo.jdisc.http.HttpRequest.Method.POST, json.toString(), JSON_CONTENT_TYPE); responseHandler.readAll(); @@ -164,14 +153,14 @@ public class JSONSearchHandlerTestCase { // Query handling takes a different code path when a query profile is active, so we test both paths. @Test - public void testInvalidQueryParamWithQueryProfile() throws IOException { + public void testInvalidQueryParamWithQueryProfile() throws Exception { try (RequestHandlerTestDriver newDriver = driverWithConfig("config_invalid_param")) { testInvalidQueryParam(newDriver); } } - private void testInvalidQueryParam(final RequestHandlerTestDriver testDriver) { - ObjectNode json = jsonMapper.createObjectNode(); + private void testInvalidQueryParam(final RequestHandlerTestDriver testDriver) throws Exception { + JSONObject json = new JSONObject(); json.put("query", "status_code:0"); json.put("hits", 20); json.put("offset", -20); @@ -184,16 +173,16 @@ public class JSONSearchHandlerTestCase { } @Test - public void testNormalResultJsonAliasRendering() { - ObjectNode json = jsonMapper.createObjectNode(); + public void testNormalResultJsonAliasRendering() throws Exception { + JSONObject json = new JSONObject(); json.put("format", "json"); json.put("query", "abc"); assertJsonResult(json, driver); } @Test - public void testNullQuery() { - ObjectNode json = jsonMapper.createObjectNode(); + public void testNullQuery() throws Exception { + JSONObject json = new JSONObject(); json.put("format", "xml"); assertEquals("<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n" + @@ -206,8 +195,8 @@ public class JSONSearchHandlerTestCase { } @Test - public void testWebServiceStatus() { - ObjectNode json = jsonMapper.createObjectNode(); + public void testWebServiceStatus() throws Exception { + JSONObject json = new JSONObject(); json.put("query", "web_service_status_code"); RequestHandlerTestDriver.MockResponseHandler responseHandler = driver.sendRequest(uri, com.yahoo.jdisc.http.HttpRequest.Method.POST, json.toString(), JSON_CONTENT_TYPE); @@ -217,39 +206,39 @@ public class JSONSearchHandlerTestCase { } @Test - public void testNormalResultImplicitDefaultRendering() { - ObjectNode json = jsonMapper.createObjectNode(); + public void testNormalResultImplicitDefaultRendering() throws Exception { + JSONObject json = new JSONObject(); json.put("query", "abc"); assertJsonResult(json, driver); } @Test - public void testNormalResultExplicitDefaultRendering() { - ObjectNode json = jsonMapper.createObjectNode(); + public void testNormalResultExplicitDefaultRendering() throws Exception { + JSONObject json = new JSONObject(); json.put("query", "abc"); json.put("format", "default"); assertJsonResult(json, driver); } @Test - public void testNormalResultXmlAliasRendering() { - ObjectNode json = jsonMapper.createObjectNode(); + public void testNormalResultXmlAliasRendering() throws Exception { + JSONObject json = new JSONObject(); json.put("query", "abc"); json.put("format", "xml"); assertXmlResult(json, driver); } @Test - public void testNormalResultExplicitDefaultRenderingFullRendererName1() { - ObjectNode json = jsonMapper.createObjectNode(); + public void testNormalResultExplicitDefaultRenderingFullRendererName1() throws Exception { + JSONObject json = new JSONObject(); json.put("query", "abc"); json.put("format", "XmlRenderer"); assertXmlResult(json, driver); } @Test - public void testNormalResultExplicitDefaultRenderingFullRendererName2() { - ObjectNode json = jsonMapper.createObjectNode(); + public void testNormalResultExplicitDefaultRenderingFullRendererName2() throws Exception { + JSONObject json = new JSONObject(); json.put("query", "abc"); json.put("format", "JsonRenderer"); assertJsonResult(json, driver); @@ -264,7 +253,7 @@ public class JSONSearchHandlerTestCase { " </hit>\n" + "</result>\n"; - private void assertXmlResult(JsonNode json, RequestHandlerTestDriver driver) { + private void assertXmlResult(JSONObject json, RequestHandlerTestDriver driver) { assertOkResult(driver.sendRequest(uri, com.yahoo.jdisc.http.HttpRequest.Method.POST, json.toString(), JSON_CONTENT_TYPE), xmlResult); } @@ -274,7 +263,7 @@ public class JSONSearchHandlerTestCase { + "{\"id\":\"testHit\",\"relevance\":1.0,\"fields\":{\"uri\":\"testHit\"}}" + "]}}"; - private void assertJsonResult(JsonNode json, RequestHandlerTestDriver driver) { + private void assertJsonResult(JSONObject json, RequestHandlerTestDriver driver) { assertOkResult(driver.sendRequest(uri, com.yahoo.jdisc.http.HttpRequest.Method.POST, json.toString(), JSON_CONTENT_TYPE), jsonResult); } @@ -299,7 +288,7 @@ public class JSONSearchHandlerTestCase { } - private RequestHandlerTestDriver driverWithConfig(String configDirectory) throws IOException { + private RequestHandlerTestDriver driverWithConfig(String configDirectory) throws Exception { IOUtils.copyDirectory(new File(testDir, configDirectory), new File(tempDir), 1); generateComponentsConfigForActive(); configurer.reloadConfig(); @@ -310,44 +299,44 @@ public class JSONSearchHandlerTestCase { } @Test - public void testSelectParameters() throws IOException { - ObjectNode json = jsonMapper.createObjectNode(); + public void testSelectParameters() throws Exception { + JSONObject json = new JSONObject(); - ObjectNode select = jsonMapper.createObjectNode(); + JSONObject select = new JSONObject(); - ObjectNode where = jsonMapper.createObjectNode(); + JSONObject where = new JSONObject(); where.put("where", "where"); - ObjectNode grouping = jsonMapper.createObjectNode(); + JSONObject grouping = new JSONObject(); grouping.put("grouping", "grouping"); - select.set("where", where); - select.set("grouping", grouping); + select.put("where", where); + select.put("grouping", grouping); - json.set("select", select); + json.put("select", select); - Inspector inspector = SlimeUtils.jsonToSlime(json.toString().getBytes(StandardCharsets.UTF_8)).get(); + Inspector inspector = SlimeUtils.jsonToSlime(json.toString().getBytes("utf-8")).get(); Map<String, String> map = new HashMap<>(); searchHandler.createRequestMapping(inspector, map, ""); - JsonNode processedWhere = jsonMapper.readTree(map.get("select.where")); - JsonTestHelper.assertJsonEquals(where.toString(), processedWhere.toString()); + JSONObject processedWhere = new JSONObject(map.get("select.where")); + assertEquals(where.toString(), processedWhere.toString()); - JsonNode processedGrouping = jsonMapper.readTree(map.get("select.grouping")); - JsonTestHelper.assertJsonEquals(grouping.toString(), processedGrouping.toString()); + JSONObject processedGrouping = new JSONObject(map.get("select.grouping")); + assertEquals(grouping.toString(), processedGrouping.toString()); } @Test - public void testJsonQueryWithSelectWhere() { - ObjectNode root = jsonMapper.createObjectNode(); - ObjectNode select = jsonMapper.createObjectNode(); - ObjectNode where = jsonMapper.createObjectNode(); - ArrayNode term = jsonMapper.createArrayNode(); - term.add("default"); - term.add("bad"); - where.set("contains", term); - select.set("where", where); - root.set("select", select); + public void testJsonQueryWithSelectWhere() throws Exception { + JSONObject root = new JSONObject(); + JSONObject select = new JSONObject(); + JSONObject where = new JSONObject(); + JSONArray term = new JSONArray(); + term.put("default"); + term.put("bad"); + where.put("contains", term); + select.put("where", where); + root.put("select", select); // Run query String result = driver.sendRequest(uri + "searchChain=echoingQuery", com.yahoo.jdisc.http.HttpRequest.Method.POST, root.toString(), JSON_CONTENT_TYPE).readAll(); @@ -404,8 +393,8 @@ public class JSONSearchHandlerTestCase { } @Test - public void testJsonQueryWithYQL() { - ObjectNode root = jsonMapper.createObjectNode(); + public void testJsonQueryWithYQL() throws Exception { + JSONObject root = new JSONObject(); root.put("yql", "select * from sources * where default contains 'bad';"); // Run query @@ -415,10 +404,10 @@ public class JSONSearchHandlerTestCase { } @Test - public void testRequestMapping() { - ObjectNode json = jsonMapper.createObjectNode(); + public void testRequestMapping() throws Exception { + JSONObject json = new JSONObject(); json.put("yql", "select * from sources * where sddocname contains \"blog_post\" limit 0 | all(group(date) max(3) order(-count())each(output(count())));"); - json.put("hits", 10); + json.put("hits", 10.0); json.put("offset", 5); json.put("queryProfile", "foo"); json.put("nocache", false); @@ -428,7 +417,7 @@ public class JSONSearchHandlerTestCase { json.put("select", "_all"); - ObjectNode model = jsonMapper.createObjectNode(); + JSONObject model = new JSONObject(); model.put("defaultIndex", 1); model.put("encoding", "json"); model.put("filter", "default"); @@ -438,9 +427,9 @@ public class JSONSearchHandlerTestCase { model.put("searchPath", "node1"); model.put("sources", "source1,source2"); model.put("type", "yql"); - json.set("model", model); + json.put("model", model); - ObjectNode ranking = jsonMapper.createObjectNode(); + JSONObject ranking = new JSONObject(); ranking.put("location", "123789.89123N;128123W"); ranking.put("features", "none"); ranking.put("listFeatures", false); @@ -450,61 +439,61 @@ public class JSONSearchHandlerTestCase { ranking.put("freshness", "0.05"); ranking.put("queryCache", false); - ObjectNode matchPhase = jsonMapper.createObjectNode(); + JSONObject matchPhase = new JSONObject(); matchPhase.put("maxHits", "100"); matchPhase.put("attribute", "title"); matchPhase.put("ascending", true); - ObjectNode diversity = jsonMapper.createObjectNode(); + JSONObject diversity = new JSONObject(); diversity.put("attribute", "title"); diversity.put("minGroups", 1); - matchPhase.set("diversity", diversity); - ranking.set("matchPhase", matchPhase); - json.set("ranking", ranking); + matchPhase.put("diversity", diversity); + ranking.put("matchPhase", matchPhase); + json.put("ranking", ranking); - ObjectNode presentation = jsonMapper.createObjectNode(); + JSONObject presentation = new JSONObject(); presentation.put("bolding", true); presentation.put("format", "json"); presentation.put("summary", "none"); presentation.put("template", "json"); presentation.put("timing", false); - json.set("presentation", presentation); + json.put("presentation", presentation); - ObjectNode collapse = jsonMapper.createObjectNode(); + JSONObject collapse = new JSONObject(); collapse.put("field", "none"); collapse.put("size", 2); collapse.put("summary", "default"); - json.set("collapse", collapse); + json.put("collapse", collapse); - ObjectNode trace = jsonMapper.createObjectNode(); + JSONObject trace = new JSONObject(); trace.put("level", 1); trace.put("timestamps", false); trace.put("rules", "none"); - json.set("trace", trace); + json.put("trace", trace); - ObjectNode pos = jsonMapper.createObjectNode(); + JSONObject pos = new JSONObject(); pos.put("ll", "1263123N;1231.9W"); pos.put("radius", "71234m"); pos.put("bb", "1237123W;123218N"); pos.put("attribute", "default"); - json.set("pos", pos); + json.put("pos", pos); - ObjectNode streaming = jsonMapper.createObjectNode(); + JSONObject streaming = new JSONObject(); streaming.put("userid", 123); streaming.put("groupname", "abc"); streaming.put("selection", "none"); streaming.put("priority", 10); streaming.put("maxbucketspervisitor", 5); - json.set("streaming", streaming); + json.put("streaming", streaming); - ObjectNode rules = jsonMapper.createObjectNode(); + JSONObject rules = new JSONObject(); rules.put("off", false); rules.put("rulebase", "default"); - json.set("rules", rules); + json.put("rules", rules); - ObjectNode metrics = jsonMapper.createObjectNode(); + JSONObject metrics = new JSONObject(); metrics.put("ignore", "_all"); - json.set("metrics", metrics); + json.put("metrics", metrics); json.put("recall", "none"); json.put("user", 123); @@ -512,7 +501,7 @@ public class JSONSearchHandlerTestCase { json.put("hitcountestimate", true); // Create mapping - Inspector inspector = SlimeUtils.jsonToSlime(json.toString().getBytes(StandardCharsets.UTF_8)).get(); + Inspector inspector = SlimeUtils.jsonToSlime(json.toString().getBytes("utf-8")).get(); Map<String, String> map = new HashMap<>(); searchHandler.createRequestMapping(inspector, map, ""); @@ -529,12 +518,12 @@ public class JSONSearchHandlerTestCase { // Get mapping Map<String, String> propertyMap = request.propertyMap(); - Assertions.assertThat(propertyMap).isEqualTo(map); + assertEquals("Should have same mapping for properties", map, propertyMap); } @Test - public void testContentTypeParsing() { - ObjectNode json = jsonMapper.createObjectNode(); + public void testContentTypeParsing() throws Exception { + JSONObject json = new JSONObject(); json.put("query", "abc"); assertOkResult(driver.sendRequest(uri, com.yahoo.jdisc.http.HttpRequest.Method.POST, json.toString(), "Application/JSON; charset=utf-8"), jsonResult); } diff --git a/container-search/src/test/java/com/yahoo/search/rendering/JsonRendererTestCase.java b/container-search/src/test/java/com/yahoo/search/rendering/JsonRendererTestCase.java index 48003f6586f..c4d49c11f5e 100644 --- a/container-search/src/test/java/com/yahoo/search/rendering/JsonRendererTestCase.java +++ b/container-search/src/test/java/com/yahoo/search/rendering/JsonRendererTestCase.java @@ -1,6 +1,7 @@ // Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. package com.yahoo.search.rendering; +import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.node.ObjectNode; import com.google.common.util.concurrent.ListenableFuture; @@ -55,6 +56,9 @@ import com.yahoo.tensor.TensorType; import com.yahoo.tensor.serialization.TypedBinaryFormat; import com.yahoo.text.Utf8; import com.yahoo.yolean.trace.TraceNode; +import org.json.JSONArray; +import org.json.JSONException; +import org.json.JSONObject; import org.junit.Before; import org.junit.Test; @@ -63,6 +67,7 @@ import java.io.IOException; import java.math.BigDecimal; import java.math.BigInteger; import java.nio.charset.StandardCharsets; +import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import java.util.concurrent.ExecutionException; @@ -78,8 +83,6 @@ import static org.junit.Assert.assertTrue; */ public class JsonRendererTestCase { - private static final ObjectMapper jsonMapper = new ObjectMapper(); - private JsonRenderer originalRenderer; private JsonRenderer renderer; @@ -956,7 +959,7 @@ public class JsonRendererTestCase { } @Test - public void testJsonObjects() throws InterruptedException, ExecutionException, IOException { + public void testJsonObjects() throws InterruptedException, ExecutionException, IOException, JSONException { String expected = "{" + " \"root\": {" + " \"children\": [" @@ -970,6 +973,14 @@ public class JsonRendererTestCase { + " }," + " \"json producer\": {" + " \"long in structured\": 7809531904" + + " }," + + " \"org.json array\": [" + + " true," + + " true," + + " false" + + " ]," + + " \"org.json object\": {" + + " \"forty-two\": 42" + " }" + " }," + " \"id\": \"json objects\"," @@ -985,17 +996,26 @@ public class JsonRendererTestCase { + "}"; Result r = newEmptyResult(); Hit h = new Hit("json objects"); - ObjectNode j = jsonMapper.createObjectNode(); + JSONObject o = new JSONObject(); + JSONArray a = new JSONArray(); + ObjectMapper mapper = new ObjectMapper(); + JsonNode j = mapper.createObjectNode(); JSONString s = new JSONString("{\"a\": \"b\"}"); Slime slime = new Slime(); Cursor c = slime.setObject(); c.setLong("long in structured", 7809531904L); SlimeAdapter slimeInit = new SlimeAdapter(slime.get()); StructuredData struct = new StructuredData(slimeInit); - j.put("Nineteen-eighty-four", 1984); + ((ObjectNode) j).put("Nineteen-eighty-four", 1984); + o.put("forty-two", 42); + a.put(true); + a.put(true); + a.put(false); h.setField("inspectable", s); h.setField("jackson", j); h.setField("json producer", struct); + h.setField("org.json array", a); + h.setField("org.json object", o); r.hits().add(h); String summary = render(r); assertEqualJson(expected, summary); @@ -1216,13 +1236,11 @@ public class JsonRendererTestCase { public void testThatTheJsonValidatorCanCatchErrors() { String json = "{" + " \"root\": {" - + " \"invalidvalue\": 1adsf," + + " \"duplicate\": 1," + + " \"duplicate\": 2" + " }" + "}"; - assertEquals( - "Unexpected character ('a' (code 97)): was expecting comma to separate Object entries\n" + - " at [Source: { \"root\": { \"invalidvalue\": 1adsf, }}; line: 1, column: 41]", - validateJSON(json)); + assertEquals("Duplicate key \"duplicate\"", validateJSON(json)); } @Test @@ -1298,9 +1316,9 @@ public class JsonRendererTestCase { private String validateJSON(String presumablyValidJson) { try { - jsonMapper.readTree(presumablyValidJson); + new JSONObject(presumablyValidJson); return ""; - } catch (IOException e) { + } catch (JSONException e) { return e.getMessage(); } } diff --git a/container-search/src/test/java/com/yahoo/select/SelectTestCase.java b/container-search/src/test/java/com/yahoo/select/SelectTestCase.java index 9bcd3addd92..3239a97a094 100644 --- a/container-search/src/test/java/com/yahoo/select/SelectTestCase.java +++ b/container-search/src/test/java/com/yahoo/select/SelectTestCase.java @@ -1,9 +1,6 @@ // Copyright 2018 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. package com.yahoo.select; -import com.fasterxml.jackson.databind.ObjectMapper; -import com.fasterxml.jackson.databind.node.ArrayNode; -import com.fasterxml.jackson.databind.node.ObjectNode; import com.yahoo.prelude.query.AndItem; import com.yahoo.prelude.query.ExactStringItem; import com.yahoo.prelude.query.Item; @@ -20,15 +17,19 @@ import com.yahoo.prelude.query.WordItem; import com.yahoo.processing.IllegalInputException; import com.yahoo.search.Query; import com.yahoo.search.grouping.GroupingRequest; +import com.yahoo.search.grouping.request.AllOperation; import com.yahoo.search.query.QueryTree; import com.yahoo.search.query.Select; import com.yahoo.search.query.SelectParser; import com.yahoo.search.query.parser.Parsable; import com.yahoo.search.query.parser.ParserEnvironment; import com.yahoo.search.yql.VespaGroupingStep; +import org.json.JSONException; +import org.json.JSONObject; import org.junit.Test; import java.util.ArrayList; +import java.util.Arrays; import java.util.List; import static org.junit.Assert.assertEquals; @@ -47,18 +48,15 @@ import static org.junit.Assert.fail; */ public class SelectTestCase { - private static final ObjectMapper jsonMapper = new ObjectMapper(); - private final SelectParser parser = new SelectParser(new ParserEnvironment()); //------------------------------------------------------------------- "where" tests @Test - public void test_contains() { - ObjectNode json = jsonMapper.createObjectNode(); - ArrayNode arrayNode = jsonMapper.createArrayNode(); - arrayNode.add("default").add("foo"); - json.set("contains", arrayNode); + public void test_contains() throws Exception { + JSONObject json = new JSONObject(); + List<String> contains = Arrays.asList("default", "foo"); + json.put("contains", contains); assertParse(json.toString(), "default:foo"); } @@ -79,21 +77,21 @@ public class SelectTestCase { @Test public void testOr() throws Exception { - ObjectNode json_two_or = jsonMapper.createObjectNode(); - ObjectNode json_three_or = jsonMapper.createObjectNode(); - ArrayNode contains1 = jsonMapper.createArrayNode().add("title").add("madonna"); - ArrayNode contains2 = jsonMapper.createArrayNode().add("title").add("saint"); - ArrayNode contains3 = jsonMapper.createArrayNode().add("title").add("angel"); - - ObjectNode contains_json1 = jsonMapper.createObjectNode(); - ObjectNode contains_json2 = jsonMapper.createObjectNode(); - ObjectNode contains_json3 = jsonMapper.createObjectNode(); - contains_json1.set("contains", contains1); - contains_json2.set("contains", contains2); - contains_json3.set("contains", contains3); - - json_two_or.set("or", jsonMapper.createArrayNode().add(contains_json1).add(contains_json2)); - json_three_or.set("or", jsonMapper.createArrayNode().add(contains_json1).add(contains_json2).add(contains_json3)); + JSONObject json_two_or = new JSONObject(); + JSONObject json_three_or = new JSONObject(); + List<String> contains1 = Arrays.asList("title", "madonna"); + List<String> contains2 = Arrays.asList("title", "saint"); + List<String> contains3 = Arrays.asList("title", "angel"); + + JSONObject contains_json1 = new JSONObject(); + JSONObject contains_json2 = new JSONObject(); + JSONObject contains_json3 = new JSONObject(); + contains_json1.put("contains", contains1); + contains_json2.put("contains", contains2); + contains_json3.put("contains", contains3); + + json_two_or.put("or", Arrays.asList(contains_json1, contains_json2)); + json_three_or.put("or", Arrays.asList(contains_json1, contains_json2, contains_json3)); assertParse(json_two_or.toString(), "OR title:madonna title:saint"); assertParse(json_three_or.toString(), "OR title:madonna title:saint title:angel"); @@ -101,178 +99,178 @@ public class SelectTestCase { @Test public void testAnd() throws Exception{ - ObjectNode json_two_and = jsonMapper.createObjectNode(); - ObjectNode json_three_and = jsonMapper.createObjectNode(); - ArrayNode contains1 = jsonMapper.createArrayNode().add("title").add("madonna"); - ArrayNode contains2 = jsonMapper.createArrayNode().add("title").add("saint"); - ArrayNode contains3 = jsonMapper.createArrayNode().add("title").add("angel"); - - ObjectNode contains_json1 = jsonMapper.createObjectNode(); - ObjectNode contains_json2 = jsonMapper.createObjectNode(); - ObjectNode contains_json3 = jsonMapper.createObjectNode(); - contains_json1.set("contains", contains1); - contains_json2.set("contains", contains2); - contains_json3.set("contains", contains3); - - json_two_and.set("and", jsonMapper.createArrayNode().add(contains_json1).add(contains_json2)); - json_three_and.set("and", jsonMapper.createArrayNode().add(contains_json1).add(contains_json2).add(contains_json3)); + JSONObject json_two_and = new JSONObject(); + JSONObject json_three_and = new JSONObject(); + List<String> contains1 = Arrays.asList("title", "madonna"); + List<String> contains2 = Arrays.asList("title", "saint"); + List<String> contains3 = Arrays.asList("title", "angel"); + + JSONObject contains_json1 = new JSONObject(); + JSONObject contains_json2 = new JSONObject(); + JSONObject contains_json3 = new JSONObject(); + contains_json1.put("contains", contains1); + contains_json2.put("contains", contains2); + contains_json3.put("contains", contains3); + + json_two_and.put("and", Arrays.asList(contains_json1, contains_json2)); + json_three_and.put("and", Arrays.asList(contains_json1, contains_json2, contains_json3)); assertParse(json_two_and.toString(), "AND title:madonna title:saint"); assertParse(json_three_and.toString(), "AND title:madonna title:saint title:angel"); } @Test - public void testAndNot() { - ObjectNode json_and_not = jsonMapper.createObjectNode(); - ArrayNode contains1 = jsonMapper.createArrayNode().add("title").add("madonna"); - ArrayNode contains2 = jsonMapper.createArrayNode().add("title").add("saint"); + public void testAndNot() throws JSONException { + JSONObject json_and_not = new JSONObject(); + List<String> contains1 = Arrays.asList("title", "madonna"); + List<String> contains2 = Arrays.asList("title", "saint"); - ObjectNode contains_json1 = jsonMapper.createObjectNode(); - ObjectNode contains_json2 = jsonMapper.createObjectNode(); - contains_json1.set("contains", contains1); - contains_json2.set("contains", contains2); + JSONObject contains_json1 = new JSONObject(); + JSONObject contains_json2 = new JSONObject(); + contains_json1.put("contains", contains1); + contains_json2.put("contains", contains2); - json_and_not.set("and_not", jsonMapper.createArrayNode().add(contains_json1).add(contains_json2)); + json_and_not.put("and_not", Arrays.asList(contains_json1, contains_json2)); assertParse(json_and_not.toString(), "+title:madonna -title:saint"); } @Test - public void testLessThan() { - ObjectNode range_json = jsonMapper.createObjectNode(); - ObjectNode operators = jsonMapper.createObjectNode(); + public void testLessThan() throws JSONException { + JSONObject range_json = new JSONObject(); + JSONObject operators = new JSONObject(); operators.put("<", 500); - ArrayNode range = jsonMapper.createArrayNode().add("price").add(operators); + List<Object> range = Arrays.asList("price", operators); - range_json.set("range", range); + range_json.put("range", range); assertParse(range_json.toString(), "price:<500"); } @Test - public void testGreaterThan() { - ObjectNode range_json = jsonMapper.createObjectNode(); - ObjectNode operators = jsonMapper.createObjectNode(); + public void testGreaterThan() throws JSONException { + JSONObject range_json = new JSONObject(); + JSONObject operators = new JSONObject(); operators.put(">", 500); - ArrayNode range = jsonMapper.createArrayNode().add("price").add(operators); + List<Object> range = Arrays.asList("price", operators); - range_json.set("range", range); + range_json.put("range", range); assertParse(range_json.toString(), "price:>500"); } @Test - public void testLessThanOrEqual() { - ObjectNode range_json = jsonMapper.createObjectNode(); - ObjectNode operators = jsonMapper.createObjectNode(); + public void testLessThanOrEqual() throws JSONException { + JSONObject range_json = new JSONObject(); + JSONObject operators = new JSONObject(); operators.put("<=", 500); - ArrayNode range = jsonMapper.createArrayNode().add("price").add(operators); + List<Object> range = Arrays.asList("price", operators); - range_json.set("range", range); + range_json.put("range", range); assertParse(range_json.toString(), "price:[;500]"); } @Test - public void testGreaterThanOrEqual() { - ObjectNode range_json = jsonMapper.createObjectNode(); - ObjectNode operators = jsonMapper.createObjectNode(); + public void testGreaterThanOrEqual() throws JSONException { + JSONObject range_json = new JSONObject(); + JSONObject operators = new JSONObject(); operators.put(">=", 500); - ArrayNode range = jsonMapper.createArrayNode().add("price").add(operators); + List<Object> range = Arrays.asList("price", operators); - range_json.set("range", range); + range_json.put("range", range); assertParse(range_json.toString(), "price:[500;]"); } @Test - public void testEquality() { - ObjectNode range_json = jsonMapper.createObjectNode(); - ObjectNode operators = jsonMapper.createObjectNode(); + public void testEquality() throws JSONException { + JSONObject range_json = new JSONObject(); + JSONObject operators = new JSONObject(); operators.put("=", 500); - ArrayNode range = jsonMapper.createArrayNode().add("price").add(operators); + List<Object> range = Arrays.asList("price", operators); - range_json.set("range", range); + range_json.put("range", range); assertParse(range_json.toString(), "price:500"); } @Test - public void testNegativeLessThan() { - ObjectNode range_json = jsonMapper.createObjectNode(); - ObjectNode operators = jsonMapper.createObjectNode(); + public void testNegativeLessThan() throws JSONException { + JSONObject range_json = new JSONObject(); + JSONObject operators = new JSONObject(); operators.put("<", -500); - ArrayNode range = jsonMapper.createArrayNode().add("price").add(operators); + List<Object> range = Arrays.asList("price", operators); - range_json.set("range", range); + range_json.put("range", range); assertParse(range_json.toString(), "price:<-500"); } @Test - public void testNegativeGreaterThan() { - ObjectNode range_json = jsonMapper.createObjectNode(); - ObjectNode operators = jsonMapper.createObjectNode(); + public void testNegativeGreaterThan() throws JSONException { + JSONObject range_json = new JSONObject(); + JSONObject operators = new JSONObject(); operators.put(">", -500); - ArrayNode range = jsonMapper.createArrayNode().add("price").add(operators); + List<Object> range = Arrays.asList("price", operators); - range_json.set("range", range); + range_json.put("range", range); assertParse(range_json.toString(), "price:>-500"); } @Test - public void testNegativeLessThanOrEqual() { - ObjectNode range_json = jsonMapper.createObjectNode(); - ObjectNode operators = jsonMapper.createObjectNode(); + public void testNegativeLessThanOrEqual() throws JSONException { + JSONObject range_json = new JSONObject(); + JSONObject operators = new JSONObject(); operators.put("<=", -500); - ArrayNode range = jsonMapper.createArrayNode().add("price").add(operators); + List<Object> range = Arrays.asList("price", operators); - range_json.set("range", range); + range_json.put("range", range); assertParse(range_json.toString(), "price:[;-500]"); } @Test - public void testNegativeGreaterThanOrEqual() { - ObjectNode range_json = jsonMapper.createObjectNode(); - ObjectNode operators = jsonMapper.createObjectNode(); + public void testNegativeGreaterThanOrEqual() throws JSONException { + JSONObject range_json = new JSONObject(); + JSONObject operators = new JSONObject(); operators.put(">=", -500); - ArrayNode range = jsonMapper.createArrayNode().add("price").add(operators); + List<Object> range = Arrays.asList("price", operators); - range_json.set("range", range); + range_json.put("range", range); assertParse(range_json.toString(), "price:[-500;]"); } @Test - public void testNegativeEquality() { - ObjectNode range_json = jsonMapper.createObjectNode(); - ObjectNode operators = jsonMapper.createObjectNode(); + public void testNegativeEquality() throws JSONException { + JSONObject range_json = new JSONObject(); + JSONObject operators = new JSONObject(); operators.put("=", -500); - ArrayNode range = jsonMapper.createArrayNode().add("price").add(operators); + List<Object> range = Arrays.asList("price", operators); - range_json.set("range", range); + range_json.put("range", range); assertParse(range_json.toString(), "price:-500"); |