Skip to content

Commit

Permalink
[feat] : BiddingService 와 테스트 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
ParkJuhan94 committed Feb 22, 2024
1 parent 1c0d178 commit 5c2d9d4
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 9 deletions.
13 changes: 11 additions & 2 deletions core/src/main/java/dev/handsup/bidding/service/BiddingService.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import org.springframework.stereotype.Service;

import dev.handsup.auction.domain.Auction;
import dev.handsup.auction.service.AuctionService;
import dev.handsup.bidding.domain.Bidding;
import dev.handsup.bidding.dto.BiddingMapper;
import dev.handsup.bidding.dto.request.RegisterBiddingRequest;
Expand All @@ -18,6 +19,7 @@
public class BiddingService {

private final BiddingRepository biddingRepository;
private final AuctionService auctionService;

private void validateBiddingPrice(int biddingPrice, Auction auction) {
Integer maxBiddingPrice = biddingRepository.findMaxBiddingPriceByAuctionId(auction.getId());
Expand All @@ -36,8 +38,15 @@ private void validateBiddingPrice(int biddingPrice, Auction auction) {
}

public RegisterBiddingResponse registerBidding(RegisterBiddingRequest request) {
validateBiddingPrice(request.biddingPrice(), request.auction());
Bidding savedBidding = biddingRepository.save(BiddingMapper.toBidding(request));
Auction auction = auctionService.getAuction(request.auctionId());
validateBiddingPrice(request.biddingPrice(), auction);

Bidding savedBidding = biddingRepository.save(Bidding.of(
request.biddingPrice(),
auction,
request.bidder()
)
);
return BiddingMapper.toRegisterBiddingResponse(savedBidding);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import static org.assertj.core.api.Assertions.*;
import static org.mockito.ArgumentMatchers.*;
import static org.mockito.BDDMockito.*;
import static org.mockito.Mockito.*;

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
Expand All @@ -14,8 +13,8 @@
import org.mockito.junit.jupiter.MockitoExtension;

import dev.handsup.auction.domain.Auction;
import dev.handsup.auction.service.AuctionService;
import dev.handsup.bidding.domain.Bidding;
import dev.handsup.bidding.dto.BiddingMapper;
import dev.handsup.bidding.dto.request.RegisterBiddingRequest;
import dev.handsup.bidding.dto.response.RegisterBiddingResponse;
import dev.handsup.bidding.repository.BiddingRepository;
Expand All @@ -30,10 +29,13 @@ class BiddingServiceTest {

@Mock
private BiddingRepository biddingRepository;
@Mock
private AuctionService auctionService;

@InjectMocks
private BiddingService biddingService;

private final String DIGITAL_DEVICE = "디지털 기기";
private final Auction auction = AuctionFixture.auction(); // 최소 입찰가 10000원
private final User user = UserFixture.user();

Expand All @@ -42,7 +44,8 @@ class BiddingServiceTest {
void validateBiddingPrice_LessThanInitPrice_ThrowsException() {
// given
given(biddingRepository.findMaxBiddingPriceByAuctionId(any(Long.class))).willReturn(null);
RegisterBiddingRequest request = RegisterBiddingRequest.of(9000, auction, user);
RegisterBiddingRequest request = RegisterBiddingRequest.of(9000, auction.getId(), user);
given(auctionService.getAuction(auction.getId())).willReturn(auction);

// when & then
assertThatThrownBy(() -> biddingService.registerBidding(request))
Expand All @@ -56,7 +59,8 @@ void validateBiddingPrice_NotHighEnough_ThrowsException() {
// given
Integer maxBiddingPrice = 15000;
given(biddingRepository.findMaxBiddingPriceByAuctionId(any(Long.class))).willReturn(maxBiddingPrice);
RegisterBiddingRequest request = RegisterBiddingRequest.of(15500, auction, user);
RegisterBiddingRequest request = RegisterBiddingRequest.of(15500, auction.getId(), user);
given(auctionService.getAuction(auction.getId())).willReturn(auction);

// when & then
assertThatThrownBy(() -> biddingService.registerBidding(request))
Expand All @@ -68,9 +72,13 @@ void validateBiddingPrice_NotHighEnough_ThrowsException() {
@DisplayName("[입찰 등록이 성공적으로 이루어진다]")
void registerBidding_Success() {
// given
RegisterBiddingRequest request = RegisterBiddingRequest.of(20000, auction, user);
Bidding bidding = BiddingMapper.toBidding(request);

RegisterBiddingRequest request = RegisterBiddingRequest.of(20000, auction.getId(), user);
given(auctionService.getAuction(auction.getId())).willReturn(auction);
Bidding bidding = Bidding.of(
request.biddingPrice(),
auction,
user
);
given(biddingRepository.save(any(Bidding.class))).willReturn(bidding);
given(biddingRepository.findMaxBiddingPriceByAuctionId(any(Long.class))).willReturn(19000);

Expand Down

0 comments on commit 5c2d9d4

Please sign in to comment.