Skip to content

fix MongoCursorItemReaderBuilder sorts field validation logic #4861

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ public MongoCursorItemReader<T> build() {
Assert.notNull(this.targetType, "targetType is required.");
Assert.state(StringUtils.hasText(this.jsonQuery) || this.query != null, "A query is required");

if (StringUtils.hasText(this.jsonQuery) || this.query != null) {
if (StringUtils.hasText(this.jsonQuery) && this.query == null) {
Assert.notNull(this.sorts, "sorts map is required.");
}

Expand All @@ -297,7 +297,9 @@ public MongoCursorItemReader<T> build() {
reader.setQuery(this.jsonQuery);
reader.setParameterValues(this.parameterValues);
reader.setFields(this.fields);
reader.setSort(this.sorts);
if (this.sorts != null) {
reader.setSort(this.sorts);
}
reader.setHint(this.hint);
reader.setBatchSize(this.batchSize);
reader.setLimit(this.limit);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
*
* @author Mahmoud Ben Hassine
*/
public class MongoCursorItemReaderBuilderTests {
class MongoCursorItemReaderBuilderTests {

@Test
void testBuild() {
Expand Down Expand Up @@ -67,4 +67,47 @@ void testBuild() {
Assertions.assertEquals(maxTime, ReflectionTestUtils.getField(reader, "maxTime"));
}

@Test
void testBuildWithQueryNoSorts() {
// given
MongoTemplate template = mock();
Class<String> targetType = String.class;
Query query = mock();
int batchSize = 100;
int limit = 10000;
Duration maxTime = Duration.ofSeconds(1);

// when & then
Assertions.assertDoesNotThrow(() -> new MongoCursorItemReaderBuilder<String>().name("reader")
.template(template)
.targetType(targetType)
.query(query)
.batchSize(batchSize)
.limit(limit)
.maxTime(maxTime)
.build());
}

@Test
void testBuildWithJsonQueryNoSorts() {
// given
MongoTemplate template = mock();
Class<String> targetType = String.class;
String jsonQuery = "{ }";
int batchSize = 100;
int limit = 10000;
Duration maxTime = Duration.ofSeconds(1);

// when & then
Assertions.assertThrows(IllegalArgumentException.class,
() -> new MongoCursorItemReaderBuilder<String>().name("reader")
.template(template)
.targetType(targetType)
.jsonQuery(jsonQuery)
.batchSize(batchSize)
.limit(limit)
.maxTime(maxTime)
.build());
}

}