diff --git a/core/src/main/java/org/elasticsearch/common/xcontent/XContentFactory.java b/core/src/main/java/org/elasticsearch/common/xcontent/XContentFactory.java index 47dd1249d4a..a5350e3c662 100644 --- a/core/src/main/java/org/elasticsearch/common/xcontent/XContentFactory.java +++ b/core/src/main/java/org/elasticsearch/common/xcontent/XContentFactory.java @@ -39,7 +39,7 @@ import java.io.OutputStream; */ public class XContentFactory { - private static int GUESS_HEADER_LENGTH = 20; + private static final int GUESS_HEADER_LENGTH = 20; /** * Returns a content builder using JSON format ({@link org.elasticsearch.common.xcontent.XContentType#JSON}. diff --git a/core/src/test/java/org/elasticsearch/action/DocWriteResponseTests.java b/core/src/test/java/org/elasticsearch/action/DocWriteResponseTests.java index 0514c65682d..017ba128d50 100644 --- a/core/src/test/java/org/elasticsearch/action/DocWriteResponseTests.java +++ b/core/src/test/java/org/elasticsearch/action/DocWriteResponseTests.java @@ -72,7 +72,7 @@ public class DocWriteResponseTests extends ESTestCase { builder.startObject(); response.toXContent(builder, ToXContent.EMPTY_PARAMS); builder.endObject(); - try (XContentParser parser = JsonXContent.jsonXContent.createParser(builder.bytes())) { + try (XContentParser parser = createParser(JsonXContent.jsonXContent, builder.bytes())) { assertThat(parser.map(), not(hasKey("forced_refresh"))); } } @@ -81,7 +81,7 @@ public class DocWriteResponseTests extends ESTestCase { builder.startObject(); response.toXContent(builder, ToXContent.EMPTY_PARAMS); builder.endObject(); - try (XContentParser parser = JsonXContent.jsonXContent.createParser(builder.bytes())) { + try (XContentParser parser = createParser(JsonXContent.jsonXContent, builder.bytes())) { assertThat(parser.map(), hasEntry("forced_refresh", true)); } } diff --git a/core/src/test/java/org/elasticsearch/action/admin/indices/shards/IndicesShardStoreResponseTests.java b/core/src/test/java/org/elasticsearch/action/admin/indices/shards/IndicesShardStoreResponseTests.java index 95b847dd658..e556e67c5fa 100644 --- a/core/src/test/java/org/elasticsearch/action/admin/indices/shards/IndicesShardStoreResponseTests.java +++ b/core/src/test/java/org/elasticsearch/action/admin/indices/shards/IndicesShardStoreResponseTests.java @@ -31,6 +31,7 @@ import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentFactory; import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.common.xcontent.XContentType; +import org.elasticsearch.common.xcontent.json.JsonXContent; import org.elasticsearch.index.shard.ShardStateMetaData; import org.elasticsearch.test.ESTestCase; import org.elasticsearch.transport.NodeDisconnectedException; @@ -73,7 +74,7 @@ public class IndicesShardStoreResponseTests extends ESTestCase { contentBuilder.endObject(); BytesReference bytes = contentBuilder.bytes(); - try (XContentParser parser = XContentFactory.xContent(XContentType.JSON).createParser(bytes)) { + try (XContentParser parser = createParser(JsonXContent.jsonXContent, bytes)) { Map map = parser.map(); List failureList = (List) map.get("failures"); assertThat(failureList.size(), equalTo(1)); diff --git a/core/src/test/java/org/elasticsearch/action/termvectors/TermVectorsUnitTests.java b/core/src/test/java/org/elasticsearch/action/termvectors/TermVectorsUnitTests.java index 95b7ba37133..81e6a5b86fb 100644 --- a/core/src/test/java/org/elasticsearch/action/termvectors/TermVectorsUnitTests.java +++ b/core/src/test/java/org/elasticsearch/action/termvectors/TermVectorsUnitTests.java @@ -41,10 +41,9 @@ import org.elasticsearch.common.bytes.BytesArray; import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.io.stream.InputStreamStreamInput; import org.elasticsearch.common.io.stream.OutputStreamStreamOutput; -import org.elasticsearch.common.xcontent.XContentFactory; import org.elasticsearch.common.xcontent.XContentHelper; import org.elasticsearch.common.xcontent.XContentParser; -import org.elasticsearch.common.xcontent.XContentType; +import org.elasticsearch.common.xcontent.json.JsonXContent; import org.elasticsearch.index.mapper.AllFieldMapper; import org.elasticsearch.index.mapper.FieldMapper; import org.elasticsearch.index.mapper.MapperParsingException; @@ -179,7 +178,7 @@ public class TermVectorsUnitTests extends ESTestCase { " {\"fields\" : [\"a\", \"b\",\"c\"], \"offsets\":false, \"positions\":false, \"payloads\":true}"); TermVectorsRequest tvr = new TermVectorsRequest(null, null, null); - XContentParser parser = XContentFactory.xContent(XContentType.JSON).createParser(inputBytes); + XContentParser parser = createParser(JsonXContent.jsonXContent, inputBytes); TermVectorsRequest.parseRequest(tvr, parser); Set fields = tvr.selectedFields(); @@ -200,7 +199,7 @@ public class TermVectorsUnitTests extends ESTestCase { inputBytes = new BytesArray(" {\"offsets\":false, \"positions\":false, \"payloads\":true}"); tvr = new TermVectorsRequest(null, null, null); - parser = XContentFactory.xContent(XContentType.JSON).createParser(inputBytes); + parser = createParser(JsonXContent.jsonXContent, inputBytes); TermVectorsRequest.parseRequest(tvr, parser); additionalFields = ""; RestTermVectorsAction.addFieldStringsFromParameter(tvr, additionalFields); @@ -217,7 +216,7 @@ public class TermVectorsUnitTests extends ESTestCase { TermVectorsRequest tvr = new TermVectorsRequest(null, null, null); boolean threwException = false; try { - XContentParser parser = XContentFactory.xContent(XContentType.JSON).createParser(inputBytes); + XContentParser parser = createParser(JsonXContent.jsonXContent, inputBytes); TermVectorsRequest.parseRequest(tvr, parser); } catch (Exception e) { threwException = true; diff --git a/core/src/test/java/org/elasticsearch/cluster/metadata/IndexGraveyardTests.java b/core/src/test/java/org/elasticsearch/cluster/metadata/IndexGraveyardTests.java index 8dd950ba8e6..0ecde102e80 100644 --- a/core/src/test/java/org/elasticsearch/cluster/metadata/IndexGraveyardTests.java +++ b/core/src/test/java/org/elasticsearch/cluster/metadata/IndexGraveyardTests.java @@ -20,19 +20,16 @@ package org.elasticsearch.cluster.metadata; import org.elasticsearch.common.UUIDs; -import org.elasticsearch.common.io.stream.ByteBufferStreamInput; import org.elasticsearch.common.io.stream.BytesStreamOutput; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.xcontent.ToXContent; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentParser; -import org.elasticsearch.common.xcontent.XContentType; import org.elasticsearch.common.xcontent.json.JsonXContent; import org.elasticsearch.index.Index; import org.elasticsearch.test.ESTestCase; import java.io.IOException; -import java.nio.ByteBuffer; import java.util.ArrayList; import java.util.Collections; import java.util.HashSet; @@ -69,7 +66,7 @@ public class IndexGraveyardTests extends ESTestCase { builder.startObject(); graveyard.toXContent(builder, ToXContent.EMPTY_PARAMS); builder.endObject(); - XContentParser parser = XContentType.JSON.xContent().createParser(builder.bytes()); + XContentParser parser = createParser(JsonXContent.jsonXContent, builder.bytes()); parser.nextToken(); // the beginning of the parser assertThat(IndexGraveyard.PROTO.fromXContent(parser), equalTo(graveyard)); } diff --git a/core/src/test/java/org/elasticsearch/cluster/metadata/IndexMetaDataTests.java b/core/src/test/java/org/elasticsearch/cluster/metadata/IndexMetaDataTests.java index faf34bd3d05..caeb889800c 100644 --- a/core/src/test/java/org/elasticsearch/cluster/metadata/IndexMetaDataTests.java +++ b/core/src/test/java/org/elasticsearch/cluster/metadata/IndexMetaDataTests.java @@ -25,7 +25,6 @@ import org.elasticsearch.common.util.set.Sets; import org.elasticsearch.common.xcontent.ToXContent; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentParser; -import org.elasticsearch.common.xcontent.XContentType; import org.elasticsearch.common.xcontent.json.JsonXContent; import org.elasticsearch.index.shard.ShardId; import org.elasticsearch.test.ESTestCase; @@ -53,7 +52,7 @@ public class IndexMetaDataTests extends ESTestCase { builder.startObject(); metaData.toXContent(builder, ToXContent.EMPTY_PARAMS); builder.endObject(); - XContentParser parser = XContentType.JSON.xContent().createParser(builder.bytes()); + XContentParser parser = createParser(JsonXContent.jsonXContent, builder.bytes()); final IndexMetaData fromXContentMeta = IndexMetaData.PROTO.fromXContent(parser, null); assertEquals(metaData, fromXContentMeta); assertEquals(metaData.hashCode(), fromXContentMeta.hashCode()); diff --git a/core/src/test/java/org/elasticsearch/cluster/metadata/MetaDataTests.java b/core/src/test/java/org/elasticsearch/cluster/metadata/MetaDataTests.java index cf040fb3c7f..4d248adf1e6 100644 --- a/core/src/test/java/org/elasticsearch/cluster/metadata/MetaDataTests.java +++ b/core/src/test/java/org/elasticsearch/cluster/metadata/MetaDataTests.java @@ -27,7 +27,6 @@ import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.xcontent.ToXContent; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentParser; -import org.elasticsearch.common.xcontent.XContentType; import org.elasticsearch.common.xcontent.json.JsonXContent; import org.elasticsearch.index.Index; import org.elasticsearch.test.ESTestCase; @@ -129,7 +128,7 @@ public class MetaDataTests extends ESTestCase { .field("random", "value") .endObject() .endObject().bytes(); - XContentParser parser = JsonXContent.jsonXContent.createParser(metadata); + XContentParser parser = createParser(JsonXContent.jsonXContent, metadata); try { MetaData.Builder.fromXContent(parser); fail(); @@ -145,7 +144,7 @@ public class MetaDataTests extends ESTestCase { .field("random", "value") .endObject() .endObject().bytes(); - XContentParser parser = JsonXContent.jsonXContent.createParser(metadata); + XContentParser parser = createParser(JsonXContent.jsonXContent, metadata); try { IndexMetaData.Builder.fromXContent(parser); fail(); @@ -173,7 +172,7 @@ public class MetaDataTests extends ESTestCase { builder.startObject(); originalMeta.toXContent(builder, ToXContent.EMPTY_PARAMS); builder.endObject(); - XContentParser parser = XContentType.JSON.xContent().createParser(builder.bytes()); + XContentParser parser = createParser(JsonXContent.jsonXContent, builder.bytes()); final MetaData fromXContentMeta = MetaData.PROTO.fromXContent(parser, null); assertThat(fromXContentMeta.indexGraveyard(), equalTo(originalMeta.indexGraveyard())); } diff --git a/core/src/test/java/org/elasticsearch/cluster/metadata/ToAndFromJsonMetaDataTests.java b/core/src/test/java/org/elasticsearch/cluster/metadata/ToAndFromJsonMetaDataTests.java index 47f420e1bea..7a69df8fcfc 100644 --- a/core/src/test/java/org/elasticsearch/cluster/metadata/ToAndFromJsonMetaDataTests.java +++ b/core/src/test/java/org/elasticsearch/cluster/metadata/ToAndFromJsonMetaDataTests.java @@ -21,8 +21,7 @@ package org.elasticsearch.cluster.metadata; import org.elasticsearch.Version; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.common.xcontent.XContentFactory; -import org.elasticsearch.common.xcontent.XContentType; +import org.elasticsearch.common.xcontent.json.JsonXContent; import org.elasticsearch.test.ESTestCase; import java.io.IOException; @@ -149,7 +148,7 @@ public class ToAndFromJsonMetaDataTests extends ESTestCase { String metaDataSource = MetaData.Builder.toXContent(metaData); // System.out.println("ToJson: " + metaDataSource); - MetaData parsedMetaData = MetaData.Builder.fromXContent(XContentFactory.xContent(XContentType.JSON).createParser(metaDataSource)); + MetaData parsedMetaData = MetaData.Builder.fromXContent(createParser(JsonXContent.jsonXContent, metaDataSource)); IndexMetaData indexMetaData = parsedMetaData.index("test1"); assertThat(indexMetaData.primaryTerm(0), equalTo(1L)); diff --git a/core/src/test/java/org/elasticsearch/cluster/routing/AllocationIdTests.java b/core/src/test/java/org/elasticsearch/cluster/routing/AllocationIdTests.java index 173caaab379..f6b5fc1bd3f 100644 --- a/core/src/test/java/org/elasticsearch/cluster/routing/AllocationIdTests.java +++ b/core/src/test/java/org/elasticsearch/cluster/routing/AllocationIdTests.java @@ -23,7 +23,7 @@ import org.elasticsearch.cluster.routing.RecoverySource.StoreRecoverySource; import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.xcontent.ToXContent; import org.elasticsearch.common.xcontent.XContentFactory; -import org.elasticsearch.common.xcontent.XContentType; +import org.elasticsearch.common.xcontent.json.JsonXContent; import org.elasticsearch.index.shard.ShardId; import org.elasticsearch.test.ESTestCase; @@ -129,7 +129,7 @@ public class AllocationIdTests extends ESTestCase { allocationId = AllocationId.newRelocation(allocationId); } BytesReference bytes = allocationId.toXContent(XContentFactory.jsonBuilder(), ToXContent.EMPTY_PARAMS).bytes(); - AllocationId parsedAllocationId = AllocationId.fromXContent(XContentFactory.xContent(XContentType.JSON).createParser(bytes)); + AllocationId parsedAllocationId = AllocationId.fromXContent(createParser(JsonXContent.jsonXContent, bytes)); assertEquals(allocationId, parsedAllocationId); } } diff --git a/core/src/test/java/org/elasticsearch/cluster/routing/allocation/AllocationCommandsTests.java b/core/src/test/java/org/elasticsearch/cluster/routing/allocation/AllocationCommandsTests.java index 5cae5d3d928..f0a50811d38 100644 --- a/core/src/test/java/org/elasticsearch/cluster/routing/allocation/AllocationCommandsTests.java +++ b/core/src/test/java/org/elasticsearch/cluster/routing/allocation/AllocationCommandsTests.java @@ -46,9 +46,8 @@ import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.logging.Loggers; import org.elasticsearch.common.network.NetworkModule; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.common.xcontent.XContentFactory; import org.elasticsearch.common.xcontent.XContentParser; -import org.elasticsearch.common.xcontent.XContentType; +import org.elasticsearch.common.xcontent.json.JsonXContent; import org.elasticsearch.index.IndexNotFoundException; import org.elasticsearch.index.shard.ShardId; import org.elasticsearch.index.shard.ShardNotFoundException; @@ -486,7 +485,7 @@ public class AllocationCommandsTests extends ESAllocationTestCase { " ,{\"cancel\" : {\"index\" : \"test\", \"shard\" : 4, \"node\" : \"node5\", \"allow_primary\" : true}} \n" + " ]\n" + "}\n"; - XContentParser parser = XContentFactory.xContent(XContentType.JSON).createParser(commands); + XContentParser parser = createParser(JsonXContent.jsonXContent, commands); // move two tokens, parser expected to be "on" `commands` field parser.nextToken(); parser.nextToken(); diff --git a/core/src/test/java/org/elasticsearch/common/geo/GeoJSONShapeParserTests.java b/core/src/test/java/org/elasticsearch/common/geo/GeoJSONShapeParserTests.java index 21112b97873..5ac50b91bd3 100644 --- a/core/src/test/java/org/elasticsearch/common/geo/GeoJSONShapeParserTests.java +++ b/core/src/test/java/org/elasticsearch/common/geo/GeoJSONShapeParserTests.java @@ -19,13 +19,6 @@ package org.elasticsearch.common.geo; -import org.locationtech.spatial4j.exception.InvalidShapeException; -import org.locationtech.spatial4j.shape.Circle; -import org.locationtech.spatial4j.shape.Rectangle; -import org.locationtech.spatial4j.shape.Shape; -import org.locationtech.spatial4j.shape.ShapeCollection; -import org.locationtech.spatial4j.shape.jts.JtsGeometry; -import org.locationtech.spatial4j.shape.jts.JtsPoint; import com.vividsolutions.jts.geom.Coordinate; import com.vividsolutions.jts.geom.Geometry; import com.vividsolutions.jts.geom.GeometryFactory; @@ -37,11 +30,19 @@ import com.vividsolutions.jts.geom.Polygon; import org.elasticsearch.ElasticsearchParseException; import org.elasticsearch.common.geo.builders.ShapeBuilder; +import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentFactory; import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.common.xcontent.json.JsonXContent; import org.elasticsearch.test.ESTestCase; import org.elasticsearch.test.hamcrest.ElasticsearchGeoAssertions; +import org.locationtech.spatial4j.exception.InvalidShapeException; +import org.locationtech.spatial4j.shape.Circle; +import org.locationtech.spatial4j.shape.Rectangle; +import org.locationtech.spatial4j.shape.Shape; +import org.locationtech.spatial4j.shape.ShapeCollection; +import org.locationtech.spatial4j.shape.jts.JtsGeometry; +import org.locationtech.spatial4j.shape.jts.JtsPoint; import java.io.IOException; import java.util.ArrayList; @@ -59,21 +60,25 @@ public class GeoJSONShapeParserTests extends ESTestCase { private static final GeometryFactory GEOMETRY_FACTORY = SPATIAL_CONTEXT.getGeometryFactory(); public void testParseSimplePoint() throws IOException { - String pointGeoJson = XContentFactory.jsonBuilder().startObject().field("type", "Point") - .startArray("coordinates").value(100.0).value(0.0).endArray() - .endObject().string(); + XContentBuilder pointGeoJson = XContentFactory.jsonBuilder() + .startObject() + .field("type", "Point") + .startArray("coordinates").value(100.0).value(0.0).endArray() + .endObject(); Point expected = GEOMETRY_FACTORY.createPoint(new Coordinate(100.0, 0.0)); assertGeometryEquals(new JtsPoint(expected, SPATIAL_CONTEXT), pointGeoJson); } public void testParseLineString() throws IOException { - String lineGeoJson = XContentFactory.jsonBuilder().startObject().field("type", "LineString") - .startArray("coordinates") - .startArray().value(100.0).value(0.0).endArray() - .startArray().value(101.0).value(1.0).endArray() - .endArray() - .endObject().string(); + XContentBuilder lineGeoJson = XContentFactory.jsonBuilder() + .startObject() + .field("type", "LineString") + .startArray("coordinates") + .startArray().value(100.0).value(0.0).endArray() + .startArray().value(101.0).value(1.0).endArray() + .endArray() + .endObject(); List lineCoordinates = new ArrayList<>(); lineCoordinates.add(new Coordinate(100, 0)); @@ -85,18 +90,20 @@ public class GeoJSONShapeParserTests extends ESTestCase { } public void testParseMultiLineString() throws IOException { - String multilinesGeoJson = XContentFactory.jsonBuilder().startObject().field("type", "MultiLineString") - .startArray("coordinates") - .startArray() - .startArray().value(100.0).value(0.0).endArray() - .startArray().value(101.0).value(1.0).endArray() - .endArray() - .startArray() - .startArray().value(102.0).value(2.0).endArray() - .startArray().value(103.0).value(3.0).endArray() - .endArray() - .endArray() - .endObject().string(); + XContentBuilder multilinesGeoJson = XContentFactory.jsonBuilder() + .startObject() + .field("type", "MultiLineString") + .startArray("coordinates") + .startArray() + .startArray().value(100.0).value(0.0).endArray() + .startArray().value(101.0).value(1.0).endArray() + .endArray() + .startArray() + .startArray().value(102.0).value(2.0).endArray() + .startArray().value(103.0).value(3.0).endArray() + .endArray() + .endArray() + .endObject(); MultiLineString expected = GEOMETRY_FACTORY.createMultiLineString(new LineString[]{ GEOMETRY_FACTORY.createLineString(new Coordinate[]{ @@ -112,10 +119,12 @@ public class GeoJSONShapeParserTests extends ESTestCase { } public void testParseCircle() throws IOException { - String multilinesGeoJson = XContentFactory.jsonBuilder().startObject().field("type", "circle") - .startArray("coordinates").value(100.0).value(0.0).endArray() - .field("radius", "100m") - .endObject().string(); + XContentBuilder multilinesGeoJson = XContentFactory.jsonBuilder() + .startObject() + .field("type", "circle") + .startArray("coordinates").value(100.0).value(0.0).endArray() + .field("radius", "100m") + .endObject(); Circle expected = SPATIAL_CONTEXT.makeCircle(100.0, 0.0, 360 * 100 / GeoUtils.EARTH_EQUATOR); assertGeometryEquals(expected, multilinesGeoJson); @@ -123,20 +132,24 @@ public class GeoJSONShapeParserTests extends ESTestCase { public void testParseMultiDimensionShapes() throws IOException { // multi dimension point - String pointGeoJson = XContentFactory.jsonBuilder().startObject().field("type", "Point") - .startArray("coordinates").value(100.0).value(0.0).value(15.0).value(18.0).endArray() - .endObject().string(); + XContentBuilder pointGeoJson = XContentFactory.jsonBuilder() + .startObject() + .field("type", "Point") + .startArray("coordinates").value(100.0).value(0.0).value(15.0).value(18.0).endArray() + .endObject(); Point expectedPt = GEOMETRY_FACTORY.createPoint(new Coordinate(100.0, 0.0)); assertGeometryEquals(new JtsPoint(expectedPt, SPATIAL_CONTEXT), pointGeoJson); // multi dimension linestring - String lineGeoJson = XContentFactory.jsonBuilder().startObject().field("type", "LineString") - .startArray("coordinates") - .startArray().value(100.0).value(0.0).value(15.0).endArray() - .startArray().value(101.0).value(1.0).value(18.0).value(19.0).endArray() - .endArray() - .endObject().string(); + XContentBuilder lineGeoJson = XContentFactory.jsonBuilder() + .startObject() + .field("type", "LineString") + .startArray("coordinates") + .startArray().value(100.0).value(0.0).value(15.0).endArray() + .startArray().value(101.0).value(1.0).value(18.0).value(19.0).endArray() + .endArray() + .endObject(); List lineCoordinates = new ArrayList<>(); lineCoordinates.add(new Coordinate(100, 0)); @@ -149,12 +162,12 @@ public class GeoJSONShapeParserTests extends ESTestCase { public void testParseEnvelope() throws IOException { // test #1: envelope with expected coordinate order (TopLeft, BottomRight) - String multilinesGeoJson = XContentFactory.jsonBuilder().startObject().field("type", "envelope") + XContentBuilder multilinesGeoJson = XContentFactory.jsonBuilder().startObject().field("type", "envelope") .startArray("coordinates") .startArray().value(-50).value(30).endArray() .startArray().value(50).value(-30).endArray() .endArray() - .endObject().string(); + .endObject(); Rectangle expected = SPATIAL_CONTEXT.makeRectangle(-50, 50, -30, 30); assertGeometryEquals(expected, multilinesGeoJson); @@ -165,7 +178,7 @@ public class GeoJSONShapeParserTests extends ESTestCase { .startArray().value(50).value(30).endArray() .startArray().value(-50).value(-30).endArray() .endArray() - .endObject().string(); + .endObject(); expected = SPATIAL_CONTEXT.makeRectangle(-50, 50, -30, 30); assertGeometryEquals(expected, multilinesGeoJson); @@ -177,8 +190,8 @@ public class GeoJSONShapeParserTests extends ESTestCase { .startArray().value(-50).value(-30).endArray() .startArray().value(50).value(-39).endArray() .endArray() - .endObject().string(); - XContentParser parser = JsonXContent.jsonXContent.createParser(multilinesGeoJson); + .endObject(); + XContentParser parser = createParser(multilinesGeoJson); parser.nextToken(); ElasticsearchGeoAssertions.assertValidException(parser, ElasticsearchParseException.class); @@ -186,24 +199,26 @@ public class GeoJSONShapeParserTests extends ESTestCase { multilinesGeoJson = XContentFactory.jsonBuilder().startObject().field("type", "envelope") .startArray("coordinates") .endArray() - .endObject().string(); - parser = JsonXContent.jsonXContent.createParser(multilinesGeoJson); + .endObject(); + parser = createParser(multilinesGeoJson); parser.nextToken(); ElasticsearchGeoAssertions.assertValidException(parser, ElasticsearchParseException.class); } public void testParsePolygonNoHoles() throws IOException { - String polygonGeoJson = XContentFactory.jsonBuilder().startObject().field("type", "Polygon") - .startArray("coordinates") - .startArray() - .startArray().value(100.0).value(1.0).endArray() - .startArray().value(101.0).value(1.0).endArray() - .startArray().value(101.0).value(0.0).endArray() - .startArray().value(100.0).value(0.0).endArray() - .startArray().value(100.0).value(1.0).endArray() - .endArray() - .endArray() - .endObject().string(); + XContentBuilder polygonGeoJson = XContentFactory.jsonBuilder() + .startObject() + .field("type", "Polygon") + .startArray("coordinates") + .startArray() + .startArray().value(100.0).value(1.0).endArray() + .startArray().value(101.0).value(1.0).endArray() + .startArray().value(101.0).value(0.0).endArray() + .startArray().value(100.0).value(0.0).endArray() + .startArray().value(100.0).value(1.0).endArray() + .endArray() + .endArray() + .endObject(); List shellCoordinates = new ArrayList<>(); shellCoordinates.add(new Coordinate(100, 0)); @@ -219,50 +234,60 @@ public class GeoJSONShapeParserTests extends ESTestCase { public void testParseInvalidPoint() throws IOException { // test case 1: create an invalid point object with multipoint data format - String invalidPoint1 = XContentFactory.jsonBuilder().startObject().field("type", "point") - .startArray("coordinates") - .startArray().value(-74.011).value(40.753).endArray() - .endArray() - .endObject().string(); - XContentParser parser = JsonXContent.jsonXContent.createParser(invalidPoint1); + XContentBuilder invalidPoint1 = XContentFactory.jsonBuilder() + .startObject() + .field("type", "point") + .startArray("coordinates") + .startArray().value(-74.011).value(40.753).endArray() + .endArray() + .endObject(); + XContentParser parser = createParser(invalidPoint1); parser.nextToken(); ElasticsearchGeoAssertions.assertValidException(parser, ElasticsearchParseException.class); // test case 2: create an invalid point object with an empty number of coordinates - String invalidPoint2 = XContentFactory.jsonBuilder().startObject().field("type", "point") - .startArray("coordinates") - .endArray() - .endObject().string(); - parser = JsonXContent.jsonXContent.createParser(invalidPoint2); + XContentBuilder invalidPoint2 = XContentFactory.jsonBuilder() + .startObject() + .field("type", "point") + .startArray("coordinates") + .endArray() + .endObject(); + parser = createParser(invalidPoint2); parser.nextToken(); ElasticsearchGeoAssertions.assertValidException(parser, ElasticsearchParseException.class); } public void testParseInvalidMultipoint() throws IOException { // test case 1: create an invalid multipoint object with single coordinate - String invalidMultipoint1 = XContentFactory.jsonBuilder().startObject().field("type", "multipoint") - .startArray("coordinates").value(-74.011).value(40.753).endArray() - .endObject().string(); - XContentParser parser = JsonXContent.jsonXContent.createParser(invalidMultipoint1); + XContentBuilder invalidMultipoint1 = XContentFactory.jsonBuilder() + .startObject() + .field("type", "multipoint") + .startArray("coordinates").value(-74.011).value(40.753).endArray() + .endObject(); + XContentParser parser = createParser(invalidMultipoint1); parser.nextToken(); ElasticsearchGeoAssertions.assertValidException(parser, ElasticsearchParseException.class); // test case 2: create an invalid multipoint object with null coordinate - String invalidMultipoint2 = XContentFactory.jsonBuilder().startObject().field("type", "multipoint") - .startArray("coordinates") - .endArray() - .endObject().string(); - parser = JsonXContent.jsonXContent.createParser(invalidMultipoint2); + XContentBuilder invalidMultipoint2 = XContentFactory.jsonBuilder() + .startObject() + .field("type", "multipoint") + .startArray("coordinates") + .endArray() + .endObject(); + parser = createParser(invalidMultipoint2); parser.nextToken(); ElasticsearchGeoAssertions.assertValidException(parser, ElasticsearchParseException.class); // test case 3: create a valid formatted multipoint object with invalid number (0) of coordinates - String invalidMultipoint3 = XContentFactory.jsonBuilder().startObject().field("type", "multipoint") - .startArray("coordinates") - .startArray().endArray() - .endArray() - .endObject().string(); - parser = JsonXContent.jsonXContent.createParser(invalidMultipoint3); + XContentBuilder invalidMultipoint3 = XContentFactory.jsonBuilder() + .startObject() + .field("type", "multipoint") + .startArray("coordinates") + .startArray().endArray() + .endArray() + .endObject(); + parser = createParser(invalidMultipoint3); parser.nextToken(); ElasticsearchGeoAssertions.assertValidException(parser, ElasticsearchParseException.class); } @@ -297,7 +322,7 @@ public class GeoJSONShapeParserTests extends ESTestCase { .endArray() .endObject().string(); - XContentParser parser = JsonXContent.jsonXContent.createParser(multiPolygonGeoJson); + XContentParser parser = createParser(JsonXContent.jsonXContent, multiPolygonGeoJson); parser.nextToken(); ElasticsearchGeoAssertions.assertValidException(parser, InvalidShapeException.class); } @@ -317,7 +342,7 @@ public class GeoJSONShapeParserTests extends ESTestCase { .endArray() .endObject().string(); - XContentParser parser = JsonXContent.jsonXContent.createParser(polygonGeoJson); + XContentParser parser = createParser(JsonXContent.jsonXContent, polygonGeoJson); parser.nextToken(); Shape shape = ShapeBuilder.parse(parser).build(); @@ -337,7 +362,7 @@ public class GeoJSONShapeParserTests extends ESTestCase { .endArray() .endObject().string(); - parser = JsonXContent.jsonXContent.createParser(polygonGeoJson); + parser = createParser(JsonXContent.jsonXContent, polygonGeoJson); parser.nextToken(); shape = ShapeBuilder.parse(parser).build(); @@ -357,7 +382,7 @@ public class GeoJSONShapeParserTests extends ESTestCase { .endArray() .endObject().string(); - parser = JsonXContent.jsonXContent.createParser(polygonGeoJson); + parser = createParser(JsonXContent.jsonXContent, polygonGeoJson); parser.nextToken(); shape = ShapeBuilder.parse(parser).build(); @@ -377,7 +402,7 @@ public class GeoJSONShapeParserTests extends ESTestCase { .endArray() .endObject().string(); - parser = JsonXContent.jsonXContent.createParser(polygonGeoJson); + parser = createParser(JsonXContent.jsonXContent, polygonGeoJson); parser.nextToken(); shape = ShapeBuilder.parse(parser).build(); @@ -405,7 +430,7 @@ public class GeoJSONShapeParserTests extends ESTestCase { .endArray() .endObject().string(); - XContentParser parser = JsonXContent.jsonXContent.createParser(polygonGeoJson); + XContentParser parser = createParser(JsonXContent.jsonXContent, polygonGeoJson); parser.nextToken(); Shape shape = ShapeBuilder.parse(parser).build(); @@ -431,7 +456,7 @@ public class GeoJSONShapeParserTests extends ESTestCase { .endArray() .endObject().string(); - parser = JsonXContent.jsonXContent.createParser(polygonGeoJson); + parser = createParser(JsonXContent.jsonXContent, polygonGeoJson); parser.nextToken(); shape = ShapeBuilder.parse(parser).build(); @@ -457,7 +482,7 @@ public class GeoJSONShapeParserTests extends ESTestCase { .endArray() .endObject().string(); - parser = JsonXContent.jsonXContent.createParser(polygonGeoJson); + parser = createParser(JsonXContent.jsonXContent, polygonGeoJson); parser.nextToken(); shape = ShapeBuilder.parse(parser).build(); @@ -483,7 +508,7 @@ public class GeoJSONShapeParserTests extends ESTestCase { .endArray() .endObject().string(); - parser = JsonXContent.jsonXContent.createParser(polygonGeoJson); + parser = createParser(JsonXContent.jsonXContent, polygonGeoJson); parser.nextToken(); shape = ShapeBuilder.parse(parser).build(); @@ -504,7 +529,7 @@ public class GeoJSONShapeParserTests extends ESTestCase { .endArray() .endArray() .endObject().string(); - XContentParser parser = JsonXContent.jsonXContent.createParser(invalidPoly); + XContentParser parser = createParser(JsonXContent.jsonXContent, invalidPoly); parser.nextToken(); ElasticsearchGeoAssertions.assertValidException(parser, ElasticsearchParseException.class); @@ -517,7 +542,7 @@ public class GeoJSONShapeParserTests extends ESTestCase { .endArray() .endObject().string(); - parser = JsonXContent.jsonXContent.createParser(invalidPoly); + parser = createParser(JsonXContent.jsonXContent, invalidPoly); parser.nextToken(); ElasticsearchGeoAssertions.assertValidException(parser, ElasticsearchParseException.class); @@ -530,7 +555,7 @@ public class GeoJSONShapeParserTests extends ESTestCase { .endArray() .endObject().string(); - parser = JsonXContent.jsonXContent.createParser(invalidPoly); + parser = createParser(JsonXContent.jsonXContent, invalidPoly); parser.nextToken(); ElasticsearchGeoAssertions.assertValidException(parser, ElasticsearchParseException.class); @@ -543,7 +568,7 @@ public class GeoJSONShapeParserTests extends ESTestCase { .endArray() .endObject().string(); - parser = JsonXContent.jsonXContent.createParser(invalidPoly); + parser = createParser(JsonXContent.jsonXContent, invalidPoly); parser.nextToken(); ElasticsearchGeoAssertions.assertValidException(parser, IllegalArgumentException.class); @@ -554,7 +579,7 @@ public class GeoJSONShapeParserTests extends ESTestCase { .endArray() .endObject().string(); - parser = JsonXContent.jsonXContent.createParser(invalidPoly); + parser = createParser(JsonXContent.jsonXContent, invalidPoly); parser.nextToken(); ElasticsearchGeoAssertions.assertValidException(parser, IllegalArgumentException.class); @@ -563,7 +588,7 @@ public class GeoJSONShapeParserTests extends ESTestCase { .startArray("coordinates").endArray() .endObject().string(); - parser = JsonXContent.jsonXContent.createParser(invalidPoly); + parser = createParser(JsonXContent.jsonXContent, invalidPoly); parser.nextToken(); ElasticsearchGeoAssertions.assertValidException(parser, ElasticsearchParseException.class); @@ -574,30 +599,32 @@ public class GeoJSONShapeParserTests extends ESTestCase { .endArray() .endObject().string(); - parser = JsonXContent.jsonXContent.createParser(invalidPoly); + parser = createParser(JsonXContent.jsonXContent, invalidPoly); parser.nextToken(); ElasticsearchGeoAssertions.assertValidException(parser, ElasticsearchParseException.class); } public void testParsePolygonWithHole() throws IOException { - String polygonGeoJson = XContentFactory.jsonBuilder().startObject().field("type", "Polygon") - .startArray("coordinates") - .startArray() - .startArray().value(100.0).value(1.0).endArray() - .startArray().value(101.0).value(1.0).endArray() - .startArray().value(101.0).value(0.0).endArray() - .startArray().value(100.0).value(0.0).endArray() - .startArray().value(100.0).value(1.0).endArray() - .endArray() - .startArray() - .startArray().value(100.2).value(0.8).endArray() - .startArray().value(100.2).value(0.2).endArray() - .startArray().value(100.8).value(0.2).endArray() - .startArray().value(100.8).value(0.8).endArray() - .startArray().value(100.2).value(0.8).endArray() - .endArray() - .endArray() - .endObject().string(); + XContentBuilder polygonGeoJson = XContentFactory.jsonBuilder() + .startObject() + .field("type", "Polygon") + .startArray("coordinates") + .startArray() + .startArray().value(100.0).value(1.0).endArray() + .startArray().value(101.0).value(1.0).endArray() + .startArray().value(101.0).value(0.0).endArray() + .startArray().value(100.0).value(0.0).endArray() + .startArray().value(100.0).value(1.0).endArray() + .endArray() + .startArray() + .startArray().value(100.2).value(0.8).endArray() + .startArray().value(100.2).value(0.2).endArray() + .startArray().value(100.8).value(0.2).endArray() + .startArray().value(100.8).value(0.8).endArray() + .startArray().value(100.2).value(0.8).endArray() + .endArray() + .endArray() + .endObject(); // add 3d point to test ISSUE #10501 List shellCoordinates = new ArrayList<>(); @@ -639,18 +666,20 @@ public class GeoJSONShapeParserTests extends ESTestCase { .endArray() .endObject().string(); - XContentParser parser = JsonXContent.jsonXContent.createParser(polygonGeoJson); + XContentParser parser = createParser(JsonXContent.jsonXContent, polygonGeoJson); parser.nextToken(); ElasticsearchGeoAssertions.assertValidException(parser, InvalidShapeException.class); } public void testParseMultiPoint() throws IOException { - String multiPointGeoJson = XContentFactory.jsonBuilder().startObject().field("type", "MultiPoint") - .startArray("coordinates") - .startArray().value(100.0).value(0.0).endArray() - .startArray().value(101.0).value(1.0).endArray() - .endArray() - .endObject().string(); + XContentBuilder multiPointGeoJson = XContentFactory.jsonBuilder() + .startObject() + .field("type", "MultiPoint") + .startArray("coordinates") + .startArray().value(100.0).value(0.0).endArray() + .startArray().value(101.0).value(1.0).endArray() + .endArray() + .endObject(); ShapeCollection expected = shapeCollection( SPATIAL_CONTEXT.makePoint(100, 0), @@ -660,35 +689,37 @@ public class GeoJSONShapeParserTests extends ESTestCase { public void testParseMultiPolygon() throws IOException { // test #1: two polygons; one without hole, one with hole - String multiPolygonGeoJson = XContentFactory.jsonBuilder().startObject().field("type", "MultiPolygon") - .startArray("coordinates") - .startArray()//first poly (without holes) - .startArray() - .startArray().value(102.0).value(2.0).endArray() - .startArray().value(103.0).value(2.0).endArray() - .startArray().value(103.0).value(3.0).endArray() - .startArray().value(102.0).value(3.0).endArray() - .startArray().value(102.0).value(2.0).endArray() - .endArray() - .endArray() - .startArray()//second poly (with hole) - .startArray() - .startArray().value(100.0).value(0.0).endArray() - .startArray().value(101.0).value(0.0).endArray() - .startArray().value(101.0).value(1.0).endArray() - .startArray().value(100.0).value(1.0).endArray() - .startArray().value(100.0).value(0.0).endArray() - .endArray() - .startArray()//hole - .startArray().value(100.2).value(0.8).endArray() - .startArray().value(100.2).value(0.2).endArray() - .startArray().value(100.8).value(0.2).endArray() - .startArray().value(100.8).value(0.8).endArray() - .startArray().value(100.2).value(0.8).endArray() - .endArray() - .endArray() - .endArray() - .endObject().string(); + XContentBuilder multiPolygonGeoJson = XContentFactory.jsonBuilder() + .startObject() + .field("type", "MultiPolygon") + .startArray("coordinates") + .startArray()//first poly (without holes) + .startArray() + .startArray().value(102.0).value(2.0).endArray() + .startArray().value(103.0).value(2.0).endArray() + .startArray().value(103.0).value(3.0).endArray() + .startArray().value(102.0).value(3.0).endArray() + .startArray().value(102.0).value(2.0).endArray() + .endArray() + .endArray() + .startArray()//second poly (with hole) + .startArray() + .startArray().value(100.0).value(0.0).endArray() + .startArray().value(101.0).value(0.0).endArray() + .startArray().value(101.0).value(1.0).endArray() + .startArray().value(100.0).value(1.0).endArray() + .startArray().value(100.0).value(0.0).endArray() + .endArray() + .startArray()//hole + .startArray().value(100.2).value(0.8).endArray() + .startArray().value(100.2).value(0.2).endArray() + .startArray().value(100.8).value(0.2).endArray() + .startArray().value(100.8).value(0.8).endArray() + .startArray().value(100.2).value(0.8).endArray() + .endArray() + .endArray() + .endArray() + .endObject(); List shellCoordinates = new ArrayList<>(); shellCoordinates.add(new Coordinate(100, 0)); @@ -727,26 +758,28 @@ public class GeoJSONShapeParserTests extends ESTestCase { // test #2: multipolygon; one polygon with one hole // this test converting the multipolygon from a ShapeCollection type // to a simple polygon (jtsGeom) - multiPolygonGeoJson = XContentFactory.jsonBuilder().startObject().field("type", "MultiPolygon") - .startArray("coordinates") - .startArray() - .startArray() - .startArray().value(100.0).value(1.0).endArray() - .startArray().value(101.0).value(1.0).endArray() - .startArray().value(101.0).value(0.0).endArray() - .startArray().value(100.0).value(0.0).endArray() - .startArray().value(100.0).value(1.0).endArray() - .endArray() - .startArray()// hole - .startArray().value(100.2).value(0.8).endArray() - .startArray().value(100.2).value(0.2).endArray() - .startArray().value(100.8).value(0.2).endArray() - .startArray().value(100.8).value(0.8).endArray() - .startArray().value(100.2).value(0.8).endArray() - .endArray() - .endArray() - .endArray() - .endObject().string(); + multiPolygonGeoJson = XContentFactory.jsonBuilder() + .startObject() + .field("type", "MultiPolygon") + .startArray("coordinates") + .startArray() + .startArray() + .startArray().value(100.0).value(1.0).endArray() + .startArray().value(101.0).value(1.0).endArray() + .startArray().value(101.0).value(0.0).endArray() + .startArray().value(100.0).value(0.0).endArray() + .startArray().value(100.0).value(1.0).endArray() + .endArray() + .startArray() // hole + .startArray().value(100.2).value(0.8).endArray() + .startArray().value(100.2).value(0.2).endArray() + .startArray().value(100.8).value(0.2).endArray() + .startArray().value(100.8).value(0.8).endArray() + .startArray().value(100.2).value(0.8).endArray() + .endArray() + .endArray() + .endArray() + .endObject(); shellCoordinates = new ArrayList<>(); shellCoordinates.add(new Coordinate(100, 1)); @@ -771,23 +804,23 @@ public class GeoJSONShapeParserTests extends ESTestCase { } public void testParseGeometryCollection() throws IOException { - String geometryCollectionGeoJson = XContentFactory.jsonBuilder().startObject() - .field("type", "GeometryCollection") - .startArray("geometries") - .startObject() - .field("type", "LineString") - .startArray("coordinates") - .startArray().value(100.0).value(0.0).endArray() - .startArray().value(101.0).value(1.0).endArray() - .endArray() - .endObject() - .startObject() - .field("type", "Point") - .startArray("coordinates").value(102.0).value(2.0).endArray() - .endObject() - .endArray() - .endObject() - .string(); + XContentBuilder geometryCollectionGeoJson = XContentFactory.jsonBuilder() + .startObject() + .field("type", "GeometryCollection") + .startArray("geometries") + .startObject() + .field("type", "LineString") + .startArray("coordinates") + .startArray().value(100.0).value(0.0).endArray() + .startArray().value(101.0).value(1.0).endArray() + .endArray() + .endObject() + .startObject() + .field("type", "Point") + .startArray("coordinates").value(102.0).value(2.0).endArray() + .endObject() + .endArray() + .endObject(); Shape[] expected = new Shape[2]; LineString expectedLineString = GEOMETRY_FACTORY.createLineString(new Coordinate[]{ @@ -803,20 +836,21 @@ public class GeoJSONShapeParserTests extends ESTestCase { } public void testThatParserExtractsCorrectTypeAndCoordinatesFromArbitraryJson() throws IOException { - String pointGeoJson = XContentFactory.jsonBuilder().startObject() - .startObject("crs") - .field("type", "name") - .startObject("properties") - .field("name", "urn:ogc:def:crs:OGC:1.3:CRS84") + XContentBuilder pointGeoJson = XContentFactory.jsonBuilder() + .startObject() + .startObject("crs") + .field("type", "name") + .startObject("properties") + .field("name", "urn:ogc:def:crs:OGC:1.3:CRS84") + .endObject() .endObject() - .endObject() - .field("bbox", "foobar") - .field("type", "point") - .field("bubu", "foobar") - .startArray("coordinates").value(100.0).value(0.0).endArray() - .startObject("nested").startArray("coordinates").value(200.0).value(0.0).endArray().endObject() - .startObject("lala").field("type", "NotAPoint").endObject() - .endObject().string(); + .field("bbox", "foobar") + .field("type", "point") + .field("bubu", "foobar") + .startArray("coordinates").value(100.0).value(0.0).endArray() + .startObject("nested").startArray("coordinates").value(200.0).value(0.0).endArray().endObject() + .startObject("lala").field("type", "NotAPoint").endObject() + .endObject(); Point expected = GEOMETRY_FACTORY.createPoint(new Coordinate(100.0, 0.0)); assertGeometryEquals(new JtsPoint(expected, SPATIAL_CONTEXT), pointGeoJson); @@ -824,170 +858,182 @@ public class GeoJSONShapeParserTests extends ESTestCase { public void testParseOrientationOption() throws IOException { // test 1: valid ccw (right handed system) poly not crossing dateline (with 'right' field) - String polygonGeoJson = XContentFactory.jsonBuilder().startObject().field("type", "Polygon") - .field("orientation", "right") - .startArray("coordinates") - .startArray() - .startArray().value(176.0).value(15.0).endArray() - .startArray().value(-177.0).value(10.0).endArray() - .startArray().value(-177.0).value(-10.0).endArray() - .startArray().value(176.0).value(-15.0).endArray() - .startArray().value(172.0).value(0.0).endArray() - .startArray().value(176.0).value(15.0).endArray() - .endArray() - .startArray() - .startArray().value(-172.0).value(8.0).endArray() - .startArray().value(174.0).value(10.0).endArray() - .startArray().value(-172.0).value(-8.0).endArray() - .startArray().value(-172.0).value(8.0).endArray() - .endArray() - .endArray() - .endObject().string(); + XContentBuilder polygonGeoJson = XContentFactory.jsonBuilder() + .startObject() + .field("type", "Polygon") + .field("orientation", "right") + .startArray("coordinates") + .startArray() + .startArray().value(176.0).value(15.0).endArray() + .startArray().value(-177.0).value(10.0).endArray() + .startArray().value(-177.0).value(-10.0).endArray() + .startArray().value(176.0).value(-15.0).endArray() + .startArray().value(172.0).value(0.0).endArray() + .startArray().value(176.0).value(15.0).endArray() + .endArray() + .startArray() + .startArray().value(-172.0).value(8.0).endArray() + .startArray().value(174.0).value(10.0).endArray() + .startArray().value(-172.0).value(-8.0).endArray() + .startArray().value(-172.0).value(8.0).endArray() + .endArray() + .endArray() + .endObject(); - XContentParser parser = JsonXContent.jsonXContent.createParser(polygonGeoJson); + XContentParser parser = createParser(polygonGeoJson); parser.nextToken(); Shape shape = ShapeBuilder.parse(parser).build(); ElasticsearchGeoAssertions.assertPolygon(shape); // test 2: valid ccw (right handed system) poly not crossing dateline (with 'ccw' field) - polygonGeoJson = XContentFactory.jsonBuilder().startObject().field("type", "Polygon") - .field("orientation", "ccw") - .startArray("coordinates") - .startArray() - .startArray().value(176.0).value(15.0).endArray() - .startArray().value(-177.0).value(10.0).endArray() - .startArray().value(-177.0).value(-10.0).endArray() - .startArray().value(176.0).value(-15.0).endArray() - .startArray().value(172.0).value(0.0).endArray() - .startArray().value(176.0).value(15.0).endArray() - .endArray() - .startArray() - .startArray().value(-172.0).value(8.0).endArray() - .startArray().value(174.0).value(10.0).endArray() - .startArray().value(-172.0).value(-8.0).endArray() - .startArray().value(-172.0).value(8.0).endArray() - .endArray() - .endArray() - .endObject().string(); + polygonGeoJson = XContentFactory.jsonBuilder() + .startObject() + .field("type", "Polygon") + .field("orientation", "ccw") + .startArray("coordinates") + .startArray() + .startArray().value(176.0).value(15.0).endArray() + .startArray().value(-177.0).value(10.0).endArray() + .startArray().value(-177.0).value(-10.0).endArray() + .startArray().value(176.0).value(-15.0).endArray() + .startArray().value(172.0).value(0.0).endArray() + .startArray().value(176.0).value(15.0).endArray() + .endArray() + .startArray() + .startArray().value(-172.0).value(8.0).endArray() + .startArray().value(174.0).value(10.0).endArray() + .startArray().value(-172.0).value(-8.0).endArray() + .startArray().value(-172.0).value(8.0).endArray() + .endArray() + .endArray() + .endObject(); - parser = JsonXContent.jsonXContent.createParser(polygonGeoJson); + parser = createParser(polygonGeoJson); parser.nextToken(); shape = ShapeBuilder.parse(parser).build(); ElasticsearchGeoAssertions.assertPolygon(shape); // test 3: valid ccw (right handed system) poly not crossing dateline (with 'counterclockwise' field) - polygonGeoJson = XContentFactory.jsonBuilder().startObject().field("type", "Polygon") - .field("orientation", "counterclockwise") - .startArray("coordinates") - .startArray() - .startArray().value(176.0).value(15.0).endArray() - .startArray().value(-177.0).value(10.0).endArray() - .startArray().value(-177.0).value(-10.0).endArray() - .startArray().value(176.0).value(-15.0).endArray() - .startArray().value(172.0).value(0.0).endArray() - .startArray().value(176.0).value(15.0).endArray() - .endArray() - .startArray() - .startArray().value(-172.0).value(8.0).endArray() - .startArray().value(174.0).value(10.0).endArray() - .startArray().value(-172.0).value(-8.0).endArray() - .startArray().value(-172.0).value(8.0).endArray() - .endArray() - .endArray() - .endObject().string(); + polygonGeoJson = XContentFactory.jsonBuilder() + .startObject() + .field("type", "Polygon") + .field("orientation", "counterclockwise") + .startArray("coordinates") + .startArray() + .startArray().value(176.0).value(15.0).endArray() + .startArray().value(-177.0).value(10.0).endArray() + .startArray().value(-177.0).value(-10.0).endArray() + .startArray().value(176.0).value(-15.0).endArray() + .startArray().value(172.0).value(0.0).endArray() + .startArray().value(176.0).value(15.0).endArray() + .endArray() + .startArray() + .startArray().value(-172.0).value(8.0).endArray() + .startArray().value(174.0).value(10.0).endArray() + .startArray().value(-172.0).value(-8.0).endArray() + .startArray().value(-172.0).value(8.0).endArray() + .endArray() + .endArray() + .endObject(); - parser = JsonXContent.jsonXContent.createParser(polygonGeoJson); + parser = createParser(polygonGeoJson); parser.nextToken(); shape = ShapeBuilder.parse(parser).build(); ElasticsearchGeoAssertions.assertPolygon(shape); // test 4: valid cw (left handed system) poly crossing dateline (with 'left' field) - polygonGeoJson = XContentFactory.jsonBuilder().startObject().field("type", "Polygon") - .field("orientation", "left") - .startArray("coordinates") - .startArray() - .startArray().value(176.0).value(15.0).endArray() - .startArray().value(-177.0).value(10.0).endArray() - .startArray().value(-177.0).value(-10.0).endArray() - .startArray().value(176.0).value(-15.0).endArray() - .startArray().value(172.0).value(0.0).endArray() - .startArray().value(176.0).value(15.0).endArray() - .endArray() - .startArray() - .startArray().value(-178.0).value(8.0).endArray() - .startArray().value(178.0).value(8.0).endArray() - .startArray().value(180.0).value(-8.0).endArray() - .startArray().value(-178.0).value(8.0).endArray() - .endArray() - .endArray() - .endObject().string(); + polygonGeoJson = XContentFactory.jsonBuilder() + .startObject() + .field("type", "Polygon") + .field("orientation", "left") + .startArray("coordinates") + .startArray() + .startArray().value(176.0).value(15.0).endArray() + .startArray().value(-177.0).value(10.0).endArray() + .startArray().value(-177.0).value(-10.0).endArray() + .startArray().value(176.0).value(-15.0).endArray() + .startArray().value(172.0).value(0.0).endArray() + .startArray().value(176.0).value(15.0).endArray() + .endArray() + .startArray() + .startArray().value(-178.0).value(8.0).endArray() + .startArray().value(178.0).value(8.0).endArray() + .startArray().value(180.0).value(-8.0).endArray() + .startArray().value(-178.0).value(8.0).endArray() + .endArray() + .endArray() + .endObject(); - parser = JsonXContent.jsonXContent.createParser(polygonGeoJson); + parser = createParser(polygonGeoJson); parser.nextToken(); shape = ShapeBuilder.parse(parser).build(); ElasticsearchGeoAssertions.assertMultiPolygon(shape); // test 5: valid cw multipoly (left handed system) poly crossing dateline (with 'cw' field) - polygonGeoJson = XContentFactory.jsonBuilder().startObject().field("type", "Polygon") - .field("orientation", "cw") - .startArray("coordinates") - .startArray() - .startArray().value(176.0).value(15.0).endArray() - .startArray().value(-177.0).value(10.0).endArray() - .startArray().value(-177.0).value(-10.0).endArray() - .startArray().value(176.0).value(-15.0).endArray() - .startArray().value(172.0).value(0.0).endArray() - .startArray().value(176.0).value(15.0).endArray() - .endArray() - .startArray() - .startArray().value(-178.0).value(8.0).endArray() - .startArray().value(178.0).value(8.0).endArray() - .startArray().value(180.0).value(-8.0).endArray() - .startArray().value(-178.0).value(8.0).endArray() - .endArray() - .endArray() - .endObject().string(); + polygonGeoJson = XContentFactory.jsonBuilder() + .startObject() + .field("type", "Polygon") + .field("orientation", "cw") + .startArray("coordinates") + .startArray() + .startArray().value(176.0).value(15.0).endArray() + .startArray().value(-177.0).value(10.0).endArray() + .startArray().value(-177.0).value(-10.0).endArray() + .startArray().value(176.0).value(-15.0).endArray() + .startArray().value(172.0).value(0.0).endArray() + .startArray().value(176.0).value(15.0).endArray() + .endArray() + .startArray() + .startArray().value(-178.0).value(8.0).endArray() + .startArray().value(178.0).value(8.0).endArray() + .startArray().value(180.0).value(-8.0).endArray() + .startArray().value(-178.0).value(8.0).endArray() + .endArray() + .endArray() + .endObject(); - parser = JsonXContent.jsonXContent.createParser(polygonGeoJson); + parser = createParser(polygonGeoJson); parser.nextToken(); shape = ShapeBuilder.parse(parser).build(); ElasticsearchGeoAssertions.assertMultiPolygon(shape); // test 6: valid cw multipoly (left handed system) poly crossing dateline (with 'clockwise' field) - polygonGeoJson = XContentFactory.jsonBuilder().startObject().field("type", "Polygon") - .field("orientation", "clockwise") - .startArray("coordinates") - .startArray() - .startArray().value(176.0).value(15.0).endArray() - .startArray().value(-177.0).value(10.0).endArray() - .startArray().value(-177.0).value(-10.0).endArray() - .startArray().value(176.0).value(-15.0).endArray() - .startArray().value(172.0).value(0.0).endArray() - .startArray().value(176.0).value(15.0).endArray() - .endArray() - .startArray() - .startArray().value(-178.0).value(8.0).endArray() - .startArray().value(178.0).value(8.0).endArray() - .startArray().value(180.0).value(-8.0).endArray() - .startArray().value(-178.0).value(8.0).endArray() - .endArray() - .endArray() - .endObject().string(); + polygonGeoJson = XContentFactory.jsonBuilder() + .startObject() + .field("type", "Polygon") + .field("orientation", "clockwise") + .startArray("coordinates") + .startArray() + .startArray().value(176.0).value(15.0).endArray() + .startArray().value(-177.0).value(10.0).endArray() + .startArray().value(-177.0).value(-10.0).endArray() + .startArray().value(176.0).value(-15.0).endArray() + .startArray().value(172.0).value(0.0).endArray() + .startArray().value(176.0).value(15.0).endArray() + .endArray() + .startArray() + .startArray().value(-178.0).value(8.0).endArray() + .startArray().value(178.0).value(8.0).endArray() + .startArray().value(180.0).value(-8.0).endArray() + .startArray().value(-178.0).value(8.0).endArray() + .endArray() + .endArray() + .endObject(); - parser = JsonXContent.jsonXContent.createParser(polygonGeoJson); + parser = createParser(polygonGeoJson); parser.nextToken(); shape = ShapeBuilder.parse(parser).build(); ElasticsearchGeoAssertions.assertMultiPolygon(shape); } - private void assertGeometryEquals(Shape expected, String geoJson) throws IOException { - XContentParser parser = JsonXContent.jsonXContent.createParser(geoJson); + private void assertGeometryEquals(Shape expected, XContentBuilder geoJson) throws IOException { + XContentParser parser = createParser(geoJson); parser.nextToken(); ElasticsearchGeoAssertions.assertEquals(expected, ShapeBuilder.parse(parser).build()); } diff --git a/core/src/test/java/org/elasticsearch/common/xcontent/ConstructingObjectParserTests.java b/core/src/test/java/org/elasticsearch/common/xcontent/ConstructingObjectParserTests.java index d1fefa81d40..af697d29377 100644 --- a/core/src/test/java/org/elasticsearch/common/xcontent/ConstructingObjectParserTests.java +++ b/core/src/test/java/org/elasticsearch/common/xcontent/ConstructingObjectParserTests.java @@ -26,6 +26,7 @@ import org.elasticsearch.common.ParseFieldMatcherSupplier; import org.elasticsearch.common.ParsingException; import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.xcontent.AbstractObjectParser.NoContextParser; +import org.elasticsearch.common.xcontent.json.JsonXContent; import org.elasticsearch.test.ESTestCase; import org.hamcrest.Matcher; @@ -80,7 +81,7 @@ public class ConstructingObjectParserTests extends ESTestCase { expected.toXContent(builder, ToXContent.EMPTY_PARAMS); builder = shuffleXContent(builder); BytesReference bytes = builder.bytes(); - try (XContentParser parser = XContentFactory.xContent(bytes).createParser(bytes)) { + try (XContentParser parser = createParser(JsonXContent.jsonXContent, bytes)) { HasCtorArguments parsed = randomFrom(HasCtorArguments.ALL_PARSERS).apply(parser, MATCHER); assertEquals(expected.animal, parsed.animal); assertEquals(expected.vegetable, parsed.vegetable); @@ -97,7 +98,7 @@ public class ConstructingObjectParserTests extends ESTestCase { } public void testMissingAllConstructorArgs() throws IOException { - XContentParser parser = XContentType.JSON.xContent().createParser( + XContentParser parser = createParser(JsonXContent.jsonXContent, "{\n" + " \"mineral\": 1\n" + "}"); @@ -112,7 +113,7 @@ public class ConstructingObjectParserTests extends ESTestCase { } public void testMissingAllConstructorArgsButNotRequired() throws IOException { - XContentParser parser = XContentType.JSON.xContent().createParser( + XContentParser parser = createParser(JsonXContent.jsonXContent, "{\n" + " \"mineral\": 1\n" + "}"); @@ -121,7 +122,7 @@ public class ConstructingObjectParserTests extends ESTestCase { } public void testMissingSecondConstructorArg() throws IOException { - XContentParser parser = XContentType.JSON.xContent().createParser( + XContentParser parser = createParser(JsonXContent.jsonXContent, "{\n" + " \"mineral\": 1,\n" + " \"animal\": \"cat\"\n" @@ -132,7 +133,7 @@ public class ConstructingObjectParserTests extends ESTestCase { } public void testMissingSecondConstructorArgButNotRequired() throws IOException { - XContentParser parser = XContentType.JSON.xContent().createParser( + XContentParser parser = createParser(JsonXContent.jsonXContent, "{\n" + " \"mineral\": 1,\n" + " \"animal\": \"cat\"\n" @@ -145,7 +146,7 @@ public class ConstructingObjectParserTests extends ESTestCase { } public void testMissingFirstConstructorArg() throws IOException { - XContentParser parser = XContentType.JSON.xContent().createParser( + XContentParser parser = createParser(JsonXContent.jsonXContent, "{\n" + " \"mineral\": 1,\n" + " \"vegetable\": 2\n" @@ -157,7 +158,7 @@ public class ConstructingObjectParserTests extends ESTestCase { } public void testMissingFirstConstructorArgButNotRequired() throws IOException { - XContentParser parser = XContentType.JSON.xContent().createParser( + XContentParser parser = createParser(JsonXContent.jsonXContent, "{\n" + " \"mineral\": 1,\n" + " \"vegetable\": 2\n" @@ -168,7 +169,7 @@ public class ConstructingObjectParserTests extends ESTestCase { } public void testRepeatedConstructorParam() throws IOException { - XContentParser parser = XContentType.JSON.xContent().createParser( + XContentParser parser = createParser(JsonXContent.jsonXContent, "{\n" + " \"vegetable\": 1,\n" + " \"vegetable\": 2\n" @@ -181,7 +182,7 @@ public class ConstructingObjectParserTests extends ESTestCase { } public void testBadParam() throws IOException { - XContentParser parser = XContentType.JSON.xContent().createParser( + XContentParser parser = createParser(JsonXContent.jsonXContent, "{\n" + " \"animal\": \"cat\",\n" + " \"vegetable\": 2,\n" @@ -195,7 +196,7 @@ public class ConstructingObjectParserTests extends ESTestCase { } public void testBadParamBeforeObjectBuilt() throws IOException { - XContentParser parser = XContentType.JSON.xContent().createParser( + XContentParser parser = createParser(JsonXContent.jsonXContent, "{\n" + " \"a\": \"supercalifragilisticexpialidocious\",\n" + " \"animal\": \"cat\"\n," @@ -220,7 +221,7 @@ public class ConstructingObjectParserTests extends ESTestCase { ConstructingObjectParser parser = new ConstructingObjectParser<>( "constructor_args_required", (a) -> new NoConstructorArgs()); try { - parser.apply(XContentType.JSON.xContent().createParser("{}"), null); + parser.apply(createParser(JsonXContent.jsonXContent, "{}"), null); fail("Expected AssertionError"); } catch (AssertionError e) { assertEquals("[constructor_args_required] must configure at least on constructor argument. If it doesn't have any it should " @@ -255,7 +256,7 @@ public class ConstructingObjectParserTests extends ESTestCase { parser.declareString(ctorArgOptional ? optionalConstructorArg() : constructorArg(), new ParseField("yeah")); // ctor arg first so we can test for the bug we found one time - XContentParser xcontent = XContentType.JSON.xContent().createParser( + XContentParser xcontent = createParser(JsonXContent.jsonXContent, "{\n" + " \"yeah\": \"!\",\n" + " \"foo\": \"foo\"\n" @@ -264,7 +265,7 @@ public class ConstructingObjectParserTests extends ESTestCase { assertTrue(result.fooSet); // and ctor arg second just in case - xcontent = XContentType.JSON.xContent().createParser( + xcontent = createParser(JsonXContent.jsonXContent, "{\n" + " \"foo\": \"foo\",\n" + " \"yeah\": \"!\"\n" @@ -274,7 +275,7 @@ public class ConstructingObjectParserTests extends ESTestCase { if (ctorArgOptional) { // and without the constructor arg if we've made it optional - xcontent = XContentType.JSON.xContent().createParser( + xcontent = createParser(JsonXContent.jsonXContent, "{\n" + " \"foo\": \"foo\"\n" + "}"); @@ -284,7 +285,7 @@ public class ConstructingObjectParserTests extends ESTestCase { } public void testIgnoreUnknownFields() throws IOException { - XContentParser parser = XContentType.JSON.xContent().createParser( + XContentParser parser = createParser(JsonXContent.jsonXContent, "{\n" + " \"test\" : \"foo\",\n" + " \"junk\" : 2\n" diff --git a/core/src/test/java/org/elasticsearch/common/xcontent/ObjectParserTests.java b/core/src/test/java/org/elasticsearch/common/xcontent/ObjectParserTests.java index 65b7bd8b656..46c0ba35723 100644 --- a/core/src/test/java/org/elasticsearch/common/xcontent/ObjectParserTests.java +++ b/core/src/test/java/org/elasticsearch/common/xcontent/ObjectParserTests.java @@ -25,6 +25,7 @@ import org.elasticsearch.common.ParsingException; import org.elasticsearch.common.xcontent.AbstractObjectParser.NoContextParser; import org.elasticsearch.common.xcontent.ObjectParser.NamedObjectParser; import org.elasticsearch.common.xcontent.ObjectParser.ValueType; +import org.elasticsearch.common.xcontent.json.JsonXContent; import org.elasticsearch.test.ESTestCase; import java.io.IOException; @@ -41,7 +42,7 @@ public class ObjectParserTests extends ESTestCase { private static final ParseFieldMatcherSupplier STRICT_PARSING = () -> ParseFieldMatcher.STRICT; public void testBasics() throws IOException { - XContentParser parser = XContentType.JSON.xContent().createParser( + XContentParser parser = createParser(JsonXContent.jsonXContent, "{\n" + " \"test\" : \"foo\",\n" + " \"test_number\" : 2,\n" @@ -97,17 +98,17 @@ public class ObjectParserTests extends ESTestCase { } public void testObjectOrDefault() throws IOException { - XContentParser parser = XContentType.JSON.xContent().createParser("{\"object\" : { \"test\": 2}}"); + XContentParser parser = createParser(JsonXContent.jsonXContent, "{\"object\" : { \"test\": 2}}"); ObjectParser objectParser = new ObjectParser<>("foo", StaticTestStruct::new); objectParser.declareInt(StaticTestStruct::setTest, new ParseField("test")); objectParser.declareObjectOrDefault(StaticTestStruct::setObject, objectParser, StaticTestStruct::new, new ParseField("object")); StaticTestStruct s = objectParser.parse(parser, STRICT_PARSING); assertEquals(s.object.test, 2); - parser = XContentType.JSON.xContent().createParser("{\"object\" : false }"); + parser = createParser(JsonXContent.jsonXContent, "{\"object\" : false }"); s = objectParser.parse(parser, STRICT_PARSING); assertNull(s.object); - parser = XContentType.JSON.xContent().createParser("{\"object\" : true }"); + parser = createParser(JsonXContent.jsonXContent, "{\"object\" : true }"); s = objectParser.parse(parser, STRICT_PARSING); assertNotNull(s.object); assertEquals(s.object.test, 0); @@ -191,7 +192,7 @@ public class ObjectParserTests extends ESTestCase { } public void testExceptions() throws IOException { - XContentParser parser = XContentType.JSON.xContent().createParser("{\"test\" : \"foo\"}"); + XContentParser parser = createParser(JsonXContent.jsonXContent, "{\"test\" : \"foo\"}"); class TestStruct { public void setTest(int test) { } @@ -208,7 +209,7 @@ public class ObjectParserTests extends ESTestCase { assertTrue(ex.getCause() instanceof NumberFormatException); } - parser = XContentType.JSON.xContent().createParser("{\"not_supported_field\" : \"foo\"}"); + parser = createParser(JsonXContent.jsonXContent, "{\"not_supported_field\" : \"foo\"}"); try { objectParser.parse(parser, s, STRICT_PARSING); fail("field not supported"); @@ -218,7 +219,7 @@ public class ObjectParserTests extends ESTestCase { } public void testDeprecationFail() throws IOException { - XContentParser parser = XContentType.JSON.xContent().createParser("{\"old_test\" : \"foo\"}"); + XContentParser parser = createParser(JsonXContent.jsonXContent, "{\"old_test\" : \"foo\"}"); class TestStruct { public String test; } @@ -235,13 +236,13 @@ public class ObjectParserTests extends ESTestCase { } assertNull(s.test); - parser = XContentType.JSON.xContent().createParser("{\"old_test\" : \"foo\"}"); + parser = createParser(JsonXContent.jsonXContent, "{\"old_test\" : \"foo\"}"); objectParser.parse(parser, s, () -> ParseFieldMatcher.EMPTY); assertEquals("foo", s.test); } public void testFailOnValueType() throws IOException { - XContentParser parser = XContentType.JSON.xContent().createParser("{\"numeric_value\" : false}"); + XContentParser parser = createParser(JsonXContent.jsonXContent, "{\"numeric_value\" : false}"); class TestStruct { public String test; } @@ -258,7 +259,7 @@ public class ObjectParserTests extends ESTestCase { } public void testParseNested() throws IOException { - XContentParser parser = XContentType.JSON.xContent().createParser("{ \"test\" : 1, \"object\" : { \"test\": 2}}"); + XContentParser parser = createParser(JsonXContent.jsonXContent, "{ \"test\" : 1, \"object\" : { \"test\": 2}}"); class TestStruct { public int test; TestStruct object; @@ -275,7 +276,7 @@ public class ObjectParserTests extends ESTestCase { } public void testParseNestedShortcut() throws IOException { - XContentParser parser = XContentType.JSON.xContent().createParser("{ \"test\" : 1, \"object\" : { \"test\": 2}}"); + XContentParser parser = createParser(JsonXContent.jsonXContent, "{ \"test\" : 1, \"object\" : { \"test\": 2}}"); ObjectParser objectParser = new ObjectParser<>("foo", StaticTestStruct::new); objectParser.declareInt(StaticTestStruct::setTest, new ParseField("test")); objectParser.declareObject(StaticTestStruct::setObject, objectParser, new ParseField("object")); @@ -285,7 +286,7 @@ public class ObjectParserTests extends ESTestCase { } public void testEmptyObject() throws IOException { - XContentParser parser = XContentType.JSON.xContent().createParser("{\"object\" : {}}"); + XContentParser parser = createParser(JsonXContent.jsonXContent, "{\"object\" : {}}"); ObjectParser objectParser = new ObjectParser<>("foo", StaticTestStruct::new); objectParser.declareObject(StaticTestStruct::setObject, objectParser, new ParseField("object")); StaticTestStruct s = objectParser.parse(parser, STRICT_PARSING); @@ -293,7 +294,7 @@ public class ObjectParserTests extends ESTestCase { } public void testEmptyObjectInArray() throws IOException { - XContentParser parser = XContentType.JSON.xContent().createParser("{\"object_array\" : [{}]}"); + XContentParser parser = createParser(JsonXContent.jsonXContent, "{\"object_array\" : [{}]}"); ObjectParser objectParser = new ObjectParser<>("foo", StaticTestStruct::new); objectParser.declareObjectArray(StaticTestStruct::setObjectArray, objectParser, new ParseField("object_array")); StaticTestStruct s = objectParser.parse(parser, STRICT_PARSING); @@ -330,7 +331,7 @@ public class ObjectParserTests extends ESTestCase { test = value; } } - XContentParser parser = XContentType.JSON.xContent().createParser("{ \"test\" : \"FOO\" }"); + XContentParser parser = createParser(JsonXContent.jsonXContent, "{ \"test\" : \"FOO\" }"); ObjectParser objectParser = new ObjectParser<>("foo"); objectParser.declareString((struct, value) -> struct.set(TestEnum.valueOf(value)), new ParseField("test")); TestStruct s = objectParser.parse(parser, new TestStruct(), STRICT_PARSING); @@ -374,7 +375,7 @@ public class ObjectParserTests extends ESTestCase { builder.field("boolean_field", nullValue); builder.field("string_or_null", nullValue ? null : "5"); builder.endObject(); - XContentParser parser = XContentType.JSON.xContent().createParser(builder.string()); + XContentParser parser = createParser(JsonXContent.jsonXContent, builder.string()); class TestStruct { int int_field; long long_field; @@ -466,7 +467,7 @@ public class ObjectParserTests extends ESTestCase { } public void testParseNamedObject() throws IOException { - XContentParser parser = XContentType.JSON.xContent().createParser( + XContentParser parser = createParser(JsonXContent.jsonXContent, "{\"named\": {\n" + " \"a\": {}" + "}}"); @@ -477,7 +478,7 @@ public class ObjectParserTests extends ESTestCase { } public void testParseNamedObjectInOrder() throws IOException { - XContentParser parser = XContentType.JSON.xContent().createParser( + XContentParser parser = createParser(JsonXContent.jsonXContent, "{\"named\": [\n" + " {\"a\": {}}" + "]}"); @@ -488,7 +489,7 @@ public class ObjectParserTests extends ESTestCase { } public void testParseNamedObjectTwoFieldsInArray() throws IOException { - XContentParser parser = XContentType.JSON.xContent().createParser( + XContentParser parser = createParser(JsonXContent.jsonXContent, "{\"named\": [\n" + " {\"a\": {}, \"b\": {}}" + "]}"); @@ -500,7 +501,7 @@ public class ObjectParserTests extends ESTestCase { } public void testParseNamedObjectNoFieldsInArray() throws IOException { - XContentParser parser = XContentType.JSON.xContent().createParser( + XContentParser parser = createParser(JsonXContent.jsonXContent, "{\"named\": [\n" + " {}" + "]}"); @@ -512,7 +513,7 @@ public class ObjectParserTests extends ESTestCase { } public void testParseNamedObjectJunkInArray() throws IOException { - XContentParser parser = XContentType.JSON.xContent().createParser( + XContentParser parser = createParser(JsonXContent.jsonXContent, "{\"named\": [\n" + " \"junk\"" + "]}"); @@ -524,7 +525,7 @@ public class ObjectParserTests extends ESTestCase { } public void testParseNamedObjectInOrderNotSupported() throws IOException { - XContentParser parser = XContentType.JSON.xContent().createParser( + XContentParser parser = createParser(JsonXContent.jsonXContent, "{\"named\": [\n" + " {\"a\": {}}" + "]}"); @@ -549,7 +550,7 @@ public class ObjectParserTests extends ESTestCase { } b.endObject(); b = shuffleXContent(b); - XContentParser parser = XContentType.JSON.xContent().createParser(b.bytes()); + XContentParser parser = createParser(JsonXContent.jsonXContent, b.bytes()); class TestStruct { public String test; @@ -573,7 +574,7 @@ public class ObjectParserTests extends ESTestCase { } b.endObject(); b = shuffleXContent(b); - XContentParser parser = XContentType.JSON.xContent().createParser(b.bytes()); + XContentParser parser = createParser(JsonXContent.jsonXContent, b.bytes()); class TestStruct { public String test; @@ -601,7 +602,7 @@ public class ObjectParserTests extends ESTestCase { } b.endObject(); b = shuffleXContent(b); - XContentParser parser = XContentType.JSON.xContent().createParser(b.bytes()); + XContentParser parser = createParser(JsonXContent.jsonXContent, b.bytes()); class TestStruct { public String test; } diff --git a/core/src/test/java/org/elasticsearch/common/xcontent/XContentParserTests.java b/core/src/test/java/org/elasticsearch/common/xcontent/XContentParserTests.java index cbcff431c2b..a7257fbca8b 100644 --- a/core/src/test/java/org/elasticsearch/common/xcontent/XContentParserTests.java +++ b/core/src/test/java/org/elasticsearch/common/xcontent/XContentParserTests.java @@ -20,6 +20,7 @@ package org.elasticsearch.common.xcontent; import org.elasticsearch.ElasticsearchParseException; +import org.elasticsearch.common.xcontent.json.JsonXContent; import org.elasticsearch.test.ESTestCase; import java.io.IOException; @@ -56,8 +57,8 @@ public class XContentParserTests extends ESTestCase { } @SuppressWarnings("unchecked") - private static List readList(String source) throws IOException { - try (XContentParser parser = XContentType.JSON.xContent().createParser(source)) { + private List readList(String source) throws IOException { + try (XContentParser parser = createParser(JsonXContent.jsonXContent, source)) { XContentParser.Token token = parser.nextToken(); assertThat(token, equalTo(XContentParser.Token.START_OBJECT)); token = parser.nextToken(); @@ -89,9 +90,8 @@ public class XContentParserTests extends ESTestCase { assertThat(map.size(), equalTo(0)); } - @SuppressWarnings("unchecked") - private static Map readMapStrings(String source) throws IOException { - try (XContentParser parser = XContentType.JSON.xContent().createParser(source)) { + private Map readMapStrings(String source) throws IOException { + try (XContentParser parser = createParser(JsonXContent.jsonXContent, source)) { XContentParser.Token token = parser.nextToken(); assertThat(token, equalTo(XContentParser.Token.START_OBJECT)); token = parser.nextToken(); diff --git a/core/src/test/java/org/elasticsearch/common/xcontent/builder/XContentBuilderTests.java b/core/src/test/java/org/elasticsearch/common/xcontent/builder/XContentBuilderTests.java index f0b1b7f5e1b..d3a5e44a89e 100644 --- a/core/src/test/java/org/elasticsearch/common/xcontent/builder/XContentBuilderTests.java +++ b/core/src/test/java/org/elasticsearch/common/xcontent/builder/XContentBuilderTests.java @@ -29,6 +29,7 @@ import org.elasticsearch.common.xcontent.XContentFactory; import org.elasticsearch.common.xcontent.XContentGenerator; import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.common.xcontent.XContentType; +import org.elasticsearch.common.xcontent.json.JsonXContent; import org.elasticsearch.test.ESTestCase; import java.io.ByteArrayOutputStream; @@ -213,7 +214,7 @@ public class XContentBuilderTests extends ESTestCase { builder.field("fakefield", terms).endObject().endObject().endObject(); - XContentParser parser = XContentFactory.xContent(XContentType.JSON).createParser(builder.bytes()); + XContentParser parser = createParser(JsonXContent.jsonXContent, builder.bytes()); XContentBuilder filterBuilder = null; XContentParser.Token token; @@ -235,7 +236,7 @@ public class XContentBuilderTests extends ESTestCase { } assertNotNull(filterBuilder); - parser = XContentFactory.xContent(XContentType.JSON).createParser(filterBuilder.bytes()); + parser = createParser(JsonXContent.jsonXContent, filterBuilder.bytes()); assertThat(parser.nextToken(), equalTo(XContentParser.Token.START_OBJECT)); assertThat(parser.nextToken(), equalTo(XContentParser.Token.FIELD_NAME)); assertThat(parser.currentName(), equalTo("terms")); diff --git a/core/src/test/java/org/elasticsearch/common/xcontent/cbor/JsonVsCborTests.java b/core/src/test/java/org/elasticsearch/common/xcontent/cbor/JsonVsCborTests.java index 1b3617bebb4..e165425400e 100644 --- a/core/src/test/java/org/elasticsearch/common/xcontent/cbor/JsonVsCborTests.java +++ b/core/src/test/java/org/elasticsearch/common/xcontent/cbor/JsonVsCborTests.java @@ -24,6 +24,7 @@ import org.elasticsearch.common.xcontent.XContentFactory; import org.elasticsearch.common.xcontent.XContentGenerator; import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.common.xcontent.XContentType; +import org.elasticsearch.common.xcontent.json.JsonXContent; import org.elasticsearch.test.ESTestCase; import java.io.IOException; @@ -62,7 +63,7 @@ public class JsonVsCborTests extends ESTestCase { xsonGen.close(); jsonGen.close(); - verifySameTokens(createParser(jsonOs.bytes()), createParser(xsonOs.bytes())); + verifySameTokens(createParser(JsonXContent.jsonXContent, jsonOs.bytes()), createParser(CborXContent.cborXContent, xsonOs.bytes())); } private void verifySameTokens(XContentParser parser1, XContentParser parser2) throws IOException { diff --git a/core/src/test/java/org/elasticsearch/common/xcontent/smile/JsonVsSmileTests.java b/core/src/test/java/org/elasticsearch/common/xcontent/smile/JsonVsSmileTests.java index 0ecb41c78f9..47913a5481e 100644 --- a/core/src/test/java/org/elasticsearch/common/xcontent/smile/JsonVsSmileTests.java +++ b/core/src/test/java/org/elasticsearch/common/xcontent/smile/JsonVsSmileTests.java @@ -24,6 +24,7 @@ import org.elasticsearch.common.xcontent.XContentFactory; import org.elasticsearch.common.xcontent.XContentGenerator; import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.common.xcontent.XContentType; +import org.elasticsearch.common.xcontent.json.JsonXContent; import org.elasticsearch.test.ESTestCase; import java.io.IOException; @@ -62,7 +63,8 @@ public class JsonVsSmileTests extends ESTestCase { xsonGen.close(); jsonGen.close(); - verifySameTokens(createParser(jsonOs.bytes()), createParser(xsonOs.bytes())); + verifySameTokens(createParser(JsonXContent.jsonXContent, jsonOs.bytes()), + createParser(SmileXContent.smileXContent, xsonOs.bytes())); } private void verifySameTokens(XContentParser parser1, XContentParser parser2) throws IOException { diff --git a/core/src/test/java/org/elasticsearch/common/xcontent/support/XContentMapValuesTests.java b/core/src/test/java/org/elasticsearch/common/xcontent/support/XContentMapValuesTests.java index 2a0bf826896..58c4f630bb6 100644 --- a/core/src/test/java/org/elasticsearch/common/xcontent/support/XContentMapValuesTests.java +++ b/core/src/test/java/org/elasticsearch/common/xcontent/support/XContentMapValuesTests.java @@ -26,6 +26,7 @@ import org.elasticsearch.common.xcontent.XContentFactory; import org.elasticsearch.common.xcontent.XContentHelper; import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.common.xcontent.XContentType; +import org.elasticsearch.common.xcontent.json.JsonXContent; import org.elasticsearch.test.ESTestCase; import org.hamcrest.Matchers; @@ -53,7 +54,7 @@ public class XContentMapValuesTests extends ESTestCase { .endObject(); Map source; - try (XContentParser parser = createParser(builder.string())) { + try (XContentParser parser = createParser(JsonXContent.jsonXContent, builder.string())) { source = parser.map(); } Map filter = XContentMapValues.filter(source, new String[]{"test1"}, Strings.EMPTY_ARRAY); @@ -81,7 +82,7 @@ public class XContentMapValuesTests extends ESTestCase { .field("test1", "value1") .endObject(); - try (XContentParser parser = createParser(builder.string())) { + try (XContentParser parser = createParser(JsonXContent.jsonXContent, builder.string())) { source = parser.map(); } filter = XContentMapValues.filter(source, new String[]{"path1"}, Strings.EMPTY_ARRAY); @@ -107,7 +108,7 @@ public class XContentMapValuesTests extends ESTestCase { .endObject(); Map map; - try (XContentParser parser = createParser(builder.string())) { + try (XContentParser parser = createParser(JsonXContent.jsonXContent, builder.string())) { map = parser.map(); } assertThat(XContentMapValues.extractValue("test", map).toString(), equalTo("value")); @@ -118,7 +119,7 @@ public class XContentMapValuesTests extends ESTestCase { .startObject("path1").startObject("path2").field("test", "value").endObject().endObject() .endObject(); - try (XContentParser parser = createParser(builder.string())) { + try (XContentParser parser = createParser(JsonXContent.jsonXContent, builder.string())) { map = parser.map(); } assertThat(XContentMapValues.extractValue("path1.path2.test", map).toString(), equalTo("value")); @@ -140,7 +141,7 @@ public class XContentMapValuesTests extends ESTestCase { .startObject("path1").array("test", "value1", "value2").endObject() .endObject(); - try (XContentParser parser = createParser(builder.string())) { + try (XContentParser parser = createParser(JsonXContent.jsonXContent, builder.string())) { map = parser.map(); } @@ -159,7 +160,7 @@ public class XContentMapValuesTests extends ESTestCase { .endObject() .endObject(); - try (XContentParser parser = createParser(builder.string())) { + try (XContentParser parser = createParser(JsonXContent.jsonXContent, builder.string())) { map = parser.map(); } @@ -175,7 +176,7 @@ public class XContentMapValuesTests extends ESTestCase { builder = XContentFactory.jsonBuilder().startObject() .field("xxx.yyy", "value") .endObject(); - try (XContentParser parser = createParser(builder.string())) { + try (XContentParser parser = createParser(JsonXContent.jsonXContent, builder.string())) { map = parser.map(); } assertThat(XContentMapValues.extractValue("xxx.yyy", map).toString(), equalTo("value")); @@ -184,7 +185,7 @@ public class XContentMapValuesTests extends ESTestCase { .startObject("path1.xxx").startObject("path2.yyy").field("test", "value").endObject().endObject() .endObject(); - try (XContentParser parser = createParser(builder.string())) { + try (XContentParser parser = createParser(JsonXContent.jsonXContent, builder.string())) { map = parser.map(); } assertThat(XContentMapValues.extractValue("path1.xxx.path2.yyy.test", map).toString(), equalTo("value")); @@ -196,7 +197,7 @@ public class XContentMapValuesTests extends ESTestCase { .endObject(); Map map; - try (XContentParser parser = createParser(builder.string())) { + try (XContentParser parser = createParser(JsonXContent.jsonXContent, builder.string())) { map = parser.map(); } assertThat(XContentMapValues.extractRawValues("test", map).get(0).toString(), equalTo("value")); @@ -205,7 +206,7 @@ public class XContentMapValuesTests extends ESTestCase { .field("test.me", "value") .endObject(); - try (XContentParser parser = createParser(builder.string())) { + try (XContentParser parser = createParser(JsonXContent.jsonXContent, builder.string())) { map = parser.map(); } assertThat(XContentMapValues.extractRawValues("test.me", map).get(0).toString(), equalTo("value")); @@ -214,7 +215,7 @@ public class XContentMapValuesTests extends ESTestCase { .startObject("path1").startObject("path2").field("test", "value").endObject().endObject() .endObject(); - try (XContentParser parser = createParser(builder.string())) { + try (XContentParser parser = createParser(JsonXContent.jsonXContent, builder.string())) { map = parser.map(); } assertThat(XContentMapValues.extractRawValues("path1.path2.test", map).get(0).toString(), equalTo("value")); @@ -223,7 +224,7 @@ public class XContentMapValuesTests extends ESTestCase { .startObject("path1.xxx").startObject("path2.yyy").field("test", "value").endObject().endObject() .endObject(); - try (XContentParser parser = createParser(builder.string())) { + try (XContentParser parser = createParser(JsonXContent.jsonXContent, builder.string())) { map = parser.map(); } assertThat(XContentMapValues.extractRawValues("path1.xxx.path2.yyy.test", map).get(0).toString(), equalTo("value")); @@ -475,7 +476,7 @@ public class XContentMapValuesTests extends ESTestCase { .startArray("some_array") .endArray().endObject(); - try (XContentParser parser = createParser(builder.string())) { + try (XContentParser parser = createParser(JsonXContent.jsonXContent, builder.string())) { assertEquals(XContentParser.Token.START_OBJECT, parser.nextToken()); assertEquals(XContentParser.Token.FIELD_NAME, parser.nextToken()); assertEquals("some_array", parser.currentName()); @@ -495,7 +496,7 @@ public class XContentMapValuesTests extends ESTestCase { .value(0) .endArray().endObject(); - try (XContentParser parser = createParser(builder.string())) { + try (XContentParser parser = createParser(JsonXContent.jsonXContent, builder.string())) { assertEquals(XContentParser.Token.START_OBJECT, parser.nextToken()); assertEquals(XContentParser.Token.FIELD_NAME, parser.nextToken()); assertEquals("some_array", parser.currentName()); @@ -515,7 +516,7 @@ public class XContentMapValuesTests extends ESTestCase { .startArray().value(2).endArray() .endArray().endObject(); - try (XContentParser parser = createParser(builder.string())) { + try (XContentParser parser = createParser(JsonXContent.jsonXContent, builder.string())) { assertEquals(XContentParser.Token.START_OBJECT, parser.nextToken()); assertEquals(XContentParser.Token.FIELD_NAME, parser.nextToken()); assertEquals("some_array", parser.currentName()); @@ -536,7 +537,7 @@ public class XContentMapValuesTests extends ESTestCase { .startObject().endObject() .endArray().endObject(); - try (XContentParser parser = createParser(builder.string())) { + try (XContentParser parser = createParser(JsonXContent.jsonXContent, builder.string())) { assertEquals(XContentParser.Token.START_OBJECT, parser.nextToken()); assertEquals(XContentParser.Token.FIELD_NAME, parser.nextToken()); assertEquals("some_array", parser.currentName()); diff --git a/core/src/test/java/org/elasticsearch/index/IndexTests.java b/core/src/test/java/org/elasticsearch/index/IndexTests.java index ffa12a1943d..d55630b1357 100644 --- a/core/src/test/java/org/elasticsearch/index/IndexTests.java +++ b/core/src/test/java/org/elasticsearch/index/IndexTests.java @@ -24,7 +24,6 @@ import org.elasticsearch.common.UUIDs; import org.elasticsearch.common.xcontent.ToXContent; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentParser; -import org.elasticsearch.common.xcontent.XContentType; import org.elasticsearch.common.xcontent.json.JsonXContent; import org.elasticsearch.test.ESTestCase; @@ -56,7 +55,7 @@ public class IndexTests extends ESTestCase { final Index original = new Index(name, uuid); final XContentBuilder builder = JsonXContent.contentBuilder(); original.toXContent(builder, ToXContent.EMPTY_PARAMS); - XContentParser parser = XContentType.JSON.xContent().createParser(builder.bytes()); + XContentParser parser = createParser(JsonXContent.jsonXContent, builder.bytes()); parser.nextToken(); // the beginning of the parser assertThat(Index.fromXContent(parser), equalTo(original)); } diff --git a/core/src/test/java/org/elasticsearch/index/mapper/CompletionFieldMapperTests.java b/core/src/test/java/org/elasticsearch/index/mapper/CompletionFieldMapperTests.java index 78f9f355b0c..802aee7f482 100644 --- a/core/src/test/java/org/elasticsearch/index/mapper/CompletionFieldMapperTests.java +++ b/core/src/test/java/org/elasticsearch/index/mapper/CompletionFieldMapperTests.java @@ -133,7 +133,7 @@ public class CompletionFieldMapperTests extends ESSingleNodeTestCase { XContentBuilder builder = jsonBuilder().startObject(); completionFieldMapper.toXContent(builder, ToXContent.EMPTY_PARAMS).endObject(); builder.close(); - Map serializedMap = JsonXContent.jsonXContent.createParser(builder.bytes()).map(); + Map serializedMap = createParser(JsonXContent.jsonXContent, builder.bytes()).map(); Map configMap = (Map) serializedMap.get("completion"); assertThat(configMap.get("analyzer").toString(), is("simple")); assertThat(configMap.get("search_analyzer").toString(), is("standard")); diff --git a/core/src/test/java/org/elasticsearch/index/mapper/CopyToMapperTests.java b/core/src/test/java/org/elasticsearch/index/mapper/CopyToMapperTests.java index 0133e7e86c5..74ef9d5de7a 100644 --- a/core/src/test/java/org/elasticsearch/index/mapper/CopyToMapperTests.java +++ b/core/src/test/java/org/elasticsearch/index/mapper/CopyToMapperTests.java @@ -76,7 +76,7 @@ public class CopyToMapperTests extends ESSingleNodeTestCase { stringFieldMapper.toXContent(builder, ToXContent.EMPTY_PARAMS).endObject(); builder.close(); Map serializedMap; - try (XContentParser parser = JsonXContent.jsonXContent.createParser(builder.bytes())) { + try (XContentParser parser = createParser(JsonXContent.jsonXContent, builder.bytes())) { serializedMap = parser.map(); } Map copyTestMap = (Map) serializedMap.get("copy_test"); diff --git a/core/src/test/java/org/elasticsearch/index/mapper/SourceFieldMapperTests.java b/core/src/test/java/org/elasticsearch/index/mapper/SourceFieldMapperTests.java index 8aa3d25aebe..adf05b9b338 100644 --- a/core/src/test/java/org/elasticsearch/index/mapper/SourceFieldMapperTests.java +++ b/core/src/test/java/org/elasticsearch/index/mapper/SourceFieldMapperTests.java @@ -20,18 +20,15 @@ package org.elasticsearch.index.mapper; import org.apache.lucene.index.IndexableField; -import org.elasticsearch.Version; -import org.elasticsearch.cluster.metadata.IndexMetaData; import org.elasticsearch.common.bytes.BytesArray; import org.elasticsearch.common.compress.CompressedXContent; -import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.xcontent.XContentFactory; import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.common.xcontent.XContentType; +import org.elasticsearch.common.xcontent.json.JsonXContent; import org.elasticsearch.plugins.Plugin; import org.elasticsearch.test.ESSingleNodeTestCase; import org.elasticsearch.test.InternalSettingsPlugin; -import org.elasticsearch.test.VersionUtils; import java.io.IOException; import java.util.Collection; @@ -82,7 +79,7 @@ public class SourceFieldMapperTests extends ESSingleNodeTestCase { IndexableField sourceField = doc.rootDoc().getField("_source"); Map sourceAsMap; - try (XContentParser parser = XContentFactory.xContent(XContentType.JSON).createParser(new BytesArray(sourceField.binaryValue()))) { + try (XContentParser parser = createParser(JsonXContent.jsonXContent, new BytesArray(sourceField.binaryValue()))) { sourceAsMap = parser.map(); } assertThat(sourceAsMap.containsKey("path1"), equalTo(true)); @@ -103,7 +100,7 @@ public class SourceFieldMapperTests extends ESSingleNodeTestCase { IndexableField sourceField = doc.rootDoc().getField("_source"); Map sourceAsMap; - try (XContentParser parser = XContentFactory.xContent(XContentType.JSON).createParser(new BytesArray(sourceField.binaryValue()))) { + try (XContentParser parser = createParser(JsonXContent.jsonXContent, new BytesArray(sourceField.binaryValue()))) { sourceAsMap = parser.map(); } assertThat(sourceAsMap.containsKey("path1"), equalTo(false)); diff --git a/core/src/test/java/org/elasticsearch/index/query/BoolQueryBuilderTests.java b/core/src/test/java/org/elasticsearch/index/query/BoolQueryBuilderTests.java index 4987ef2ece7..5f0193ce951 100644 --- a/core/src/test/java/org/elasticsearch/index/query/BoolQueryBuilderTests.java +++ b/core/src/test/java/org/elasticsearch/index/query/BoolQueryBuilderTests.java @@ -26,7 +26,6 @@ import org.apache.lucene.search.MatchAllDocsQuery; import org.apache.lucene.search.Query; import org.elasticsearch.common.ParseFieldMatcher; import org.elasticsearch.common.ParsingException; -import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentFactory; import org.elasticsearch.common.xcontent.XContentType; @@ -173,25 +172,25 @@ public class BoolQueryBuilderTests extends AbstractQueryTestCase context.parseTopLevelQueryBuilder()); assertEquals("request does not support [foo]", exception.getMessage()); @@ -92,7 +91,7 @@ public class QueryParseContextTests extends ESTestCase { public void testParseInnerQueryBuilder() throws IOException { QueryBuilder query = new MatchQueryBuilder("foo", "bar"); String source = query.toString(); - try (XContentParser parser = XContentFactory.xContent(source).createParser(source)) { + try (XContentParser parser = createParser(JsonXContent.jsonXContent, source)) { QueryParseContext context = new QueryParseContext(indicesQueriesRegistry, parser, ParseFieldMatcher.STRICT); QueryBuilder actual = context.parseInnerQueryBuilder(); assertEquals(query, actual); @@ -101,7 +100,7 @@ public class QueryParseContextTests extends ESTestCase { public void testParseInnerQueryBuilderExceptions() throws IOException { String source = "{ \"foo\": \"bar\" }"; - try (XContentParser parser = JsonXContent.jsonXContent.createParser(source)) { + try (XContentParser parser = createParser(JsonXContent.jsonXContent, source)) { parser.nextToken(); parser.nextToken(); // don't start with START_OBJECT to provoke exception QueryParseContext context = new QueryParseContext(indicesQueriesRegistry, parser, ParseFieldMatcher.STRICT); @@ -110,21 +109,21 @@ public class QueryParseContextTests extends ESTestCase { } source = "{}"; - try (XContentParser parser = JsonXContent.jsonXContent.createParser(source)) { + try (XContentParser parser = createParser(JsonXContent.jsonXContent, source)) { QueryParseContext context = new QueryParseContext(indicesQueriesRegistry, parser, ParseFieldMatcher.EMPTY); IllegalArgumentException exception = expectThrows(IllegalArgumentException.class, () -> context.parseInnerQueryBuilder()); assertEquals("query malformed, empty clause found at [1:2]", exception.getMessage()); } source = "{ \"foo\" : \"bar\" }"; - try (XContentParser parser = JsonXContent.jsonXContent.createParser(source)) { + try (XContentParser parser = createParser(JsonXContent.jsonXContent, source)) { QueryParseContext context = new QueryParseContext(indicesQueriesRegistry, parser, ParseFieldMatcher.STRICT); ParsingException exception = expectThrows(ParsingException.class, () -> context.parseInnerQueryBuilder()); assertEquals("[foo] query malformed, no start_object after query name", exception.getMessage()); } source = "{ \"foo\" : {} }"; - try (XContentParser parser = JsonXContent.jsonXContent.createParser(source)) { + try (XContentParser parser = createParser(JsonXContent.jsonXContent, source)) { QueryParseContext context = new QueryParseContext(indicesQueriesRegistry, parser, ParseFieldMatcher.STRICT); ParsingException exception = expectThrows(ParsingException.class, () -> context.parseInnerQueryBuilder()); assertEquals("no [query] registered for [foo]", exception.getMessage()); diff --git a/core/src/test/java/org/elasticsearch/index/query/functionscore/FunctionScoreQueryBuilderTests.java b/core/src/test/java/org/elasticsearch/index/query/functionscore/FunctionScoreQueryBuilderTests.java index 7079ccab4cd..5ae26565917 100644 --- a/core/src/test/java/org/elasticsearch/index/query/functionscore/FunctionScoreQueryBuilderTests.java +++ b/core/src/test/java/org/elasticsearch/index/query/functionscore/FunctionScoreQueryBuilderTests.java @@ -27,6 +27,7 @@ import org.apache.lucene.search.Query; import org.apache.lucene.search.TermQuery; import org.elasticsearch.common.ParseFieldMatcher; import org.elasticsearch.common.ParsingException; +import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.geo.GeoPoint; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.lucene.search.function.CombineFunction; @@ -35,6 +36,7 @@ import org.elasticsearch.common.lucene.search.function.FiltersFunctionScoreQuery import org.elasticsearch.common.lucene.search.function.FunctionScoreQuery; import org.elasticsearch.common.lucene.search.function.WeightFactorFunction; import org.elasticsearch.common.unit.DistanceUnit; +import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.common.xcontent.XContentType; import org.elasticsearch.index.query.AbstractQueryBuilder; import org.elasticsearch.index.query.MatchAllQueryBuilder; @@ -360,7 +362,10 @@ public class FunctionScoreQueryBuilderTests extends AbstractQueryTestCase GeoUtils.parseGeoPoint(parser)); assertThat(e.getMessage(), is("field must be either [lat], [lon] or [geohash]")); @@ -122,7 +122,7 @@ public class GeoPointParsingTests extends ESTestCase { content.field("lat", 0).field("geohash", stringEncode(0d, 0d)); content.endObject(); - XContentParser parser = JsonXContent.jsonXContent.createParser(content.bytes()); + XContentParser parser = createParser(JsonXContent.jsonXContent, content.bytes()); parser.nextToken(); Exception e = expectThrows(ElasticsearchParseException.class, () -> GeoUtils.parseGeoPoint(parser)); @@ -135,7 +135,7 @@ public class GeoPointParsingTests extends ESTestCase { content.field("lon", 0).field("geohash", stringEncode(0d, 0d)); content.endObject(); - XContentParser parser = JsonXContent.jsonXContent.createParser(content.bytes()); + XContentParser parser = createParser(JsonXContent.jsonXContent, content.bytes()); parser.nextToken(); Exception e = expectThrows(ElasticsearchParseException.class, () -> GeoUtils.parseGeoPoint(parser)); @@ -148,43 +148,43 @@ public class GeoPointParsingTests extends ESTestCase { content.field("lon", 0).field("lat", 0).field("test", 0); content.endObject(); - XContentParser parser = JsonXContent.jsonXContent.createParser(content.bytes()); + XContentParser parser = createParser(JsonXContent.jsonXContent, content.bytes()); parser.nextToken(); Exception e = expectThrows(ElasticsearchParseException.class, () -> GeoUtils.parseGeoPoint(parser)); assertThat(e.getMessage(), is("field must be either [lat], [lon] or [geohash]")); } - private static XContentParser objectLatLon(double lat, double lon) throws IOException { + private XContentParser objectLatLon(double lat, double lon) throws IOException { XContentBuilder content = JsonXContent.contentBuilder(); content.startObject(); content.field("lat", lat).field("lon", lon); content.endObject(); - XContentParser parser = JsonXContent.jsonXContent.createParser(content.bytes()); + XContentParser parser = createParser(JsonXContent.jsonXContent, content.bytes()); parser.nextToken(); return parser; } - private static XContentParser arrayLatLon(double lat, double lon) throws IOException { + private XContentParser arrayLatLon(double lat, double lon) throws IOException { XContentBuilder content = JsonXContent.contentBuilder(); content.startArray().value(lon).value(lat).endArray(); - XContentParser parser = JsonXContent.jsonXContent.createParser(content.bytes()); + XContentParser parser = createParser(JsonXContent.jsonXContent, content.bytes()); parser.nextToken(); return parser; } - private static XContentParser stringLatLon(double lat, double lon) throws IOException { + private XContentParser stringLatLon(double lat, double lon) throws IOException { XContentBuilder content = JsonXContent.contentBuilder(); content.value(Double.toString(lat) + ", " + Double.toString(lon)); - XContentParser parser = JsonXContent.jsonXContent.createParser(content.bytes()); + XContentParser parser = createParser(JsonXContent.jsonXContent, content.bytes()); parser.nextToken(); return parser; } - private static XContentParser geohash(double lat, double lon) throws IOException { + private XContentParser geohash(double lat, double lon) throws IOException { XContentBuilder content = JsonXContent.contentBuilder(); content.value(stringEncode(lon, lat)); - XContentParser parser = JsonXContent.jsonXContent.createParser(content.bytes()); + XContentParser parser = createParser(JsonXContent.jsonXContent, content.bytes()); parser.nextToken(); return parser; } diff --git a/core/src/test/java/org/elasticsearch/index/snapshots/blobstore/FileInfoTests.java b/core/src/test/java/org/elasticsearch/index/snapshots/blobstore/FileInfoTests.java index 5968ff25e98..2733d80e36a 100644 --- a/core/src/test/java/org/elasticsearch/index/snapshots/blobstore/FileInfoTests.java +++ b/core/src/test/java/org/elasticsearch/index/snapshots/blobstore/FileInfoTests.java @@ -28,11 +28,13 @@ import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentFactory; import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.common.xcontent.XContentType; +import org.elasticsearch.common.xcontent.json.JsonXContent; import org.elasticsearch.index.snapshots.blobstore.BlobStoreIndexShardSnapshot.FileInfo; import org.elasticsearch.index.store.StoreFileMetaData; import org.elasticsearch.test.ESTestCase; import java.io.IOException; + import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.is; @@ -54,7 +56,7 @@ public class FileInfoTests extends ESTestCase { byte[] xcontent = BytesReference.toBytes(shuffleXContent(builder).bytes()); final BlobStoreIndexShardSnapshot.FileInfo parsedInfo; - try (XContentParser parser = XContentFactory.xContent(XContentType.JSON).createParser(xcontent)) { + try (XContentParser parser = createParser(JsonXContent.jsonXContent, xcontent)) { parser.nextToken(); parsedInfo = BlobStoreIndexShardSnapshot.FileInfo.fromXContent(parser); } @@ -115,7 +117,7 @@ public class FileInfoTests extends ESTestCase { if (failure == null) { // No failures should read as usual final BlobStoreIndexShardSnapshot.FileInfo parsedInfo; - try (XContentParser parser = XContentFactory.xContent(XContentType.JSON).createParser(xContent)) { + try (XContentParser parser = createParser(JsonXContent.jsonXContent, xContent)) { parser.nextToken(); parsedInfo = BlobStoreIndexShardSnapshot.FileInfo.fromXContent(parser); } @@ -126,14 +128,13 @@ public class FileInfoTests extends ESTestCase { assertEquals("666", parsedInfo.metadata().checksum()); assertEquals(Version.LATEST, parsedInfo.metadata().writtenBy()); } else { - try (XContentParser parser = XContentFactory.xContent(XContentType.JSON).createParser(xContent)) { + try (XContentParser parser = createParser(JsonXContent.jsonXContent, xContent)) { parser.nextToken(); BlobStoreIndexShardSnapshot.FileInfo.fromXContent(parser); fail("Should have failed with [" + failure + "]"); } catch (ElasticsearchParseException ex) { assertThat(ex.getMessage(), containsString(failure)); } - } } } diff --git a/core/src/test/java/org/elasticsearch/repositories/IndexIdTests.java b/core/src/test/java/org/elasticsearch/repositories/IndexIdTests.java index 30002d54a6b..9c39061419c 100644 --- a/core/src/test/java/org/elasticsearch/repositories/IndexIdTests.java +++ b/core/src/test/java/org/elasticsearch/repositories/IndexIdTests.java @@ -24,7 +24,6 @@ import org.elasticsearch.common.io.stream.BytesStreamOutput; import org.elasticsearch.common.xcontent.ToXContent; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentParser; -import org.elasticsearch.common.xcontent.XContentType; import org.elasticsearch.common.xcontent.json.JsonXContent; import org.elasticsearch.test.ESTestCase; @@ -69,7 +68,7 @@ public class IndexIdTests extends ESTestCase { IndexId indexId = new IndexId(randomAsciiOfLength(8), UUIDs.randomBase64UUID()); XContentBuilder builder = JsonXContent.contentBuilder(); indexId.toXContent(builder, ToXContent.EMPTY_PARAMS); - XContentParser parser = XContentType.JSON.xContent().createParser(builder.bytes()); + XContentParser parser = createParser(JsonXContent.jsonXContent, builder.bytes()); assertEquals(XContentParser.Token.START_OBJECT, parser.nextToken()); String name = null; String id = null; diff --git a/core/src/test/java/org/elasticsearch/repositories/RepositoryDataTests.java b/core/src/test/java/org/elasticsearch/repositories/RepositoryDataTests.java index 1fb34249fd2..c1ac1abfdb5 100644 --- a/core/src/test/java/org/elasticsearch/repositories/RepositoryDataTests.java +++ b/core/src/test/java/org/elasticsearch/repositories/RepositoryDataTests.java @@ -23,7 +23,6 @@ import org.elasticsearch.common.UUIDs; import org.elasticsearch.common.xcontent.ToXContent; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentParser; -import org.elasticsearch.common.xcontent.XContentType; import org.elasticsearch.common.xcontent.json.JsonXContent; import org.elasticsearch.snapshots.SnapshotId; import org.elasticsearch.test.ESTestCase; @@ -56,7 +55,7 @@ public class RepositoryDataTests extends ESTestCase { RepositoryData repositoryData = generateRandomRepoData(); XContentBuilder builder = JsonXContent.contentBuilder(); repositoryData.toXContent(builder, ToXContent.EMPTY_PARAMS); - XContentParser parser = XContentType.JSON.xContent().createParser(builder.bytes()); + XContentParser parser = createParser(JsonXContent.jsonXContent, builder.bytes()); assertEquals(repositoryData, RepositoryData.fromXContent(parser)); } diff --git a/core/src/test/java/org/elasticsearch/search/aggregations/AggregationCollectorTests.java b/core/src/test/java/org/elasticsearch/search/aggregations/AggregationCollectorTests.java index 8b632a3de7d..c81ec476b86 100644 --- a/core/src/test/java/org/elasticsearch/search/aggregations/AggregationCollectorTests.java +++ b/core/src/test/java/org/elasticsearch/search/aggregations/AggregationCollectorTests.java @@ -62,7 +62,7 @@ public class AggregationCollectorTests extends ESSingleNodeTestCase { private boolean needsScores(IndexService index, String agg) throws IOException { AggregatorParsers parser = getInstanceFromNode(SearchRequestParsers.class).aggParsers; IndicesQueriesRegistry queriesRegistry = getInstanceFromNode(IndicesQueriesRegistry.class); - XContentParser aggParser = JsonXContent.jsonXContent.createParser(agg); + XContentParser aggParser = createParser(JsonXContent.jsonXContent, agg); QueryParseContext parseContext = new QueryParseContext(queriesRegistry, aggParser, ParseFieldMatcher.STRICT); aggParser.nextToken(); SearchContext context = createSearchContext(index); diff --git a/core/src/test/java/org/elasticsearch/search/aggregations/bucket/geogrid/GeoHashGridParserTests.java b/core/src/test/java/org/elasticsearch/search/aggregations/bucket/geogrid/GeoHashGridParserTests.java index c75fac3606e..48b393ef46d 100644 --- a/core/src/test/java/org/elasticsearch/search/aggregations/bucket/geogrid/GeoHashGridParserTests.java +++ b/core/src/test/java/org/elasticsearch/search/aggregations/bucket/geogrid/GeoHashGridParserTests.java @@ -33,7 +33,7 @@ public class GeoHashGridParserTests extends ESTestCase { public void testParseValidFromInts() throws Exception { int precision = randomIntBetween(1, 12); - XContentParser stParser = JsonXContent.jsonXContent.createParser( + XContentParser stParser = createParser(JsonXContent.jsonXContent, "{\"field\":\"my_loc\", \"precision\":" + precision + ", \"size\": 500, \"shard_size\": 550}"); QueryParseContext parseContext = new QueryParseContext(mockRegistry, stParser, ParseFieldMatcher.STRICT); @@ -45,7 +45,7 @@ public class GeoHashGridParserTests extends ESTestCase { public void testParseValidFromStrings() throws Exception { int precision = randomIntBetween(1, 12); - XContentParser stParser = JsonXContent.jsonXContent.createParser( + XContentParser stParser = createParser(JsonXContent.jsonXContent, "{\"field\":\"my_loc\", \"precision\":\"" + precision + "\", \"size\": \"500\", \"shard_size\": \"550\"}"); QueryParseContext parseContext = new QueryParseContext(mockRegistry, stParser, ParseFieldMatcher.STRICT); XContentParser.Token token = stParser.nextToken(); @@ -55,7 +55,7 @@ public class GeoHashGridParserTests extends ESTestCase { } public void testParseErrorOnNonIntPrecision() throws Exception { - XContentParser stParser = JsonXContent.jsonXContent.createParser("{\"field\":\"my_loc\", \"precision\":\"2.0\"}"); + XContentParser stParser = createParser(JsonXContent.jsonXContent, "{\"field\":\"my_loc\", \"precision\":\"2.0\"}"); QueryParseContext parseContext = new QueryParseContext(mockRegistry, stParser, ParseFieldMatcher.STRICT); XContentParser.Token token = stParser.nextToken(); assertSame(XContentParser.Token.START_OBJECT, token); @@ -69,7 +69,7 @@ public class GeoHashGridParserTests extends ESTestCase { } public void testParseErrorOnBooleanPrecision() throws Exception { - XContentParser stParser = JsonXContent.jsonXContent.createParser("{\"field\":\"my_loc\", \"precision\":false}"); + XContentParser stParser = createParser(JsonXContent.jsonXContent, "{\"field\":\"my_loc\", \"precision\":false}"); QueryParseContext parseContext = new QueryParseContext(mockRegistry, stParser, ParseFieldMatcher.STRICT); XContentParser.Token token = stParser.nextToken(); assertSame(XContentParser.Token.START_OBJECT, token); @@ -82,7 +82,7 @@ public class GeoHashGridParserTests extends ESTestCase { } public void testParseErrorOnPrecisionOutOfRange() throws Exception { - XContentParser stParser = JsonXContent.jsonXContent.createParser("{\"field\":\"my_loc\", \"precision\":\"13\"}"); + XContentParser stParser = createParser(JsonXContent.jsonXContent, "{\"field\":\"my_loc\", \"precision\":\"13\"}"); QueryParseContext parseContext = new QueryParseContext(mockRegistry, stParser, ParseFieldMatcher.STRICT); XContentParser.Token token = stParser.nextToken(); assertSame(XContentParser.Token.START_OBJECT, token); diff --git a/core/src/test/java/org/elasticsearch/search/aggregations/bucket/histogram/ExtendedBoundsTests.java b/core/src/test/java/org/elasticsearch/search/aggregations/bucket/histogram/ExtendedBoundsTests.java index e2609f97eaa..cd19785f3e0 100644 --- a/core/src/test/java/org/elasticsearch/search/aggregations/bucket/histogram/ExtendedBoundsTests.java +++ b/core/src/test/java/org/elasticsearch/search/aggregations/bucket/histogram/ExtendedBoundsTests.java @@ -36,7 +36,6 @@ import org.elasticsearch.index.IndexSettings; import org.elasticsearch.index.query.QueryShardContext; import org.elasticsearch.search.DocValueFormat; import org.elasticsearch.search.SearchParseException; -import org.elasticsearch.search.aggregations.bucket.histogram.ExtendedBounds; import org.elasticsearch.search.internal.SearchContext; import org.elasticsearch.test.ESTestCase; import org.joda.time.DateTimeZone; @@ -164,7 +163,7 @@ public class ExtendedBoundsTests extends ESTestCase { orig.toXContent(out, ToXContent.EMPTY_PARAMS); out.endObject(); - try (XContentParser in = JsonXContent.jsonXContent.createParser(out.bytes())) { + try (XContentParser in = createParser(JsonXContent.jsonXContent, out.bytes())) { XContentParser.Token token = in.currentToken(); assertNull(token); diff --git a/core/src/test/java/org/elasticsearch/search/aggregations/bucket/significant/SignificanceHeuristicTests.java b/core/src/test/java/org/elasticsearch/search/aggregations/bucket/significant/SignificanceHeuristicTests.java index e46a61657df..655a03a75c0 100644 --- a/core/src/test/java/org/elasticsearch/search/aggregations/bucket/significant/SignificanceHeuristicTests.java +++ b/core/src/test/java/org/elasticsearch/search/aggregations/bucket/significant/SignificanceHeuristicTests.java @@ -269,7 +269,7 @@ public class SignificanceHeuristicTests extends ESTestCase { IndicesQueriesRegistry registry = new IndicesQueriesRegistry(); try { - XContentParser stParser = JsonXContent.jsonXContent.createParser( + XContentParser stParser = createParser(JsonXContent.jsonXContent, "{\"field\":\"text\", " + faultyHeuristicDefinition + ",\"min_doc_count\":200}"); QueryParseContext parseContext = new QueryParseContext(registry, stParser, ParseFieldMatcher.STRICT); stParser.nextToken(); @@ -286,7 +286,7 @@ public class SignificanceHeuristicTests extends ESTestCase { stBuilder.significanceHeuristic(significanceHeuristic).field("text").minDocCount(200); XContentBuilder stXContentBuilder = XContentFactory.jsonBuilder(); stBuilder.internalXContent(stXContentBuilder, null); - XContentParser stParser = JsonXContent.jsonXContent.createParser(stXContentBuilder.string()); + XContentParser stParser = createParser(JsonXContent.jsonXContent, stXContentBuilder.string()); return parseSignificanceHeuristic(significanceHeuristicParserRegistry, searchContext, stParser); } @@ -308,7 +308,7 @@ public class SignificanceHeuristicTests extends ESTestCase { protected SignificanceHeuristic parseFromString(ParseFieldRegistry significanceHeuristicParserRegistry, SearchContext searchContext, String heuristicString) throws IOException { - XContentParser stParser = JsonXContent.jsonXContent.createParser( + XContentParser stParser = createParser(JsonXContent.jsonXContent, "{\"field\":\"text\", " + heuristicString + ", \"min_doc_count\":200}"); return parseSignificanceHeuristic(significanceHeuristicParserRegistry, searchContext, stParser); } diff --git a/core/src/test/java/org/elasticsearch/search/fetch/subphase/highlight/HighlightBuilderTests.java b/core/src/test/java/org/elasticsearch/search/fetch/subphase/highlight/HighlightBuilderTests.java index 6fe98a2a3ef..a7f442463e9 100644 --- a/core/src/test/java/org/elasticsearch/search/fetch/subphase/highlight/HighlightBuilderTests.java +++ b/core/src/test/java/org/elasticsearch/search/fetch/subphase/highlight/HighlightBuilderTests.java @@ -34,6 +34,7 @@ import org.elasticsearch.common.xcontent.XContentFactory; import org.elasticsearch.common.xcontent.XContentHelper; import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.common.xcontent.XContentType; +import org.elasticsearch.common.xcontent.json.JsonXContent; import org.elasticsearch.index.Index; import org.elasticsearch.index.IndexSettings; import org.elasticsearch.index.mapper.ContentPath; @@ -168,8 +169,8 @@ public class HighlightBuilderTests extends ESTestCase { } } - private static T expectParseThrows(Class exceptionClass, String highlightElement) throws IOException { - XContentParser parser = XContentFactory.xContent(highlightElement).createParser(highlightElement); + private T expectParseThrows(Class exceptionClass, String highlightElement) throws IOException { + XContentParser parser = createParser(JsonXContent.jsonXContent, highlightElement); QueryParseContext context = new QueryParseContext(indicesQueriesRegistry, parser, ParseFieldMatcher.STRICT); return expectThrows(exceptionClass, () -> HighlightBuilder.fromXContent(context)); } @@ -378,7 +379,7 @@ public class HighlightBuilderTests extends ESTestCase { String highlightElement = "{\n" + " \"tags_schema\" : \"styled\"\n" + "}\n"; - XContentParser parser = XContentFactory.xContent(highlightElement).createParser(highlightElement); + XContentParser parser = createParser(JsonXContent.jsonXContent, highlightElement); QueryParseContext context = new QueryParseContext(indicesQueriesRegistry, parser, ParseFieldMatcher.EMPTY); HighlightBuilder highlightBuilder = HighlightBuilder.fromXContent(context); @@ -390,7 +391,7 @@ public class HighlightBuilderTests extends ESTestCase { highlightElement = "{\n" + " \"tags_schema\" : \"default\"\n" + "}\n"; - parser = XContentFactory.xContent(highlightElement).createParser(highlightElement); + parser = createParser(JsonXContent.jsonXContent, highlightElement); context = new QueryParseContext(indicesQueriesRegistry, parser, ParseFieldMatcher.EMPTY); highlightBuilder = HighlightBuilder.fromXContent(context); @@ -411,21 +412,21 @@ public class HighlightBuilderTests extends ESTestCase { */ public void testParsingEmptyStructure() throws IOException { String highlightElement = "{ }"; - XContentParser parser = XContentFactory.xContent(highlightElement).createParser(highlightElement); + XContentParser parser = createParser(JsonXContent.jsonXContent, highlightElement); QueryParseContext context = new QueryParseContext(indicesQueriesRegistry, parser, ParseFieldMatcher.EMPTY); HighlightBuilder highlightBuilder = HighlightBuilder.fromXContent(context); assertEquals("expected plain HighlightBuilder", new HighlightBuilder(), highlightBuilder); highlightElement = "{ \"fields\" : { } }"; - parser = XContentFactory.xContent(highlightElement).createParser(highlightElement); + parser = createParser(JsonXContent.jsonXContent, highlightElement); context = new QueryParseContext(indicesQueriesRegistry, parser, ParseFieldMatcher.EMPTY); highlightBuilder = HighlightBuilder.fromXContent(context); assertEquals("defining no field should return plain HighlightBuilder", new HighlightBuilder(), highlightBuilder); highlightElement = "{ \"fields\" : { \"foo\" : { } } }"; - parser = XContentFactory.xContent(highlightElement).createParser(highlightElement); + parser = createParser(JsonXContent.jsonXContent, highlightElement); context = new QueryParseContext(indicesQueriesRegistry, parser, ParseFieldMatcher.EMPTY); highlightBuilder = HighlightBuilder.fromXContent(context); diff --git a/core/src/test/java/org/elasticsearch/search/rescore/QueryRescoreBuilderTests.java b/core/src/test/java/org/elasticsearch/search/rescore/QueryRescoreBuilderTests.java index 36e02da1f1b..b2f031a4529 100644 --- a/core/src/test/java/org/elasticsearch/search/rescore/QueryRescoreBuilderTests.java +++ b/core/src/test/java/org/elasticsearch/search/rescore/QueryRescoreBuilderTests.java @@ -33,6 +33,7 @@ import org.elasticsearch.common.xcontent.XContentFactory; import org.elasticsearch.common.xcontent.XContentHelper; import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.common.xcontent.XContentType; +import org.elasticsearch.common.xcontent.json.JsonXContent; import org.elasticsearch.index.IndexSettings; import org.elasticsearch.index.mapper.ContentPath; import org.elasticsearch.index.mapper.MappedFieldType; @@ -250,8 +251,8 @@ public class QueryRescoreBuilderTests extends ESTestCase { /** * create a new parser from the rescorer string representation and reset context with it */ - private static QueryParseContext createContext(String rescoreElement) throws IOException { - XContentParser parser = XContentFactory.xContent(rescoreElement).createParser(rescoreElement); + private QueryParseContext createContext(String rescoreElement) throws IOException { + XContentParser parser = createParser(JsonXContent.jsonXContent, rescoreElement); QueryParseContext context = new QueryParseContext(indicesQueriesRegistry, parser, ParseFieldMatcher.STRICT); // move to first token, this is where the internal fromXContent assertTrue(parser.nextToken() == XContentParser.Token.START_OBJECT); diff --git a/core/src/test/java/org/elasticsearch/search/searchafter/SearchAfterBuilderTests.java b/core/src/test/java/org/elasticsearch/search/searchafter/SearchAfterBuilderTests.java index 0c96eb15e0e..451b7b78632 100644 --- a/core/src/test/java/org/elasticsearch/search/searchafter/SearchAfterBuilderTests.java +++ b/core/src/test/java/org/elasticsearch/search/searchafter/SearchAfterBuilderTests.java @@ -28,6 +28,7 @@ import org.elasticsearch.common.xcontent.XContentFactory; import org.elasticsearch.common.xcontent.XContentHelper; import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.common.xcontent.XContentType; +import org.elasticsearch.common.xcontent.json.JsonXContent; import org.elasticsearch.index.query.MatchAllQueryBuilder; import org.elasticsearch.index.query.QueryParseContext; import org.elasticsearch.index.query.QueryParser; @@ -108,7 +109,7 @@ public class SearchAfterBuilderTests extends ESTestCase { // ensure that every number type remain the same before/after xcontent (de)serialization. // This is not a problem because the final type of each field value is extracted from associated sort field. // This little trick ensure that equals and hashcode are the same when using the xcontent serialization. - private static SearchAfterBuilder randomJsonSearchFromBuilder() throws IOException { + private SearchAfterBuilder randomJsonSearchFromBuilder() throws IOException { int numSearchAfter = randomIntBetween(1, 10); XContentBuilder jsonBuilder = XContentFactory.jsonBuilder(); jsonBuilder.startObject(); @@ -150,7 +151,7 @@ public class SearchAfterBuilderTests extends ESTestCase { } jsonBuilder.endArray(); jsonBuilder.endObject(); - XContentParser parser = XContentFactory.xContent(XContentType.JSON).createParser(jsonBuilder.bytes()); + XContentParser parser = createParser(JsonXContent.jsonXContent, jsonBuilder.bytes()); parser.nextToken(); parser.nextToken(); parser.nextToken(); diff --git a/core/src/test/java/org/elasticsearch/search/suggest/completion/CategoryContextMappingTests.java b/core/src/test/java/org/elasticsearch/search/suggest/completion/CategoryContextMappingTests.java index fd7a33ee5ba..a74376554f5 100644 --- a/core/src/test/java/org/elasticsearch/search/suggest/completion/CategoryContextMappingTests.java +++ b/core/src/test/java/org/elasticsearch/search/suggest/completion/CategoryContextMappingTests.java @@ -26,9 +26,8 @@ import org.apache.lucene.search.suggest.document.ContextSuggestField; import org.elasticsearch.common.ParseFieldMatcher; import org.elasticsearch.common.compress.CompressedXContent; import org.elasticsearch.common.xcontent.XContentBuilder; -import org.elasticsearch.common.xcontent.XContentFactory; import org.elasticsearch.common.xcontent.XContentParser; -import org.elasticsearch.common.xcontent.XContentType; +import org.elasticsearch.common.xcontent.json.JsonXContent; import org.elasticsearch.index.mapper.DocumentMapper; import org.elasticsearch.index.mapper.FieldMapper; import org.elasticsearch.index.mapper.MappedFieldType; @@ -191,7 +190,7 @@ public class CategoryContextMappingTests extends ESSingleNodeTestCase { public void testQueryContextParsingBasic() throws Exception { XContentBuilder builder = jsonBuilder().value("context1"); - XContentParser parser = XContentFactory.xContent(XContentType.JSON).createParser(builder.bytes()); + XContentParser parser = createParser(JsonXContent.jsonXContent, builder.bytes()); CategoryContextMapping mapping = ContextBuilder.category("cat").build(); List internalQueryContexts = mapping.parseQueryContext(createParseContext(parser)); assertThat(internalQueryContexts.size(), equalTo(1)); @@ -202,10 +201,10 @@ public class CategoryContextMappingTests extends ESSingleNodeTestCase { public void testQueryContextParsingArray() throws Exception { XContentBuilder builder = jsonBuilder().startArray() - .value("context1") - .value("context2") + .value("context1") + .value("context2") .endArray(); - XContentParser parser = XContentFactory.xContent(XContentType.JSON).createParser(builder.bytes()); + XContentParser parser = createParser(JsonXContent.jsonXContent, builder.bytes()); CategoryContextMapping mapping = ContextBuilder.category("cat").build(); List internalQueryContexts = mapping.parseQueryContext(createParseContext(parser)); assertThat(internalQueryContexts.size(), equalTo(2)); @@ -223,7 +222,7 @@ public class CategoryContextMappingTests extends ESSingleNodeTestCase { .field("boost", 10) .field("prefix", true) .endObject(); - XContentParser parser = XContentFactory.xContent(XContentType.JSON).createParser(builder.bytes()); + XContentParser parser = createParser(JsonXContent.jsonXContent, builder.bytes()); CategoryContextMapping mapping = ContextBuilder.category("cat").build(); List internalQueryContexts = mapping.parseQueryContext(createParseContext(parser)); assertThat(internalQueryContexts.size(), equalTo(1)); @@ -246,7 +245,7 @@ public class CategoryContextMappingTests extends ESSingleNodeTestCase { .field("prefix", false) .endObject() .endArray(); - XContentParser parser = XContentFactory.xContent(XContentType.JSON).createParser(builder.bytes()); + XContentParser parser = createParser(JsonXContent.jsonXContent, builder.bytes()); CategoryContextMapping mapping = ContextBuilder.category("cat").build(); List internalQueryContexts = mapping.parseQueryContext(createParseContext(parser)); assertThat(internalQueryContexts.size(), equalTo(2)); @@ -271,7 +270,7 @@ public class CategoryContextMappingTests extends ESSingleNodeTestCase { .endObject() .value("context2") .endArray(); - XContentParser parser = XContentFactory.xContent(XContentType.JSON).createParser(builder.bytes()); + XContentParser parser = createParser(JsonXContent.jsonXContent, builder.bytes()); CategoryContextMapping mapping = ContextBuilder.category("cat").build(); List internalQueryContexts = mapping.parseQueryContext(createParseContext(parser)); assertThat(internalQueryContexts.size(), equalTo(2)); diff --git a/core/src/test/java/org/elasticsearch/search/suggest/completion/GeoContextMappingTests.java b/core/src/test/java/org/elasticsearch/search/suggest/completion/GeoContextMappingTests.java index 2237c1a41da..a6879ebf297 100644 --- a/core/src/test/java/org/elasticsearch/search/suggest/completion/GeoContextMappingTests.java +++ b/core/src/test/java/org/elasticsearch/search/suggest/completion/GeoContextMappingTests.java @@ -23,9 +23,8 @@ import org.apache.lucene.index.IndexableField; import org.elasticsearch.common.ParseFieldMatcher; import org.elasticsearch.common.compress.CompressedXContent; import org.elasticsearch.common.xcontent.XContentBuilder; -import org.elasticsearch.common.xcontent.XContentFactory; import org.elasticsearch.common.xcontent.XContentParser; -import org.elasticsearch.common.xcontent.XContentType; +import org.elasticsearch.common.xcontent.json.JsonXContent; import org.elasticsearch.index.mapper.DocumentMapper; import org.elasticsearch.index.mapper.FieldMapper; import org.elasticsearch.index.mapper.MappedFieldType; @@ -204,7 +203,7 @@ public class GeoContextMappingTests extends ESSingleNodeTestCase { public void testParsingQueryContextBasic() throws Exception { XContentBuilder builder = jsonBuilder().value("ezs42e44yx96"); - XContentParser parser = XContentFactory.xContent(XContentType.JSON).createParser(builder.bytes()); + XContentParser parser = createParser(JsonXContent.jsonXContent, builder.bytes()); GeoContextMapping mapping = ContextBuilder.geo("geo").build(); List internalQueryContexts = mapping.parseQueryContext(createParseContext(parser)); assertThat(internalQueryContexts.size(), equalTo(1 + 8)); @@ -223,7 +222,7 @@ public class GeoContextMappingTests extends ESSingleNodeTestCase { .field("lat", 23.654242) .field("lon", 90.047153) .endObject(); - XContentParser parser = XContentFactory.xContent(XContentType.JSON).createParser(builder.bytes()); + XContentParser parser = createParser(JsonXContent.jsonXContent, builder.bytes()); GeoContextMapping mapping = ContextBuilder.geo("geo").build(); List internalQueryContexts = mapping.parseQueryContext(createParseContext(parser)); assertThat(internalQueryContexts.size(), equalTo(1 + 8)); @@ -246,7 +245,7 @@ public class GeoContextMappingTests extends ESSingleNodeTestCase { .field("boost", 10) .array("neighbours", 1, 2, 3) .endObject(); - XContentParser parser = XContentFactory.xContent(XContentType.JSON).createParser(builder.bytes()); + XContentParser parser = createParser(JsonXContent.jsonXContent, builder.bytes()); GeoContextMapping mapping = ContextBuilder.geo("geo").build(); List internalQueryContexts = mapping.parseQueryContext(createParseContext(parser)); assertThat(internalQueryContexts.size(), equalTo(1 + 1 + 8 + 1 + 8 + 1 + 8)); @@ -284,7 +283,7 @@ public class GeoContextMappingTests extends ESSingleNodeTestCase { .array("neighbours", 5) .endObject() .endArray(); - XContentParser parser = XContentFactory.xContent(XContentType.JSON).createParser(builder.bytes()); + XContentParser parser = createParser(JsonXContent.jsonXContent, builder.bytes()); GeoContextMapping mapping = ContextBuilder.geo("geo").build(); List internalQueryContexts = mapping.parseQueryContext(createParseContext(parser)); assertThat(internalQueryContexts.size(), equalTo(1 + 1 + 8 + 1 + 8 + 1 + 8 + 1 + 1 + 8)); @@ -327,7 +326,7 @@ public class GeoContextMappingTests extends ESSingleNodeTestCase { .field("lon", 92.112583) .endObject() .endArray(); - XContentParser parser = XContentFactory.xContent(XContentType.JSON).createParser(builder.bytes()); + XContentParser parser = createParser(JsonXContent.jsonXContent, builder.bytes()); GeoContextMapping mapping = ContextBuilder.geo("geo").build(); List internalQueryContexts = mapping.parseQueryContext(createParseContext(parser)); assertThat(internalQueryContexts.size(), equalTo(1 + 1 + 8 + 1 + 8 + 1 + 8)); diff --git a/core/src/test/java/org/elasticsearch/threadpool/SimpleThreadPoolIT.java b/core/src/test/java/org/elasticsearch/threadpool/SimpleThreadPoolIT.java index 1b5202762c6..bff004579ff 100644 --- a/core/src/test/java/org/elasticsearch/threadpool/SimpleThreadPoolIT.java +++ b/core/src/test/java/org/elasticsearch/threadpool/SimpleThreadPoolIT.java @@ -34,7 +34,6 @@ import org.elasticsearch.test.ESIntegTestCase.ClusterScope; import org.elasticsearch.test.ESIntegTestCase.Scope; import org.elasticsearch.test.InternalTestCluster; import org.elasticsearch.test.hamcrest.RegexMatcher; -import org.elasticsearch.tribe.TribeIT; import java.io.IOException; import java.lang.management.ManagementFactory; @@ -136,7 +135,7 @@ public class SimpleThreadPoolIT extends ESIntegTestCase { builder.endObject(); builder.close(); Map poolsMap; - try (XContentParser parser = JsonXContent.jsonXContent.createParser(builder.string())) { + try (XContentParser parser = createParser(JsonXContent.jsonXContent, builder.string())) { poolsMap = parser.map(); } return (Map) ((Map) poolsMap.get("thread_pool")).get(poolName); diff --git a/core/src/test/java/org/elasticsearch/threadpool/ThreadPoolStatsTests.java b/core/src/test/java/org/elasticsearch/threadpool/ThreadPoolStatsTests.java index a7312f59406..bcfca78bb6d 100644 --- a/core/src/test/java/org/elasticsearch/threadpool/ThreadPoolStatsTests.java +++ b/core/src/test/java/org/elasticsearch/threadpool/ThreadPoolStatsTests.java @@ -24,6 +24,7 @@ import org.elasticsearch.common.xcontent.ToXContent; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.common.xcontent.XContentType; +import org.elasticsearch.common.xcontent.json.JsonXContent; import org.elasticsearch.test.ESTestCase; import java.io.IOException; @@ -78,7 +79,7 @@ public class ThreadPoolStatsTests extends ESTestCase { builder.endObject(); } - try (XContentParser parser = XContentType.JSON.xContent().createParser(os.bytes())) { + try (XContentParser parser = createParser(JsonXContent.jsonXContent, os.bytes())) { XContentParser.Token token = parser.currentToken(); assertNull(token); diff --git a/modules/lang-mustache/src/test/java/org/elasticsearch/script/mustache/TemplateQueryBuilderTests.java b/modules/lang-mustache/src/test/java/org/elasticsearch/script/mustache/TemplateQueryBuilderTests.java index 57b9ad622da..34e06410a78 100644 --- a/modules/lang-mustache/src/test/java/org/elasticsearch/script/mustache/TemplateQueryBuilderTests.java +++ b/modules/lang-mustache/src/test/java/org/elasticsearch/script/mustache/TemplateQueryBuilderTests.java @@ -135,10 +135,11 @@ public class TemplateQueryBuilderTests extends AbstractQueryTestCase> QB testQuery = createTestQueryBuilder(); XContentBuilder builder = toXContent(testQuery, randomFrom(XContentType.values())); XContentBuilder shuffled = shuffleXContent(builder, shuffleProtectedFields()); - assertParsedQuery(shuffled.bytes(), testQuery); + assertParsedQuery(createParser(shuffled), testQuery); for (Map.Entry alternateVersion : getAlternateVersions().entrySet()) { String queryAsString = alternateVersion.getKey(); - assertParsedQuery(new BytesArray(queryAsString), alternateVersion.getValue()); + assertParsedQuery(createParser(JsonXContent.jsonXContent, queryAsString), alternateVersion.getValue()); } } } @@ -559,13 +559,13 @@ public abstract class AbstractQueryTestCase> /** * Parses the query provided as bytes argument and compares it with the expected result provided as argument as a {@link QueryBuilder} */ - private void assertParsedQuery(BytesReference queryAsBytes, QueryBuilder expectedQuery) throws IOException { - assertParsedQuery(queryAsBytes, expectedQuery, ParseFieldMatcher.STRICT); + private void assertParsedQuery(XContentParser parser, QueryBuilder expectedQuery) throws IOException { + assertParsedQuery(parser, expectedQuery, ParseFieldMatcher.STRICT); } - private void assertParsedQuery(BytesReference queryAsBytes, QueryBuilder expectedQuery, ParseFieldMatcher matcher) + private void assertParsedQuery(XContentParser parser, QueryBuilder expectedQuery, ParseFieldMatcher matcher) throws IOException { - QueryBuilder newQuery = parseQuery(queryAsBytes, matcher); + QueryBuilder newQuery = parseQuery(parser, matcher); assertNotSame(newQuery, expectedQuery); assertEquals(expectedQuery, newQuery); assertEquals(expectedQuery.hashCode(), newQuery.hashCode()); @@ -575,17 +575,16 @@ public abstract class AbstractQueryTestCase> return parseQuery(queryAsString, ParseFieldMatcher.STRICT); } + protected QueryBuilder parseQuery(XContentParser parser) throws IOException { + return parseQuery(parser, ParseFieldMatcher.STRICT); + } + + protected QueryBuilder parseQuery(AbstractQueryBuilder builder) throws IOException { + return parseQuery(createParser(JsonXContent.jsonXContent, builder.buildAsBytes(XContentType.JSON)), ParseFieldMatcher.STRICT); + } + protected QueryBuilder parseQuery(String queryAsString, ParseFieldMatcher matcher) throws IOException { - XContentParser parser = createParser(queryAsString); - return parseQuery(parser, matcher); - } - - protected QueryBuilder parseQuery(BytesReference queryAsBytes) throws IOException { - return parseQuery(queryAsBytes, ParseFieldMatcher.STRICT); - } - - protected QueryBuilder parseQuery(BytesReference queryAsBytes, ParseFieldMatcher matcher) throws IOException { - XContentParser parser = createParser(queryAsBytes); + XContentParser parser = createParser(JsonXContent.jsonXContent, queryAsString); return parseQuery(parser, matcher); } diff --git a/test/framework/src/main/java/org/elasticsearch/test/ESTestCase.java b/test/framework/src/main/java/org/elasticsearch/test/ESTestCase.java index 4bf47228f47..9a4085ff2e4 100644 --- a/test/framework/src/main/java/org/elasticsearch/test/ESTestCase.java +++ b/test/framework/src/main/java/org/elasticsearch/test/ESTestCase.java @@ -29,6 +29,7 @@ import com.carrotsearch.randomizedtesting.generators.RandomNumbers; import com.carrotsearch.randomizedtesting.generators.RandomPicks; import com.carrotsearch.randomizedtesting.generators.RandomStrings; import com.carrotsearch.randomizedtesting.rules.TestRuleAdapter; + import org.apache.logging.log4j.Level; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; @@ -47,7 +48,6 @@ import org.elasticsearch.Version; import org.elasticsearch.bootstrap.BootstrapForTesting; import org.elasticsearch.client.Requests; import org.elasticsearch.cluster.metadata.IndexMetaData; -import org.elasticsearch.common.bytes.BytesArray; import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.io.PathUtils; import org.elasticsearch.common.io.PathUtilsForTesting; @@ -62,9 +62,9 @@ import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.transport.TransportAddress; import org.elasticsearch.common.util.MockBigArrays; import org.elasticsearch.common.util.MockPageCacheRecycler; +import org.elasticsearch.common.xcontent.XContent; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentFactory; -import org.elasticsearch.common.xcontent.XContentHelper; import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.common.xcontent.XContentType; import org.elasticsearch.env.Environment; @@ -100,6 +100,7 @@ import org.junit.internal.AssumptionViolatedException; import org.junit.rules.RuleChain; import java.io.IOException; +import java.io.InputStream; import java.io.UncheckedIOException; import java.nio.file.DirectoryStream; import java.nio.file.Files; @@ -883,15 +884,36 @@ public abstract class ESTestCase extends LuceneTestCase { /** * Create a new {@link XContentParser}. */ - protected final XContentParser createParser(BytesReference data) throws IOException { - return XContentFactory.xContent(data).createParser(data); + protected final XContentParser createParser(XContentBuilder builder) throws IOException { + return builder.generator().contentType().xContent().createParser(builder.bytes()); } /** * Create a new {@link XContentParser}. */ - protected final XContentParser createParser(String string) throws IOException { - return createParser(new BytesArray(string)); + protected final XContentParser createParser(XContent xContent, String data) throws IOException { + return xContent.createParser(data); + } + + /** + * Create a new {@link XContentParser}. + */ + protected final XContentParser createParser(XContent xContent, InputStream data) throws IOException { + return xContent.createParser(data); + } + + /** + * Create a new {@link XContentParser}. + */ + protected final XContentParser createParser(XContent xContent, byte[] data) throws IOException { + return xContent.createParser(data); + } + + /** + * Create a new {@link XContentParser}. + */ + protected final XContentParser createParser(XContent xContent, BytesReference data) throws IOException { + return xContent.createParser(data); } /** Returns the suite failure marker: internal use only! */ diff --git a/test/framework/src/test/java/org/elasticsearch/test/rest/yaml/parser/AbstractParserTestCase.java b/test/framework/src/test/java/org/elasticsearch/test/rest/yaml/parser/AbstractClientYamlTestFragmentParserTestCase.java similarity index 88% rename from test/framework/src/test/java/org/elasticsearch/test/rest/yaml/parser/AbstractParserTestCase.java rename to test/framework/src/test/java/org/elasticsearch/test/rest/yaml/parser/AbstractClientYamlTestFragmentParserTestCase.java index 7d32389ddba..4346115c184 100644 --- a/test/framework/src/test/java/org/elasticsearch/test/rest/yaml/parser/AbstractParserTestCase.java +++ b/test/framework/src/test/java/org/elasticsearch/test/rest/yaml/parser/AbstractClientYamlTestFragmentParserTestCase.java @@ -25,7 +25,10 @@ import org.junit.After; import static org.hamcrest.Matchers.nullValue; -public abstract class AbstractParserTestCase extends ESTestCase { +/** + * Superclass for tests of subclasses of {@link ClientYamlTestFragmentParser}. + */ +public abstract class AbstractClientYamlTestFragmentParserTestCase extends ESTestCase { protected XContentParser parser; diff --git a/test/framework/src/test/java/org/elasticsearch/test/rest/yaml/parser/AssertionParsersTests.java b/test/framework/src/test/java/org/elasticsearch/test/rest/yaml/parser/AssertionParsersTests.java index 4e382fb7b09..c908f079149 100644 --- a/test/framework/src/test/java/org/elasticsearch/test/rest/yaml/parser/AssertionParsersTests.java +++ b/test/framework/src/test/java/org/elasticsearch/test/rest/yaml/parser/AssertionParsersTests.java @@ -19,13 +19,6 @@ package org.elasticsearch.test.rest.yaml.parser; import org.elasticsearch.common.xcontent.yaml.YamlXContent; -import org.elasticsearch.test.rest.yaml.parser.GreaterThanParser; -import org.elasticsearch.test.rest.yaml.parser.IsFalseParser; -import org.elasticsearch.test.rest.yaml.parser.IsTrueParser; -import org.elasticsearch.test.rest.yaml.parser.LengthParser; -import org.elasticsearch.test.rest.yaml.parser.LessThanParser; -import org.elasticsearch.test.rest.yaml.parser.MatchParser; -import org.elasticsearch.test.rest.yaml.parser.ClientYamlTestSuiteParseContext; import org.elasticsearch.test.rest.yaml.section.GreaterThanAssertion; import org.elasticsearch.test.rest.yaml.section.IsFalseAssertion; import org.elasticsearch.test.rest.yaml.section.IsTrueAssertion; @@ -40,9 +33,9 @@ import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.instanceOf; import static org.hamcrest.Matchers.notNullValue; -public class AssertionParsersTests extends AbstractParserTestCase { +public class AssertionParsersTests extends AbstractClientYamlTestFragmentParserTestCase { public void testParseIsTrue() throws Exception { - parser = YamlXContent.yamlXContent.createParser( + parser = createParser(YamlXContent.yamlXContent, "get.fields._timestamp" ); @@ -54,7 +47,7 @@ public class AssertionParsersTests extends AbstractParserTestCase { } public void testParseIsFalse() throws Exception { - parser = YamlXContent.yamlXContent.createParser( + parser = createParser(YamlXContent.yamlXContent, "docs.1._source" ); @@ -66,7 +59,7 @@ public class AssertionParsersTests extends AbstractParserTestCase { } public void testParseGreaterThan() throws Exception { - parser = YamlXContent.yamlXContent.createParser( + parser = createParser(YamlXContent.yamlXContent, "{ field: 3}" ); @@ -79,7 +72,7 @@ public class AssertionParsersTests extends AbstractParserTestCase { } public void testParseLessThan() throws Exception { - parser = YamlXContent.yamlXContent.createParser( + parser = createParser(YamlXContent.yamlXContent, "{ field: 3}" ); @@ -92,7 +85,7 @@ public class AssertionParsersTests extends AbstractParserTestCase { } public void testParseLength() throws Exception { - parser = YamlXContent.yamlXContent.createParser( + parser = createParser(YamlXContent.yamlXContent, "{ _id: 22}" ); @@ -105,7 +98,7 @@ public class AssertionParsersTests extends AbstractParserTestCase { } public void testParseMatchSimpleIntegerValue() throws Exception { - parser = YamlXContent.yamlXContent.createParser( + parser = createParser(YamlXContent.yamlXContent, "{ field: 10 }" ); @@ -119,7 +112,7 @@ public class AssertionParsersTests extends AbstractParserTestCase { } public void testParseMatchSimpleStringValue() throws Exception { - parser = YamlXContent.yamlXContent.createParser( + parser = createParser(YamlXContent.yamlXContent, "{ foo: bar }" ); @@ -133,7 +126,7 @@ public class AssertionParsersTests extends AbstractParserTestCase { } public void testParseMatchArray() throws Exception { - parser = YamlXContent.yamlXContent.createParser( + parser = createParser(YamlXContent.yamlXContent, "{'matches': ['test_percolator_1', 'test_percolator_2']}" ); @@ -151,7 +144,7 @@ public class AssertionParsersTests extends AbstractParserTestCase { @SuppressWarnings("unchecked") public void testParseMatchSourceValues() throws Exception { - parser = YamlXContent.yamlXContent.createParser( + parser = createParser(YamlXContent.yamlXContent, "{ _source: { responses.0.hits.total: 3, foo: bar }}" ); diff --git a/test/framework/src/test/java/org/elasticsearch/test/rest/yaml/parser/ClientYamlSuiteTestParserTests.java b/test/framework/src/test/java/org/elasticsearch/test/rest/yaml/parser/ClientYamlSuiteTestParserTests.java index 3b7313ec221..801bc4e06e4 100644 --- a/test/framework/src/test/java/org/elasticsearch/test/rest/yaml/parser/ClientYamlSuiteTestParserTests.java +++ b/test/framework/src/test/java/org/elasticsearch/test/rest/yaml/parser/ClientYamlSuiteTestParserTests.java @@ -76,7 +76,7 @@ public class ClientYamlSuiteTestParserTests extends ESTestCase { " index: test_index\n" + "\n"); } - parser = YamlXContent.yamlXContent.createParser( + parser = createParser(YamlXContent.yamlXContent, testSpecBuilder.toString() + "---\n" + "\"Get index mapping\":\n" + @@ -177,7 +177,7 @@ public class ClientYamlSuiteTestParserTests extends ESTestCase { } public void testParseTestSingleTestSection() throws Exception { - parser = YamlXContent.yamlXContent.createParser( + parser = createParser(YamlXContent.yamlXContent, "---\n" + "\"Index with ID\":\n" + "\n" + @@ -275,7 +275,7 @@ public class ClientYamlSuiteTestParserTests extends ESTestCase { } public void testParseTestMultipleTestSections() throws Exception { - parser = YamlXContent.yamlXContent.createParser( + parser = createParser(YamlXContent.yamlXContent, "---\n" + "\"Missing document (partial doc)\":\n" + "\n" + @@ -366,7 +366,7 @@ public class ClientYamlSuiteTestParserTests extends ESTestCase { } public void testParseTestDuplicateTestSections() throws Exception { - parser = YamlXContent.yamlXContent.createParser( + parser = createParser(YamlXContent.yamlXContent, "---\n" + "\"Missing document (script)\":\n" + "\n" + diff --git a/test/framework/src/test/java/org/elasticsearch/test/rest/yaml/parser/DoSectionParserTests.java b/test/framework/src/test/java/org/elasticsearch/test/rest/yaml/parser/DoSectionParserTests.java index 0cecc508bf5..8992b3abde4 100644 --- a/test/framework/src/test/java/org/elasticsearch/test/rest/yaml/parser/DoSectionParserTests.java +++ b/test/framework/src/test/java/org/elasticsearch/test/rest/yaml/parser/DoSectionParserTests.java @@ -19,11 +19,7 @@ package org.elasticsearch.test.rest.yaml.parser; import org.elasticsearch.common.xcontent.XContentParser; -import org.elasticsearch.common.xcontent.json.JsonXContent; import org.elasticsearch.common.xcontent.yaml.YamlXContent; -import org.elasticsearch.test.rest.yaml.parser.DoSectionParser; -import org.elasticsearch.test.rest.yaml.parser.ClientYamlTestParseException; -import org.elasticsearch.test.rest.yaml.parser.ClientYamlTestSuiteParseContext; import org.elasticsearch.test.rest.yaml.section.ApiCallSection; import org.elasticsearch.test.rest.yaml.section.DoSection; import org.hamcrest.MatcherAssert; @@ -38,9 +34,9 @@ import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.notNullValue; import static org.hamcrest.Matchers.nullValue; -public class DoSectionParserTests extends AbstractParserTestCase { +public class DoSectionParserTests extends AbstractClientYamlTestFragmentParserTestCase { public void testParseDoSectionNoBody() throws Exception { - parser = YamlXContent.yamlXContent.createParser( + parser = createParser(YamlXContent.yamlXContent, "get:\n" + " index: test_index\n" + " type: test_type\n" + @@ -61,7 +57,7 @@ public class DoSectionParserTests extends AbstractParserTestCase { } public void testParseDoSectionNoParamsNoBody() throws Exception { - parser = YamlXContent.yamlXContent.createParser( + parser = createParser(YamlXContent.yamlXContent, "cluster.node_info: {}" ); @@ -77,7 +73,7 @@ public class DoSectionParserTests extends AbstractParserTestCase { public void testParseDoSectionWithJsonBody() throws Exception { String body = "{ \"include\": { \"field1\": \"v1\", \"field2\": \"v2\" }, \"count\": 1 }"; - parser = YamlXContent.yamlXContent.createParser( + parser = createParser(YamlXContent.yamlXContent, "index:\n" + " index: test_1\n" + " type: test\n" + @@ -107,7 +103,7 @@ public class DoSectionParserTests extends AbstractParserTestCase { "{ \"index\": { \"_index\":\"test_index2\", \"_type\":\"test_type2\", \"_id\":\"test_id2\" } }\n", "{ \"f1\":\"v2\", \"f2\":47 }\n" }; - parser = YamlXContent.yamlXContent.createParser( + parser = createParser(YamlXContent.yamlXContent, "bulk:\n" + " refresh: true\n" + " body: |\n" + @@ -134,7 +130,7 @@ public class DoSectionParserTests extends AbstractParserTestCase { "{ \"index\": { \"_index\":\"test_index\", \"_type\":\"test_type\", \"_id\":\"test_id\" } }", "{ \"f1\":\"v1\", \"f2\":42 }", }; - parser = YamlXContent.yamlXContent.createParser( + parser = createParser(YamlXContent.yamlXContent, "bulk:\n" + " refresh: true\n" + " body: \n" + @@ -159,7 +155,7 @@ public class DoSectionParserTests extends AbstractParserTestCase { } public void testParseDoSectionWithYamlBody() throws Exception { - parser = YamlXContent.yamlXContent.createParser( + parser = createParser(YamlXContent.yamlXContent, "search:\n" + " body:\n" + " \"_source\": [ include.field1, include.field2 ]\n" + @@ -180,7 +176,7 @@ public class DoSectionParserTests extends AbstractParserTestCase { } public void testParseDoSectionWithYamlMultipleBodies() throws Exception { - parser = YamlXContent.yamlXContent.createParser( + parser = createParser(YamlXContent.yamlXContent, "bulk:\n" + " refresh: true\n" + " body:\n" + @@ -220,7 +216,7 @@ public class DoSectionParserTests extends AbstractParserTestCase { } public void testParseDoSectionWithYamlMultipleBodiesRepeatedProperty() throws Exception { - parser = YamlXContent.yamlXContent.createParser( + parser = createParser(YamlXContent.yamlXContent, "bulk:\n" + " refresh: true\n" + " body:\n" + @@ -253,7 +249,7 @@ public class DoSectionParserTests extends AbstractParserTestCase { } public void testParseDoSectionWithYamlBodyMultiGet() throws Exception { - parser = YamlXContent.yamlXContent.createParser( + parser = createParser(YamlXContent.yamlXContent, "mget:\n" + " body:\n" + " docs:\n" + @@ -278,7 +274,7 @@ public class DoSectionParserTests extends AbstractParserTestCase { } public void testParseDoSectionWithBodyStringified() throws Exception { - parser = YamlXContent.yamlXContent.createParser( + parser = createParser(YamlXContent.yamlXContent, "index:\n" + " index: test_1\n" + " type: test\n" + @@ -303,7 +299,7 @@ public class DoSectionParserTests extends AbstractParserTestCase { } public void testParseDoSectionWithBodiesStringifiedAndNot() throws Exception { - parser = YamlXContent.yamlXContent.createParser( + parser = createParser(YamlXContent.yamlXContent, "index:\n" + " body:\n" + " - \"{ \\\"_source\\\": true, \\\"query\\\": { \\\"match_all\\\": {} } }\"\n" + @@ -326,7 +322,7 @@ public class DoSectionParserTests extends AbstractParserTestCase { } public void testParseDoSectionWithCatch() throws Exception { - parser = YamlXContent.yamlXContent.createParser( + parser = createParser(YamlXContent.yamlXContent, "catch: missing\n" + "indices.get_warmer:\n" + " index: test_index\n" + @@ -344,7 +340,7 @@ public class DoSectionParserTests extends AbstractParserTestCase { } public void testParseDoSectionWithHeaders() throws Exception { - parser = YamlXContent.yamlXContent.createParser( + parser = createParser(YamlXContent.yamlXContent, "headers:\n" + " Authorization: \"thing one\"\n" + " Content-Type: \"application/json\"\n" + @@ -367,7 +363,7 @@ public class DoSectionParserTests extends AbstractParserTestCase { } public void testParseDoSectionWithoutClientCallSection() throws Exception { - parser = YamlXContent.yamlXContent.createParser( + parser = createParser(YamlXContent.yamlXContent, "catch: missing\n" ); @@ -381,7 +377,7 @@ public class DoSectionParserTests extends AbstractParserTestCase { } public void testParseDoSectionMultivaluedField() throws Exception { - parser = YamlXContent.yamlXContent.createParser( + parser = createParser(YamlXContent.yamlXContent, "indices.get_field_mapping:\n" + " index: test_index\n" + " type: test_type\n" + @@ -403,7 +399,7 @@ public class DoSectionParserTests extends AbstractParserTestCase { } public void testParseDoSectionExpectedWarnings() throws Exception { - parser = YamlXContent.yamlXContent.createParser( + parser = createParser(YamlXContent.yamlXContent, "indices.get_field_mapping:\n" + " index: test_index\n" + " type: test_type\n" + @@ -427,7 +423,7 @@ public class DoSectionParserTests extends AbstractParserTestCase { "some test warning they are typically pretty long", "some other test warning somtimes they have [in] them"))); - parser = YamlXContent.yamlXContent.createParser( + parser = createParser(YamlXContent.yamlXContent, "indices.get_field_mapping:\n" + " index: test_index\n" + "warnings:\n" + @@ -442,9 +438,9 @@ public class DoSectionParserTests extends AbstractParserTestCase { } - private static void assertJsonEquals(Map actual, String expected) throws IOException { + private void assertJsonEquals(Map actual, String expected) throws IOException { Map expectedMap; - try (XContentParser parser = JsonXContent.jsonXContent.createParser(expected)) { + try (XContentParser parser = createParser(YamlXContent.yamlXContent, expected)) { expectedMap = parser.mapOrdered(); } MatcherAssert.assertThat(actual, equalTo(expectedMap)); diff --git a/test/framework/src/test/java/org/elasticsearch/test/rest/yaml/parser/SetSectionParserTests.java b/test/framework/src/test/java/org/elasticsearch/test/rest/yaml/parser/SetSectionParserTests.java index 386cdd6deb4..91d35758785 100644 --- a/test/framework/src/test/java/org/elasticsearch/test/rest/yaml/parser/SetSectionParserTests.java +++ b/test/framework/src/test/java/org/elasticsearch/test/rest/yaml/parser/SetSectionParserTests.java @@ -19,18 +19,15 @@ package org.elasticsearch.test.rest.yaml.parser; import org.elasticsearch.common.xcontent.yaml.YamlXContent; -import org.elasticsearch.test.rest.yaml.parser.ClientYamlTestParseException; -import org.elasticsearch.test.rest.yaml.parser.ClientYamlTestSuiteParseContext; -import org.elasticsearch.test.rest.yaml.parser.SetSectionParser; import org.elasticsearch.test.rest.yaml.section.SetSection; import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.notNullValue; -public class SetSectionParserTests extends AbstractParserTestCase { +public class SetSectionParserTests extends AbstractClientYamlTestFragmentParserTestCase { public void testParseSetSectionSingleValue() throws Exception { - parser = YamlXContent.yamlXContent.createParser( + parser = createParser(YamlXContent.yamlXContent, "{ _id: id }" ); @@ -45,7 +42,7 @@ public class SetSectionParserTests extends AbstractParserTestCase { } public void testParseSetSectionMultipleValues() throws Exception { - parser = YamlXContent.yamlXContent.createParser( + parser = createParser(YamlXContent.yamlXContent, "{ _id: id, _type: type, _index: index }" ); @@ -62,7 +59,7 @@ public class SetSectionParserTests extends AbstractParserTestCase { } public void testParseSetSectionNoValues() throws Exception { - parser = YamlXContent.yamlXContent.createParser( + parser = createParser(YamlXContent.yamlXContent, "{ }" ); diff --git a/test/framework/src/test/java/org/elasticsearch/test/rest/yaml/parser/SetupSectionParserTests.java b/test/framework/src/test/java/org/elasticsearch/test/rest/yaml/parser/SetupSectionParserTests.java index fa7c34eecde..e18e4a3e78e 100644 --- a/test/framework/src/test/java/org/elasticsearch/test/rest/yaml/parser/SetupSectionParserTests.java +++ b/test/framework/src/test/java/org/elasticsearch/test/rest/yaml/parser/SetupSectionParserTests.java @@ -20,16 +20,14 @@ package org.elasticsearch.test.rest.yaml.parser; import org.elasticsearch.Version; import org.elasticsearch.common.xcontent.yaml.YamlXContent; -import org.elasticsearch.test.rest.yaml.parser.ClientYamlTestSuiteParseContext; -import org.elasticsearch.test.rest.yaml.parser.SetupSectionParser; import org.elasticsearch.test.rest.yaml.section.SetupSection; import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.notNullValue; -public class SetupSectionParserTests extends AbstractParserTestCase { +public class SetupSectionParserTests extends AbstractClientYamlTestFragmentParserTestCase { public void testParseSetupSection() throws Exception { - parser = YamlXContent.yamlXContent.createParser( + parser = createParser(YamlXContent.yamlXContent, " - do:\n" + " index1:\n" + " index: test_1\n" + @@ -55,7 +53,7 @@ public class SetupSectionParserTests extends AbstractParserTestCase { } public void testParseSetupAndSkipSectionNoSkip() throws Exception { - parser = YamlXContent.yamlXContent.createParser( + parser = createParser(YamlXContent.yamlXContent, " - skip:\n" + " version: \"2.0.0 - 2.3.0\"\n" + " reason: \"Update doesn't return metadata fields, waiting for #3259\"\n" + diff --git a/test/framework/src/test/java/org/elasticsearch/test/rest/yaml/parser/SkipSectionParserTests.java b/test/framework/src/test/java/org/elasticsearch/test/rest/yaml/parser/SkipSectionParserTests.java index 7473e393e5c..9f488fd9d5c 100644 --- a/test/framework/src/test/java/org/elasticsearch/test/rest/yaml/parser/SkipSectionParserTests.java +++ b/test/framework/src/test/java/org/elasticsearch/test/rest/yaml/parser/SkipSectionParserTests.java @@ -21,9 +21,6 @@ package org.elasticsearch.test.rest.yaml.parser; import org.elasticsearch.Version; import org.elasticsearch.common.xcontent.yaml.YamlXContent; import org.elasticsearch.test.VersionUtils; -import org.elasticsearch.test.rest.yaml.parser.ClientYamlTestParseException; -import org.elasticsearch.test.rest.yaml.parser.ClientYamlTestSuiteParseContext; -import org.elasticsearch.test.rest.yaml.parser.SkipSectionParser; import org.elasticsearch.test.rest.yaml.section.SkipSection; import java.util.Arrays; @@ -33,9 +30,9 @@ import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.notNullValue; import static org.hamcrest.Matchers.nullValue; -public class SkipSectionParserTests extends AbstractParserTestCase { +public class SkipSectionParserTests extends AbstractClientYamlTestFragmentParserTestCase { public void testParseSkipSectionVersionNoFeature() throws Exception { - parser = YamlXContent.yamlXContent.createParser( + parser = createParser(YamlXContent.yamlXContent, "version: \" - 2.1.0\"\n" + "reason: Delete ignores the parent param" ); @@ -52,7 +49,7 @@ public class SkipSectionParserTests extends AbstractParserTestCase { } public void testParseSkipSectionAllVersions() throws Exception { - parser = YamlXContent.yamlXContent.createParser( + parser = createParser(YamlXContent.yamlXContent, "version: \" all \"\n" + "reason: Delete ignores the parent param" ); @@ -69,7 +66,7 @@ public class SkipSectionParserTests extends AbstractParserTestCase { } public void testParseSkipSectionFeatureNoVersion() throws Exception { - parser = YamlXContent.yamlXContent.createParser( + parser = createParser(YamlXContent.yamlXContent, "features: regex" ); @@ -85,7 +82,7 @@ public class SkipSectionParserTests extends AbstractParserTestCase { } public void testParseSkipSectionFeaturesNoVersion() throws Exception { - parser = YamlXContent.yamlXContent.createParser( + parser = createParser(YamlXContent.yamlXContent, "features: [regex1,regex2,regex3]" ); @@ -103,7 +100,7 @@ public class SkipSectionParserTests extends AbstractParserTestCase { } public void testParseSkipSectionBothFeatureAndVersion() throws Exception { - parser = YamlXContent.yamlXContent.createParser( + parser = createParser(YamlXContent.yamlXContent, "version: \" - 0.90.2\"\n" + "features: regex\n" + "reason: Delete ignores the parent param" @@ -118,7 +115,7 @@ public class SkipSectionParserTests extends AbstractParserTestCase { } public void testParseSkipSectionNoReason() throws Exception { - parser = YamlXContent.yamlXContent.createParser( + parser = createParser(YamlXContent.yamlXContent, "version: \" - 0.90.2\"\n" ); @@ -132,7 +129,7 @@ public class SkipSectionParserTests extends AbstractParserTestCase { } public void testParseSkipSectionNoVersionNorFeature() throws Exception { - parser = YamlXContent.yamlXContent.createParser( + parser = createParser(YamlXContent.yamlXContent, "reason: Delete ignores the parent param\n" ); diff --git a/test/framework/src/test/java/org/elasticsearch/test/rest/yaml/parser/TeardownSectionParserTests.java b/test/framework/src/test/java/org/elasticsearch/test/rest/yaml/parser/TeardownSectionParserTests.java index 4ad47e35e48..e84eb6b6818 100644 --- a/test/framework/src/test/java/org/elasticsearch/test/rest/yaml/parser/TeardownSectionParserTests.java +++ b/test/framework/src/test/java/org/elasticsearch/test/rest/yaml/parser/TeardownSectionParserTests.java @@ -31,10 +31,10 @@ import static org.hamcrest.Matchers.notNullValue; /** * Unit tests for the teardown section parser */ -public class TeardownSectionParserTests extends AbstractParserTestCase { +public class TeardownSectionParserTests extends AbstractClientYamlTestFragmentParserTestCase { public void testParseTeardownSection() throws Exception { - parser = YamlXContent.yamlXContent.createParser( + parser = createParser(YamlXContent.yamlXContent, " - do:\n" + " delete:\n" + " index: foo\n" + @@ -60,7 +60,7 @@ public class TeardownSectionParserTests extends AbstractParserTestCase { } public void testParseWithSkip() throws Exception { - parser = YamlXContent.yamlXContent.createParser( + parser = createParser(YamlXContent.yamlXContent, " - skip:\n" + " version: \"2.0.0 - 2.3.0\"\n" + " reason: \"there is a reason\"\n" + diff --git a/test/framework/src/test/java/org/elasticsearch/test/rest/yaml/parser/TestSectionParserTests.java b/test/framework/src/test/java/org/elasticsearch/test/rest/yaml/parser/TestSectionParserTests.java index aa23cabe07b..5673f20f065 100644 --- a/test/framework/src/test/java/org/elasticsearch/test/rest/yaml/parser/TestSectionParserTests.java +++ b/test/framework/src/test/java/org/elasticsearch/test/rest/yaml/parser/TestSectionParserTests.java @@ -20,8 +20,7 @@ package org.elasticsearch.test.rest.yaml.parser; import org.elasticsearch.Version; import org.elasticsearch.common.xcontent.yaml.YamlXContent; -import org.elasticsearch.test.rest.yaml.parser.ClientYamlTestSectionParser; -import org.elasticsearch.test.rest.yaml.parser.ClientYamlTestSuiteParseContext; +import org.elasticsearch.test.rest.yaml.section.ClientYamlTestSection; import org.elasticsearch.test.rest.yaml.section.DoSection; import org.elasticsearch.test.rest.yaml.section.GreaterThanAssertion; import org.elasticsearch.test.rest.yaml.section.IsFalseAssertion; @@ -31,7 +30,6 @@ import org.elasticsearch.test.rest.yaml.section.LessThanAssertion; import org.elasticsearch.test.rest.yaml.section.MatchAssertion; import org.elasticsearch.test.rest.yaml.section.SetSection; import org.elasticsearch.test.rest.yaml.section.SkipSection; -import org.elasticsearch.test.rest.yaml.section.ClientYamlTestSection; import java.util.Map; @@ -40,9 +38,9 @@ import static org.hamcrest.Matchers.instanceOf; import static org.hamcrest.Matchers.notNullValue; import static org.hamcrest.Matchers.nullValue; -public class TestSectionParserTests extends AbstractParserTestCase { +public class TestSectionParserTests extends AbstractClientYamlTestFragmentParserTestCase { public void testParseTestSectionWithDoSection() throws Exception { - parser = YamlXContent.yamlXContent.createParser( + parser = createParser(YamlXContent.yamlXContent, "\"First test section\": \n" + " - do :\n" + " catch: missing\n" + @@ -81,7 +79,7 @@ public class TestSectionParserTests extends AbstractParserTestCase { ClientYamlTestSectionParser testSectionParser = new ClientYamlTestSectionParser(); - parser = YamlXContent.yamlXContent.createParser(yaml); + parser = createParser(YamlXContent.yamlXContent,yaml); ClientYamlTestSection testSection = testSectionParser.parse(new ClientYamlTestSuiteParseContext("api", "suite", parser)); assertThat(testSection, notNullValue()); @@ -103,7 +101,7 @@ public class TestSectionParserTests extends AbstractParserTestCase { } public void testParseTestSectionWithMultipleDoSections() throws Exception { - parser = YamlXContent.yamlXContent.createParser( + parser = createParser(YamlXContent.yamlXContent, "\"Basic\":\n" + "\n" + " - do:\n" + @@ -141,7 +139,7 @@ public class TestSectionParserTests extends AbstractParserTestCase { } public void testParseTestSectionWithDoSectionsAndAssertions() throws Exception { - parser = YamlXContent.yamlXContent.createParser( + parser = createParser(YamlXContent.yamlXContent, "\"Basic\":\n" + "\n" + " - do:\n" + @@ -235,7 +233,7 @@ public class TestSectionParserTests extends AbstractParserTestCase { } public void testSmallSection() throws Exception { - parser = YamlXContent.yamlXContent.createParser( + parser = createParser(YamlXContent.yamlXContent, "\"node_info test\":\n" + " - do:\n" + " cluster.node_info: {}\n" + diff --git a/test/framework/src/test/java/org/elasticsearch/test/rest/yaml/restspec/ClientYamlSuiteRestApiParserFailingTests.java b/test/framework/src/test/java/org/elasticsearch/test/rest/yaml/restspec/ClientYamlSuiteRestApiParserFailingTests.java index 02995e84bd9..2d3afa91eda 100644 --- a/test/framework/src/test/java/org/elasticsearch/test/rest/yaml/restspec/ClientYamlSuiteRestApiParserFailingTests.java +++ b/test/framework/src/test/java/org/elasticsearch/test/rest/yaml/restspec/ClientYamlSuiteRestApiParserFailingTests.java @@ -19,11 +19,8 @@ package org.elasticsearch.test.rest.yaml.restspec; import org.elasticsearch.common.xcontent.XContentParser; -import org.elasticsearch.common.xcontent.json.JsonXContent; +import org.elasticsearch.common.xcontent.yaml.YamlXContent; import org.elasticsearch.test.ESTestCase; -import org.elasticsearch.test.rest.yaml.restspec.ClientYamlSuiteRestApiParser; - -import java.io.IOException; import static org.hamcrest.Matchers.containsString; @@ -144,7 +141,7 @@ public class ClientYamlSuiteRestApiParserFailingTests extends ESTestCase { } private void parseAndExpectFailure(String brokenJson, String expectedErrorMessage) throws Exception { - XContentParser parser = JsonXContent.jsonXContent.createParser(brokenJson); + XContentParser parser = createParser(YamlXContent.yamlXContent, brokenJson); ClientYamlSuiteRestApiParser restApiParser = new ClientYamlSuiteRestApiParser(); IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> restApiParser.parse("location", parser)); diff --git a/test/framework/src/test/java/org/elasticsearch/test/rest/yaml/restspec/ClientYamlSuiteRestApiParserTests.java b/test/framework/src/test/java/org/elasticsearch/test/rest/yaml/restspec/ClientYamlSuiteRestApiParserTests.java index 8396e343a56..caca95b1315 100644 --- a/test/framework/src/test/java/org/elasticsearch/test/rest/yaml/restspec/ClientYamlSuiteRestApiParserTests.java +++ b/test/framework/src/test/java/org/elasticsearch/test/rest/yaml/restspec/ClientYamlSuiteRestApiParserTests.java @@ -18,18 +18,16 @@ */ package org.elasticsearch.test.rest.yaml.restspec; -import org.elasticsearch.common.xcontent.json.JsonXContent; -import org.elasticsearch.test.rest.yaml.parser.AbstractParserTestCase; -import org.elasticsearch.test.rest.yaml.restspec.ClientYamlSuiteRestApi; -import org.elasticsearch.test.rest.yaml.restspec.ClientYamlSuiteRestApiParser; +import org.elasticsearch.common.xcontent.yaml.YamlXContent; +import org.elasticsearch.test.rest.yaml.parser.AbstractClientYamlTestFragmentParserTestCase; import static org.hamcrest.Matchers.contains; import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.notNullValue; -public class ClientYamlSuiteRestApiParserTests extends AbstractParserTestCase { +public class ClientYamlSuiteRestApiParserTests extends AbstractClientYamlTestFragmentParserTestCase { public void testParseRestSpecIndexApi() throws Exception { - parser = JsonXContent.jsonXContent.createParser(REST_SPEC_INDEX_API); + parser = createParser(YamlXContent.yamlXContent, REST_SPEC_INDEX_API); ClientYamlSuiteRestApi restApi = new ClientYamlSuiteRestApiParser().parse("location", parser); assertThat(restApi, notNullValue()); @@ -51,7 +49,7 @@ public class ClientYamlSuiteRestApiParserTests extends AbstractParserTestCase { } public void testParseRestSpecGetTemplateApi() throws Exception { - parser = JsonXContent.jsonXContent.createParser(REST_SPEC_GET_TEMPLATE_API); + parser = createParser(YamlXContent.yamlXContent, REST_SPEC_GET_TEMPLATE_API); ClientYamlSuiteRestApi restApi = new ClientYamlSuiteRestApiParser().parse("location", parser); assertThat(restApi, notNullValue()); assertThat(restApi.getName(), equalTo("indices.get_template")); @@ -68,7 +66,7 @@ public class ClientYamlSuiteRestApiParserTests extends AbstractParserTestCase { } public void testParseRestSpecCountApi() throws Exception { - parser = JsonXContent.jsonXContent.createParser(REST_SPEC_COUNT_API); + parser = createParser(YamlXContent.yamlXContent, REST_SPEC_COUNT_API); ClientYamlSuiteRestApi restApi = new ClientYamlSuiteRestApiParser().parse("location", parser); assertThat(restApi, notNullValue()); assertThat(restApi.getName(), equalTo("count"));