diff --git a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/data/builder/MongoItemReaderBuilder.java b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/data/builder/MongoItemReaderBuilder.java index 9bb116a68..40f148fb8 100644 --- a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/data/builder/MongoItemReaderBuilder.java +++ b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/data/builder/MongoItemReaderBuilder.java @@ -1,5 +1,5 @@ /* - * Copyright 2017-2020 the original author or authors. + * Copyright 2017-2022 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. @@ -284,7 +284,7 @@ public class MongoItemReaderBuilder { 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)) { + if (StringUtils.hasText(this.jsonQuery) || this.query != null) { Assert.notNull(this.sorts, "sorts map is required."); } diff --git a/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/data/builder/MongoItemReaderBuilderTests.java b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/data/builder/MongoItemReaderBuilderTests.java index c80c2dc4a..4531aab2c 100644 --- a/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/data/builder/MongoItemReaderBuilderTests.java +++ b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/data/builder/MongoItemReaderBuilderTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2017-2021 the original author or authors. + * Copyright 2017-2022 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. @@ -38,11 +38,14 @@ import static org.junit.Assert.assertNull; import static org.junit.Assert.fail; import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.when; +import static org.springframework.data.mongodb.core.query.Criteria.where; +import static org.springframework.data.mongodb.core.query.Query.query; /** * @author Glenn Renfro * @author Drummond Dawson * @author Parikshit Dutta + * @author Mahmoud Ben Hassine */ public class MongoItemReaderBuilderTests { @@ -212,7 +215,7 @@ public class MongoItemReaderBuilderTests { } @Test - public void testNullSorts() { + public void testNullSortsWithQueryString() { validateExceptionMessage(new MongoItemReaderBuilder().template(this.template) .targetType(String.class) .jsonQuery("{ }") @@ -220,6 +223,15 @@ public class MongoItemReaderBuilderTests { .pageSize(50), "sorts map is required."); } + @Test + public void testNullSortsWithQuery() { + validateExceptionMessage(new MongoItemReaderBuilder().template(this.template) + .targetType(String.class) + .query(query(where("_id").is("10"))) + .name("mongoReaderTest") + .pageSize(50), "sorts map is required."); + } + @Test public void testNullName() { validateExceptionMessage(new MongoItemReaderBuilder().template(this.template)