[M] Change http status to indicate username already exists, 406 -> 409.
This commit is contained in:
@@ -13,7 +13,6 @@ import org.springframework.web.bind.annotation.RequestHeader;
|
|||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
import javax.validation.constraints.NotNull;
|
|
||||||
import javax.validation.constraints.Pattern;
|
import javax.validation.constraints.Pattern;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
@@ -32,7 +31,11 @@ public class UserController {
|
|||||||
// https://www.baeldung.com/spring-rest-http-headers
|
// https://www.baeldung.com/spring-rest-http-headers
|
||||||
// TODO: This method should be synchronized to avoid race condition.
|
// TODO: This method should be synchronized to avoid race condition.
|
||||||
// Also, this method should not be private, or else cannot use userRepository.
|
// Also, this method should not be private, or else cannot use userRepository.
|
||||||
public synchronized ResponseEntity<String> register(
|
|
||||||
|
// TODO: 2021/1/22 Need a better design!
|
||||||
|
// Controller Return error code list as List<String>, or return uuid as String.
|
||||||
|
@SuppressWarnings("rawtypes")
|
||||||
|
public synchronized ResponseEntity register(
|
||||||
// username & password shouldn't be null, and should match thr regex.
|
// username & password shouldn't be null, and should match thr regex.
|
||||||
// [!] @RequestHeader(required = false) makes no need make another error handler.
|
// [!] @RequestHeader(required = false) makes no need make another error handler.
|
||||||
// [!] And also, ExceptionHandler of MissingRequestHeaderException cannot deal with all missing fields.
|
// [!] And also, ExceptionHandler of MissingRequestHeaderException cannot deal with all missing fields.
|
||||||
@@ -42,9 +45,10 @@ public class UserController {
|
|||||||
@RequestHeader String password
|
@RequestHeader String password
|
||||||
) {
|
) {
|
||||||
// First, spring will check args. If not pass there regex, raise ConstraintViolationException.
|
// First, spring will check args. If not pass there regex, raise ConstraintViolationException.
|
||||||
// Check if username not exists.
|
// Then, we check if username not exists. If username already exists, return ErrorCode with 409 conflict.
|
||||||
if (userRepository.existsByUsername(username))
|
if (userRepository.existsByUsername(username))
|
||||||
return ResponseEntity.status(HttpStatus.NOT_ACCEPTABLE).body(ErrorCode.USER_NAME_ALREADY_EXISTS);
|
// TODO: 2021/1/22 Using library instead of hand make.
|
||||||
|
return ResponseEntity.status(HttpStatus.CONFLICT).body(List.of(ErrorCode.USER_NAME_ALREADY_EXISTS));
|
||||||
|
|
||||||
User user = new User();
|
User user = new User();
|
||||||
user.setUsername(username);
|
user.setUsername(username);
|
||||||
|
|||||||
@@ -122,7 +122,7 @@ class UserRegisterDeleteNodeTest {
|
|||||||
// Notice: inserted user should be delete.
|
// Notice: inserted user should be delete.
|
||||||
val tempUuid = result.response.contentAsString
|
val tempUuid = result.response.contentAsString
|
||||||
mockMvc.perform(post(REGISTER_NODE).header(H_USERNAME, V_USERNAME).header(H_PASSWORD, V_PASSWORD))
|
mockMvc.perform(post(REGISTER_NODE).header(H_USERNAME, V_USERNAME).header(H_PASSWORD, V_PASSWORD))
|
||||||
.andExpect(status().isNotAcceptable)
|
.andExpect(status().isConflict)
|
||||||
.andExpect(content().json(String.format("[\"%s\"]", USER_NAME_ALREADY_EXISTS)))
|
.andExpect(content().json(String.format("[\"%s\"]", USER_NAME_ALREADY_EXISTS)))
|
||||||
userRepository.deleteById(tempUuid)
|
userRepository.deleteById(tempUuid)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user