DATAREDIS-937 - Add ReactiveRedisOperations Coroutines extensions.

This commit introduces Coroutines support for ReactiveRedisOperations and its sub-operation APIs via Kotlin extensions that provide
suspendable functions prefixed by `await` or suffixed by `AndAwait` for Mono based APIs.

Extensions for Flux will be added when Kotlin/kotlinx.coroutines#254 will be fixed.

Original Pull Request: #386
This commit is contained in:
Mark Paluch
2019-02-25 17:01:16 +01:00
committed by Christoph Strobl
parent 087dba0735
commit 02c260aae1
19 changed files with 3642 additions and 0 deletions

View File

@@ -0,0 +1,217 @@
/*
* 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
*
* http://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.redis.core
import io.mockk.every
import io.mockk.mockk
import io.mockk.verify
import kotlinx.coroutines.runBlocking
import org.assertj.core.api.Assertions.assertThat
import org.junit.Test
import org.springframework.data.geo.Distance
import org.springframework.data.geo.Metrics
import org.springframework.data.geo.Point
import org.springframework.data.redis.connection.RedisGeoCommands
import reactor.core.publisher.Mono
/**
* Unit tests for [ReactiveGeoOperationsExtensions].
*
* @author Mark Paluch
*/
class ReactiveGeoOperationsExtensionsUnitTests {
@Test // DATAREDIS-937
fun addPoint() {
val operations = mockk<ReactiveGeoOperations<String, String>>()
every { operations.add(any(), any(), any()) } returns Mono.just(1)
runBlocking {
assertThat(operations.addAndAwait("foo", Point(1.0, 2.0), "bar")).isEqualTo(1)
}
verify {
operations.add("foo", Point(1.0, 2.0), "bar")
}
}
@Test // DATAREDIS-937
fun addGeoLocation() {
val operations = mockk<ReactiveGeoOperations<String, String>>()
every { operations.add(any(), any<RedisGeoCommands.GeoLocation<String>>()) } returns Mono.just(1)
runBlocking {
assertThat(operations.addAndAwait("foo", RedisGeoCommands.GeoLocation("bar", Point(1.0, 2.0)))).isEqualTo(1)
}
verify {
operations.add("foo", RedisGeoCommands.GeoLocation("bar", Point(1.0, 2.0)))
}
}
@Test // DATAREDIS-937
fun addLocationMap() {
val operations = mockk<ReactiveGeoOperations<String, String>>()
every { operations.add(any(), any<Map<String, Point>>()) } returns Mono.just(1)
runBlocking {
assertThat(operations.addAndAwait("foo", mapOf("foo" to Point(1.0, 2.0)))).isEqualTo(1)
}
verify {
operations.add("foo", mapOf("foo" to Point(1.0, 2.0)))
}
}
@Test // DATAREDIS-937
fun addGeoLocationList() {
val operations = mockk<ReactiveGeoOperations<String, String>>()
every { operations.add(any(), any<List<RedisGeoCommands.GeoLocation<String>>>()) } returns Mono.just(1)
runBlocking {
assertThat(operations.addAndAwait("foo", listOf(RedisGeoCommands.GeoLocation("bar", Point(1.0, 2.0))))).isEqualTo(1)
}
verify {
operations.add("foo", listOf(RedisGeoCommands.GeoLocation("bar", Point(1.0, 2.0))))
}
}
@Test // DATAREDIS-937
fun distance() {
val operations = mockk<ReactiveGeoOperations<String, String>>()
every { operations.distance(any(), any(), any()) } returns Mono.just(Distance(2.0))
runBlocking {
assertThat(operations.distanceAndAwait("foo", "from", "to")).isEqualTo(Distance(2.0))
}
verify {
operations.distance("foo", "from", "to")
}
}
@Test // DATAREDIS-937
fun distanceWithMetric() {
val operations = mockk<ReactiveGeoOperations<String, String>>()
every { operations.distance(any(), any(), any(), any()) } returns Mono.just(Distance(2.0))
runBlocking {
assertThat(operations.distanceAndAwait("foo", "from", "to", Metrics.KILOMETERS)).isEqualTo(Distance(2.0))
}
verify {
operations.distance("foo", "from", "to", Metrics.KILOMETERS)
}
}
@Test // DATAREDIS-937
fun hash() {
val operations = mockk<ReactiveGeoOperations<String, String>>()
every { operations.hash(any(), any()) } returns Mono.just("baz")
runBlocking {
assertThat(operations.hashAndAwait("foo", "bar")).isEqualTo("baz")
}
verify {
operations.hash("foo", "bar")
}
}
@Test // DATAREDIS-937
fun hashVararg() {
val operations = mockk<ReactiveGeoOperations<String, String>>()
every { operations.hash(any(), any(), any()) } returns Mono.just(listOf("baz1", "baz2"))
runBlocking {
assertThat(operations.hashAndAwait("foo", "bar", "baz")).isEqualTo(listOf("baz1", "baz2"))
}
verify {
operations.hash("foo", "bar", "baz")
}
}
@Test // DATAREDIS-937
fun position() {
val operations = mockk<ReactiveGeoOperations<String, String>>()
every { operations.position(any(), any()) } returns Mono.just(Point(1.0, 2.0))
runBlocking {
assertThat(operations.positionAndAwait("foo", "bar")).isEqualTo(Point(1.0, 2.0))
}
verify {
operations.position("foo", "bar")
}
}
@Test // DATAREDIS-937
fun positionVararg() {
val operations = mockk<ReactiveGeoOperations<String, String>>()
every { operations.position(any(), any(), any()) } returns Mono.just(listOf(Point(1.0, 2.0), Point(2.0, 3.0)))
runBlocking {
assertThat(operations.positionAndAwait("foo", "bar", "baz")).isEqualTo(listOf(Point(1.0, 2.0), Point(2.0, 3.0)))
}
verify {
operations.position("foo", "bar", "baz")
}
}
@Test // DATAREDIS-937
fun remove() {
val operations = mockk<ReactiveGeoOperations<String, String>>()
every { operations.remove(any(), any()) } returns Mono.just(1)
runBlocking {
assertThat(operations.removeAndAwait("foo", "bar")).isEqualTo(1)
}
verify {
operations.remove("foo", "bar")
}
}
@Test // DATAREDIS-937
fun delete() {
val operations = mockk<ReactiveGeoOperations<String, String>>()
every { operations.delete(any()) } returns Mono.just(true)
runBlocking {
assertThat(operations.deleteAndAwait("foo")).isTrue()
}
verify {
operations.delete("foo")
}
}
}

View File

