Skip to content

Commit

Permalink
Merge branch 'dev' into NO-JIRA--junhyuk--test--card-service-test
Browse files Browse the repository at this point in the history
  • Loading branch information
hi-june authored Jan 30, 2024
2 parents 5c62689 + e0e3a32 commit 2cf809e
Show file tree
Hide file tree
Showing 25 changed files with 395 additions and 363 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/CICD.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ jobs:
# application.properties를 프로젝트에 포함
- name: Make application.properties
run: |
cd ./src/main/resources
cd nabimarket-api/src/main/resources
touch ./application.properties
echo "${{ secrets.DATABASE_PROPERTIES }}" >> ./application.properties
Expand Down
8 changes: 1 addition & 7 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -204,14 +204,8 @@ gradle-app.setting
# Java heap dump
*.hprof

nabimarket-api/src/main/resources/application-test.yml
nabimarket-api/src/main/resources/application-prod.yml
nabimarket-api/src/main/resources/application-local.yml
nabimarket-api/src/main/resources/application-jwt.yml
nabimarket-api/src/main/resources/application-dev.yml
nabimarket-api/src/main/resources/firebase/*

*.yml
!nabimarket-api/src/main/resources/application.yml
nabimarket-api/src/main/resources/firebase/*

# End of https://www.toptal.com/developers/gitignore/api/macos,java,gradle,intellij
13 changes: 11 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@ repositories {
mavenCentral()
}

bootJar {
enabled = false
}
jar {
enabled = true
}

// 각 모듈에 적용할 공통 설정
subprojects {
group = 'org.prgrms'
Expand Down Expand Up @@ -37,11 +44,13 @@ subprojects {
implementation 'org.springframework.boot:spring-boot-starter-web'

// test
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.8.1'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.8.1'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}

// gradle 빌드시에는 각 프로젝트를 실행가능한 jar형태로 만들게 되는데, module-common 프로젝트의 경우 main메소드가 없음
// 단순히 참조용 클래스들만 있는 프로젝트를 위해 아래와 같은 설정(bootJar, jar)들을 추가함
// // gradle 빌드시에는 각 프로젝트를 실행가능한 jar형태로 만들게 되는데, module-common 프로젝트의 경우 main메소드가 없음
// // 단순히 참조용 클래스들만 있는 프로젝트를 위해 아래와 같은 설정(bootJar, jar)들을 추가함
bootJar {
enabled = false
}
Expand Down
8 changes: 8 additions & 0 deletions nabimarket-api/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,11 @@ dependencies {
// security
implementation 'org.springframework.boot:spring-boot-starter-security'
}


bootJar {
enabled = true
}
jar {
enabled = true
}
11 changes: 8 additions & 3 deletions nabimarket-application/build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
dependencies {
testImplementation platform('org.junit:junit-bom:5.9.1')
testImplementation platform('org.junit:junit-bom:5.9.2')
testImplementation 'org.junit.jupiter:junit-jupiter'
// swagger
implementation 'org.springdoc:springdoc-openapi-ui:1.6.15'
Expand All @@ -21,8 +21,13 @@ dependencies {
implementation 'org.springframework.boot:spring-boot-starter-security'
}

bootJar {
enabled = false
}
jar {
enabled = true
}

task wrapper(type: Wrapper) {
gradleVersion = '8.3'
}

tasks.register("prepareKotlinBuildScriptModel") {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package setup.jwt;

import org.prgrms.nabimarketbe.jwt.dto.TokenResponseDTO;

public class TokenResponseDTOBuilder {
public static TokenResponseDTO createTestDTO() {
return TokenResponseDTO.builder()
.grantType("Bearer")
.accessToken("testAccessToken")
.refreshToken("testRefreshToken")
.accessTokenExpireDate(60 * 60 * 1000L)
.build();
}
}
16 changes: 16 additions & 0 deletions nabimarket-application/src/test/java/setup/user/UserBuilder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package setup.user;

import org.prgrms.nabimarketbe.user.entity.Role;
import org.prgrms.nabimarketbe.user.entity.User;

public class UserBuilder {
public static User createTestEntity() {
return User.builder()
.accountId("testAccountId")
.nickname("testNickname")
.role(Role.USER.getKey())
.imageUrl("testImageUrl")
.provider("KAKAO")
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package setup.user.request;

import org.prgrms.nabimarketbe.user.dto.request.SocialUserInfoDTO;

public class SocialUserInfoDTOBuilder {
public static SocialUserInfoDTO createTestDTO() {
return SocialUserInfoDTO.builder()
.accountId("testAccount")
.provider("KAKAO")
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package setup.user.request;

import org.prgrms.nabimarketbe.user.dto.request.UserNicknameUpdateRequestDTO;

public class UserNicknameUpdateRequestDTOBuilder {
public static UserNicknameUpdateRequestDTO createTestDTO() {
return new UserNicknameUpdateRequestDTO("testUpdateNickname");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package setup.user.request;

import org.prgrms.nabimarketbe.user.dto.request.UserProfileUpdateRequestDTO;

public class UserProfileUpdateRequestDTOBuilder {
public static UserProfileUpdateRequestDTO createTestDTO() {
return new UserProfileUpdateRequestDTO("testImageUrl");
}
}
71 changes: 71 additions & 0 deletions nabimarket-application/src/test/java/user/CheckServiceTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package user;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.BDDMockito.given;

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.prgrms.nabimarketbe.jwt.provider.JwtProvider;
import org.prgrms.nabimarketbe.user.service.CheckService;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.impl.DefaultClaims;

@ExtendWith(MockitoExtension.class)
class CheckServiceTest {
@InjectMocks
CheckService checkService;

@Mock
JwtProvider jwtProvider;

@DisplayName("parseToken 메소드를 호출하면 유저의 Id가 파싱된다.")
@Test
void parseToken_test() {
// given
Long expectedUserId = 1L;
String token = "1L";
Claims claims = new DefaultClaims();
claims.put("sub", expectedUserId);

given(jwtProvider.parseClaims(any())).willReturn(claims);

// when
Long userId = checkService.parseToken(token);

// then
assertThat(userId).isEqualTo(expectedUserId);
}

@DisplayName("String(유저토큰)과 Long(유저Id)이 같은지 비교한다.")
@Test
void isEqual_String_Long_test() {
// given
Long expectedUserId = 1L;
String token = "1L";
Claims claims = new DefaultClaims();
claims.put("sub", expectedUserId);

given(jwtProvider.parseClaims(any())).willReturn(claims);

// when & then
assertThat(checkService.isEqual(token, expectedUserId)).isTrue();
}

@DisplayName("Long(유저1 Id)과 Long(유저2 Id)이 같은지 비교한다.")
@Test
void isEqual_Long_Long_test() {
// given
Long userOneId = 1L;
Long userOneId2 = 1L;

// when & then
assertThat(checkService.isEqual(userOneId, userOneId2)).isTrue();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package user;

import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.prgrms.nabimarketbe.user.service.RandomNicknameGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.context.annotation.Import;
import org.springframework.retry.annotation.EnableRetry;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.springframework.web.client.RestTemplate;

import com.fasterxml.jackson.databind.ObjectMapper;

@ExtendWith(SpringExtension.class)
@Import({RandomNicknameGenerator.class, ObjectMapper.class})
@EnableRetry
class RandomNicknameGeneratorTest {
@Autowired
RandomNicknameGenerator randomNicknameGenerator;

@MockBean
RestTemplate restTemplate;

@DisplayName("랜덤닉네임 생성 시에 RuntimeException 발생시 2번까지 재시도한다.")
@Test
void generateRandomNickname_with_retry() {
// given
doThrow(new RuntimeException("Intended Retry Error")).when(restTemplate)
.getForObject(anyString(), eq(String.class));

// then
assertThrows(RuntimeException.class, () -> {
randomNicknameGenerator.generateRandomNickname();
});
verify(restTemplate, times(2)).getForObject(anyString(), eq(String.class));
}
}
59 changes: 59 additions & 0 deletions nabimarket-application/src/test/java/user/SignServiceTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package user;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.BDDMockito.given;

import java.util.Optional;

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.prgrms.nabimarketbe.jwt.dto.TokenResponseDTO;
import org.prgrms.nabimarketbe.jwt.provider.JwtProvider;
import org.prgrms.nabimarketbe.user.dto.request.SocialUserInfoDTO;
import org.prgrms.nabimarketbe.user.dto.response.UserGetResponseDTO;
import org.prgrms.nabimarketbe.user.dto.response.UserLoginResponseDTO;
import org.prgrms.nabimarketbe.user.entity.User;
import org.prgrms.nabimarketbe.user.repository.UserRepository;
import org.prgrms.nabimarketbe.user.service.SignService;

import setup.jwt.TokenResponseDTOBuilder;
import setup.user.UserBuilder;
import setup.user.request.SocialUserInfoDTOBuilder;

@ExtendWith(MockitoExtension.class)
class SignServiceTest {
@InjectMocks
SignService signService;

@Mock
UserRepository userRepository;

@Mock
JwtProvider jwtProvider;

@DisplayName("사용자는 소셜로그인 이후 받아온 정보로 나비장터에 로그인을 할 수 있다.")
@Test
void signIn_test() {
// given
SocialUserInfoDTO socialUserInfoDTO = SocialUserInfoDTOBuilder.createTestDTO();
User user = UserBuilder.createTestEntity();
TokenResponseDTO tokenResponseDTO = TokenResponseDTOBuilder.createTestDTO();
UserGetResponseDTO userGetResponseDTO = UserGetResponseDTO.from(user);

given(userRepository.findByAccountId(any())).willReturn(Optional.ofNullable(user));
given(jwtProvider.createTokenDTO(any(), anyString())).willReturn(tokenResponseDTO);

// when
UserLoginResponseDTO loginResponseDTO = signService.signIn(socialUserInfoDTO);

// then
assertThat(loginResponseDTO.userInfo()).usingRecursiveComparison().isEqualTo(userGetResponseDTO);
assertThat(loginResponseDTO.token()).usingRecursiveComparison().isEqualTo(tokenResponseDTO);
}
}
Loading

0 comments on commit 2cf809e

Please sign in to comment.