From e8a359f17642a8699ae8497ae5a546d23eed821a Mon Sep 17 00:00:00 2001 From: Juan Medina Date: Mon, 20 Jul 2020 18:50:57 +0100 Subject: [PATCH] DATAJDBC-522 - Add Kotlin extensions for Criteria Adding Kotlin extensions for easier consumption of Criteria when using Kotlin --- spring-data-relational/pom.xml | 14 ++++ .../core/query/CriteriaStepExtensions.kt | 43 +++++++++++ .../core/query/CriteriaStepExtensionsTests.kt | 75 +++++++++++++++++++ 3 files changed, 132 insertions(+) create mode 100644 spring-data-relational/src/main/kotlin/org/springframework/data/relational/core/query/CriteriaStepExtensions.kt create mode 100644 spring-data-relational/src/test/kotlin/org/springframework/data/relational/core/query/CriteriaStepExtensionsTests.kt diff --git a/spring-data-relational/pom.xml b/spring-data-relational/pom.xml index 5ff3aef680..a6f996f9d3 100644 --- a/spring-data-relational/pom.xml +++ b/spring-data-relational/pom.xml @@ -71,6 +71,20 @@ test + + + org.jetbrains.kotlin + kotlin-stdlib + true + + + + io.mockk + mockk + ${mockk} + test + + diff --git a/spring-data-relational/src/main/kotlin/org/springframework/data/relational/core/query/CriteriaStepExtensions.kt b/spring-data-relational/src/main/kotlin/org/springframework/data/relational/core/query/CriteriaStepExtensions.kt new file mode 100644 index 0000000000..d7a7e99409 --- /dev/null +++ b/spring-data-relational/src/main/kotlin/org/springframework/data/relational/core/query/CriteriaStepExtensions.kt @@ -0,0 +1,43 @@ +/* + * Copyright 2019-2020 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. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.data.relational.core.query + +/** + * Extension for [Criteria.CriteriaStep. is] providing a + * `isEquals(value)` variant. + * + * @author Juan Medina + */ +infix fun Criteria.CriteriaStep.isEquals(value: Any): Criteria = + `is`(value) + +/** + * Extension for [Criteria.CriteriaStep. in] providing a + * `isIn(value)` variant. + * + * @author Juan Medina + */ +fun Criteria.CriteriaStep.isIn(vararg value: Any): Criteria = + `in`(value) + +/** + * Extension for [Criteria.CriteriaStep. in] providing a + * `isIn(value)` variant. + * + * @author Juan Medina + */ +fun Criteria.CriteriaStep.isIn(values: Collection): Criteria = + `in`(values) diff --git a/spring-data-relational/src/test/kotlin/org/springframework/data/relational/core/query/CriteriaStepExtensionsTests.kt b/spring-data-relational/src/test/kotlin/org/springframework/data/relational/core/query/CriteriaStepExtensionsTests.kt new file mode 100644 index 0000000000..4321da75f5 --- /dev/null +++ b/spring-data-relational/src/test/kotlin/org/springframework/data/relational/core/query/CriteriaStepExtensionsTests.kt @@ -0,0 +1,75 @@ +/* + * Copyright 2019 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. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.data.relational.core.query + +import io.mockk.every +import io.mockk.mockk +import io.mockk.verify +import org.assertj.core.api.Assertions.assertThat +import org.junit.Test + +/** + * Unit tests for [Criteria.CriteriaStep] extensions. + * + * @author Juan Medina + */ +class CriteriaStepExtensionsTests { + + @Test // DATAJDBC-522 + fun eqIsCriteriaStep(){ + + val spec = mockk() + val criteria = mockk() + + every { spec.`is`("test") } returns criteria + + assertThat(spec isEquals "test").isEqualTo(criteria) + + verify { + spec.`is`("test") + } + } + + @Test // DATAJDBC-522 + fun inVarargCriteriaStep() { + + val spec = mockk() + val criteria = mockk() + + every { spec.`in`(any() as Array) } returns criteria + + assertThat(spec.isIn("test")).isEqualTo(criteria) + + verify { + spec.`in`(arrayOf("test")) + } + } + + @Test // DATAJDBC-522 + fun inListCriteriaStep() { + + val spec = mockk() + val criteria = mockk() + + every { spec.`in`(listOf("test")) } returns criteria + + assertThat(spec.isIn(listOf("test"))).isEqualTo(criteria) + + verify { + spec.`in`(listOf("test")) + } + } +}