From bcb6090a012c6610d750058090d0cd04f08e7afd Mon Sep 17 00:00:00 2001 From: Mark Paluch Date: Sun, 30 Jun 2019 00:02:54 +0200 Subject: [PATCH] Add Kotlin extensions. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit We now provide extensions for VaultOperations, ReactiveVaultOperations, VaultKeyValueOperations, VaultVersionedKeyValueOperations, and VaultWrappingOperations to leverage reified type parameters with read(…) methods. Closes gh-442. --- pom.xml | 86 ++++++++++++++++++- spring-vault-core/pom.xml | 33 +++++++ .../core/ReactiveVaultOperationsExtensions.kt | 29 +++++++ .../core/VaultKeyValueOperationsExtensions.kt | 28 ++++++ .../vault/core/VaultOperationsExtensions.kt | 28 ++++++ ...ltVersionedKeyValueOperationsExtensions.kt | 28 ++++++ .../core/VaultWrappingOperationsExtensions.kt | 29 +++++++ .../ReactiveVaultOperationsExtensionsTests.kt | 46 ++++++++++ .../VaultKeyValueOperationsExtensionsTests.kt | 46 ++++++++++ .../core/VaultOperationsExtensionsTests.kt | 46 ++++++++++ ...sionedKeyValueOperationsExtensionsTests.kt | 46 ++++++++++ .../VaultWrappingOperationsExtensionsTests.kt | 51 +++++++++++ 12 files changed, 495 insertions(+), 1 deletion(-) create mode 100644 spring-vault-core/src/main/kotlin/org/springframework/vault/core/ReactiveVaultOperationsExtensions.kt create mode 100644 spring-vault-core/src/main/kotlin/org/springframework/vault/core/VaultKeyValueOperationsExtensions.kt create mode 100644 spring-vault-core/src/main/kotlin/org/springframework/vault/core/VaultOperationsExtensions.kt create mode 100644 spring-vault-core/src/main/kotlin/org/springframework/vault/core/VaultVersionedKeyValueOperationsExtensions.kt create mode 100644 spring-vault-core/src/main/kotlin/org/springframework/vault/core/VaultWrappingOperationsExtensions.kt create mode 100644 spring-vault-core/src/test/kotlin/org/springframework/vault/core/ReactiveVaultOperationsExtensionsTests.kt create mode 100644 spring-vault-core/src/test/kotlin/org/springframework/vault/core/VaultKeyValueOperationsExtensionsTests.kt create mode 100644 spring-vault-core/src/test/kotlin/org/springframework/vault/core/VaultOperationsExtensionsTests.kt create mode 100644 spring-vault-core/src/test/kotlin/org/springframework/vault/core/VaultVersionedKeyValueOperationsExtensionsTests.kt create mode 100644 spring-vault-core/src/test/kotlin/org/springframework/vault/core/VaultWrappingOperationsExtensionsTests.kt diff --git a/pom.xml b/pom.xml index 1d6041ab..97530191 100644 --- a/pom.xml +++ b/pom.xml @@ -25,6 +25,8 @@ 3.12.0 2.4.0 4.12 + 1.3.40 + 1.9.3 2.23.4 5.2.0.M3 Moore-M4 @@ -138,7 +140,6 @@ import - org.springframework.security @@ -148,6 +149,15 @@ import + + + org.jetbrains.kotlin + kotlin-bom + ${kotlin.version} + pom + import + + @@ -185,11 +195,55 @@ test + + io.mockk + mockk + ${mockk.version} + test + + + + org.jetbrains.kotlin + kotlin-maven-plugin + ${kotlin.version} + + ${java.version} + + + + compile + compile + + compile + + + + ${project.basedir}/src/main/kotlin + ${project.basedir}/src/main/java + + + + + test-compile + test-compile + + test-compile + + + + ${project.basedir}/src/test/kotlin + ${project.basedir}/src/test/java + + + + + + org.apache.maven.plugins maven-compiler-plugin @@ -197,6 +251,30 @@ ${java.version} ${java.version} + + + default-compile + none + + + default-testCompile + none + + + java-compile + compile + + compile + + + + java-test-compile + test-compile + + testCompile + + + @@ -395,6 +473,12 @@ 2.0.0 + + org.codehaus.mojo + build-helper-maven-plugin + 3.0.0 + + diff --git a/spring-vault-core/pom.xml b/spring-vault-core/pom.xml index cd80ef3e..00e4ceb1 100644 --- a/spring-vault-core/pom.xml +++ b/spring-vault-core/pom.xml @@ -26,6 +26,26 @@ META-INF + + + org.codehaus.mojo + build-helper-maven-plugin + + + add-kotlin-source + prepare-package + + add-source + + + + ${project.basedir}/src/main/kotlin + + + + + + @@ -93,6 +113,13 @@ true + + + org.jetbrains.kotlin + kotlin-stdlib + true + + @@ -202,6 +229,12 @@ test + + io.mockk + mockk + test + + org.mockito mockito-core diff --git a/spring-vault-core/src/main/kotlin/org/springframework/vault/core/ReactiveVaultOperationsExtensions.kt b/spring-vault-core/src/main/kotlin/org/springframework/vault/core/ReactiveVaultOperationsExtensions.kt new file mode 100644 index 00000000..8fa2f995 --- /dev/null +++ b/spring-vault-core/src/main/kotlin/org/springframework/vault/core/ReactiveVaultOperationsExtensions.kt @@ -0,0 +1,29 @@ +/* + * 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.vault.core + +import org.springframework.vault.support.VaultResponseSupport +import reactor.core.publisher.Mono + +/** + * Extension for [ReactiveVaultOperations.read] leveraging reified type parameters. + * + * @author Mark Paluch + * @since 2.2 + */ +@Suppress("EXTENSION_SHADOWED_BY_MEMBER") +inline fun ReactiveVaultOperations.read(path: String): Mono> = + read(path, T::class.java) diff --git a/spring-vault-core/src/main/kotlin/org/springframework/vault/core/VaultKeyValueOperationsExtensions.kt b/spring-vault-core/src/main/kotlin/org/springframework/vault/core/VaultKeyValueOperationsExtensions.kt new file mode 100644 index 00000000..d814840f --- /dev/null +++ b/spring-vault-core/src/main/kotlin/org/springframework/vault/core/VaultKeyValueOperationsExtensions.kt @@ -0,0 +1,28 @@ +/* + * 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.vault.core + +import org.springframework.vault.support.VaultResponseSupport + +/** + * Extension for [VaultKeyValueOperations.get] leveraging reified type parameters. + * + * @author Mark Paluch + * @since 2.2 + */ +@Suppress("EXTENSION_SHADOWED_BY_MEMBER") +inline fun VaultKeyValueOperations.get(path: String): VaultResponseSupport? = + get(path, T::class.java) diff --git a/spring-vault-core/src/main/kotlin/org/springframework/vault/core/VaultOperationsExtensions.kt b/spring-vault-core/src/main/kotlin/org/springframework/vault/core/VaultOperationsExtensions.kt new file mode 100644 index 00000000..a9dc84e0 --- /dev/null +++ b/spring-vault-core/src/main/kotlin/org/springframework/vault/core/VaultOperationsExtensions.kt @@ -0,0 +1,28 @@ +/* + * 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.vault.core + +import org.springframework.vault.support.VaultResponseSupport + +/** + * Extension for [VaultOperations.read] leveraging reified type parameters. + * + * @author Mark Paluch + * @since 2.2 + */ +@Suppress("EXTENSION_SHADOWED_BY_MEMBER") +inline fun VaultOperations.read(path: String): VaultResponseSupport? = + read(path, T::class.java) diff --git a/spring-vault-core/src/main/kotlin/org/springframework/vault/core/VaultVersionedKeyValueOperationsExtensions.kt b/spring-vault-core/src/main/kotlin/org/springframework/vault/core/VaultVersionedKeyValueOperationsExtensions.kt new file mode 100644 index 00000000..4ed887fc --- /dev/null +++ b/spring-vault-core/src/main/kotlin/org/springframework/vault/core/VaultVersionedKeyValueOperationsExtensions.kt @@ -0,0 +1,28 @@ +/* + * 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.vault.core + +import org.springframework.vault.support.Versioned + +/** + * Extension for [VaultVersionedKeyValueOperations.get] leveraging reified type parameters. + * + * @author Mark Paluch + * @since 2.2 + */ +@Suppress("EXTENSION_SHADOWED_BY_MEMBER") +inline fun VaultVersionedKeyValueOperations.get(path: String): Versioned? = + get(path, T::class.java) diff --git a/spring-vault-core/src/main/kotlin/org/springframework/vault/core/VaultWrappingOperationsExtensions.kt b/spring-vault-core/src/main/kotlin/org/springframework/vault/core/VaultWrappingOperationsExtensions.kt new file mode 100644 index 00000000..8c690040 --- /dev/null +++ b/spring-vault-core/src/main/kotlin/org/springframework/vault/core/VaultWrappingOperationsExtensions.kt @@ -0,0 +1,29 @@ +/* + * 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.vault.core + +import org.springframework.vault.support.VaultResponseSupport +import org.springframework.vault.support.VaultToken + +/** + * Extension for [VaultWrappingOperations.get] leveraging reified type parameters. + * + * @author Mark Paluch + * @since 2.2 + */ +@Suppress("EXTENSION_SHADOWED_BY_MEMBER") +inline fun VaultWrappingOperations.read(token: VaultToken): VaultResponseSupport? = + read(token, T::class.java) diff --git a/spring-vault-core/src/test/kotlin/org/springframework/vault/core/ReactiveVaultOperationsExtensionsTests.kt b/spring-vault-core/src/test/kotlin/org/springframework/vault/core/ReactiveVaultOperationsExtensionsTests.kt new file mode 100644 index 00000000..3848ce01 --- /dev/null +++ b/spring-vault-core/src/test/kotlin/org/springframework/vault/core/ReactiveVaultOperationsExtensionsTests.kt @@ -0,0 +1,46 @@ +/* + * 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.vault.core + +import io.mockk.mockk +import io.mockk.verify +import org.junit.Test + +/** + * Unit tests for [ReactiveVaultOperationsExtensions]. + * + * @author Mark Paluch + */ +class ReactiveVaultOperationsExtensionsTests { + + val operation = mockk(relaxed = true) + + @Test + fun `ReactiveVaultOperations#read() with reified type parameter extension should call its Java counterpart`() { + + operation.read("the-path") + verify { operation.read("the-path", Person::class.java) } + } + + @Test + fun `ReactiveVaultOperations#read() should call its Java counterpart`() { + + operation.read("the-path") + verify { operation.read("the-path") } + } + + class Person +} diff --git a/spring-vault-core/src/test/kotlin/org/springframework/vault/core/VaultKeyValueOperationsExtensionsTests.kt b/spring-vault-core/src/test/kotlin/org/springframework/vault/core/VaultKeyValueOperationsExtensionsTests.kt new file mode 100644 index 00000000..54edec96 --- /dev/null +++ b/spring-vault-core/src/test/kotlin/org/springframework/vault/core/VaultKeyValueOperationsExtensionsTests.kt @@ -0,0 +1,46 @@ +/* + * 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.vault.core + +import io.mockk.mockk +import io.mockk.verify +import org.junit.Test + +/** + * Unit tests for [VaultKeyValueOperationsExtensions]. + * + * @author Mark Paluch + */ +class VaultKeyValueOperationsExtensionsTests { + + val operation = mockk(relaxed = true) + + @Test + fun `VaultKeyValueOperations#get() with reified type parameter extension should call its Java counterpart`() { + + operation.get("the-path") + verify { operation.get("the-path", Person::class.java) } + } + + @Test + fun `VaultKeyValueOperations#get() should call its Java counterpart`() { + + operation.get("the-path") + verify { operation.get("the-path") } + } + + class Person +} diff --git a/spring-vault-core/src/test/kotlin/org/springframework/vault/core/VaultOperationsExtensionsTests.kt b/spring-vault-core/src/test/kotlin/org/springframework/vault/core/VaultOperationsExtensionsTests.kt new file mode 100644 index 00000000..9be49df6 --- /dev/null +++ b/spring-vault-core/src/test/kotlin/org/springframework/vault/core/VaultOperationsExtensionsTests.kt @@ -0,0 +1,46 @@ +/* + * 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.vault.core + +import io.mockk.mockk +import io.mockk.verify +import org.junit.Test + +/** + * Unit tests for [VaultOperationsExtensions]. + * + * @author Mark Paluch + */ +class VaultOperationsExtensionsTests { + + val operation = mockk(relaxed = true) + + @Test + fun `VaultOperations#read() with reified type parameter extension should call its Java counterpart`() { + + operation.read("the-path") + verify { operation.read("the-path", Person::class.java) } + } + + @Test + fun `VaultOperations#read() should call its Java counterpart`() { + + operation.read("the-path") + verify { operation.read("the-path") } + } + + class Person +} diff --git a/spring-vault-core/src/test/kotlin/org/springframework/vault/core/VaultVersionedKeyValueOperationsExtensionsTests.kt b/spring-vault-core/src/test/kotlin/org/springframework/vault/core/VaultVersionedKeyValueOperationsExtensionsTests.kt new file mode 100644 index 00000000..23ecb14a --- /dev/null +++ b/spring-vault-core/src/test/kotlin/org/springframework/vault/core/VaultVersionedKeyValueOperationsExtensionsTests.kt @@ -0,0 +1,46 @@ +/* + * 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.vault.core + +import io.mockk.mockk +import io.mockk.verify +import org.junit.Test + +/** + * Unit tests for [VaultVersionedKeyValueOperationsExtensions]. + * + * @author Mark Paluch + */ +class VaultVersionedKeyValueOperationsExtensionsTests { + + val operation = mockk(relaxed = true) + + @Test + fun `VaultVersionedKeyValueOperations#get() with reified type parameter extension should call its Java counterpart`() { + + operation.get("the-path") + verify { operation.get("the-path", Person::class.java) } + } + + @Test + fun `VaultVersionedKeyValueOperations#get() should call its Java counterpart`() { + + operation.get("the-path") + verify { operation.get("the-path") } + } + + class Person +} diff --git a/spring-vault-core/src/test/kotlin/org/springframework/vault/core/VaultWrappingOperationsExtensionsTests.kt b/spring-vault-core/src/test/kotlin/org/springframework/vault/core/VaultWrappingOperationsExtensionsTests.kt new file mode 100644 index 00000000..1e93e5c9 --- /dev/null +++ b/spring-vault-core/src/test/kotlin/org/springframework/vault/core/VaultWrappingOperationsExtensionsTests.kt @@ -0,0 +1,51 @@ +/* + * 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.vault.core + +import io.mockk.mockk +import io.mockk.verify +import org.junit.Test +import org.springframework.vault.support.VaultToken + +/** + * Unit tests for [VaultWrappingOperationsExtensions]. + * + * @author Mark Paluch + */ +class VaultWrappingOperationsExtensionsTests { + + val operation = mockk(relaxed = true) + + @Test + fun `VaultWrappingOperations#read() with reified type parameter extension should call its Java counterpart`() { + + val token = VaultToken.of("token") + + operation.read(token) + verify { operation.read(token, Person::class.java) } + } + + @Test + fun `VaultWrappingOperations#read() should call its Java counterpart`() { + + val token = VaultToken.of("token") + + operation.read(token) + verify { operation.read(token) } + } + + class Person +}