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();