Add realistic hlrc response serialization test base class (#39844)
The base class facilitates generating a server side response test instance, that gets serialized as xcontent, which then gets parsed into a hlrc response instance, which then gets asserted against the server side response instance. This way of testing is more realistic then how hlrc response classes are tested today, which basically tests that serialization works by generating hlrc response instance, serialize that to xcontent and then parse it back to a hlrc response instance. Besides adding a base test class, this change also cuts AcknowledgedResponseTests and BroadcastResponseTests over to use this base class. Relates to #39745
This commit is contained in:
parent
522f33e9eb
commit
a478196f79
|
@ -0,0 +1,70 @@
|
|||
/*
|
||||
* Licensed to Elasticsearch under one or more contributor
|
||||
* license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright
|
||||
* ownership. Elasticsearch licenses this file to you under
|
||||
* the Apache License, Version 2.0 (the "License"); you may
|
||||
* not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
package org.elasticsearch.client;
|
||||
|
||||
import org.elasticsearch.cluster.ClusterModule;
|
||||
import org.elasticsearch.common.bytes.BytesReference;
|
||||
import org.elasticsearch.common.xcontent.LoggingDeprecationHandler;
|
||||
import org.elasticsearch.common.xcontent.NamedXContentRegistry;
|
||||
import org.elasticsearch.common.xcontent.ToXContent;
|
||||
import org.elasticsearch.common.xcontent.XContent;
|
||||
import org.elasticsearch.common.xcontent.XContentFactory;
|
||||
import org.elasticsearch.common.xcontent.XContentParser;
|
||||
import org.elasticsearch.common.xcontent.XContentType;
|
||||
import org.elasticsearch.test.ESTestCase;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* Base class for HLRC response parsing tests.
|
||||
*
|
||||
* This case class facilitates generating server side reponse test instances and
|
||||
* verifies that they are correctly parsed into HLRC response instances.
|
||||
*
|
||||
* @param <S> The class representing the response on the server side.
|
||||
* @param <C> The class representing the response on the client side.
|
||||
*/
|
||||
public abstract class AbstractResponseTestCase<S extends ToXContent, C> extends ESTestCase {
|
||||
|
||||
private static final int NUMBER_OF_TEST_RUNS = 20;
|
||||
|
||||
public final void testFromXContent() throws IOException {
|
||||
for (int i = 0; i < NUMBER_OF_TEST_RUNS; i++) {
|
||||
final S serverTestInstance = createServerTestInstance();
|
||||
|
||||
final XContentType xContentType = randomFrom(XContentType.values());
|
||||
final BytesReference bytes = toShuffledXContent(serverTestInstance, xContentType, ToXContent.EMPTY_PARAMS, randomBoolean());
|
||||
|
||||
final XContent xContent = XContentFactory.xContent(xContentType);
|
||||
final XContentParser parser = xContent.createParser(
|
||||
new NamedXContentRegistry(ClusterModule.getNamedXWriteables()),
|
||||
LoggingDeprecationHandler.INSTANCE,
|
||||
bytes.streamInput());
|
||||
final C clientInstance = doParseToClientInstance(parser);
|
||||
assertInstances(serverTestInstance, clientInstance);
|
||||
}
|
||||
}
|
||||
|
||||
protected abstract S createServerTestInstance();
|
||||
|
||||
protected abstract C doParseToClientInstance(XContentParser parser) throws IOException;
|
||||
|
||||
protected abstract void assertInstances(S serverTestInstance, C clientInstance);
|
||||
|
||||
}
|
|
@ -18,27 +18,35 @@
|
|||
*/
|
||||
package org.elasticsearch.client.core;
|
||||
|
||||
import org.elasticsearch.client.AbstractResponseTestCase;
|
||||
import org.elasticsearch.common.xcontent.XContentBuilder;
|
||||
import org.elasticsearch.test.ESTestCase;
|
||||
import org.elasticsearch.common.xcontent.XContentParser;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import static org.elasticsearch.test.AbstractXContentTestCase.xContentTester;
|
||||
import static org.hamcrest.Matchers.is;
|
||||
|
||||
public class AcknowledgedResponseTests extends ESTestCase {
|
||||
public class AcknowledgedResponseTests extends AbstractResponseTestCase<org.elasticsearch.action.support.master.AcknowledgedResponse,
|
||||
AcknowledgedResponse> {
|
||||
|
||||
public void testFromXContent() throws IOException {
|
||||
xContentTester(this::createParser,
|
||||
this::createTestInstance,
|
||||
AcknowledgedResponseTests::toXContent,
|
||||
AcknowledgedResponse::fromXContent)
|
||||
.supportsUnknownFields(false)
|
||||
.test();
|
||||
}
|
||||
private AcknowledgedResponse createTestInstance() {
|
||||
return new AcknowledgedResponse(randomBoolean());
|
||||
@Override
|
||||
protected org.elasticsearch.action.support.master.AcknowledgedResponse createServerTestInstance() {
|
||||
return new org.elasticsearch.action.support.master.AcknowledgedResponse(randomBoolean());
|
||||
}
|
||||
|
||||
@Override
|
||||
protected AcknowledgedResponse doParseToClientInstance(XContentParser parser) throws IOException {
|
||||
return AcknowledgedResponse.fromXContent(parser);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void assertInstances(org.elasticsearch.action.support.master.AcknowledgedResponse serverTestInstance,
|
||||
AcknowledgedResponse clientInstance) {
|
||||
assertThat(clientInstance.isAcknowledged(), is(serverTestInstance.isAcknowledged()));
|
||||
}
|
||||
|
||||
// Still needed for StopRollupJobResponseTests and StartRollupJobResponseTests test classes
|
||||
// This method can't be moved to these classes because getFieldName() method isn't accessible from these test classes.
|
||||
public static void toXContent(AcknowledgedResponse response, XContentBuilder builder) throws IOException {
|
||||
builder.startObject();
|
||||
{
|
||||
|
|
|
@ -20,17 +20,9 @@
|
|||
package org.elasticsearch.client.core;
|
||||
|
||||
import org.elasticsearch.action.support.DefaultShardOperationFailedException;
|
||||
import org.elasticsearch.cluster.ClusterModule;
|
||||
import org.elasticsearch.common.bytes.BytesReference;
|
||||
import org.elasticsearch.common.xcontent.LoggingDeprecationHandler;
|
||||
import org.elasticsearch.common.xcontent.NamedXContentRegistry;
|
||||
import org.elasticsearch.common.xcontent.ToXContent;
|
||||
import org.elasticsearch.common.xcontent.XContent;
|
||||
import org.elasticsearch.common.xcontent.XContentFactory;
|
||||
import org.elasticsearch.client.AbstractResponseTestCase;
|
||||
import org.elasticsearch.common.xcontent.XContentParser;
|
||||
import org.elasticsearch.common.xcontent.XContentType;
|
||||
import org.elasticsearch.index.seqno.RetentionLeaseNotFoundException;
|
||||
import org.elasticsearch.test.ESTestCase;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
|
@ -43,16 +35,22 @@ import static org.hamcrest.Matchers.equalTo;
|
|||
import static org.hamcrest.Matchers.hasSize;
|
||||
import static org.hamcrest.Matchers.isIn;
|
||||
|
||||
public class BroadcastResponseTests extends ESTestCase {
|
||||
public class BroadcastResponseTests extends AbstractResponseTestCase<org.elasticsearch.action.support.broadcast.BroadcastResponse,
|
||||
BroadcastResponse> {
|
||||
|
||||
public void testFromXContent() throws IOException {
|
||||
final String index = randomAlphaOfLength(8);
|
||||
final String id = randomAlphaOfLength(8);
|
||||
private String index;
|
||||
private String id;
|
||||
private Set<Integer> shardIds;
|
||||
|
||||
@Override
|
||||
protected org.elasticsearch.action.support.broadcast.BroadcastResponse createServerTestInstance() {
|
||||
index = randomAlphaOfLength(8);
|
||||
id = randomAlphaOfLength(8);
|
||||
final int total = randomIntBetween(1, 16);
|
||||
final int successful = total - scaledRandomIntBetween(0, total);
|
||||
final int failed = scaledRandomIntBetween(0, total - successful);
|
||||
final List<DefaultShardOperationFailedException> failures = new ArrayList<>();
|
||||
final Set<Integer> shardIds = new HashSet<>();
|
||||
shardIds = new HashSet<>();
|
||||
for (int i = 0; i < failed; i++) {
|
||||
final DefaultShardOperationFailedException failure = new DefaultShardOperationFailedException(
|
||||
index,
|
||||
|
@ -62,25 +60,24 @@ public class BroadcastResponseTests extends ESTestCase {
|
|||
shardIds.add(failure.shardId());
|
||||
}
|
||||
|
||||
final org.elasticsearch.action.support.broadcast.BroadcastResponse to =
|
||||
new org.elasticsearch.action.support.broadcast.BroadcastResponse(total, successful, failed, failures);
|
||||
return new org.elasticsearch.action.support.broadcast.BroadcastResponse(total, successful, failed, failures);
|
||||
}
|
||||
|
||||
final XContentType xContentType = randomFrom(XContentType.values());
|
||||
final BytesReference bytes = toShuffledXContent(to, xContentType, ToXContent.EMPTY_PARAMS, randomBoolean());
|
||||
@Override
|
||||
protected BroadcastResponse doParseToClientInstance(XContentParser parser) throws IOException {
|
||||
return BroadcastResponse.fromXContent(parser);
|
||||
}
|
||||
|
||||
final XContent xContent = XContentFactory.xContent(xContentType);
|
||||
final XContentParser parser = xContent.createParser(
|
||||
new NamedXContentRegistry(ClusterModule.getNamedXWriteables()),
|
||||
LoggingDeprecationHandler.INSTANCE,
|
||||
bytes.streamInput());
|
||||
final BroadcastResponse from = BroadcastResponse.fromXContent(parser);
|
||||
assertThat(from.shards().total(), equalTo(total));
|
||||
assertThat(from.shards().successful(), equalTo(successful));
|
||||
assertThat(from.shards().skipped(), equalTo(0));
|
||||
assertThat(from.shards().failed(), equalTo(failed));
|
||||
assertThat(from.shards().failures(), hasSize(failed == 0 ? failed : 1)); // failures are grouped
|
||||
if (failed > 0) {
|
||||
final DefaultShardOperationFailedException groupedFailure = from.shards().failures().iterator().next();
|
||||
@Override
|
||||
protected void assertInstances(org.elasticsearch.action.support.broadcast.BroadcastResponse serverTestInstance,
|
||||
BroadcastResponse clientInstance) {
|
||||
assertThat(clientInstance.shards().total(), equalTo(serverTestInstance.getTotalShards()));
|
||||
assertThat(clientInstance.shards().successful(), equalTo(serverTestInstance.getSuccessfulShards()));
|
||||
assertThat(clientInstance.shards().skipped(), equalTo(0));
|
||||
assertThat(clientInstance.shards().failed(), equalTo(serverTestInstance.getFailedShards()));
|
||||
assertThat(clientInstance.shards().failures(), hasSize(clientInstance.shards().failed() == 0 ? 0 : 1)); // failures are grouped
|
||||
if (clientInstance.shards().failed() > 0) {
|
||||
final DefaultShardOperationFailedException groupedFailure = clientInstance.shards().failures().iterator().next();
|
||||
assertThat(groupedFailure.index(), equalTo(index));
|
||||
assertThat(groupedFailure.shardId(), isIn(shardIds));
|
||||
assertThat(groupedFailure.reason(), containsString("reason=retention lease with ID [" + id + "] not found"));
|
||||
|
|
Loading…
Reference in New Issue