Skip to content

Commit

Permalink
Remove ImmutableOpenMap from most tests (#88285)
Browse files Browse the repository at this point in the history
This commit removes ImmutableOpenMap from most test cases from server,
where the type is no longer needed because Map is used by the class
being constructed.

relates #86239
  • Loading branch information
rjernst committed Jul 5, 2022
1 parent bff8840 commit a9d1986
Show file tree
Hide file tree
Showing 28 changed files with 122 additions and 208 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@
import org.elasticsearch.cluster.ClusterState;
import org.elasticsearch.cluster.metadata.IndexMetadata;
import org.elasticsearch.cluster.metadata.Metadata;
import org.elasticsearch.common.collect.ImmutableOpenMap;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.indices.SystemIndexDescriptor;
import org.elasticsearch.indices.SystemIndices;
import org.elasticsearch.test.ESTestCase;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import static org.elasticsearch.action.admin.cluster.migration.GetFeatureUpgradeStatusResponse.UpgradeStatus.MIGRATION_NEEDED;
import static org.hamcrest.Matchers.equalTo;
Expand Down Expand Up @@ -87,12 +87,7 @@ private static ClusterState getClusterState() {
.build();

ClusterState clusterState = new ClusterState.Builder(ClusterState.EMPTY_STATE).metadata(
new Metadata.Builder().indices(
ImmutableOpenMap.<String, IndexMetadata>builder()
.fPut(".test-index-1", indexMetadata1)
.fPut(".test-index-2", indexMetadata2)
.build()
).build()
new Metadata.Builder().indices(Map.of(".test-index-1", indexMetadata1, ".test-index-2", indexMetadata2)).build()
).build();
return clusterState;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import org.elasticsearch.cluster.routing.allocation.command.AllocateReplicaAllocationCommand;
import org.elasticsearch.cluster.routing.allocation.decider.Decision;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.collect.ImmutableOpenMap;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.TransportAddress;
import org.elasticsearch.common.xcontent.XContentHelper;
Expand Down Expand Up @@ -50,9 +49,7 @@ public void testToXContent() throws IOException {
.build()
)
.build();
ImmutableOpenMap.Builder<String, IndexMetadata> openMapBuilder = ImmutableOpenMap.builder();
openMapBuilder.put("index", indexMetadata);
Metadata metadata = Metadata.builder().indices(openMapBuilder.build()).build();
Metadata metadata = Metadata.builder().indices(Map.of("index", indexMetadata)).build();
ClusterState clusterState = ClusterState.builder(new ClusterName("test")).nodes(nodes).metadata(metadata).build();

RoutingExplanations routingExplanations = new RoutingExplanations();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import org.elasticsearch.cluster.metadata.IndexMetadata;
import org.elasticsearch.cluster.metadata.IndexNameExpressionResolver;
import org.elasticsearch.cluster.metadata.Metadata;
import org.elasticsearch.common.collect.ImmutableOpenMap;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.util.concurrent.ThreadContext;
import org.elasticsearch.core.Tuple;
Expand Down Expand Up @@ -45,9 +44,7 @@ public void testPostProcess() {
ClusterState clusterState = ClusterState.builder(ClusterState.EMPTY_STATE).metadata(metadata).build();

GetAliasesRequest request = new GetAliasesRequest();
ImmutableOpenMap<String, List<AliasMetadata>> aliases = ImmutableOpenMap.<String, List<AliasMetadata>>builder()
.fPut("b", Collections.singletonList(new AliasMetadata.Builder("y").build()))
.build();
Map<String, List<AliasMetadata>> aliases = Map.of("b", Collections.singletonList(new AliasMetadata.Builder("y").build()));
Map<String, List<AliasMetadata>> result = TransportGetAliasesAction.postProcess(
request,
new String[] { "a", "b", "c" },
Expand All @@ -64,9 +61,7 @@ public void testPostProcess() {

request = new GetAliasesRequest();
request.replaceAliases("y", "z");
aliases = ImmutableOpenMap.<String, List<AliasMetadata>>builder()
.fPut("b", Collections.singletonList(new AliasMetadata.Builder("y").build()))
.build();
aliases = Map.of("b", Collections.singletonList(new AliasMetadata.Builder("y").build()));
result = TransportGetAliasesAction.postProcess(
request,
new String[] { "a", "b", "c" },
Expand All @@ -82,9 +77,7 @@ public void testPostProcess() {
assertThat(result.get("c").size(), equalTo(0));

request = new GetAliasesRequest("y", "z");
aliases = ImmutableOpenMap.<String, List<AliasMetadata>>builder()
.fPut("b", Collections.singletonList(new AliasMetadata.Builder("y").build()))
.build();
aliases = Map.of("b", Collections.singletonList(new AliasMetadata.Builder("y").build()));
result = TransportGetAliasesAction.postProcess(
request,
new String[] { "a", "b", "c" },
Expand All @@ -102,10 +95,12 @@ public void testDeprecationWarningEmittedForTotalWildcard() {
ClusterState state = systemIndexTestClusterState();

GetAliasesRequest request = new GetAliasesRequest();
ImmutableOpenMap<String, List<AliasMetadata>> aliases = ImmutableOpenMap.<String, List<AliasMetadata>>builder()
.fPut(".b", Collections.singletonList(new AliasMetadata.Builder(".y").build()))
.fPut("c", Collections.singletonList(new AliasMetadata.Builder("d").build()))
.build();
Map<String, List<AliasMetadata>> aliases = Map.of(
".b",
Collections.singletonList(new AliasMetadata.Builder(".y").build()),
"c",
Collections.singletonList(new AliasMetadata.Builder("d").build())
);
final String[] concreteIndices = { "a", ".b", "c" };
assertEquals(state.metadata().findAliases(request.aliases(), concreteIndices), aliases);
Map<String, List<AliasMetadata>> result = TransportGetAliasesAction.postProcess(
Expand Down Expand Up @@ -136,9 +131,7 @@ public void testDeprecationWarningEmittedWhenSystemIndexIsRequested() {

GetAliasesRequest request = new GetAliasesRequest();
request.indices(".b");
ImmutableOpenMap<String, List<AliasMetadata>> aliases = ImmutableOpenMap.<String, List<AliasMetadata>>builder()
.fPut(".b", Collections.singletonList(new AliasMetadata.Builder(".y").build()))
.build();
Map<String, List<AliasMetadata>> aliases = Map.of(".b", Collections.singletonList(new AliasMetadata.Builder(".y").build()));
final String[] concreteIndices = { ".b" };
assertEquals(state.metadata().findAliases(request.aliases(), concreteIndices), aliases);
Map<String, List<AliasMetadata>> result = TransportGetAliasesAction.postProcess(
Expand Down Expand Up @@ -166,9 +159,7 @@ public void testDeprecationWarningEmittedWhenSystemIndexIsRequestedByAlias() {
ClusterState state = systemIndexTestClusterState();

GetAliasesRequest request = new GetAliasesRequest(".y");
ImmutableOpenMap<String, List<AliasMetadata>> aliases = ImmutableOpenMap.<String, List<AliasMetadata>>builder()
.fPut(".b", Collections.singletonList(new AliasMetadata.Builder(".y").build()))
.build();
Map<String, List<AliasMetadata>> aliases = Map.of(".b", Collections.singletonList(new AliasMetadata.Builder(".y").build()));
final String[] concreteIndices = { "a", ".b", "c" };
assertEquals(state.metadata().findAliases(request.aliases(), concreteIndices), aliases);
Map<String, List<AliasMetadata>> result = TransportGetAliasesAction.postProcess(
Expand Down Expand Up @@ -196,9 +187,7 @@ public void testDeprecationWarningNotEmittedWhenSystemAccessAllowed() {
ClusterState state = systemIndexTestClusterState();

GetAliasesRequest request = new GetAliasesRequest(".y");
ImmutableOpenMap<String, List<AliasMetadata>> aliases = ImmutableOpenMap.<String, List<AliasMetadata>>builder()
.fPut(".b", Collections.singletonList(new AliasMetadata.Builder(".y").build()))
.build();
Map<String, List<AliasMetadata>> aliases = Map.of(".b", Collections.singletonList(new AliasMetadata.Builder(".y").build()));
final String[] concreteIndices = { "a", ".b", "c" };
assertEquals(state.metadata().findAliases(request.aliases(), concreteIndices), aliases);
Map<String, List<AliasMetadata>> result = TransportGetAliasesAction.postProcess(
Expand All @@ -222,9 +211,7 @@ public void testDeprecationWarningNotEmittedWhenOnlyNonsystemIndexRequested() {

GetAliasesRequest request = new GetAliasesRequest();
request.indices("c");
ImmutableOpenMap<String, List<AliasMetadata>> aliases = ImmutableOpenMap.<String, List<AliasMetadata>>builder()
.fPut("c", Collections.singletonList(new AliasMetadata.Builder("d").build()))
.build();
Map<String, List<AliasMetadata>> aliases = Map.of("c", Collections.singletonList(new AliasMetadata.Builder("d").build()));
final String[] concreteIndices = { "c" };
assertEquals(state.metadata().findAliases(request.aliases(), concreteIndices), aliases);
Map<String, List<AliasMetadata>> result = TransportGetAliasesAction.postProcess(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import org.elasticsearch.action.admin.indices.mapping.get.GetMappingsResponseTests;
import org.elasticsearch.cluster.metadata.AliasMetadata;
import org.elasticsearch.cluster.metadata.MappingMetadata;
import org.elasticsearch.common.collect.ImmutableOpenMap;
import org.elasticsearch.common.io.stream.Writeable;
import org.elasticsearch.common.settings.IndexScopedSettings;
import org.elasticsearch.common.settings.Settings;
Expand All @@ -23,8 +22,10 @@
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

public class GetIndexResponseTests extends AbstractWireSerializingTestCase<GetIndexResponse> {

Expand All @@ -36,11 +37,11 @@ protected Writeable.Reader<GetIndexResponse> instanceReader() {
@Override
protected GetIndexResponse createTestInstance() {
String[] indices = generateRandomStringArray(5, 5, false, false);
ImmutableOpenMap.Builder<String, MappingMetadata> mappings = ImmutableOpenMap.builder();
ImmutableOpenMap.Builder<String, List<AliasMetadata>> aliases = ImmutableOpenMap.builder();
ImmutableOpenMap.Builder<String, Settings> settings = ImmutableOpenMap.builder();
ImmutableOpenMap.Builder<String, Settings> defaultSettings = ImmutableOpenMap.builder();
ImmutableOpenMap.Builder<String, String> dataStreams = ImmutableOpenMap.builder();
Map<String, MappingMetadata> mappings = new HashMap<>();
Map<String, List<AliasMetadata>> aliases = new HashMap<>();
Map<String, Settings> settings = new HashMap<>();
Map<String, Settings> defaultSettings = new HashMap<>();
Map<String, String> dataStreams = new HashMap<>();
IndexScopedSettings indexScopedSettings = IndexScopedSettings.DEFAULT_SCOPED_SETTINGS;
boolean includeDefaults = randomBoolean();
for (String index : indices) {
Expand Down Expand Up @@ -70,13 +71,6 @@ protected GetIndexResponse createTestInstance() {
dataStreams.put(index, randomAlphaOfLength(5).toLowerCase(Locale.ROOT));
}
}
return new GetIndexResponse(
indices,
mappings.build(),
aliases.build(),
settings.build(),
defaultSettings.build(),
dataStreams.build()
);
return new GetIndexResponse(indices, mappings, aliases, settings, defaultSettings, dataStreams);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
package org.elasticsearch.action.admin.indices.mapping.get;

import org.elasticsearch.cluster.metadata.MappingMetadata;
import org.elasticsearch.common.collect.ImmutableOpenMap;
import org.elasticsearch.common.io.stream.Writeable;
import org.elasticsearch.index.mapper.MapperService;
import org.elasticsearch.test.AbstractWireSerializingTestCase;
Expand Down Expand Up @@ -59,9 +58,7 @@ public static MappingMetadata createMappingsForIndex() {

@Override
protected GetMappingsResponse createTestInstance() {
ImmutableOpenMap.Builder<String, MappingMetadata> indexBuilder = ImmutableOpenMap.builder();
indexBuilder.put("index-" + randomAlphaOfLength(5), createMappingsForIndex());
GetMappingsResponse resp = new GetMappingsResponse(indexBuilder.build());
GetMappingsResponse resp = new GetMappingsResponse(Map.of("index-" + randomAlphaOfLength(5), createMappingsForIndex()));
logger.debug("--> created: {}", resp);
return resp;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

package org.elasticsearch.action.admin.indices.settings.get;

import org.elasticsearch.common.collect.ImmutableOpenMap;
import org.elasticsearch.common.io.stream.Writeable;
import org.elasticsearch.common.settings.IndexScopedSettings;
import org.elasticsearch.common.settings.Settings;
Expand Down Expand Up @@ -48,9 +47,6 @@ protected GetSettingsResponse createTestInstance() {
builder.put("index.refresh_interval", "1s");
indexToSettings.put(indexName, builder.build());
}
ImmutableOpenMap<String, Settings> immutableIndexToSettings = ImmutableOpenMap.<String, Settings>builder()
.putAllFromMap(indexToSettings)
.build();

if (randomBoolean()) {
for (String indexName : indexToSettings.keySet()) {
Expand All @@ -59,11 +55,7 @@ protected GetSettingsResponse createTestInstance() {
}
}

ImmutableOpenMap<String, Settings> immutableIndexToDefaultSettings = ImmutableOpenMap.<String, Settings>builder()
.putAllFromMap(indexToDefaultSettings)
.build();

return new GetSettingsResponse(immutableIndexToSettings, immutableIndexToDefaultSettings);
return new GetSettingsResponse(indexToSettings, indexToDefaultSettings);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -202,28 +202,22 @@ public void setupAction() {
when(state.getNodes()).thenReturn(nodes);
Metadata metadata = Metadata.builder()
.indices(
ImmutableOpenMap.<String, IndexMetadata>builder()
.putAllFromMap(
Map.of(
WITH_DEFAULT_PIPELINE,
IndexMetadata.builder(WITH_DEFAULT_PIPELINE)
.settings(
settings(Version.CURRENT).put(IndexSettings.DEFAULT_PIPELINE.getKey(), "default_pipeline").build()
)
.putAlias(AliasMetadata.builder(WITH_DEFAULT_PIPELINE_ALIAS).build())
.numberOfShards(1)
.numberOfReplicas(1)
.build(),
".system",
IndexMetadata.builder(".system")
.settings(settings(Version.CURRENT))
.system(true)
.numberOfShards(1)
.numberOfReplicas(0)
.build()
)
)
.build()
Map.of(
WITH_DEFAULT_PIPELINE,
IndexMetadata.builder(WITH_DEFAULT_PIPELINE)
.settings(settings(Version.CURRENT).put(IndexSettings.DEFAULT_PIPELINE.getKey(), "default_pipeline").build())
.putAlias(AliasMetadata.builder(WITH_DEFAULT_PIPELINE_ALIAS).build())
.numberOfShards(1)
.numberOfReplicas(1)
.build(),
".system",
IndexMetadata.builder(".system")
.settings(settings(Version.CURRENT))
.system(true)
.numberOfShards(1)
.numberOfReplicas(0)
.build()
)
)
.build();
when(state.getMetadata()).thenReturn(metadata);
Expand Down Expand Up @@ -667,7 +661,7 @@ public void testFindDefaultPipelineFromTemplateMatch() {
when(state.getMetadata()).thenReturn(metadata);
when(metadata.templates()).thenReturn(templateMetadata);
when(metadata.getTemplates()).thenReturn(templateMetadata);
when(metadata.indices()).thenReturn(ImmutableOpenMap.of());
when(metadata.indices()).thenReturn(Map.of());

IndexRequest indexRequest = new IndexRequest("missing_index").id("id");
indexRequest.source(Collections.emptyMap());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import org.elasticsearch.cluster.routing.TestShardRouting;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.UUIDs;
import org.elasticsearch.common.collect.ImmutableOpenMap;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.TransportAddress;
import org.elasticsearch.common.xcontent.XContentHelper;
Expand Down Expand Up @@ -102,10 +101,9 @@ public void testBuilderRejectsNullCustom() {
public void testBuilderRejectsNullInCustoms() {
final ClusterState.Builder builder = ClusterState.builder(ClusterName.DEFAULT);
final String key = randomAlphaOfLength(10);
final ImmutableOpenMap.Builder<String, ClusterState.Custom> mapBuilder = ImmutableOpenMap.builder();
mapBuilder.put(key, null);
final ImmutableOpenMap<String, ClusterState.Custom> map = mapBuilder.build();
assertThat(expectThrows(NullPointerException.class, () -> builder.customs(map)).getMessage(), containsString(key));
final Map<String, ClusterState.Custom> customs = new HashMap<>();
customs.put(key, null);
assertThat(expectThrows(NullPointerException.class, () -> builder.customs(customs)).getMessage(), containsString(key));
}

public void testCopyAndUpdate() throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import org.elasticsearch.cluster.routing.allocation.DataTier;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.collect.ImmutableOpenMap;
import org.elasticsearch.common.io.stream.BytesStreamOutput;
import org.elasticsearch.common.io.stream.NamedWriteableAwareStreamInput;
import org.elasticsearch.common.io.stream.NamedWriteableRegistry;
Expand Down Expand Up @@ -124,9 +123,7 @@ public void testIndexMetadataSerialization() throws IOException {
assertEquals(metadata.getRoutingFactor(), fromXContentMeta.getRoutingFactor());
assertEquals(metadata.primaryTerm(0), fromXContentMeta.primaryTerm(0));
assertEquals(metadata.isSystem(), fromXContentMeta.isSystem());
ImmutableOpenMap.Builder<String, DiffableStringMap> expectedCustomBuilder = ImmutableOpenMap.builder();
expectedCustomBuilder.put("my_custom", new DiffableStringMap(customMap));
ImmutableOpenMap<String, DiffableStringMap> expectedCustom = expectedCustomBuilder.build();
Map<String, DiffableStringMap> expectedCustom = Map.of("my_custom", new DiffableStringMap(customMap));
assertEquals(metadata.getCustomData(), expectedCustom);
assertEquals(metadata.getCustomData(), fromXContentMeta.getCustomData());

Expand Down

0 comments on commit a9d1986

Please sign in to comment.