@@ -0,0 +1,182 @@
/*
* 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
*
* http://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.redis.core
import io.mockk.every
import io.mockk.mockk
import io.mockk.verify
import kotlinx.coroutines.runBlocking
import org.assertj.core.api.Assertions.assertThat
import org.junit.Test
import reactor.core.publisher.Mono
/**
* Unit tests for [ReactiveHashOperationsExtensions].
*
* @author Mark Paluch
*/
class ReactiveHashOperationsExtensionsUnitTests {
@Test // DATAREDIS-937
fun hasKey() {
val operations = mockk<ReactiveHashOperations<String, String, String>>()
every { operations.hasKey(any(), any()) } returns Mono.just(true)
runBlocking {
assertThat(operations.hasKeyAndAwait("foo", "bar")).isTrue()
}
verify {
operations.hasKey("foo", "bar")
}
}
@Test // DATAREDIS-937
fun get() {
val operations = mockk<ReactiveHashOperations<String, String, String>>()
every { operations.get(any(), any()) } returns Mono.just("baz")
runBlocking {
assertThat(operations.getAndAwait("foo", "bar")).isEqualTo("baz")
}
verify {
operations.get("foo", "bar")
}
}
@Test // DATAREDIS-937
fun multiGet() {
val operations = mockk<ReactiveHashOperations<String, String, String>>()
every { operations.multiGet(any(), any()) } returns Mono.just(listOf("baz1", "baz2"))
runBlocking {
assertThat(operations.multiGetAndAwait("foo", "bar", "joe")).isEqualTo(listOf("baz1", "baz2"))
}
verify {
operations.multiGet("foo", listOf("bar", "joe"))
}
}
@Test // DATAREDIS-937
fun increment() {
val operations = mockk<ReactiveHashOperations<String, String, String>>()
every { operations.increment(any(), any(), 1) } returns Mono.just(2)
runBlocking {
assertThat(operations.incrementAndAwait("foo", "bar", 1)).isEqualTo(2)
}
verify {
operations.increment("foo", "bar", 1)
}
}
@Test // DATAREDIS-937
fun incrementDouble() {
val operations = mockk<ReactiveHashOperations<String, String, String>>()
every { operations.increment(any(), any(), 1.0) } returns Mono.just(2.0)
runBlocking {
assertThat(operations.incrementAndAwait("foo", "bar", 1.0)).isEqualTo(2.0)
}
verify {
operations.increment("foo", "bar", 1.0)
}
}
@Test // DATAREDIS-937
fun size() {
val operations = mockk<ReactiveHashOperations<String, String, String>>()
every { operations.size(any()) } returns Mono.just(1)
runBlocking {
assertThat(operations.sizeAndAwait("foo")).isEqualTo(1)
}
verify {
operations.size("foo")
}
}
@Test // DATAREDIS-937
fun put() {
val operations = mockk<ReactiveHashOperations<String, String, String>>()
every { operations.put(any(), any(), any()) } returns Mono.just(true)
runBlocking {
assertThat(operations.putAndAwait("foo", "bar", "baz")).isTrue()
}
verify {
operations.put("foo", "bar", "baz")
}
}
@Test // DATAREDIS-937
fun putAll() {
val operations = mockk<ReactiveHashOperations<String, String, String>>()
every { operations.putAll(any(), any()) } returns Mono.just(true)
runBlocking {
assertThat(operations.putAllAndAwait("foo", mapOf("bar" to "baz"))).isTrue()
}
verify {
operations.putAll("foo", mapOf("bar" to "baz"))
}
}
@Test // DATAREDIS-937
fun putIfAbsent() {
val operations = mockk<ReactiveHashOperations<String, String, String>>()
every { operations.putIfAbsent(any(), any(), any()) } returns Mono.just(true)
runBlocking {
assertThat(operations.putIfAbsentAndAwait("foo", "bar", "baz")).isTrue()
}
verify {
operations.putIfAbsent("foo", "bar", "baz")
}
}
@Test // DATAREDIS-937
fun remove() {
val operations = mockk<ReactiveHashOperations<String, String, String>>()
every { operations.remove(any(), any()) } returns Mono.just(1)
runBlocking {
assertThat(operations.removeAndAwait("foo", "bar")).isEqualTo(1)
}
verify {
operations.remove("foo", "bar")
}
}
}

View File

@@ -0,0 +1,92 @@
/*
* 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
*
* http://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.redis.core
import io.mockk.every
import io.mockk.mockk
import io.mockk.verify
import kotlinx.coroutines.runBlocking
import org.assertj.core.api.Assertions.assertThat
import org.junit.Test
import reactor.core.publisher.Mono
/**
* Unit tests for [ReactiveHyperLogLogOperationsExtensions]
*
* @author Mark Paluch
*/
class ReactiveHyperLogLogOperationsExtensionsUnitTests {
@Test // DATAREDIS-937
fun add() {
val operations = mockk<ReactiveHyperLogLogOperations<String, String>>()
every { operations.add(any(), any()) } returns Mono.just(1)
runBlocking {
assertThat(operations.addAndAwait("foo", "bar")).isEqualTo(1)
}
verify {
operations.add("foo", "bar")
}
}
@Test // DATAREDIS-937
fun size() {
val operations = mockk<ReactiveHyperLogLogOperations<String, String>>()
every { operations.size("foo") } returns Mono.just(1)
runBlocking {
assertThat(operations.sizeAndAwait("foo")).isEqualTo(1)
}
verify {
operations.size("foo")
}
}
@Test // DATAREDIS-937
fun union() {
val operations = mockk<ReactiveHyperLogLogOperations<String, String>>()
every { operations.union("foo", "bar", "baz") } returns Mono.just(true)
runBlocking {
assertThat(operations.unionAndAwait("foo", "bar", "baz")).isTrue()
}
verify {
operations.union("foo", "bar", "baz")
}
}
@Test // DATAREDIS-937
fun remove() {
val operations = mockk<ReactiveHyperLogLogOperations<String, String>>()
every { operations.delete(any()) } returns Mono.just(true)
runBlocking {
assertThat(operations.deleteAndAwait("foo")).isTrue()
}
verify {
operations.delete("foo")
}
}
}

View File

