Search in sources :

Example 1 with ToXContentObject

use of org.elasticsearch.common.xcontent.ToXContentObject in project elasticsearch by elastic.

the class StringsTests method testToStringToXContent.

public void testToStringToXContent() {
    final ToXContent toXContent;
    final boolean error;
    if (randomBoolean()) {
        if (randomBoolean()) {
            error = false;
            toXContent = (builder, params) -> builder.field("ok", "here").field("catastrophe", "");
        } else {
            error = true;
            toXContent = (builder, params) -> builder.startObject().field("ok", "here").field("catastrophe", "").endObject();
        }
    } else {
        if (randomBoolean()) {
            error = false;
            toXContent = (ToXContentObject) (builder, params) -> builder.startObject().field("ok", "here").field("catastrophe", "").endObject();
        } else {
            error = true;
            toXContent = (ToXContentObject) (builder, params) -> builder.field("ok", "here").field("catastrophe", "");
        }
    }
    String toString = Strings.toString(toXContent);
    if (error) {
        assertThat(toString, containsString("Error building toString out of XContent"));
    } else {
        assertThat(toString, containsString("\"ok\":\"here\""));
        assertThat(toString, containsString("\"catastrophe\":\"\""));
    }
}
Also used : ToXContent(org.elasticsearch.common.xcontent.ToXContent) ToXContentObject(org.elasticsearch.common.xcontent.ToXContentObject) ToXContent(org.elasticsearch.common.xcontent.ToXContent) ESTestCase(org.elasticsearch.test.ESTestCase) Matchers.containsString(org.hamcrest.Matchers.containsString) Sets(org.elasticsearch.common.util.set.Sets) Matchers.containsString(org.hamcrest.Matchers.containsString)

Example 2 with ToXContentObject

use of org.elasticsearch.common.xcontent.ToXContentObject in project elasticsearch by elastic.

the class XContentHelperTests method testToXContent.

public void testToXContent() throws IOException {
    final XContentType xContentType = randomFrom(XContentType.values());
    final ToXContent toXContent;
    final boolean error;
    if (randomBoolean()) {
        if (randomBoolean()) {
            error = false;
            toXContent = (builder, params) -> builder.field("field", "value");
        } else {
            error = true;
            toXContent = (builder, params) -> builder.startObject().field("field", "value").endObject();
        }
    } else {
        if (randomBoolean()) {
            error = false;
            toXContent = (ToXContentObject) (builder, params) -> builder.startObject().field("field", "value").endObject();
        } else {
            error = true;
            toXContent = (ToXContentObject) (builder, params) -> builder.field("field", "value");
        }
    }
    if (error) {
        expectThrows(IOException.class, () -> XContentHelper.toXContent(toXContent, xContentType, randomBoolean()));
    } else {
        BytesReference bytes = XContentHelper.toXContent(toXContent, xContentType, randomBoolean());
        try (XContentParser parser = xContentType.xContent().createParser(NamedXContentRegistry.EMPTY, bytes)) {
            assertEquals(XContentParser.Token.START_OBJECT, parser.nextToken());
            assertEquals(XContentParser.Token.FIELD_NAME, parser.nextToken());
            assertTrue(parser.nextToken().isValue());
            assertEquals("value", parser.text());
            assertEquals(XContentParser.Token.END_OBJECT, parser.nextToken());
            assertNull(parser.nextToken());
        }
    }
}
Also used : ToXContent(org.elasticsearch.common.xcontent.ToXContent) Arrays(java.util.Arrays) XContentType(org.elasticsearch.common.xcontent.XContentType) ToXContent(org.elasticsearch.common.xcontent.ToXContent) Matchers(org.hamcrest.Matchers) IOException(java.io.IOException) HashMap(java.util.HashMap) BytesReference(org.elasticsearch.common.bytes.BytesReference) XContentHelper(org.elasticsearch.common.xcontent.XContentHelper) XContentParser(org.elasticsearch.common.xcontent.XContentParser) List(java.util.List) Map(java.util.Map) ToXContentObject(org.elasticsearch.common.xcontent.ToXContentObject) ESTestCase(org.elasticsearch.test.ESTestCase) NamedXContentRegistry(org.elasticsearch.common.xcontent.NamedXContentRegistry) BytesReference(org.elasticsearch.common.bytes.BytesReference) XContentType(org.elasticsearch.common.xcontent.XContentType) XContentParser(org.elasticsearch.common.xcontent.XContentParser)

Aggregations

ToXContent (org.elasticsearch.common.xcontent.ToXContent)2 ToXContentObject (org.elasticsearch.common.xcontent.ToXContentObject)2 ESTestCase (org.elasticsearch.test.ESTestCase)2 IOException (java.io.IOException)1 Arrays (java.util.Arrays)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 BytesReference (org.elasticsearch.common.bytes.BytesReference)1 Sets (org.elasticsearch.common.util.set.Sets)1 NamedXContentRegistry (org.elasticsearch.common.xcontent.NamedXContentRegistry)1 XContentHelper (org.elasticsearch.common.xcontent.XContentHelper)1 XContentParser (org.elasticsearch.common.xcontent.XContentParser)1 XContentType (org.elasticsearch.common.xcontent.XContentType)1 Matchers (org.hamcrest.Matchers)1 Matchers.containsString (org.hamcrest.Matchers.containsString)1