#14 simple blog : use builder
This commit is contained in:
@@ -17,7 +17,10 @@ public class ExceptionController {
|
|||||||
public ErrorResponse invalidRequestHandler(MethodArgumentNotValidException e) {
|
public ErrorResponse invalidRequestHandler(MethodArgumentNotValidException e) {
|
||||||
|
|
||||||
// if (e.hasErrors()) {
|
// if (e.hasErrors()) {
|
||||||
ErrorResponse response = new ErrorResponse("400", "잘못된 요청입니다.");
|
ErrorResponse response = ErrorResponse.builder()
|
||||||
|
.code("400")
|
||||||
|
.message("잘못된 요청입니다.")
|
||||||
|
.build();
|
||||||
|
|
||||||
e.getFieldErrors().forEach(fieldError -> {
|
e.getFieldErrors().forEach(fieldError -> {
|
||||||
response.addValidation(fieldError.getField(), fieldError.getDefaultMessage());
|
response.addValidation(fieldError.getField(), fieldError.getDefaultMessage());
|
||||||
|
|||||||
@@ -18,8 +18,7 @@ public class PostController {
|
|||||||
private final PostService postService;
|
private final PostService postService;
|
||||||
|
|
||||||
@PostMapping("/posts")
|
@PostMapping("/posts")
|
||||||
public PostCreate post(@RequestBody @Valid PostCreate request) {
|
public void post(@RequestBody @Valid PostCreate request) {
|
||||||
postService.write(request);
|
postService.write(request);
|
||||||
return request;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package com.example.simpleblog.domain;
|
package com.example.simpleblog.domain;
|
||||||
|
|
||||||
import lombok.AccessLevel;
|
import lombok.AccessLevel;
|
||||||
|
import lombok.Builder;
|
||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
import lombok.NoArgsConstructor;
|
import lombok.NoArgsConstructor;
|
||||||
|
|
||||||
@@ -20,6 +21,7 @@ public class Post {
|
|||||||
@Lob
|
@Lob
|
||||||
private String content;
|
private String content;
|
||||||
|
|
||||||
|
@Builder
|
||||||
public Post(String title, String content) {
|
public Post(String title, String content) {
|
||||||
this.title = title;
|
this.title = title;
|
||||||
this.content = content;
|
this.content = content;
|
||||||
|
|||||||
@@ -1,17 +1,28 @@
|
|||||||
package com.example.simpleblog.request;
|
package com.example.simpleblog.request;
|
||||||
|
|
||||||
import lombok.AllArgsConstructor;
|
import lombok.Builder;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
|
|
||||||
import javax.validation.constraints.NotBlank;
|
import javax.validation.constraints.NotBlank;
|
||||||
|
|
||||||
@Data
|
@Data
|
||||||
@AllArgsConstructor
|
|
||||||
public class PostCreate {
|
public class PostCreate {
|
||||||
|
|
||||||
@NotBlank(message = "제목을 입력해주세요.")
|
@NotBlank(message = "제목을 입력해주세요.")
|
||||||
private String title;
|
private final String title;
|
||||||
|
|
||||||
@NotBlank(message = "내용을 입력해주세요.")
|
@NotBlank(message = "내용을 입력해주세요.")
|
||||||
private String content;
|
private final String content;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Builder
|
||||||
|
* - 가독성
|
||||||
|
* - 필요한 값만 받아서 생성 가능
|
||||||
|
* - 객체의 불변성
|
||||||
|
*/
|
||||||
|
@Builder
|
||||||
|
public PostCreate(String title, String content) {
|
||||||
|
this.title = title;
|
||||||
|
this.content = content;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
package com.example.simpleblog.response;
|
package com.example.simpleblog.response;
|
||||||
|
|
||||||
|
import lombok.Builder;
|
||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
import lombok.RequiredArgsConstructor;
|
|
||||||
|
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
@@ -16,7 +16,6 @@ import java.util.Map;
|
|||||||
* }
|
* }
|
||||||
*/
|
*/
|
||||||
@Getter
|
@Getter
|
||||||
@RequiredArgsConstructor
|
|
||||||
public class ErrorResponse {
|
public class ErrorResponse {
|
||||||
|
|
||||||
private final String code;
|
private final String code;
|
||||||
@@ -24,6 +23,12 @@ public class ErrorResponse {
|
|||||||
|
|
||||||
private final Map<String, String> validation = new HashMap<>();
|
private final Map<String, String> validation = new HashMap<>();
|
||||||
|
|
||||||
|
@Builder
|
||||||
|
public ErrorResponse(String code, String message) {
|
||||||
|
this.code = code;
|
||||||
|
this.message = message;
|
||||||
|
}
|
||||||
|
|
||||||
public void addValidation(String field, String errorMessage) {
|
public void addValidation(String field, String errorMessage) {
|
||||||
this.validation.put(field, errorMessage);
|
this.validation.put(field, errorMessage);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -16,7 +16,11 @@ public class PostService {
|
|||||||
|
|
||||||
public void write(PostCreate postCreate) {
|
public void write(PostCreate postCreate) {
|
||||||
|
|
||||||
Post post = new Post(postCreate.getTitle(), postCreate.getContent());
|
Post post = Post.builder()
|
||||||
|
.title(postCreate.getTitle())
|
||||||
|
.content(postCreate.getContent())
|
||||||
|
.build();
|
||||||
|
|
||||||
postRepository.save(post);
|
postRepository.save(post);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,10 +10,10 @@ import org.junit.jupiter.api.Test;
|
|||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
|
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
|
||||||
import org.springframework.boot.test.context.SpringBootTest;
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
import org.springframework.http.MediaType;
|
|
||||||
import org.springframework.test.web.servlet.MockMvc;
|
import org.springframework.test.web.servlet.MockMvc;
|
||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
import static org.springframework.http.MediaType.APPLICATION_JSON;
|
||||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
|
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
|
||||||
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
|
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
|
||||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
|
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
|
||||||
@@ -39,28 +39,37 @@ class PostControllerTest {
|
|||||||
@Test
|
@Test
|
||||||
@DisplayName("/posts 요청시 저장된 값을 출력한다.")
|
@DisplayName("/posts 요청시 저장된 값을 출력한다.")
|
||||||
void posts() throws Exception {
|
void posts() throws Exception {
|
||||||
PostCreate request = new PostCreate("글 제목입니다.", "글 내용입니다.");
|
// given
|
||||||
|
PostCreate request = PostCreate.builder()
|
||||||
|
.title("글 제목입니다.")
|
||||||
|
.content("글 내용입니다.")
|
||||||
|
.build();
|
||||||
|
|
||||||
String jsonString = objectMapper.writeValueAsString(request);
|
String jsonString = objectMapper.writeValueAsString(request);
|
||||||
|
|
||||||
// expected
|
// expected
|
||||||
mockMvc.perform(post("/posts")
|
mockMvc.perform(post("/posts")
|
||||||
.contentType(MediaType.APPLICATION_JSON)
|
.contentType(APPLICATION_JSON)
|
||||||
.content(jsonString)
|
.content(jsonString)
|
||||||
)
|
)
|
||||||
.andExpect(status().isOk())
|
.andExpect(status().isOk())
|
||||||
.andExpect(content().json(jsonString))
|
.andExpect(content().string(""))
|
||||||
.andDo(print());
|
.andDo(print());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@DisplayName("/posts 요청시 title값은 필수다.")
|
@DisplayName("/posts 요청시 title값은 필수다.")
|
||||||
void posts_validation() throws Exception {
|
void posts_validation() throws Exception {
|
||||||
PostCreate request = new PostCreate("", "글 내용입니다.");
|
// given
|
||||||
|
PostCreate request = PostCreate.builder()
|
||||||
|
.content("글 내용입니다.")
|
||||||
|
.build();
|
||||||
|
|
||||||
String jsonString = objectMapper.writeValueAsString(request);
|
String jsonString = objectMapper.writeValueAsString(request);
|
||||||
|
|
||||||
// expected
|
// expected
|
||||||
mockMvc.perform(post("/posts")
|
mockMvc.perform(post("/posts")
|
||||||
.contentType(MediaType.APPLICATION_JSON)
|
.contentType(APPLICATION_JSON)
|
||||||
.content(jsonString)
|
.content(jsonString)
|
||||||
)
|
)
|
||||||
.andExpect(status().isBadRequest())
|
.andExpect(status().isBadRequest())
|
||||||
@@ -74,12 +83,17 @@ class PostControllerTest {
|
|||||||
@Test
|
@Test
|
||||||
@DisplayName("/posts 요청시 DB에 값이 저장된다.")
|
@DisplayName("/posts 요청시 DB에 값이 저장된다.")
|
||||||
void save_post() throws Exception {
|
void save_post() throws Exception {
|
||||||
PostCreate request = new PostCreate("글 제목입니다.", "글 내용입니다.");
|
// given
|
||||||
|
PostCreate request = PostCreate.builder()
|
||||||
|
.title("글 제목입니다.")
|
||||||
|
.content("글 내용입니다.")
|
||||||
|
.build();
|
||||||
|
|
||||||
String jsonString = objectMapper.writeValueAsString(request);
|
String jsonString = objectMapper.writeValueAsString(request);
|
||||||
|
|
||||||
// when
|
// when
|
||||||
mockMvc.perform(post("/posts")
|
mockMvc.perform(post("/posts")
|
||||||
.contentType(MediaType.APPLICATION_JSON)
|
.contentType(APPLICATION_JSON)
|
||||||
.content(jsonString)
|
.content(jsonString)
|
||||||
)
|
)
|
||||||
.andExpect(status().isOk())
|
.andExpect(status().isOk())
|
||||||
|
|||||||
Reference in New Issue
Block a user