@@ -0,0 +1,334 @@
/*
* 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
*
* http://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.redis.core
import io.mockk.every
import io.mockk.mockk
import io.mockk.verify
import kotlinx.coroutines.runBlocking
import org.assertj.core.api.Assertions.assertThat
import org.junit.Test
import reactor.core.publisher.Mono
import java.time.Duration
/**
* Unit tests for [ReactiveHyperLogLogOperationsExtensions]
*
* @author Mark Paluch
*/
class ReactiveListOperationsExtensionsUnitTests {
@Test // DATAREDIS-937
fun trim() {
val operations = mockk<ReactiveListOperations<String, String>>()
every { operations.trim(any(), any(), any()) } returns Mono.just(true)
runBlocking {
assertThat(operations.trimAndAwait("foo", 2, 3)).isTrue()
}
verify {
operations.trim("foo", 2, 3)
}
}
@Test // DATAREDIS-937
fun size() {
val operations = mockk<ReactiveListOperations<String, String>>()
every { operations.size(any()) } returns Mono.just(2)
runBlocking {
assertThat(operations.sizeAndAwait("foo")).isEqualTo(2)
}
verify {
operations.size("foo")
}
}
@Test // DATAREDIS-937
fun leftPush() {
val operations = mockk<ReactiveListOperations<String, String>>()
every { operations.leftPush(any(), any()) } returns Mono.just(2)
runBlocking {
assertThat(operations.leftPushAndAwait("foo", "bar")).isEqualTo(2)
}
verify {
operations.leftPush("foo", "bar")
}
}
@Test // DATAREDIS-937
fun leftPushAll() {
val operations = mockk<ReactiveListOperations<String, String>>()
every { operations.leftPushAll("foo", "bar", "baz") } returns Mono.just(2)
runBlocking {
assertThat(operations.leftPushAllAndAwait("foo", "bar", "baz")).isEqualTo(2)
}
verify {
operations.leftPushAll("foo", "bar", "baz")
}
}
@Test // DATAREDIS-937
fun leftPushAllCollection() {
val operations = mockk<ReactiveListOperations<String, String>>()
every { operations.leftPushAll("foo", listOf("bar", "baz")) } returns Mono.just(2)
runBlocking {
assertThat(operations.leftPushAllAndAwait("foo", listOf("bar", "baz"))).isEqualTo(2)
}
verify {
operations.leftPushAll("foo", listOf("bar", "baz"))
}
}
@Test // DATAREDIS-937
fun leftPushIfPresent() {
val operations = mockk<ReactiveListOperations<String, String>>()
every { operations.leftPushIfPresent(any(), any()) } returns Mono.just(2)
runBlocking {
assertThat(operations.leftPushIfPresentAndAwait("foo", "bar")).isEqualTo(2)
}
verify {
operations.leftPushIfPresent("foo", "bar")
}
}
@Test // DATAREDIS-937
fun leftPushPivot() {
val operations = mockk<ReactiveListOperations<String, String>>()
every { operations.leftPush("foo", "bar", "baz") } returns Mono.just(2)
runBlocking {
assertThat(operations.leftPushAndAwait("foo", "bar", "baz")).isEqualTo(2)
}
verify {
operations.leftPush("foo", "bar", "baz")
}
}
@Test // DATAREDIS-937
fun rightPush() {
val operations = mockk<ReactiveListOperations<String, String>>()
every { operations.rightPush(any(), any()) } returns Mono.just(2)
runBlocking {
assertThat(operations.rightPushAndAwait("foo", "bar")).isEqualTo(2)
}
verify {
operations.rightPush("foo", "bar")
}
}
@Test // DATAREDIS-937
fun rightPushAll() {
val operations = mockk<ReactiveListOperations<String, String>>()
every { operations.rightPushAll("foo", "bar", "baz") } returns Mono.just(2)
runBlocking {
assertThat(operations.rightPushAllAndAwait("foo", "bar", "baz")).isEqualTo(2)
}
verify {
operations.rightPushAll("foo", "bar", "baz")
}
}
@Test // DATAREDIS-937
fun rightPushAllCollection() {
val operations = mockk<ReactiveListOperations<String, String>>()
every { operations.rightPushAll("foo", listOf("bar", "baz")) } returns Mono.just(2)
runBlocking {
assertThat(operations.rightPushAllAndAwait("foo", listOf("bar", "baz"))).isEqualTo(2)
}
verify {
operations.rightPushAll("foo", listOf("bar", "baz"))
}
}
@Test // DATAREDIS-937
fun rightPushIfPresent() {
val operations = mockk<ReactiveListOperations<String, String>>()
every { operations.rightPushIfPresent(any(), any()) } returns Mono.just(2)
runBlocking {
assertThat(operations.rightPushIfPresentAndAwait("foo", "bar")).isEqualTo(2)
}
verify {
operations.rightPushIfPresent("foo", "bar")
}
}
@Test // DATAREDIS-937
fun rightPushPivot() {
val operations = mockk<ReactiveListOperations<String, String>>()
every { operations.rightPush("foo", "bar", "baz") } returns Mono.just(2)
runBlocking {
assertThat(operations.rightPushAndAwait("foo", "bar", "baz")).isEqualTo(2)
}
verify {
operations.rightPush("foo", "bar", "baz")
}
}
@Test // DATAREDIS-937
fun set() {
val operations = mockk<ReactiveListOperations<String, String>>()
every { operations.set(any(), any(), any()) } returns Mono.just(true)
runBlocking {
assertThat(operations.setAndAwait("foo", 1, "baz")).isTrue()
}
verify {
operations.set("foo", 1, "baz")
}
}
@Test // DATAREDIS-937
fun remove() {
val operations = mockk<ReactiveListOperations<String, String>>()
every { operations.remove(any(), any(), any()) } returns Mono.just(1)
runBlocking {
assertThat(operations.removeAndAwait("foo", 1, "baz")).isEqualTo(1)
}
verify {
operations.remove("foo", 1, "baz")
}
}
@Test // DATAREDIS-937
fun index() {
val operations = mockk<ReactiveListOperations<String, String>>()
every { operations.index(any(), any()) } returns Mono.just("foo")
runBlocking {
assertThat(operations.indexAndAwait("foo", 1)).isEqualTo("foo")
}
verify {
operations.index("foo", 1)
}
}
@Test // DATAREDIS-937
fun leftPop() {
val operations = mockk<ReactiveListOperations<String, String>>()
every { operations.leftPop(any()) } returns Mono.just("foo")
runBlocking {
assertThat(operations.leftPopAndAwait("foo")).isEqualTo("foo")
}
verify {
operations.leftPop("foo")
}
}
@Test // DATAREDIS-937
fun blockingLeftPop() {
val operations = mockk<ReactiveListOperations<String, String>>()
every { operations.leftPop(any(), any()) } returns Mono.just("foo")
runBlocking {
assertThat(operations.leftPopAndAwait("foo", Duration.ofDays(1))).isEqualTo("foo")
}
verify {
operations.leftPop("foo", Duration.ofDays(1))
}
}
@Test // DATAREDIS-937
fun rightPop() {
val operations = mockk<ReactiveListOperations<String, String>>()
every { operations.rightPop(any()) } returns Mono.just("foo")
runBlocking {
assertThat(operations.rightPopAndAwait("foo")).isEqualTo("foo")
}
verify {
operations.rightPop("foo")
}
}
@Test // DATAREDIS-937
fun blockingRightPop() {
val operations = mockk<ReactiveListOperations<String, String>>()
every { operations.rightPop(any(), any()) } returns Mono.just("foo")
runBlocking {
assertThat(operations.rightPopAndAwait("foo", Duration.ofDays(1))).isEqualTo("foo")
}
verify {
operations.rightPop("foo", Duration.ofDays(1))
}
}
@Test // DATAREDIS-937
fun delete() {
val operations = mockk<ReactiveListOperations<String, String>>()
every { operations.delete(any()) } returns Mono.just(true)
runBlocking {
assertThat(operations.deleteAndAwait("foo")).isTrue()
}
verify {
operations.delete("foo")
}
}
}

View File

