Skip to content

Commit

Permalink
이미 서포터 리뷰를 작성했으면 예외 반환하도록 함 (#552)
Browse files Browse the repository at this point in the history
* refactor: 이미 서포터 리뷰를 작성했으면 예외 반환하도록 함

* test: Repository 테스트 추가

* test: 테스트에 em.flush와 em.clear 추가

* style: 예외 메세지 이름 변경

* feat: Feedback 을 하면 isReviewed 속성이 true 가 되도록 변경
  • Loading branch information
cookienc authored Sep 20, 2023
1 parent b9289a2 commit 711b670
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@
import touch.baton.domain.feedback.SupporterFeedback;

public interface SupporterFeedbackRepository extends JpaRepository<SupporterFeedback, Long> {

boolean existsByRunnerPostIdAndSupporterId(final Long runnerPostId, final Long supporterId);
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package touch.baton.domain.feedback.service;


import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
Expand Down Expand Up @@ -33,14 +32,17 @@ public Long createSupporterFeedback(final Runner runner, final SupporterFeedBack
final RunnerPost foundRunnerPost = runnerPostRepository.findById(request.runnerPostId())
.orElseThrow(() -> new FeedbackBusinessException("러너 게시글을 찾을 수 없습니다."));

if (supporterFeedbackRepository.existsByRunnerPostIdAndSupporterId(foundRunnerPost.getId(), foundSupporter.getId())) {
throw new FeedbackBusinessException("서포터에 대한 피드백을 작성했으면 추가적인 피드백을 남길 수 없습니다.");
}
if (foundRunnerPost.isNotOwner(runner)) {
throw new FeedbackBusinessException("리뷰 글을 작성한 주인만 글을 작성할 수 있습니다.");
}

if (foundRunnerPost.isDifferentSupporter(foundSupporter)) {
throw new FeedbackBusinessException("리뷰를 작성한 서포터에 대해서만 피드백을 작성할 수 있습니다.");
}

foundRunnerPost.finishFeedback();
final SupporterFeedback supporterFeedback = SupporterFeedback.builder()
.reviewType(ReviewType.valueOf(request.reviewType()))
.description(new Description(String.join(DELIMITER, request.descriptions())))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,10 @@ public void finishReview() {
updateReviewStatus(DONE);
}

public void finishFeedback() {
this.isReviewed = IsReviewed.reviewed();
}

public void updateReviewStatus(final ReviewStatus other) {
if (this.reviewStatus.isSame(NOT_STARTED) && other.isSame(IN_PROGRESS)) {
throw new RunnerPostDomainException("ReviewStatus 를 수정하던 도중 NOT_STARTED 에서 IN_PROGRESS 로 리뷰 상태 정책을 원인으로 실패하였습니다.");
Expand Down Expand Up @@ -285,10 +289,10 @@ public boolean isReviewStatusNotStarted() {
}

@Override
public boolean equals(Object o) {
public boolean equals(final Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
RunnerPost that = (RunnerPost) o;
final RunnerPost that = (RunnerPost) o;
return Objects.equals(id, that.id);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,4 +98,6 @@ class SupporterFeedbackCreateAssuredTest extends AssuredTestConfig {
.서버_응답()
.러너_게시글이_성공적으로_리뷰_완료_상태인지_확인한다(new HttpStatusAndLocationHeader(HttpStatus.NO_CONTENT, "/api/v1/posts/runner"));
}

// FIXME: 2023/09/19 피드백 실패 테스트 추가해줘잉
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package touch.baton.domain.feedback.repository;

import jakarta.persistence.EntityManager;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import touch.baton.config.RepositoryTestConfig;
import touch.baton.domain.feedback.SupporterFeedback;
import touch.baton.domain.member.Member;
import touch.baton.domain.runner.Runner;
import touch.baton.domain.runnerpost.RunnerPost;
import touch.baton.domain.supporter.Supporter;
import touch.baton.fixture.domain.MemberFixture;
import touch.baton.fixture.domain.RunnerFixture;
import touch.baton.fixture.domain.RunnerPostFixture;
import touch.baton.fixture.domain.SupporterFeedbackFixture;
import touch.baton.fixture.domain.SupporterFixture;
import touch.baton.fixture.vo.DeadlineFixture;

import java.time.LocalDateTime;

import static org.assertj.core.api.SoftAssertions.assertSoftly;

class SupporterFeedbackRepositoryTest extends RepositoryTestConfig {

@Autowired
private EntityManager em;
@Autowired
private SupporterFeedbackRepository supporterFeedbackRepository;

@DisplayName("러너 게시글 아이디와 서포터 아이디로 서포터 피드백 존재 유무를 확인할 수 있다.")
@Test
void existsByRunnerPostIdAndSupporterId() {
// given
final Member runnerMember = MemberFixture.createEthan();
em.persist(runnerMember);
final Runner runner = RunnerFixture.createRunner(runnerMember);
em.persist(runner);

final Member reviewedSupporterMember = MemberFixture.createHyena();
em.persist(reviewedSupporterMember);
final Supporter reviewedSupporter = SupporterFixture.create(reviewedSupporterMember);
em.persist(reviewedSupporter);

final RunnerPost runnerPost = RunnerPostFixture.create(runner, DeadlineFixture.deadline(LocalDateTime.now().plusDays(10)));
em.persist(runnerPost);

final SupporterFeedback supporterFeedback = SupporterFeedbackFixture.create(reviewedSupporter, runner, runnerPost);
em.persist(supporterFeedback);

final Member notReviewedSupporterMember = MemberFixture.createHyena();
em.persist(notReviewedSupporterMember);
final Supporter notReviewedSupporter = SupporterFixture.create(notReviewedSupporterMember);
em.persist(notReviewedSupporter);

em.flush();
em.close();

// when, then
assertSoftly(softly -> {
softly.assertThat(supporterFeedbackRepository.existsByRunnerPostIdAndSupporterId(runnerPost.getId(), reviewedSupporter.getId())).isTrue();
softly.assertThat(supporterFeedbackRepository.existsByRunnerPostIdAndSupporterId(runnerPost.getId(), notReviewedSupporter.getId())).isFalse();
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,26 +17,27 @@
import java.util.ArrayList;
import java.util.List;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.assertj.core.api.SoftAssertions.assertSoftly;

class FeedbackServiceTest extends ServiceTestConfig {

private FeedbackService feedbackService;
private Runner exactRunner;
private RunnerPost runnerPost;
private SupporterFeedBackCreateRequest request;
private Supporter reviewedSupporter;

@BeforeEach
void setUp() {
feedbackService = new FeedbackService(supporterFeedbackRepository, runnerPostRepository, supporterRepository);
Member ethan = memberRepository.save(MemberFixture.createEthan());
final Member ethan = memberRepository.save(MemberFixture.createEthan());
exactRunner = runnerRepository.save(RunnerFixture.createRunner(ethan));
Member ditoo = memberRepository.save(MemberFixture.createDitoo());
Supporter supporterDitoo = supporterRepository.save(SupporterFixture.create(ditoo));
runnerPost = runnerPostRepository.save(RunnerPostFixture.create(exactRunner, supporterDitoo));
final Member ditoo = memberRepository.save(MemberFixture.createDitoo());
reviewedSupporter = supporterRepository.save(SupporterFixture.create(ditoo));
runnerPost = runnerPostRepository.save(RunnerPostFixture.create(exactRunner, reviewedSupporter));

request = new SupporterFeedBackCreateRequest("GOOD", List.of("코드리뷰가 맛있어요.", "말투가 친절해요."), supporterDitoo.getId(), runnerPost.getId());
request = new SupporterFeedBackCreateRequest("GOOD", List.of("코드리뷰가 맛있어요.", "말투가 친절해요."), reviewedSupporter.getId(), runnerPost.getId());
}

@DisplayName("러너가 서포터 피드백을 할 수 있다.")
Expand All @@ -46,7 +47,10 @@ void createSupporterFeedback() {
final Long expected = feedbackService.createSupporterFeedback(exactRunner, request);

// then
assertThat(expected).isNotNull();
assertSoftly(softly -> {
softly.assertThat(expected).isNotNull();
softly.assertThat(runnerPost.getIsReviewed().getValue()).isTrue();
});
}

@DisplayName("소유자가 아닌 러너는 피드백을 할 수 없다.")
Expand All @@ -58,8 +62,7 @@ void fail_createSupporterFeedback_if_not_owner_runner() {

// when, then
assertThatThrownBy(() -> feedbackService.createSupporterFeedback(notOwner, request))
.isInstanceOf(FeedbackBusinessException.class)
.hasMessage("리뷰 글을 작성한 주인만 글을 작성할 수 있습니다.");
.isInstanceOf(FeedbackBusinessException.class);
}

@DisplayName("리뷰를 하지 않은 서포터를 피드백을 할 수 없다.")
Expand All @@ -72,7 +75,18 @@ void fail_createSupporterFeedback_if_not_review_supporter_runner() {

// when, then
assertThatThrownBy(() -> feedbackService.createSupporterFeedback(exactRunner, notReviewSupporterRequest))
.isInstanceOf(FeedbackBusinessException.class)
.hasMessage("리뷰를 작성한 서포터에 대해서만 피드백을 작성할 수 있습니다.");
.isInstanceOf(FeedbackBusinessException.class);
}

@DisplayName("이미 서포터 피드백을 작성했으면 서포터 피드백을 할 수 없다.")
@Test
void fail_createSupporterFeedback_if_already_reviewed_supporter() {
// given
final SupporterFeedBackCreateRequest supporterFeedBackCreateRequest = new SupporterFeedBackCreateRequest("GOOD", new ArrayList<>(), reviewedSupporter.getId(), runnerPost.getId());
feedbackService.createSupporterFeedback(exactRunner, supporterFeedBackCreateRequest);

// when, then
assertThatThrownBy(() -> feedbackService.createSupporterFeedback(exactRunner, supporterFeedBackCreateRequest))
.isInstanceOf(FeedbackBusinessException.class);
}
}

0 comments on commit 711b670

Please sign in to comment.