From 5926e0e344c228ec330253044b140490d81b65f9 Mon Sep 17 00:00:00 2001 From: hi-june Date: Sat, 27 Jan 2024 00:02:55 +0900 Subject: [PATCH 1/7] =?UTF-8?q?chore:=20test=20=EC=9D=98=EC=A1=B4=EC=84=B1?= =?UTF-8?q?=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build.gradle | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/build.gradle b/build.gradle index d94eb85b..6bdaf57a 100644 --- a/build.gradle +++ b/build.gradle @@ -36,8 +36,8 @@ subprojects { // web implementation 'org.springframework.boot:spring-boot-starter-web' -// testImplementation 'org.junit.jupiter:junit-jupiter-api:5.8.1' -// testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.8.1' + // test + testImplementation 'org.springframework.boot:spring-boot-starter-test' } // gradle 빌드시에는 각 프로젝트를 실행가능한 jar형태로 만들게 되는데, module-common 프로젝트의 경우 main메소드가 없음 From d5bf7b075209eafa1ff5317e436df495077ae93b Mon Sep 17 00:00:00 2001 From: hi-june Date: Sat, 27 Jan 2024 00:05:41 +0900 Subject: [PATCH 2/7] =?UTF-8?q?NO-JIRA--test:=20=ED=85=8C=EC=8A=A4?= =?UTF-8?q?=ED=8A=B8=20=EB=8F=84=EB=A9=94=EC=9D=B8=20=EC=84=B8=ED=8C=85?= =?UTF-8?q?=EC=9A=A9=20=EB=A9=94=EC=86=8C=EB=93=9C=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../card/service/CardServiceTest.java | 128 ++++++++++++++++++ 1 file changed, 128 insertions(+) create mode 100644 nabimarket-application/src/test/java/org/prgrms/nabimarketbe/card/service/CardServiceTest.java diff --git a/nabimarket-application/src/test/java/org/prgrms/nabimarketbe/card/service/CardServiceTest.java b/nabimarket-application/src/test/java/org/prgrms/nabimarketbe/card/service/CardServiceTest.java new file mode 100644 index 00000000..99cb80ae --- /dev/null +++ b/nabimarket-application/src/test/java/org/prgrms/nabimarketbe/card/service/CardServiceTest.java @@ -0,0 +1,128 @@ +package org.prgrms.nabimarketbe.card.service; + +import org.junit.jupiter.api.BeforeEach; +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 org.prgrms.nabimarketbe.card.dto.request.CardCreateRequestDTO; +import org.prgrms.nabimarketbe.card.dto.request.CardStatusUpdateRequestDTO; +import org.prgrms.nabimarketbe.card.dto.request.CardUpdateRequestDTO; +import org.prgrms.nabimarketbe.card.dto.response.CardCreateResponseDTO; +import org.prgrms.nabimarketbe.card.dto.response.CardDetailResponseDTO; +import org.prgrms.nabimarketbe.card.dto.response.CardUpdateResponseDTO; +import org.prgrms.nabimarketbe.card.dto.response.wrapper.CardResponseDTO; +import org.prgrms.nabimarketbe.card.dto.response.wrapper.CardUserResponseDTO; +import org.prgrms.nabimarketbe.card.entity.Card; +import org.prgrms.nabimarketbe.card.entity.CardStatus; +import org.prgrms.nabimarketbe.card.entity.TradeType; +import org.prgrms.nabimarketbe.card.repository.CardRepository; +import org.prgrms.nabimarketbe.cardImage.entity.CardImage; +import org.prgrms.nabimarketbe.cardImage.repository.CardImageBatchRepository; +import org.prgrms.nabimarketbe.cardImage.repository.CardImageRepository; +import org.prgrms.nabimarketbe.cardimage.dto.request.CardImageCreateRequestDTO; +import org.prgrms.nabimarketbe.cardimage.dto.response.CardImageCreateResponseDTO; +import org.prgrms.nabimarketbe.cardimage.dto.response.CardImageSingleReadResponseDTO; +import org.prgrms.nabimarketbe.cardimage.dto.response.CardImageUpdateResponseDTO; +import org.prgrms.nabimarketbe.category.entity.Category; +import org.prgrms.nabimarketbe.category.entity.CategoryEnum; +import org.prgrms.nabimarketbe.category.repository.CategoryRepository; +import org.prgrms.nabimarketbe.item.entity.Item; +import org.prgrms.nabimarketbe.item.entity.PriceRange; +import org.prgrms.nabimarketbe.item.repository.ItemRepository; +import org.prgrms.nabimarketbe.user.dto.response.UserSummaryResponseDTO; +import org.prgrms.nabimarketbe.user.entity.User; +import org.prgrms.nabimarketbe.user.repository.UserRepository; +import org.prgrms.nabimarketbe.user.service.CheckService; +import org.springframework.test.util.ReflectionTestUtils; + +import java.time.LocalDateTime; +import java.util.Arrays; +import java.util.List; +import java.util.Optional; + +import static org.mockito.Mockito.*; +import static org.assertj.core.api.Assertions.*; + +@ExtendWith(MockitoExtension.class) +class CardServiceTest { + @InjectMocks + private CardService cardService; + + @Mock + private CheckService checkService; + + @Mock + private CardRepository cardRepository; + + @Mock + private ItemRepository itemRepository; + + @Mock + private CategoryRepository categoryRepository; + + @Mock + private CardImageRepository cardImageRepository; + + @Mock + private UserRepository userRepository; + + @Mock + private CardImageBatchRepository cardImageBatchRepository; + + private static String token; + private static Long userId; + private static Long cardId; + private static LocalDateTime createdDate; + private static LocalDateTime modifiedDate; + private static User user; + private static Category category; + private static Item item; + private static Card card; + private static List images; + + @BeforeEach + void setUp() { + token = "token"; + userId = 1L; + cardId = 1L; + createdDate = LocalDateTime.now(); + modifiedDate = LocalDateTime.now(); + + user = User.builder() + .role("USER") + .provider("GOOGLE") + .nickname("doby") + .imageUrl("img_url") + .accountId("1010110") + .build(); + ReflectionTestUtils.setField(user, "userId", userId); + category = new Category(CategoryEnum.ELECTRONICS); + item = Item.builder() + .priceRange(PriceRange.PRICE_RANGE_FOUR) + .itemName("apple macbook pro") + .category(category) + .build(); + card = Card.builder() + .cardTitle("macbook pro 교환 원해요") + .content("상태 최상입니다.") + .tradeType(TradeType.DIRECT_DEALING) + .pokeAvailable(true) + .tradeArea("서울") + .thumbnail("thumbnail_img_url") + .user(user) + .item(item) + .build(); + ReflectionTestUtils.setField(card, "cardId", cardId); + ReflectionTestUtils.setField(card, "createdDate", createdDate); + ReflectionTestUtils.setField(card, "modifiedDate", modifiedDate); + + images = Arrays.asList( + new CardImage("url1", card), + new CardImage("url2", card), + new CardImage("url3", card) + ); + } +} \ No newline at end of file From 7a363e5622669e017fafea0cdaf89c7f078ceb04 Mon Sep 17 00:00:00 2001 From: hi-june Date: Sat, 27 Jan 2024 00:06:49 +0900 Subject: [PATCH 3/7] =?UTF-8?q?NO-JIRA--test:=20card=20=EC=83=9D=EC=84=B1?= =?UTF-8?q?=20=ED=85=8C=EC=8A=A4=ED=8A=B8=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../card/service/CardServiceTest.java | 60 +++++++++++++++++++ 1 file changed, 60 insertions(+) diff --git a/nabimarket-application/src/test/java/org/prgrms/nabimarketbe/card/service/CardServiceTest.java b/nabimarket-application/src/test/java/org/prgrms/nabimarketbe/card/service/CardServiceTest.java index 99cb80ae..1d5f044f 100644 --- a/nabimarket-application/src/test/java/org/prgrms/nabimarketbe/card/service/CardServiceTest.java +++ b/nabimarket-application/src/test/java/org/prgrms/nabimarketbe/card/service/CardServiceTest.java @@ -125,4 +125,64 @@ void setUp() { new CardImage("url3", card) ); } + + @Test + @DisplayName("유저가 새로운 카드를 생성한다.") + void createCard() { + // given + CardCreateRequestDTO createRequest = new CardCreateRequestDTO( + "macbook pro 교환 원해요", + "thumbnail_img_url", + "apple macbook pro", + PriceRange.PRICE_RANGE_FOUR, + TradeType.DIRECT_DEALING, + "서울", + CategoryEnum.ELECTRONICS, + true, + "상태 최상입니다.", + Arrays.asList( + new CardImageCreateRequestDTO("url1"), + new CardImageCreateRequestDTO("url2"), + new CardImageCreateRequestDTO("url3") + ) + ); + + when(checkService.parseToken(token)).thenReturn(userId); + when(userRepository.findById(userId)).thenReturn(Optional.ofNullable(user)); + when(categoryRepository.findCategoryByCategoryName(createRequest.category())).thenReturn(Optional.of(category)); + when(itemRepository.save(any())).thenReturn(item); + when(cardRepository.save(any())).thenReturn(card); + + CardResponseDTO expectCardResponse = new CardResponseDTO<>( + new CardCreateResponseDTO( + cardId, + "macbook pro 교환 원해요", + "thumbnail_img_url", + "apple macbook pro", + PriceRange.PRICE_RANGE_FOUR, + TradeType.DIRECT_DEALING, + CategoryEnum.ELECTRONICS, + "서울", + true, + "상태 최상입니다.", + 0, + 0, + createdDate, + modifiedDate, + Arrays.asList( + new CardImageCreateResponseDTO("url1"), + new CardImageCreateResponseDTO("url2"), + new CardImageCreateResponseDTO("url3") + ) + ) + ); + + // when + CardResponseDTO actualCardResponse = cardService.createCard(token, createRequest); + + // then + assertThat(actualCardResponse) + .usingRecursiveComparison() + .isEqualTo(expectCardResponse); + } } \ No newline at end of file From 431dd150949f6a3cc2db4848845b258330cbb934 Mon Sep 17 00:00:00 2001 From: hi-june Date: Sat, 27 Jan 2024 00:07:23 +0900 Subject: [PATCH 4/7] =?UTF-8?q?NO-JIRA--test:=20card=20=EC=A0=95=EB=B3=B4?= =?UTF-8?q?=20=EC=A1=B0=ED=9A=8C=20=ED=85=8C=EC=8A=A4=ED=8A=B8=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../card/service/CardServiceTest.java | 64 +++++++++++++++++++ 1 file changed, 64 insertions(+) diff --git a/nabimarket-application/src/test/java/org/prgrms/nabimarketbe/card/service/CardServiceTest.java b/nabimarket-application/src/test/java/org/prgrms/nabimarketbe/card/service/CardServiceTest.java index 1d5f044f..cf4e7011 100644 --- a/nabimarket-application/src/test/java/org/prgrms/nabimarketbe/card/service/CardServiceTest.java +++ b/nabimarket-application/src/test/java/org/prgrms/nabimarketbe/card/service/CardServiceTest.java @@ -185,4 +185,68 @@ void createCard() { .usingRecursiveComparison() .isEqualTo(expectCardResponse); } + + @Test + @DisplayName("유저가 자신의 카드 정보를 수정한다.") + void updateCardById() { + // given + CardUpdateRequestDTO updateRequest = new CardUpdateRequestDTO( + "galaxy book 교환 원해요", + "thumbnail_img_url", + "galaxy book", + PriceRange.PRICE_RANGE_FIVE, + TradeType.SHIPPING, + CategoryEnum.ELECTRONICS, + "서울", + true, + "상태 최상입니다.", + Arrays.asList( + new CardImageCreateRequestDTO("url1"), + new CardImageCreateRequestDTO("url2"), + new CardImageCreateRequestDTO("url3") + ) + ); + + when(checkService.parseToken(token)).thenReturn(userId); + when(userRepository.existsById(userId)).thenReturn(true); + when(cardRepository.findActiveCardById(cardId)).thenReturn(Optional.of(card)); + when(checkService.isEqual(userId, card.getUser().getUserId())).thenReturn(true); + when(categoryRepository.findCategoryByCategoryName(updateRequest.category())).thenReturn(Optional.of(category)); + + CardResponseDTO expectedResponse = new CardResponseDTO<>( + new CardUpdateResponseDTO( + cardId, + "galaxy book 교환 원해요", + "thumbnail_img_url", + "galaxy book", + PriceRange.PRICE_RANGE_FIVE, + TradeType.SHIPPING, + CategoryEnum.ELECTRONICS, + "서울", + true, + "상태 최상입니다.", + 0, + 0, + createdDate, + modifiedDate, + Arrays.asList( + new CardImageUpdateResponseDTO("url1"), + new CardImageUpdateResponseDTO("url2"), + new CardImageUpdateResponseDTO("url3") + ) + ) + ); + + // when + CardResponseDTO actualResponse = cardService.updateCardById( + token, + cardId, + updateRequest + ); + + // then + assertThat(actualResponse) + .usingRecursiveComparison() + .isEqualTo(expectedResponse); + } } \ No newline at end of file From fa19fefc098b4d9a97d511eb7bc8371f29ada216 Mon Sep 17 00:00:00 2001 From: hi-june Date: Sat, 27 Jan 2024 00:08:10 +0900 Subject: [PATCH 5/7] =?UTF-8?q?NO-JIRA--test:=20=EC=B9=B4=EB=93=9C=20?= =?UTF-8?q?=EC=A0=95=EB=B3=B4=20=EC=A1=B0=ED=9A=8C=20=ED=85=8C=EC=8A=A4?= =?UTF-8?q?=ED=8A=B8=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../card/service/CardServiceTest.java | 49 +++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/nabimarket-application/src/test/java/org/prgrms/nabimarketbe/card/service/CardServiceTest.java b/nabimarket-application/src/test/java/org/prgrms/nabimarketbe/card/service/CardServiceTest.java index cf4e7011..5ffc2f71 100644 --- a/nabimarket-application/src/test/java/org/prgrms/nabimarketbe/card/service/CardServiceTest.java +++ b/nabimarket-application/src/test/java/org/prgrms/nabimarketbe/card/service/CardServiceTest.java @@ -249,4 +249,53 @@ void updateCardById() { .usingRecursiveComparison() .isEqualTo(expectedResponse); } + + @Test + @DisplayName("특정 카드에 대한 정보를 조회한다.") + void getCardById() { + // given + String token = null; + + when(cardRepository.findActiveCardById(cardId)).thenReturn(Optional.of(card)); + when(cardImageRepository.findAllByCard(card)).thenReturn(images); + + CardUserResponseDTO expectedResponse = new CardUserResponseDTO( + new CardDetailResponseDTO( + cardId, + "macbook pro 교환 원해요", + "thumbnail_img_url", + CategoryEnum.ELECTRONICS, + "apple macbook pro", + true, + createdDate, + modifiedDate, + 0, + PriceRange.PRICE_RANGE_FOUR, + "상태 최상입니다.", + CardStatus.TRADE_AVAILABLE, + TradeType.DIRECT_DEALING, + "서울", + 0, + false, + Arrays.asList( + new CardImageSingleReadResponseDTO("url1"), + new CardImageSingleReadResponseDTO("url2"), + new CardImageSingleReadResponseDTO("url3") + ) + ), + new UserSummaryResponseDTO( + userId, + "doby", + "img_url" + ) + ); + + // when + CardUserResponseDTO actualResponse = cardService.getCardById(token, cardId); + + // then + assertThat(actualResponse) + .usingRecursiveComparison() + .isEqualTo(expectedResponse); + } } \ No newline at end of file From 81da13bdb6ad9158f8aa863321d7fdc3ceae2555 Mon Sep 17 00:00:00 2001 From: hi-june Date: Sat, 27 Jan 2024 00:08:37 +0900 Subject: [PATCH 6/7] =?UTF-8?q?NO-JIRA--test:=20=EC=B9=B4=EB=93=9C=20?= =?UTF-8?q?=EC=83=81=ED=83=9C=20=EB=B3=80=EA=B2=BD=20=EB=A9=94=EC=86=8C?= =?UTF-8?q?=EB=93=9C=20=ED=85=8C=EC=8A=A4=ED=8A=B8=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../card/service/CardServiceTest.java | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/nabimarket-application/src/test/java/org/prgrms/nabimarketbe/card/service/CardServiceTest.java b/nabimarket-application/src/test/java/org/prgrms/nabimarketbe/card/service/CardServiceTest.java index 5ffc2f71..f915f6a8 100644 --- a/nabimarket-application/src/test/java/org/prgrms/nabimarketbe/card/service/CardServiceTest.java +++ b/nabimarket-application/src/test/java/org/prgrms/nabimarketbe/card/service/CardServiceTest.java @@ -298,4 +298,22 @@ void getCardById() { .usingRecursiveComparison() .isEqualTo(expectedResponse); } + + @Test + @DisplayName("유저는 자신의 카드의 상태값을 변경할 수 있다.") + void updateCardStatusById() { + // given + CardStatusUpdateRequestDTO statusUpdateRequest = new CardStatusUpdateRequestDTO(CardStatus.RESERVED); + + when(checkService.parseToken(token)).thenReturn(userId); + when(userRepository.existsById(userId)).thenReturn(true); + when(cardRepository.findActiveCardById(cardId)).thenReturn(Optional.of(card)); + when(checkService.isEqual(userId, card.getUser().getUserId())).thenReturn(true); + + // when + cardService.updateCardStatusById(token, cardId, statusUpdateRequest); + + // then + assertThat(card.getStatus()).isEqualTo(statusUpdateRequest.status()); + } } \ No newline at end of file From 5c626890f4c5dd17c86f66eae756cbfdbf14653b Mon Sep 17 00:00:00 2001 From: hi-june Date: Sat, 27 Jan 2024 00:09:02 +0900 Subject: [PATCH 7/7] =?UTF-8?q?NO-JIRA--test:=20=EC=B9=B4=EB=93=9C=20?= =?UTF-8?q?=EB=B9=84=ED=99=9C=EC=84=B1=ED=99=94=20=ED=85=8C=EC=8A=A4?= =?UTF-8?q?=ED=8A=B8=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../card/service/CardServiceTest.java | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/nabimarket-application/src/test/java/org/prgrms/nabimarketbe/card/service/CardServiceTest.java b/nabimarket-application/src/test/java/org/prgrms/nabimarketbe/card/service/CardServiceTest.java index f915f6a8..8177b59a 100644 --- a/nabimarket-application/src/test/java/org/prgrms/nabimarketbe/card/service/CardServiceTest.java +++ b/nabimarket-application/src/test/java/org/prgrms/nabimarketbe/card/service/CardServiceTest.java @@ -316,4 +316,20 @@ void updateCardStatusById() { // then assertThat(card.getStatus()).isEqualTo(statusUpdateRequest.status()); } + + @Test + @DisplayName("유저는 자신의 카드를 비활성화시킬 수 있다.") + void deleteCardById() { + // given + when(checkService.parseToken(token)).thenReturn(userId); + when(userRepository.existsById(userId)).thenReturn(true); + when(cardRepository.findActiveCardById(cardId)).thenReturn(Optional.of(card)); + when(checkService.isEqual(userId, card.getUser().getUserId())).thenReturn(true); + + // when + cardService.deleteCardById(token, cardId); + + // then + assertThat(card.getIsActive()).isEqualTo(false); + } } \ No newline at end of file