@@ -0,0 +1,231 @@
/*
* 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
*
* http://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.redis.core
import io.mockk.every
import io.mockk.mockk
import io.mockk.verify
import kotlinx.coroutines.runBlocking
import org.assertj.core.api.Assertions.assertThat
import org.junit.Test
import org.springframework.data.redis.connection.DataType
import reactor.core.publisher.Mono
import java.time.Duration
import java.time.Instant
/**
* Unit tests for [ReactiveRedisOperationsExtensions].
*
* @author Mark Paluch
*/
class ReactiveRedisOperationsExtensionsUnitTests {
@Test // DATAREDIS-937
fun convertAndSend() {
val operations = mockk<ReactiveRedisOperations<String, String>>()
every { operations.convertAndSend(any(), any()) } returns Mono.just(1)
runBlocking {
assertThat(operations.sendAndAwait("foo", "bar")).isEqualTo(1)
}
verify {
operations.convertAndSend("foo", "bar")
}
}
@Test // DATAREDIS-937
fun hasKey() {
val operations = mockk<ReactiveRedisOperations<String, String>>()
every { operations.hasKey(any()) } returns Mono.just(true)
runBlocking {
assertThat(operations.hasKeyAndAwait("foo")).isTrue()
}
verify {
operations.hasKey("foo")
}
}
@Test // DATAREDIS-937
fun type() {
val operations = mockk<ReactiveRedisOperations<String, String>>()
every { operations.type(any()) } returns Mono.just(DataType.HASH)
runBlocking {
assertThat(operations.typeAndAwait("foo")).isEqualTo(DataType.HASH)
}
verify {
operations.type("foo")
}
}
@Test // DATAREDIS-937
fun randomKey() {
val operations = mockk<ReactiveRedisOperations<String, String>>()
every { operations.randomKey() } returns Mono.just("foo")
runBlocking {
assertThat(operations.randomKeyAndAwait()).isEqualTo("foo")
}
verify {
operations.randomKey()
}
}
@Test // DATAREDIS-937
fun rename() {
val operations = mockk<ReactiveRedisOperations<String, String>>()
every { operations.rename(any(), any()) } returns Mono.just(true)
runBlocking {
assertThat(operations.renameAndAwait("foo", "bar")).isTrue()
}
verify {
operations.rename("foo", "bar")
}
}
@Test // DATAREDIS-937
fun renameIfAbsent() {
val operations = mockk<ReactiveRedisOperations<String, String>>()
every { operations.renameIfAbsent(any(), any()) } returns Mono.just(true)
runBlocking {
assertThat(operations.renameIfAbsentAndAwait("foo", "bar")).isTrue()
}
verify {
operations.renameIfAbsent("foo", "bar")
}
}
@Test // DATAREDIS-937
fun delete() {
val operations = mockk<ReactiveRedisOperations<String, String>>()
every { operations.delete("foo", "bar") } returns Mono.just(2)
runBlocking {
assertThat(operations.deleteAndAwait("foo", "bar")).isEqualTo(2)
}
verify {
operations.delete("foo", "bar")
}
}
@Test // DATAREDIS-937
fun unlink() {
val operations = mockk<ReactiveRedisOperations<String, String>>()
every { operations.unlink("foo", "bar") } returns Mono.just(2)
runBlocking {
assertThat(operations.unlinkAndAwait("foo", "bar")).isEqualTo(2)
}
verify {
operations.unlink("foo", "bar")
}
}
@Test // DATAREDIS-937
fun expire() {
val operations = mockk<ReactiveRedisOperations<String, String>>()
every { operations.expire(any(), any()) } returns Mono.just(true)
runBlocking {
assertThat(operations.expireAndAwait("foo", Duration.ofDays(1))).isTrue()
}
verify {
operations.expire("foo", Duration.ofDays(1))
}
}
@Test // DATAREDIS-937
fun expireAt() {
val operations = mockk<ReactiveRedisOperations<String, String>>()
every { operations.expireAt(any(), any()) } returns Mono.just(true)
runBlocking {
assertThat(operations.expireAtAndAwait("foo", Instant.ofEpochSecond(2))).isTrue()
}
verify {
operations.expireAt("foo", Instant.ofEpochSecond(2))
}
}
@Test // DATAREDIS-937
fun persist() {
val operations = mockk<ReactiveRedisOperations<String, String>>()
every { operations.persist(any()) } returns Mono.just(true)
runBlocking {
assertThat(operations.persistAndAwait("foo")).isTrue()
}
verify {
operations.persist("foo")
}
}
@Test // DATAREDIS-937
fun move() {
val operations = mockk<ReactiveRedisOperations<String, String>>()
every { operations.move(any(), any()) } returns Mono.just(true)
runBlocking {
assertThat(operations.moveAndAwait("foo", 2)).isTrue()
}
verify {
operations.move("foo", 2)
}
}
@Test // DATAREDIS-937
fun getExpire() {
val operations = mockk<ReactiveRedisOperations<String, String>>()
every { operations.getExpire(any()) } returns Mono.just(Duration.ofDays(1))
runBlocking {
assertThat(operations.getExpireAndAwait("foo")).isEqualTo(Duration.ofDays(1))
}
verify {
operations.getExpire("foo")
}
}
}

View File

