Skip to content

Commit

Permalink
Add convenience methods to KiwiSort and KiwiSort.Direction (#780)
Browse files Browse the repository at this point in the history
* Add isDescending() to KiwiSort.Direction enum
* Add a Direction property named directionObject to KiwiSort to allow
  code to easily get the direction as the enum instead of a String.
  But, ignore it when serializing to JSON, since the serialized
  value is just the enum constant's name, e.g. "ASC" or "DESC", which
  it already has via the direction property
* Add isDescending() to KiwiSort

Closes #779
  • Loading branch information
sleberknight authored Sep 6, 2022
1 parent d1429df commit 91e29e7
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 19 deletions.
22 changes: 22 additions & 0 deletions src/main/java/org/kiwiproject/spring/data/KiwiSort.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import static org.kiwiproject.base.KiwiPreconditions.checkArgumentNotBlank;
import static org.kiwiproject.base.KiwiPreconditions.checkArgumentNotNull;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import lombok.Getter;
import lombok.Setter;
Expand Down Expand Up @@ -60,8 +61,19 @@ public static Direction fromString(String value) {

return direction;
}

/**
* Convenience method to allow checking if this {@link Direction} is descending.
*
* @return true if this sort is descending, false if ascending
*/
public boolean isDescending() {
return !ascending;
}
}

@JsonIgnore
private Direction directionObject;
private String direction; // this is intentionally a string, for JSON serialization purposes
private String property;
private boolean ignoreCase;
Expand Down Expand Up @@ -128,6 +140,7 @@ public static KiwiSort of(String property, KiwiSort.Direction direction) {

var sort = new KiwiSort();
sort.setProperty(property);
sort.setDirectionObject(direction);
sort.setDirection(direction.name());
sort.setAscending(direction.isAscending());
sort.setIgnoreCase(false);
Expand All @@ -143,4 +156,13 @@ public KiwiSort ignoringCase() {
setIgnoreCase(true);
return this;
}

/**
* Convenience method to allow checking if this {@link KiwiSort} is descending.
*
* @return true if this sort is descending, false if ascending
*/
public boolean isDescending() {
return !ascending;
}
}
80 changes: 61 additions & 19 deletions src/test/java/org/kiwiproject/spring/data/KiwiSortTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
import org.junit.jupiter.params.provider.CsvSource;
import org.junit.jupiter.params.provider.MethodSource;
import org.junit.jupiter.params.provider.ValueSource;
import org.kiwiproject.json.JsonHelper;
import org.kiwiproject.json.JsonHelper.OutputFormat;
import org.kiwiproject.spring.data.KiwiSort.Direction;
import org.kiwiproject.util.BlankStringArgumentsProvider;

