Skip to content

Commit 02313a0

Browse files
Add ReindexTaskStateDoc tests (#52767)
Added xcontent serialization tests for ReindexTaskStateDoc Related to #42612 Depends on #49278 (todo)
1 parent 9a4b794 commit 02313a0

File tree

2 files changed

+110
-1
lines changed

2 files changed

+110
-1
lines changed

modules/reindex/src/main/java/org/elasticsearch/index/reindex/ReindexTaskStateDoc.java

-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131

3232
import java.io.IOException;
3333

34-
// TODO: This class has become complicated enough that we should implement a xcontent serialization test
3534
public class ReindexTaskStateDoc implements ToXContentObject {
3635

3736
public static final ConstructingObjectParser<ReindexTaskStateDoc, Void> PARSER =
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
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+
20+
package org.elasticsearch.index.reindex;
21+
22+
import org.elasticsearch.common.unit.TimeValue;
23+
import org.elasticsearch.common.xcontent.XContentParser;
24+
import org.elasticsearch.tasks.TaskId;
25+
import org.elasticsearch.test.AbstractXContentTestCase;
26+
27+
import java.io.IOException;
28+
import java.util.Collections;
29+
30+
public class ReindexTaskStateDocTests extends AbstractXContentTestCase<ReindexTaskStateDoc> {
31+
@Override
32+
protected ReindexTaskStateDoc createTestInstance() {
33+
ReindexTaskStateDoc doc = new ReindexTaskStateDoc(new ReindexRequest().setSourceIndices("source").setDestIndex("dest"),
34+
randomBoolean(), randomNonNegativeLong());
35+
36+
if (randomBoolean()) {
37+
doc = doc.withNewAllocation(randomNonNegativeLong(), new TaskId(randomAlphaOfLength(10), randomNonNegativeLong()));
38+
}
39+
40+
if (randomBoolean()) {
41+
doc = doc.withCheckpoint(new ScrollableHitSource.Checkpoint(randomNonNegativeLong()),
42+
randomStatus());
43+
}
44+
45+
if (randomBoolean()) {
46+
doc = doc.withRequestsPerSecond(randomFloat());
47+
}
48+
49+
if (randomBoolean()) {
50+
doc = doc.withFinishedState(randomLongBetween(doc.getStartTimeMillis(), Long.MAX_VALUE),
51+
new BulkByScrollResponse(TimeValue.timeValueMillis(randomNonNegativeLong()),
52+
randomStatus(), Collections.emptyList(), Collections.emptyList(), randomBoolean()), null);
53+
} else if (randomBoolean()) {
54+
// todo: need fixing once #49278 is merged.
55+
// doc = doc.withFinishedState(null, new ElasticsearchException("test"));
56+
}
57+
58+
return doc;
59+
}
60+
61+
private BulkByScrollTask.Status randomStatus() {
62+
return new BulkByScrollTask.Status(null, randomNonNegativeLong(), randomNonNegativeLong(), randomNonNegativeLong(),
63+
randomNonNegativeLong(), randomIntBetween(0, 1000), randomNonNegativeLong(), randomNonNegativeLong(),
64+
randomNonNegativeLong(), randomNonNegativeLong(), TimeValue.timeValueMillis(randomNonNegativeLong()),
65+
randomFloat(), randomBoolean() ? randomAlphaOfLength(10) : null, TimeValue.timeValueMillis(randomNonNegativeLong()));
66+
}
67+
68+
@Override
69+
protected ReindexTaskStateDoc doParseInstance(XContentParser parser) throws IOException {
70+
return ReindexTaskStateDoc.fromXContent(parser);
71+
}
72+
73+
@Override
74+
protected boolean supportsUnknownFields() {
75+
return false;
76+
}
77+
78+
@Override
79+
protected void assertEqualInstances(ReindexTaskStateDoc expectedInstance, ReindexTaskStateDoc newInstance) {
80+
assertNotSame(newInstance, expectedInstance);
81+
assertArrayEquals(expectedInstance.getReindexRequest().getSearchRequest().indices(),
82+
newInstance.getReindexRequest().getSearchRequest().indices());
83+
// todo: once ReindexRequestTests are moved to reindex module, we can validate this properly.
84+
assertEquals(expectedInstance.getReindexRequest().getDestination().index(),
85+
newInstance.getReindexRequest().getDestination().index());
86+
assertEquals(expectedInstance.getAllocationId(), newInstance.getAllocationId());
87+
if (expectedInstance.getCheckpoint() != null) {
88+
assertEquals(expectedInstance.getCheckpoint().getRestartFromValue(), newInstance.getCheckpoint().getRestartFromValue());
89+
} else {
90+
assertNull(newInstance.getCheckpoint());
91+
}
92+
assertEquals(expectedInstance.getEphemeralTaskId(), newInstance.getEphemeralTaskId());
93+
if (expectedInstance.getException() != null) {
94+
assertEquals(expectedInstance.getException().getMessage(), newInstance.getException().getMessage());
95+
} else {
96+
assertNull(newInstance.getException());
97+
}
98+
assertEquals(expectedInstance.getFailureStatusCode(), newInstance.getFailureStatusCode());
99+
assertEquals(expectedInstance.getRethrottledReindexRequest().getRequestsPerSecond(),
100+
newInstance.getRethrottledReindexRequest().getRequestsPerSecond(),
101+
0.0001d);
102+
// todo: once BulkByScrollResponseTests is moved to reindex module, we can validate this properly.
103+
if (expectedInstance.getReindexResponse() != null) {
104+
assertEquals(expectedInstance.getReindexResponse().getCreated(), newInstance.getReindexResponse().getCreated());
105+
} else {
106+
assertNull(newInstance.getReindexResponse());
107+
}
108+
assertEquals(expectedInstance.isResilient(), newInstance.isResilient());
109+
}
110+
}

0 commit comments

Comments
 (0)