Skip to content

Commit

Permalink
refactor: #479 채팅방 관련 테스트 코드 리팩토링 (#485)
Browse files Browse the repository at this point in the history
* test: ChatRoomTest Fixture 추가 및 테스트 케이스 리팩토링

* test: JpaChatRoomRepositoryTest Fixture 추가 및 테스트 케이스 리팩토링

* test: QuerydslChatRoomRepositoryImplTest Fixture 추가 및 테스트 케이스 리팩토링

* test: QuerydslChatRoomAndImageRepositoryImplTest Fixture 추가 및 테스트 케이스 리팩토링

* test: QuerydslChatRoomAndMessageAndImageRepositoryImplTest Fixture 추가 및 테스트 케이스 리팩토링

* test: ChatRoomServiceTest Fixture 추가 및 테스트 케이스 리팩토링

* test: 모든 픽스처 객체를 `@BeforeEach`로 세팅하도록 변경

* test: 테스트 픽스처 추가 및 코드 리팩토링

* test: 서비스의 반환 값도 픽스처를 사용해서 비교하도록 수정

* stule: 개행 수정 및 사용하지 않는 필드 제거

* test: 픽스처 클래스 필드 중 사용하지 않는 필드 정리
  • Loading branch information
kwonyj1022 authored and apptie committed Oct 7, 2023
1 parent 2e3c5c8 commit dddceb8
Show file tree
Hide file tree
Showing 12 changed files with 893 additions and 1,297 deletions.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1,76 +1,28 @@
package com.ddang.ddang.chat.domain;

import com.ddang.ddang.auction.domain.Auction;
import com.ddang.ddang.auction.domain.BidUnit;
import com.ddang.ddang.auction.domain.Price;
import com.ddang.ddang.auction.infrastructure.persistence.JpaAuctionRepository;
import com.ddang.ddang.bid.domain.Bid;
import com.ddang.ddang.bid.domain.BidPrice;
import com.ddang.ddang.bid.infrastructure.persistence.JpaBidRepository;
import com.ddang.ddang.chat.infrastructure.persistence.JpaChatRoomRepository;
import com.ddang.ddang.configuration.JpaConfiguration;
import com.ddang.ddang.configuration.QuerydslConfiguration;
import com.ddang.ddang.image.domain.ProfileImage;
import com.ddang.ddang.chat.domain.fixture.ChatRoomFixture;
import com.ddang.ddang.user.domain.User;
import com.ddang.ddang.user.infrastructure.persistence.JpaUserRepository;
import jakarta.persistence.EntityManager;
import jakarta.persistence.PersistenceContext;
import org.junit.jupiter.api.DisplayNameGeneration;
import org.junit.jupiter.api.DisplayNameGenerator;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.context.annotation.Import;
import org.springframework.test.util.ReflectionTestUtils;

import java.time.LocalDateTime;

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

