|
24 | 24 | import org.elasticsearch.ingest.RandomDocumentPicks;
|
25 | 25 | import org.elasticsearch.ingest.core.IngestDocument;
|
26 | 26 | import org.elasticsearch.test.ESTestCase;
|
| 27 | +import org.elasticsearch.test.XContentTestUtils; |
27 | 28 |
|
28 | 29 | import java.io.IOException;
|
29 | 30 | import java.util.Collections;
|
30 | 31 | import java.util.HashMap;
|
31 | 32 | import java.util.Map;
|
32 | 33 |
|
| 34 | +import static org.elasticsearch.test.XContentTestUtils.convertToMap; |
| 35 | +import static org.elasticsearch.test.XContentTestUtils.differenceBetweenMapsIgnoringArrayOrder; |
33 | 36 | import static org.hamcrest.Matchers.equalTo;
|
| 37 | +import static org.hamcrest.Matchers.is; |
34 | 38 | import static org.hamcrest.Matchers.not;
|
| 39 | +import static org.hamcrest.Matchers.nullValue; |
35 | 40 |
|
36 | 41 | public class WriteableIngestDocumentTests extends ESTestCase {
|
37 | 42 |
|
@@ -111,4 +116,30 @@ public void testSerialization() throws IOException {
|
111 | 116 | WriteableIngestDocument otherWriteableIngestDocument = new WriteableIngestDocument(streamInput);
|
112 | 117 | assertThat(otherWriteableIngestDocument, equalTo(writeableIngestDocument));
|
113 | 118 | }
|
| 119 | + |
| 120 | + @SuppressWarnings("unchecked") |
| 121 | + public void testToXContent() throws IOException { |
| 122 | + IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random()); |
| 123 | + WriteableIngestDocument writeableIngestDocument = new WriteableIngestDocument(new IngestDocument(ingestDocument)); |
| 124 | + |
| 125 | + Map<String, Object> toXContentMap = convertToMap(writeableIngestDocument); |
| 126 | + Map<String, Object> toXContentDoc = (Map<String, Object>) toXContentMap.get("doc"); |
| 127 | + Map<String, Object> toXContentSource = (Map<String, Object>) toXContentDoc.get("_source"); |
| 128 | + Map<String, String> toXContentIngestMetadata = (Map<String, String>) toXContentDoc.get("_ingest"); |
| 129 | + |
| 130 | + Map<IngestDocument.MetaData, String> metadataMap = ingestDocument.extractMetadata(); |
| 131 | + for (Map.Entry<IngestDocument.MetaData, String> metadata : metadataMap.entrySet()) { |
| 132 | + String fieldName = metadata.getKey().getFieldName(); |
| 133 | + if (metadata.getValue() == null) { |
| 134 | + assertThat(toXContentDoc.containsKey(fieldName), is(false)); |
| 135 | + } else { |
| 136 | + assertThat(toXContentDoc.get(fieldName), equalTo(metadata.getValue())); |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + String sourceDiff = differenceBetweenMapsIgnoringArrayOrder(toXContentSource, ingestDocument.getSourceAndMetadata()); |
| 141 | + assertThat(sourceDiff, is(nullValue())); |
| 142 | + |
| 143 | + assertThat(toXContentIngestMetadata, equalTo(ingestDocument.getIngestMetadata())); |
| 144 | + } |
114 | 145 | }
|
0 commit comments