forked from codesquad-members-2023/second-hand-max
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[feat] 관심상품 카테고리 목록 조회 API 구현 (#126)
* #125 feat: 관심상품의 카테고리 목록 조회 서비스 구현 - /api/wish/categories * #125 test: 관심상품의 카테고리 조회 목록 테스트 코드 추가 * #125 test: 관심상품의 카테고리 조회 목록 테스트 코드 추가
- Loading branch information
1 parent
99ccc02
commit b4eaa04
Showing
11 changed files
with
283 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
backend/src/main/java/codesquard/app/api/wishitem/response/WishCategoryItemResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package codesquard.app.api.wishitem.response; | ||
|
||
import codesquard.app.domain.category.Category; | ||
import lombok.AccessLevel; | ||
import lombok.AllArgsConstructor; | ||
|
||
@AllArgsConstructor(access = AccessLevel.PRIVATE) | ||
public class WishCategoryItemResponse { | ||
private Long categoryId; | ||
private String categoryName; | ||
|
||
public static WishCategoryItemResponse from(Category category) { | ||
return new WishCategoryItemResponse(category.getId(), category.getName()); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return String.format("%s, %s(categoryId=%d, categoryName=%s)", "관심상품 카테고리 항목", this.getClass().getSimpleName(), | ||
categoryId, categoryName); | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
backend/src/main/java/codesquard/app/api/wishitem/response/WishCategoryListResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package codesquard.app.api.wishitem.response; | ||
|
||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
import codesquard.app.domain.category.Category; | ||
import lombok.AccessLevel; | ||
import lombok.AllArgsConstructor; | ||
|
||
@AllArgsConstructor(access = AccessLevel.PRIVATE) | ||
public class WishCategoryListResponse { | ||
private List<WishCategoryItemResponse> categories; | ||
|
||
public static WishCategoryListResponse of(List<Category> categories) { | ||
return new WishCategoryListResponse(categories.stream() | ||
.map(WishCategoryItemResponse::from) | ||
.collect(Collectors.toUnmodifiableList()) | ||
); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return String.format("%s, %s(categories=%s)", "관심상품 카테고리 리스트 응답", this.getClass().getSimpleName(), categories); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package codesquard.app; | ||
|
||
import static java.time.LocalDateTime.*; | ||
|
||
import codesquard.app.domain.category.Category; | ||
import codesquard.app.domain.item.Item; | ||
import codesquard.app.domain.item.ItemStatus; | ||
import codesquard.app.domain.member.Member; | ||
|
||
public class ItemTestSupport { | ||
|
||
public static Item createItem(String title, String content, Long price, ItemStatus status, String region, | ||
Member member, Category category) { | ||
return Item.builder() | ||
.title(title) | ||
.content(content) | ||
.price(price) | ||
.status(status) | ||
.region(region) | ||
.createdAt(now()) | ||
.wishCount(0L) | ||
.viewCount(0L) | ||
.chatCount(0L) | ||
.member(member) | ||
.category(category) | ||
.build(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
backend/src/test/java/codesquard/app/api/wishitem/WishItemControllerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package codesquard.app.api.wishitem; | ||
|
||
import static codesquard.app.CategoryTestSupport.*; | ||
import static org.hamcrest.Matchers.*; | ||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.BDDMockito.*; | ||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.*; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; | ||
|
||
import java.util.List; | ||
|
||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
import org.mockito.ArgumentMatchers; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; | ||
import org.springframework.boot.test.mock.mockito.MockBean; | ||
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter; | ||
import org.springframework.test.context.ActiveProfiles; | ||
import org.springframework.test.web.servlet.MockMvc; | ||
import org.springframework.test.web.servlet.setup.MockMvcBuilders; | ||
|
||
import codesquard.app.ControllerTestSupport; | ||
import codesquard.app.api.wishitem.response.WishCategoryListResponse; | ||
import codesquard.app.domain.oauth.support.Principal; | ||
|
||
@ActiveProfiles("test") | ||
@WebMvcTest(controllers = WishItemController.class) | ||
class WishItemControllerTest extends ControllerTestSupport { | ||
private MockMvc mockMvc; | ||
|
||
@Autowired | ||
private MappingJackson2HttpMessageConverter jackson2HttpMessageConverter; | ||
|
||
@Autowired | ||
private WishItemController wishItemController; | ||
|
||
@MockBean | ||
private WishItemService wishItemService; | ||
|
||
@BeforeEach | ||
public void setup() { | ||
mockMvc = MockMvcBuilders.standaloneSetup(wishItemController) | ||
.setControllerAdvice(globalExceptionHandler) | ||
.setCustomArgumentResolvers(authPrincipalArgumentResolver) | ||
.setMessageConverters(jackson2HttpMessageConverter) | ||
.alwaysDo(print()) | ||
.build(); | ||
|
||
given(authPrincipalArgumentResolver.supportsParameter(any())).willReturn(true); | ||
|
||
Principal principal = new Principal(1L, "[email protected]", "23Yong", null, null); | ||
given(authPrincipalArgumentResolver.resolveArgument(any(), any(), any(), any())).willReturn(principal); | ||
} | ||
|
||
@DisplayName("관심 상품들의 카테고리 목록을 요청한다") | ||
@Test | ||
public void readWishCategories() throws Exception { | ||
// given | ||
WishCategoryListResponse response = WishCategoryListResponse.of( | ||
List.of(findByName("스포츠/레저"), findByName("가구/인테리어"))); | ||
given(wishItemService.readWishCategories(ArgumentMatchers.any(Principal.class))) | ||
.willReturn(response); | ||
|
||
// when & then | ||
mockMvc.perform(get("/api/wishes/categories")) | ||
.andExpect(status().isOk()) | ||
.andExpect(jsonPath("statusCode").value(equalTo(200))) | ||
.andExpect(jsonPath("message").value(equalTo("관심상품의 카테고리 목록 조회를 완료하였습니다."))) | ||
.andExpect(jsonPath("data.categories[*].categoryName").value( | ||
containsInAnyOrder("스포츠/레저", "가구/인테리어"))); | ||
} | ||
|
||
} |
Oops, something went wrong.