committed by
Mahmoud Ben Hassine
parent
70e8a1c497
commit
1bd0e0c874
1
pom.xml
1
pom.xml
@@ -70,6 +70,7 @@
|
||||
<spring-data-commons.version>3.2.0-SNAPSHOT</spring-data-commons.version>
|
||||
<spring-data-jpa.version>3.2.0-SNAPSHOT</spring-data-jpa.version>
|
||||
<spring-data-mongodb.version>4.2.0-SNAPSHOT</spring-data-mongodb.version>
|
||||
<spring-data-redis.version>2.5.2-SNAPSHOT</spring-data-redis.version>
|
||||
<spring-kafka.version>3.0.11-SNAPSHOT</spring-kafka.version>
|
||||
<spring-amqp.version>3.0.9-SNAPSHOT</spring-amqp.version>
|
||||
<spring-ldap.version>3.2.0-SNAPSHOT</spring-ldap.version>
|
||||
|
||||
@@ -185,6 +185,12 @@
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.data</groupId>
|
||||
<artifactId>spring-data-redis</artifactId>
|
||||
<version>${spring-data-redis.version}</version>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.mongodb</groupId>
|
||||
<artifactId>mongodb-driver-sync</artifactId>
|
||||
|
||||
@@ -0,0 +1,51 @@
|
||||
/*
|
||||
* Copyright 2019-2021 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.batch.item.database;
|
||||
|
||||
import org.springframework.batch.item.ItemWriter;
|
||||
import org.springframework.batch.item.KeyValueItemWriter;
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* An {@link ItemWriter} implementation for Redis using a
|
||||
* {@link RedisTemplate} .
|
||||
* </p>
|
||||
*
|
||||
* @author Santiago Molano
|
||||
* @since 4.2
|
||||
*/
|
||||
public class RedisItemWriter<K, T> extends KeyValueItemWriter<K, T> {
|
||||
private RedisTemplate<K, T> redisTemplate;
|
||||
|
||||
|
||||
@Override
|
||||
protected void writeKeyValue(K key, T value) {
|
||||
|
||||
redisTemplate.opsForValue().set(key, value);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void init() {
|
||||
Assert.notNull(redisTemplate, "RedisTemplate must not be null");
|
||||
}
|
||||
|
||||
public void setRedisTemplate(RedisTemplate<K, T> redisTemplate) {
|
||||
this.redisTemplate = redisTemplate;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
/*
|
||||
* Copyright 2019-2021 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.batch.item.database;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
import org.springframework.core.convert.converter.Converter;
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
import org.springframework.data.redis.core.ValueOperations;
|
||||
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
public class RedisItemWriterTests {
|
||||
@Mock
|
||||
private RedisTemplate<String, String> redisTemplate;
|
||||
@Mock
|
||||
private ValueOperations<String, String> valueOperations;
|
||||
private RedisItemWriter<String, String> redisItemWriter = new RedisItemWriter<>();
|
||||
private RedisItemKeyMapper redisItemKeyMapper;
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
MockitoAnnotations.openMocks(this);
|
||||
redisItemWriter.setRedisTemplate(this.redisTemplate);
|
||||
when(redisTemplate.opsForValue()).thenReturn(valueOperations);
|
||||
doNothing().when(valueOperations).set(any(), any());
|
||||
this.redisItemKeyMapper = new RedisItemKeyMapper();
|
||||
this.redisItemWriter.setItemKeyMapper(redisItemKeyMapper);
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldWriteToRedisDatabaseUsingKeyValue() {
|
||||
this.redisItemWriter.writeKeyValue("oneKey", "oneValue");
|
||||
verify(this.redisTemplate.opsForValue()).set("oneKey", "oneValue");
|
||||
}
|
||||
@Test
|
||||
public void shouldWriteAllItemsToRedis() throws Exception {
|
||||
List<String> items = Arrays.asList("val1", "val2");
|
||||
this.redisItemWriter.write(items);
|
||||
verify(this.redisTemplate.opsForValue()).set(items.get(0), items.get(0));
|
||||
verify(this.redisTemplate.opsForValue()).set(items.get(1), items.get(1));
|
||||
}
|
||||
static class RedisItemKeyMapper implements Converter<String, String> {
|
||||
|
||||
@Override
|
||||
public String convert(String source) {
|
||||
return source;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user