From bc7240ead1a907e1218ceed9b8be343abe18ddb3 Mon Sep 17 00:00:00 2001 From: sameira Date: Fri, 22 Jan 2016 08:44:58 +0530 Subject: [PATCH] Adding Java config --- .../spring/data/redis/config/RedisConfig.java | 21 ++++++++ .../spring/data/redis/model/Student.java | 2 +- .../redis/repo/StudentRepositoryImpl.java | 6 ++- .../redis/repo/StudentRepositoryTest.java | 48 +++++++++++-------- 4 files changed, 54 insertions(+), 23 deletions(-) create mode 100644 spring-data-redis/src/main/java/org/baeldung/spring/data/redis/config/RedisConfig.java diff --git a/spring-data-redis/src/main/java/org/baeldung/spring/data/redis/config/RedisConfig.java b/spring-data-redis/src/main/java/org/baeldung/spring/data/redis/config/RedisConfig.java new file mode 100644 index 0000000000..83244a4d94 --- /dev/null +++ b/spring-data-redis/src/main/java/org/baeldung/spring/data/redis/config/RedisConfig.java @@ -0,0 +1,21 @@ +package org.baeldung.spring.data.redis.config; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.data.redis.connection.jedis.JedisConnectionFactory; +import org.springframework.data.redis.core.RedisTemplate; + +@Configuration +public class RedisConfig { + @Bean + JedisConnectionFactory jedisConnectionFactory() { + return new JedisConnectionFactory(); + } + + @Bean + public RedisTemplate< String, Object> redisTemplate() { + final RedisTemplate< String, Object> template = new RedisTemplate(); + template.setConnectionFactory(jedisConnectionFactory()); + return template; + } +} diff --git a/spring-data-redis/src/main/java/org/baeldung/spring/data/redis/model/Student.java b/spring-data-redis/src/main/java/org/baeldung/spring/data/redis/model/Student.java index a37ecb8dd9..825248f183 100644 --- a/spring-data-redis/src/main/java/org/baeldung/spring/data/redis/model/Student.java +++ b/spring-data-redis/src/main/java/org/baeldung/spring/data/redis/model/Student.java @@ -7,7 +7,7 @@ public class Student implements Serializable { private static final long serialVersionUID = -1907106213598514113L; public enum Gender { - Male, Female + MALE, FEMALE } private String id; diff --git a/spring-data-redis/src/main/java/org/baeldung/spring/data/redis/repo/StudentRepositoryImpl.java b/spring-data-redis/src/main/java/org/baeldung/spring/data/redis/repo/StudentRepositoryImpl.java index d964b830d2..020a96f1e4 100644 --- a/spring-data-redis/src/main/java/org/baeldung/spring/data/redis/repo/StudentRepositoryImpl.java +++ b/spring-data-redis/src/main/java/org/baeldung/spring/data/redis/repo/StudentRepositoryImpl.java @@ -2,16 +2,18 @@ package org.baeldung.spring.data.redis.repo; import org.baeldung.spring.data.redis.model.Student; import org.springframework.data.redis.core.RedisTemplate; +import org.springframework.stereotype.Repository; import java.util.Map; +@Repository public class StudentRepositoryImpl implements StudentRepository { private static final String KEY = "Student"; - private RedisTemplate redisTemplate; + private RedisTemplate redisTemplate; - public void setRedisTemplate(RedisTemplate redisTemplate) { + public void setRedisTemplate(RedisTemplate redisTemplate) { this.redisTemplate = redisTemplate; } diff --git a/spring-data-redis/src/test/java/org/baeldung/spring/data/redis/repo/StudentRepositoryTest.java b/spring-data-redis/src/test/java/org/baeldung/spring/data/redis/repo/StudentRepositoryTest.java index a68d93c173..09bc74e5cf 100644 --- a/spring-data-redis/src/test/java/org/baeldung/spring/data/redis/repo/StudentRepositoryTest.java +++ b/spring-data-redis/src/test/java/org/baeldung/spring/data/redis/repo/StudentRepositoryTest.java @@ -1,9 +1,10 @@ package org.baeldung.spring.data.redis.repo; +import org.baeldung.spring.data.redis.config.RedisConfig; import org.baeldung.spring.data.redis.model.Student; +import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @@ -13,46 +14,53 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNull; @RunWith(SpringJUnit4ClassRunner.class) -@ContextConfiguration(locations = { "classpath:/spring-config.xml" }) +//@ContextConfiguration(locations = { "classpath:/spring-config.xml" }) +@ContextConfiguration(classes = RedisConfig.class) public class StudentRepositoryTest { - @Autowired - private StudentRepository studentRepository; + private StudentRepositoryImpl studentRepositoryImpl; + + @Before + public void setUp(){ + studentRepositoryImpl = new StudentRepositoryImpl(); + RedisConfig redisConfig = new RedisConfig(); + studentRepositoryImpl.setRedisTemplate(redisConfig.redisTemplate()); + } @Test public void whenSavingStudent_thenAvailableOnRetrieval() throws Exception { - final Student student = new Student("Eng2015001", "John Doe", Student.Gender.Male, 1); - studentRepository.saveStudent(student); - final Student retrievedStudent = studentRepository.findStudent(student.getId()); + final Student student = new Student("Eng2015001", "John Doe", Student.Gender.MALE, 1); + studentRepositoryImpl.saveStudent(student); + final Student retrievedStudent = studentRepositoryImpl.findStudent(student.getId()); assertEquals(student.getId(), retrievedStudent.getId()); } @Test public void whenUpdatingStudent_thenAvailableOnRetrieval() throws Exception { - final Student student = new Student("Eng2015001", "John Doe", Student.Gender.Male, 1); - studentRepository.saveStudent(student); + final Student student = new Student("Eng2015001", "John Doe", Student.Gender.MALE, 1); + studentRepositoryImpl.saveStudent(student); student.setName("Richard Watson"); - studentRepository.saveStudent(student); - final Student retrievedStudent = studentRepository.findStudent(student.getId()); + studentRepositoryImpl.saveStudent(student); + final Student retrievedStudent = studentRepositoryImpl.findStudent(student.getId()); assertEquals(student.getName(), retrievedStudent.getName()); } @Test public void whenSavingStudents_thenAllShouldAvailableOnRetrieval() throws Exception { - final Student engStudent = new Student("Eng2015001", "John Doe", Student.Gender.Male, 1); - final Student medStudent = new Student("Med2015001", "Gareth Houston", Student.Gender.Male, 2); - studentRepository.saveStudent(engStudent); - studentRepository.saveStudent(medStudent); - final Map retrievedStudent = studentRepository.findAllStudents(); + final Student engStudent = new Student("Eng2015001", "John Doe", Student.Gender.MALE, 1); + final Student medStudent = new Student("Med2015001", "Gareth Houston", Student.Gender.MALE, 2); + studentRepositoryImpl.saveStudent(engStudent); + studentRepositoryImpl.saveStudent(medStudent); + final Map retrievedStudent = studentRepositoryImpl.findAllStudents(); assertEquals(retrievedStudent.size(), 2); } @Test public void whenDeletingStudent_thenNotAvailableOnRetrieval() throws Exception { - final Student student = new Student("Eng2015001", "John Doe", Student.Gender.Male, 1); - studentRepository.saveStudent(student); - studentRepository.deleteStudent(student.getId()); - final Student retrievedStudent = studentRepository.findStudent(student.getId()); + final Student student = new Student("Eng2015001", "John Doe", Student.Gender.MALE, 1); + studentRepositoryImpl.saveStudent(student); + studentRepositoryImpl.deleteStudent(student.getId()); + final Student retrievedStudent = studentRepositoryImpl.findStudent(student.getId()); assertNull(retrievedStudent); } } \ No newline at end of file