diff --git a/.github/workflows/maven_build.yml b/.github/workflows/maven_build.yml index 943d2ab..063b908 100644 --- a/.github/workflows/maven_build.yml +++ b/.github/workflows/maven_build.yml @@ -50,7 +50,7 @@ jobs: uses: actions/setup-java@v4 with: distribution: temurin - java-version: 17 + java-version: 21 cache: 'maven' cache-dependency-path: '**/pom.xml' server-id: 'github' diff --git a/.github/workflows/maven_publish.yml b/.github/workflows/maven_publish.yml index 16706be..c3150a6 100644 --- a/.github/workflows/maven_publish.yml +++ b/.github/workflows/maven_publish.yml @@ -27,7 +27,7 @@ jobs: uses: actions/setup-java@v4 with: distribution: temurin - java-version: 17 + java-version: 21 cache: 'maven' cache-dependency-path: '**/pom.xml' server-id: 'central' @@ -70,7 +70,7 @@ jobs: uses: actions/setup-java@v4 with: distribution: temurin - java-version: 17 + java-version: 21 cache: 'maven' cache-dependency-path: '**/pom.xml' server-id: 'github' diff --git a/cursorpaging-jpa-api/src/test/java/io/vigier/cursorpaging/jpa/api/DtoPageRequestTest.java b/cursorpaging-jpa-api/src/test/java/io/vigier/cursorpaging/jpa/api/DtoPageRequestTest.java index 6045b4c..6c8fc20 100644 --- a/cursorpaging-jpa-api/src/test/java/io/vigier/cursorpaging/jpa/api/DtoPageRequestTest.java +++ b/cursorpaging-jpa-api/src/test/java/io/vigier/cursorpaging/jpa/api/DtoPageRequestTest.java @@ -71,7 +71,7 @@ void shouldDeserializeWithOrRootList() throws Exception { }, "filterBy": { "OR": [ - { "GT": { "id": [ "666" ] } } + { "GT": { "id": [ 666 ] } } ] }, "pageSize": 10, diff --git a/cursorpaging-jpa/src/main/java/io/vigier/cursorpaging/jpa/Attribute.java b/cursorpaging-jpa/src/main/java/io/vigier/cursorpaging/jpa/Attribute.java index 10a43fe..8ed109b 100644 --- a/cursorpaging-jpa/src/main/java/io/vigier/cursorpaging/jpa/Attribute.java +++ b/cursorpaging-jpa/src/main/java/io/vigier/cursorpaging/jpa/Attribute.java @@ -177,19 +177,55 @@ public boolean ignoreCase() { return ignoreCase; } - public Comparable verify( Comparable v ) { - if ( type().isAssignableFrom( v.getClass() ) ) { - return type().cast( v ); - } else if ( v instanceof Integer && type().isAssignableFrom( Long.class ) ) { - return Long.valueOf( (Integer) v ); - } else if ( v instanceof Long && type().isAssignableFrom( Integer.class ) && (Long) v <= Integer.MAX_VALUE ) { - return Integer.valueOf( v.toString() ); + public Comparable verify( final Comparable value ) { + if ( type().isAssignableFrom( value.getClass() ) ) { + return type().cast( value ); + } else if ( value instanceof Integer && typeIsInteger() ) { + return value; + } else if ( value instanceof Long && typeIsLong() ) { + return value; + } else if ( value instanceof Short && typeIsShort() ) { + return value; + } else if ( value instanceof Character && (type() == Character.class || type() == char.class) ) { + return value; + } else if ( value instanceof Boolean && typeIsBoolean() ) { + return value; + } else if ( value instanceof Byte && (type() == Byte.class || type() == byte.class) ) { + return value; + } else if ( value instanceof Integer && typeIsLong() ) { + return Long.valueOf( (Integer) value ); + } else if ( value instanceof Long && typeIsInteger() && ((Long) value) <= Integer.MAX_VALUE ) { + return Integer.valueOf( value.toString() ); + } else if ( value instanceof String && typeIsInteger() ) { + return Integer.valueOf( value.toString() ); + } else if ( value instanceof String && typeIsLong() ) { + return Long.valueOf( value.toString() ); + } else if ( value instanceof String && typeIsShort() ) { + return Short.valueOf( value.toString() ); + } else if ( value instanceof String && typeIsBoolean() ) { + return Boolean.valueOf( value.toString() ); } else { throw new IllegalArgumentException( - "Value %s (%s) is not of type %s".formatted( v, v.getClass().getName(), type() ) ); + "Value %s (%s) is not of type %s".formatted( value, value.getClass().getName(), type() ) ); } } + private boolean typeIsBoolean() { + return type() == Boolean.class || type() == boolean.class; + } + + private boolean typeIsShort() { + return type() == Short.class || type() == short.class; + } + + private boolean typeIsInteger() { + return type() == Integer.class || type() == int.class; + } + + private boolean typeIsLong() { + return type() == Long.class || type() == long.class; + } + public List> verify( List> values ) { return values.stream().map( this::verify ).toList(); } diff --git a/cursorpaging-jpa/src/test/java/io/vigier/cursorpaging/jpa/FilterTest.java b/cursorpaging-jpa/src/test/java/io/vigier/cursorpaging/jpa/FilterTest.java index 5de308b..8bef81a 100644 --- a/cursorpaging-jpa/src/test/java/io/vigier/cursorpaging/jpa/FilterTest.java +++ b/cursorpaging-jpa/src/test/java/io/vigier/cursorpaging/jpa/FilterTest.java @@ -1,10 +1,13 @@ package io.vigier.cursorpaging.jpa; +import io.vigier.cursorpaging.jpa.Filters.FilterCreator; import io.vigier.cursorpaging.jpa.itest.model.DataRecord; import io.vigier.cursorpaging.jpa.itest.model.DataRecord_; import io.vigier.cursorpaging.jpa.itest.model.Tag_; import jakarta.persistence.metamodel.SingularAttribute; import java.util.Set; +import java.util.function.Function; +import org.assertj.core.api.Assertions; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.Mock; @@ -65,4 +68,34 @@ private static > void verifyNameAndValue( final assertThat( f.values( type ) ).containsExactly( values ); } ); } + + @Test + void shouldBeEqual() { + shouldBeEqual( f1 -> f1.like( "John" ), f2 -> f2.like( "John" ) ); + shouldBeEqual( f1 -> f1.equalTo( "John" ), f2 -> f2.equalTo( "John" ) ); + shouldBeEqual( f1 -> f1.greaterThan( "1L" ), f2 -> f2.greaterThan( "1L" ) ); + shouldBeEqual( f1 -> f1.lessThan( "1L" ), f2 -> f2.lessThan( "1L" ) ); + } + + private void shouldBeEqual( Function f1, Function f2 ) { + var name1 = Attribute.of( "name", String.class ); + var name2 = Attribute.of( "name", String.class ); + + Assertions.assertThat( f1.apply( Filters.attribute( name1 ) ) ) + .isEqualTo( f2.apply( Filters.attribute( name2 ) ) ); + } + + @Test + void filterListsShouldBeEqual() { + var nameAndAge1 = Filters.and( Filters.attribute( Attribute.of( "name", String.class ) ).equalTo( "John" ), + Filters.attribute( Attribute.of( "age", Long.class ) ).equalTo( 18 ) ); + var nameAndAge2 = Filters.and( Filters.attribute( Attribute.of( "name", String.class ) ).equalTo( "John" ), + Filters.attribute( Attribute.of( "age", Long.class ) ).equalTo( 18 ) ); + + Assertions.assertThat( nameAndAge1 ).isEqualTo( nameAndAge2 ); + Assertions.assertThat( Filters.and( nameAndAge1 ) ).isEqualTo( Filters.and( nameAndAge2 ) ); + Assertions.assertThat( Filters.or( nameAndAge1 ) ).isEqualTo( Filters.or( nameAndAge2 ) ); + + Assertions.assertThat( Filters.or( nameAndAge1 ) ).isNotEqualTo( Filters.and( nameAndAge2 ) ); + } } \ No newline at end of file diff --git a/cursorpaging-jpa/src/test/java/io/vigier/cursorpaging/jpa/filter/FilterTest.java b/cursorpaging-jpa/src/test/java/io/vigier/cursorpaging/jpa/filter/FilterTest.java deleted file mode 100644 index 54fecc4..0000000 --- a/cursorpaging-jpa/src/test/java/io/vigier/cursorpaging/jpa/filter/FilterTest.java +++ /dev/null @@ -1,45 +0,0 @@ -package io.vigier.cursorpaging.jpa.filter; - -import io.vigier.cursorpaging.jpa.Attribute; -import io.vigier.cursorpaging.jpa.Filter; -import io.vigier.cursorpaging.jpa.Filters; -import io.vigier.cursorpaging.jpa.Filters.FilterCreator; -import java.util.function.Function; -import org.assertj.core.api.Assertions; -import org.junit.jupiter.api.Test; - -/** - * Some test to make sure that comparing by equals is safe in other tests - */ -class FilterTest { - - @Test - void shouldBeEqual() { - shouldBeEqual( f1 -> f1.like( "John" ), f2 -> f2.like( "John" ) ); - shouldBeEqual( f1 -> f1.equalTo( "John" ), f2 -> f2.equalTo( "John" ) ); - shouldBeEqual( f1 -> f1.greaterThan( 1L ), f2 -> f2.greaterThan( 1L ) ); - shouldBeEqual( f1 -> f1.lessThan( 1L ), f2 -> f2.lessThan( 1L ) ); - } - - private void shouldBeEqual( Function f1, Function f2 ) { - var name1 = Attribute.of( "name", String.class ); - var name2 = Attribute.of( "name", String.class ); - - Assertions.assertThat( f1.apply( Filters.attribute( name1 ) ) ) - .isEqualTo( f2.apply( Filters.attribute( name2 ) ) ); - } - - @Test - void filterListsShouldBeEqual() { - var nameAndAge1 = Filters.and( Filters.attribute( Attribute.of( "name", String.class ) ).equalTo( "John" ), - Filters.attribute( Attribute.of( "age", Long.class ) ).equalTo( 18 ) ); - var nameAndAge2 = Filters.and( Filters.attribute( Attribute.of( "name", String.class ) ).equalTo( "John" ), - Filters.attribute( Attribute.of( "age", Long.class ) ).equalTo( 18 ) ); - - Assertions.assertThat( nameAndAge1 ).isEqualTo( nameAndAge2 ); - Assertions.assertThat( Filters.and( nameAndAge1 ) ).isEqualTo( Filters.and( nameAndAge2 ) ); - Assertions.assertThat( Filters.or( nameAndAge1 ) ).isEqualTo( Filters.or( nameAndAge2 ) ); - - Assertions.assertThat( Filters.or( nameAndAge1 ) ).isNotEqualTo( Filters.and( nameAndAge2 ) ); - } -} \ No newline at end of file