From ff3830ac3c64ad08fd3e91fd49ec5dbd8bcb18a3 Mon Sep 17 00:00:00 2001 From: Scott Leberknight <174812+sleberknight@users.noreply.github.com> Date: Thu, 1 Sep 2022 19:11:56 +0000 Subject: [PATCH] Add ofAscending and ofDescending factories to KiwiSort Closes #775 --- .../org/kiwiproject/spring/data/KiwiSort.java | 26 ++++++++++++++ .../kiwiproject/spring/data/KiwiSortTest.java | 35 ++++++++++++++++++- 2 files changed, 60 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/kiwiproject/spring/data/KiwiSort.java b/src/main/java/org/kiwiproject/spring/data/KiwiSort.java index 407fec57..87e54688 100644 --- a/src/main/java/org/kiwiproject/spring/data/KiwiSort.java +++ b/src/main/java/org/kiwiproject/spring/data/KiwiSort.java @@ -68,6 +68,32 @@ public static Direction fromString(String value) { private boolean ignoreCase; private boolean ascending; + /** + * Create a new instance with ascending sort direction. + *
+ * If you want to specify that the sort is not case-sensitive, you can immediately call the + * {@link #ignoringCase()} in a fluent style. + * + * @param property the property the sort is applied to + * @return a new instance + */ + public static KiwiSort ofAscending(String property) { + return of(property, Direction.ASC); + } + + /** + * Create a new instance with descending sort direction. + *
+ * If you want to specify that the sort is not case-sensitive, you can immediately call the + * {@link #ignoringCase()} in a fluent style. + * + * @param property the property the sort is applied to + * @return a new instance + */ + public static KiwiSort ofDescending(String property) { + return of(property, Direction.DESC); + } + /** * Create a new instance. *
diff --git a/src/test/java/org/kiwiproject/spring/data/KiwiSortTest.java b/src/test/java/org/kiwiproject/spring/data/KiwiSortTest.java index eef218f5..05babdb2 100644 --- a/src/test/java/org/kiwiproject/spring/data/KiwiSortTest.java +++ b/src/test/java/org/kiwiproject/spring/data/KiwiSortTest.java @@ -3,7 +3,6 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; import static org.junit.jupiter.params.provider.Arguments.arguments; -import static org.kiwiproject.base.KiwiStrings.f; import org.assertj.core.api.SoftAssertions; import org.assertj.core.api.junit.jupiter.SoftAssertionsExtension; @@ -70,6 +69,40 @@ void shouldValidateArgumentsWhenUsing_DirectionStringFactoryMethod(String proper .isThrownBy(() -> KiwiSort.of(property, direction)); } + @Test + void shouldCreateNewAscendingInstance(SoftAssertions softly) { + var sort = KiwiSort.ofAscending("sortedProperty"); + + softly.assertThat(sort.getProperty()).isEqualTo("sortedProperty"); + softly.assertThat(sort.getDirection()).isEqualTo("ASC"); + softly.assertThat(sort.isAscending()).isTrue(); + softly.assertThat(sort.isIgnoreCase()).isFalse(); + } + + @ParameterizedTest + @ArgumentsSource(BlankStringArgumentsProvider.class) + void shouldNotPermitBlankPropertyForAscendingSort(String value) { + assertThatIllegalArgumentException() + .isThrownBy(() -> KiwiSort.ofAscending(value)); + } + + @Test + void shouldCreateNewDescendingInstance(SoftAssertions softly) { + var sort = KiwiSort.ofDescending("sortedProperty"); + + softly.assertThat(sort.getProperty()).isEqualTo("sortedProperty"); + softly.assertThat(sort.getDirection()).isEqualTo("DESC"); + softly.assertThat(sort.isAscending()).isFalse(); + softly.assertThat(sort.isIgnoreCase()).isFalse(); + } + + @ParameterizedTest + @ArgumentsSource(BlankStringArgumentsProvider.class) + void shouldNotPermitBlankPropertyForDescendingSort(String value) { + assertThatIllegalArgumentException() + .isThrownBy(() -> KiwiSort.ofDescending(value)); + } + @Test void shouldSetIgnoringCase(SoftAssertions softly) { var sort = KiwiSort.of("otherProperty", KiwiSort.Direction.ASC).ignoringCase();