Skip to content

Commit

Permalink
test: 벌레 내역 조회 컨트롤러 통합 테스트
Browse files Browse the repository at this point in the history
  • Loading branch information
kmebin committed Nov 26, 2023
1 parent f3beca8 commit d6c18d4
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 12 deletions.
19 changes: 11 additions & 8 deletions src/main/java/com/moabam/api/application/bug/BugMapper.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.moabam.api.application.bug;

import java.util.List;
import java.util.Optional;

import com.moabam.api.domain.bug.Bug;
import com.moabam.api.domain.bug.BugActionType;
Expand Down Expand Up @@ -37,17 +38,19 @@ public static BugResponse toBugResponse(Bug bug) {
}

public static BugHistoryItemResponse toBugHistoryItemResponse(BugHistoryDto dto) {
BugHistoryItemResponse.PaymentResponse payment = BugHistoryItemResponse.PaymentResponse.builder()
.id(dto.payment().getId())
.orderName(dto.payment().getOrder().getName())
.discountAmount(dto.payment().getDiscountAmount())
.totalAmount(dto.payment().getTotalAmount())
.build();
BugHistoryItemResponse.PaymentResponse payment = Optional.ofNullable(dto.payment())
.map(p -> BugHistoryItemResponse.PaymentResponse.builder()
.id(p.getId())
.orderName(p.getOrder().getName())
.discountAmount(p.getDiscountAmount())
.totalAmount(p.getTotalAmount())
.build())
.orElse(null);

return BugHistoryItemResponse.builder()
.id(dto.id())
.bugType(dto.bugType().name())
.actionType(dto.actionType().name())
.bugType(dto.bugType())
.actionType(dto.actionType())
.quantity(dto.quantity())
.date(DateUtils.format(dto.createdAt()))
.payment(payment)
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/com/moabam/api/domain/bug/BugHistory.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,9 @@ public class BugHistory extends BaseTimeEntity {
private int quantity;

@Builder
private BugHistory(Long memberId, BugType bugType, BugActionType actionType, int quantity) {
private BugHistory(Long memberId, Payment payment, BugType bugType, BugActionType actionType, int quantity) {
this.memberId = requireNonNull(memberId);
this.payment = payment;
this.bugType = requireNonNull(bugType);
this.actionType = requireNonNull(actionType);
this.quantity = validateQuantity(quantity);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public class BugHistorySearchRepository {
private final JPAQueryFactory jpaQueryFactory;

public List<BugHistoryDto> findByMemberIdWithPayment(Long memberId) {
return jpaQueryFactory.select(Projections.fields(
return jpaQueryFactory.select(Projections.constructor(
BugHistoryDto.class,
bugHistory.id,
bugHistory.bugType,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@
import static com.fasterxml.jackson.annotation.JsonInclude.Include.*;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.moabam.api.domain.bug.BugActionType;
import com.moabam.api.domain.bug.BugType;

import lombok.Builder;

@Builder
public record BugHistoryItemResponse(
Long id,
String bugType,
String actionType,
BugType bugType,
BugActionType actionType,
int quantity,
String date,
@JsonInclude(NON_NULL) PaymentResponse payment
Expand Down
65 changes: 65 additions & 0 deletions src/test/java/com/moabam/api/presentation/BugControllerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import static com.moabam.global.auth.model.AuthorizationThreadLocal.*;
import static com.moabam.support.fixture.BugFixture.*;
import static com.moabam.support.fixture.MemberFixture.*;
import static com.moabam.support.fixture.PaymentFixture.*;
import static com.moabam.support.fixture.ProductFixture.*;
import static java.nio.charset.StandardCharsets.*;
import static org.assertj.core.api.Assertions.*;
Expand All @@ -15,6 +16,7 @@
import java.util.List;

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
Expand All @@ -27,10 +29,15 @@
import com.moabam.api.application.bug.BugMapper;
import com.moabam.api.application.member.MemberService;
import com.moabam.api.application.product.ProductMapper;
import com.moabam.api.domain.bug.BugActionType;
import com.moabam.api.domain.bug.BugType;
import com.moabam.api.domain.bug.repository.BugHistoryRepository;
import com.moabam.api.domain.bug.repository.BugHistorySearchRepository;
import com.moabam.api.domain.payment.Payment;
import com.moabam.api.domain.payment.repository.PaymentRepository;
import com.moabam.api.domain.product.Product;
import com.moabam.api.domain.product.repository.ProductRepository;
import com.moabam.api.dto.bug.BugHistoryResponse;
import com.moabam.api.dto.bug.BugResponse;
import com.moabam.api.dto.product.ProductsResponse;
import com.moabam.api.dto.product.PurchaseProductRequest;
Expand Down Expand Up @@ -61,6 +68,9 @@ class BugControllerTest extends WithoutFilterSupporter {
@Autowired
ProductRepository productRepository;

@Autowired
PaymentRepository paymentRepository;

@DisplayName("벌레를 조회한다.")
@WithMember
@Test
Expand All @@ -82,6 +92,61 @@ void get_bug_success() throws Exception {
assertThat(actual).isEqualTo(expected);
}

@DisplayName("벌레 내역을 조회한다.")
@Nested
class GetBugHistory {

@DisplayName("성공한다.")
@WithMember
@Test
void success() throws Exception {
// given
Long memberId = getAuthMember().id();
bugHistoryRepository.save(rewardMorningBugHistory(memberId));

// expected
String content = mockMvc.perform(get("/bugs/history")
.contentType(APPLICATION_JSON))
.andExpect(status().isOk())
.andDo(print())
.andReturn()
.getResponse()
.getContentAsString(UTF_8);
BugHistoryResponse actual = objectMapper.readValue(content, BugHistoryResponse.class);
assertThat(actual.history().get(0).bugType()).isEqualTo(BugType.MORNING);
assertThat(actual.history().get(0).actionType()).isEqualTo(BugActionType.REWARD);
assertThat(actual.history().get(0).quantity()).isEqualTo(REWARD_MORNING_BUG);
assertThat(actual.history().get(0).payment()).isNull();
}

@DisplayName("벌레 충전 내역인 경우 결제 정보를 포함한다.")
@WithMember
@Test
void charge_success() throws Exception {
// given
Long memberId = getAuthMember().id();
Product product = productRepository.save(bugProduct());
Payment payment = paymentRepository.save(payment(product));
bugHistoryRepository.save(chargeGoldenBugHistory(memberId, payment));

// expected
String content = mockMvc.perform(get("/bugs/history")
.contentType(APPLICATION_JSON))
.andExpect(status().isOk())
.andDo(print())
.andReturn()
.getResponse()
.getContentAsString(UTF_8);
BugHistoryResponse actual = objectMapper.readValue(content, BugHistoryResponse.class);
assertThat(actual.history().get(0).bugType()).isEqualTo(BugType.GOLDEN);
assertThat(actual.history().get(0).actionType()).isEqualTo(BugActionType.CHARGE);
assertThat(actual.history().get(0).quantity()).isEqualTo(BUG_PRODUCT_QUANTITY);
assertThat(actual.history().get(0).payment().orderName()).isEqualTo(BUG_PRODUCT_NAME);
assertThat(actual.history().get(0).payment().totalAmount()).isEqualTo(BUG_PRODUCT_PRICE);
assertThat(actual.history().get(0).payment().discountAmount()).isZero();
}
}

@DisplayName("벌레 상품 목록을 조회한다.")
@Test
void get_bug_products_success() throws Exception {
Expand Down
27 changes: 27 additions & 0 deletions src/test/java/com/moabam/support/fixture/BugFixture.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
package com.moabam.support.fixture;

import static com.moabam.support.fixture.ProductFixture.*;

import com.moabam.api.domain.bug.Bug;
import com.moabam.api.domain.bug.BugActionType;
import com.moabam.api.domain.bug.BugHistory;
import com.moabam.api.domain.bug.BugType;
import com.moabam.api.domain.payment.Payment;

public final class BugFixture {

public static final int MORNING_BUG = 10;
public static final int NIGHT_BUG = 20;
public static final int GOLDEN_BUG = 30;
public static final int REWARD_MORNING_BUG = 3;
public static final int USE_NIGHT_BUG = 5;

public static Bug bug() {
return Bug.builder()
Expand All @@ -15,4 +23,23 @@ public static Bug bug() {
.goldenBug(GOLDEN_BUG)
.build();
}

public static BugHistory rewardMorningBugHistory(Long memberId) {
return BugHistory.builder()
.memberId(memberId)
.bugType(BugType.MORNING)
.actionType(BugActionType.REWARD)
.quantity(REWARD_MORNING_BUG)
.build();
}

public static BugHistory chargeGoldenBugHistory(Long memberId, Payment payment) {
return BugHistory.builder()
.memberId(memberId)
.payment(payment)
.bugType(BugType.GOLDEN)
.actionType(BugActionType.CHARGE)
.quantity(BUG_PRODUCT_QUANTITY)
.build();
}
}

0 comments on commit d6c18d4

Please sign in to comment.