-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #228 from Yanol-Market/feature/172
Feature/172 product 조회 및 삭제 테스트 코드 작성
- Loading branch information
Showing
6 changed files
with
354 additions
and
0 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
17 changes: 17 additions & 0 deletions
17
src/test/java/site/goldenticket/common/utils/ChatRoomUtils.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,17 @@ | ||
package site.goldenticket.common.utils; | ||
|
||
import site.goldenticket.domain.chat.entity.ChatRoom; | ||
import site.goldenticket.domain.product.model.Product; | ||
import site.goldenticket.domain.user.entity.User; | ||
|
||
public class ChatRoomUtils { | ||
|
||
private ChatRoomUtils() {} | ||
|
||
public static ChatRoom createChatRoom(Product product, User user) { | ||
return ChatRoom.builder() | ||
.productId(product.getId()) | ||
.buyerId(user.getId()) | ||
.build(); | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
src/test/java/site/goldenticket/common/utils/ChatUtils.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,31 @@ | ||
package site.goldenticket.common.utils; | ||
|
||
import site.goldenticket.domain.chat.entity.Chat; | ||
import site.goldenticket.domain.chat.entity.ChatRoom; | ||
import site.goldenticket.domain.chat.entity.SenderType; | ||
import site.goldenticket.domain.user.entity.User; | ||
|
||
import static site.goldenticket.domain.chat.entity.SenderType.BUYER; | ||
|
||
public class ChatUtils { | ||
private static final SenderType senderType = BUYER; | ||
public static final String content = "채팅 메시지 내용"; | ||
private static final Boolean viewedBySeller = false; | ||
private static final Boolean viewedByBuyer = true; | ||
|
||
private ChatUtils() {} | ||
|
||
public static Chat createChat(ChatRoom chatRoom, User user) { | ||
Long chatRoomId = chatRoom.getId(); | ||
Long userId = user.getId(); | ||
|
||
return Chat.builder() | ||
.chatRoomId(chatRoomId) | ||
.senderType(senderType) | ||
.userId(userId) | ||
.content(content) | ||
.viewedBySeller(viewedBySeller) | ||
.viewedByBuyer(viewedByBuyer) | ||
.build(); | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
src/test/java/site/goldenticket/common/utils/NegoUtils.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,39 @@ | ||
package site.goldenticket.common.utils; | ||
|
||
import site.goldenticket.domain.nego.entity.Nego; | ||
import site.goldenticket.domain.nego.status.NegotiationStatus; | ||
import site.goldenticket.domain.product.model.Product; | ||
import site.goldenticket.domain.user.entity.User; | ||
|
||
import java.time.LocalDateTime; | ||
|
||
import static site.goldenticket.domain.nego.status.NegotiationStatus.NEGOTIATING; | ||
|
||
public class NegoUtils { | ||
private static final Integer price = 50000; | ||
private static final Integer count = 1; | ||
private static final NegotiationStatus status = NEGOTIATING; | ||
private static final boolean consent = false; | ||
private static final LocalDateTime expirationTime = LocalDateTime.now().plusMinutes(20); | ||
private static final LocalDateTime createdAt = LocalDateTime.now(); | ||
private static final LocalDateTime updatedAt = LocalDateTime.now(); | ||
|
||
private NegoUtils() {} | ||
|
||
public static Nego createNego(Product product, User user) { | ||
Nego nego = Nego.builder() | ||
.price(price) | ||
.count(count) | ||
.status(NEGOTIATING) | ||
.consent(false) | ||
.expirationTime(expirationTime) | ||
.createdAt(createdAt) | ||
.updatedAt(updatedAt) | ||
.build(); | ||
|
||
nego.setProduct(product); | ||
nego.setUser(user); | ||
|
||
return nego; | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
src/test/java/site/goldenticket/common/utils/OrderUtils.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,31 @@ | ||
package site.goldenticket.common.utils; | ||
|
||
import site.goldenticket.common.constants.OrderStatus; | ||
import site.goldenticket.domain.nego.status.NegotiationStatus; | ||
import site.goldenticket.domain.payment.model.Order; | ||
import site.goldenticket.domain.product.model.Product; | ||
import site.goldenticket.domain.user.entity.User; | ||
|
||
import static site.goldenticket.common.constants.OrderStatus.COMPLETED_TRANSFER; | ||
import static site.goldenticket.domain.nego.status.NegotiationStatus.TRANSFER_PENDING; | ||
|
||
public class OrderUtils { | ||
private static final OrderStatus status = COMPLETED_TRANSFER; | ||
private static final NegotiationStatus negoStatus = TRANSFER_PENDING; | ||
private static final Integer price = 50000; | ||
|
||
private OrderUtils() {} | ||
|
||
public static Order createOrder(Product product, User user){ | ||
Long productId = product.getId(); | ||
Long userId = user.getId(); | ||
|
||
return Order.builder() | ||
.productId(productId) | ||
.userId(userId) | ||
.status(status) | ||
.negoStatus(negoStatus) | ||
.price(price) | ||
.build(); | ||
} | ||
} |
234 changes: 234 additions & 0 deletions
234
src/test/java/site/goldenticket/domain/product/controller/ProductControllerTest.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,234 @@ | ||
package site.goldenticket.domain.product.controller; | ||
|
||
import io.restassured.RestAssured; | ||
import io.restassured.path.json.JsonPath; | ||
import io.restassured.response.ExtractableResponse; | ||
import io.restassured.response.Response; | ||
import io.restassured.specification.RequestSpecification; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.http.HttpStatus; | ||
import site.goldenticket.common.config.ApiTest; | ||
import site.goldenticket.domain.chat.entity.Chat; | ||
import site.goldenticket.domain.chat.entity.ChatRoom; | ||
import site.goldenticket.domain.chat.repository.ChatRepository; | ||
import site.goldenticket.domain.chat.repository.ChatRoomRepository; | ||
import site.goldenticket.domain.nego.entity.Nego; | ||
import site.goldenticket.domain.nego.repository.NegoRepository; | ||
import site.goldenticket.domain.payment.model.Order; | ||
import site.goldenticket.domain.payment.repository.OrderRepository; | ||
import site.goldenticket.domain.product.model.Product; | ||
import site.goldenticket.domain.product.repository.ProductRepository; | ||
|
||
import java.util.List; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.junit.jupiter.api.Assertions.assertAll; | ||
import static site.goldenticket.common.utils.ChatRoomUtils.createChatRoom; | ||
import static site.goldenticket.common.utils.ChatUtils.createChat; | ||
import static site.goldenticket.common.utils.NegoUtils.createNego; | ||
import static site.goldenticket.common.utils.OrderUtils.createOrder; | ||
import static site.goldenticket.common.utils.ProductUtils.createProduct; | ||
import static site.goldenticket.domain.product.constants.ProductStatus.EXPIRED; | ||
import static site.goldenticket.domain.product.constants.ProductStatus.SOLD_OUT; | ||
|
||
public class ProductControllerTest extends ApiTest { | ||
|
||
@Autowired | ||
private ProductRepository productRepository; | ||
|
||
@Autowired | ||
private ChatRoomRepository chatRoomRepository; | ||
|
||
@Autowired | ||
private ChatRepository chatRepository; | ||
|
||
@Autowired | ||
private NegoRepository negoRepository; | ||
|
||
@Autowired | ||
private OrderRepository orderRepository; | ||
|
||
@Test | ||
void getProduct() { | ||
// given | ||
Product product = saveProduct(); | ||
|
||
String url = "/products/" + product.getId(); | ||
|
||
// when | ||
final ExtractableResponse<Response> response = performGetRequest(url, false); | ||
|
||
// then | ||
assertThat(response.statusCode()).isEqualTo(HttpStatus.OK.value()); | ||
} | ||
|
||
@Test | ||
void deleteProduct() { | ||
// given | ||
Product product = saveProduct(); | ||
|
||
String url = "/products/" + product.getId(); | ||
|
||
// when | ||
final ExtractableResponse<Response> response = performDeleteRequest(url); | ||
|
||
// then | ||
assertThat(response.statusCode()).isEqualTo(HttpStatus.OK.value()); | ||
} | ||
|
||
@Test | ||
void getProgressProducts() { | ||
// given | ||
Product product = saveProduct(); | ||
ChatRoom chatRoom = saveChatRoom(product); | ||
saveChat(chatRoom); | ||
saveNego(product); | ||
|
||
String url = "/products/history/progress"; | ||
|
||
// when | ||
final ExtractableResponse<Response> response = performGetRequest(url, true); | ||
|
||
// then | ||
assertThat(response.statusCode()).isEqualTo(HttpStatus.OK.value()); | ||
final JsonPath result = response.jsonPath(); | ||
assertAll( | ||
() -> assertThat(result.getList("data.productId", Long.class)).contains(product.getId()), | ||
() -> assertThat(result.getList("data.chats[0].chatRoomId", Long.class)).contains(chatRoom.getId()) | ||
); | ||
} | ||
|
||
@Test | ||
void getAllCompletedProducts() { | ||
// given | ||
List<Product> products = createCompletedProductsList(); | ||
|
||
String url = "/products/history/completed"; | ||
|
||
// when | ||
final ExtractableResponse<Response> response = performGetRequest(url, true); | ||
|
||
// then | ||
assertThat(response.statusCode()).isEqualTo(HttpStatus.OK.value()); | ||
final JsonPath result = response.jsonPath(); | ||
assertAll( | ||
() -> assertThat(result.getLong("data[0].productId")).isEqualTo(products.get(0).getId().intValue()), | ||
() -> assertThat(result.getLong("data[1].productId")).isEqualTo(products.get(1).getId().intValue()) | ||
); | ||
} | ||
|
||
@Test | ||
void getCompletedProductDetails() { | ||
// given | ||
Product product = saveSoldOutProduct(); | ||
ChatRoom chatRoom = saveChatRoom(product); | ||
saveChat(chatRoom); | ||
saveOrder(product); | ||
|
||
String url = "/products/history/completed/" + product.getId(); | ||
String parameterName = "productStatus"; | ||
String parameterValues = product.getProductStatus().toString(); | ||
|
||
// when | ||
final ExtractableResponse<Response> response = performGetRequestWithQueryParam(url, parameterName, parameterValues, true); | ||
|
||
// then | ||
assertThat(response.statusCode()).isEqualTo(HttpStatus.OK.value()); | ||
final JsonPath result = response.jsonPath(); | ||
assertThat(result.getLong("data.productId")).isEqualTo(product.getId()); | ||
} | ||
|
||
@Test | ||
void deleteCompletedProduct(){ | ||
// given | ||
Product product = saveSoldOutProduct(); | ||
|
||
String url = "/products/history/completed/" + product.getId(); | ||
|
||
// when | ||
final ExtractableResponse<Response> response = performDeleteRequest(url); | ||
|
||
// then | ||
assertThat(response.statusCode()).isEqualTo(HttpStatus.OK.value()); | ||
} | ||
|
||
private Product saveProduct() { | ||
Product product = createProduct(); | ||
return productRepository.save(product); | ||
} | ||
|
||
private Product saveSoldOutProduct() { | ||
Product product = createProduct(); | ||
product.setProductStatus(SOLD_OUT); | ||
return productRepository.save(product); | ||
} | ||
|
||
private Product saveExpiredProduct() { | ||
Product product = createProduct(); | ||
product.setProductStatus(EXPIRED); | ||
return productRepository.save(product); | ||
} | ||
|
||
private List<Product> createCompletedProductsList() { | ||
return List.of(saveSoldOutProduct(), saveExpiredProduct()); | ||
} | ||
|
||
private ChatRoom saveChatRoom(Product product) { | ||
ChatRoom chatRoom = createChatRoom(product, user); | ||
return chatRoomRepository.save(chatRoom); | ||
} | ||
|
||
private void saveChat(ChatRoom chatRoom) { | ||
Chat chat = createChat(chatRoom, user); | ||
chatRepository.save(chat); | ||
} | ||
|
||
private void saveNego(Product product) { | ||
Nego nego = createNego(product, user); | ||
negoRepository.save(nego); | ||
} | ||
|
||
private void saveOrder(Product product) { | ||
Order order = createOrder(product, user); | ||
orderRepository.save(order); | ||
} | ||
|
||
private RequestSpecification setupRequestSpecification(boolean needsAuthentication) { | ||
RequestSpecification requestSpecification = RestAssured.given().log().all(); | ||
|
||
if (needsAuthentication) { | ||
requestSpecification.header("Authorization", "Bearer " + accessToken); | ||
} | ||
|
||
return requestSpecification; | ||
} | ||
|
||
private ExtractableResponse<Response> performGetRequest(String url, boolean needsAuthentication) { | ||
RequestSpecification requestSpecification = setupRequestSpecification(needsAuthentication); | ||
|
||
return requestSpecification | ||
.when().get(url) | ||
.then().log().all() | ||
.extract(); | ||
} | ||
|
||
private ExtractableResponse<Response> performGetRequestWithQueryParam(String url, String parameterName, String parameterValues, boolean needsAuthentication) { | ||
RequestSpecification requestSpecification = setupRequestSpecification(needsAuthentication); | ||
|
||
return requestSpecification | ||
.queryParam(parameterName, parameterValues) | ||
.when().get(url) | ||
.then().log().all() | ||
.extract(); | ||
} | ||
|
||
private ExtractableResponse<Response> performDeleteRequest(String url) { | ||
RequestSpecification requestSpecification = setupRequestSpecification(true); | ||
|
||
return requestSpecification | ||
.when().delete(url) | ||
.then().log().all() | ||
.extract(); | ||
} | ||
} |