|
| 1 | +package com.backend.domain.cash.controller; |
| 2 | + |
| 3 | +import com.backend.domain.cash.constant.CashTxType; |
| 4 | +import com.backend.domain.cash.constant.RelatedType; |
| 5 | +import com.backend.domain.cash.entity.Cash; |
| 6 | +import com.backend.domain.cash.entity.CashTransaction; |
| 7 | +import com.backend.domain.cash.repository.CashRepository; |
| 8 | +import com.backend.domain.cash.repository.CashTransactionRepository; |
| 9 | +import com.backend.domain.member.entity.Member; |
| 10 | +import com.backend.domain.member.repository.MemberRepository; |
| 11 | +import org.junit.jupiter.api.BeforeEach; |
| 12 | +import org.junit.jupiter.api.Test; |
| 13 | +import org.springframework.beans.factory.annotation.Autowired; |
| 14 | +import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; |
| 15 | +import org.springframework.boot.test.context.SpringBootTest; |
| 16 | +import org.springframework.security.test.context.support.WithMockUser; |
| 17 | +import org.springframework.test.web.servlet.MockMvc; |
| 18 | +import org.springframework.transaction.annotation.Transactional; |
| 19 | + |
| 20 | +import static org.hamcrest.Matchers.*; |
| 21 | +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; |
| 22 | +import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; |
| 23 | +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; |
| 24 | + |
| 25 | +@SpringBootTest |
| 26 | +@AutoConfigureMockMvc(addFilters = false) // JWT 등 보안 필터 비활성화 |
| 27 | +@Transactional |
| 28 | +class ApiV1CashControllerTest { |
| 29 | + |
| 30 | + @Autowired MockMvc mvc; |
| 31 | + |
| 32 | + @Autowired MemberRepository memberRepository; |
| 33 | + @Autowired CashRepository cashRepository; |
| 34 | + @Autowired CashTransactionRepository cashTxRepository; |
| 35 | + |
| 36 | + Member me; |
| 37 | + Member other; |
| 38 | + Cash myCash; |
| 39 | + CashTransaction txDeposit; // id가 더 작음 |
| 40 | + CashTransaction txWithdraw; // id가 더 큼 (목록에서 먼저 나와야 함) |
| 41 | + |
| 42 | + @BeforeEach |
| 43 | + void setUp() { |
| 44 | + // 회원(비번은 NOT NULL이라 꼭 채움) |
| 45 | + me = memberRepository.save(Member.builder() |
| 46 | + . email( "[email protected]"). password( "pw"). nickname( "me"). build()); |
| 47 | + other = memberRepository.save(Member.builder() |
| 48 | + . email( "[email protected]"). password( "pw"). nickname( "other"). build()); |
| 49 | + |
| 50 | + // 내 지갑(잔액 10,000) |
| 51 | + myCash = cashRepository.save(Cash.builder().member(me).balance(10_000L).build()); |
| 52 | + |
| 53 | + // 원장 2건: 입금(10,000) → 출금(3,000) (정상 흐름) |
| 54 | + txDeposit = cashTxRepository.save(CashTransaction.builder() |
| 55 | + .cash(myCash) |
| 56 | + .type(CashTxType.DEPOSIT) |
| 57 | + .amount(10_000L) |
| 58 | + .balanceAfter(10_000L) |
| 59 | + .relatedType(RelatedType.PAYMENT) |
| 60 | + .relatedId(101L) // 임의 값 |
| 61 | + .build()); |
| 62 | + |
| 63 | + txWithdraw = cashTxRepository.save(CashTransaction.builder() |
| 64 | + .cash(myCash) |
| 65 | + .type(CashTxType.WITHDRAW) |
| 66 | + .amount(3_000L) |
| 67 | + .balanceAfter(7_000L) |
| 68 | + .relatedType(RelatedType.PAYMENT) |
| 69 | + .relatedId(102L) // 임의 값 |
| 70 | + .build()); |
| 71 | + |
| 72 | + // other 유저의 지갑/원장(권한 차단 테스트용) |
| 73 | + Cash otherCash = cashRepository.save(Cash.builder().member(other).balance(5_000L).build()); |
| 74 | + cashTxRepository.save(CashTransaction.builder() |
| 75 | + .cash(otherCash) |
| 76 | + .type(CashTxType.DEPOSIT) |
| 77 | + .amount(5_000L) |
| 78 | + .balanceAfter(5_000L) |
| 79 | + .relatedType(RelatedType.PAYMENT) |
| 80 | + .relatedId(201L) |
| 81 | + .build()); |
| 82 | + } |
| 83 | + |
| 84 | + // ============ /api/v1/cash ============ |
| 85 | + @Test |
| 86 | + @WithMockUser( username = "[email protected]") // 컨트롤러는 email로 회원 조회 |
| 87 | + void 내_지갑_조회_200() throws Exception { |
| 88 | + mvc.perform(get("/api/v1/cash")) |
| 89 | + .andDo(print()) |
| 90 | + .andExpect(status().isOk()) |
| 91 | + .andExpect(jsonPath("$.memberId", is(me.getId().intValue()))) |
| 92 | + .andExpect(jsonPath("$.cashId", is(myCash.getId().intValue()))) |
| 93 | + .andExpect(jsonPath("$.balance", is(10_000))) |
| 94 | + .andExpect(jsonPath("$.createDate", notNullValue())) |
| 95 | + .andExpect(jsonPath("$.modifyDate", notNullValue())); |
| 96 | + } |
| 97 | + |
| 98 | + @Test |
| 99 | + void 내_지갑_조회_인증없으면_401() throws Exception { |
| 100 | + // GlobalExceptionHandler가 ResponseStatusException을 RsData로 변환하진 않으므로 |
| 101 | + // 여기서는 상태코드만 검사 |
| 102 | + mvc.perform(get("/api/v1/cash")) |
| 103 | + .andDo(print()) |
| 104 | + .andExpect(status().isUnauthorized()); |
| 105 | + } |
| 106 | + |
| 107 | + // ============ /api/v1/cash/transactions ============ |
| 108 | + @Test |
| 109 | + @WithMockUser( username = "[email protected]") |
| 110 | + void 내_원장_목록_200_정렬검증() throws Exception { |
| 111 | + mvc.perform(get("/api/v1/cash/transactions") |
| 112 | + .param("page", "1") |
| 113 | + .param("size", "20")) |
| 114 | + .andDo(print()) |
| 115 | + .andExpect(status().isOk()) |
| 116 | + .andExpect(jsonPath("$.page", is(1))) |
| 117 | + .andExpect(jsonPath("$.size", is(20))) |
| 118 | + .andExpect(jsonPath("$.total", is(2))) |
| 119 | + .andExpect(jsonPath("$.items", hasSize(2))) |
| 120 | + // 최신(id DESC) → 출금(7000) 먼저 |
| 121 | + .andExpect(jsonPath("$.items[0].type", is("WITHDRAW"))) |
| 122 | + .andExpect(jsonPath("$.items[0].amount", is(3_000))) |
| 123 | + .andExpect(jsonPath("$.items[0].balanceAfter", is(7_000))) |
| 124 | + .andExpect(jsonPath("$.items[0].related.type", is("PAYMENT"))) |
| 125 | + .andExpect(jsonPath("$.items[0].related.id", is(102))) |
| 126 | + |
| 127 | + // 그 다음 입금(10000) |
| 128 | + .andExpect(jsonPath("$.items[1].type", is("DEPOSIT"))) |
| 129 | + .andExpect(jsonPath("$.items[1].amount", is(10_000))) |
| 130 | + .andExpect(jsonPath("$.items[1].balanceAfter", is(10_000))) |
| 131 | + .andExpect(jsonPath("$.items[1].related.type", is("PAYMENT"))) |
| 132 | + .andExpect(jsonPath("$.items[1].related.id", is(101))); |
| 133 | + } |
| 134 | + |
| 135 | + @Test |
| 136 | + void 내_원장_목록_인증없으면_401() throws Exception { |
| 137 | + mvc.perform(get("/api/v1/cash/transactions")) |
| 138 | + .andDo(print()) |
| 139 | + .andExpect(status().isUnauthorized()); |
| 140 | + } |
| 141 | + |
| 142 | + // ============ /api/v1/cash/transactions/{id} ============ |
| 143 | + @Test |
| 144 | + @WithMockUser( username = "[email protected]") |
| 145 | + void 내_원장_단건_상세_200_링크존재() throws Exception { |
| 146 | + mvc.perform(get("/api/v1/cash/transactions/{id}", txWithdraw.getId())) |
| 147 | + .andDo(print()) |
| 148 | + .andExpect(status().isOk()) |
| 149 | + .andExpect(jsonPath("$.transactionId", is(txWithdraw.getId().intValue()))) |
| 150 | + .andExpect(jsonPath("$.cashId", is(myCash.getId().intValue()))) |
| 151 | + .andExpect(jsonPath("$.type", is("WITHDRAW"))) |
| 152 | + .andExpect(jsonPath("$.amount", is(3_000))) |
| 153 | + .andExpect(jsonPath("$.balanceAfter", is(7_000))) |
| 154 | + .andExpect(jsonPath("$.createdAt", notNullValue())) |
| 155 | + // Related + Links (PAYMENT면 paymentDetail 링크 생성) |
| 156 | + .andExpect(jsonPath("$.related.type", is("PAYMENT"))) |
| 157 | + .andExpect(jsonPath("$.related.id", is(102))) |
| 158 | + .andExpect(jsonPath("$.related.links.paymentDetail", is("/api/v1/payments/me/102"))); |
| 159 | + } |
| 160 | + |
| 161 | + @Test |
| 162 | + @WithMockUser( username = "[email protected]") |
| 163 | + void 다른사람_원장_단건_조회시_404() throws Exception { |
| 164 | + // other 유저의 트랜잭션 id를 찾아서 접근 시도 |
| 165 | + Long othersTxId = cashTxRepository.findAll().stream() |
| 166 | + .filter(tx -> tx.getCash().getMember().getId().equals(other.getId())) |
| 167 | + .map(CashTransaction::getId) |
| 168 | + .findFirst() |
| 169 | + .orElseThrow(); |
| 170 | + |
| 171 | + // CashService는 ResponseStatusException(404)을 던짐 → 전역 핸들러가 RsData로 바꾸지 않으므로 상태만 체크 |
| 172 | + mvc.perform(get("/api/v1/cash/transactions/{id}", othersTxId)) |
| 173 | + .andDo(print()) |
| 174 | + .andExpect(status().isNotFound()); |
| 175 | + } |
| 176 | +} |
0 commit comments