Check arguments of MongoItemReader#setSort

Issue #4014
This commit is contained in:
Edgar Asatryan
2021-10-16 23:10:26 +02:00
committed by Mahmoud Ben Hassine
parent d8b8bab95b
commit 6417065141
2 changed files with 18 additions and 5 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2020 the original author or authors.
* Copyright 2012-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.
@@ -82,9 +82,9 @@ import org.springframework.util.StringUtils;
* @author Parikshit Dutta
*/
public class MongoItemReader<T> extends AbstractPaginatedDataItemReader<T> implements InitializingBean {
private static final Logger log = LoggerFactory.getLogger(MongoItemReader.class);
private MongoOperations template;
private Query query;
private String queryString;
@@ -168,6 +168,7 @@ public class MongoItemReader<T> extends AbstractPaginatedDataItemReader<T> imple
* @param sorts map of properties and direction to sort each.
*/
public void setSort(Map<String, Sort.Direction> sorts) {
Assert.notNull(sorts, "Sorts must not be null");
this.sort = convertToSort(sorts);
}
@@ -252,7 +253,7 @@ public class MongoItemReader<T> extends AbstractPaginatedDataItemReader<T> imple
}
private Sort convertToSort(Map<String, Sort.Direction> sorts) {
List<Sort.Order> sortValues = new ArrayList<>();
List<Sort.Order> sortValues = new ArrayList<>(sorts.size());
for (Map.Entry<String, Sort.Direction> curSort : sorts.entrySet()) {
sortValues.add(new Sort.Order(curSort.getValue(), curSort.getKey()));

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2013-2020 the original author or authors.
* Copyright 2013-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.
@@ -31,6 +31,7 @@ import org.springframework.data.domain.Sort.Order;
import org.springframework.data.mongodb.core.MongoOperations;
import org.springframework.data.mongodb.core.query.Query;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
@@ -379,4 +380,15 @@ public class MongoItemReaderTests {
assertEquals(0, actualQuery.getSkip());
assertEquals("collection", stringContainer.getValue());
}
@Test
public void testSortThrowsExceptionWhenInvokedWithNull() {
// given
reader = new MongoItemReader<>();
// when + then
assertThatIllegalArgumentException()
.isThrownBy(() -> reader.setSort(null))
.withMessage("Sorts must not be null");
}
}