@@ -0,0 +1,242 @@
/*
* 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
*
* http://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.redis.core
import io.mockk.every
import io.mockk.mockk
import io.mockk.verify
import kotlinx.coroutines.runBlocking
import org.assertj.core.api.Assertions.assertThat
import org.junit.Test
import reactor.core.publisher.Mono
/**
* Unit tests for [ReactiveSetOperationsExtensions].
*
* @author Mark Paluch
*/
class ReactiveSetOperationsExtensionsUnitTests {
@Test // DATAREDIS-937
fun add() {
val operations = mockk<ReactiveSetOperations<String, String>>()
every { operations.add("foo", "bar", "baz") } returns Mono.just(1)
runBlocking {
assertThat(operations.addAndAwait("foo", "bar", "baz")).isEqualTo(1)
}
verify {
operations.add("foo", "bar", "baz")
}
}
@Test // DATAREDIS-937
fun remove() {
val operations = mockk<ReactiveSetOperations<String, String>>()
every { operations.remove("foo", "bar", "baz") } returns Mono.just(1)
runBlocking {
assertThat(operations.removeAndAwait("foo", "bar", "baz")).isEqualTo(1)
}
verify {
operations.remove("foo", "bar", "baz")
}
}
@Test // DATAREDIS-937
fun pop() {
val operations = mockk<ReactiveSetOperations<String, String>>()
every { operations.pop(any()) } returns Mono.just("bar")
runBlocking {
assertThat(operations.popAndAwait("foo")).isEqualTo("bar")
}
verify {
operations.pop("foo")
}
}
@Test // DATAREDIS-937
fun move() {
val operations = mockk<ReactiveSetOperations<String, String>>()
every { operations.move(any(), any(), any()) } returns Mono.just(true)
runBlocking {
assertThat(operations.moveAndAwait("foo", "from", "to")).isTrue()
}
verify {
operations.move("foo", "from", "to")
}
}
@Test // DATAREDIS-937
fun size() {
val operations = mockk<ReactiveSetOperations<String, String>>()
every { operations.size(any()) } returns Mono.just(1)
runBlocking {
assertThat(operations.sizeAndAwait("foo")).isEqualTo(1)
}
verify {
operations.size("foo")
}
}
@Test // DATAREDIS-937
fun isMember() {
val operations = mockk<ReactiveSetOperations<String, String>>()
every { operations.isMember("foo", "bar") } returns Mono.just(true)
runBlocking {
assertThat(operations.isMemberAndAwait("foo", "bar")).isTrue()
}
verify {
operations.isMember("foo", "bar")
}
}
@Test // DATAREDIS-937
fun intersectAndStore() {
val operations = mockk<ReactiveSetOperations<String, String>>()
every { operations.intersectAndStore("foo", "bar", "baz") } returns Mono.just(3)
runBlocking {
assertThat(operations.intersectAndStoreAndAwait("foo", "bar", "baz")).isEqualTo(3)
}
verify {
operations.intersectAndStore("foo", "bar", "baz")
}
}
@Test // DATAREDIS-937
fun intersectAndStoreCollection() {
val operations = mockk<ReactiveSetOperations<String, String>>()
every { operations.intersectAndStore(listOf("foo", "bar"), "baz") } returns Mono.just(3)
runBlocking {
assertThat(operations.intersectAndStoreAndAwait(listOf("foo", "bar"), "baz")).isEqualTo(3)
}
verify {
operations.intersectAndStore(listOf("foo", "bar"), "baz")
}
}
@Test // DATAREDIS-937
fun unionAndStore() {
val operations = mockk<ReactiveSetOperations<String, String>>()
every { operations.unionAndStore("foo", "bar", "baz") } returns Mono.just(3)
runBlocking {
assertThat(operations.unionAndStoreAndAwait("foo", "bar", "baz")).isEqualTo(3)
}
verify {
operations.unionAndStore("foo", "bar", "baz")
}
}
@Test // DATAREDIS-937
fun unionAndStoreCollection() {
val operations = mockk<ReactiveSetOperations<String, String>>()
every { operations.unionAndStore(listOf("foo", "bar"), "baz") } returns Mono.just(3)
runBlocking {
assertThat(operations.unionAndStoreAndAwait(listOf("foo", "bar"), "baz")).isEqualTo(3)
}
verify {
operations.unionAndStore(listOf("foo", "bar"), "baz")
}
}
@Test // DATAREDIS-937
fun differenceAndStore() {
val operations = mockk<ReactiveSetOperations<String, String>>()
every { operations.differenceAndStore("foo", "bar", "baz") } returns Mono.just(3)
runBlocking {
assertThat(operations.differenceAndStoreAndAwait("foo", "bar", "baz")).isEqualTo(3)
}
verify {
operations.differenceAndStore("foo", "bar", "baz")
}
}
@Test // DATAREDIS-937
fun differenceAndStoreCollection() {
val operations = mockk<ReactiveSetOperations<String, String>>()
every { operations.differenceAndStore(listOf("foo", "bar"), "baz") } returns Mono.just(3)
runBlocking {
assertThat(operations.differenceAndStoreAndAwait(listOf("foo", "bar"), "baz")).isEqualTo(3)
}
verify {
operations.differenceAndStore(listOf("foo", "bar"), "baz")
}
}
@Test // DATAREDIS-937
fun randomMember() {
val operations = mockk<ReactiveSetOperations<String, String>>()
every { operations.randomMember(any()) } returns Mono.just("bar")
runBlocking {
assertThat(operations.randomMemberAndAwait("foo")).isEqualTo("bar")
}
verify {
operations.randomMember("foo")
}
}
@Test // DATAREDIS-937
fun delete() {
val operations = mockk<ReactiveSetOperations<String, String>>()
every { operations.delete(any()) } returns Mono.just(true)
runBlocking {
assertThat(operations.deleteAndAwait("foo")).isTrue()
}
verify {
operations.delete("foo")
}
}
}

View File

@@ -0,0 +1,250 @@
/*
* 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
*
* http://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.redis.core
import io.mockk.every
import io.mockk.mockk
import io.mockk.verify
import kotlinx.coroutines.runBlocking
import org.assertj.core.api.Assertions.assertThat
import org.junit.Test
import org.springframework.data.redis.connection.stream.*
import reactor.core.publisher.Mono
/**
* Unit tests for [ReactiveStreamOperationsExtensions].
*
* @author Mark Paluch
*/
class ReactiveStreamOperationsExtensionsUnitTests {
@Test // DATAREDIS-937
fun acknowledge() {
val operations = mockk<ReactiveStreamOperations<String, String, String>>()
every { operations.acknowledge("foo", "bar", "0-0") } returns Mono.just(1)
runBlocking {
assertThat(operations.acknowledgeAndAwait("foo", "bar", "0-0")).isEqualTo(1)
}
verify {
operations.acknowledge("foo", "bar", "0-0")
}
}
@Test // DATAREDIS-937
fun acknowledgeRecordId() {
val operations = mockk<ReactiveStreamOperations<String, String, String>>()
val recordId = RecordId.of("0-0")
every { operations.acknowledge("foo", "bar", recordId) } returns Mono.just(1)
runBlocking {
assertThat(operations.acknowledgeAndAwait("foo", "bar", recordId)).isEqualTo(1)
}
verify {
operations.acknowledge("foo", "bar", recordId)
}
}
@Test // DATAREDIS-937
fun acknowledgeRecord() {
val operations = mockk<ReactiveStreamOperations<String, String, String>>()
every { operations.acknowledge(any(), any<Record<String, String>>()) } returns Mono.just(1)
runBlocking {
assertThat(operations.acknowledgeAndAwait("foo", Record.of("bar"))).isEqualTo(1)
}
verify {
operations.acknowledge("foo", Record.of("bar"))
}
}
@Test // DATAREDIS-937
fun add() {
val operations = mockk<ReactiveStreamOperations<String, String, String>>()
val record = MapRecord.create("foo", mapOf("a" to "b"))
val redordId = RecordId.of("0-0")
every { operations.add(record) } returns Mono.just(redordId)
runBlocking {
assertThat(operations.addAndAwait(record)).isEqualTo(redordId)
}
verify {
operations.add(record)
}
}
@Test // DATAREDIS-937
fun addRecord() {
val operations = mockk<ReactiveStreamOperations<String, String, String>>()
val record = Record.of<String, String>("foo").withStreamKey("bar")
val recordId = RecordId.of("0-0")
every { operations.add(record) } returns Mono.just(recordId)
runBlocking {
assertThat(operations.addAndAwait(record)).isEqualTo(recordId)
}
verify {
operations.add(record)
}
}
@Test // DATAREDIS-937
fun delete() {
val operations = mockk<ReactiveStreamOperations<String, String, String>>()
val recordId = RecordId.of("0-0")
every { operations.delete("foo", recordId) } returns Mono.just(1)
runBlocking {
assertThat(operations.deleteAndAwait("foo", recordId)).isEqualTo(1)
}
verify {
operations.delete("foo", recordId)
}
}
@Test // DATAREDIS-937
fun deleteRecord() {
val operations = mockk<ReactiveStreamOperations<String, String, String>>()
val record = Record.of<String, String>("foo").withStreamKey("bar")
every { operations.delete(record) } returns Mono.just(1)
runBlocking {
assertThat(operations.deleteAndAwait(record)).isEqualTo(1)
}
verify {
operations.delete(record)
}
}
@Test // DATAREDIS-937
fun deleteRecordIds() {
val operations = mockk<ReactiveStreamOperations<String, String, String>>()
every { operations.delete("foo", "0-0") } returns Mono.just(1)
runBlocking {
assertThat(operations.deleteAndAwait("foo", "0-0")).isEqualTo(1)
}
verify {
operations.delete("foo", "0-0")
}
}
@Test // DATAREDIS-937
fun createGroup() {
val operations = mockk<ReactiveStreamOperations<String, String, String>>()
every { operations.createGroup(any(), any()) } returns Mono.just("OK")
runBlocking {
assertThat(operations.createGroupAndAwait("foo", "bar")).isEqualTo("OK")
}
verify {
operations.createGroup("foo", "bar")
}
}
@Test // DATAREDIS-937
fun createGroupWithOffset() {
val operations = mockk<ReactiveStreamOperations<String, String, String>>()
every { operations.createGroup(any(), ReadOffset.lastConsumed(), any()) } returns Mono.just("OK")
runBlocking {
assertThat(operations.createGroupAndAwait("foo", ReadOffset.lastConsumed(), "bar")).isEqualTo("OK")
}
verify {
operations.createGroup("foo", ReadOffset.lastConsumed(), "bar")
}
}
@Test // DATAREDIS-937
fun deleteConsumer() {
val operations = mockk<ReactiveStreamOperations<String, String, String>>()
every { operations.deleteConsumer(any(), any()) } returns Mono.just("OK")
runBlocking {
assertThat(operations.deleteConsumerAndAwait("foo", Consumer.from("bar", "baz"))).isEqualTo("OK")
}
verify {
operations.deleteConsumer("foo", Consumer.from("bar", "baz"))
}
}
@Test // DATAREDIS-937
fun destroyGroup() {
val operations = mockk<ReactiveStreamOperations<String, String, String>>()
every { operations.destroyGroup(any(), any()) } returns Mono.just("OK")
runBlocking {
assertThat(operations.destroyGroupAndAwait("foo", "bar")).isEqualTo("OK")
}
verify {
operations.destroyGroup("foo", "bar")
}
}
@Test // DATAREDIS-937
fun size() {
val operations = mockk<ReactiveStreamOperations<String, String, String>>()
every { operations.size(any()) } returns Mono.just(1)
runBlocking {
assertThat(operations.sizeAndAwait("foo")).isEqualTo(1)
}
verify {
operations.size("foo")
}
}
@Test // DATAREDIS-937
fun trim() {
val operations = mockk<ReactiveStreamOperations<String, String, String>>()
every { operations.trim(any(), any()) } returns Mono.just(1)
runBlocking {
assertThat(operations.trimAndAwait("foo", 1)).isEqualTo(1)
}
verify {
operations.trim("foo", 1)
}
}
}

