Skip to content

Commit c828f3a

Browse files
authored
Merge pull request #3 from codej99/feature/redisannotation
add TestCase @RedisHash
2 parents e889afd + 4e989fa commit c828f3a

File tree

4 files changed

+59
-1
lines changed

4 files changed

+59
-1
lines changed

src/main/java/com/redis/cluster/entity/User.java

-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
@NoArgsConstructor // 인자없는 생성자를 자동으로 생성합니다.
1818
@AllArgsConstructor // 인자를 모두 갖춘 생성자를 자동으로 생성합니다.
1919
@Table(name = "user") // 'user' 테이블과 매핑됨을 명시
20-
//@RedisHash("user")
2120
public class User implements Serializable {
2221
@Id // pk
2322
@GeneratedValue(strategy = GenerationType.IDENTITY)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.redis.cluster.entity.redis;
2+
3+
import lombok.Builder;
4+
import lombok.Getter;
5+
import org.springframework.data.annotation.Id;
6+
import org.springframework.data.redis.core.RedisHash;
7+
8+
@Getter
9+
@Builder
10+
@RedisHash("student")
11+
public class Student {
12+
@Id
13+
private long studentId;
14+
private String name;
15+
16+
public void update(String name) {
17+
this.name = name;
18+
}
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.redis.cluster.repo.redis;
2+
3+
import com.redis.cluster.entity.redis.Student;
4+
import org.springframework.data.repository.CrudRepository;
5+
6+
public interface StudentRedisRepo extends CrudRepository<Student, Long> {
7+
}
8+

src/test/java/com/redis/cluster/RedisClusterTest.java

+32
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.redis.cluster;
22

3+
import com.redis.cluster.entity.redis.Student;
4+
import com.redis.cluster.repo.redis.StudentRedisRepo;
35
import org.junit.Test;
46
import org.junit.runner.RunWith;
57
import org.slf4j.Logger;
@@ -29,6 +31,9 @@ public class RedisClusterTest {
2931
@Autowired
3032
private RedisTemplate<String, String> redisTemplate;
3133

34+
@Autowired
35+
private StudentRedisRepo redisRepo;
36+
3237
/**
3338
* 문자 데이터 구조 처리
3439
*/
@@ -191,4 +196,31 @@ public void commonCommand() {
191196
// Key 일괄 삭제
192197
assertThat(redisTemplate.delete(Arrays.asList("key1", "key2", "key3")), greaterThan(0L));
193198
}
199+
200+
@Test
201+
public void redisHash_Insert() {
202+
long studentId = 1L;
203+
String name = "행복하라";
204+
Student student = Student.builder().studentId(studentId).name(name).build();
205+
redisRepo.save(student);
206+
207+
Student cachedStudent = redisRepo.findById(studentId).orElse(null);
208+
assertNotNull(cachedStudent);
209+
assertEquals(1L, cachedStudent.getStudentId());
210+
assertEquals(name, cachedStudent.getName());
211+
}
212+
213+
@Test
214+
public void redisHash_Update() {
215+
long studentId = 1L;
216+
String name = "행복하라";
217+
Student student = Student.builder().studentId(studentId).name(name).build();
218+
student.update("정직하라");
219+
redisRepo.save(student);
220+
221+
Student cachedStudent = redisRepo.findById(studentId).orElse(null);
222+
assertNotNull(cachedStudent);
223+
assertEquals(1L, cachedStudent.getStudentId());
224+
assertEquals("정직하라", cachedStudent.getName());
225+
}
194226
}

0 commit comments

Comments
 (0)