-
Notifications
You must be signed in to change notification settings - Fork 25.8k
Add Paginated Hit Source Tests #142592
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
elasticsearchmachine
merged 12 commits into
elastic:main
from
joshua-adams-1:spacetime-paginated-hit-source
Mar 12, 2026
Merged
Add Paginated Hit Source Tests #142592
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
51c7da7
Add tests for PaginatedHitSource
joshua-adams-1 ea85c30
Spotless apply
joshua-adams-1 e3d1b26
Spotless apply
joshua-adams-1 ecde50f
Remove redundant equals tests
joshua-adams-1 9c7cf13
[CI] Auto commit changes from spotless
beb809d
Merge branch 'main' into spacetime-paginated-hit-source
joshua-adams-1 15fb4d9
Address Comments
joshua-adams-1 f277128
Merge branch 'spacetime-paginated-hit-source' of github.com:joshua-ad…
joshua-adams-1 78ae500
Merge branch 'main' into spacetime-paginated-hit-source
joshua-adams-1 bcc50a2
Move out of individual folder
joshua-adams-1 4e8b981
Merge branch 'spacetime-paginated-hit-source' of github.com:joshua-ad…
joshua-adams-1 0673b09
Merge branch 'main' into spacetime-paginated-hit-source
joshua-adams-1 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
106 changes: 106 additions & 0 deletions
106
server/src/test/java/org/elasticsearch/index/reindex/BasicHitTests.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,106 @@ | ||
| /* | ||
| * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
| * or more contributor license agreements. Licensed under the "Elastic License | ||
| * 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side | ||
| * Public License v 1"; you may not use this file except in compliance with, at | ||
| * your election, the "Elastic License 2.0", the "GNU Affero General Public | ||
| * License v3.0 only", or the "Server Side Public License, v 1". | ||
| */ | ||
|
|
||
| package org.elasticsearch.index.reindex; | ||
|
|
||
| import org.elasticsearch.common.bytes.BytesArray; | ||
| import org.elasticsearch.common.bytes.BytesReference; | ||
| import org.elasticsearch.index.reindex.PaginatedHitSource.BasicHit; | ||
| import org.elasticsearch.test.ESTestCase; | ||
| import org.elasticsearch.xcontent.XContentType; | ||
|
|
||
| public class BasicHitTests extends ESTestCase { | ||
szybia marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| /** | ||
| * Verifies that index, id, and version provided at construction are returned unchanged by their respective getters. | ||
| * Verifies that optional fields are null or zero by default before any setters are invoked. | ||
| */ | ||
| public void testConstructor() { | ||
| String index = randomAlphaOfLengthBetween(3, 10); | ||
| String id = randomAlphaOfLengthBetween(3, 10); | ||
| long version = randomNonNegativeLong(); | ||
| PaginatedHitSource.BasicHit hit = new BasicHit(index, id, version); | ||
|
|
||
| assertEquals(index, hit.getIndex()); | ||
| assertEquals(id, hit.getId()); | ||
| assertEquals(version, hit.getVersion()); | ||
|
|
||
| assertNull(hit.getSource()); | ||
| assertNull(hit.getXContentType()); | ||
| assertNull(hit.getRouting()); | ||
| assertEquals(0L, hit.getSeqNo()); | ||
| assertEquals(0L, hit.getPrimaryTerm()); | ||
| } | ||
|
|
||
| /** | ||
| * Verifies that setSource correctly sets both the source bytes and the associated XContentType. | ||
| * Verifies that setSource returns the same instance, allowing fluent-style method chaining. | ||
| */ | ||
| public void testSetSource() { | ||
| BasicHit hit = new BasicHit(randomAlphaOfLengthBetween(3, 10), randomAlphaOfLengthBetween(3, 10), randomNonNegativeLong()); | ||
| BytesReference source = new BytesArray(randomAlphaOfLengthBetween(5, 50)); | ||
| XContentType xContentType = randomFrom(XContentType.values()); | ||
|
|
||
| BasicHit returned = hit.setSource(source, xContentType); | ||
| assertSame(source, hit.getSource()); | ||
| assertEquals(xContentType, hit.getXContentType()); | ||
| assertSame(hit, returned); | ||
| } | ||
|
|
||
| /** | ||
| * Verifies that routing can be set and retrieved correctly. | ||
| * Verifies that setRouting returns the same instance, allowing fluent-style chaining. | ||
| */ | ||
| public void testSetRouting() { | ||
| BasicHit hit = new BasicHit(randomAlphaOfLengthBetween(3, 10), randomAlphaOfLengthBetween(3, 10), randomNonNegativeLong()); | ||
| String routing = randomAlphaOfLengthBetween(3, 20); | ||
| BasicHit returned = hit.setRouting(routing); | ||
| assertEquals(routing, hit.getRouting()); | ||
| assertSame(hit, returned); | ||
| } | ||
|
|
||
| /** | ||
| * Verifies that sequence number can be set and retrieved correctly. | ||
| */ | ||
| public void testSetSeqNo() { | ||
| BasicHit hit = new BasicHit(randomAlphaOfLengthBetween(3, 10), randomAlphaOfLengthBetween(3, 10), randomNonNegativeLong()); | ||
| long seqNo = randomNonNegativeLong(); | ||
| hit.setSeqNo(seqNo); | ||
| assertEquals(seqNo, hit.getSeqNo()); | ||
| } | ||
|
|
||
| /** | ||
| * Verifies that primary term can be set and retrieved correctly. | ||
| */ | ||
| public void testSetPrimaryTerm() { | ||
| BasicHit hit = new BasicHit(randomAlphaOfLengthBetween(3, 10), randomAlphaOfLengthBetween(3, 10), randomNonNegativeLong()); | ||
| long primaryTerm = randomNonNegativeLong(); | ||
| hit.setPrimaryTerm(primaryTerm); | ||
| assertEquals(primaryTerm, hit.getPrimaryTerm()); | ||
| } | ||
|
|
||
| /** | ||
| * Verifies that setting all optional fields does not affect the required constructor-provided fields. | ||
| */ | ||
| public void testOptionalSettersDoNotAffectRequiredFields() { | ||
| String index = randomAlphaOfLengthBetween(3, 10); | ||
| String id = randomAlphaOfLengthBetween(3, 10); | ||
| long version = randomNonNegativeLong(); | ||
| BasicHit hit = new BasicHit(index, id, version); | ||
|
|
||
| hit.setRouting(randomAlphaOfLengthBetween(3, 20)); | ||
| hit.setSeqNo(randomNonNegativeLong()); | ||
| hit.setPrimaryTerm(randomNonNegativeLong()); | ||
| hit.setSource(new BytesArray(randomAlphaOfLengthBetween(5, 50)), randomFrom(XContentType.values())); | ||
|
|
||
| assertEquals(index, hit.getIndex()); | ||
| assertEquals(id, hit.getId()); | ||
| assertEquals(version, hit.getVersion()); | ||
| } | ||
| } | ||
77 changes: 77 additions & 0 deletions
77
server/src/test/java/org/elasticsearch/index/reindex/ResponseTests.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| /* | ||
| * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
| * or more contributor license agreements. Licensed under the "Elastic License | ||
| * 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side | ||
| * Public License v 1"; you may not use this file except in compliance with, at | ||
| * your election, the "Elastic License 2.0", the "GNU Affero General Public | ||
| * License v3.0 only", or the "Server Side Public License, v 1". | ||
| */ | ||
|
|
||
| package org.elasticsearch.index.reindex; | ||
|
|
||
| import org.elasticsearch.index.reindex.PaginatedHitSource.BasicHit; | ||
| import org.elasticsearch.index.reindex.PaginatedHitSource.Hit; | ||
| import org.elasticsearch.index.reindex.PaginatedHitSource.Response; | ||
| import org.elasticsearch.index.reindex.PaginatedHitSource.SearchFailure; | ||
| import org.elasticsearch.test.ESTestCase; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.Collections; | ||
| import java.util.List; | ||
|
|
||
| public class ResponseTests extends ESTestCase { | ||
|
|
||
| /** | ||
| * Verifies that all values provided to the constructor are returned unchanged by their respective getters. | ||
| */ | ||
| public void testConstructor() { | ||
| boolean timedOut = randomBoolean(); | ||
| List<SearchFailure> failures = randomBoolean() ? Collections.emptyList() : randomFailures(); | ||
| long totalHits = randomNonNegativeLong(); | ||
| List<PaginatedHitSource.Hit> hits = randomBoolean() ? Collections.emptyList() : randomHits(); | ||
| String scrollId = randomAlphaOfLengthBetween(3, 20); | ||
| Response response = new Response(timedOut, failures, totalHits, hits, scrollId); | ||
|
|
||
| assertEquals(timedOut, response.isTimedOut()); | ||
| assertSame(failures, response.getFailures()); | ||
| assertEquals(totalHits, response.getTotalHits()); | ||
| assertSame(hits, response.getHits()); | ||
| assertEquals(scrollId, response.getScrollId()); | ||
| } | ||
|
|
||
| /** | ||
| * Verifies that providing null values for optional collections is preserved and returned as-is by the getters. | ||
| */ | ||
| public void testNullCollectionsArePreserved() { | ||
| List<SearchFailure> failures = null; | ||
| List<Hit> hits = null; | ||
| Response response = new Response(randomBoolean(), failures, randomNonNegativeLong(), hits, randomAlphaOfLengthBetween(3, 20)); | ||
| assertNull(response.getFailures()); | ||
| assertNull(response.getHits()); | ||
| } | ||
|
|
||
| private static List<SearchFailure> randomFailures() { | ||
| int size = randomIntBetween(1, 5); | ||
| List<SearchFailure> failures = new ArrayList<>(size); | ||
| for (int i = 0; i < size; i++) { | ||
| failures.add( | ||
| new SearchFailure( | ||
| new IllegalStateException(randomAlphaOfLengthBetween(5, 20)), | ||
| randomBoolean() ? randomAlphaOfLengthBetween(3, 10) : null, | ||
| randomBoolean() ? randomIntBetween(0, 10) : null, | ||
| randomBoolean() ? randomAlphaOfLengthBetween(3, 10) : null | ||
| ) | ||
| ); | ||
| } | ||
| return failures; | ||
| } | ||
|
|
||
| private static List<Hit> randomHits() { | ||
| int size = randomIntBetween(1, 5); | ||
| List<Hit> hits = new ArrayList<>(size); | ||
| for (int i = 0; i < size; i++) { | ||
| hits.add(new BasicHit(randomAlphaOfLengthBetween(3, 10), randomAlphaOfLengthBetween(3, 10), randomNonNegativeLong())); | ||
| } | ||
| return hits; | ||
| } | ||
| } |
90 changes: 90 additions & 0 deletions
90
server/src/test/java/org/elasticsearch/index/reindex/SearchFailureTests.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,90 @@ | ||
| /* | ||
| * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
| * or more contributor license agreements. Licensed under the "Elastic License | ||
| * 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side | ||
| * Public License v 1"; you may not use this file except in compliance with, at | ||
| * your election, the "Elastic License 2.0", the "GNU Affero General Public | ||
| * License v3.0 only", or the "Server Side Public License, v 1". | ||
| */ | ||
|
|
||
| package org.elasticsearch.index.reindex; | ||
|
|
||
| import org.elasticsearch.ElasticsearchException; | ||
| import org.elasticsearch.ExceptionsHelper; | ||
| import org.elasticsearch.common.Strings; | ||
| import org.elasticsearch.common.xcontent.XContentHelper; | ||
| import org.elasticsearch.index.reindex.PaginatedHitSource.SearchFailure; | ||
| import org.elasticsearch.test.ESTestCase; | ||
| import org.elasticsearch.xcontent.XContentType; | ||
|
|
||
| import java.util.Map; | ||
|
|
||
| import static org.hamcrest.Matchers.equalTo; | ||
| import static org.hamcrest.Matchers.hasKey; | ||
| import static org.hamcrest.Matchers.not; | ||
| import static org.hamcrest.Matchers.notNullValue; | ||
|
|
||
| public class SearchFailureTests extends ESTestCase { | ||
|
|
||
| public void testConstructorWithReasonOnly() { | ||
| Throwable reason = randomException(); | ||
| SearchFailure failure = new SearchFailure(reason); | ||
| assertSame(reason, failure.getReason()); | ||
| assertNull(failure.getIndex()); | ||
| assertNull(failure.getShardId()); | ||
| assertNull(failure.getNodeId()); | ||
| assertEquals(ExceptionsHelper.status(reason), failure.getStatus()); | ||
| } | ||
|
|
||
| public void testConstructorWithAllFields() { | ||
| Throwable reason = randomException(); | ||
| String index = randomAlphaOfLengthBetween(3, 10); | ||
| Integer shardId = randomIntBetween(0, 100); | ||
| String nodeId = randomAlphaOfLengthBetween(3, 10); | ||
| SearchFailure failure = new SearchFailure(reason, index, shardId, nodeId); | ||
| assertSame(reason, failure.getReason()); | ||
| assertEquals(index, failure.getIndex()); | ||
| assertEquals(shardId, failure.getShardId()); | ||
| assertEquals(nodeId, failure.getNodeId()); | ||
| assertEquals(ExceptionsHelper.status(reason), failure.getStatus()); | ||
| } | ||
|
|
||
| public void testToXContentIncludesExpectedFields() { | ||
| String message = randomAlphaOfLengthBetween(1, 20); | ||
| Throwable reason = randomException(message); | ||
| String index = randomAlphaOfLengthBetween(3, 10); | ||
| Integer shardId = randomIntBetween(0, 10); | ||
| String nodeId = randomAlphaOfLengthBetween(3, 10); | ||
| SearchFailure failure = new SearchFailure(reason, index, shardId, nodeId); | ||
| String json = Strings.toString(failure); | ||
| Map<String, Object> map = XContentHelper.convertToMap(XContentType.JSON.xContent(), json, false); | ||
| assertThat(map.get(SearchFailure.INDEX_FIELD), equalTo(index)); | ||
| assertThat(map.get(SearchFailure.SHARD_FIELD), equalTo(shardId)); | ||
| assertThat(map.get(SearchFailure.NODE_FIELD), equalTo(nodeId)); | ||
| assertThat(map.get(SearchFailure.STATUS_FIELD), equalTo(failure.getStatus().getStatus())); | ||
| assertThat(map, hasKey(SearchFailure.REASON_FIELD)); | ||
| @SuppressWarnings("unchecked") | ||
| Map<String, Object> reasonMap = (Map<String, Object>) map.get(SearchFailure.REASON_FIELD); | ||
| assertThat(reasonMap.get("type"), notNullValue()); | ||
| assertThat(reasonMap.get("reason"), equalTo(message)); | ||
| } | ||
|
|
||
| public void testToXContentOmitsNullOptionalFields() { | ||
| SearchFailure failure = new SearchFailure(randomException()); | ||
| String json = Strings.toString(failure); | ||
| Map<String, Object> map = XContentHelper.convertToMap(XContentType.JSON.xContent(), json, false); | ||
| assertThat(map, not(hasKey(SearchFailure.INDEX_FIELD))); | ||
| assertThat(map, not(hasKey(SearchFailure.SHARD_FIELD))); | ||
| assertThat(map, not(hasKey(SearchFailure.NODE_FIELD))); | ||
| assertThat(map, hasKey(SearchFailure.STATUS_FIELD)); | ||
| assertThat(map, hasKey(SearchFailure.REASON_FIELD)); | ||
| } | ||
|
|
||
| public static Throwable randomException() { | ||
| return randomException(randomAlphaOfLengthBetween(1, 20)); | ||
| } | ||
|
|
||
| public static Throwable randomException(String message) { | ||
| return randomFrom(new IllegalArgumentException(message), new IllegalStateException(message), new ElasticsearchException(message)); | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.