Adding Java config

This commit is contained in:
sameira 2016-01-22 08:44:58 +05:30
parent 97ca1980ac
commit bc7240ead1
4 changed files with 54 additions and 23 deletions

View File

@ -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<String, Object>();
template.setConnectionFactory(jedisConnectionFactory());
return template;
}
}

View File

@ -7,7 +7,7 @@ public class Student implements Serializable {
private static final long serialVersionUID = -1907106213598514113L; private static final long serialVersionUID = -1907106213598514113L;
public enum Gender { public enum Gender {
Male, Female MALE, FEMALE
} }
private String id; private String id;

View File

@ -2,16 +2,18 @@ package org.baeldung.spring.data.redis.repo;
import org.baeldung.spring.data.redis.model.Student; import org.baeldung.spring.data.redis.model.Student;
import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Repository;
import java.util.Map; import java.util.Map;
@Repository
public class StudentRepositoryImpl implements StudentRepository { public class StudentRepositoryImpl implements StudentRepository {
private static final String KEY = "Student"; private static final String KEY = "Student";
private RedisTemplate<String, Student> redisTemplate; private RedisTemplate<String, Object> redisTemplate;
public void setRedisTemplate(RedisTemplate<String, Student> redisTemplate) { public void setRedisTemplate(RedisTemplate<String, Object> redisTemplate) {
this.redisTemplate = redisTemplate; this.redisTemplate = redisTemplate;
} }

View File

@ -1,9 +1,10 @@
package org.baeldung.spring.data.redis.repo; 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.baeldung.spring.data.redis.model.Student;
import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@ -13,46 +14,53 @@ import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull; import static org.junit.Assert.assertNull;
@RunWith(SpringJUnit4ClassRunner.class) @RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:/spring-config.xml" }) //@ContextConfiguration(locations = { "classpath:/spring-config.xml" })
@ContextConfiguration(classes = RedisConfig.class)
public class StudentRepositoryTest { public class StudentRepositoryTest {
@Autowired private StudentRepositoryImpl studentRepositoryImpl;
private StudentRepository studentRepository;
@Before
public void setUp(){
studentRepositoryImpl = new StudentRepositoryImpl();
RedisConfig redisConfig = new RedisConfig();
studentRepositoryImpl.setRedisTemplate(redisConfig.redisTemplate());
}
@Test @Test
public void whenSavingStudent_thenAvailableOnRetrieval() throws Exception { public void whenSavingStudent_thenAvailableOnRetrieval() throws Exception {
final Student student = new Student("Eng2015001", "John Doe", Student.Gender.Male, 1); final Student student = new Student("Eng2015001", "John Doe", Student.Gender.MALE, 1);
studentRepository.saveStudent(student); studentRepositoryImpl.saveStudent(student);
final Student retrievedStudent = studentRepository.findStudent(student.getId()); final Student retrievedStudent = studentRepositoryImpl.findStudent(student.getId());
assertEquals(student.getId(), retrievedStudent.getId()); assertEquals(student.getId(), retrievedStudent.getId());
} }
@Test @Test
public void whenUpdatingStudent_thenAvailableOnRetrieval() throws Exception { public void whenUpdatingStudent_thenAvailableOnRetrieval() throws Exception {
final Student student = new Student("Eng2015001", "John Doe", Student.Gender.Male, 1); final Student student = new Student("Eng2015001", "John Doe", Student.Gender.MALE, 1);
studentRepository.saveStudent(student); studentRepositoryImpl.saveStudent(student);
student.setName("Richard Watson"); student.setName("Richard Watson");
studentRepository.saveStudent(student); studentRepositoryImpl.saveStudent(student);
final Student retrievedStudent = studentRepository.findStudent(student.getId()); final Student retrievedStudent = studentRepositoryImpl.findStudent(student.getId());
assertEquals(student.getName(), retrievedStudent.getName()); assertEquals(student.getName(), retrievedStudent.getName());
} }
@Test @Test
public void whenSavingStudents_thenAllShouldAvailableOnRetrieval() throws Exception { public void whenSavingStudents_thenAllShouldAvailableOnRetrieval() throws Exception {
final Student engStudent = new Student("Eng2015001", "John Doe", Student.Gender.Male, 1); final Student engStudent = new Student("Eng2015001", "John Doe", Student.Gender.MALE, 1);
final Student medStudent = new Student("Med2015001", "Gareth Houston", Student.Gender.Male, 2); final Student medStudent = new Student("Med2015001", "Gareth Houston", Student.Gender.MALE, 2);
studentRepository.saveStudent(engStudent); studentRepositoryImpl.saveStudent(engStudent);
studentRepository.saveStudent(medStudent); studentRepositoryImpl.saveStudent(medStudent);
final Map<Object, Object> retrievedStudent = studentRepository.findAllStudents(); final Map<Object, Object> retrievedStudent = studentRepositoryImpl.findAllStudents();
assertEquals(retrievedStudent.size(), 2); assertEquals(retrievedStudent.size(), 2);
} }
@Test @Test
public void whenDeletingStudent_thenNotAvailableOnRetrieval() throws Exception { public void whenDeletingStudent_thenNotAvailableOnRetrieval() throws Exception {
final Student student = new Student("Eng2015001", "John Doe", Student.Gender.Male, 1); final Student student = new Student("Eng2015001", "John Doe", Student.Gender.MALE, 1);
studentRepository.saveStudent(student); studentRepositoryImpl.saveStudent(student);
studentRepository.deleteStudent(student.getId()); studentRepositoryImpl.deleteStudent(student.getId());
final Student retrievedStudent = studentRepository.findStudent(student.getId()); final Student retrievedStudent = studentRepositoryImpl.findStudent(student.getId());
assertNull(retrievedStudent); assertNull(retrievedStudent);
} }
} }