Skip to content

Commit

Permalink
[#54] refactor: 롬복 의존성 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
kwj1270 committed Nov 17, 2021
1 parent 3427af2 commit 30ff775
Show file tree
Hide file tree
Showing 12 changed files with 60 additions and 67 deletions.
17 changes: 14 additions & 3 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,36 @@ repositories {
}

dependencies {
// basic
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-security'
implementation 'org.springframework.boot:spring-boot-starter-validation'
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
compileOnly 'org.projectlombok:lombok'
annotationProcessor 'org.projectlombok:lombok'

// query dsl
implementation 'com.querydsl:querydsl-jpa'
implementation 'com.github.gavlyukovskiy:p6spy-spring-boot-starter:1.5.8'

// database
runtimeOnly 'com.h2database:h2'
testImplementation 'org.springframework.boot:spring-boot-starter-test'

// test
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testImplementation 'io.rest-assured:rest-assured:4.4.0'
testImplementation 'io.rest-assured:json-path:4.4.0'
testImplementation 'io.rest-assured:spring-mock-mvc:4.4.0'
testImplementation 'org.springframework.restdocs:spring-restdocs-mockmvc'
asciidoctor 'org.springframework.restdocs:spring-restdocs-asciidoctor'

// jwt
compile group: 'io.jsonwebtoken', name: 'jjwt-api', version: '0.11.2'
runtimeOnly group: 'io.jsonwebtoken', name: 'jjwt-impl', version: '0.11.2'
runtimeOnly group: 'io.jsonwebtoken', name: 'jjwt-jackson', version: '0.11.2'
}

apply from: 'test.gradle'
apply from: 'jacoco.gradle'

def querydslDir = "$buildDir/generated/querydsl"
querydsl {
Expand Down Expand Up @@ -68,7 +79,7 @@ asciidoctor {

bootJar {
dependsOn asciidoctor
from ("${asciidoctor.outputDir}/html5") { // gradle은 src/docs/asciidoc 에 메인 adoc생성!
from("${asciidoctor.outputDir}/html5") { // gradle은 src/docs/asciidoc 에 메인 adoc생성!
into 'static/docs' // asciidoctor로 만든 문서는 static/docs 디렉토리로.!
}
}
Expand Down
File renamed without changes.
14 changes: 7 additions & 7 deletions src/main/java/com/study/realworld/domain/follow/TestAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import com.study.realworld.domain.follow.application.FollowService;
import com.study.realworld.domain.follow.domain.FollowQueryDslRepository;
import com.study.realworld.domain.follow.dto.FollowableDto;
import com.study.realworld.domain.follow.dto.FollowResponse;
import com.study.realworld.domain.user.domain.persist.User;
import com.study.realworld.domain.user.domain.persist.UserRepository;
import org.springframework.http.ResponseEntity;
Expand All @@ -27,20 +27,20 @@ public TestAPI(final FollowQueryDslRepository queryDslRepository, final UserRepo
}

@PostMapping("follow/{targetId}")
public ResponseEntity<FollowableDto> follow(@AuthenticationPrincipal Long id, @PathVariable Long targetId) {
public ResponseEntity<FollowResponse> follow(@AuthenticationPrincipal Long id, @PathVariable Long targetId) {
User user = userRepository.findById(id).get();
User user1 = userRepository.findById(targetId).get();
followService.following(id, targetId);
FollowableDto followableDto = queryDslRepository.existMeAndFollowing(user, user1);
return ResponseEntity.ok().body(followableDto);
FollowResponse followResponse = queryDslRepository.existMeAndFollowing(user, user1);
return ResponseEntity.ok().body(followResponse);
}

@GetMapping("test/{targetId}")
public ResponseEntity<FollowableDto> test(@AuthenticationPrincipal Long id, @PathVariable Long targetId) {
public ResponseEntity<FollowResponse> test(@AuthenticationPrincipal Long id, @PathVariable Long targetId) {
User user = userRepository.findById(id).get();
User user1 = userRepository.findById(targetId).get();
FollowableDto followableDto = queryDslRepository.existMeAndFollowing(user, user1);
return ResponseEntity.ok().body(followableDto);
FollowResponse followResponse = queryDslRepository.existMeAndFollowing(user, user1);
return ResponseEntity.ok().body(followResponse);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import com.study.realworld.domain.follow.domain.Follow;
import com.study.realworld.domain.follow.domain.FollowQueryDslRepository;
import com.study.realworld.domain.follow.domain.FollowRepository;
import com.study.realworld.domain.follow.dto.FollowableDto;
import com.study.realworld.domain.user.domain.persist.User;
import com.study.realworld.domain.user.domain.persist.UserRepository;
import com.study.realworld.domain.user.error.exception.IdentityNotFoundException;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import com.querydsl.core.types.Projections;
import com.querydsl.jpa.JPAExpressions;
import com.querydsl.jpa.impl.JPAQueryFactory;
import com.study.realworld.domain.follow.dto.FollowableDto;
import com.study.realworld.domain.follow.dto.FollowResponse;
import com.study.realworld.domain.user.domain.persist.User;
import org.springframework.stereotype.Repository;

Expand All @@ -19,8 +19,8 @@ public FollowQueryDslRepository(final JPAQueryFactory jpaQueryFactory) {
this.query = jpaQueryFactory;
}

public FollowableDto existMeAndFollowing(final User me, final User following) {
return query.select(Projections.constructor(FollowableDto.class, user,
public FollowResponse existMeAndFollowing(final User me, final User following) {
return query.select(Projections.constructor(FollowResponse.class, user,
ExpressionUtils.isNotNull(JPAExpressions.selectOne()
.from(follow)
.where(follow.following.eq(following), follow.follower.eq(me)))))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import com.study.realworld.domain.user.domain.vo.Image;
import com.study.realworld.domain.user.domain.vo.Name;

public class FollowableDto {
public class FollowResponse {

@JsonProperty("username")
private Name username;
Expand All @@ -20,21 +20,14 @@ public class FollowableDto {
@JsonProperty("following")
private Boolean followable;

public static FollowableDto fromUserAndFollowable(final User me, final boolean followable) {
return new FollowableDto(me.username(), me.bio(), me.image(), followable);
FollowResponse() {
}

FollowableDto() {
public FollowResponse(final User me, final boolean followable) {
this(me.username(), me.bio(), me.image(), followable);
}

public FollowableDto(final User me, final boolean followable) {
this.username = me.username();
this.bio = me.bio();
this.image = me.image();
this.followable = followable;
}

private FollowableDto(final Name username, final Bio bio, final Image image, final Boolean followable) {
private FollowResponse(final Name username, final Bio bio, final Image image, final Boolean followable) {
this.username = username;
this.bio = bio;
this.image = image;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,34 +1,27 @@
package com.study.realworld.domain.user.application;

import com.study.realworld.domain.follow.domain.FollowRepository;
import com.study.realworld.domain.user.domain.vo.Email;
import com.study.realworld.domain.user.domain.persist.User;
import com.study.realworld.domain.user.domain.persist.UserRepository;
import com.study.realworld.domain.user.domain.vo.Email;
import com.study.realworld.domain.user.error.exception.EmailNotFoundException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Transactional(readOnly = true)
@Service
public class UserFindService {
public class UserQueryService {

private final UserRepository userRepository;
private final FollowRepository followRepository;

public UserFindService(final UserRepository userRepository, final FollowRepository followRepository) {
public UserQueryService(final UserRepository userRepository, final FollowRepository followRepository) {
this.userRepository = userRepository;
this.followRepository = followRepository;
}

public User findUserByEmail(final String email) {
return findByEmail(email);
}

// public User findProfileByName(final Long id, final Name username) {
// final User user = userRepository.findById(id).orElseThrow(IllegalArgumentException::new);
// return findByEmail();
// }

private User findByEmail(final String email) {
return userRepository.findByEmail(new Email(email))
.orElseThrow(() -> new EmailNotFoundException(email));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.study.realworld.global.jwt.authentication;

import com.study.realworld.domain.auth.infrastructure.TokenProvider;
import com.study.realworld.domain.user.application.UserFindService;
import com.study.realworld.domain.user.application.UserQueryService;
import com.study.realworld.domain.user.domain.persist.User;
import com.study.realworld.global.security.error.exception.UserDetailsNullPointerException;
import org.springframework.security.authentication.AuthenticationProvider;
Expand All @@ -14,11 +14,11 @@
@Component
public class JwtAuthenticationProvider implements AuthenticationProvider {

private final UserFindService userFindService;
private final UserQueryService userQueryService;
private final TokenProvider tokenProvider;

public JwtAuthenticationProvider(final UserFindService userFindService, final TokenProvider tokenProvider) {
this.userFindService = userFindService;
public JwtAuthenticationProvider(final UserQueryService userQueryService, final TokenProvider tokenProvider) {
this.userQueryService = userQueryService;
this.tokenProvider = tokenProvider;
}

Expand All @@ -35,7 +35,7 @@ private String determineUsername(final Authentication authentication) {
}

private User retrieveUser(final String email) {
final User user = userFindService.findUserByEmail(email);
final User user = userQueryService.findUserByEmail(email);
validateUserDetailsNull(user);
return user;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
package com.study.realworld.domain.follow.domain;

import com.querydsl.jpa.impl.JPAQueryFactory;
import com.study.realworld.domain.follow.dto.FollowableDto;
import com.study.realworld.domain.follow.dto.FollowResponse;
import com.study.realworld.domain.user.domain.persist.User;
import com.study.realworld.domain.user.domain.vo.*;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;

Expand Down Expand Up @@ -59,11 +56,11 @@ void existMeAndFollowing_test() {

// when
final FollowQueryDslRepository followQueryDslRepository = new FollowQueryDslRepository(jpaQueryFactory);
final FollowableDto followableDto = followQueryDslRepository.existMeAndFollowing(user, other);
final FollowResponse followResponse = followQueryDslRepository.existMeAndFollowing(user, other);

assertAll(
() -> assertThat(followableDto).isNotNull(),
() -> assertThat(followableDto).isExactlyInstanceOf(FollowableDto.class)
() -> assertThat(followResponse).isNotNull(),
() -> assertThat(followResponse).isExactlyInstanceOf(FollowResponse.class)
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,16 @@
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.*;

class FollowableDtoTest {
class FollowResponseTest {

@DisplayName("FollowableDto 기본 생성자 테스트")
@Test
void default_constructor_test() {
final FollowableDto followableDto = new FollowableDto();
final FollowResponse followResponse = new FollowResponse();

assertAll(
() -> assertThat(followableDto).isNotNull(),
() -> assertThat(followableDto).isExactlyInstanceOf(FollowableDto.class)
() -> assertThat(followResponse).isNotNull(),
() -> assertThat(followResponse).isExactlyInstanceOf(FollowResponse.class)
);
}

Expand All @@ -32,11 +32,11 @@ void default_constructor_test() {
void fromUserAndFollowable_test() {
final User user = userBuilder(new Email(EMAIL), new Name(USERNAME), new Password(PASSWORD), new Bio(BIO), new Image(IMAGE));
final boolean followable = false;
final FollowableDto followableDto = FollowableDto.fromUserAndFollowable(user, followable);
final FollowResponse followResponse = new FollowResponse(user, followable);

assertAll(
() -> assertThat(followableDto).isNotNull(),
() -> assertThat(followableDto).isExactlyInstanceOf(FollowableDto.class)
() -> assertThat(followResponse).isNotNull(),
() -> assertThat(followResponse).isExactlyInstanceOf(FollowResponse.class)
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,20 @@
import static org.mockito.BDDMockito.given;

@ExtendWith(MockitoExtension.class)
public class UserFindServiceTest {
public class UserQueryServiceTest {

@Mock private UserRepository userRepository;
@Mock private FollowRepository followRepository;
@InjectMocks private UserFindService userFindService;
@InjectMocks private UserQueryService userQueryService;

@DisplayName("JwtUserDetailsService 인스턴스 생성자 테스트")
@Test
void constructor_test() {
final UserFindService userFindService = new UserFindService(userRepository, followRepository);
final UserQueryService userQueryService = new UserQueryService(userRepository, followRepository);

assertAll(
() -> assertThat(userFindService).isNotNull(),
() -> assertThat(userFindService).isInstanceOf(UserFindService.class)
() -> assertThat(userQueryService).isNotNull(),
() -> assertThat(userQueryService).isInstanceOf(UserQueryService.class)
);
}

Expand All @@ -50,7 +50,7 @@ void findByEmail_test() {
final User user = userBuilder(new Email(EMAIL), new Name(USERNAME), new Password(PASSWORD), new Bio(BIO), new Image(IMAGE));
given(userRepository.findByEmail(any())).willReturn(Optional.ofNullable(user));

final User findUser = userFindService.findUserByEmail(EMAIL);
final User findUser = userQueryService.findUserByEmail(EMAIL);
assertAll(
() -> assertThat(findUser.email().email()).isEqualTo(EMAIL),
() -> assertThat(findUser.password().password()).isEqualTo(PASSWORD)
Expand All @@ -62,7 +62,7 @@ void findByEmail_test() {
void fail_findByEmail_test() {
given(userRepository.findByEmail(any())).willReturn(Optional.ofNullable(null));

assertThatThrownBy(() -> userFindService.findUserByEmail(EMAIL))
assertThatThrownBy(() -> userQueryService.findUserByEmail(EMAIL))
.isInstanceOf(EmailNotFoundException.class)
.hasMessage(String.format("이메일 : [ %s ] 를 찾을 수 없습니다.", EMAIL));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.study.realworld.global.jwt;

import com.study.realworld.domain.auth.infrastructure.TokenProvider;
import com.study.realworld.domain.user.application.UserFindService;
import com.study.realworld.domain.user.application.UserQueryService;
import com.study.realworld.domain.user.domain.persist.User;
import com.study.realworld.domain.user.domain.vo.*;
import com.study.realworld.global.jwt.authentication.JwtAuthentication;
Expand Down Expand Up @@ -36,13 +36,13 @@
class JwtAuthenticationProviderTest {

@Mock private TokenProvider tokenProvider;
@Mock private UserFindService userFindService;
@Mock private UserQueryService userQueryService;
@InjectMocks private JwtAuthenticationProvider jwtAuthenticationProvider;

@DisplayName("JwtAuthenticationProvider 인스턴스 생성자 테스트")
@Test
void constructor_test() {
final JwtAuthenticationProvider jwtAuthenticationProvider = new JwtAuthenticationProvider(userFindService, tokenProvider);
final JwtAuthenticationProvider jwtAuthenticationProvider = new JwtAuthenticationProvider(userQueryService, tokenProvider);

assertAll(
() -> assertThat(jwtAuthenticationProvider).isNotNull(),
Expand All @@ -60,7 +60,7 @@ void authenticate_test() {
final User user = userBuilder(email, new Name(USERNAME), password, new Bio(BIO), new Image(IMAGE));
ReflectionTestUtils.setField(user, "id", 1L);
doReturn(USERNAME).when(tokenProvider).mapToUsername(any());
doReturn(user).when(userFindService).findUserByEmail(any());
doReturn(user).when(userQueryService).findUserByEmail(any());

final JwtAuthentication jwtAuthentication = initAuthentication(testToken());
final Authentication authenticate = jwtAuthenticationProvider.authenticate(jwtAuthentication);
Expand Down Expand Up @@ -88,7 +88,7 @@ void supports_test() {
@Test
void validateUserDetailsNull_test() {
doReturn(USERNAME).when(tokenProvider).mapToUsername(any());
doReturn(null).when(userFindService).findUserByEmail(any());
doReturn(null).when(userQueryService).findUserByEmail(any());

final JwtAuthentication jwtAuthentication = initAuthentication(testToken());
assertThatThrownBy(() -> jwtAuthenticationProvider.authenticate(jwtAuthenticationProvider.authenticate(jwtAuthentication)))
Expand Down

0 comments on commit 30ff775

Please sign in to comment.