Skip to content

Commit

Permalink
Add new base test case for chunked xcontent types (#90707)
Browse files Browse the repository at this point in the history
Making sure that none of the chunked implementations also implement
`toXContent`. Added some utilities for working with chunked xcontent
that mirror xcontent utilities to keep the changeset as quiet as possible.
No big production code changes here, this mostly affects test code paths.
  • Loading branch information
original-brownbear committed Oct 6, 2022
1 parent fcd0ce3 commit aa7a8a0
Show file tree
Hide file tree
Showing 280 changed files with 706 additions and 595 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@
package org.elasticsearch.ingest.geoip;

import org.elasticsearch.common.io.stream.Writeable;
import org.elasticsearch.test.AbstractSerializingTestCase;
import org.elasticsearch.test.AbstractXContentSerializingTestCase;
import org.elasticsearch.xcontent.XContentParser;

import java.io.IOException;

public class GeoIpTaskStateSerializationTests extends AbstractSerializingTestCase<GeoIpTaskState> {
public class GeoIpTaskStateSerializationTests extends AbstractXContentSerializingTestCase<GeoIpTaskState> {
@Override
protected GeoIpTaskState doParseInstance(XContentParser parser) throws IOException {
return GeoIpTaskState.fromXContent(parser);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@
package org.elasticsearch.ingest.geoip.stats;

import org.elasticsearch.common.io.stream.Writeable;
import org.elasticsearch.test.AbstractSerializingTestCase;
import org.elasticsearch.test.AbstractXContentSerializingTestCase;
import org.elasticsearch.xcontent.XContentParser;

import java.io.IOException;

public class GeoIpDownloaderStatsSerializingTests extends AbstractSerializingTestCase<GeoIpDownloaderStats> {
public class GeoIpDownloaderStatsSerializingTests extends AbstractXContentSerializingTestCase<GeoIpDownloaderStats> {

@Override
protected GeoIpDownloaderStats doParseInstance(XContentParser parser) throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@
package org.elasticsearch.painless.action;

import org.elasticsearch.common.io.stream.Writeable;
import org.elasticsearch.test.AbstractSerializingTestCase;
import org.elasticsearch.test.AbstractXContentSerializingTestCase;
import org.elasticsearch.xcontent.XContentParser;

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

public class ContextInfoTests extends AbstractSerializingTestCase<PainlessContextInfo> {
public class ContextInfoTests extends AbstractXContentSerializingTestCase<PainlessContextInfo> {

@Override
protected PainlessContextInfo doParseInstance(XContentParser parser) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@
package org.elasticsearch.painless.action;

import org.elasticsearch.common.io.stream.Writeable;
import org.elasticsearch.test.AbstractSerializingTestCase;
import org.elasticsearch.test.AbstractXContentSerializingTestCase;
import org.elasticsearch.xcontent.XContentParser;

import java.io.IOException;

public class PainlessExecuteResponseTests extends AbstractSerializingTestCase<PainlessExecuteAction.Response> {
public class PainlessExecuteResponseTests extends AbstractXContentSerializingTestCase<PainlessExecuteAction.Response> {

@Override
protected Writeable.Reader<PainlessExecuteAction.Response> instanceReader() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
package org.elasticsearch.common.xcontent;

import org.elasticsearch.xcontent.ToXContent;
import org.elasticsearch.xcontent.ToXContentObject;
import org.elasticsearch.xcontent.XContentBuilder;

import java.io.IOException;
Expand All @@ -30,10 +31,21 @@ public interface ChunkedToXContent extends ToXContent {

@Override
default XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
Iterator<? extends ToXContent> serialization = toXContentChunked();
while (serialization.hasNext()) {
serialization.next().toXContent(builder, params);
}
return builder;
return wrapAsXContentObject(this).toXContent(builder, params);
}

/**
* Wraps the given instance in a {@link ToXContentObject} that will fully serialize the instance when serialized.
* @param chunkedToXContent instance to wrap
* @return x-content object
*/
static ToXContentObject wrapAsXContentObject(ChunkedToXContent chunkedToXContent) {
return (builder, params) -> {
Iterator<? extends ToXContent> serialization = chunkedToXContent.toXContentChunked();
while (serialization.hasNext()) {
serialization.next().toXContent(builder, params);
}
return builder;
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import org.elasticsearch.common.util.Maps;
import org.elasticsearch.core.TimeValue;
import org.elasticsearch.rest.RestStatus;
import org.elasticsearch.test.AbstractSerializingTestCase;
import org.elasticsearch.test.AbstractXContentSerializingTestCase;
import org.elasticsearch.xcontent.ToXContent;
import org.elasticsearch.xcontent.XContentParser;
import org.hamcrest.Matchers;
Expand All @@ -39,7 +39,7 @@
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.lessThanOrEqualTo;

public class ClusterHealthResponsesTests extends AbstractSerializingTestCase<ClusterHealthResponse> {
public class ClusterHealthResponsesTests extends AbstractXContentSerializingTestCase<ClusterHealthResponse> {
private final ClusterHealthRequest.Level level = randomFrom(ClusterHealthRequest.Level.values());

public void testIsTimeout() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@
import org.elasticsearch.common.settings.Setting;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.settings.Settings.Builder;
import org.elasticsearch.test.AbstractSerializingTestCase;
import org.elasticsearch.test.AbstractXContentSerializingTestCase;
import org.elasticsearch.xcontent.XContentParser;

import java.util.List;
import java.util.Set;
import java.util.function.Predicate;

public class ClusterUpdateSettingsResponseTests extends AbstractSerializingTestCase<ClusterUpdateSettingsResponse> {
public class ClusterUpdateSettingsResponseTests extends AbstractXContentSerializingTestCase<ClusterUpdateSettingsResponse> {

@Override
protected ClusterUpdateSettingsResponse doParseInstance(XContentParser parser) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import org.elasticsearch.Version;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.UUIDs;
import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.io.stream.NamedWriteableRegistry;
import org.elasticsearch.index.shard.ShardId;
import org.elasticsearch.snapshots.Snapshot;
Expand All @@ -24,9 +23,8 @@
import org.elasticsearch.snapshots.SnapshotShardFailure;
import org.elasticsearch.test.ESTestCase;
import org.elasticsearch.xcontent.ToXContent;
import org.elasticsearch.xcontent.XContentBuilder;
import org.elasticsearch.xcontent.XContentParser;
import org.elasticsearch.xcontent.json.JsonXContent;
import org.elasticsearch.xcontent.XContentType;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
Expand All @@ -44,8 +42,7 @@
import java.util.regex.Pattern;

import static org.elasticsearch.snapshots.SnapshotInfo.INDEX_DETAILS_XCONTENT_PARAM;
import static org.elasticsearch.test.AbstractXContentTestCase.xContentTester;
import static org.elasticsearch.xcontent.ToXContent.EMPTY_PARAMS;
import static org.elasticsearch.test.AbstractXContentTestCase.chunkedXContentTester;
import static org.hamcrest.CoreMatchers.containsString;

public class GetSnapshotsResponseTests extends ESTestCase {
Expand Down Expand Up @@ -167,7 +164,9 @@ public void testFromXContent() throws IOException {
.asMatchPredicate()
.or(Pattern.compile("snapshots\\.\\d+\\.index_details").asMatchPredicate())
.or(Pattern.compile("failures\\.*").asMatchPredicate());
xContentTester(this::createParser, this::createTestInstance, params, this::doParseInstance).numberOfTestRuns(1)
chunkedXContentTester(this::createParser, (XContentType t) -> createTestInstance(), params, this::doParseInstance).numberOfTestRuns(
1
)
.supportsUnknownFields(true)
.shuffleFieldsExceptions(Strings.EMPTY_ARRAY)
.randomFieldsExcludeFilter(predicate)
Expand All @@ -178,19 +177,15 @@ public void testFromXContent() throws IOException {
.test();
}

public void testToChunkedXContent() throws Exception {
public void testToChunkedXContent() {
final GetSnapshotsResponse response = createTestInstance();
final XContentBuilder builder = JsonXContent.contentBuilder();
final Iterator<ToXContent> serialization = response.toXContentChunked();
serialization.next().toXContent(builder, EMPTY_PARAMS);
for (int i = 0; i < response.getSnapshots().size(); i++) {
serialization.next().toXContent(builder, EMPTY_PARAMS);
assertTrue(serialization.hasNext());
int chunks = 0;
while (serialization.hasNext()) {
serialization.next();
chunks++;
}
serialization.next().toXContent(builder, EMPTY_PARAMS);
assertFalse(serialization.hasNext());
final BytesReference bytesReferenceFromChunked = BytesReference.bytes(builder);
assertEquals(bytesReferenceFromChunked, BytesReference.bytes(response.toXContent(JsonXContent.contentBuilder(), EMPTY_PARAMS)));
assertEquals(chunks, response.getSnapshots().size() + 2);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@
package org.elasticsearch.action.admin.cluster.storedscripts;

import org.elasticsearch.common.io.stream.Writeable;
import org.elasticsearch.test.AbstractSerializingTestCase;
import org.elasticsearch.test.AbstractXContentSerializingTestCase;
import org.elasticsearch.xcontent.XContentParser;

import java.io.IOException;
import java.util.Collections;

public class GetScriptContextResponseTests extends AbstractSerializingTestCase<GetScriptContextResponse> {
public class GetScriptContextResponseTests extends AbstractXContentSerializingTestCase<GetScriptContextResponse> {

@Override
protected GetScriptContextResponse createTestInstance() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import org.elasticsearch.common.io.stream.Writeable;
import org.elasticsearch.common.util.set.Sets;
import org.elasticsearch.script.ScriptLanguagesInfo;
import org.elasticsearch.test.AbstractSerializingTestCase;
import org.elasticsearch.test.AbstractXContentSerializingTestCase;
import org.elasticsearch.xcontent.XContentParser;

import java.io.IOException;
Expand All @@ -23,7 +23,7 @@
import java.util.Map;
import java.util.Set;

public class GetScriptLanguageResponseTests extends AbstractSerializingTestCase<GetScriptLanguageResponse> {
public class GetScriptLanguageResponseTests extends AbstractXContentSerializingTestCase<GetScriptLanguageResponse> {
private static int MAX_VALUES = 4;
private static final int MIN_LENGTH = 1;
private static final int MAX_LENGTH = 16;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import org.elasticsearch.common.io.stream.Writeable;
import org.elasticsearch.script.Script;
import org.elasticsearch.script.StoredScriptSource;
import org.elasticsearch.test.AbstractSerializingTestCase;
import org.elasticsearch.test.AbstractXContentSerializingTestCase;
import org.elasticsearch.xcontent.XContentParser;
import org.elasticsearch.xcontent.XContentType;

Expand All @@ -20,7 +20,7 @@
import java.util.Map;
import java.util.function.Predicate;

public class GetStoredScriptResponseTests extends AbstractSerializingTestCase<GetStoredScriptResponse> {
public class GetStoredScriptResponseTests extends AbstractXContentSerializingTestCase<GetStoredScriptResponse> {

@Override
protected GetStoredScriptResponse doParseInstance(XContentParser parser) throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import org.elasticsearch.common.io.stream.Writeable;
import org.elasticsearch.common.util.set.Sets;
import org.elasticsearch.script.ScriptContextInfo;
import org.elasticsearch.test.AbstractSerializingTestCase;
import org.elasticsearch.test.AbstractXContentSerializingTestCase;
import org.elasticsearch.xcontent.XContentParser;

import java.io.IOException;
Expand All @@ -22,7 +22,7 @@
import java.util.Set;
import java.util.stream.Collectors;

public class ScriptContextInfoSerializingTests extends AbstractSerializingTestCase<ScriptContextInfo> {
public class ScriptContextInfoSerializingTests extends AbstractXContentSerializingTestCase<ScriptContextInfo> {
private static final int MIN_LENGTH = 1;
private static final int MAX_LENGTH = 16;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import org.elasticsearch.common.io.stream.Writeable;
import org.elasticsearch.common.util.set.Sets;
import org.elasticsearch.script.ScriptContextInfo.ScriptMethodInfo;
import org.elasticsearch.test.AbstractSerializingTestCase;
import org.elasticsearch.test.AbstractXContentSerializingTestCase;
import org.elasticsearch.xcontent.XContentParser;

import java.io.IOException;
Expand All @@ -20,7 +20,7 @@
import java.util.HashSet;
import java.util.Set;

public class ScriptMethodInfoSerializingTests extends AbstractSerializingTestCase<ScriptMethodInfo> {
public class ScriptMethodInfoSerializingTests extends AbstractXContentSerializingTestCase<ScriptMethodInfo> {
private static final String EXECUTE = "execute";
private static final String GET_PREFIX = "get";
private static final int MIN_LENGTH = 1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

import org.elasticsearch.common.io.stream.Writeable;
import org.elasticsearch.script.ScriptContextInfo.ScriptMethodInfo.ParameterInfo;
import org.elasticsearch.test.AbstractSerializingTestCase;
import org.elasticsearch.test.AbstractXContentSerializingTestCase;
import org.elasticsearch.xcontent.XContentParser;

import java.io.IOException;
Expand All @@ -20,7 +20,7 @@
import java.util.List;
import java.util.Set;

public class ScriptParameterInfoSerializingTests extends AbstractSerializingTestCase<ParameterInfo> {
public class ScriptParameterInfoSerializingTests extends AbstractXContentSerializingTestCase<ParameterInfo> {
private static int minLength = 1;
private static int maxLength = 8;
private static String baseType = "type-";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@

import org.elasticsearch.common.Strings;
import org.elasticsearch.common.io.stream.Writeable;
import org.elasticsearch.test.AbstractSerializingTestCase;
import org.elasticsearch.test.AbstractXContentSerializingTestCase;
import org.elasticsearch.xcontent.XContentParser;

public class CreateIndexResponseTests extends AbstractSerializingTestCase<CreateIndexResponse> {
public class CreateIndexResponseTests extends AbstractXContentSerializingTestCase<CreateIndexResponse> {

@Override
protected CreateIndexResponse createTestInstance() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
package org.elasticsearch.action.admin.indices.open;

import org.elasticsearch.common.io.stream.Writeable;
import org.elasticsearch.test.AbstractSerializingTestCase;
import org.elasticsearch.test.AbstractXContentSerializingTestCase;
import org.elasticsearch.xcontent.XContentParser;

public class OpenIndexResponseTests extends AbstractSerializingTestCase<OpenIndexResponse> {
public class OpenIndexResponseTests extends AbstractXContentSerializingTestCase<OpenIndexResponse> {

@Override
protected OpenIndexResponse doParseInstance(XContentParser parser) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import org.elasticsearch.action.admin.indices.resolve.ResolveIndexAction.Response;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.io.stream.Writeable;
import org.elasticsearch.test.AbstractSerializingTestCase;
import org.elasticsearch.test.AbstractXContentSerializingTestCase;
import org.elasticsearch.xcontent.ConstructingObjectParser;
import org.elasticsearch.xcontent.XContentParser;

Expand All @@ -31,7 +31,7 @@
import static org.elasticsearch.action.admin.indices.resolve.ResolveIndexAction.Response.DATA_STREAMS_FIELD;
import static org.elasticsearch.action.admin.indices.resolve.ResolveIndexAction.Response.INDICES_FIELD;

public class ResolveIndexResponseTests extends AbstractSerializingTestCase<Response> {
public class ResolveIndexResponseTests extends AbstractXContentSerializingTestCase<Response> {

@Override
protected Writeable.Reader<Response> instanceReader() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,11 @@ public void testToXContentSerialiationWithSortedFields() throws Exception {
0,
Collections.emptyList()
);
var serialization = response.toXContentChunked();
try (XContentBuilder builder = jsonBuilder()) {
response.toXContent(builder, ToXContent.EMPTY_PARAMS);
while (serialization.hasNext()) {
serialization.next().toXContent(builder, ToXContent.EMPTY_PARAMS);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import org.elasticsearch.common.settings.IndexScopedSettings;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.index.RandomCreateIndexGenerator;
import org.elasticsearch.test.AbstractSerializingTestCase;
import org.elasticsearch.test.AbstractChunkedSerializingTestCase;
import org.elasticsearch.xcontent.XContentParser;

import java.io.IOException;
Expand All @@ -21,7 +21,7 @@
import java.util.Set;
import java.util.function.Predicate;

public class GetSettingsResponseTests extends AbstractSerializingTestCase<GetSettingsResponse> {
public class GetSettingsResponseTests extends AbstractChunkedSerializingTestCase<GetSettingsResponse> {

@Override
protected GetSettingsResponse createTestInstance() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@

import org.elasticsearch.common.Strings;
import org.elasticsearch.common.io.stream.Writeable;
import org.elasticsearch.test.AbstractSerializingTestCase;
import org.elasticsearch.test.AbstractXContentSerializingTestCase;
import org.elasticsearch.xcontent.XContentParser;

public class ResizeResponseTests extends AbstractSerializingTestCase<ResizeResponse> {
public class ResizeResponseTests extends AbstractXContentSerializingTestCase<ResizeResponse> {

public void testToXContent() {
ResizeResponse response = new ResizeResponse(true, false, "index_name");
Expand Down

0 comments on commit aa7a8a0

Please sign in to comment.