Search in sources :

Example 1 with ToXContentObject

use of org.opensearch.common.xcontent.ToXContentObject in project OpenSearch by opensearch-project.

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, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, bytes.streamInput())) {
            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.opensearch.common.xcontent.ToXContent) Arrays(java.util.Arrays) BytesReference(org.opensearch.common.bytes.BytesReference) ToXContent(org.opensearch.common.xcontent.ToXContent) OpenSearchTestCase(org.opensearch.test.OpenSearchTestCase) CompressedXContent(org.opensearch.common.compress.CompressedXContent) Matchers(org.hamcrest.Matchers) IOException(java.io.IOException) HashMap(java.util.HashMap) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) XContentHelper(org.opensearch.common.xcontent.XContentHelper) XContentParser(org.opensearch.common.xcontent.XContentParser) List(java.util.List) ToXContentObject(org.opensearch.common.xcontent.ToXContentObject) NamedXContentRegistry(org.opensearch.common.xcontent.NamedXContentRegistry) Map(java.util.Map) XContentType(org.opensearch.common.xcontent.XContentType) DeprecationHandler(org.opensearch.common.xcontent.DeprecationHandler) BytesReference(org.opensearch.common.bytes.BytesReference) XContentType(org.opensearch.common.xcontent.XContentType) XContentParser(org.opensearch.common.xcontent.XContentParser)

Example 2 with ToXContentObject

use of org.opensearch.common.xcontent.ToXContentObject in project OpenSearch by opensearch-project.

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\":\"error building toString out of XContent:"));
        assertThat(toString, containsString("\"stack_trace\":"));
    } else {
        assertThat(toString, containsString("\"ok\":\"here\""));
        assertThat(toString, containsString("\"catastrophe\":\"\""));
    }
}
Also used : ToXContent(org.opensearch.common.xcontent.ToXContent) Sets(org.opensearch.common.util.set.Sets) ToXContentObject(org.opensearch.common.xcontent.ToXContentObject) ToXContent(org.opensearch.common.xcontent.ToXContent) OpenSearchTestCase(org.opensearch.test.OpenSearchTestCase) Matchers.is(org.hamcrest.Matchers.is) Collections(java.util.Collections) Matchers.containsString(org.hamcrest.Matchers.containsString) Matchers.containsString(org.hamcrest.Matchers.containsString)

Aggregations

ToXContent (org.opensearch.common.xcontent.ToXContent)2 ToXContentObject (org.opensearch.common.xcontent.ToXContentObject)2 OpenSearchTestCase (org.opensearch.test.OpenSearchTestCase)2 IOException (java.io.IOException)1 Arrays (java.util.Arrays)1 Collections (java.util.Collections)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 Matchers (org.hamcrest.Matchers)1 Matchers.containsString (org.hamcrest.Matchers.containsString)1 Matchers.is (org.hamcrest.Matchers.is)1 BytesReference (org.opensearch.common.bytes.BytesReference)1 CompressedXContent (org.opensearch.common.compress.CompressedXContent)1 Sets (org.opensearch.common.util.set.Sets)1 DeprecationHandler (org.opensearch.common.xcontent.DeprecationHandler)1 NamedXContentRegistry (org.opensearch.common.xcontent.NamedXContentRegistry)1 XContentBuilder (org.opensearch.common.xcontent.XContentBuilder)1 XContentHelper (org.opensearch.common.xcontent.XContentHelper)1 XContentParser (org.opensearch.common.xcontent.XContentParser)1