Skip to content

Commit bdba82a

Browse files
authored
Move HLRC GetIndexResponseTests to AbstractResponseTestCase (#66934)
This was done in master via #47364, but that change was never backported to 7.x. This change only includes the changes to GetIndexResponseTests from that pr. This should also fix the failure in #66653, since no fields are shuffled in the server side response. Closes #66653
1 parent 338afe3 commit bdba82a

File tree

2 files changed

+57
-72
lines changed

2 files changed

+57
-72
lines changed

client/rest-high-level/src/test/java/org/elasticsearch/client/AbstractResponseTestCase.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
package org.elasticsearch.client;
2020

2121
import org.elasticsearch.common.bytes.BytesReference;
22+
import org.elasticsearch.common.collect.ImmutableOpenMap;
2223
import org.elasticsearch.common.xcontent.LoggingDeprecationHandler;
2324
import org.elasticsearch.common.xcontent.NamedXContentRegistry;
2425
import org.elasticsearch.common.xcontent.ToXContent;
@@ -29,6 +30,10 @@
2930
import org.elasticsearch.test.ESTestCase;
3031

3132
import java.io.IOException;
33+
import java.util.HashSet;
34+
import java.util.Iterator;
35+
import java.util.Map;
36+
import java.util.Set;
3237

3338
/**
3439
* Base class for HLRC response parsing tests.
@@ -85,4 +90,16 @@ protected ToXContent.Params getParams() {
8590
return ToXContent.EMPTY_PARAMS;
8691
}
8792

93+
protected static <T> void assertMapEquals(ImmutableOpenMap<String, T> expected, Map<String, T> actual) {
94+
Set<String> expectedKeys = new HashSet<>();
95+
Iterator<String> keysIt = expected.keysIt();
96+
while (keysIt.hasNext()) {
97+
expectedKeys.add(keysIt.next());
98+
}
99+
100+
assertEquals(expectedKeys, actual.keySet());
101+
for (String key : expectedKeys) {
102+
assertEquals(expected.get(key), actual.get(key));
103+
}
104+
}
88105
}

client/rest-high-level/src/test/java/org/elasticsearch/client/indices/GetIndexResponseTests.java

Lines changed: 40 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,19 @@
1919

2020
package org.elasticsearch.client.indices;
2121

22+
import com.carrotsearch.hppc.cursors.ObjectObjectCursor;
2223
import org.apache.lucene.util.CollectionUtil;
24+
import org.elasticsearch.client.AbstractResponseTestCase;
2325
import org.elasticsearch.client.GetAliasesResponseTests;
2426
import org.elasticsearch.cluster.metadata.AliasMetadata;
2527
import org.elasticsearch.cluster.metadata.MappingMetadata;
2628
import org.elasticsearch.common.collect.ImmutableOpenMap;
2729
import org.elasticsearch.common.settings.IndexScopedSettings;
2830
import org.elasticsearch.common.settings.Settings;
29-
import org.elasticsearch.common.xcontent.ToXContent;
30-
import org.elasticsearch.common.xcontent.ToXContent.Params;
31-
import org.elasticsearch.common.xcontent.XContentBuilder;
31+
import org.elasticsearch.common.xcontent.XContentParser;
32+
import org.elasticsearch.common.xcontent.XContentType;
3233
import org.elasticsearch.index.RandomCreateIndexGenerator;
3334
import org.elasticsearch.index.mapper.MapperService;
34-
import org.elasticsearch.rest.BaseRestHandler;
35-
import org.elasticsearch.test.ESTestCase;
3635

3736
import java.io.IOException;
3837
import java.util.ArrayList;
@@ -44,44 +43,25 @@
4443
import java.util.Map;
4544
import java.util.Objects;
4645

47-
import static org.elasticsearch.test.AbstractXContentTestCase.xContentTester;
48-
49-
public class GetIndexResponseTests extends ESTestCase {
50-
51-
// Because the client-side class does not have a toXContent method, we test xContent serialization by creating
52-
// a random client object, converting it to a server object then serializing it to xContent, and finally
53-
// parsing it back as a client object. We check equality between the original client object, and the parsed one.
54-
public void testFromXContent() throws IOException {
55-
xContentTester(
56-
this::createParser,
57-
GetIndexResponseTests::createTestInstance,
58-
GetIndexResponseTests::toXContent,
59-
GetIndexResponse::fromXContent)
60-
.supportsUnknownFields(false)
61-
.assertToXContentEquivalence(false)
62-
.assertEqualsConsumer(GetIndexResponseTests::assertEqualInstances)
63-
.test();
64-
}
46+
import static org.hamcrest.Matchers.equalTo;
6547

66-
private static void assertEqualInstances(GetIndexResponse expected, GetIndexResponse actual) {
67-
assertArrayEquals(expected.getIndices(), actual.getIndices());
68-
assertEquals(expected.getMappings(), actual.getMappings());
69-
assertEquals(expected.getSettings(), actual.getSettings());
70-
assertEquals(expected.getDefaultSettings(), actual.getDefaultSettings());
71-
assertEquals(expected.getAliases(), actual.getAliases());
72-
}
48+
public class GetIndexResponseTests extends AbstractResponseTestCase<org.elasticsearch.action.admin.indices.get.GetIndexResponse,
49+
GetIndexResponse> {
7350

74-
private static GetIndexResponse createTestInstance() {
51+
@Override
52+
protected org.elasticsearch.action.admin.indices.get.GetIndexResponse createServerTestInstance(XContentType xContentType) {
7553
String[] indices = generateRandomStringArray(5, 5, false, false);
76-
Map<String, MappingMetadata> mappings = new HashMap<>();
77-
Map<String, List<AliasMetadata>> aliases = new HashMap<>();
78-
Map<String, Settings> settings = new HashMap<>();
79-
Map<String, Settings> defaultSettings = new HashMap<>();
80-
Map<String, String> dataStreams = new HashMap<>();
54+
ImmutableOpenMap.Builder<String, ImmutableOpenMap<String, MappingMetadata>> mappings = ImmutableOpenMap.builder();
55+
ImmutableOpenMap.Builder<String, List<AliasMetadata>> aliases = ImmutableOpenMap.builder();
56+
ImmutableOpenMap.Builder<String, Settings> settings = ImmutableOpenMap.builder();
57+
ImmutableOpenMap.Builder<String, Settings> defaultSettings = ImmutableOpenMap.builder();
58+
ImmutableOpenMap.Builder<String, String> dataStreams = ImmutableOpenMap.builder();
8159
IndexScopedSettings indexScopedSettings = IndexScopedSettings.DEFAULT_SCOPED_SETTINGS;
8260
boolean includeDefaults = randomBoolean();
8361
for (String index: indices) {
84-
mappings.put(index, createMappingsForIndex());
62+
ImmutableOpenMap.Builder<String, MappingMetadata> indexMapping = ImmutableOpenMap.builder();
63+
indexMapping.put(MapperService.SINGLE_MAPPING_NAME, createMappingsForIndex());
64+
mappings.put(index, indexMapping.build());
8565

8666
List<AliasMetadata> aliasMetadataList = new ArrayList<>();
8767
int aliasesNum = randomIntBetween(0, 3);
@@ -103,7 +83,29 @@ private static GetIndexResponse createTestInstance() {
10383
dataStreams.put(index, randomAlphaOfLength(5).toLowerCase(Locale.ROOT));
10484
}
10585
}
106-
return new GetIndexResponse(indices, mappings, aliases, settings, defaultSettings, dataStreams);
86+
return new org.elasticsearch.action.admin.indices.get.GetIndexResponse(indices,
87+
mappings.build(), aliases.build(), settings.build(), defaultSettings.build(), dataStreams.build());
88+
}
89+
90+
@Override
91+
protected GetIndexResponse doParseToClientInstance(XContentParser parser) throws IOException {
92+
return GetIndexResponse.fromXContent(parser);
93+
}
94+
95+
@Override
96+
protected void assertInstances(org.elasticsearch.action.admin.indices.get.GetIndexResponse serverTestInstance,
97+
GetIndexResponse clientInstance) {
98+
assertArrayEquals(serverTestInstance.getIndices(), clientInstance.getIndices());
99+
assertThat(serverTestInstance.getMappings().size(), equalTo(clientInstance.getMappings().size()));
100+
for (ObjectObjectCursor<String, ImmutableOpenMap<String, MappingMetadata>> cursor : serverTestInstance.getMappings()) {
101+
MappingMetadata serverMapping = cursor.value.get(MapperService.SINGLE_MAPPING_NAME);
102+
MappingMetadata clientMapping = clientInstance.getMappings().get(cursor.key);
103+
assertThat(serverMapping, equalTo(clientMapping));
104+
}
105+
assertMapEquals(serverTestInstance.getSettings(), clientInstance.getSettings());
106+
assertMapEquals(serverTestInstance.defaultSettings(), clientInstance.getDefaultSettings());
107+
assertMapEquals(serverTestInstance.getAliases(), clientInstance.getAliases());
108+
assertMapEquals(serverTestInstance.getDataStreams(), clientInstance.getDataStreams());
107109
}
108110

109111
private static MappingMetadata createMappingsForIndex() {
@@ -166,38 +168,4 @@ private static Map<String, Object> randomFieldMapping() {
166168
return mappings;
167169
}
168170

169-
private static void toXContent(GetIndexResponse response, XContentBuilder builder) throws IOException {
170-
// first we need to repackage from GetIndexResponse to org.elasticsearch.action.admin.indices.get.GetIndexResponse
171-
ImmutableOpenMap.Builder<String, ImmutableOpenMap<String, MappingMetadata>> allMappings = ImmutableOpenMap.builder();
172-
ImmutableOpenMap.Builder<String, List<AliasMetadata>> aliases = ImmutableOpenMap.builder();
173-
ImmutableOpenMap.Builder<String, Settings> settings = ImmutableOpenMap.builder();
174-
ImmutableOpenMap.Builder<String, Settings> defaultSettings = ImmutableOpenMap.builder();
175-
176-
Map<String, MappingMetadata> indexMappings = response.getMappings();
177-
for (String index : response.getIndices()) {
178-
MappingMetadata mmd = indexMappings.get(index);
179-
ImmutableOpenMap.Builder<String, MappingMetadata> typedMappings = ImmutableOpenMap.builder();
180-
if (mmd != null) {
181-
typedMappings.put(MapperService.SINGLE_MAPPING_NAME, mmd);
182-
}
183-
allMappings.put(index, typedMappings.build());
184-
aliases.put(index, response.getAliases().get(index));
185-
settings.put(index, response.getSettings().get(index));
186-
defaultSettings.put(index, response.getDefaultSettings().get(index));
187-
}
188-
189-
org.elasticsearch.action.admin.indices.get.GetIndexResponse serverResponse
190-
= new org.elasticsearch.action.admin.indices.get.GetIndexResponse(
191-
response.getIndices(),
192-
allMappings.build(),
193-
aliases.build(),
194-
settings.build(),
195-
defaultSettings.build(),
196-
ImmutableOpenMap.<String, String>builder().build()
197-
);
198-
199-
// then we can call its toXContent method, forcing no output of types
200-
Params params = new ToXContent.MapParams(Collections.singletonMap(BaseRestHandler.INCLUDE_TYPE_NAME_PARAMETER, "false"));
201-
serverResponse.toXContent(builder, params);
202-
}
203171
}

0 commit comments

Comments
 (0)