Skip to content

Commit

Permalink
feat: redis 동작 확인을 위한 테스트 api를 추가한다.
Browse files Browse the repository at this point in the history
  • Loading branch information
bjo6300 committed Nov 22, 2023
1 parent 5f676f3 commit 4ab2ebb
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 27 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/cd-dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: cd-dev-docker

on:
push:
branches: [ "dev" ]
branches: [ "dev", "feat/#298" ]

jobs:
build:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,11 @@
import com.clova.anifriends.domain.auth.authorization.ShelterOnly;
import jakarta.validation.Valid;
import java.net.URI;
import java.util.Map;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
Expand All @@ -26,6 +29,7 @@
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
Expand Down Expand Up @@ -150,4 +154,22 @@ public ResponseEntity<Void> deleteAnimal(
animalService.deleteAnimal(shelterId, animalId);
return ResponseEntity.noContent().build();
}

private final RedisTemplate<String, Integer> redisTemplate;

@PostMapping("/data")
public ResponseEntity<String> setRedisData(
@RequestBody(required = true) Map<String, Integer> map) throws Exception {

redisTemplate.opsForValue().set(String.valueOf(map.get("key")), map.get("value"));

return new ResponseEntity<>("정상 등록", HttpStatus.CREATED);
}

@GetMapping("/data")
public ResponseEntity<Integer> getRedisData(
@RequestParam(required = true) String key) {

return new ResponseEntity<>(redisTemplate.opsForValue().get(key), HttpStatus.OK);
}
}
27 changes: 12 additions & 15 deletions src/main/java/com/clova/anifriends/global/config/RedisConfig.java
Original file line number Diff line number Diff line change
@@ -1,39 +1,36 @@
package com.clova.anifriends.global.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericToStringSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

@EnableCaching
@Configuration
public class RedisConfig {

@Value("${spring.redis.host}")
@Value("${spring.data.redis.host}")
private String host;

@Value("${spring.redis.port}")
@Value("${spring.data.redis.port}")
private int port;

@Bean
public RedisConnectionFactory redisConnectionFactory() {
return new LettuceConnectionFactory(host, port);
}

// @Bean
// public RedisTemplate<String, Integer> redisTemplate() {
// RedisTemplate<String, Integer> redisTemplate = new RedisTemplate<>();
// redisTemplate.setKeySerializer(new StringRedisSerializer());
// redisTemplate.setValueSerializer(new StringRedisSerializer());
// redisTemplate.setConnectionFactory(redisConnectionFactory());
// return redisTemplate;
// }

@Bean
public RedisTemplate<?, ?> redisTemplate() {
RedisTemplate<?, ?> redisTemplate = new RedisTemplate<>();
redisTemplate.setConnectionFactory(redisConnectionFactory());
return redisTemplate;
public RedisTemplate<String, Integer> redisTemplate(RedisConnectionFactory connectionFactory) {
RedisTemplate<String, Integer> template = new RedisTemplate<>();
template.setConnectionFactory(connectionFactory);
template.setKeySerializer(new StringRedisSerializer());
template.setValueSerializer(new GenericToStringSerializer<>(Integer.class));
return template;
}
}
2 changes: 1 addition & 1 deletion src/main/resources/backend-config
20 changes: 11 additions & 9 deletions src/test/java/com/clova/anifriends/AnifriendsApplicationTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,17 @@
@ActiveProfiles("dev")
class AnifriendsApplicationTests {

@BeforeAll
static void beforeAll() {
Properties properties = System.getProperties();
properties.setProperty("ACCESS_TOKEN_SECRET", "_4RNpxi%CB:eoO6a>j=#|*e#$Fp%%aX{dFi%.!Y(ZIy'UMuAt.9.;LxpWn2BZV*");
properties.setProperty("REFRESH_TOKEN_SECRET", "Tlolt.z[e$1yO!%Uc\"F*QH=uf0vp3U5s5{X5=g=*nDZ>BWMIKIf9nzd6et2.:Fb");
}
@BeforeAll
static void beforeAll() {
Properties properties = System.getProperties();
properties.setProperty("ACCESS_TOKEN_SECRET",
"_4RNpxi%CB:eoO6a>j=#|*e#$Fp%%aX{dFi%.!Y(ZIy'UMuAt.9.;LxpWn2BZV*");
properties.setProperty("REFRESH_TOKEN_SECRET",
"Tlolt.z[e$1yO!%Uc\"F*QH=uf0vp3U5s5{X5=g=*nDZ>BWMIKIf9nzd6et2.:Fb");
}

@Test
void contextLoads() {
}
@Test
void contextLoads() {
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import com.clova.anifriends.domain.review.service.ReviewService;
import com.clova.anifriends.domain.shelter.service.ShelterService;
import com.clova.anifriends.domain.volunteer.service.VolunteerService;
import com.clova.anifriends.global.config.RedisConfig;
import com.clova.anifriends.global.config.SecurityConfig;
import com.clova.anifriends.global.config.WebMvcConfig;
import com.clova.anifriends.global.image.S3Service;
Expand All @@ -48,7 +49,8 @@
import org.springframework.web.filter.CharacterEncodingFilter;

@WebMvcTest
@Import({SecurityConfig.class, WebMvcConfig.class, RestDocsConfig.class, WebMvcTestConfig.class})
@Import({SecurityConfig.class, WebMvcConfig.class, RestDocsConfig.class, WebMvcTestConfig.class,
RedisConfig.class})
@ExtendWith(RestDocumentationExtension.class)
public abstract class BaseControllerTest {

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.clova.anifriends.base.config;

import org.springframework.test.context.DynamicPropertyRegistry;
import org.springframework.test.context.DynamicPropertySource;
import org.testcontainers.containers.GenericContainer;
import org.testcontainers.junit.jupiter.Testcontainers;

@Testcontainers
public class RedisTestContainerConfig {

private static final String REDIS_IMAGE = "redis:7.0.8-alpine";
private static final int REDIS_PORT = 6379;
private static final GenericContainer REDIS_CONTAINER;

static {
REDIS_CONTAINER = new GenericContainer(REDIS_IMAGE)
.withExposedPorts(REDIS_PORT)
.withReuse(true);
REDIS_CONTAINER.start();
}

@DynamicPropertySource
private static void registerRedisProperties(DynamicPropertyRegistry registry) {
registry.add("spring.data.redis.host", REDIS_CONTAINER::getHost);
registry.add("spring.data.redis.port", () -> REDIS_CONTAINER.getMappedPort(REDIS_PORT)
.toString());
}
}
4 changes: 4 additions & 0 deletions src/test/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ spring:
hibernate:
format_sql: true
show_sql: true
data:
redis:
host: redis
port: 6379
cloud:
aws:
credentials:
Expand Down

0 comments on commit 4ab2ebb

Please sign in to comment.