Skip to content

Commit

Permalink
refactor: 벌레 컨트롤러 및 테스트 패키지 구조 변경 (#97)
Browse files Browse the repository at this point in the history
* refactor: 벌레 상품 조회 API URL 변경

* style: 테스트 패키지 구조 변경
  • Loading branch information
kmebin authored Nov 16, 2023
1 parent 17247e1 commit 58d5280
Show file tree
Hide file tree
Showing 15 changed files with 86 additions and 179 deletions.
12 changes: 12 additions & 0 deletions src/main/java/com/moabam/api/application/bug/BugService.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,24 @@

import static com.moabam.api.domain.bug.BugActionType.*;
import static com.moabam.api.domain.bug.BugType.*;
import static com.moabam.api.domain.product.ProductType.*;

import java.util.List;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.moabam.api.application.member.MemberService;
import com.moabam.api.application.product.ProductMapper;
import com.moabam.api.domain.bug.BugHistory;
import com.moabam.api.domain.bug.BugType;
import com.moabam.api.domain.bug.repository.BugHistorySearchRepository;
import com.moabam.api.domain.member.Member;
import com.moabam.api.domain.product.Product;
import com.moabam.api.domain.product.repository.ProductRepository;
import com.moabam.api.dto.TodayBugResponse;
import com.moabam.api.dto.bug.BugResponse;
import com.moabam.api.dto.product.ProductsResponse;
import com.moabam.global.common.util.ClockHolder;

import lombok.RequiredArgsConstructor;
Expand All @@ -26,6 +31,7 @@ public class BugService {

private final MemberService memberService;
private final BugHistorySearchRepository bugHistorySearchRepository;
private final ProductRepository productRepository;
private final ClockHolder clockHolder;

public BugResponse getBug(Long memberId) {
Expand All @@ -42,6 +48,12 @@ public TodayBugResponse getTodayBug(Long memberId) {
return BugMapper.toTodayBugResponse(morningBug, nightBug);
}

public ProductsResponse getBugProducts() {
List<Product> products = productRepository.findAllByType(BUG);

return ProductMapper.toProductsResponse(products);
}

private int calculateBugQuantity(List<BugHistory> bugHistory, BugType bugType) {
return bugHistory.stream()
.filter(history -> bugType.equals(history.getBugType()))
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package com.moabam.api.domain.product.repository;

import java.util.List;

import org.springframework.data.jpa.repository.JpaRepository;

import com.moabam.api.domain.product.Product;
import com.moabam.api.domain.product.ProductType;

public interface ProductRepository extends JpaRepository<Product, Long> {

List<Product> findAllByType(ProductType type);
}
7 changes: 7 additions & 0 deletions src/main/java/com/moabam/api/presentation/BugController.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import com.moabam.api.application.bug.BugService;
import com.moabam.api.dto.TodayBugResponse;
import com.moabam.api.dto.bug.BugResponse;
import com.moabam.api.dto.product.ProductsResponse;
import com.moabam.global.auth.annotation.CurrentMember;
import com.moabam.global.auth.model.AuthorizationMember;

Expand All @@ -32,4 +33,10 @@ public BugResponse getBug(@CurrentMember AuthorizationMember member) {
public TodayBugResponse getTodayBug(@CurrentMember AuthorizationMember member) {
return bugService.getTodayBug(member.id());
}

@GetMapping("/products")
@ResponseStatus(HttpStatus.OK)
public ProductsResponse getBugProducts() {
return bugService.getBugProducts();
}
}
26 changes: 0 additions & 26 deletions src/main/java/com/moabam/api/presentation/ProductController.java

This file was deleted.

48 changes: 0 additions & 48 deletions src/test/java/com/moabam/api/application/ProductServiceTest.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,21 +1,29 @@
package com.moabam.api.application;
package com.moabam.api.application.bug;

import static com.moabam.api.domain.product.ProductType.*;
import static com.moabam.support.fixture.MemberFixture.*;
import static com.moabam.support.fixture.ProductFixture.*;
import static org.assertj.core.api.Assertions.*;
import static org.mockito.BDDMockito.*;

import java.util.List;

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;

import com.moabam.api.application.bug.BugService;
import com.moabam.api.application.member.MemberService;
import com.moabam.api.domain.bug.Bug;
import com.moabam.api.domain.member.Member;
import com.moabam.api.domain.product.Product;
import com.moabam.api.domain.product.repository.ProductRepository;
import com.moabam.api.dto.bug.BugResponse;
import com.moabam.api.dto.product.ProductResponse;
import com.moabam.api.dto.product.ProductsResponse;
import com.moabam.global.common.util.StreamUtils;

@ExtendWith(MockitoExtension.class)
class BugServiceTest {
Expand All @@ -26,6 +34,9 @@ class BugServiceTest {
@Mock
MemberService memberService;

@Mock
ProductRepository productRepository;

@DisplayName("벌레를 조회한다.")
@Test
void get_bug_success() {
Expand All @@ -43,4 +54,21 @@ void get_bug_success() {
assertThat(response.nightBug()).isEqualTo(bug.getNightBug());
assertThat(response.goldenBug()).isEqualTo(bug.getGoldenBug());
}

@DisplayName("벌레 상품 목록을 조회한다.")
@Test
void get_bug_products_success() {
// given
Product product1 = bugProduct();
Product product2 = bugProduct();
given(productRepository.findAllByType(BUG)).willReturn(List.of(product1, product2));

// when
ProductsResponse response = bugService.getBugProducts();

// then
List<String> productNames = StreamUtils.map(response.products(), ProductResponse::name);
assertThat(response.products()).hasSize(2);
assertThat(productNames).containsExactly(BUG_PRODUCT_NAME, BUG_PRODUCT_NAME);
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.moabam.api.application;
package com.moabam.api.application.item;

import static com.moabam.support.fixture.InventoryFixture.*;
import static com.moabam.support.fixture.ItemFixture.*;
Expand All @@ -19,7 +19,6 @@
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;

import com.moabam.api.application.item.ItemService;
import com.moabam.api.application.member.MemberService;
import com.moabam.api.domain.bug.BugHistory;
import com.moabam.api.domain.bug.BugType;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.moabam.api.domain.entity;
package com.moabam.api.domain.bug;

import static com.moabam.support.fixture.BugFixture.*;
import static org.assertj.core.api.Assertions.*;
Expand All @@ -10,8 +10,6 @@
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;

import com.moabam.api.domain.bug.Bug;
import com.moabam.api.domain.bug.BugType;
import com.moabam.global.error.exception.BadRequestException;

class BugTest {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.moabam.api.domain.entity;
package com.moabam.api.domain.item;

import static com.moabam.support.fixture.ItemFixture.*;
import static org.assertj.core.api.Assertions.*;
Expand All @@ -11,7 +11,6 @@
import org.junit.jupiter.params.provider.CsvSource;

import com.moabam.api.domain.bug.BugType;
import com.moabam.api.domain.item.Item;
import com.moabam.global.error.exception.BadRequestException;

class ItemTest {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.moabam.api.domain.repository;
package com.moabam.api.domain.item.repository;

import static com.moabam.support.fixture.InventoryFixture.*;
import static com.moabam.support.fixture.ItemFixture.*;
Expand All @@ -16,9 +16,6 @@
import com.moabam.api.domain.item.Inventory;
import com.moabam.api.domain.item.Item;
import com.moabam.api.domain.item.ItemType;
import com.moabam.api.domain.item.repository.InventoryRepository;
import com.moabam.api.domain.item.repository.InventorySearchRepository;
import com.moabam.api.domain.item.repository.ItemRepository;
import com.moabam.api.domain.member.Member;
import com.moabam.api.domain.member.repository.MemberRepository;
import com.moabam.support.annotation.QuerydslRepositoryTest;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.moabam.api.domain.repository;
package com.moabam.api.domain.item.repository;

import static com.moabam.support.fixture.InventoryFixture.*;
import static com.moabam.support.fixture.ItemFixture.*;
Expand All @@ -13,9 +13,6 @@

import com.moabam.api.domain.item.Item;
import com.moabam.api.domain.item.ItemType;
import com.moabam.api.domain.item.repository.InventoryRepository;
import com.moabam.api.domain.item.repository.ItemRepository;
import com.moabam.api.domain.item.repository.ItemSearchRepository;
import com.moabam.support.annotation.QuerydslRepositoryTest;

@QuerydslRepositoryTest
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package com.moabam.api.domain.entity;
package com.moabam.api.domain.product;

import static org.assertj.core.api.AssertionsForClassTypes.*;

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

import com.moabam.api.domain.product.Product;
import com.moabam.global.error.exception.BadRequestException;

class ProductTest {
Expand Down
27 changes: 27 additions & 0 deletions src/test/java/com/moabam/api/presentation/BugControllerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import static com.moabam.support.fixture.BugFixture.*;
import static com.moabam.support.fixture.BugHistoryFixture.*;
import static com.moabam.support.fixture.MemberFixture.*;
import static com.moabam.support.fixture.ProductFixture.*;
import static java.nio.charset.StandardCharsets.*;
import static org.assertj.core.api.Assertions.*;
import static org.mockito.BDDMockito.*;
Expand All @@ -26,10 +27,14 @@
import com.fasterxml.jackson.databind.ObjectMapper;
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.repository.BugHistoryRepository;
import com.moabam.api.domain.bug.repository.BugHistorySearchRepository;
import com.moabam.api.domain.product.Product;
import com.moabam.api.domain.product.repository.ProductRepository;
import com.moabam.api.dto.TodayBugResponse;
import com.moabam.api.dto.bug.BugResponse;
import com.moabam.api.dto.product.ProductsResponse;
import com.moabam.support.annotation.WithMember;
import com.moabam.support.common.WithoutFilterSupporter;

Expand All @@ -53,6 +58,9 @@ class BugControllerTest extends WithoutFilterSupporter {
@Autowired
BugHistorySearchRepository bugHistorySearchRepository;

@Autowired
ProductRepository productRepository;

@DisplayName("벌레를 조회한다.")
@WithMember
@Test
Expand Down Expand Up @@ -97,4 +105,23 @@ void get_today_bug_success() throws Exception {
TodayBugResponse actual = objectMapper.readValue(content, TodayBugResponse.class);
assertThat(actual).isEqualTo(expected);
}

@DisplayName("벌레 상품 목록을 조회한다.")
@Test
void get_products_success() throws Exception {
// given
List<Product> products = productRepository.saveAll(List.of(bugProduct(), bugProduct()));
ProductsResponse expected = ProductMapper.toProductsResponse(products);

// when, then
String content = mockMvc.perform(get("/bugs/products")
.contentType(APPLICATION_JSON))
.andExpect(status().isOk())
.andDo(print())
.andReturn()
.getResponse()
.getContentAsString(UTF_8);
ProductsResponse actual = objectMapper.readValue(content, ProductsResponse.class);
assertThat(actual).isEqualTo(expected);
}
}
Loading

0 comments on commit 58d5280

Please sign in to comment.