Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BE] 인수 테스트 에서 사용하는 레포지토리 제거 및 설정 명확하게 변경 (#512) #522

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,8 @@ public record GithubPullRequestReview(

@JsonProperty("html_url")
String html_url
){
) {
public boolean isEqualGithubUserId(String githubUserId) {
return user.id().equals(githubUserId);
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package corea.auth.service;

import corea.auth.dto.GithubPullRequestReview;
import corea.auth.dto.GithubUserInfo;
import corea.auth.infrastructure.GithubOAuthClient;
import corea.room.domain.PullRequestReviews;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

Expand All @@ -17,7 +17,7 @@ public GithubUserInfo getUserInfo(String code) {
return githubOAuthClient.getUserInfo(accessToken);
}

public GithubPullRequestReview[] getPullRequestReview(String prLink) {
return githubOAuthClient.getReviewLink(prLink);
public PullRequestReviews getPullRequestReview(String prLink) {
return new PullRequestReviews(githubOAuthClient.getReviewLink(prLink));
}
}
2 changes: 2 additions & 0 deletions backend/src/main/java/corea/exception/ExceptionType.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ public enum ExceptionType {
INVALID_CALCULATION_FORMULA(HttpStatus.BAD_REQUEST, "잘못된 계산식입니다."),
INVALID_VALUE(HttpStatus.BAD_REQUEST, "올바르지 않은 값입니다."),
INVALID_RECRUITMENT_DEADLINE(HttpStatus.BAD_REQUEST, "올바르지 않은 모집 마감 시간입니다."),
NOT_EXIST_GITHUB_REVIEW(HttpStatus.BAD_REQUEST, "깃허브 리뷰가 존재하지 않습니다."),

INVALID_REVIEW_DEADLINE(HttpStatus.BAD_REQUEST, "올바르지 않은 리뷰 마감 시간입니다."),
INVALID_PULL_REQUEST_URL(HttpStatus.BAD_REQUEST, "올바르지 않은 풀 리퀘스트 주소입니다."),

Expand Down
15 changes: 6 additions & 9 deletions backend/src/main/java/corea/review/service/ReviewService.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import corea.matching.repository.MatchResultRepository;
import corea.member.domain.Member;
import corea.member.repository.MemberRepository;
import corea.room.domain.PullRequestReviews;
import lombok.RequiredArgsConstructor;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
Expand Down Expand Up @@ -39,18 +40,14 @@ public void completeReview(long roomId, long reviewerId, long revieweeId) {
private void updateReviewLink(MatchResult matchResult, long reviewerId) {
Member reviewer = memberRepository.findById(reviewerId)
.orElseThrow(() -> new CoreaException(ExceptionType.MEMBER_NOT_FOUND, String.format("%d에 해당하는 멤버가 없습니다.", reviewerId)));
String userName = reviewer.getUsername();
String newReviewLink = findReviewLink(userName, matchResult.getPrLink());
String githubUserId = reviewer.getGithubUserId();
String newReviewLink = findReviewLink(githubUserId, matchResult.getPrLink());
matchResult.updateReviewLink(newReviewLink);
}

private String findReviewLink(String userName, String prLink) {
GithubPullRequestReview[] githubPullRequestReviews = githubOAuthProvider.getPullRequestReview(prLink);
return Stream.of(githubPullRequestReviews)
.filter(review -> review.user().login().equals(userName))
.findFirst()
.map(GithubPullRequestReview::html_url)
.orElse(prLink);
private String findReviewLink(String githubUserId, String prLink) {
PullRequestReviews pullRequestReviews = githubOAuthProvider.getPullRequestReview(prLink);
return pullRequestReviews.getReviewUrl(githubUserId);
}

private MatchResult getMatchResult(long roomId, long reviewerId, long revieweeId) {
Expand Down
17 changes: 17 additions & 0 deletions backend/src/main/java/corea/room/domain/PullRequestReviews.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package corea.room.domain;

import corea.auth.dto.GithubPullRequestReview;
import corea.exception.CoreaException;
import corea.exception.ExceptionType;

import java.util.Arrays;

public record PullRequestReviews(GithubPullRequestReview[] pullRequestReviews) {
public String getReviewUrl(String githubUserId) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
public String getReviewUrl(String githubUserId) {
public String getReviewUrl(String githubUserId) {

return Arrays.stream(pullRequestReviews)
.filter(githubPullRequestReview -> githubPullRequestReview.isEqualGithubUserId(githubUserId))
.findFirst()
.map(GithubPullRequestReview::html_url)
.orElseThrow(() -> new CoreaException(ExceptionType.NOT_EXIST_GITHUB_REVIEW));
}
}
27 changes: 27 additions & 0 deletions backend/src/test/java/corea/bdd/MatchingGiven.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package corea.bdd;

import corea.matching.dto.MatchResultResponse;
import corea.matching.dto.MatchResultResponses;
import io.restassured.RestAssured;
import io.restassured.http.ContentType;

import java.util.List;

public class MatchingGiven {
public static void 매칭(String accessToken, long roomId) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
public static void 매칭(String accessToken, long roomId) {
public static void 매칭(String accessToken, long roomId) {

//@formatter:off
RestAssured.given().auth().oauth2(accessToken).contentType(ContentType.JSON)
.when().post("/rooms/"+roomId+"/matching")
.then().assertThat().statusCode(200);
//@formatter:on
}

public static List<MatchResultResponse> 리뷰이_목록_조회(String accessToken, long roomId) {
//@formatter:off
MatchResultResponses responses = RestAssured.given().auth().oauth2(accessToken).contentType(ContentType.JSON)
.when().get("/rooms/"+roomId+"/reviewees")
.then().assertThat().statusCode(200).extract().as(MatchResultResponses.class);
//@formatter:on
return responses.matchResultResponses();
}
}
29 changes: 29 additions & 0 deletions backend/src/test/java/corea/bdd/MemberGiven.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package corea.bdd;

import corea.auth.dto.GithubUserInfo;
import corea.auth.dto.LoginRequest;
import corea.auth.service.GithubOAuthProvider;
import corea.member.domain.Member;
import io.restassured.RestAssured;
import io.restassured.http.ContentType;
import org.mockito.Mockito;

public class MemberGiven {

public static String 멤버_로그인(GithubOAuthProvider githubOAuthProvider, Member member) {
String mockingCode = member.getGithubUserId();
Mockito.when(githubOAuthProvider.getUserInfo(mockingCode))
.thenReturn(new GithubUserInfo(
member.getUsername(),
member.getName(),
member.getThumbnailUrl(),
member.getEmail(),
member.getGithubUserId()
));
//@formatter:off
return RestAssured.given().body(new LoginRequest(mockingCode)).contentType(ContentType.JSON)
.when().post("/login")
.then().statusCode(200).extract().header("Authorization");
//@formatter:on
}
}
15 changes: 15 additions & 0 deletions backend/src/test/java/corea/bdd/ParticipationGiven.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package corea.bdd;

import io.restassured.RestAssured;
import io.restassured.http.ContentType;

public class ParticipationGiven {

public static void 방_참가(String accessToken, long roomId) {
//@formatter:off
RestAssured.given().auth().oauth2(accessToken).contentType(ContentType.JSON)
.when().post("/participate/"+roomId)
.then().assertThat().statusCode(200);
//@formatter:on
}
}
15 changes: 15 additions & 0 deletions backend/src/test/java/corea/bdd/ReviewGiven.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package corea.bdd;

import corea.review.dto.ReviewRequest;
import io.restassured.RestAssured;
import io.restassured.http.ContentType;

public class ReviewGiven {
public static void 리뷰_완료(String accessToken, long roomId, long revieweeId) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
public static void 리뷰_완료(String accessToken, long roomId, long revieweeId) {
public static void 리뷰_완료(String accessToken, long roomId, long revieweeId) {

//@formatter:off
RestAssured.given().auth().oauth2(accessToken).body(new ReviewRequest(roomId,revieweeId)).contentType(ContentType.JSON)
.when().post("/review/complete")
.then().assertThat().statusCode(200);
//@formatter:on
}
}
35 changes: 35 additions & 0 deletions backend/src/test/java/corea/bdd/RoomGiven.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package corea.bdd;

import corea.room.domain.RoomClassification;
import corea.room.dto.RoomCreateRequest;
import corea.room.dto.RoomResponse;
import io.restassured.RestAssured;
import io.restassured.http.ContentType;

import java.time.LocalDateTime;
import java.util.List;

public class RoomGiven {
public static RoomResponse 방생성(String accessToken, int matchingSize) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
public static RoomResponse 방생성(String accessToken, int matchingSize) {
public static RoomResponse 방생성(String accessToken, int matchingSize) {


LocalDateTime now = LocalDateTime.now();
final RoomCreateRequest request =
new RoomCreateRequest(
"제목", "내용", "https://github.com/example/java-racingcar",
"https://gongu.copyright.or.kr/gongu/wrt/cmmn/wrtFileImageView.do?wrtSn=13301655&filePath=L2Rpc2sxL25ld2RhdGEvMjAyMS8yMS9DTFMxMDAwNC8xMzMwMTY1NV9XUlRfMjFfQ0xTMTAwMDRfMjAyMTEyMTNfMQ==&thumbAt=Y&thumbSe=b_tbumb&wrtTy=10004",
matchingSize,
List.of("TDD", "클린코드"),
200,
now.plusHours(2),
now.plusDays(2),
RoomClassification.BACKEND
);

//@formatter:off
return RestAssured.given().auth().oauth2(accessToken).body(request).contentType(ContentType.JSON)
.when().post("/rooms")
.then().assertThat().statusCode(201).extract().as(RoomResponse.class);
//@formatter:on
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change

}
Original file line number Diff line number Diff line change
@@ -1,68 +1,92 @@
package corea.feedback.controller;

import config.ControllerTest;
import corea.auth.service.TokenService;
import corea.auth.service.GithubOAuthProvider;
import corea.bdd.MemberGiven;
import corea.bdd.RoomGiven;
import corea.feedback.dto.SocialFeedbackRequest;
import corea.fixture.MatchResultFixture;
import corea.fixture.MemberFixture;
import corea.fixture.RoomFixture;
import corea.matching.repository.MatchResultRepository;
import corea.member.domain.Member;
import corea.member.repository.MemberRepository;
import corea.room.domain.Room;
import corea.room.repository.RoomRepository;
import corea.matching.domain.PullRequestInfo;
import corea.matching.dto.MatchResultResponse;
import corea.matching.service.PullRequestProvider;
import corea.room.domain.PullRequestReviews;
import io.restassured.RestAssured;
import io.restassured.http.ContentType;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.mockito.Mockito;
import org.springframework.boot.test.mock.mockito.MockBean;

import java.time.LocalDateTime;
import java.util.List;

import static corea.bdd.MatchingGiven.리뷰이_목록_조회;
import static corea.bdd.MatchingGiven.매칭;
import static corea.bdd.ParticipationGiven.방_참가;
import static corea.bdd.ReviewGiven.리뷰_완료;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.when;

@ControllerTest
class SocialFeedbackFeedbackControllerTest {

@Autowired
private RoomRepository roomRepository;
@MockBean
private PullRequestProvider pullRequestProvider;

@MockBean
private GithubOAuthProvider githubOAuthProvider;

@Autowired
private MemberRepository memberRepository;
@BeforeEach
void setUp() {
PullRequestInfo info = Mockito.mock(PullRequestInfo.class);
when(info.containsGithubMemberId(anyString())).thenReturn(true);
when(info.getPullrequestLinkWithGithubMemberId(anyString())).thenReturn("");
when(pullRequestProvider.getUntilDeadline(anyString(), any(LocalDateTime.class)))
.thenReturn(info);

@Autowired
private MatchResultRepository matchResultRepository;
PullRequestReviews pullRequestReviews = Mockito.mock(PullRequestReviews.class);
when(pullRequestReviews.getReviewUrl(anyString())).thenReturn("");

@Autowired
private TokenService tokenService;
when(githubOAuthProvider.getPullRequestReview(anyString())).thenReturn(pullRequestReviews);
}

@Test
@DisplayName("소셜(리뷰이 -> 리뷰어)에 대한 피드백을 작성한다.")
void create() {
Member manager = memberRepository.save(MemberFixture.MEMBER_ROOM_MANAGER_JOYSON());
Room room = roomRepository.save(RoomFixture.ROOM_DOMAIN(manager));
Member reviewer = memberRepository.save(MemberFixture.MEMBER_PORORO());
Member reviewee = memberRepository.save(MemberFixture.MEMBER_YOUNGSU());
String token = tokenService.createAccessToken(reviewee);
matchResultRepository.save(MatchResultFixture.MATCH_RESULT_DOMAIN(
room.getId(),
reviewer,
reviewee
));
String 액세스_토큰 = MemberGiven.멤버_로그인(githubOAuthProvider, MemberFixture.MEMBER_ROOM_MANAGER_JOYSON());
long 방번호 = RoomGiven.방생성(액세스_토큰, 2)
.id();

String 리뷰어_토큰 = MemberGiven.멤버_로그인(githubOAuthProvider, MemberFixture.MEMBER_PORORO());
String 리뷰이_토큰 = MemberGiven.멤버_로그인(githubOAuthProvider, MemberFixture.MEMBER_YOUNGSU());

방_참가(리뷰어_토큰, 방번호);
방_참가(리뷰이_토큰, 방번호);

매칭(액세스_토큰, 방번호);

Long 리뷰이_아이디 = 리뷰이_목록_조회(리뷰어_토큰, 방번호)
.stream()
.findAny()
.map(MatchResultResponse::userId)
.orElseThrow();


Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change

리뷰_완료(리뷰어_토큰, 방번호, 리뷰이_아이디);

SocialFeedbackRequest request = new SocialFeedbackRequest(
reviewer.getId(),
리뷰이_아이디,
4,
List.of("방의 목적에 맞게 코드를 작성했어요", "코드를 이해하기 쉬웠어요"),
"유용한 블로그나 아티클도 남겨주시고, \n 사소한 부분까지 잘 챙겨준게 좋았씁니다."
);

RestAssured.given()
.auth()
.oauth2(token)
.contentType(ContentType.JSON)
.body(request)
.when()
.post("/rooms/" + room.getId() + "/social/feedbacks")
.then()
.statusCode(200);
//@formatter:off
RestAssured.given().auth().oauth2(리뷰어_토큰).body(request).contentType(ContentType.JSON)
.when().post("/rooms/" + 방번호 + "/social/feedbacks")
.then().statusCode(200);
//@formatter:on
}
}
18 changes: 18 additions & 0 deletions backend/src/test/java/corea/review/service/ReviewServiceTest.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package corea.review.service;

import config.ServiceTest;
import corea.auth.service.GithubOAuthProvider;
import corea.exception.CoreaException;
import corea.fixture.MatchResultFixture;
import corea.fixture.MemberFixture;
Expand All @@ -10,15 +11,21 @@
import corea.matching.repository.MatchResultRepository;
import corea.member.domain.Member;
import corea.member.repository.MemberRepository;
import corea.room.domain.PullRequestReviews;
import corea.room.domain.Room;
import corea.room.repository.RoomRepository;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.transaction.annotation.Transactional;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.when;

@ServiceTest
class ReviewServiceTest {
Expand All @@ -35,6 +42,17 @@ class ReviewServiceTest {
@Autowired
private MatchResultRepository matchResultRepository;

@MockBean
private GithubOAuthProvider githubOAuthProvider;

@BeforeEach
void setup(){
PullRequestReviews pullRequestReviews = Mockito.mock(PullRequestReviews.class);
when(pullRequestReviews.getReviewUrl(anyString())).thenReturn("");

when(githubOAuthProvider.getPullRequestReview(anyString())).thenReturn(pullRequestReviews);
}

@Test
@Transactional
@DisplayName("리뷰를 완료한다.")
Expand Down
Loading
Loading