From f1be9b736db3b29abcb7dbe1932cd52c7807d6ae Mon Sep 17 00:00:00 2001 From: Christoph Strobl Date: Thu, 26 Apr 2018 13:45:21 +0200 Subject: [PATCH] DATAREDIS-786 - Polishing. Update Javadoc and add setIfPresent to BoundValueOperations. Original Pull Request: #334 --- .../data/redis/core/BoundValueOperations.java | 28 +++++++- .../core/DefaultBoundValueOperations.java | 21 ++++++ .../data/redis/core/ValueOperations.java | 27 +++++--- .../DefaultBoundValueOperationsUnitTests.java | 67 +++++++++++++++++++ .../core/DefaultValueOperationsTests.java | 10 ++- 5 files changed, 138 insertions(+), 15 deletions(-) create mode 100644 src/test/java/org/springframework/data/redis/core/DefaultBoundValueOperationsUnitTests.java diff --git a/src/main/java/org/springframework/data/redis/core/BoundValueOperations.java b/src/main/java/org/springframework/data/redis/core/BoundValueOperations.java index 207f2bf6f..45cc070e7 100644 --- a/src/main/java/org/springframework/data/redis/core/BoundValueOperations.java +++ b/src/main/java/org/springframework/data/redis/core/BoundValueOperations.java @@ -25,6 +25,7 @@ import org.springframework.lang.Nullable; * @author Costin Leau * @author Mark Paluch * @author Jiahe Cai + * @author Christoph Strobl */ public interface BoundValueOperations extends BoundKeyOperations { @@ -69,6 +70,32 @@ public interface BoundValueOperations extends BoundKeyOperations { @Nullable Boolean setIfAbsent(V value, long timeout, TimeUnit unit); + /** + * Set the bound key to hold the string {@code value} if {@code key} is present. + * + * @param value must not be {@literal null}. + * @return command result indicating if the key has been set. + * @throws IllegalArgumentException if {@code value} is not present. + * @see Redis Documentation: SET + * @since 2.1 + */ + @Nullable + Boolean setIfPresent(V value); + + /** + * Set the bound key to hold the string {@code value} and expiration {@code timeout} if {@code key} is present. + * + * @param value must not be {@literal null}. + * @param timeout the key expiration timeout. + * @param unit must not be {@literal null}. + * @return command result indicating if the key has been set. + * @throws IllegalArgumentException if either {@code value} or {@code timeout} is not present. + * @see Redis Documentation: SET + * @since 2.1 + */ + @Nullable + Boolean setIfPresent(V value, long timeout, TimeUnit unit); + /** * Get the value of the bound key. * @@ -90,7 +117,6 @@ public interface BoundValueOperations extends BoundKeyOperations { /** * Increment an integer value stored as string value under the bound key by one. * - * @param delta * @return {@literal null} when used in pipeline / transaction. * @since 2.1 * @see Redis Documentation: INCR diff --git a/src/main/java/org/springframework/data/redis/core/DefaultBoundValueOperations.java b/src/main/java/org/springframework/data/redis/core/DefaultBoundValueOperations.java index 31af095d3..dd58280c5 100644 --- a/src/main/java/org/springframework/data/redis/core/DefaultBoundValueOperations.java +++ b/src/main/java/org/springframework/data/redis/core/DefaultBoundValueOperations.java @@ -18,6 +18,7 @@ package org.springframework.data.redis.core; import java.util.concurrent.TimeUnit; import org.springframework.data.redis.connection.DataType; +import org.springframework.lang.Nullable; /** * @author Costin Leau @@ -157,6 +158,26 @@ class DefaultBoundValueOperations extends DefaultBoundKeyOperations imp return ops.setIfAbsent(getKey(), value, timeout, unit); } + /* + * (non-Javadoc) + * @see org.springframework.data.redis.core.BoundValueOperations#setIfPresent(java.lang.Object) + */ + @Nullable + @Override + public Boolean setIfPresent(V value) { + return ops.setIfPresent(getKey(), value); + } + + /* + * (non-Javadoc) + * @see org.springframework.data.redis.core.BoundValueOperations#setIfPresent(java.lang.Object, long, java.util.concurrent.TimeUnit) + */ + @Nullable + @Override + public Boolean setIfPresent(V value, long timeout, TimeUnit unit) { + return ops.setIfPresent(getKey(), value, timeout, unit); + } + /* * (non-Javadoc) * @see org.springframework.data.redis.core.BoundValueOperations#set(java.lang.Object, long) diff --git a/src/main/java/org/springframework/data/redis/core/ValueOperations.java b/src/main/java/org/springframework/data/redis/core/ValueOperations.java index 200e6b5f5..24f049dba 100644 --- a/src/main/java/org/springframework/data/redis/core/ValueOperations.java +++ b/src/main/java/org/springframework/data/redis/core/ValueOperations.java @@ -36,7 +36,7 @@ public interface ValueOperations { * Set {@code value} for {@code key}. * * @param key must not be {@literal null}. - * @param value + * @param value must not be {@literal null}. * @see Redis Documentation: SET */ void set(K key, V value); @@ -45,8 +45,8 @@ public interface ValueOperations { * Set the {@code value} and expiration {@code timeout} for {@code key}. * * @param key must not be {@literal null}. - * @param value - * @param timeout + * @param value must not be {@literal null}. + * @param timeout the key expiration timeout. * @param unit must not be {@literal null}. * @see Redis Documentation: SETEX */ @@ -56,7 +56,7 @@ public interface ValueOperations { * Set {@code key} to hold the string {@code value} if {@code key} is absent. * * @param key must not be {@literal null}. - * @param value + * @param value must not be {@literal null}. * @return {@literal null} when used in pipeline / transaction. * @see Redis Documentation: SETNX */ @@ -67,8 +67,8 @@ public interface ValueOperations { * Set {@code key} to hold the string {@code value} and expiration {@code timeout} if {@code key} is absent. * * @param key must not be {@literal null}. - * @param value - * @param timeout must not be {@literal null}. + * @param value must not be {@literal null}. + * @param timeout the key expiration timeout. * @param unit must not be {@literal null}. * @return {@literal null} when used in pipeline / transaction. * @since 2.1 @@ -81,9 +81,11 @@ public interface ValueOperations { * Set {@code key} to hold the string {@code value} if {@code key} is present. * * @param key must not be {@literal null}. - * @param value - * @since 2.1 + * @param value must not be {@literal null}. + * @return command result indicating if the key has been set. + * @throws IllegalArgumentException if either {@code key} or {@code value} is not present. * @see Redis Documentation: SET + * @since 2.1 */ @Nullable Boolean setIfPresent(K key, V value); @@ -92,10 +94,13 @@ public interface ValueOperations { * Set {@code key} to hold the string {@code value} and expiration {@code timeout} if {@code key} is present. * * @param key must not be {@literal null}. - * @param value - * @param timeout must not be {@literal null}. - * @since 2.1 + * @param value must not be {@literal null}. + * @param timeout the key expiration timeout. + * @param unit must not be {@literal null}. + * @return command result indicating if the key has been set. + * @throws IllegalArgumentException if either {@code key}, {@code value} or {@code timeout} is not present. * @see Redis Documentation: SET + * @since 2.1 */ @Nullable Boolean setIfPresent(K key, V value, long timeout, TimeUnit unit); diff --git a/src/test/java/org/springframework/data/redis/core/DefaultBoundValueOperationsUnitTests.java b/src/test/java/org/springframework/data/redis/core/DefaultBoundValueOperationsUnitTests.java new file mode 100644 index 000000000..e0eb0fc7d --- /dev/null +++ b/src/test/java/org/springframework/data/redis/core/DefaultBoundValueOperationsUnitTests.java @@ -0,0 +1,67 @@ +/* + * Copyright 2018 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 static org.mockito.Mockito.*; + +import java.util.concurrent.TimeUnit; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.junit.MockitoJUnitRunner; + +/** + * Unit tests for {@link DefaultBoundValueOperations} + * + * @author Christoph Strobl + */ +@RunWith(MockitoJUnitRunner.class) +public class DefaultBoundValueOperationsUnitTests { + + DefaultBoundValueOperations boundValueOps; + + @Mock ValueOperations valueOps; + + static final String KEY = "key-1"; + static final Object VALUE = "value"; + + @Before + public void setUp() { + + RedisOperations redisOps = mock(RedisOperations.class); + when(redisOps.opsForValue()).thenReturn(valueOps); + + boundValueOps = new DefaultBoundValueOperations<>(KEY, redisOps); + } + + @Test // DATAREDIS-786 + public void setIfPresentShouldDelegateCorrectly() { + + boundValueOps.setIfPresent(VALUE); + + verify(valueOps).setIfPresent(eq(KEY), eq(VALUE)); + } + + @Test // DATAREDIS-786 + public void setIfPresentWithTimeoutShouldDelegateCorrectly() { + + boundValueOps.setIfPresent(VALUE, 10, TimeUnit.SECONDS); + + verify(valueOps).setIfPresent(eq(KEY), eq(VALUE), eq(10L), eq(TimeUnit.SECONDS)); + } +} diff --git a/src/test/java/org/springframework/data/redis/core/DefaultValueOperationsTests.java b/src/test/java/org/springframework/data/redis/core/DefaultValueOperationsTests.java index 53f000692..fef2f75b6 100644 --- a/src/test/java/org/springframework/data/redis/core/DefaultValueOperationsTests.java +++ b/src/test/java/org/springframework/data/redis/core/DefaultValueOperationsTests.java @@ -345,13 +345,12 @@ public class DefaultValueOperationsTests { } @Test // DATAREDIS-786 - public void testSetIfPresent() { + public void setIfPresentReturnsTrueWhenKeyExists() { K key = keyFactory.instance(); V value1 = valueFactory.instance(); V value2 = valueFactory.instance(); - assertFalse(valueOps.setIfPresent(key, value1)); valueOps.set(key, value1); assertTrue(valueOps.setIfPresent(key, value2)); @@ -359,7 +358,12 @@ public class DefaultValueOperationsTests { } @Test // DATAREDIS-786 - public void testSetIfPresentWithExpiration() { + public void setIfPresentReturnsFalseWhenKeyDoesNotExist() { + assertFalse(valueOps.setIfPresent(keyFactory.instance(), valueFactory.instance())); + } + + @Test // DATAREDIS-786 + public void setIfPresentShouldSetExpirationCorrectly() { K key = keyFactory.instance(); V value1 = valueFactory.instance();