- Parameter Validator란?
- 내부 로직에서 처리할 수 없는 입력값을 사전에 검증하고, 필요한 오류 및 메시지로 매핑해서 응답하는 것
- @NotEmpty
- 해당 값이 null이거나 empty string("")에 대해서 검증하는 어노테이션
- 속성
- message : 해당 validation을 통과하지 못할 경우 표시할 오류 메시지
- @NotBlank
- 해당 값이 null이거나 empty string("") 및 공백 문자열(" ")까지 검증하는 어노테이션
- @Valid
- 일반적으로 validator는 해당 인자에 대해서만 검증하므로, 검증 대상이 객체이면 recursive하게 검증할 수 있도록 표시해주는 어노테이션
@Data
@NoArgsConstructor
@AllArgsConstructor(staticName = "of")
public class PersonDto {
@NotBlank(message = "이름은 필수값입니다")
private String name;
private String hobby;
private String address;
private LocalDate birthday;
private String job;
private String phoneNumber;
}
@RequestMapping(value = "/api/person")
@RestController
@Slf4j
public class PersonController {
@Autowired
private PersonService personService;
@GetMapping("/{id}")
public Person getPerson(@PathVariable Long id) {
return personService.getPerson(id);
}
@PostMapping
@ResponseStatus(HttpStatus.CREATED)
public void postPerson(@RequestBody @Valid PersonDto personDto) {
personService.put(personDto);
}
@PutMapping("/{id}")
public void modifyPerson(@PathVariable Long id, @RequestBody PersonDto personDto) {
personService.modify(id, personDto);
}
@PatchMapping("/{id}")
public void modifyPerson(@PathVariable Long id, String name) {
personService.modify(id, name);
}
@DeleteMapping("/{id}")
public void deletePerson(@PathVariable Long id) {
personService.delete(id);
}
}
@Data
@AllArgsConstructor(access = AccessLevel.PRIVATE)
public class ErrorResponse {
private int code;
private String message;
public static ErrorResponse of(HttpStatus httpStatus, String message) {
return new ErrorResponse(httpStatus.value(), message);
}
public static ErrorResponse of(HttpStatus httpStatus, FieldError fieldError) {
if (fieldError == null) {
return new ErrorResponse(httpStatus.value(), "invalid params");
} else {
return new ErrorResponse(httpStatus.value(), fieldError.getDefaultMessage());
}
}
}
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {
@ExceptionHandler(RenameIsNotPermittedException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
public ErrorResponse handleRenameNoPermittedException(RenameIsNotPermittedException ex) {
return ErrorResponse.of(HttpStatus.BAD_REQUEST, ex.getMessage());
}
@ExceptionHandler(PersonNotFoundException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
public ErrorResponse handlePersonNotFoundException(PersonNotFoundException ex) {
return ErrorResponse.of(HttpStatus.BAD_REQUEST, ex.getMessage());
}
@ExceptionHandler(MethodArgumentNotValidException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
public ErrorResponse handleMethodArgumentNotValidException(MethodArgumentNotValidException ex) {
return ErrorResponse.of(HttpStatus.BAD_REQUEST, ex.getBindingResult().getFieldError().getDefaultMessage());
}
@ExceptionHandler(RuntimeException.class)
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
public ErrorResponse handleRuntimeException(RuntimeException ex) {
log.error("서버오류 : {}", ex.getMessage(), ex);
return ErrorResponse.of(HttpStatus.INTERNAL_SERVER_ERROR, "알 수 없는 서버 오류가 발생하였습니다");
}
}
@Slf4j
@SpringBootTest
@Transactional
class PersonControllerTest {
@Autowired
private PersonRepository personRepository;
@Autowired
private ObjectMapper objectMapper;
@Autowired
private WebApplicationContext wac;
private MockMvc mockMvc;
@BeforeEach
void beforeEach() {
mockMvc = MockMvcBuilders
.webAppContextSetup(wac)
.alwaysDo(print())
.build();
}
@Test
void getPerson() throws Exception {
mockMvc.perform(
MockMvcRequestBuilders.get("/api/person/1"))
.andExpect(status().isOk())
.andExpect(jsonPath("$.name").value("martin"))
.andExpect(jsonPath("$.hobby").isEmpty())
.andExpect(jsonPath("$.address").isEmpty())
.andExpect(jsonPath("$.birthday").value("1991-08-15"))
.andExpect(jsonPath("$.job").isEmpty())
.andExpect(jsonPath("$.phoneNumber").isEmpty())
.andExpect(jsonPath("$.deleted").value(false))
.andExpect(jsonPath("$.age").isNumber())
.andExpect(jsonPath("$.birthdayToday").isBoolean());
}
@Test
void postPerson() throws Exception {
PersonDto dto = PersonDto.of("martin", "programming", "판교", LocalDate.now(), "programmer", "010-1111-2222");
mockMvc.perform(
MockMvcRequestBuilders.post("/api/person")
.contentType(MediaType.APPLICATION_JSON_UTF8)
.content(toJsonString(dto)))
.andExpect(status().isCreated());
Person result = personRepository.findAll(Sort.by(Direction.DESC, "id")).get(0);
assertAll(
() -> assertThat(result.getName()).isEqualTo("martin"),
() -> assertThat(result.getHobby()).isEqualTo("programming"),
() -> assertThat(result.getAddress()).isEqualTo("판교"),
() -> assertThat(result.getBirthday()).isEqualTo(Birthday.of(LocalDate.now())),
() -> assertThat(result.getJob()).isEqualTo("programmer"),
() -> assertThat(result.getPhoneNumber()).isEqualTo("010-1111-2222")
);
}
@Test
void postPersonIfNameIsNull() throws Exception {
PersonDto dto = new PersonDto();
mockMvc.perform(
MockMvcRequestBuilders.post("/api/person")
.contentType(MediaType.APPLICATION_JSON_UTF8)
.content(toJsonString(dto)))
.andExpect(status().isBadRequest())
.andExpect(jsonPath("$.code").value(400))
.andExpect(jsonPath("$.message").value("이름은 필수값입니다"));
}
@Test
void postPersonIfNameIsEmptyString() throws Exception {
PersonDto dto = new PersonDto();
dto.setName("");
mockMvc.perform(
MockMvcRequestBuilders.post("/api/person")
.contentType(MediaType.APPLICATION_JSON_UTF8)
.content(toJsonString(dto)))
.andExpect(status().isBadRequest())
.andExpect(jsonPath("$.code").value(400))
.andExpect(jsonPath("$.message").value("이름은 필수값입니다"));
}
@Test
void postPersonIfNameIsBlankString() throws Exception {
PersonDto dto = new PersonDto();
dto.setName(" ");
mockMvc.perform(
MockMvcRequestBuilders.post("/api/person")
.contentType(MediaType.APPLICATION_JSON_UTF8)
.content(toJsonString(dto)))
.andExpect(status().isBadRequest())
.andExpect(jsonPath("$.code").value(400))
.andExpect(jsonPath("$.message").value("이름은 필수값입니다"));
}
@Test
void modifyPerson() throws Exception {
PersonDto dto = PersonDto.of("martin", "programming", "판교", LocalDate.now(), "programmer", "010-1111-2222");
mockMvc.perform(
MockMvcRequestBuilders.put("/api/person/1")
.contentType(MediaType.APPLICATION_JSON_UTF8)
.content(toJsonString(dto)))
.andExpect(status().isOk());
Person result = personRepository.findById(1L).get();
assertAll(
() -> assertThat(result.getName()).isEqualTo("martin"),
() -> assertThat(result.getHobby()).isEqualTo("programming"),
() -> assertThat(result.getAddress()).isEqualTo("판교"),
() -> assertThat(result.getBirthday()).isEqualTo(Birthday.of(LocalDate.now())),
() -> assertThat(result.getJob()).isEqualTo("programmer"),
() ->assertThat(result.getPhoneNumber()).isEqualTo("010-1111-2222")
);
}
@Test
void modifyPersonIfNameIsDifferent() throws Exception {
PersonDto dto = PersonDto.of("james", "programming", "판교", LocalDate.now(), "programmer", "010-1111-2222");
mockMvc.perform(
MockMvcRequestBuilders.put("/api/person/1")
.contentType(MediaType.APPLICATION_JSON_UTF8)
.content(toJsonString(dto)))
.andExpect(status().isBadRequest())
.andExpect(jsonPath("$.code").value(400))
.andExpect(jsonPath("$.message").value("이름 변경이 허용되지 않습니다"));
}
@Test
void modifyPersonIfPersonNotFound() throws Exception {
PersonDto dto = PersonDto.of("martin", "programming", "판교", LocalDate.now(), "programmer", "010-1111-2222");
mockMvc.perform(
MockMvcRequestBuilders.put("/api/person/10")
.contentType(MediaType.APPLICATION_JSON_UTF8)
.content(toJsonString(dto)))
.andExpect(status().isBadRequest())
.andExpect(jsonPath("$.code").value(400))
.andExpect(jsonPath("$.message").value("Person Entity가 존재하지 않습니다"));
}
@Test
void modifyName() throws Exception {
mockMvc.perform(
MockMvcRequestBuilders.patch("/api/person/1")
.param("name", "martinModified"))
.andExpect(status().isOk());
assertThat(personRepository.findById(1L).get().getName()).isEqualTo("martinModified");
}
@Test
void deletePerson() throws Exception {
mockMvc.perform(
MockMvcRequestBuilders.delete("/api/person/1"))
.andExpect(status().isOk());
assertTrue(personRepository.findPeopleDeleted().stream().anyMatch(person -> person.getId().equals(1L)));
}
private String toJsonString(PersonDto personDto) throws JsonProcessingException {
return objectMapper.writeValueAsString(personDto);
}
}