@DataJpaTest
@DisplayNameGeneration(DisplayNameGenerator.ReplaceUnderscores.class)
@SuppressWarnings("NonAsciiCharacters")
@Import({JpaConfiguration.class, QuerydslConfiguration.class})
class ChatRoomTest {

@PersistenceContext
EntityManager em;

@Autowired
JpaAuctionRepository auctionRepository;

@Autowired
JpaBidRepository bidRepository;

@Autowired
JpaUserRepository userRepository;

@Autowired
JpaChatRoomRepository chatRoomRepository;
class ChatRoomTest extends ChatRoomFixture {

@ParameterizedTest
@CsvSource(value = {"0:true", "9:true", "10:false"}, delimiter = ':')
void 채팅방_비활성화_여부를_체크한다(final long plusDay, final boolean expected) {
// given
final User buyer = User.builder()
.name("회원")
.profileImage(new ProfileImage("upload.png", "store.png"))
.reliability(4.7d)
.oauthId("12345")
.build();
final Auction auction = Auction.builder()
.title("title")
.build();

userRepository.save(buyer);
auctionRepository.save(auction);

final ChatRoom chatRoom = new ChatRoom(auction, buyer);
chatRoomRepository.save(chatRoom);

em.flush();
em.clear();
final ChatRoom chatRoom = new ChatRoom(경매, 구매자);
ReflectionTestUtils.setField(chatRoom, "createdTime", LocalDateTime.now());

// when
final boolean actual = chatRoom.isChatAvailableTime(chatRoom.getCreatedTime().plusDays(plusDay));
Expand All @@ -82,73 +34,22 @@ class ChatRoomTest {
@Test
void 주어진_사용자의_채팅상대를_반환한다() {
// given
final User seller = User.builder()
.name("판매자")
.profileImage(new ProfileImage("upload.png", "store.png"))
.reliability(4.7d)
.oauthId("12345")
.build();
final User buyer = User.builder()
.name("구매자")
.profileImage(new ProfileImage("upload.png", "store.png"))
.reliability(4.7d)
.oauthId("12346")
.build();
userRepository.save(seller);
userRepository.save(buyer);

final Auction auction = Auction.builder()
.title("title")
.seller(seller)
.build();

auctionRepository.save(auction);

final ChatRoom chatRoom = new ChatRoom(auction, buyer);
chatRoomRepository.save(chatRoom);

em.flush();
em.clear();
final ChatRoom chatRoom = new ChatRoom(경매, 구매자);

// when
final User actual = chatRoom.calculateChatPartnerOf(buyer);
final User actual = chatRoom.calculateChatPartnerOf(구매자);

// then
assertThat(actual).isEqualTo(seller);
assertThat(actual).isEqualTo(판매자);
}

@Test
void 주어진_사용자가_판매자라면_채팅_참여자이다() {
// given
final User seller = User.builder()
.name("판매자")
.profileImage(new ProfileImage("upload.png", "store.png"))
.reliability(4.7d)
.oauthId("12345")
.build();
final User buyer = User.builder()
.name("구매자")
.profileImage(new ProfileImage("upload.png", "store.png"))
.reliability(4.7d)
.oauthId("12346")
.build();
userRepository.save(seller);
userRepository.save(buyer);

final Auction auction = Auction.builder()
.title("경매")
.seller(seller)
.build();
auctionRepository.save(auction);

final ChatRoom chatRoom = new ChatRoom(auction, buyer);
chatRoomRepository.save(chatRoom);

em.flush();
em.clear();
final ChatRoom chatRoom = new ChatRoom(경매, 구매자);

// when
final boolean actual = chatRoom.isParticipant(seller);
final boolean actual = chatRoom.isParticipant(판매자);

// then
assertThat(actual).isTrue();
Expand All @@ -157,42 +58,10 @@ class ChatRoomTest {
@Test
void 주어진_사용자가_구매자라면_채팅_참여자이다() {
// given
final User seller = User.builder()
.name("판매자")
.profileImage(new ProfileImage("upload.png", "store.png"))
.reliability(4.7d)
.oauthId("12345")
.build();
final User buyer = User.builder()
.name("구매자")
.profileImage(new ProfileImage("upload.png", "store.png"))
.reliability(4.7d)
.oauthId("12346")
.build();
userRepository.save(seller);
userRepository.save(buyer);

final Auction auction = Auction.builder()
.title("경매")
.seller(seller)
.bidUnit(new BidUnit(1_000))
.startPrice(new Price(10_000))
.closingTime(LocalDateTime.now())
.build();
auctionRepository.save(auction);

final Bid bid = new Bid(auction, buyer, new BidPrice(15_000));
bidRepository.save(bid);
auction.updateLastBid(bid);

final ChatRoom chatRoom = new ChatRoom(auction, buyer);
chatRoomRepository.save(chatRoom);

em.flush();
em.clear();
final ChatRoom chatRoom = new ChatRoom(경매, 구매자);

// when
final boolean actual = chatRoom.isParticipant(buyer);
final boolean actual = chatRoom.isParticipant(구매자);

// then
assertThat(actual).isTrue();
Expand All @@ -201,49 +70,10 @@ class ChatRoomTest {
@Test
void 주어진_사용자가_판매자와_구매자_모두_아니라면_채팅_참여자가_아니다() {
// given
final User seller = User.builder()
.name("판매자")
.profileImage(new ProfileImage("upload.png", "store.png"))
.reliability(4.7d)
.oauthId("12345")
.build();
final User buyer = User.builder()
.name("구매자")
.profileImage(new ProfileImage("upload.png", "store.png"))
.reliability(4.7d)
.oauthId("12346")
.build();
final User stranger = User.builder()
.name("일반인")
.profileImage(new ProfileImage("upload.png", "store.png"))
.reliability(4.7d)
.oauthId("12347")
.build();
userRepository.save(seller);
userRepository.save(buyer);
userRepository.save(stranger);

final Auction auction = Auction.builder()
.title("경매")
.seller(seller)
.bidUnit(new BidUnit(1_000))
.startPrice(new Price(10_000))
.closingTime(LocalDateTime.now())
.build();
auctionRepository.save(auction);

final Bid bid = new Bid(auction, buyer, new BidPrice(15_000));
bidRepository.save(bid);
auction.updateLastBid(bid);

final ChatRoom chatRoom = new ChatRoom(auction, buyer);
chatRoomRepository.save(chatRoom);

em.flush();
em.clear();
final ChatRoom chatRoom = new ChatRoom(경매, 구매자);

// when
final boolean actual = chatRoom.isParticipant(stranger);
final boolean actual = chatRoom.isParticipant(경매에_참여하지_않는_사용자);

// then
assertThat(actual).isFalse();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package com.ddang.ddang.chat.domain.fixture;

import com.ddang.ddang.auction.domain.Auction;
import com.ddang.ddang.auction.domain.BidUnit;
import com.ddang.ddang.auction.domain.Price;
import com.ddang.ddang.bid.domain.Bid;
import com.ddang.ddang.bid.domain.BidPrice;
import com.ddang.ddang.category.domain.Category;
import com.ddang.ddang.image.domain.ProfileImage;
import com.ddang.ddang.user.domain.User;
import org.junit.jupiter.api.BeforeEach;
import org.springframework.test.util.ReflectionTestUtils;

import java.time.LocalDateTime;

@SuppressWarnings("NonAsciiCharacters")
public class ChatRoomFixture {

protected User 판매자;
protected User 구매자;
protected User 경매에_참여하지_않는_사용자;
protected Auction 경매;
protected Bid 입찰;

@BeforeEach
void setUp() {
판매자 = User.builder()
.name("판매자")
.profileImage(new ProfileImage("upload.png", "store.png"))
.reliability(4.7d)
.oauthId("12345")
.build();
구매자 = User.builder()
.name("구매자")
.profileImage(new ProfileImage("upload.png", "store.png"))
.reliability(4.7d)
.oauthId("12346")
.build();
경매에_참여하지_않는_사용자 = User.builder()
.name("경매에 참여하지 않는 사용자")
.profileImage(new ProfileImage("upload.png", "store.png"))
.reliability(4.7d)
.oauthId("12347")
.build();

경매 = Auction.builder()
.seller(판매자)
.title("맥북")
.description("맥북 팔아요")
.subCategory(new Category("전자기기"))
.startPrice(new Price(10_000))
.bidUnit(new BidUnit(1_000))
.closingTime(LocalDateTime.now())
.build();
입찰 = new Bid(경매, 구매자, new BidPrice(15_000));

ReflectionTestUtils.setField(판매자, "id", 1L);
ReflectionTestUtils.setField(구매자, "id", 2L);
ReflectionTestUtils.setField(경매에_참여하지_않는_사용자, "id", 3L);

경매.updateLastBid(입찰);
}
}
Loading

0 comments on commit dddceb8

Please sign in to comment.