|
| 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.settings.Settings; |
| 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 GetDataStreamSettingsActionTests extends ESTestCase { |
| 29 | + |
| 30 | + public void testResponseToXContentEmpty() throws IOException { |
| 31 | + List<GetDataStreamSettingsAction.DataStreamSettingsResponse> responseList = new ArrayList<>(); |
| 32 | + GetDataStreamSettingsAction.Response response = new GetDataStreamSettingsAction.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, String> dataStream1Settings = Map.of("setting1", "value1", "setting2", "value2"); |
| 49 | + Map<String, String> dataStream1EffectiveSettings = Map.of("setting1", "value1", "setting2", "value2", "setting3", "value3"); |
| 50 | + Map<String, String> dataStream2Settings = Map.of("setting4", "value4", "setting5", "value5"); |
| 51 | + Map<String, String> dataStream2EffectiveSettings = Map.of("setting4", "value4", "setting5", "value5", "settings6", "value6"); |
| 52 | + GetDataStreamSettingsAction.DataStreamSettingsResponse dataStreamSettingsResponse1 = |
| 53 | + new GetDataStreamSettingsAction.DataStreamSettingsResponse( |
| 54 | + "dataStream1", |
| 55 | + Settings.builder().loadFromMap(dataStream1Settings).build(), |
| 56 | + Settings.builder().loadFromMap(dataStream1EffectiveSettings).build() |
| 57 | + ); |
| 58 | + GetDataStreamSettingsAction.DataStreamSettingsResponse dataStreamSettingsResponse2 = |
| 59 | + new GetDataStreamSettingsAction.DataStreamSettingsResponse( |
| 60 | + "dataStream2", |
| 61 | + Settings.builder().loadFromMap(dataStream2Settings).build(), |
| 62 | + Settings.builder().loadFromMap(dataStream2EffectiveSettings).build() |
| 63 | + ); |
| 64 | + List<GetDataStreamSettingsAction.DataStreamSettingsResponse> responseList = List.of( |
| 65 | + dataStreamSettingsResponse1, |
| 66 | + dataStreamSettingsResponse2 |
| 67 | + ); |
| 68 | + GetDataStreamSettingsAction.Response response = new GetDataStreamSettingsAction.Response(responseList); |
| 69 | + try (XContentBuilder builder = XContentBuilder.builder(XContentType.JSON.xContent())) { |
| 70 | + builder.humanReadable(true); |
| 71 | + response.toXContentChunked(ToXContent.EMPTY_PARAMS).forEachRemaining(xcontent -> { |
| 72 | + try { |
| 73 | + xcontent.toXContent(builder, EMPTY_PARAMS); |
| 74 | + } catch (IOException e) { |
| 75 | + fail(e); |
| 76 | + } |
| 77 | + }); |
| 78 | + Map<String, Object> xContentMap = XContentHelper.convertToMap(BytesReference.bytes(builder), false, builder.contentType()).v2(); |
| 79 | + assertThat( |
| 80 | + xContentMap, |
| 81 | + equalTo( |
| 82 | + Map.of( |
| 83 | + "data_streams", |
| 84 | + List.of( |
| 85 | + Map.of( |
| 86 | + "name", |
| 87 | + "dataStream1", |
| 88 | + "settings", |
| 89 | + dataStream1Settings, |
| 90 | + "effective_settings", |
| 91 | + dataStream1EffectiveSettings |
| 92 | + ), |
| 93 | + Map.of( |
| 94 | + "name", |
| 95 | + "dataStream2", |
| 96 | + "settings", |
| 97 | + dataStream2Settings, |
| 98 | + "effective_settings", |
| 99 | + dataStream2EffectiveSettings |
| 100 | + ) |
| 101 | + ) |
| 102 | + ) |
| 103 | + ) |
| 104 | + ); |
| 105 | + } |
| 106 | + } |
| 107 | +} |
0 commit comments