From 0f9c38ab7fadf97421da0ba0a92eeb7f9ab8fb72 Mon Sep 17 00:00:00 2001 From: Subi Hwang Date: Thu, 29 Feb 2024 14:31:08 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20result=20post=20api=20test=20=EA=B5=AC?= =?UTF-8?q?=ED=98=84=20#59?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../accessory/service/AccessoryService.java | 23 +- .../custom/color/service/ColorService.java | 4 + .../oot/custom/font/service/FontService.java | 5 + .../pattern/service/PatternService.java | 7 +- .../result/controller/ResultController.java | 49 ++-- .../result/dto/ResultGetResponseDto.java | 43 ++++ .../result/dto/ResultPostResponseDto.java | 17 ++ .../custom/result/dto/ResultRequestDto.java | 28 +++ .../custom/result/dto/ResultResponseDTO.java | 42 ---- .../oot/custom/result/entity/Result.java | 76 +++--- .../custom/result/service/ResultService.java | 51 +++- .../custom/wheel/service/WheelService.java | 21 +- .../controller/ResultControllerTest.java | 223 ++++++++++-------- 13 files changed, 374 insertions(+), 215 deletions(-) create mode 100644 src/main/java/com/endlesshorses/oot/custom/result/dto/ResultGetResponseDto.java create mode 100644 src/main/java/com/endlesshorses/oot/custom/result/dto/ResultPostResponseDto.java create mode 100644 src/main/java/com/endlesshorses/oot/custom/result/dto/ResultRequestDto.java delete mode 100644 src/main/java/com/endlesshorses/oot/custom/result/dto/ResultResponseDTO.java diff --git a/src/main/java/com/endlesshorses/oot/custom/accessory/service/AccessoryService.java b/src/main/java/com/endlesshorses/oot/custom/accessory/service/AccessoryService.java index 5f46a20..023b4ac 100644 --- a/src/main/java/com/endlesshorses/oot/custom/accessory/service/AccessoryService.java +++ b/src/main/java/com/endlesshorses/oot/custom/accessory/service/AccessoryService.java @@ -1,10 +1,11 @@ package com.endlesshorses.oot.custom.accessory.service; import com.endlesshorses.oot.custom.accessory.dto.AccessoryListResponseDto; +import com.endlesshorses.oot.custom.accessory.enity.Accessory; import com.endlesshorses.oot.custom.accessory.repository.AccessoryRepository; -import org.springframework.transaction.annotation.Transactional; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; import java.util.List; import java.util.stream.Collectors; @@ -12,13 +13,17 @@ @Service @RequiredArgsConstructor public class AccessoryService { - private final AccessoryRepository accessoryRepository; + private final AccessoryRepository accessoryRepository; + + // 액세서리 전체 목록 조회 + @Transactional(readOnly = true) + public List list() { + return accessoryRepository.findAll().stream() + .map(AccessoryListResponseDto::new) + .collect(Collectors.toList()); + } - // 액세서리 전체 목록 조회 - @Transactional(readOnly = true) - public List list() { - return accessoryRepository.findAll().stream() - .map(AccessoryListResponseDto::new) - .collect(Collectors.toList()); - } + public Accessory read(Long id) { + return accessoryRepository.findById(id).orElseThrow(); + } } diff --git a/src/main/java/com/endlesshorses/oot/custom/color/service/ColorService.java b/src/main/java/com/endlesshorses/oot/custom/color/service/ColorService.java index 4520721..233de76 100644 --- a/src/main/java/com/endlesshorses/oot/custom/color/service/ColorService.java +++ b/src/main/java/com/endlesshorses/oot/custom/color/service/ColorService.java @@ -1,6 +1,7 @@ package com.endlesshorses.oot.custom.color.service; import com.endlesshorses.oot.custom.color.dto.ColorListResponseDto; +import com.endlesshorses.oot.custom.color.entity.Color; import com.endlesshorses.oot.custom.color.repository.ColorRepository; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; @@ -21,4 +22,7 @@ public List list() { .collect(Collectors.toList()); } + public Color read(Long id) { + return colorRepository.findById(id).orElseThrow(); + } } diff --git a/src/main/java/com/endlesshorses/oot/custom/font/service/FontService.java b/src/main/java/com/endlesshorses/oot/custom/font/service/FontService.java index 2aad0bb..366e726 100644 --- a/src/main/java/com/endlesshorses/oot/custom/font/service/FontService.java +++ b/src/main/java/com/endlesshorses/oot/custom/font/service/FontService.java @@ -1,6 +1,7 @@ package com.endlesshorses.oot.custom.font.service; import com.endlesshorses.oot.custom.font.dto.FontListResponseDto; +import com.endlesshorses.oot.custom.font.entity.Font; import com.endlesshorses.oot.custom.font.repository.FontRepository; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; @@ -20,4 +21,8 @@ public List list() { .map(FontListResponseDto::new) .collect(Collectors.toList()); } + + public Font read(Long id) { + return fontRepository.findById(id).orElseThrow(); + } } diff --git a/src/main/java/com/endlesshorses/oot/custom/pattern/service/PatternService.java b/src/main/java/com/endlesshorses/oot/custom/pattern/service/PatternService.java index 5cb09e8..110b1e5 100644 --- a/src/main/java/com/endlesshorses/oot/custom/pattern/service/PatternService.java +++ b/src/main/java/com/endlesshorses/oot/custom/pattern/service/PatternService.java @@ -1,10 +1,9 @@ package com.endlesshorses.oot.custom.pattern.service; import com.endlesshorses.oot.custom.pattern.dto.PatternListResponseDto; +import com.endlesshorses.oot.custom.pattern.entity.Pattern; import com.endlesshorses.oot.custom.pattern.repository.PatternRepository; -import lombok.NoArgsConstructor; import lombok.RequiredArgsConstructor; -import lombok.Setter; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -24,5 +23,9 @@ public List list() { .collect(Collectors.toList()); } + public Pattern read(Long id) { + return patternRepository.findById(id).orElseThrow(); + } } + diff --git a/src/main/java/com/endlesshorses/oot/custom/result/controller/ResultController.java b/src/main/java/com/endlesshorses/oot/custom/result/controller/ResultController.java index d4c3759..d3ec724 100644 --- a/src/main/java/com/endlesshorses/oot/custom/result/controller/ResultController.java +++ b/src/main/java/com/endlesshorses/oot/custom/result/controller/ResultController.java @@ -1,6 +1,8 @@ package com.endlesshorses.oot.custom.result.controller; -import com.endlesshorses.oot.custom.result.dto.ResultResponseDTO; +import com.endlesshorses.oot.custom.result.dto.ResultGetResponseDto; +import com.endlesshorses.oot.custom.result.dto.ResultPostResponseDto; +import com.endlesshorses.oot.custom.result.dto.ResultRequestDto; import com.endlesshorses.oot.custom.result.service.ResultService; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.responses.ApiResponse; @@ -10,23 +12,40 @@ import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; +import java.io.IOException; + @RestController @RequiredArgsConstructor @Tag(name = "Result", description = "타이어 커스텀 결과물 관련 API") @RequestMapping("/api/results") public class ResultController { - private final ResultService resultService; - - @Operation(summary = "타이어 커스텀 결과물 조회 메서드", description = "클라이언트가 요청한 타이어 결과물 정보를 조회하기 위한 메서드") - @GetMapping("/{id}") - @ResponseBody - @ApiResponses({ - @ApiResponse(responseCode = "200", description = "결과물 조회 성공"), - @ApiResponse(responseCode = "400", description = "잘못된 요청, UUID 형식이 아닌 id가 입력된 경우"), - @ApiResponse(responseCode = "404", description = "결과물을 찾을 수 없음, 주어진 id에 해당하는 결과물이 없는 경우"), - @ApiResponse(responseCode = "500", description = "서버 내부 오류"), - }) - public ResponseEntity read(@PathVariable String id) { - return ResponseEntity.ok(resultService.read(id)); - } + private final ResultService resultService; + + @Operation(summary = "타이어 커스텀 결과물 조회 메서드", description = "클라이언트가 요청한 타이어 결과물 정보를 조회하기 위한 메서드") + @GetMapping("/{id}") + @ResponseBody + @ApiResponses({ + @ApiResponse(responseCode = "200", description = "결과물 조회 성공"), + @ApiResponse(responseCode = "400", description = "잘못된 요청, UUID 형식이 아닌 id가 입력된 경우"), + @ApiResponse(responseCode = "404", description = "결과물을 찾을 수 없음, 주어진 id에 해당하는 결과물이 없는 경우"), + @ApiResponse(responseCode = "500", description = "서버 내부 오류"), + }) + + public ResponseEntity read(@PathVariable String id) { + return ResponseEntity.ok(resultService.read(id)); + } + + @PostMapping + @Operation(summary = "타이어 커스텀 결과물 저장", description = "타이어 커스텀 결과물을 저장합니다.") + @ApiResponses({ + @ApiResponse(responseCode = "200(202)", description = "정보 등록 요청"), + }) //ResultResponseDto를 프론트에게 ResponseEntity를 통해서 전달하겠다! + public ResponseEntity create(@RequestBody ResultRequestDto resultRequestDto) throws + IOException { + return ResponseEntity.ok( + resultService.create(resultRequestDto) + ); + } + + } \ No newline at end of file diff --git a/src/main/java/com/endlesshorses/oot/custom/result/dto/ResultGetResponseDto.java b/src/main/java/com/endlesshorses/oot/custom/result/dto/ResultGetResponseDto.java new file mode 100644 index 0000000..5a8dc4e --- /dev/null +++ b/src/main/java/com/endlesshorses/oot/custom/result/dto/ResultGetResponseDto.java @@ -0,0 +1,43 @@ +package com.endlesshorses.oot.custom.result.dto; + +import com.endlesshorses.oot.custom.accessory.enity.Accessory; +import com.endlesshorses.oot.custom.color.entity.Color; +import com.endlesshorses.oot.custom.font.entity.Font; +import com.endlesshorses.oot.custom.pattern.entity.Pattern; +import com.endlesshorses.oot.custom.result.entity.Result; +import com.endlesshorses.oot.custom.wheel.entity.Wheel; +import lombok.Builder; +import lombok.Getter; +import lombok.NoArgsConstructor; + +import java.time.LocalDateTime; + +@Getter +@NoArgsConstructor +public class ResultGetResponseDto { + private String id; + + private Pattern pattern; + + private Wheel wheel; + + private Font font; + + private Color color; + + private Accessory accessory; + + private LocalDateTime createdAt; + + @Builder + public ResultGetResponseDto(Result result) { + this.id = result.getId(); + this.pattern = result.getPattern(); + this.wheel = result.getWheel(); + this.font = result.getFont(); + this.color = result.getColor(); + this.accessory = result.getAccessory(); + this.createdAt = result.getCreatedAt(); + } + +} \ No newline at end of file diff --git a/src/main/java/com/endlesshorses/oot/custom/result/dto/ResultPostResponseDto.java b/src/main/java/com/endlesshorses/oot/custom/result/dto/ResultPostResponseDto.java new file mode 100644 index 0000000..b4b9f53 --- /dev/null +++ b/src/main/java/com/endlesshorses/oot/custom/result/dto/ResultPostResponseDto.java @@ -0,0 +1,17 @@ +package com.endlesshorses.oot.custom.result.dto; + +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.Setter; + +@Getter +@Setter +@NoArgsConstructor +public class ResultPostResponseDto { //주는 DTO + private String id; + + public ResultPostResponseDto(String id) { + this.id = id; + } + +} \ No newline at end of file diff --git a/src/main/java/com/endlesshorses/oot/custom/result/dto/ResultRequestDto.java b/src/main/java/com/endlesshorses/oot/custom/result/dto/ResultRequestDto.java new file mode 100644 index 0000000..6be34ce --- /dev/null +++ b/src/main/java/com/endlesshorses/oot/custom/result/dto/ResultRequestDto.java @@ -0,0 +1,28 @@ +package com.endlesshorses.oot.custom.result.dto; + +import lombok.Builder; +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.Setter; + +@Setter +@Getter +@NoArgsConstructor +public +class ResultRequestDto { //요청 DTO + private Long patternId; + private Long wheelId; + private Long fontId; + private Long colorId; + private Long accessoryId; + + @Builder + public ResultRequestDto(Long patternId, Long wheelId, Long fontId, Long colorId, Long accessoryId) { + this.patternId = patternId; + this.wheelId = wheelId; + this.fontId = fontId; + this.colorId = colorId; + this.accessoryId = accessoryId; + } + +} \ No newline at end of file diff --git a/src/main/java/com/endlesshorses/oot/custom/result/dto/ResultResponseDTO.java b/src/main/java/com/endlesshorses/oot/custom/result/dto/ResultResponseDTO.java deleted file mode 100644 index 14c1729..0000000 --- a/src/main/java/com/endlesshorses/oot/custom/result/dto/ResultResponseDTO.java +++ /dev/null @@ -1,42 +0,0 @@ -package com.endlesshorses.oot.custom.result.dto; - -import com.endlesshorses.oot.custom.accessory.enity.Accessory; -import com.endlesshorses.oot.custom.color.entity.Color; -import com.endlesshorses.oot.custom.font.entity.Font; -import com.endlesshorses.oot.custom.pattern.entity.Pattern; -import com.endlesshorses.oot.custom.result.entity.Result; -import com.endlesshorses.oot.custom.wheel.entity.Wheel; -import lombok.Builder; -import lombok.Getter; -import lombok.NoArgsConstructor; - -import java.time.LocalDateTime; - -@Getter -@NoArgsConstructor -public class ResultResponseDTO { - private String id; - - private Pattern pattern; - - private Wheel wheel; - - private Font font; - - private Color color; - - private Accessory accessory; - - private LocalDateTime createdAt; - - @Builder - public ResultResponseDTO(Result result) { - this.id = result.getId(); - this.pattern = result.getPattern(); - this.wheel = result.getWheel(); - this.font = result.getFont(); - this.color = result.getColor(); - this.accessory = result.getAccessory(); - this.createdAt = result.getCreatedAt(); - } -} \ No newline at end of file diff --git a/src/main/java/com/endlesshorses/oot/custom/result/entity/Result.java b/src/main/java/com/endlesshorses/oot/custom/result/entity/Result.java index fc01915..cb692d3 100644 --- a/src/main/java/com/endlesshorses/oot/custom/result/entity/Result.java +++ b/src/main/java/com/endlesshorses/oot/custom/result/entity/Result.java @@ -22,41 +22,43 @@ @EntityListeners(AuditingEntityListener.class) public class Result { - @Id - private String id; - - @ManyToOne - @JoinColumn(name = "PATTERN_ID", nullable = false) - private Pattern pattern; - - @ManyToOne - @JoinColumn(name = "WHEEL_ID", nullable = false) - private Wheel wheel; - - @ManyToOne - @JoinColumn(name = "FONT_ID", nullable = false) - private Font font; - - @ManyToOne - @JoinColumn(name = "FONT_COLOR_ID", nullable = false) - private Color color; - - @ManyToOne - @JoinColumn(name = "ACCESSORY_ID") - private Accessory accessory; - - @Column(nullable = false) - @CreatedDate - private LocalDateTime createdAt; - - @Builder - public Result(String id, Pattern pattern, Wheel wheel, Font font, Color color, Accessory accessory) { - this.id = id; - this.pattern = pattern; - this.wheel = wheel; - this.font = font; - this.color = color; - this.accessory = accessory; - this.createdAt = LocalDateTime.now(); - } + @Id + private String id; + + @ManyToOne + @JoinColumn(name = "PATTERN_ID", nullable = false) + private Pattern pattern; + + @ManyToOne + @JoinColumn(name = "WHEEL_ID", nullable = false) + private Wheel wheel; + + @ManyToOne + @JoinColumn(name = "FONT_ID", nullable = false) + private Font font; + + @ManyToOne + @JoinColumn(name = "FONT_COLOR_ID", nullable = false) + private Color color; + + @ManyToOne + @JoinColumn(name = "ACCESSORY_ID") + private Accessory accessory; + + @Column(nullable = false) + @CreatedDate + private LocalDateTime createdAt; + + @Builder + public Result(String id, Pattern pattern, Wheel wheel, Font font, Color color, Accessory accessory) { + this.id = id; + this.pattern = pattern; + this.wheel = wheel; + this.font = font; + this.color = color; + this.accessory = accessory; + this.createdAt = LocalDateTime.now(); + } + + } \ No newline at end of file diff --git a/src/main/java/com/endlesshorses/oot/custom/result/service/ResultService.java b/src/main/java/com/endlesshorses/oot/custom/result/service/ResultService.java index 0f7ed68..2364689 100644 --- a/src/main/java/com/endlesshorses/oot/custom/result/service/ResultService.java +++ b/src/main/java/com/endlesshorses/oot/custom/result/service/ResultService.java @@ -1,18 +1,55 @@ package com.endlesshorses.oot.custom.result.service; -import com.endlesshorses.oot.custom.result.dto.ResultResponseDTO; +import com.endlesshorses.oot.custom.accessory.service.AccessoryService; +import com.endlesshorses.oot.custom.color.service.ColorService; +import com.endlesshorses.oot.custom.font.service.FontService; +import com.endlesshorses.oot.custom.pattern.service.PatternService; +import com.endlesshorses.oot.custom.result.dto.ResultGetResponseDto; +import com.endlesshorses.oot.custom.result.dto.ResultPostResponseDto; +import com.endlesshorses.oot.custom.result.dto.ResultRequestDto; +import com.endlesshorses.oot.custom.result.entity.Result; import com.endlesshorses.oot.custom.result.repository.ResultRepository; +import com.endlesshorses.oot.custom.wheel.service.WheelService; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; +import java.util.UUID; + @Service @RequiredArgsConstructor public class ResultService { - private final ResultRepository resultRepository; + private final ResultRepository resultRepository; + private final PatternService patternService; + private final WheelService wheelService; + private final ColorService colorService; + private final FontService fontService; + private final AccessoryService accessoryService; + + public ResultGetResponseDto read(String id) { + return resultRepository.findById(id) + .map(ResultGetResponseDto::new) + .orElseThrow(() -> new IllegalArgumentException("해당 id의 결과가 없습니다. id: " + id)); + } + + public ResultPostResponseDto create(ResultRequestDto resultRequestDto) { + + //새로운 result record 생성 (행) + Result result = Result.builder() + .id(generateUUID()) + .pattern(patternService.read(resultRequestDto.getPatternId())) + .wheel(wheelService.read(resultRequestDto.getWheelId())) + .color(colorService.read(resultRequestDto.getWheelId())) + .font(fontService.read(resultRequestDto.getFontId())) + .accessory(accessoryService.read(resultRequestDto.getAccessoryId())) + .build(); + resultRepository.save(result); + + return new ResultPostResponseDto(result.getId()); + + } + + public String generateUUID() { + return UUID.randomUUID().toString(); + } - public ResultResponseDTO read(String id) { - return resultRepository.findById(id) - .map(ResultResponseDTO::new) - .orElseThrow(() -> new IllegalArgumentException("해당 id의 결과가 없습니다. id: " + id)); - } } \ No newline at end of file diff --git a/src/main/java/com/endlesshorses/oot/custom/wheel/service/WheelService.java b/src/main/java/com/endlesshorses/oot/custom/wheel/service/WheelService.java index ba95572..ad372b5 100644 --- a/src/main/java/com/endlesshorses/oot/custom/wheel/service/WheelService.java +++ b/src/main/java/com/endlesshorses/oot/custom/wheel/service/WheelService.java @@ -1,6 +1,7 @@ package com.endlesshorses.oot.custom.wheel.service; import com.endlesshorses.oot.custom.wheel.dto.WheelListResponseDto; +import com.endlesshorses.oot.custom.wheel.entity.Wheel; import com.endlesshorses.oot.custom.wheel.repository.WheelRepository; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; @@ -12,13 +13,17 @@ @Service @RequiredArgsConstructor public class WheelService { - private final WheelRepository wheelRepository; + private final WheelRepository wheelRepository; - // 휠 모양 전체 목록 조회 - @Transactional(readOnly = true) - public List list() { - return wheelRepository.findAll().stream() - .map(WheelListResponseDto::new) - .collect(Collectors.toList()); - } + // 휠 모양 전체 목록 조회 + @Transactional(readOnly = true) + public List list() { + return wheelRepository.findAll().stream() + .map(WheelListResponseDto::new) + .collect(Collectors.toList()); + } + + public Wheel read(Long id) { + return wheelRepository.findById(id).orElseThrow(); + } } diff --git a/src/test/java/com/endlesshorses/oot/custom/result/controller/ResultControllerTest.java b/src/test/java/com/endlesshorses/oot/custom/result/controller/ResultControllerTest.java index c218e8a..7890ce9 100644 --- a/src/test/java/com/endlesshorses/oot/custom/result/controller/ResultControllerTest.java +++ b/src/test/java/com/endlesshorses/oot/custom/result/controller/ResultControllerTest.java @@ -4,10 +4,12 @@ import com.endlesshorses.oot.custom.color.entity.Color; import com.endlesshorses.oot.custom.font.entity.Font; import com.endlesshorses.oot.custom.pattern.entity.Pattern; -import com.endlesshorses.oot.custom.result.dto.ResultResponseDTO; +import com.endlesshorses.oot.custom.result.dto.ResultGetResponseDto; +import com.endlesshorses.oot.custom.result.dto.ResultRequestDto; import com.endlesshorses.oot.custom.result.entity.Result; import com.endlesshorses.oot.custom.result.service.ResultService; import com.endlesshorses.oot.custom.wheel.entity.Wheel; +import com.fasterxml.jackson.databind.ObjectMapper; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; import org.mockito.Mockito; @@ -17,106 +19,137 @@ import org.springframework.boot.test.mock.mockito.MockBean; import org.springframework.http.MediaType; import org.springframework.test.web.servlet.MockMvc; - -import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; -import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; +import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; +import org.springframework.test.web.servlet.result.MockMvcResultHandlers; +import org.springframework.test.web.servlet.result.MockMvcResultMatchers; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + @SpringBootTest @AutoConfigureMockMvc class ResultControllerTest { - @Autowired - private MockMvc mockMvc; - - @MockBean - private ResultService resultService; - - @Test - @DisplayName("GET /api/result/{id} 테스트") - public void read() throws Exception { - // given - String id = "b172456d-733d-4eed-9f15-c790e0cbc318"; - - Pattern pattern = new Pattern(); - pattern.setId(1004L); - pattern.setName("리브 패턴"); - pattern.setPrice(1004L); - pattern.setImageUrl("http://~"); - pattern.setExplanation("테스트!"); - - Wheel wheel = new Wheel(); - wheel.setId(1L); - wheel.setName("스포크 휠"); - wheel.setPrice(10000L); - wheel.setImageUrl("image_url_1"); - wheel.setExplanation("설명1~"); - - Font font = new Font(); - font.setId(1004L); - font.setName("폰트 색상"); - font.setPrice(5000L); - font.setImageUrl("http://~"); - - Color color = new Color(); - color.setId(1004L); - color.setName("색상 이름"); - color.setRgb("FFFFFF"); - color.setImageUrl("http://~"); - - Accessory accessory1 = new Accessory(); - accessory1.setId(1L); - accessory1.setName("휠 프로텍터"); - accessory1.setPrice(10000L); - accessory1.setImageUrl("imageUrl1"); - accessory1.setExplanation("설명1~"); - - Result result = new Result(); - result.setId("b172456d-733d-4eed-9f15-c790e0cbc318"); - result.setPattern(pattern); - result.setWheel(wheel); - result.setFont(font); - result.setColor(color); - result.setAccessory(accessory1); - result.setCreatedAt(LocalDateTime.now()); - - ResultResponseDTO resultDto = new ResultResponseDTO(result); - - // when - Mockito.when(resultService.read(result.getId())).thenReturn(resultDto); - - // then - mockMvc.perform(get("/api/results/{id}", id) - .contentType(MediaType.APPLICATION_JSON)) - .andExpect(jsonPath("$.id").value(resultDto.getId())) - .andExpect(jsonPath("$.pattern.id").value(resultDto.getPattern().getId())) - .andExpect(jsonPath("$.pattern.name").value(resultDto.getPattern().getName())) - .andExpect(jsonPath("$.pattern.price").value(resultDto.getPattern().getPrice())) - .andExpect(jsonPath("$.pattern.imageUrl").value(resultDto.getPattern().getImageUrl())) - .andExpect(jsonPath("$.pattern.explanation").value(resultDto.getPattern().getExplanation())) - .andExpect(jsonPath("$.wheel.id").value(resultDto.getWheel().getId())) - .andExpect(jsonPath("$.wheel.name").value(resultDto.getWheel().getName())) - .andExpect(jsonPath("$.wheel.price").value(resultDto.getWheel().getPrice())) - .andExpect(jsonPath("$.wheel.imageUrl").value(resultDto.getWheel().getImageUrl())) - .andExpect(jsonPath("$.wheel.explanation").value(resultDto.getWheel().getExplanation())) - .andExpect(jsonPath("$.font.id").value(resultDto.getFont().getId())) - .andExpect(jsonPath("$.font.name").value(resultDto.getFont().getName())) - .andExpect(jsonPath("$.font.price").value(resultDto.getFont().getPrice())) - .andExpect(jsonPath("$.font.imageUrl").value(resultDto.getFont().getImageUrl())) - .andExpect(jsonPath("$.color.id").value(resultDto.getColor().getId())) - .andExpect(jsonPath("$.color.name").value(resultDto.getColor().getName())) - .andExpect(jsonPath("$.color.rgb").value(resultDto.getColor().getRgb())) - .andExpect(jsonPath("$.color.imageUrl").value(resultDto.getColor().getImageUrl())) - .andExpect(jsonPath("$.accessory.id").value(resultDto.getAccessory().getId())) - .andExpect(jsonPath("$.accessory.name").value(resultDto.getAccessory().getName())) - .andExpect(jsonPath("$.accessory.price").value(resultDto.getAccessory().getPrice())) - .andExpect(jsonPath("$.accessory.imageUrl").value(resultDto.getAccessory().getImageUrl())) - .andExpect(jsonPath("$.accessory.explanation").value(resultDto.getAccessory().getExplanation())) - .andExpect(jsonPath("$.createdAt").value(resultDto.getCreatedAt().format(DateTimeFormatter.ISO_LOCAL_DATE_TIME))) - .andExpect(status().isOk()) - .andDo(print()); - } + @Autowired + private MockMvc mockMvc; + + @MockBean + private ResultService resultService; + + @Test + @DisplayName("GET /api/result/{id} 테스트") + public void read() throws Exception { + // given + String id = "b172456d-733d-4eed-9f15-c790e0cbc318"; + + Pattern pattern = new Pattern(); + pattern.setId(1004L); + pattern.setName("리브 패턴"); + pattern.setPrice(1004L); + pattern.setImageUrl("http://~"); + pattern.setExplanation("테스트!"); + + Wheel wheel = new Wheel(); + wheel.setId(1L); + wheel.setName("스포크 휠"); + wheel.setPrice(10000L); + wheel.setImageUrl("image_url_1"); + wheel.setExplanation("설명1~"); + + Font font = new Font(); + font.setId(1004L); + font.setName("폰트 색상"); + font.setPrice(5000L); + font.setImageUrl("http://~"); + + Color color = new Color(); + color.setId(1004L); + color.setName("색상 이름"); + color.setRgb("FFFFFF"); + color.setImageUrl("http://~"); + + Accessory accessory1 = new Accessory(); + accessory1.setId(1L); + accessory1.setName("휠 프로텍터"); + accessory1.setPrice(10000L); + accessory1.setImageUrl("imageUrl1"); + accessory1.setExplanation("설명1~"); + + Result result = new Result(); + result.setId("b172456d-733d-4eed-9f15-c790e0cbc318"); + result.setPattern(pattern); + result.setWheel(wheel); + result.setFont(font); + result.setColor(color); + result.setAccessory(accessory1); + result.setCreatedAt(LocalDateTime.now()); + + ResultGetResponseDto resultDto = new ResultGetResponseDto(result); + + // when + Mockito.when(resultService.read(result.getId())).thenReturn(resultDto); + + // then + mockMvc.perform(get("/api/results/{id}", id) + .contentType(MediaType.APPLICATION_JSON)) + .andExpect(jsonPath("$.id").value(resultDto.getId())) + .andExpect(jsonPath("$.pattern.id").value(resultDto.getPattern().getId())) + .andExpect(jsonPath("$.pattern.name").value(resultDto.getPattern().getName())) + .andExpect(jsonPath("$.pattern.price").value(resultDto.getPattern().getPrice())) + .andExpect(jsonPath("$.pattern.imageUrl").value(resultDto.getPattern().getImageUrl())) + .andExpect(jsonPath("$.pattern.explanation").value(resultDto.getPattern().getExplanation())) + .andExpect(jsonPath("$.wheel.id").value(resultDto.getWheel().getId())) + .andExpect(jsonPath("$.wheel.name").value(resultDto.getWheel().getName())) + .andExpect(jsonPath("$.wheel.price").value(resultDto.getWheel().getPrice())) + .andExpect(jsonPath("$.wheel.imageUrl").value(resultDto.getWheel().getImageUrl())) + .andExpect(jsonPath("$.wheel.explanation").value(resultDto.getWheel().getExplanation())) + .andExpect(jsonPath("$.font.id").value(resultDto.getFont().getId())) + .andExpect(jsonPath("$.font.name").value(resultDto.getFont().getName())) + .andExpect(jsonPath("$.font.price").value(resultDto.getFont().getPrice())) + .andExpect(jsonPath("$.font.imageUrl").value(resultDto.getFont().getImageUrl())) + .andExpect(jsonPath("$.color.id").value(resultDto.getColor().getId())) + .andExpect(jsonPath("$.color.name").value(resultDto.getColor().getName())) + .andExpect(jsonPath("$.color.rgb").value(resultDto.getColor().getRgb())) + .andExpect(jsonPath("$.color.imageUrl").value(resultDto.getColor().getImageUrl())) + .andExpect(jsonPath("$.accessory.id").value(resultDto.getAccessory().getId())) + .andExpect(jsonPath("$.accessory.name").value(resultDto.getAccessory().getName())) + .andExpect(jsonPath("$.accessory.price").value(resultDto.getAccessory().getPrice())) + .andExpect(jsonPath("$.accessory.imageUrl").value(resultDto.getAccessory().getImageUrl())) + .andExpect(jsonPath("$.accessory.explanation").value(resultDto.getAccessory().getExplanation())) + .andExpect(jsonPath("$.createdAt").value(resultDto.getCreatedAt().format(DateTimeFormatter.ISO_LOCAL_DATE_TIME))) + .andExpect(status().isOk()) + .andDo(print()); + } + + + @Test + @DisplayName("결과 저장하기 테스트") + public void getResultPostTest() throws Exception { + + ResultRequestDto resultRequestDto = new ResultRequestDto(); + resultRequestDto.setPatternId(1L); + resultRequestDto.setAccessoryId(1L); + resultRequestDto.setColorId(1L); + resultRequestDto.setFontId(1L); + resultRequestDto.setWheelId(1L); + + // ObjectMapper 객체를 사용해 RequestBody 부분 String으로 바꿔서 json 형태로 + String json = new ObjectMapper().writeValueAsString(resultRequestDto); + + mockMvc.perform( + MockMvcRequestBuilders.post("http://localhost:8080/api/results") + .contentType(MediaType.APPLICATION_JSON) + .content(json) // RequestBody + ).andExpect( + MockMvcResultMatchers.status().isOk() + ) + .andDo(MockMvcResultHandlers.print()); + + } + } \ No newline at end of file