From 8e7eba3299e7b4c0e3a908d4943b51c893b2ec66 Mon Sep 17 00:00:00 2001 From: kimsuyeondev Date: Tue, 21 May 2024 00:18:15 +0900 Subject: [PATCH] =?UTF-8?q?#30=20=EC=83=81=ED=92=88=20=ED=85=8C=EC=8A=A4?= =?UTF-8?q?=ED=8A=B8=20=EC=9E=91=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../goods/GoodsApiApplicationTest.java | 18 ++++++++---------- .../goods/dto/GoodsManagementRequest.java | 5 ++++- .../goods/entity/GoodsManagementEntity.java | 2 +- .../domain/goods/service/GoodsService.java | 1 - 4 files changed, 13 insertions(+), 13 deletions(-) diff --git a/module-api/src/test/java/com/cosmetics/goods/GoodsApiApplicationTest.java b/module-api/src/test/java/com/cosmetics/goods/GoodsApiApplicationTest.java index 907f3c7..67cf5c9 100644 --- a/module-api/src/test/java/com/cosmetics/goods/GoodsApiApplicationTest.java +++ b/module-api/src/test/java/com/cosmetics/goods/GoodsApiApplicationTest.java @@ -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; @@ -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(); @@ -77,9 +76,9 @@ 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 responseEntity = testRestTemplate.getForEntity(url, GoodsManagement.class,1L); + ResponseEntity responseEntity = testRestTemplate.getForEntity(url, GoodsManagement.class, 1L); assertThat(responseEntity.getStatusCode()).isEqualTo(HttpStatus.OK); assertThat(responseEntity.getBody().getGoodsNm()).isEqualTo("닥터스킨"); } @@ -87,11 +86,10 @@ private static GoodsManagementRequest requestGoods() { @DisplayName("상품삭제") @Test @Order(3) - public void 상품삭제() throws Exception{ - String url = "http://localhost:" + port + "/v1/goods/{goodsNo}"; - ResponseEntity responseEntity = testRestTemplate.exchange(url, HttpMethod.DELETE, null, Map.class, 1L); + public void 상품삭제() throws Exception { + String url = "http://localhost:" + port + "/v1/goods/{goodsNo}"; + ResponseEntity responseEntity = testRestTemplate.exchange(url, HttpMethod.DELETE, null, GoodsManagementResponse.class, 1L); assertThat(responseEntity.getStatusCode()).isEqualTo(HttpStatus.OK); - assertThat(responseEntity.getBody().get("resultCode")).isEqualTo("0000"); } @Test @@ -100,8 +98,8 @@ private static GoodsManagementRequest requestGoods() { public void illegalGoodsTest() throws Exception { //통합테스트에서도 이런 테스트를 해보는게 맞는걸까? 확인필요 String goodsNo = "존재하지않는상품번호"; - String url = "http://localhost:" + port + "/v1/goods/{goodsNo}"; - ResponseEntity responseEntity = testRestTemplate.exchange(url, HttpMethod.DELETE, null, GoodsManagement.class, goodsNo); + String url = "http://localhost:" + port + "/v1/goods/{goodsNo}"; + ResponseEntity 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("존재하지 않는 상품입니다")); diff --git a/module-domain/src/main/java/com/cosmetics/domain/goods/dto/GoodsManagementRequest.java b/module-domain/src/main/java/com/cosmetics/domain/goods/dto/GoodsManagementRequest.java index 8ca84d0..6344c58 100644 --- a/module-domain/src/main/java/com/cosmetics/domain/goods/dto/GoodsManagementRequest.java +++ b/module-domain/src/main/java/com/cosmetics/domain/goods/dto/GoodsManagementRequest.java @@ -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 @@ -88,7 +90,8 @@ public GoodsManagementRequest(Long goodsNo, } public List toGoodsItemManagementList(List item){ - return item.stream() + return Optional.ofNullable(item).orElse(new ArrayList<>()) + .stream() .map(GoodsItemManagement::new).collect(Collectors.toList()); } diff --git a/module-domain/src/main/java/com/cosmetics/domain/goods/entity/GoodsManagementEntity.java b/module-domain/src/main/java/com/cosmetics/domain/goods/entity/GoodsManagementEntity.java index 29fe677..7469e2c 100644 --- a/module-domain/src/main/java/com/cosmetics/domain/goods/entity/GoodsManagementEntity.java +++ b/module-domain/src/main/java/com/cosmetics/domain/goods/entity/GoodsManagementEntity.java @@ -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 items = new ArrayList<>(); private String image; diff --git a/module-domain/src/main/java/com/cosmetics/domain/goods/service/GoodsService.java b/module-domain/src/main/java/com/cosmetics/domain/goods/service/GoodsService.java index 5a65420..dc95c2e 100644 --- a/module-domain/src/main/java/com/cosmetics/domain/goods/service/GoodsService.java +++ b/module-domain/src/main/java/com/cosmetics/domain/goods/service/GoodsService.java @@ -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", "등록성공");