Skip to content

Commit 0e0357c

Browse files
authored
Add realistic hlrc response serialization test base class (elastic#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 elastic#39745
1 parent b8f8ed7 commit 0e0357c

File tree

3 files changed

+121
-46
lines changed

3 files changed

+121
-46
lines changed
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
* Licensed to Elasticsearch under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.elasticsearch.client;
20+
21+
import org.elasticsearch.cluster.ClusterModule;
22+
import org.elasticsearch.common.bytes.BytesReference;
23+
import org.elasticsearch.common.xcontent.LoggingDeprecationHandler;
24+
import org.elasticsearch.common.xcontent.NamedXContentRegistry;
25+
import org.elasticsearch.common.xcontent.ToXContent;
26+
import org.elasticsearch.common.xcontent.XContent;
27+
import org.elasticsearch.common.xcontent.XContentFactory;
28+
import org.elasticsearch.common.xcontent.XContentParser;
29+
import org.elasticsearch.common.xcontent.XContentType;
30+
import org.elasticsearch.test.ESTestCase;
31+
32+
import java.io.IOException;
33+
34+
/**
35+
* Base class for HLRC response parsing tests.
36+
*
37+
* This case class facilitates generating server side reponse test instances and
38+
* verifies that they are correctly parsed into HLRC response instances.
39+
*
40+
* @param <S> The class representing the response on the server side.
41+
* @param <C> The class representing the response on the client side.
42+
*/
43+
public abstract class AbstractResponseTestCase<S extends ToXContent, C> extends ESTestCase {
44+
45+
private static final int NUMBER_OF_TEST_RUNS = 20;
46+
47+
public final void testFromXContent() throws IOException {
48+
for (int i = 0; i < NUMBER_OF_TEST_RUNS; i++) {
49+
final S serverTestInstance = createServerTestInstance();
50+
51+
final XContentType xContentType = randomFrom(XContentType.values());
52+
final BytesReference bytes = toShuffledXContent(serverTestInstance, xContentType, ToXContent.EMPTY_PARAMS, randomBoolean());
53+
54+
final XContent xContent = XContentFactory.xContent(xContentType);
55+
final XContentParser parser = xContent.createParser(
56+
new NamedXContentRegistry(ClusterModule.getNamedXWriteables()),
57+
LoggingDeprecationHandler.INSTANCE,
58+
bytes.streamInput());
59+
final C clientInstance = doParseToClientInstance(parser);
60+
assertInstances(serverTestInstance, clientInstance);
61+
}
62+
}
63+
64+
protected abstract S createServerTestInstance();
65+
66+
protected abstract C doParseToClientInstance(XContentParser parser) throws IOException;
67+
68+
protected abstract void assertInstances(S serverTestInstance, C clientInstance);
69+
70+
}

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

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,27 +18,35 @@
1818
*/
1919
package org.elasticsearch.client.core;
2020

21+
import org.elasticsearch.client.AbstractResponseTestCase;
2122
import org.elasticsearch.common.xcontent.XContentBuilder;
22-
import org.elasticsearch.test.ESTestCase;
23+
import org.elasticsearch.common.xcontent.XContentParser;
2324

2425
import java.io.IOException;
2526

26-
import static org.elasticsearch.test.AbstractXContentTestCase.xContentTester;
27+
import static org.hamcrest.Matchers.is;
2728

28-
public class AcknowledgedResponseTests extends ESTestCase {
29+
public class AcknowledgedResponseTests extends AbstractResponseTestCase<org.elasticsearch.action.support.master.AcknowledgedResponse,
30+
AcknowledgedResponse> {
2931

30-
public void testFromXContent() throws IOException {
31-
xContentTester(this::createParser,
32-
this::createTestInstance,
33-
AcknowledgedResponseTests::toXContent,
34-
AcknowledgedResponse::fromXContent)
35-
.supportsUnknownFields(false)
36-
.test();
32+
@Override
33+
protected org.elasticsearch.action.support.master.AcknowledgedResponse createServerTestInstance() {
34+
return new org.elasticsearch.action.support.master.AcknowledgedResponse(randomBoolean());
3735
}
38-
private AcknowledgedResponse createTestInstance() {
39-
return new AcknowledgedResponse(randomBoolean());
36+
37+
@Override
38+
protected AcknowledgedResponse doParseToClientInstance(XContentParser parser) throws IOException {
39+
return AcknowledgedResponse.fromXContent(parser);
40+
}
41+
42+
@Override
43+
protected void assertInstances(org.elasticsearch.action.support.master.AcknowledgedResponse serverTestInstance,
44+
AcknowledgedResponse clientInstance) {
45+
assertThat(clientInstance.isAcknowledged(), is(serverTestInstance.isAcknowledged()));
4046
}
4147

48+
// Still needed for StopRollupJobResponseTests and StartRollupJobResponseTests test classes
49+
// This method can't be moved to these classes because getFieldName() method isn't accessible from these test classes.
4250
public static void toXContent(AcknowledgedResponse response, XContentBuilder builder) throws IOException {
4351
builder.startObject();
4452
{

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

Lines changed: 31 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,9 @@
2020
package org.elasticsearch.client.core;
2121

2222
import org.elasticsearch.action.support.DefaultShardOperationFailedException;
23-
import org.elasticsearch.cluster.ClusterModule;
24-
import org.elasticsearch.common.bytes.BytesReference;
25-
import org.elasticsearch.common.xcontent.LoggingDeprecationHandler;
26-
import org.elasticsearch.common.xcontent.NamedXContentRegistry;
27-
import org.elasticsearch.common.xcontent.ToXContent;
28-
import org.elasticsearch.common.xcontent.XContent;
29-
import org.elasticsearch.common.xcontent.XContentFactory;
23+
import org.elasticsearch.client.AbstractResponseTestCase;
3024
import org.elasticsearch.common.xcontent.XContentParser;
31-
import org.elasticsearch.common.xcontent.XContentType;
3225
import org.elasticsearch.index.seqno.RetentionLeaseNotFoundException;
33-
import org.elasticsearch.test.ESTestCase;
3426

3527
import java.io.IOException;
3628
import java.util.ArrayList;
@@ -43,44 +35,49 @@
4335
import static org.hamcrest.Matchers.hasSize;
4436
import static org.hamcrest.Matchers.isIn;
4537

46-
public class BroadcastResponseTests extends ESTestCase {
38+
public class BroadcastResponseTests extends AbstractResponseTestCase<org.elasticsearch.action.support.broadcast.BroadcastResponse,
39+
BroadcastResponse> {
4740

48-
public void testFromXContent() throws IOException {
49-
final String index = randomAlphaOfLength(8);
50-
final String id = randomAlphaOfLength(8);
41+
private String index;
42+
private String id;
43+
private Set<Integer> shardIds;
44+
45+
@Override
46+
protected org.elasticsearch.action.support.broadcast.BroadcastResponse createServerTestInstance() {
47+
index = randomAlphaOfLength(8);
48+
id = randomAlphaOfLength(8);
5149
final int total = randomIntBetween(1, 16);
5250
final int successful = total - scaledRandomIntBetween(0, total);
5351
final int failed = scaledRandomIntBetween(0, total - successful);
5452
final List<DefaultShardOperationFailedException> failures = new ArrayList<>();
55-
final Set<Integer> shardIds = new HashSet<>();
53+
shardIds = new HashSet<>();
5654
for (int i = 0; i < failed; i++) {
5755
final DefaultShardOperationFailedException failure = new DefaultShardOperationFailedException(
58-
index,
59-
randomValueOtherThanMany(shardIds::contains, () -> randomIntBetween(0, total - 1)),
60-
new RetentionLeaseNotFoundException(id));
56+
index,
57+
randomValueOtherThanMany(shardIds::contains, () -> randomIntBetween(0, total - 1)),
58+
new RetentionLeaseNotFoundException(id));
6159
failures.add(failure);
6260
shardIds.add(failure.shardId());
6361
}
6462

65-
final org.elasticsearch.action.support.broadcast.BroadcastResponse to =
66-
new org.elasticsearch.action.support.broadcast.BroadcastResponse(total, successful, failed, failures);
63+
return new org.elasticsearch.action.support.broadcast.BroadcastResponse(total, successful, failed, failures);
64+
}
6765

68-
final XContentType xContentType = randomFrom(XContentType.values());
69-
final BytesReference bytes = toShuffledXContent(to, xContentType, ToXContent.EMPTY_PARAMS, randomBoolean());
66+
@Override
67+
protected BroadcastResponse doParseToClientInstance(XContentParser parser) throws IOException {
68+
return BroadcastResponse.fromXContent(parser);
69+
}
7070

71-
final XContent xContent = XContentFactory.xContent(xContentType);
72-
final XContentParser parser = xContent.createParser(
73-
new NamedXContentRegistry(ClusterModule.getNamedXWriteables()),
74-
LoggingDeprecationHandler.INSTANCE,
75-
bytes.streamInput());
76-
final BroadcastResponse from = BroadcastResponse.fromXContent(parser);
77-
assertThat(from.shards().total(), equalTo(total));
78-
assertThat(from.shards().successful(), equalTo(successful));
79-
assertThat(from.shards().skipped(), equalTo(0));
80-
assertThat(from.shards().failed(), equalTo(failed));
81-
assertThat(from.shards().failures(), hasSize(failed == 0 ? failed : 1)); // failures are grouped
82-
if (failed > 0) {
83-
final DefaultShardOperationFailedException groupedFailure = from.shards().failures().iterator().next();
71+
@Override
72+
protected void assertInstances(org.elasticsearch.action.support.broadcast.BroadcastResponse serverTestInstance,
73+
BroadcastResponse clientInstance) {
74+
assertThat(clientInstance.shards().total(), equalTo(serverTestInstance.getTotalShards()));
75+
assertThat(clientInstance.shards().successful(), equalTo(serverTestInstance.getSuccessfulShards()));
76+
assertThat(clientInstance.shards().skipped(), equalTo(0));
77+
assertThat(clientInstance.shards().failed(), equalTo(serverTestInstance.getFailedShards()));
78+
assertThat(clientInstance.shards().failures(), hasSize(clientInstance.shards().failed() == 0 ? 0 : 1)); // failures are grouped
79+
if (clientInstance.shards().failed() > 0) {
80+
final DefaultShardOperationFailedException groupedFailure = clientInstance.shards().failures().iterator().next();
8481
assertThat(groupedFailure.index(), equalTo(index));
8582
assertThat(groupedFailure.shardId(), isIn(shardIds));
8683
assertThat(groupedFailure.reason(), containsString("reason=retention lease with ID [" + id + "] not found"));

0 commit comments

Comments
 (0)