|
19 | 19 |
|
20 | 20 | package org.elasticsearch.common.xcontent.support; |
21 | 21 |
|
| 22 | +import org.elasticsearch.common.bytes.BytesReference; |
| 23 | +import org.elasticsearch.common.xcontent.ToXContent; |
22 | 24 | import org.elasticsearch.common.xcontent.XContentHelper; |
| 25 | +import org.elasticsearch.common.xcontent.XContentParser; |
| 26 | +import org.elasticsearch.common.xcontent.XContentType; |
23 | 27 | import org.elasticsearch.test.ESTestCase; |
24 | 28 | import org.hamcrest.Matchers; |
25 | 29 |
|
| 30 | +import java.io.IOException; |
26 | 31 | import java.util.Arrays; |
27 | 32 | import java.util.HashMap; |
28 | 33 | import java.util.List; |
@@ -61,5 +66,27 @@ public void testMergingListValuesAreMapsOfOne() { |
61 | 66 | assertThat(content, Matchers.equalTo(expected)); |
62 | 67 | } |
63 | 68 |
|
64 | | - |
| 69 | + public void testToXContentWrapInObject() throws IOException { |
| 70 | + boolean wrapInObject = randomBoolean(); |
| 71 | + XContentType xContentType = randomFrom(XContentType.values()); |
| 72 | + ToXContent toXContent = (builder, params) -> { |
| 73 | + if (wrapInObject == false) { |
| 74 | + builder.startObject(); |
| 75 | + } |
| 76 | + builder.field("field", "value"); |
| 77 | + if (wrapInObject == false) { |
| 78 | + builder.endObject(); |
| 79 | + } |
| 80 | + return builder; |
| 81 | + }; |
| 82 | + BytesReference bytes = XContentHelper.toXContent(toXContent, xContentType, wrapInObject); |
| 83 | + try (XContentParser parser = xContentType.xContent().createParser(bytes)) { |
| 84 | + assertEquals(XContentParser.Token.START_OBJECT, parser.nextToken()); |
| 85 | + assertEquals(XContentParser.Token.FIELD_NAME, parser.nextToken()); |
| 86 | + assertTrue(parser.nextToken().isValue()); |
| 87 | + assertEquals("value", parser.text()); |
| 88 | + assertEquals(XContentParser.Token.END_OBJECT, parser.nextToken()); |
| 89 | + assertNull(parser.nextToken()); |
| 90 | + } |
| 91 | + } |
65 | 92 | } |
0 commit comments