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

Closed
wants to merge 2 commits into from
Closed
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
@@ -1,5 +1,5 @@
/*
* Copyright 2006-2023 the original author or authors.
* Copyright 2006-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -29,6 +29,7 @@
*
* @author Dave Syer
* @author Mahmoud Ben Hassine
* @author JiWon Seo
*
*/
public class ExitStatus implements Serializable, Comparable<ExitStatus> {
Expand Down Expand Up @@ -231,7 +232,7 @@ public ExitStatus replaceExitCode(String code) {
* @return {@code true} if the exit code is {@code EXECUTING} or {@code UNKNOWN}.
*/
public boolean isRunning() {
return "EXECUTING".equals(this.exitCode) || "UNKNOWN".equals(this.exitCode);
return EXECUTING.exitCode.equals(this.exitCode) || UNKNOWN.exitCode.equals(this.exitCode);
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2006-2024 the original author or authors.
* Copyright 2006-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -33,6 +33,7 @@
/**
* @author Dave Syer
* @author Mahmoud Ben Hassine
* @author JiWon Seo
*
*/
class ExitStatusTests {
Expand Down Expand Up @@ -153,7 +154,7 @@ void testAddExitDescription() {
}

@Test
void testAddExitDescriptionWIthStacktrace() {
void testAddExitDescriptionWithStacktrace() {
ExitStatus status = ExitStatus.EXECUTING.addExitDescription(new RuntimeException("Foo"));
assertNotSame(ExitStatus.EXECUTING, status);
String description = status.getExitDescription();
Expand Down Expand Up @@ -182,8 +183,15 @@ void testAddExitCodeWithDescription() {
}

@Test
void testUnknownIsRunning() {
void testIsRunning() {
// running statuses
assertTrue(ExitStatus.EXECUTING.isRunning());
assertTrue(ExitStatus.UNKNOWN.isRunning());
// non running statuses
assertFalse(ExitStatus.COMPLETED.isRunning());
assertFalse(ExitStatus.FAILED.isRunning());
assertFalse(ExitStatus.STOPPED.isRunning());
assertFalse(ExitStatus.NOOP.isRunning());
}

@Test
Expand Down
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());
}

}