View File

@@ -0,0 +1,395 @@
/*
* 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
*
* http://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.redis.core
import io.mockk.every
import io.mockk.mockk
import io.mockk.verify
import kotlinx.coroutines.runBlocking
import org.assertj.core.api.Assertions.assertThat
import org.junit.Test
import org.springframework.data.redis.connection.BitFieldSubCommands
import reactor.core.publisher.Mono
import java.time.Duration
/**
* Unit tests for [ReactiveValueOperationsExtensions].
*
* @author Mark Paluch
*/
class ReactiveValueOperationsExtensionsUnitTests {
@Test // DATAREDIS-937
fun set() {
val operations = mockk<ReactiveValueOperations<String, String>>()
every { operations.set(any(), any()) } returns Mono.just(true)
runBlocking {
assertThat(operations.setAndAwait("foo", "bar")).isTrue()
}
verify {
operations.set("foo", "bar")
}
}
@Test // DATAREDIS-937
fun setWithDuration() {
val operations = mockk<ReactiveValueOperations<String, String>>()
every { operations.set(any(), any(), any<Duration>()) } returns Mono.just(true)
runBlocking {
assertThat(operations.setAndAwait("foo", "bar", Duration.ofDays(1))).isTrue()
}
verify {
operations.set("foo", "bar", Duration.ofDays(1))
}
}
@Test // DATAREDIS-937
fun setIfAbsent() {
val operations = mockk<ReactiveValueOperations<String, String>>()
every { operations.setIfAbsent(any(), any()) } returns Mono.just(true)
runBlocking {
assertThat(operations.setIfAbsentAndAwait("foo", "bar")).isTrue()
}
verify {
operations.setIfAbsent("foo", "bar")
}
}
@Test // DATAREDIS-937
fun setIfAbsentWithDuration() {
val operations = mockk<ReactiveValueOperations<String, String>>()
every { operations.setIfAbsent(any(), any(), any<Duration>()) } returns Mono.just(true)
runBlocking {
assertThat(operations.setIfAbsentAndAwait("foo", "bar", Duration.ofDays(1))).isTrue()
}
verify {
operations.setIfAbsent("foo", "bar", Duration.ofDays(1))
}
}
@Test // DATAREDIS-937
fun setIfPresent() {
val operations = mockk<ReactiveValueOperations<String, String>>()
every { operations.setIfPresent(any(), any()) } returns Mono.just(true)
runBlocking {
assertThat(operations.setIfPresentAndAwait("foo", "bar")).isTrue()
}
verify {
operations.setIfPresent("foo", "bar")
}
}
@Test // DATAREDIS-937
fun setIfPresentWithDuration() {
val operations = mockk<ReactiveValueOperations<String, String>>()
every { operations.setIfPresent(any(), any(), any<Duration>()) } returns Mono.just(true)
runBlocking {
assertThat(operations.setIfPresentAndAwait("foo", "bar", Duration.ofDays(1))).isTrue()
}
verify {
operations.setIfPresent("foo", "bar", Duration.ofDays(1))
}
}
@Test // DATAREDIS-937
fun multiSet() {
val operations = mockk<ReactiveValueOperations<String, String>>()
every { operations.multiSet(any()) } returns Mono.just(true)
runBlocking {
assertThat(operations.multiSetAndAwait(mapOf("foo" to "bar"))).isTrue()
}
verify {
operations.multiSet(mapOf("foo" to "bar"))
}
}
@Test // DATAREDIS-937
fun multiSetIfAbsent() {
val operations = mockk<ReactiveValueOperations<String, String>>()
every { operations.multiSetIfAbsent(any()) } returns Mono.just(true)
runBlocking {
assertThat(operations.multiSetIfAbsentAndAwait(mapOf("foo" to "bar"))).isTrue()
}
verify {
operations.multiSetIfAbsent(mapOf("foo" to "bar"))
}
}
@Test // DATAREDIS-937
fun get() {
val operations = mockk<ReactiveValueOperations<String, String>>()
every { operations.get(any()) } returns Mono.just("bar")
runBlocking {
assertThat(operations.getAndAwait("foo")).isEqualTo("bar")
}
verify {
operations.get("foo")
}
}
@Test // DATAREDIS-937
fun getAndSet() {
val operations = mockk<ReactiveValueOperations<String, String>>()
every { operations.getAndSet(any(), any()) } returns Mono.just("baz")
runBlocking {
assertThat(operations.getAndSetAndAwait("foo", "bar")).isEqualTo("baz")
}
verify {
operations.getAndSet("foo", "bar")
}
}
@Test // DATAREDIS-937
fun multiGet() {
val operations = mockk<ReactiveValueOperations<String, String>>()
every { operations.multiGet(any()) } returns Mono.just(listOf("baz"))
runBlocking {
assertThat(operations.multiGetAndAwait("foo", "bar")).isEqualTo(listOf("baz"))
}
verify {
operations.multiGet(listOf("foo", "bar"))
}
}
@Test // DATAREDIS-937
fun increment() {
val operations = mockk<ReactiveValueOperations<String, String>>()
every { operations.increment(any()) } returns Mono.just(1)
runBlocking {
assertThat(operations.incrementAndAwait("foo")).isEqualTo(1)
}
verify {
operations.increment("foo")
}
}
@Test // DATAREDIS-937
fun incrementWithDelta() {
val operations = mockk<ReactiveValueOperations<String, String>>()
every { operations.increment(any(), 2) } returns Mono.just(1)
runBlocking {
assertThat(operations.incrementAndAwait("foo", 2)).isEqualTo(1)
}
verify {
operations.increment("foo", 2)
}
}
@Test // DATAREDIS-937
fun incrementWithDoubleDelta() {
val operations = mockk<ReactiveValueOperations<String, String>>()
every { operations.increment(any(), 2.0) } returns Mono.just(1.0)
runBlocking {
assertThat(operations.incrementAndAwait("foo", 2.0)).isEqualTo(1.0)
}
verify {
operations.increment("foo", 2.0)
}
}
@Test // DATAREDIS-937
fun decrement() {
val operations = mockk<ReactiveValueOperations<String, String>>()
every { operations.decrement(any()) } returns Mono.just(1)
runBlocking {
assertThat(operations.decrementAndAwait("foo")).isEqualTo(1)
}
verify {
operations.decrement("foo")
}
}
@Test // DATAREDIS-937
fun decrementWithDelta() {
val operations = mockk<ReactiveValueOperations<String, String>>()
every { operations.decrement(any(), 2) } returns Mono.just(1)
runBlocking {
assertThat(operations.decrementAndAwait("foo", 2)).isEqualTo(1)
}
verify {
operations.decrement("foo", 2)
}
}
@Test // DATAREDIS-937
fun append() {
val operations = mockk<ReactiveValueOperations<String, String>>()
every { operations.append(any(), any()) } returns Mono.just(2)
runBlocking {
assertThat(operations.appendAndAwait("foo", "bar")).isEqualTo(2)
}
verify {
operations.append("foo", "bar")
}
}
@Test // DATAREDIS-937
fun getSubstring() {
val operations = mockk<ReactiveValueOperations<String, String>>()
every { operations.get(any(), any(), any()) } returns Mono.just("foo")
runBlocking {
assertThat(operations.getAndAwait("foo", 1, 2)).isEqualTo("foo")
}
verify {
operations.get("foo", 1, 2)
}
}
@Test // DATAREDIS-937
fun setSubstring() {
val operations = mockk<ReactiveValueOperations<String, String>>()
every { operations.set(any(), any(), any<Long>()) } returns Mono.just(1)
runBlocking {
assertThat(operations.setAndAwait("foo", "bar", 2)).isEqualTo(1)
}
verify {
operations.set("foo", "bar", 2)
}
}
@Test // DATAREDIS-937
fun size() {
val operations = mockk<ReactiveValueOperations<String, String>>()
every { operations.size(any()) } returns Mono.just(1)
runBlocking {
assertThat(operations.sizeAndAwait("foo")).isEqualTo(1)
}
verify {
operations.size("foo")
}
}
@Test // DATAREDIS-937
fun setBit() {
val operations = mockk<ReactiveValueOperations<String, String>>()
every { operations.setBit(any(), any(), any()) } returns Mono.just(true)
runBlocking {
assertThat(operations.setBitAndAwait("foo", 1, true)).isTrue()
}
verify {
operations.setBit("foo", 1, true)
}
}
@Test // DATAREDIS-937
fun getBit() {
val operations = mockk<ReactiveValueOperations<String, String>>()
every { operations.getBit(any(), any()) } returns Mono.just(true)
runBlocking {
assertThat(operations.getBitAndAwait("foo", 1)).isTrue()
}
verify {
operations.getBit("foo", 1)
}
}
@Test // DATAREDIS-937
fun bitField() {
val operations = mockk<ReactiveValueOperations<String, String>>()
val commands = BitFieldSubCommands.create();
every { operations.bitField(any(), any()) } returns Mono.just(listOf(1L))
runBlocking {
assertThat(operations.bitFieldAndAwait("foo", commands)).isEqualTo(listOf(1L))
}
verify {
operations.bitField("foo", commands)
}
}
@Test // DATAREDIS-937
fun delete() {
val operations = mockk<ReactiveValueOperations<String, String>>()
every { operations.delete(any()) } returns Mono.just(true)
runBlocking {
assertThat(operations.deleteAndAwait("foo")).isTrue()
}
verify {
operations.delete("foo")
}
}
}

