diff --git a/spring-data-radis/pom.xml b/spring-data-radis/pom.xml new file mode 100644 index 0000000000..4d3b467d7b --- /dev/null +++ b/spring-data-radis/pom.xml @@ -0,0 +1,78 @@ + + 4.0.0 + + org.baeldung + sprint-data-radis + 0.0.1-SNAPSHOT + jar + + sprint-data-radis + + + UTF-8 + 4.2.2.RELEASE + 1.6.2.RELEASE + 0.8.0 + + + + + org.springframework.data + spring-data-redis + ${spring-data-redis} + + + + cglib + cglib-nodep + 2.2 + + + + log4j + log4j + 1.2.16 + + + + redis.clients + jedis + 2.5.1 + jar + + + + org.springframework + spring-core + ${spring.version} + + + + org.springframework + spring-context + ${spring.version} + + + + junit + junit + 4.12 + test + + + + org.springframework + spring-test + ${spring.version} + test + + + + com.lordofthejars + nosqlunit-redis + ${nosqlunit.version} + + + + diff --git a/spring-data-radis/src/main/java/org/baeldung/spring/data/radis/model/Student.java b/spring-data-radis/src/main/java/org/baeldung/spring/data/radis/model/Student.java new file mode 100644 index 0000000000..4b8a90943c --- /dev/null +++ b/spring-data-radis/src/main/java/org/baeldung/spring/data/radis/model/Student.java @@ -0,0 +1,64 @@ +package org.baeldung.spring.data.radis.model; + + +import java.io.Serializable; + +public class Student implements Serializable { + + private static final long serialVersionUID = -1907106213598514113L; + + public enum Gender{Male, Female} + private String id; + private String name; + private Gender gender; + private int grade; + + public Student(String id, String name, Gender gender, int grade) { + this.id = id; + this.name = name; + this.gender = gender; + this.grade = grade; + } + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public Gender getGender() { + return gender; + } + + public void setGender(Gender gender) { + this.gender = gender; + } + + public int getGrade() { + return grade; + } + + public void setGrade(int grade) { + this.grade = grade; + } + + @Override + public String toString() { + return "Student{" + + "id='" + id + '\'' + + ", name='" + name + '\'' + + ", gender=" + gender + + ", grade=" + grade + + '}'; + } +} \ No newline at end of file diff --git a/spring-data-radis/src/main/java/org/baeldung/spring/data/radis/repo/StudentRepository.java b/spring-data-radis/src/main/java/org/baeldung/spring/data/radis/repo/StudentRepository.java new file mode 100644 index 0000000000..bc1201478c --- /dev/null +++ b/spring-data-radis/src/main/java/org/baeldung/spring/data/radis/repo/StudentRepository.java @@ -0,0 +1,17 @@ +package org.baeldung.spring.data.radis.repo; + +import org.baeldung.spring.data.radis.model.Student; + +import java.util.Map; + +public interface StudentRepository { + void saveStudent(Student person); + + void updateStudent(Student student); + + Student findStudent(String id); + + Map findAllStudents(); + + void deleteStudent(String id); +} diff --git a/spring-data-radis/src/main/java/org/baeldung/spring/data/radis/repo/StudentRepositoryImpl.java b/spring-data-radis/src/main/java/org/baeldung/spring/data/radis/repo/StudentRepositoryImpl.java new file mode 100644 index 0000000000..3033020c24 --- /dev/null +++ b/spring-data-radis/src/main/java/org/baeldung/spring/data/radis/repo/StudentRepositoryImpl.java @@ -0,0 +1,37 @@ +package org.baeldung.spring.data.radis.repo; + +import org.baeldung.spring.data.radis.model.Student; +import org.springframework.data.redis.core.RedisTemplate; + +import java.util.Map; + +public class StudentRepositoryImpl implements StudentRepository { + + private static final String KEY = "Student"; + + private RedisTemplate redisTemplate; + + public void setRedisTemplate(RedisTemplate redisTemplate) { + this.redisTemplate = redisTemplate; + } + + public void saveStudent(final Student student) { + this.redisTemplate.opsForHash().put(KEY, student.getId(), student); + } + + public void updateStudent(final Student student) { + this.redisTemplate.opsForHash().put(KEY, student.getId(), student); + } + + public Student findStudent(final String id) { + return (Student)this.redisTemplate.opsForHash().get(KEY, id); + } + + public Map findAllStudents() { + return this.redisTemplate.opsForHash().entries(KEY); + } + + public void deleteStudent(final String id) { + this.redisTemplate.opsForHash().delete(KEY,id); + } +} diff --git a/spring-data-radis/src/main/resources/logback.xml b/spring-data-radis/src/main/resources/logback.xml new file mode 100644 index 0000000000..215eeede64 --- /dev/null +++ b/spring-data-radis/src/main/resources/logback.xml @@ -0,0 +1,20 @@ + + + + + web - %date [%thread] %-5level %logger{36} - %message%n + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/spring-data-radis/src/main/resources/spring-config.xml b/spring-data-radis/src/main/resources/spring-config.xml new file mode 100644 index 0000000000..950cd9a6fd --- /dev/null +++ b/spring-data-radis/src/main/resources/spring-config.xml @@ -0,0 +1,19 @@ + + + + + + + + + + + + \ No newline at end of file diff --git a/spring-data-radis/src/main/resources/test.png b/spring-data-radis/src/main/resources/test.png new file mode 100644 index 0000000000..c3b5e80276 Binary files /dev/null and b/spring-data-radis/src/main/resources/test.png differ diff --git a/spring-data-radis/src/test/java/org/baeldung/spring/data/radis/repo/StudentRepositoryTest.java b/spring-data-radis/src/test/java/org/baeldung/spring/data/radis/repo/StudentRepositoryTest.java new file mode 100644 index 0000000000..024fd06295 --- /dev/null +++ b/spring-data-radis/src/test/java/org/baeldung/spring/data/radis/repo/StudentRepositoryTest.java @@ -0,0 +1,58 @@ +package org.baeldung.spring.data.radis.repo; + +import org.baeldung.spring.data.radis.model.Student; +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; + +import java.util.Map; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; + +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration(locations = {"classpath:/spring-config.xml"}) +public class StudentRepositoryTest { + + @Autowired + private StudentRepository studentRepository; + + @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()); + 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); + student.setName("Richard Watson"); + studentRepository.saveStudent(student); + final Student retrievedStudent = studentRepository.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(); + 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()); + assertNull(retrievedStudent); + } +} \ No newline at end of file