import java.util.stream.Stream;
Expand All @@ -26,11 +29,13 @@ class KiwiSortTest {

@Test
void shouldCreateNewInstanceUsing_DirectionEnumFactoryMethod(SoftAssertions softly) {
var sort = KiwiSort.of("someProperty", KiwiSort.Direction.DESC);
var sort = KiwiSort.of("someProperty", Direction.DESC);

softly.assertThat(sort.getProperty()).isEqualTo("someProperty");
softly.assertThat(sort.getDirection()).isEqualTo("DESC");
softly.assertThat(sort.getDirectionObject()).isEqualTo(Direction.DESC);
softly.assertThat(sort.isAscending()).isFalse();
softly.assertThat(sort.isDescending()).isTrue();
softly.assertThat(sort.isIgnoreCase()).isFalse();
}

Expand All @@ -40,7 +45,9 @@ void shouldCreateNewInstanceUsing_DirectionStringFactoryMethod(SoftAssertions so

softly.assertThat(sort.getProperty()).isEqualTo("someProperty");
softly.assertThat(sort.getDirection()).isEqualTo("ASC");
softly.assertThat(sort.getDirectionObject()).isEqualTo(Direction.ASC);
softly.assertThat(sort.isAscending()).isTrue();
softly.assertThat(sort.isDescending()).isFalse();
softly.assertThat(sort.isIgnoreCase()).isFalse();
}

Expand All @@ -51,7 +58,7 @@ void shouldCreateNewInstanceUsing_DirectionStringFactoryMethod(SoftAssertions so
" ' ' , DESC",
" lastName, ",
})
void shouldValidateArgumentsWhenUsing_DirectionEnumFactoryMethod(String property, KiwiSort.Direction direction) {
void shouldValidateArgumentsWhenUsing_DirectionEnumFactoryMethod(String property, Direction direction) {
assertThatIllegalArgumentException()
.isThrownBy(() -> KiwiSort.of(property, direction));
}
Expand All @@ -75,7 +82,9 @@ void shouldCreateNewAscendingInstance(SoftAssertions softly) {

softly.assertThat(sort.getProperty()).isEqualTo("sortedProperty");
softly.assertThat(sort.getDirection()).isEqualTo("ASC");
softly.assertThat(sort.getDirectionObject()).isEqualTo(Direction.ASC);
softly.assertThat(sort.isAscending()).isTrue();
softly.assertThat(sort.isDescending()).isFalse();
softly.assertThat(sort.isIgnoreCase()).isFalse();
}

Expand All @@ -92,7 +101,9 @@ void shouldCreateNewDescendingInstance(SoftAssertions softly) {

softly.assertThat(sort.getProperty()).isEqualTo("sortedProperty");
softly.assertThat(sort.getDirection()).isEqualTo("DESC");
softly.assertThat(sort.getDirectionObject()).isEqualTo(Direction.DESC);
softly.assertThat(sort.isAscending()).isFalse();
softly.assertThat(sort.isDescending()).isTrue();
softly.assertThat(sort.isIgnoreCase()).isFalse();
}

Expand All @@ -105,17 +116,19 @@ void shouldNotPermitBlankPropertyForDescendingSort(String value) {

@Test
void shouldSetIgnoringCase(SoftAssertions softly) {
var sort = KiwiSort.of("otherProperty", KiwiSort.Direction.ASC).ignoringCase();
var sort = KiwiSort.of("otherProperty", Direction.ASC).ignoringCase();

softly.assertThat(sort.getProperty()).isEqualTo("otherProperty");
softly.assertThat(sort.getDirection()).isEqualTo("ASC");
softly.assertThat(sort.getDirectionObject()).isEqualTo(Direction.ASC);
softly.assertThat(sort.isAscending()).isTrue();
softly.assertThat(sort.isDescending()).isFalse();
softly.assertThat(sort.isIgnoreCase()).isTrue();
}

@Test
void shouldCallToString() {
var sort = KiwiSort.of("someProperty", KiwiSort.Direction.DESC);
var sort = KiwiSort.of("someProperty", Direction.DESC);
var str = sort.toString();
assertThat(str).isNotBlank();
}
Expand All @@ -128,7 +141,7 @@ class DirectionFromString {
@ArgumentsSource(BlankStringArgumentsProvider.class)
void shouldRequireNonBlankValue(String value) {
assertThatIllegalArgumentException()
.isThrownBy(() -> KiwiSort.Direction.fromString(value))
.isThrownBy(() -> Direction.fromString(value))
.withMessage("direction value must not be blank");
}

Expand All @@ -144,30 +157,59 @@ void shouldRequireNonBlankValue(String value) {
void shouldThrowIllegalArgument_ForInvalidValues(String value) {
assertThatIllegalArgumentException()
.describedAs("expecting IllegalArgumentException with same message as from Enum#valueOf")
.isThrownBy(() -> KiwiSort.Direction.fromString(value))
.isThrownBy(() -> Direction.fromString(value))
.withMessage("no matching enum constant found in Direction");
}

@ParameterizedTest
@MethodSource("org.kiwiproject.spring.data.KiwiSortTest#directionEnumValues")
void shouldCreateFromStrings(String value, KiwiSort.Direction expected) {
assertThat(KiwiSort.Direction.fromString(value)).isEqualTo(expected);
void shouldCreateFromStrings(String value, Direction expected) {
assertThat(Direction.fromString(value)).isEqualTo(expected);
}
}

@Nested
class DirectionEnum {

@Test
void shouldHaveExpectedAscendingValue() {
assertThat(Direction.ASC.isAscending()).isTrue();
assertThat(Direction.DESC.isAscending()).isFalse();
}

@Test
void shouldHaveExpectedDescendingValue() {
assertThat(Direction.ASC.isDescending()).isFalse();
assertThat(Direction.DESC.isDescending()).isTrue();
}
}

static Stream<Arguments> directionEnumValues() {
return Stream.of(
arguments("asc", KiwiSort.Direction.ASC),
arguments(" asc ", KiwiSort.Direction.ASC),
arguments(" \r\n\tasc\r\n\t ", KiwiSort.Direction.ASC),
arguments("ASC", KiwiSort.Direction.ASC),
arguments("Asc", KiwiSort.Direction.ASC),
arguments("AsC", KiwiSort.Direction.ASC),
arguments("desc", KiwiSort.Direction.DESC),
arguments("\r\ndesc\t \t \r\n", KiwiSort.Direction.DESC),
arguments("DESC", KiwiSort.Direction.DESC),
arguments("Desc", KiwiSort.Direction.DESC),
arguments("DeSc", KiwiSort.Direction.DESC)
arguments("asc", Direction.ASC),
arguments(" asc ", Direction.ASC),
arguments(" \r\n\tasc\r\n\t ", Direction.ASC),
arguments("ASC", Direction.ASC),
arguments("Asc", Direction.ASC),
arguments("AsC", Direction.ASC),
arguments("desc", Direction.DESC),
arguments("\r\ndesc\t \t \r\n", Direction.DESC),
arguments("DESC", Direction.DESC),
arguments("Desc", Direction.DESC),
arguments("DeSc", Direction.DESC)
);
}

@Test
void shouldSerializeToJson(SoftAssertions softly) {
var jsonHelper = JsonHelper.newDropwizardJsonHelper();
var json = jsonHelper.toJson(KiwiSort.ofAscending("lastName"), OutputFormat.PRETTY);

softly.assertThat(json).containsIgnoringWhitespaces("\"direction\" : \"ASC\"");
softly.assertThat(json).containsIgnoringWhitespaces("\"property\" : \"lastName\"");
softly.assertThat(json).containsIgnoringWhitespaces("\"ignoreCase\" : false");
softly.assertThat(json).containsIgnoringWhitespaces("\"ascending\" : true");
softly.assertThat(json).containsIgnoringWhitespaces("\"descending\" : false");
softly.assertThat(json).doesNotContain("\"directionObject\" :");
}
}

0 comments on commit 91e29e7

Please sign in to comment.