|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the "Elastic License |
| 4 | + * 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side |
| 5 | + * Public License v 1"; you may not use this file except in compliance with, at |
| 6 | + * your election, the "Elastic License 2.0", the "GNU Affero General Public |
| 7 | + * License v3.0 only", or the "Server Side Public License, v 1". |
| 8 | + */ |
| 9 | + |
| 10 | +package org.elasticsearch.action.datastreams; |
| 11 | + |
| 12 | +import org.elasticsearch.common.bytes.BytesReference; |
| 13 | +import org.elasticsearch.common.compress.CompressedXContent; |
| 14 | +import org.elasticsearch.common.xcontent.XContentHelper; |
| 15 | +import org.elasticsearch.test.ESTestCase; |
| 16 | +import org.elasticsearch.xcontent.ToXContent; |
| 17 | +import org.elasticsearch.xcontent.XContentBuilder; |
| 18 | +import org.elasticsearch.xcontent.XContentType; |
| 19 | + |
| 20 | +import java.io.IOException; |
| 21 | +import java.util.ArrayList; |
| 22 | +import java.util.List; |
| 23 | +import java.util.Map; |
| 24 | + |
| 25 | +import static org.elasticsearch.xcontent.ToXContent.EMPTY_PARAMS; |
| 26 | +import static org.hamcrest.Matchers.equalTo; |
| 27 | + |
| 28 | +public class GetDataStreamMappingsActionTests extends ESTestCase { |
| 29 | + |
| 30 | + public void testResponseToXContentEmpty() throws IOException { |
| 31 | + List<GetDataStreamMappingsAction.DataStreamMappingsResponse> responseList = new ArrayList<>(); |
| 32 | + GetDataStreamMappingsAction.Response response = new GetDataStreamMappingsAction.Response(responseList); |
| 33 | + try (XContentBuilder builder = XContentBuilder.builder(XContentType.JSON.xContent())) { |
| 34 | + builder.humanReadable(true); |
| 35 | + response.toXContentChunked(ToXContent.EMPTY_PARAMS).forEachRemaining(xcontent -> { |
| 36 | + try { |
| 37 | + xcontent.toXContent(builder, EMPTY_PARAMS); |
| 38 | + } catch (IOException e) { |
| 39 | + fail(e); |
| 40 | + } |
| 41 | + }); |
| 42 | + Map<String, Object> xContentMap = XContentHelper.convertToMap(BytesReference.bytes(builder), false, builder.contentType()).v2(); |
| 43 | + assertThat(xContentMap, equalTo(Map.of("data_streams", List.of()))); |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + public void testResponseToXContent() throws IOException { |
| 48 | + Map<String, Object> dataStream1Mappings = Map.of( |
| 49 | + "properties", |
| 50 | + Map.of("field2", Map.of("type", "text"), "field3", Map.of("type", "keyword")) |
| 51 | + ); |
| 52 | + Map<String, Object> dataStream1EffectiveMappings = Map.of( |
| 53 | + "properties", |
| 54 | + Map.of("field1", Map.of("type", "keyword"), "field2", Map.of("type", "text"), "field3", Map.of("type", "keyword")) |
| 55 | + ); |
| 56 | + Map<String, Object> dataStream2Mappings = Map.of( |
| 57 | + "properties", |
| 58 | + Map.of("field4", Map.of("type", "text"), "field5", Map.of("type", "keyword")) |
| 59 | + ); |
| 60 | + Map<String, Object> dataStream2EffectiveMappings = Map.of( |
| 61 | + "properties", |
| 62 | + Map.of("field4", Map.of("type", "text"), "field5", Map.of("type", "keyword"), "field6", Map.of("type", "keyword")) |
| 63 | + ); |
| 64 | + GetDataStreamMappingsAction.DataStreamMappingsResponse DataStreamMappingsResponse1 = |
| 65 | + new GetDataStreamMappingsAction.DataStreamMappingsResponse( |
| 66 | + "dataStream1", |
| 67 | + new CompressedXContent(dataStream1Mappings), |
| 68 | + new CompressedXContent(dataStream1EffectiveMappings) |
| 69 | + ); |
| 70 | + GetDataStreamMappingsAction.DataStreamMappingsResponse DataStreamMappingsResponse2 = |
| 71 | + new GetDataStreamMappingsAction.DataStreamMappingsResponse( |
| 72 | + "dataStream2", |
| 73 | + new CompressedXContent(dataStream2Mappings), |
| 74 | + new CompressedXContent(dataStream2EffectiveMappings) |
| 75 | + ); |
| 76 | + List<GetDataStreamMappingsAction.DataStreamMappingsResponse> responseList = List.of( |
| 77 | + DataStreamMappingsResponse1, |
| 78 | + DataStreamMappingsResponse2 |
| 79 | + ); |
| 80 | + GetDataStreamMappingsAction.Response response = new GetDataStreamMappingsAction.Response(responseList); |
| 81 | + try (XContentBuilder builder = XContentBuilder.builder(XContentType.JSON.xContent())) { |
| 82 | + builder.humanReadable(true); |
| 83 | + response.toXContentChunked(ToXContent.EMPTY_PARAMS).forEachRemaining(xcontent -> { |
| 84 | + try { |
| 85 | + xcontent.toXContent(builder, EMPTY_PARAMS); |
| 86 | + } catch (IOException e) { |
| 87 | + fail(e); |
| 88 | + } |
| 89 | + }); |
| 90 | + Map<String, Object> xContentMap = XContentHelper.convertToMap(BytesReference.bytes(builder), false, builder.contentType()).v2(); |
| 91 | + assertThat( |
| 92 | + xContentMap, |
| 93 | + equalTo( |
| 94 | + Map.of( |
| 95 | + "data_streams", |
| 96 | + List.of( |
| 97 | + Map.of( |
| 98 | + "name", |
| 99 | + "dataStream1", |
| 100 | + "mappings", |
| 101 | + dataStream1Mappings, |
| 102 | + "effective_mappings", |
| 103 | + dataStream1EffectiveMappings |
| 104 | + ), |
| 105 | + Map.of( |
| 106 | + "name", |
| 107 | + "dataStream2", |
| 108 | + "mappings", |
| 109 | + dataStream2Mappings, |
| 110 | + "effective_mappings", |
| 111 | + dataStream2EffectiveMappings |
| 112 | + ) |
| 113 | + ) |
| 114 | + ) |
| 115 | + ) |
| 116 | + ); |
| 117 | + } |
| 118 | + } |
| 119 | +} |
0 commit comments