Add Kotlin extensions.

We now provide extensions for VaultOperations, ReactiveVaultOperations, VaultKeyValueOperations, VaultVersionedKeyValueOperations, and VaultWrappingOperations to leverage reified type parameters with read(…) methods.

Closes gh-442.
This commit is contained in:
Mark Paluch
2019-06-30 00:02:54 +02:00
parent 8f92d1899b
commit bcb6090a01
12 changed files with 495 additions and 1 deletions

86
pom.xml
View File

@@ -25,6 +25,8 @@
<assertj-core.version>3.12.0</assertj-core.version>
<json-path.version>2.4.0</json-path.version>
<junit.version>4.12</junit.version>
<kotlin.version>1.3.40</kotlin.version>
<mockk.version>1.9.3</mockk.version>
<mockito-core.version>2.23.4</mockito-core.version>
<spring.version>5.2.0.M3</spring.version>
<spring-data-releasetrain.version>Moore-M4</spring-data-releasetrain.version>
@@ -138,7 +140,6 @@
<scope>import</scope>
</dependency>
<!-- Spring Security -->
<dependency>
<groupId>org.springframework.security</groupId>
@@ -148,6 +149,15 @@
<scope>import</scope>
</dependency>
<!-- Kotlin -->
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-bom</artifactId>
<version>${kotlin.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!-- Testing -->
<dependency>
@@ -185,11 +195,55 @@
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.mockk</groupId>
<artifactId>mockk</artifactId>
<version>${mockk.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-plugin</artifactId>
<version>${kotlin.version}</version>
<configuration>
<jvmTarget>${java.version}</jvmTarget>
</configuration>
<executions>
<execution>
<id>compile</id>
<phase>compile</phase>
<goals>
<goal>compile</goal>
</goals>
<configuration>
<sourceDirs>
<sourceDir>${project.basedir}/src/main/kotlin</sourceDir>
<sourceDir>${project.basedir}/src/main/java</sourceDir>
</sourceDirs>
</configuration>
</execution>
<execution>
<id>test-compile</id>
<phase>test-compile</phase>
<goals>
<goal>test-compile</goal>
</goals>
<configuration>
<sourceDirs>
<sourceDir>${project.basedir}/src/test/kotlin</sourceDir>
<sourceDir>${project.basedir}/src/test/java</sourceDir>
</sourceDirs>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
@@ -197,6 +251,30 @@
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
<executions>
<execution>
<id>default-compile</id>
<phase>none</phase>
</execution>
<execution>
<id>default-testCompile</id>
<phase>none</phase>
</execution>
<execution>
<id>java-compile</id>
<phase>compile</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>
<execution>
<id>java-test-compile</id>
<phase>test-compile</phase>
<goals>
<goal>testCompile</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
@@ -395,6 +473,12 @@
<version>2.0.0</version>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>3.0.0</version>
</plugin>
</plugins>
</pluginManagement>
</build>

View File

@@ -26,6 +26,26 @@
<targetPath>META-INF</targetPath>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<executions>
<execution>
<id>add-kotlin-source</id>
<phase>prepare-package</phase>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>${project.basedir}/src/main/kotlin</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencies>
@@ -93,6 +113,13 @@
<optional>true</optional>
</dependency>
<!-- Kotlin extension -->
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib</artifactId>
<optional>true</optional>
</dependency>
<!-- HTTP clients -->
<dependency>
@@ -202,6 +229,12 @@
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.mockk</groupId>
<artifactId>mockk</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>

View File

@@ -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 <reified T : Any> ReactiveVaultOperations.read(path: String): Mono<VaultResponseSupport<T>> =
read(path, T::class.java)

View File

@@ -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 <reified T : Any> VaultKeyValueOperations.get(path: String): VaultResponseSupport<T>? =
get(path, T::class.java)

View File

@@ -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 <reified T : Any> VaultOperations.read(path: String): VaultResponseSupport<T>? =
read(path, T::class.java)

View File

@@ -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 <reified T : Any> VaultVersionedKeyValueOperations.get(path: String): Versioned<T>? =
get(path, T::class.java)

View File

@@ -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 <reified T : Any> VaultWrappingOperations.read(token: VaultToken): VaultResponseSupport<T>? =
read(token, T::class.java)

View File

@@ -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<ReactiveVaultOperations>(relaxed = true)
@Test
fun `ReactiveVaultOperations#read() with reified type parameter extension should call its Java counterpart`() {
operation.read<Person>("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
}

View File

@@ -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<VaultKeyValueOperations>(relaxed = true)
@Test
fun `VaultKeyValueOperations#get() with reified type parameter extension should call its Java counterpart`() {
operation.get<Person>("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
}

View File

@@ -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<VaultOperations>(relaxed = true)
@Test
fun `VaultOperations#read() with reified type parameter extension should call its Java counterpart`() {
operation.read<Person>("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
}

View File

@@ -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<VaultVersionedKeyValueOperations>(relaxed = true)
@Test
fun `VaultVersionedKeyValueOperations#get() with reified type parameter extension should call its Java counterpart`() {
operation.get<Person>("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
}

View File

@@ -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<VaultWrappingOperations>(relaxed = true)
@Test
fun `VaultWrappingOperations#read() with reified type parameter extension should call its Java counterpart`() {
val token = VaultToken.of("token")
operation.read<Person>(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
}