Skip to content

Commit

Permalink
Check arguments of MongoItemReader#setSort
Browse files Browse the repository at this point in the history
Issue #4014
  • Loading branch information
nstdio authored and fmbenhassine committed Feb 23, 2022
1 parent 212b3ce commit 2e68451
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2021 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.
Expand Down Expand Up @@ -164,6 +164,7 @@ public void setFields(String fields) {
* @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);
}

Expand Down Expand Up @@ -248,7 +249,7 @@ private String replacePlaceholders(String input, List<Object> values) {
}

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()));
Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright 2013-2021 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.
Expand Down Expand Up @@ -33,6 +33,7 @@
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;
Expand Down Expand Up @@ -377,4 +378,15 @@ public void testQueryObjectWithCollection() throws Exception {
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");
}
}

0 comments on commit 2e68451

Please sign in to comment.