View File

@@ -0,0 +1,305 @@
/*
* 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
*
* http://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.redis.core
import io.mockk.every
import io.mockk.mockk
import io.mockk.verify
import kotlinx.coroutines.runBlocking
import org.assertj.core.api.Assertions.assertThat
import org.junit.Test
import org.springframework.data.domain.Range
import org.springframework.data.redis.connection.RedisZSetCommands.Aggregate
import org.springframework.data.redis.connection.RedisZSetCommands.Weights
import reactor.core.publisher.Mono
/**
* Unit tests for [ReactiveZSetOperationsExtensions].
*
* @author Mark Paluch
*/
class ReactiveZSetOperationsExtensionsUnitTests {
@Test // DATAREDIS-937
fun add() {
val operations = mockk<ReactiveZSetOperations<String, String>>()
every { operations.add(any(), any(), 1.0) } returns Mono.just(true)
runBlocking {
assertThat(operations.addAndAwait("foo", "bar", 1.0)).isTrue()
}
verify {
operations.add("foo", "bar", 1.0)
}
}
@Test // DATAREDIS-937
fun addAll() {
val operations = mockk<ReactiveZSetOperations<String, String>>()
every { operations.addAll(any(), any()) } returns Mono.just(1)
runBlocking {
assertThat(operations.addAllAndAwait("foo", listOf(DefaultTypedTuple("v", 1.0)))).isEqualTo(1)
}
verify {
operations.addAll("foo", listOf(DefaultTypedTuple("v", 1.0)))
}
}
@Test // DATAREDIS-937
fun remove() {
val operations = mockk<ReactiveZSetOperations<String, String>>()
every { operations.remove("foo", "bar") } returns Mono.just(1)
runBlocking {
assertThat(operations.removeAndAwait("foo", "bar")).isEqualTo(1)
}
verify {
operations.remove("foo", "bar")
}
}
@Test // DATAREDIS-937
fun incrementScore() {
val operations = mockk<ReactiveZSetOperations<String, String>>()
every { operations.incrementScore(any(), any(), 1.0) } returns Mono.just(1.0)
runBlocking {
assertThat(operations.incrementScoreAndAwait("foo", "bar", 1.0)).isEqualTo(1.0)
}
verify {
operations.incrementScore("foo", "bar", 1.0)
}
}
@Test // DATAREDIS-937
fun rank() {
val operations = mockk<ReactiveZSetOperations<String, String>>()
every { operations.rank(any(), any()) } returns Mono.just(1)
runBlocking {
assertThat(operations.rankAndAwait("foo", "bar")).isEqualTo(1)
}
verify {
operations.rank("foo", "bar")
}
}
@Test // DATAREDIS-937
fun reverseRank() {
val operations = mockk<ReactiveZSetOperations<String, String>>()
every { operations.reverseRank(any(), any()) } returns Mono.just(1)
runBlocking {
assertThat(operations.reverseRankAndAwait("foo", "bar")).isEqualTo(1)
}
verify {
operations.reverseRank("foo", "bar")
}
}
@Test // DATAREDIS-937
fun count() {
val operations = mockk<ReactiveZSetOperations<String, String>>()
every { operations.count(any(), any()) } returns Mono.just(1)
runBlocking {
assertThat(operations.countAndAwait("foo", Range.unbounded())).isEqualTo(1)
}
verify {
operations.count("foo", Range.unbounded())
}
}
@Test // DATAREDIS-937
fun score() {
val operations = mockk<ReactiveZSetOperations<String, String>>()
every { operations.score(any(), any()) } returns Mono.just(1.0)
runBlocking {
assertThat(operations.scoreAndAwait("foo", "bar")).isEqualTo(1.0)
}
verify {
operations.score("foo", "bar")
}
}
@Test // DATAREDIS-937
fun removeRange() {
val operations = mockk<ReactiveZSetOperations<String, String>>()
every { operations.removeRange(any(), any()) } returns Mono.just(1)
runBlocking {
assertThat(operations.removeRangeAndAwait("foo", Range.unbounded())).isEqualTo(1)
}
verify {
operations.removeRange("foo", Range.unbounded())
}
}
@Test // DATAREDIS-937
fun removeRangeByScore() {
val operations = mockk<ReactiveZSetOperations<String, String>>()
every { operations.removeRangeByScore(any(), any()) } returns Mono.just(1)
runBlocking {
assertThat(operations.removeRangeByScoreAndAwait("foo", Range.unbounded())).isEqualTo(1)
}
verify {
operations.removeRangeByScore("foo", Range.unbounded())
}
}
@Test // DATAREDIS-937
fun unionAndStore() {
val operations = mockk<ReactiveZSetOperations<String, String>>()
every { operations.unionAndStore("foo", "bar", "baz") } returns Mono.just(1)
runBlocking {
assertThat(operations.unionAndStoreAndAwait("foo", "bar", "baz")).isEqualTo(1)
}
verify {
operations.unionAndStore("foo", "bar", "baz")
}
}
@Test // DATAREDIS-937
fun unionAndStoreListOfKeys() {
val operations = mockk<ReactiveZSetOperations<String, String>>()
every { operations.unionAndStore("foo", listOf("bar"), "baz") } returns Mono.just(1)
runBlocking {
assertThat(operations.unionAndStoreAndAwait("foo", listOf("bar"), "baz")).isEqualTo(1)
}
verify {
operations.unionAndStore("foo", listOf("bar"), "baz")
}
}
@Test // DATAREDIS-937
fun unionAndStoreAggregate() {
val operations = mockk<ReactiveZSetOperations<String, String>>()
every { operations.unionAndStore(any(), any(), any(), any()) } returns Mono.just(1)
runBlocking {
assertThat(operations.unionAndStoreAndAwait("foo", listOf("bar"), "baz", Aggregate.MAX)).isEqualTo(1)
}
verify {
operations.unionAndStore("foo", listOf("bar"), "baz", Aggregate.MAX)
}
}
@Test // DATAREDIS-937
fun unionAndStoreWeights() {
val operations = mockk<ReactiveZSetOperations<String, String>>()
every { operations.unionAndStore(any(), any(), any(), any(), any()) } returns Mono.just(1)
runBlocking {
assertThat(operations.unionAndStoreAndAwait("foo", listOf("bar"), "baz", Aggregate.MAX, Weights.fromSetCount(1))).isEqualTo(1)
}
verify {
operations.unionAndStore("foo", listOf("bar"), "baz", Aggregate.MAX, Weights.fromSetCount(1))
}
}
@Test // DATAREDIS-937
fun intersectAndStore() {
val operations = mockk<ReactiveZSetOperations<String, String>>()
every { operations.intersectAndStore("foo", "bar", "baz") } returns Mono.just(1)
runBlocking {
assertThat(operations.intersectAndStoreAndAwait("foo", "bar", "baz")).isEqualTo(1)
}
verify {
operations.intersectAndStore("foo", "bar", "baz")
}
}
@Test // DATAREDIS-937
fun intersectAndStoreListOfKeys() {
val operations = mockk<ReactiveZSetOperations<String, String>>()
every { operations.intersectAndStore("foo", listOf("bar"), "baz") } returns Mono.just(1)
runBlocking {
assertThat(operations.intersectAndStoreAndAwait("foo", listOf("bar"), "baz")).isEqualTo(1)
}
verify {
operations.intersectAndStore("foo", listOf("bar"), "baz")
}
}
@Test // DATAREDIS-937
fun intersectAndStoreAggregate() {
val operations = mockk<ReactiveZSetOperations<String, String>>()
every { operations.intersectAndStore(any(), any(), any(), any()) } returns Mono.just(1)
runBlocking {
assertThat(operations.intersectAndStoreAndAwait("foo", listOf("bar"), "baz", Aggregate.MAX)).isEqualTo(1)
}
verify {
operations.intersectAndStore("foo", listOf("bar"), "baz", Aggregate.MAX)
}
}
@Test // DATAREDIS-937
fun intersectAndStoreWeights() {
val operations = mockk<ReactiveZSetOperations<String, String>>()
every { operations.intersectAndStore(any(), any(), any(), any(), any()) } returns Mono.just(1)
runBlocking {
assertThat(operations.intersectAndStoreAndAwait("foo", listOf("bar"), "baz", Aggregate.MAX, Weights.fromSetCount(1))).isEqualTo(1)
}
verify {
operations.intersectAndStore("foo", listOf("bar"), "baz", Aggregate.MAX, Weights.fromSetCount(1))
}
}
}