Skip to content

Commit

Permalink
#30 상품 테스트 작성
Browse files Browse the repository at this point in the history
  • Loading branch information
kimsuyeondev committed May 20, 2024
1 parent 17b7c73 commit 8e7eba3
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertNotNull;
Expand Down Expand Up @@ -63,7 +62,7 @@ private static GoodsManagementRequest requestGoods() {
@DisplayName("상품등록")
@Test
@Order(1)
public void 상품등록() throws Exception{
public void 상품등록() throws Exception {
String url = "http://localhost:" + port + "/v1/goods";
GoodsManagementRequest goodsManagementRequest = requestGoods();

Expand All @@ -77,21 +76,20 @@ private static GoodsManagementRequest requestGoods() {
@DisplayName("상품조회")
@Test
@Order(2)
public void 상품조회() throws Exception{
public void 상품조회() throws Exception {
String url = "http://localhost:" + port + "/v1/goods/{goodsNo}";
ResponseEntity<GoodsManagement> responseEntity = testRestTemplate.getForEntity(url, GoodsManagement.class,1L);
ResponseEntity<GoodsManagement> responseEntity = testRestTemplate.getForEntity(url, GoodsManagement.class, 1L);
assertThat(responseEntity.getStatusCode()).isEqualTo(HttpStatus.OK);
assertThat(responseEntity.getBody().getGoodsNm()).isEqualTo("닥터스킨");
}

@DisplayName("상품삭제")
@Test
@Order(3)
public void 상품삭제() throws Exception{
String url = "http://localhost:" + port + "/v1/goods/{goodsNo}";
ResponseEntity<Map> responseEntity = testRestTemplate.exchange(url, HttpMethod.DELETE, null, Map.class, 1L);
public void 상품삭제() throws Exception {
String url = "http://localhost:" + port + "/v1/goods/{goodsNo}";
ResponseEntity<GoodsManagementResponse> responseEntity = testRestTemplate.exchange(url, HttpMethod.DELETE, null, GoodsManagementResponse.class, 1L);
assertThat(responseEntity.getStatusCode()).isEqualTo(HttpStatus.OK);
assertThat(responseEntity.getBody().get("resultCode")).isEqualTo("0000");
}

@Test
Expand All @@ -100,8 +98,8 @@ private static GoodsManagementRequest requestGoods() {
public void illegalGoodsTest() throws Exception {
//통합테스트에서도 이런 테스트를 해보는게 맞는걸까? 확인필요
String goodsNo = "존재하지않는상품번호";
String url = "http://localhost:" + port + "/v1/goods/{goodsNo}";
ResponseEntity<GoodsManagement> responseEntity = testRestTemplate.exchange(url, HttpMethod.DELETE, null, GoodsManagement.class, goodsNo);
String url = "http://localhost:" + port + "/v1/goods/{goodsNo}";
ResponseEntity<GoodsManagementResponse> responseEntity = testRestTemplate.exchange(url, HttpMethod.DELETE, null, GoodsManagementResponse.class, goodsNo);
assertThat(responseEntity.getStatusCode()).isEqualTo(HttpStatus.BAD_REQUEST);
assertThat(jsonPath("errorCode").value("INVALID_PARAMETER"));
assertThat(jsonPath("errorMessage").value("존재하지 않는 상품입니다"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
import lombok.Getter;
import lombok.NoArgsConstructor;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

@Getter
Expand Down Expand Up @@ -88,7 +90,8 @@ public GoodsManagementRequest(Long goodsNo,
}

public List<GoodsItemManagement> toGoodsItemManagementList(List<GoodsItemManagementRequest> item){
return item.stream()
return Optional.ofNullable(item).orElse(new ArrayList<>())
.stream()
.map(GoodsItemManagement::new).collect(Collectors.toList());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public class GoodsManagementEntity {
@LastModifiedDate
private LocalDateTime updateDtime;

@OneToMany(mappedBy = "goodsManagementEntity", cascade = CascadeType.PERSIST) // 보통 외래키가 있는 쪽이 주인, 주인은 mappyedBy를 설정하지 않음
@OneToMany(mappedBy = "goodsManagementEntity", cascade = CascadeType.PERSIST, orphanRemoval=true) // 보통 외래키가 있는 쪽이 주인, 주인은 mappyedBy를 설정하지 않음
private List<GoodsItemManagementEntity> items = new ArrayList<>();

private String image;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ public GoodsManagementResponse save(GoodsManagement goodsManagement) {
//dto -> response
GoodsManagementResponse resultGoodsManagementResponse = GoodsManagementResponse.toResponseDto(resultGoodsManagement);
log.error("{}", resultGoodsManagement.getGoodsNo());
log.error("{}", resultGoodsManagement.getItems().get(0).getItemNo());

if (resultGoodsManagement.getGoodsNo() != null) {
resultGoodsManagementResponse.updateSuccess("0000", "등록성공");
Expand Down

0 comments on commit 8e7eba3

Please sign in to comment.