Skip to content

Commit

Permalink
Deploy: DB mysql로 마이그레이션 구현 (#171)
Browse files Browse the repository at this point in the history
* deploy: h2 -> mysql 마이그레이션

* test: 내부 저장소를 사용하지 않고 mysql을 사용하도록 수정

* test: 테스트 mock 추가

* git action 테스트에 mysql 정보 주입 및 secret 관리

* deploy: mysql 준비과정까지 대기 aciton 추가:

* deploy: 도커 사용을 위해 root 비밀번호 설정

* deploy: action 로직 개편

* deploy: 설정된 환경변수 스프링 실행 때 삽입

* test: ddl 설정 변경

* test: 테스트에는 트랜잭션 적용
  • Loading branch information
GitJIHO authored Nov 6, 2024
1 parent 6e35123 commit ac992c0
Show file tree
Hide file tree
Showing 22 changed files with 85 additions and 51 deletions.
18 changes: 18 additions & 0 deletions .github/workflows/testcode.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,20 @@ jobs:
--health-interval 10s
--health-timeout 5s
--health-retries 5
mysql:
image: mysql:8.0
ports:
- 3306:3306
env:
MYSQL_DATABASE: ${{ secrets.MYSQL_DATABASE }}
MYSQL_USER: ${{ secrets.MYSQL_USERNAME }}
MYSQL_PASSWORD: ${{ secrets.MYSQL_PASSWORD }}
MYSQL_ROOT_PASSWORD: ${{ secrets.MYSQL_ROOT_PASSWORD }}
options: >-
--health-cmd "mysqladmin ping --silent"
--health-interval 10s
--health-timeout 5s
--health-retries 5
steps:
- name: Checkout code
Expand All @@ -32,5 +46,9 @@ jobs:

- name: Run Tests
env:
SPRING_DATASOURCE_URL: ${{ secrets.MYSQL_URL }}
SPRING_DATASOURCE_USERNAME: ${{ secrets.MYSQL_USERNAME }}
SPRING_DATASOURCE_PASSWORD: ${{ secrets.MYSQL_PASSWORD }}
SPRING_JPA_HIBERNATE_DDL_AUTO: "create"
JWT_SECRET: ${{ secrets.JWT_SECRET }}
run: ./gradlew test
3 changes: 2 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ dependencies {
implementation 'org.springframework.boot:spring-boot-starter-validation'
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation group: 'org.springdoc', name: 'springdoc-openapi-starter-webmvc-ui', version: '2.6.0'
runtimeOnly 'com.h2database:h2'
implementation 'io.jsonwebtoken:jjwt-api:0.11.2'
implementation 'io.jsonwebtoken:jjwt-impl:0.11.2'
implementation 'io.jsonwebtoken:jjwt-jackson:0.11.2'
Expand All @@ -34,6 +33,8 @@ dependencies {
implementation 'org.apache.httpcomponents.client5:httpclient5:5.2.1'
implementation 'org.springframework.boot:spring-boot-starter-actuator'
implementation 'ch.qos.logback:logback-classic:1.4.12'
runtimeOnly 'com.mysql:mysql-connector-j'
testImplementation 'mysql:mysql-connector-java:8.0.33'
}

tasks.named('test') {
Expand Down
9 changes: 3 additions & 6 deletions src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
spring.application.name=sinnitto-example
spring.profiles.include=dev
spring.h2.console.enabled=true
spring.h2.console.path=/h2-console
spring.h2.console.settings.web-allow-others=true
spring.datasource.url=jdbc:h2:mem:testdb
spring.datasource.username=sa
spring.datasource.driver-class-name=org.h2.Driver
spring.jpa.properties.hibernate.format_sql=true
spring.jpa.show-sql=true
logging.level.org.hibernate.orm.jdbc.bind=TRACE
spring.profiles.active=dev
spring.data.redis.host=localhost
spring.data.redis.port=6379
spring.data.redis.password=sinitto
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQLDialect
spring.h2.console.enabled=false
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.transaction.annotation.Transactional;

import static org.hibernate.validator.internal.util.Contracts.assertNotNull;

@SpringBootTest
@Transactional
class SinittoApplicationTests {

@Value("${jwt.secret}")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;

import java.util.Optional;

import static org.assertj.core.api.AssertionsForClassTypes.assertThat;

@DataJpaTest
@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
public class KakaoTokenRepositoryTest {

@Autowired
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,29 +11,24 @@
import org.springframework.beans.factory.annotation.Value;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.Mockito.*;
import static org.junit.jupiter.api.Assertions.*;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.Mockito.when;

@MockitoSettings
public class KakaoApiServiceTest {
@InjectMocks
KakaoApiService kakaoApiService;
@Mock
private HttpServletRequest httpServletRequest;

@Mock
private KakaoProperties kakaoProperties;

@Value("kakao.clientId")
private String clientId;

@Value("kakao.devRedirectUri")
private String devRedirectUri;

@Value("kakao.redirectUri")
private String redirectUri;

@InjectMocks
KakaoApiService kakaoApiService;

@Test
@DisplayName("getAuthorizationUrl 메소드 테스트 - devUri 포홤 시")
void getAuthorizationUrlTestWithDevUri() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
import java.util.Optional;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.Mockito.*;
import static org.junit.jupiter.api.Assertions.*;

@MockitoSettings
public class KakaoTokenServiceTest {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@
import java.util.Base64;

import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.*;
import static org.mockito.Mockito.when;

@MockitoSettings
public class TokenServiceTest {
String key = "thisistestkeynotrealkeythisistestkeynotrealkey";
@Mock
private RedisTemplate<String, Object> redisTemplate;

Expand All @@ -24,8 +25,6 @@ public class TokenServiceTest {

private TokenService tokenService;

String key = "thisistestkeynotrealkeythisistestkeynotrealkey";

@BeforeEach
void setUp() {
MockitoAnnotations.openMocks(this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
Expand All @@ -21,6 +22,7 @@
import static org.junit.jupiter.api.Assertions.*;

@DataJpaTest
@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
class CallbackTest {

@Autowired
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;

import java.time.LocalDateTime;
Expand All @@ -18,6 +19,7 @@
import static org.junit.jupiter.api.Assertions.*;

@DataJpaTest
@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
class CallbackRepositoryTest {

@Autowired
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,34 @@ class CallbackServiceTest {
@InjectMocks
CallbackService callbackService;

@Test
@DisplayName("보호자의 콜백 요청 내역 조회 테스트")
void getCallbackHistoryOfGuard() {
// given
Long memberId = 1L;
Member member = mock(Member.class);
Senior senior = mock(Senior.class);
List<Senior> seniors = List.of(senior);
Callback callback = mock(Callback.class);
Page<Callback> callbackPage = new PageImpl<>(List.of(callback));
Pageable pageable = PageRequest.of(0, 10);

when(memberRepository.findById(memberId)).thenReturn(Optional.of(member));
when(seniorRepository.findAllByMember(member)).thenReturn(seniors);
when(callbackRepository.findAllBySeniorIn(seniors, pageable)).thenReturn(callbackPage);
when(callback.getId()).thenReturn(1L);
when(callback.getSeniorName()).thenReturn("SeniorName");
when(callback.getPostTime()).thenReturn(LocalDateTime.now());
when(callback.getStatus()).thenReturn(Callback.Status.WAITING.name());

// when
Page<CallbackUsageHistoryResponse> result = callbackService.getCallbackHistoryOfGuard(memberId, pageable);

// then
assertEquals(1, result.getContent().size());
assertEquals("SeniorName", result.getContent().getFirst().seniorName());
}

@Nested
@DisplayName("대기 상태인 콜백 조회 테스트")
class GetWaitingCallbackTest {
Expand Down Expand Up @@ -501,32 +529,4 @@ void getCallbackForSinitto4() {
assertEquals("", result.seniorPhoneNumber());
}
}

@Test
@DisplayName("보호자의 콜백 요청 내역 조회 테스트")
void getCallbackHistoryOfGuard() {
// given
Long memberId = 1L;
Member member = mock(Member.class);
Senior senior = mock(Senior.class);
List<Senior> seniors = List.of(senior);
Callback callback = mock(Callback.class);
Page<Callback> callbackPage = new PageImpl<>(List.of(callback));
Pageable pageable = PageRequest.of(0, 10);

when(memberRepository.findById(memberId)).thenReturn(Optional.of(member));
when(seniorRepository.findAllByMember(member)).thenReturn(seniors);
when(callbackRepository.findAllBySeniorIn(seniors, pageable)).thenReturn(callbackPage);
when(callback.getId()).thenReturn(1L);
when(callback.getSeniorName()).thenReturn("SeniorName");
when(callback.getPostTime()).thenReturn(LocalDateTime.now());
when(callback.getStatus()).thenReturn(Callback.Status.WAITING.name());

// when
Page<CallbackUsageHistoryResponse> result = callbackService.getCallbackHistoryOfGuard(memberId, pageable);

// then
assertEquals(1, result.getContent().size());
assertEquals("SeniorName", result.getContent().getFirst().seniorName());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;

import static org.assertj.core.api.AssertionsForClassTypes.assertThat;

@DataJpaTest
@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
public class SeniorRepositoryTest {
@Autowired
private SeniorRepository seniorRepository;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
import java.util.Optional;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.Mockito.*;
import static org.junit.jupiter.api.Assertions.*;

@MockitoSettings
public class GuardServiceTest {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;

import java.util.List;

import static org.junit.jupiter.api.Assertions.*;

@DataJpaTest
@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
class GuardGuidelineRepositoryTest {

@Autowired
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.Mockito.*;
import static org.junit.jupiter.api.Assertions.*;

@MockitoSettings
public class GuardGuildelineServiceTest {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;

import java.time.LocalDate;
Expand All @@ -21,6 +22,7 @@
import static org.assertj.core.api.Assertions.assertThat;

@DataJpaTest
@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
class HelloCallRepositoryTest {

@Autowired
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;

import java.time.LocalDate;
Expand All @@ -24,6 +25,7 @@
import static org.assertj.core.api.Assertions.assertThat;

@DataJpaTest
@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
class HelloCallTimeLogRepositoryTest {

@Autowired
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;

import java.time.LocalDate;
Expand All @@ -21,6 +22,7 @@
import static org.assertj.core.api.Assertions.assertThat;

@DataJpaTest
@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
class TimeSlotRepositoryTest {

@Autowired
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;

import java.util.Arrays;
Expand All @@ -13,6 +14,7 @@
import static org.assertj.core.api.Assertions.assertThat;

@DataJpaTest
@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
public class MemberRepositoryTest {

@Autowired
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import org.mockito.junit.jupiter.MockitoSettings;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;

import java.util.Optional;

Expand All @@ -36,6 +37,8 @@ public class MemberServiceTest {
private HashOperations<String, Object, Object> hashOperations;
@InjectMocks
MemberService memberService;
@Mock
private ValueOperations<String, String> valueOperations;

@Test
@DisplayName("getMemberIdByToken 메소드 테스트")
Expand Down
Loading

0 comments on commit ac992c0

Please sign in to comment.