From e3e9fcc4c7acbe07ef88b35168e2edab2412a486 Mon Sep 17 00:00:00 2001 From: kimyonghwa Date: Tue, 7 May 2019 19:31:15 +0900 Subject: [PATCH 1/8] Spring data redis - redis annotation --- src/main/java/com/redis/cluster/RedisClusterApplication.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/main/java/com/redis/cluster/RedisClusterApplication.java b/src/main/java/com/redis/cluster/RedisClusterApplication.java index d4d4c7a..80f9d28 100644 --- a/src/main/java/com/redis/cluster/RedisClusterApplication.java +++ b/src/main/java/com/redis/cluster/RedisClusterApplication.java @@ -2,11 +2,13 @@ import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.cache.annotation.EnableCaching; import org.springframework.context.annotation.Bean; import org.springframework.security.crypto.factory.PasswordEncoderFactories; import org.springframework.security.crypto.password.PasswordEncoder; @SpringBootApplication +@EnableCaching public class RedisClusterApplication { public static void main(String[] args) { From 4124ed0d56409d55337d3026edc70f65799711fc Mon Sep 17 00:00:00 2001 From: kimyonghwa Date: Wed, 8 May 2019 02:10:51 +0900 Subject: [PATCH 2/8] seperate configuration --- src/main/resources/application.yml | 8 -------- src/main/resources/application_local.yml | 13 +++++++++++++ 2 files changed, 13 insertions(+), 8 deletions(-) create mode 100644 src/main/resources/application_local.yml diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index eac2546..f9b9fa8 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -2,14 +2,6 @@ server: port: 8081 spring: - datasource: - url: jdbc:h2:tcp://localhost/~/test - driver-class-name: org.h2.Driver - username: sa - jpa: - database-platform: org.hibernate.dialect.H2Dialect - properties.hibernate.hbm2ddl.auto: update - showSql: true redis: cluster: nodes: diff --git a/src/main/resources/application_local.yml b/src/main/resources/application_local.yml new file mode 100644 index 0000000..20bec35 --- /dev/null +++ b/src/main/resources/application_local.yml @@ -0,0 +1,13 @@ +server: + port: 8081 + +spring: + profiles: local + datasource: + url: jdbc:h2:tcp://localhost:9000/mem:test;MVCC=TRUE + driver-class-name: org.h2.Driver + username: sa + jpa: + database-platform: org.hibernate.dialect.H2Dialect + properties.hibernate.hbm2ddl.auto: create-drop + showSql: true \ No newline at end of file From 85921d5ca15e5832dd7a06db6f5cac7898d8fac6 Mon Sep 17 00:00:00 2001 From: kimyonghwa Date: Wed, 8 May 2019 02:11:35 +0900 Subject: [PATCH 3/8] modify build.gradle --- build.gradle | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/build.gradle b/build.gradle index d0aef53..fa304b0 100644 --- a/build.gradle +++ b/build.gradle @@ -23,11 +23,10 @@ dependencies { implementation 'org.springframework.boot:spring-boot-starter-data-jpa' implementation 'org.springframework.boot:spring-boot-starter-freemarker' implementation 'org.springframework.boot:spring-boot-starter-web' - implementation 'org.springframework.boot:spring-boot-starter-security' implementation 'org.springframework.boot:spring-boot-starter-data-redis' implementation 'com.google.code.gson:gson' + implementation 'com.h2database:h2' compileOnly 'org.projectlombok:lombok' - runtimeOnly 'com.h2database:h2' runtimeOnly 'mysql:mysql-connector-java' annotationProcessor 'org.projectlombok:lombok' testImplementation 'org.springframework.boot:spring-boot-starter-test' From 3b617db300a3ddb6322fcc42507b0e06c00a74f0 Mon Sep 17 00:00:00 2001 From: kimyonghwa Date: Wed, 8 May 2019 02:17:56 +0900 Subject: [PATCH 4/8] update redis, h2 configuration --- .../cluster/RedisClusterApplication.java | 32 +++++++++++-- .../com/redis/cluster/common/CacheKey.java | 11 +++++ .../redis/cluster/config/EmbededH2Config.java | 18 ++++++++ .../cluster/controller/RedisController.java | 46 ++++++++++--------- 4 files changed, 80 insertions(+), 27 deletions(-) create mode 100644 src/main/java/com/redis/cluster/common/CacheKey.java create mode 100644 src/main/java/com/redis/cluster/config/EmbededH2Config.java diff --git a/src/main/java/com/redis/cluster/RedisClusterApplication.java b/src/main/java/com/redis/cluster/RedisClusterApplication.java index 80f9d28..682b78d 100644 --- a/src/main/java/com/redis/cluster/RedisClusterApplication.java +++ b/src/main/java/com/redis/cluster/RedisClusterApplication.java @@ -1,11 +1,20 @@ package com.redis.cluster; +import com.redis.cluster.common.CacheKey; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cache.annotation.EnableCaching; import org.springframework.context.annotation.Bean; -import org.springframework.security.crypto.factory.PasswordEncoderFactories; -import org.springframework.security.crypto.password.PasswordEncoder; +import org.springframework.data.redis.cache.CacheKeyPrefix; +import org.springframework.data.redis.cache.RedisCacheConfiguration; +import org.springframework.data.redis.cache.RedisCacheManager; +import org.springframework.data.redis.connection.RedisConnectionFactory; +import org.springframework.data.redis.serializer.RedisSerializationContext; +import org.springframework.data.redis.serializer.StringRedisSerializer; + +import java.time.Duration; +import java.util.HashMap; +import java.util.Map; @SpringBootApplication @EnableCaching @@ -15,8 +24,21 @@ public static void main(String[] args) { SpringApplication.run(RedisClusterApplication.class, args); } - @Bean - public PasswordEncoder passwordEncoder() { - return PasswordEncoderFactories.createDelegatingPasswordEncoder(); + // https://stackoverflow.com/questions/51660160/set-expire-key-at-specific-time-when-using-spring-caching-with-redis + @Bean(name = "cacheManager") + public RedisCacheManager cacheManager(RedisConnectionFactory connectionFactory) { + + RedisCacheConfiguration configuration = RedisCacheConfiguration.defaultCacheConfig() + .disableCachingNullValues() + .entryTtl(Duration.ofSeconds(CacheKey.DEFAULT_EXPIRE_SEC)) + .computePrefixWith(CacheKeyPrefix.simple()) + .serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(new StringRedisSerializer())); + + Map cacheConfigurations = new HashMap<>(); + cacheConfigurations.put(CacheKey.USER_SINGLE, RedisCacheConfiguration.defaultCacheConfig() + .entryTtl(Duration.ofSeconds(CacheKey.USER_SINGLE_EXPIRE_SEC))); + + return RedisCacheManager.RedisCacheManagerBuilder.fromConnectionFactory(connectionFactory).cacheDefaults(configuration) + .withInitialCacheConfigurations(cacheConfigurations).build(); } } diff --git a/src/main/java/com/redis/cluster/common/CacheKey.java b/src/main/java/com/redis/cluster/common/CacheKey.java new file mode 100644 index 0000000..b479c5e --- /dev/null +++ b/src/main/java/com/redis/cluster/common/CacheKey.java @@ -0,0 +1,11 @@ +package com.redis.cluster.common; + +public class CacheKey { + + private CacheKey() { + } + + public static final int DEFAULT_EXPIRE_SEC = 60; + public static final String USER_SINGLE = "user-single"; + public static final int USER_SINGLE_EXPIRE_SEC = 180; +} diff --git a/src/main/java/com/redis/cluster/config/EmbededH2Config.java b/src/main/java/com/redis/cluster/config/EmbededH2Config.java new file mode 100644 index 0000000..e15e67c --- /dev/null +++ b/src/main/java/com/redis/cluster/config/EmbededH2Config.java @@ -0,0 +1,18 @@ +package com.redis.cluster.config; + +import org.h2.tools.Server; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Profile; + +import java.sql.SQLException; + +@Configuration +@Profile("local") +public class EmbededH2Config { + @Bean + public Server h2TcpServer() throws SQLException { + return Server.createTcpServer() + .start(); + } +} diff --git a/src/main/java/com/redis/cluster/controller/RedisController.java b/src/main/java/com/redis/cluster/controller/RedisController.java index 1a7f84f..ca280de 100644 --- a/src/main/java/com/redis/cluster/controller/RedisController.java +++ b/src/main/java/com/redis/cluster/controller/RedisController.java @@ -1,45 +1,47 @@ package com.redis.cluster.controller; +import com.redis.cluster.common.CacheKey; import com.redis.cluster.entity.User; import com.redis.cluster.repo.UserJpaRepo; import lombok.RequiredArgsConstructor; -import org.springframework.data.redis.core.RedisTemplate; -import org.springframework.data.redis.core.ValueOperations; -import org.springframework.security.crypto.password.PasswordEncoder; +import org.springframework.cache.annotation.CachePut; +import org.springframework.cache.annotation.Cacheable; import org.springframework.web.bind.annotation.*; -import java.util.ArrayList; -import java.util.Collection; import java.util.Collections; -import java.util.List; @RequiredArgsConstructor @RequestMapping("/redis") @RestController public class RedisController { - private final RedisTemplate redisTemplate; private final UserJpaRepo userJpaRepo; - private final PasswordEncoder passwordEncoder; - - @GetMapping("/ops/value") - public List redisClusterTest() { - ValueOperations valueOps = redisTemplate.opsForValue(); - Collection keys = new ArrayList<>(); - for (int i = 0; i < 10; i++) { - keys.add("valueOps_" + i); - valueOps.set("valueOps_" + i, String.valueOf(i)); - } - return valueOps.multiGet(keys); + private static final String UID = "strongdaddy@naver.com"; + + @Cacheable(value = CacheKey.USER_SINGLE, key = "#uid", unless = "#result == null") + @GetMapping("/user/{uid}") + public User findOne(@PathVariable String uid) { + return userJpaRepo.findByUid(uid).orElse(null); } - @PostMapping("/post/user") - public void redisClusterPostUser() { + @PostMapping("/user") + @ResponseBody + public User postUser() { userJpaRepo.save(User.builder() - .uid("strongdaddy@naver.com") - .password(passwordEncoder.encode("1234")) + .uid(UID) + .password("1234") .name("strongdaddy") .roles(Collections.singletonList("ROLE_USER")) .build()); + + return userJpaRepo.findByUid(UID).orElse(null); + } + + @CachePut(value = CacheKey.USER_SINGLE, key = "#user.uid") + @PutMapping("/user") + @ResponseBody + public User putUser(@RequestBody User user) { + userJpaRepo.save(user); + return userJpaRepo.findByUid(UID).orElse(null); } } From 3218267a6a4c70588c46bfaa6553039e91e98cf3 Mon Sep 17 00:00:00 2001 From: kimyonghwa Date: Wed, 8 May 2019 02:18:15 +0900 Subject: [PATCH 5/8] update gradle wrapper --- gradle/wrapper/gradle-wrapper.properties | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index 290541c..fd624f1 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,5 +1,6 @@ +#Tue May 07 23:44:13 KST 2019 distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-4.10.3-bin.zip zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists +distributionUrl=https\://services.gradle.org/distributions/gradle-4.10.3-all.zip From 9e67838387b8f97a8c623756fca1817531fe1a04 Mon Sep 17 00:00:00 2001 From: kimyonghwa Date: Wed, 8 May 2019 18:09:46 +0900 Subject: [PATCH 6/8] Spring data redis - redis annotation --- build.gradle | 2 + .../cluster/RedisClusterApplication.java | 32 ------- .../com/redis/cluster/common/CacheKey.java | 5 +- .../redis/cluster/config/EmbededH2Config.java | 18 ---- .../cluster/config/RedisCacheConfig.java | 39 +++++++++ .../cluster/controller/RedisController.java | 36 ++++---- src/main/resources/application.yml | 21 ++++- src/main/resources/application_local.yml | 13 --- .../controller/RedisControllerTest.java | 83 +++++++++++++++++-- 9 files changed, 155 insertions(+), 94 deletions(-) delete mode 100644 src/main/java/com/redis/cluster/config/EmbededH2Config.java create mode 100644 src/main/java/com/redis/cluster/config/RedisCacheConfig.java delete mode 100644 src/main/resources/application_local.yml diff --git a/build.gradle b/build.gradle index fa304b0..50885f4 100644 --- a/build.gradle +++ b/build.gradle @@ -24,10 +24,12 @@ dependencies { implementation 'org.springframework.boot:spring-boot-starter-freemarker' implementation 'org.springframework.boot:spring-boot-starter-web' implementation 'org.springframework.boot:spring-boot-starter-data-redis' + implementation 'org.springframework.boot:spring-boot-starter-cache' implementation 'com.google.code.gson:gson' implementation 'com.h2database:h2' compileOnly 'org.projectlombok:lombok' runtimeOnly 'mysql:mysql-connector-java' annotationProcessor 'org.projectlombok:lombok' testImplementation 'org.springframework.boot:spring-boot-starter-test' + testCompileOnly 'org.projectlombok:lombok' } \ No newline at end of file diff --git a/src/main/java/com/redis/cluster/RedisClusterApplication.java b/src/main/java/com/redis/cluster/RedisClusterApplication.java index 682b78d..2c45a59 100644 --- a/src/main/java/com/redis/cluster/RedisClusterApplication.java +++ b/src/main/java/com/redis/cluster/RedisClusterApplication.java @@ -1,44 +1,12 @@ package com.redis.cluster; -import com.redis.cluster.common.CacheKey; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; -import org.springframework.cache.annotation.EnableCaching; -import org.springframework.context.annotation.Bean; -import org.springframework.data.redis.cache.CacheKeyPrefix; -import org.springframework.data.redis.cache.RedisCacheConfiguration; -import org.springframework.data.redis.cache.RedisCacheManager; -import org.springframework.data.redis.connection.RedisConnectionFactory; -import org.springframework.data.redis.serializer.RedisSerializationContext; -import org.springframework.data.redis.serializer.StringRedisSerializer; - -import java.time.Duration; -import java.util.HashMap; -import java.util.Map; @SpringBootApplication -@EnableCaching public class RedisClusterApplication { public static void main(String[] args) { SpringApplication.run(RedisClusterApplication.class, args); } - - // https://stackoverflow.com/questions/51660160/set-expire-key-at-specific-time-when-using-spring-caching-with-redis - @Bean(name = "cacheManager") - public RedisCacheManager cacheManager(RedisConnectionFactory connectionFactory) { - - RedisCacheConfiguration configuration = RedisCacheConfiguration.defaultCacheConfig() - .disableCachingNullValues() - .entryTtl(Duration.ofSeconds(CacheKey.DEFAULT_EXPIRE_SEC)) - .computePrefixWith(CacheKeyPrefix.simple()) - .serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(new StringRedisSerializer())); - - Map cacheConfigurations = new HashMap<>(); - cacheConfigurations.put(CacheKey.USER_SINGLE, RedisCacheConfiguration.defaultCacheConfig() - .entryTtl(Duration.ofSeconds(CacheKey.USER_SINGLE_EXPIRE_SEC))); - - return RedisCacheManager.RedisCacheManagerBuilder.fromConnectionFactory(connectionFactory).cacheDefaults(configuration) - .withInitialCacheConfigurations(cacheConfigurations).build(); - } } diff --git a/src/main/java/com/redis/cluster/common/CacheKey.java b/src/main/java/com/redis/cluster/common/CacheKey.java index b479c5e..92421a0 100644 --- a/src/main/java/com/redis/cluster/common/CacheKey.java +++ b/src/main/java/com/redis/cluster/common/CacheKey.java @@ -6,6 +6,7 @@ private CacheKey() { } public static final int DEFAULT_EXPIRE_SEC = 60; - public static final String USER_SINGLE = "user-single"; - public static final int USER_SINGLE_EXPIRE_SEC = 180; + + public static final String USER = "user"; + public static final int USER_EXPIRE_SEC = 180; } diff --git a/src/main/java/com/redis/cluster/config/EmbededH2Config.java b/src/main/java/com/redis/cluster/config/EmbededH2Config.java deleted file mode 100644 index e15e67c..0000000 --- a/src/main/java/com/redis/cluster/config/EmbededH2Config.java +++ /dev/null @@ -1,18 +0,0 @@ -package com.redis.cluster.config; - -import org.h2.tools.Server; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; -import org.springframework.context.annotation.Profile; - -import java.sql.SQLException; - -@Configuration -@Profile("local") -public class EmbededH2Config { - @Bean - public Server h2TcpServer() throws SQLException { - return Server.createTcpServer() - .start(); - } -} diff --git a/src/main/java/com/redis/cluster/config/RedisCacheConfig.java b/src/main/java/com/redis/cluster/config/RedisCacheConfig.java new file mode 100644 index 0000000..1c11297 --- /dev/null +++ b/src/main/java/com/redis/cluster/config/RedisCacheConfig.java @@ -0,0 +1,39 @@ +package com.redis.cluster.config; + +import com.redis.cluster.common.CacheKey; +import org.springframework.cache.annotation.EnableCaching; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.data.redis.cache.CacheKeyPrefix; +import org.springframework.data.redis.cache.RedisCacheConfiguration; +import org.springframework.data.redis.cache.RedisCacheManager; +import org.springframework.data.redis.connection.RedisConnectionFactory; +import org.springframework.data.redis.serializer.RedisSerializationContext; +import org.springframework.data.redis.serializer.StringRedisSerializer; + +import java.time.Duration; +import java.util.HashMap; +import java.util.Map; + +@Configuration +@EnableCaching +public class RedisCacheConfig { + + @Bean(name = "cacheManager") + public RedisCacheManager cacheManager(RedisConnectionFactory connectionFactory) { + + RedisCacheConfiguration configuration = RedisCacheConfiguration.defaultCacheConfig() + .disableCachingNullValues() + .entryTtl(Duration.ofSeconds(CacheKey.DEFAULT_EXPIRE_SEC)) + .computePrefixWith(CacheKeyPrefix.simple()) + .serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(new StringRedisSerializer())); + + Map cacheConfigurations = new HashMap<>(); + // User + cacheConfigurations.put(CacheKey.USER, RedisCacheConfiguration.defaultCacheConfig() + .entryTtl(Duration.ofSeconds(CacheKey.USER_EXPIRE_SEC))); + + return RedisCacheManager.RedisCacheManagerBuilder.fromConnectionFactory(connectionFactory).cacheDefaults(configuration) + .withInitialCacheConfigurations(cacheConfigurations).build(); + } +} \ No newline at end of file diff --git a/src/main/java/com/redis/cluster/controller/RedisController.java b/src/main/java/com/redis/cluster/controller/RedisController.java index ca280de..acafc8a 100644 --- a/src/main/java/com/redis/cluster/controller/RedisController.java +++ b/src/main/java/com/redis/cluster/controller/RedisController.java @@ -4,44 +4,42 @@ import com.redis.cluster.entity.User; import com.redis.cluster.repo.UserJpaRepo; import lombok.RequiredArgsConstructor; +import org.springframework.cache.annotation.CacheEvict; import org.springframework.cache.annotation.CachePut; import org.springframework.cache.annotation.Cacheable; import org.springframework.web.bind.annotation.*; -import java.util.Collections; - @RequiredArgsConstructor @RequestMapping("/redis") @RestController public class RedisController { private final UserJpaRepo userJpaRepo; - private static final String UID = "strongdaddy@naver.com"; - @Cacheable(value = CacheKey.USER_SINGLE, key = "#uid", unless = "#result == null") - @GetMapping("/user/{uid}") - public User findOne(@PathVariable String uid) { - return userJpaRepo.findByUid(uid).orElse(null); + @Cacheable(value = CacheKey.USER, key = "#msrl", unless = "#result == null") + @GetMapping("/user/{msrl}") + public User findOne(@PathVariable long msrl) { + return userJpaRepo.findById(msrl).orElse(null); } @PostMapping("/user") @ResponseBody - public User postUser() { - userJpaRepo.save(User.builder() - .uid(UID) - .password("1234") - .name("strongdaddy") - .roles(Collections.singletonList("ROLE_USER")) - .build()); - - return userJpaRepo.findByUid(UID).orElse(null); + public User postUser(@RequestBody User user) { + return userJpaRepo.save(user); } - @CachePut(value = CacheKey.USER_SINGLE, key = "#user.uid") + @CachePut(value = CacheKey.USER, key = "#user.msrl") @PutMapping("/user") @ResponseBody public User putUser(@RequestBody User user) { - userJpaRepo.save(user); - return userJpaRepo.findByUid(UID).orElse(null); + return userJpaRepo.save(user); + } + + @CacheEvict(value = CacheKey.USER, key = "#msrl") + @DeleteMapping("/user/{msrl}") + @ResponseBody + public boolean deleteUser(@PathVariable long msrl) { + userJpaRepo.deleteById(msrl); + return true; } } diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index f9b9fa8..1f7d355 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -1,7 +1,25 @@ server: port: 8081 +# +#logging: +# level: +# root: warn +# com.rest.api: debug +# org.springframework.data.redis: trace spring: +# main.allow-bean-definition-overriding: true + datasource: + url: jdbc:h2:tcp://localhost/~/test + driver-class-name: org.h2.Driver + username: sa + jpa: + database-platform: org.hibernate.dialect.H2Dialect + properties.hibernate: + hbm2ddl.auto: update + format_sql: true + showSql: true + generate-ddl: true redis: cluster: nodes: @@ -11,4 +29,5 @@ spring: - 15.164.98.87:6400 - 15.164.98.87:6401 - 15.164.98.87:6402 - password: XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX \ No newline at end of file + max-redirects: 3 + password: 02cc9f8b3ff0eead714a76cc0ab94da4e3ea094a81838347b507648f5d8ec9bb diff --git a/src/main/resources/application_local.yml b/src/main/resources/application_local.yml deleted file mode 100644 index 20bec35..0000000 --- a/src/main/resources/application_local.yml +++ /dev/null @@ -1,13 +0,0 @@ -server: - port: 8081 - -spring: - profiles: local - datasource: - url: jdbc:h2:tcp://localhost:9000/mem:test;MVCC=TRUE - driver-class-name: org.h2.Driver - username: sa - jpa: - database-platform: org.hibernate.dialect.H2Dialect - properties.hibernate.hbm2ddl.auto: create-drop - showSql: true \ No newline at end of file diff --git a/src/test/java/com/redis/cluster/controller/RedisControllerTest.java b/src/test/java/com/redis/cluster/controller/RedisControllerTest.java index a73cb60..3cb3e1e 100644 --- a/src/test/java/com/redis/cluster/controller/RedisControllerTest.java +++ b/src/test/java/com/redis/cluster/controller/RedisControllerTest.java @@ -1,42 +1,107 @@ package com.redis.cluster.controller; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.redis.cluster.entity.User; +import lombok.extern.slf4j.Slf4j; +import org.junit.After; +import org.junit.Before; +import org.junit.FixMethodOrder; import org.junit.Test; import org.junit.runner.RunWith; +import org.junit.runners.MethodSorters; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.json.JacksonJsonParser; import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.http.MediaType; import org.springframework.test.context.junit4.SpringRunner; import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.MvcResult; -import static org.hamcrest.Matchers.hasSize; -import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; -import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; +import java.util.Collections; + +import static org.hamcrest.Matchers.greaterThan; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; +@Slf4j @RunWith(SpringRunner.class) @SpringBootTest @AutoConfigureMockMvc +@FixMethodOrder(MethodSorters.NAME_ASCENDING) public class RedisControllerTest { @Autowired private MockMvc mockMvc; + @Autowired + private ObjectMapper objectMapper; + + private long msrl; + + @Before + public void setUp() throws Exception { + User user = User.builder() + .uid("happydaddy@naver.com") + .name("happydaddy") + .password("password") + .roles(Collections.singletonList("ROLE_USER")) + .build(); + MvcResult action = mockMvc.perform(post("/redis/user") + .header("Accept", "application/json;charset=UTF-8") + .contentType(MediaType.APPLICATION_JSON) + .content(objectMapper.writeValueAsString(user))) + .andDo(print()) + .andExpect(status().isOk()) + .andExpect(jsonPath("$.msrl").value(greaterThan(0))) + .andExpect(jsonPath("$.uid").value("happydaddy@naver.com")) + .andExpect(jsonPath("$.name").value("happydaddy")) + .andExpect(jsonPath("$.roles").isArray()) + .andReturn(); + + String resultString = action.getResponse().getContentAsString(); + JacksonJsonParser jsonParser = new JacksonJsonParser(); + msrl = Long.valueOf(jsonParser.parseMap(resultString).get("msrl").toString()); + } + @Test - public void opsForValue() throws Exception { - mockMvc.perform(get("/redis/ops/value")) + public void A_getUser() throws Exception { + mockMvc.perform(get("/redis/user/" + msrl)) .andDo(print()) .andExpect(status().isOk()) - .andExpect(jsonPath("$").isArray()) - .andExpect(jsonPath("$", hasSize(10))); + .andExpect(jsonPath("$.msrl").value(msrl)) + .andExpect(jsonPath("$.uid").value("happydaddy@naver.com")) + .andExpect(jsonPath("$.name").value("happydaddy")) + .andExpect(jsonPath("$.roles[0]").value("ROLE_USER")); } @Test - public void postUser() throws Exception { - mockMvc.perform(post("/redis/post/user")) + public void B_putUser() throws Exception { + User user = User.builder() + .msrl(msrl) + .uid("happydaddy@naver.com") + .name("happydaddy_re") + .roles(Collections.singletonList("ROLE_ADMIN")).build(); + mockMvc.perform(put("/redis/user") + .header("Accept", "application/json;charset=UTF-8") + .contentType(MediaType.APPLICATION_JSON) + .content(objectMapper.writeValueAsString(user))) + .andDo(print()) + .andExpect(status().isOk()) + .andExpect(jsonPath("$.msrl").value(msrl)) + .andExpect(jsonPath("$.uid").value("happydaddy@naver.com")) + .andExpect(jsonPath("$.name").value("happydaddy_re")) + .andExpect(jsonPath("$.roles[0]").value("ROLE_ADMIN")); + } + + @After + public void delUser() throws Exception { + mockMvc.perform(delete("/redis/user/" + msrl)) .andDo(print()) .andExpect(status().isOk()); + } } \ No newline at end of file From 06bad5c8f1e45dce56a1912a8f4e4b04ece81328 Mon Sep 17 00:00:00 2001 From: kimyonghwa Date: Wed, 8 May 2019 18:13:09 +0900 Subject: [PATCH 7/8] Spring data redis - redis annotation --- src/main/resources/application.yml | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index 1f7d355..8fd0652 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -1,14 +1,12 @@ server: port: 8081 -# -#logging: -# level: -# root: warn -# com.rest.api: debug -# org.springframework.data.redis: trace + +logging: + level: + root: warn + com.rest.api: debug spring: -# main.allow-bean-definition-overriding: true datasource: url: jdbc:h2:tcp://localhost/~/test driver-class-name: org.h2.Driver @@ -30,4 +28,4 @@ spring: - 15.164.98.87:6401 - 15.164.98.87:6402 max-redirects: 3 - password: 02cc9f8b3ff0eead714a76cc0ab94da4e3ea094a81838347b507648f5d8ec9bb + password: XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX From 739045c9d4ad9a30a7ac16dcd1b27c8d765cd6bd Mon Sep 17 00:00:00 2001 From: kimyonghwa Date: Fri, 10 May 2019 00:15:17 +0900 Subject: [PATCH 8/8] update build.gradle --- build.gradle | 1 - 1 file changed, 1 deletion(-) diff --git a/build.gradle b/build.gradle index 50885f4..d3759fe 100644 --- a/build.gradle +++ b/build.gradle @@ -24,7 +24,6 @@ dependencies { implementation 'org.springframework.boot:spring-boot-starter-freemarker' implementation 'org.springframework.boot:spring-boot-starter-web' implementation 'org.springframework.boot:spring-boot-starter-data-redis' - implementation 'org.springframework.boot:spring-boot-starter-cache' implementation 'com.google.code.gson:gson' implementation 'com.h2database:h2' compileOnly 'org.projectlombok:lombok'