Skip to content

Commit

Permalink
Change HLRC CCR response tests to use AbstractResponseTestCase base c…
Browse files Browse the repository at this point in the history
…lass. (elastic#40257)

This way the response classes are tested in a more realistic setting.

Relates to elastic#39745
  • Loading branch information
martijnvg authored Apr 8, 2019
1 parent cc38a83 commit 675bf3c
Show file tree
Hide file tree
Showing 6 changed files with 368 additions and 616 deletions.
3 changes: 3 additions & 0 deletions client/rest-high-level/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ dependencies {
testCompile "org.hamcrest:hamcrest-all:${versions.hamcrest}"
//this is needed to make RestHighLevelClientTests#testApiNamingConventions work from IDEs
testCompile "org.elasticsearch:rest-api-spec:${version}"
// Needed for serialization tests:
// (In order to serialize a server side class to a client side class or the other way around)
testCompile "org.elasticsearch.plugin:x-pack-core:${version}"

restSpec "org.elasticsearch:rest-api-spec:${version}"
}
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -19,59 +19,89 @@

package org.elasticsearch.client.ccr;

import org.elasticsearch.client.ccr.FollowInfoResponse.FollowerInfo;
import org.elasticsearch.common.xcontent.ToXContent;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.test.ESTestCase;
import org.elasticsearch.client.AbstractResponseTestCase;
import org.elasticsearch.common.unit.ByteSizeValue;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.xpack.core.ccr.action.FollowInfoAction;
import org.elasticsearch.xpack.core.ccr.action.FollowParameters;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import static org.elasticsearch.test.AbstractXContentTestCase.xContentTester;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.nullValue;

public class FollowInfoResponseTests extends ESTestCase {
public class FollowInfoResponseTests extends AbstractResponseTestCase<FollowInfoAction.Response, FollowInfoResponse> {

public void testFromXContent() throws IOException {
xContentTester(this::createParser,
FollowInfoResponseTests::createTestInstance,
FollowInfoResponseTests::toXContent,
FollowInfoResponse::fromXContent)
.supportsUnknownFields(true)
.test();
}

private static void toXContent(FollowInfoResponse response, XContentBuilder builder) throws IOException {
builder.startObject();
builder.startArray(FollowInfoResponse.FOLLOWER_INDICES_FIELD.getPreferredName());
for (FollowerInfo info : response.getInfos()) {
builder.startObject();
builder.field(FollowerInfo.FOLLOWER_INDEX_FIELD.getPreferredName(), info.getFollowerIndex());
builder.field(FollowerInfo.REMOTE_CLUSTER_FIELD.getPreferredName(), info.getRemoteCluster());
builder.field(FollowerInfo.LEADER_INDEX_FIELD.getPreferredName(), info.getLeaderIndex());
builder.field(FollowerInfo.STATUS_FIELD.getPreferredName(), info.getStatus().getName());
if (info.getParameters() != null) {
builder.startObject(FollowerInfo.PARAMETERS_FIELD.getPreferredName());
{
info.getParameters().toXContentFragment(builder, ToXContent.EMPTY_PARAMS);
}
builder.endObject();
@Override
protected FollowInfoAction.Response createServerTestInstance() {
int numInfos = randomIntBetween(0, 32);
List<FollowInfoAction.Response.FollowerInfo> infos = new ArrayList<>(numInfos);
for (int i = 0; i < numInfos; i++) {
FollowParameters followParameters = null;
if (randomBoolean()) {
followParameters = randomFollowParameters();
}
builder.endObject();

infos.add(new FollowInfoAction.Response.FollowerInfo(randomAlphaOfLength(4), randomAlphaOfLength(4), randomAlphaOfLength(4),
randomFrom(FollowInfoAction.Response.Status.values()), followParameters));
}
builder.endArray();
builder.endObject();
return new FollowInfoAction.Response(infos);
}

private static FollowInfoResponse createTestInstance() {
int numInfos = randomIntBetween(0, 64);
List<FollowerInfo> infos = new ArrayList<>(numInfos);
for (int i = 0; i < numInfos; i++) {
FollowInfoResponse.Status status = randomFrom(FollowInfoResponse.Status.values());
FollowConfig followConfig = randomBoolean() ? FollowConfigTests.createTestInstance() : null;
infos.add(new FollowerInfo(randomAlphaOfLength(4), randomAlphaOfLength(4), randomAlphaOfLength(4), status, followConfig));
static FollowParameters randomFollowParameters() {
FollowParameters followParameters = new FollowParameters();
followParameters.setMaxOutstandingReadRequests(randomIntBetween(0, Integer.MAX_VALUE));
followParameters.setMaxOutstandingWriteRequests(randomIntBetween(0, Integer.MAX_VALUE));
followParameters.setMaxReadRequestOperationCount(randomIntBetween(0, Integer.MAX_VALUE));
followParameters.setMaxWriteRequestOperationCount(randomIntBetween(0, Integer.MAX_VALUE));
followParameters.setMaxReadRequestSize(new ByteSizeValue(randomNonNegativeLong()));
followParameters.setMaxWriteRequestSize(new ByteSizeValue(randomNonNegativeLong()));
followParameters.setMaxWriteBufferCount(randomIntBetween(0, Integer.MAX_VALUE));
followParameters.setMaxWriteBufferSize(new ByteSizeValue(randomNonNegativeLong()));
followParameters.setMaxRetryDelay(new TimeValue(randomNonNegativeLong()));
followParameters.setReadPollTimeout(new TimeValue(randomNonNegativeLong()));
return followParameters;
}

@Override
protected FollowInfoResponse doParseToClientInstance(XContentParser parser) throws IOException {
return FollowInfoResponse.fromXContent(parser);
}

@Override
protected void assertInstances(FollowInfoAction.Response serverTestInstance, FollowInfoResponse clientInstance) {
assertThat(serverTestInstance.getFollowInfos().size(), equalTo(clientInstance.getInfos().size()));
for (int i = 0; i < serverTestInstance.getFollowInfos().size(); i++) {
FollowInfoAction.Response.FollowerInfo serverFollowInfo = serverTestInstance.getFollowInfos().get(i);
FollowInfoResponse.FollowerInfo clientFollowerInfo = clientInstance.getInfos().get(i);

assertThat(serverFollowInfo.getRemoteCluster(), equalTo(clientFollowerInfo.getRemoteCluster()));
assertThat(serverFollowInfo.getLeaderIndex(), equalTo(clientFollowerInfo.getLeaderIndex()));
assertThat(serverFollowInfo.getFollowerIndex(), equalTo(clientFollowerInfo.getFollowerIndex()));
assertThat(serverFollowInfo.getStatus().toString().toLowerCase(Locale.ROOT),
equalTo(clientFollowerInfo.getStatus().getName().toLowerCase(Locale.ROOT)));

FollowParameters serverParams = serverFollowInfo.getParameters();
FollowConfig clientParams = clientFollowerInfo.getParameters();
if (serverParams != null) {
assertThat(serverParams.getMaxReadRequestOperationCount(), equalTo(clientParams.getMaxReadRequestOperationCount()));
assertThat(serverParams.getMaxWriteRequestOperationCount(), equalTo(clientParams.getMaxWriteRequestOperationCount()));
assertThat(serverParams.getMaxOutstandingReadRequests(), equalTo(clientParams.getMaxOutstandingReadRequests()));
assertThat(serverParams.getMaxOutstandingWriteRequests(), equalTo(clientParams.getMaxOutstandingWriteRequests()));
assertThat(serverParams.getMaxReadRequestSize(), equalTo(clientParams.getMaxReadRequestSize()));
assertThat(serverParams.getMaxWriteRequestSize(), equalTo(clientParams.getMaxWriteRequestSize()));
assertThat(serverParams.getMaxWriteBufferCount(), equalTo(clientParams.getMaxWriteBufferCount()));
assertThat(serverParams.getMaxWriteBufferSize(), equalTo(clientParams.getMaxWriteBufferSize()));
assertThat(serverParams.getMaxRetryDelay(), equalTo(clientParams.getMaxRetryDelay()));
assertThat(serverParams.getReadPollTimeout(), equalTo(clientParams.getReadPollTimeout()));
} else {
assertThat(clientParams, nullValue());
}
}
return new FollowInfoResponse(infos);
}

}
Loading

0 comments on commit 675bf3c

Please sign in to comment.