-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #130 from fengyuanyang/issue-129
Issue 129
- Loading branch information
Showing
4 changed files
with
134 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
src/main/java/com/ordestiny/tdd/controller/UserController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package com.ordestiny.tdd.controller; | ||
|
||
import com.ordestiny.tdd.model.User; | ||
import lombok.AllArgsConstructor; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.validation.FieldError; | ||
import org.springframework.web.bind.MethodArgumentNotValidException; | ||
import org.springframework.web.bind.annotation.*; | ||
|
||
import javax.validation.Valid; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
@RestController | ||
@AllArgsConstructor | ||
public class UserController { | ||
|
||
@PostMapping("/user") | ||
ResponseEntity<User> addUser(@Valid @RequestBody User user) { | ||
return new ResponseEntity<>(user, HttpStatus.OK); | ||
} | ||
|
||
|
||
@ResponseStatus(HttpStatus.BAD_REQUEST) | ||
@ExceptionHandler(MethodArgumentNotValidException.class) | ||
public Map<String, String> handleValidationExceptions( | ||
MethodArgumentNotValidException ex) { | ||
Map<String, String> errors = new HashMap<>(); | ||
ex.getBindingResult().getAllErrors().forEach((error) -> { | ||
String fieldName = ((FieldError) error).getField(); | ||
String errorMessage = error.getDefaultMessage(); | ||
errors.put(fieldName, errorMessage); | ||
}); | ||
return errors; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package com.ordestiny.tdd.model; | ||
|
||
import lombok.Getter; | ||
import lombok.Setter; | ||
|
||
import javax.validation.constraints.NotBlank; | ||
import javax.validation.constraints.Pattern; | ||
|
||
@Getter | ||
@Setter | ||
public class User { | ||
@NotBlank(message = "name is mandatory") | ||
private String name; | ||
|
||
@Pattern(regexp = "M|F", flags = Pattern.Flag.CASE_INSENSITIVE) | ||
private String sex; | ||
|
||
@Pattern(regexp="^[A-Za-z0-9+_.-]+@(.+)$") | ||
private String email; | ||
|
||
@Pattern(regexp="(^$|[0-9]{10})") | ||
private String phone; | ||
} |
72 changes: 72 additions & 0 deletions
72
src/test/java/com/ordestiny/tdd/controller/UserControllerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package com.ordestiny.tdd.controller; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.ordestiny.tdd.model.User; | ||
import org.hamcrest.core.Is; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.mockito.InjectMocks; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.test.web.servlet.MockMvc; | ||
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; | ||
import org.springframework.test.web.servlet.result.MockMvcResultMatchers; | ||
import org.springframework.test.web.servlet.setup.MockMvcBuilders; | ||
|
||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; | ||
|
||
@ExtendWith(MockitoExtension.class) | ||
class UserControllerTest { | ||
|
||
@InjectMocks | ||
UserController controller; | ||
|
||
MockMvc mockMvc; | ||
|
||
@BeforeEach | ||
void setUp() { | ||
mockMvc = MockMvcBuilders.standaloneSetup(controller).build(); | ||
} | ||
|
||
@Test | ||
void addUser_illegalInput_expect4xx() throws Exception { | ||
User user = new User(); | ||
user.setEmail("testgmail.com"); | ||
user.setPhone("DDDD"); | ||
user.setSex("Boy"); | ||
|
||
ObjectMapper mapper = new ObjectMapper(); | ||
String userObject = mapper.writeValueAsString(user); | ||
|
||
mockMvc.perform(MockMvcRequestBuilders.post("/user").contentType(MediaType.APPLICATION_JSON).content(userObject)) | ||
.andExpect(status().isBadRequest()) | ||
.andExpect(MockMvcResultMatchers.jsonPath("$.name", Is.is("name is mandatory"))) | ||
.andExpect(MockMvcResultMatchers.jsonPath("$.phone", Is.is("must match \"(^$|[0-9]{10})\""))) | ||
.andExpect(MockMvcResultMatchers.jsonPath("$.email", Is.is("must match \"^[A-Za-z0-9+_.-]+@(.+)$\""))) | ||
.andExpect(MockMvcResultMatchers.jsonPath("$.sex", Is.is("must match \"M|F\""))) | ||
.andExpect(MockMvcResultMatchers.content() | ||
.contentType(MediaType.APPLICATION_JSON)); | ||
} | ||
|
||
@Test | ||
void addUser_legalInput_expect200AndSuccess() throws Exception { | ||
User user = new User(); | ||
user.setEmail("[email protected]"); | ||
user.setPhone("0911111111"); | ||
user.setSex("F"); | ||
user.setName("owen"); | ||
|
||
ObjectMapper mapper = new ObjectMapper(); | ||
String userObject = mapper.writeValueAsString(user); | ||
|
||
mockMvc.perform(MockMvcRequestBuilders.post("/user").contentType(MediaType.APPLICATION_JSON).content(userObject)) | ||
.andExpect(status().isOk()) | ||
.andExpect(MockMvcResultMatchers.jsonPath("$.name", Is.is(user.getName()))) | ||
.andExpect(MockMvcResultMatchers.jsonPath("$.phone", Is.is(user.getPhone()))) | ||
.andExpect(MockMvcResultMatchers.jsonPath("$.email", Is.is(user.getEmail()))) | ||
.andExpect(MockMvcResultMatchers.jsonPath("$.sex", Is.is(user.getSex()))) | ||
.andExpect(MockMvcResultMatchers.content() | ||
.contentType(MediaType.APPLICATION_JSON)); | ||
} | ||
} |