INT-3085 Add a Redis-backed MetadataStore

* Add tests
* Add documentation

INT-3085 Code review changes

INT-3085 Add Twitter Integration Test

JIRA: https://jira.springsource.org/browse/INT-3085
This commit is contained in:
Gunnar Hillert
2013-10-04 11:00:57 +03:00
committed by Artem Bilan
parent bd2cde4202
commit 1fa9b92c0b
10 changed files with 533 additions and 36 deletions

View File

@@ -0,0 +1,68 @@
/*
* Copyright 2013 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.integration.redis.store.metadata;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.BoundValueOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.integration.store.metadata.MetadataStore;
import org.springframework.util.Assert;
/**
* Redis implementation of {@link MetadataStore}. Use this {@link MetadataStore}
* to achieve meta-data persistence across application restarts.
*
* @author Gunnar Hillert
* @since 3.0
*/
public class RedisMetadataStore implements MetadataStore {
private final StringRedisTemplate redisTemplate;
/**
* Initializes the {@link RedisTemplate}.
* A {@link StringRedisTemplate} is used with default properties.
*
* @param connectionFactory Must not be null
*/
public RedisMetadataStore(RedisConnectionFactory connectionFactory) {
Assert.notNull(connectionFactory, "'connectionFactory' must not be null.");
this.redisTemplate = new StringRedisTemplate(connectionFactory);
}
/**
* Persists the provided key and value to Redis.
*
* @param key Must not be null
* @param value Must not be null
*/
public void put(String key, String value) {
Assert.notNull(key, "'key' must not be null.");
Assert.notNull(value, "'value' must not be null.");
BoundValueOperations<String, String> ops = this.redisTemplate.boundValueOps(key);
ops.set(value);
}
/**
* Retrieve the persisted value for the provided key.
*
* @param key Must not be null
*/
public String get(String key) {
Assert.notNull(key, "'key' must not be null.");
BoundValueOperations<String, String> ops = this.redisTemplate.boundValueOps(key);
return ops.get();
}
}

View File

@@ -0,0 +1,5 @@
/**
* Provides support for Redis-based
* {@link org.springframework.integration.store.metadata.MetadataStore}s.
*/
package org.springframework.integration.redis.store.metadata;

View File

@@ -0,0 +1,145 @@
/*
* Copyright 2013 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.integration.redis.store.metadata;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.fail;
import org.junit.Test;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.BoundValueOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.integration.redis.rules.RedisAvailable;
import org.springframework.integration.redis.rules.RedisAvailableTests;
/**
* @author Gunnar Hillert
* @since 3.0
*
*/
public class RedisMetadataStoreTests extends RedisAvailableTests {
@Test
@RedisAvailable
public void testGetNonExistingKeyValue(){
JedisConnectionFactory jcf = this.getConnectionFactoryForTest();
RedisMetadataStore metadataStore = new RedisMetadataStore(jcf);
String retrievedValue = metadataStore.get("does-not-exist");
assertNull(retrievedValue);
}
@Test
@RedisAvailable
public void testPersistKeyValue(){
JedisConnectionFactory jcf = this.getConnectionFactoryForTest();
RedisMetadataStore metadataStore = new RedisMetadataStore(jcf);
metadataStore.put("RedisMetadataStoreTests-Spring", "Integration");
StringRedisTemplate redisTemplate = new StringRedisTemplate(jcf);
BoundValueOperations<String, String> ops = redisTemplate.boundValueOps("RedisMetadataStoreTests-Spring");
assertEquals("Integration", ops.get());
}
@Test
@RedisAvailable
public void testGetValueFromMetadataStore(){
JedisConnectionFactory jcf = this.getConnectionFactoryForTest();
RedisMetadataStore metadataStore = new RedisMetadataStore(jcf);
metadataStore.put("RedisMetadataStoreTests-GetValue", "Hello Redis");
String retrievedValue = metadataStore.get("RedisMetadataStoreTests-GetValue");
assertEquals("Hello Redis", retrievedValue);
}
@Test
@RedisAvailable
public void testPersistEmptyStringToMetadataStore(){
JedisConnectionFactory jcf = this.getConnectionFactoryForTest();
RedisMetadataStore metadataStore = new RedisMetadataStore(jcf);
metadataStore.put("RedisMetadataStoreTests-PersistEmpty", "");
String retrievedValue = metadataStore.get("RedisMetadataStoreTests-PersistEmpty");
assertEquals("", retrievedValue);
}
@Test
@RedisAvailable
public void testPersistNullStringToMetadataStore(){
JedisConnectionFactory jcf = this.getConnectionFactoryForTest();
RedisMetadataStore metadataStore = new RedisMetadataStore(jcf);
try {
metadataStore.put("RedisMetadataStoreTests-PersistEmpty", null);
}
catch (IllegalArgumentException e) {
assertEquals("'value' must not be null.", e.getMessage());
return;
}
fail("Expected an IllegalArgumentException to be thrown.");
}
@Test
@RedisAvailable
public void testPersistWithEmptyKeyToMetadataStore(){
JedisConnectionFactory jcf = this.getConnectionFactoryForTest();
RedisMetadataStore metadataStore = new RedisMetadataStore(jcf);
metadataStore.put("", "PersistWithEmptyKey");
String retrievedValue = metadataStore.get("");
assertEquals("PersistWithEmptyKey", retrievedValue);
}
@Test
@RedisAvailable
public void testPersistWithNullKeyToMetadataStore(){
JedisConnectionFactory jcf = this.getConnectionFactoryForTest();
RedisMetadataStore metadataStore = new RedisMetadataStore(jcf);
try {
metadataStore.put(null, "something");
}
catch (IllegalArgumentException e) {
assertEquals("'key' must not be null.", e.getMessage());
return;
}
fail("Expected an IllegalArgumentException to be thrown.");
}
@Test
@RedisAvailable
public void testGetValueWithNullKeyFromMetadataStore(){
JedisConnectionFactory jcf = this.getConnectionFactoryForTest();
RedisMetadataStore metadataStore = new RedisMetadataStore(jcf);
try {
metadataStore.get(null);
}
catch (IllegalArgumentException e) {
assertEquals("'key' must not be null.", e.getMessage());
return;
}
fail("Expected an IllegalArgumentException to be thrown.");
}
}