Guest User

Untitled

a guest
Nov 27th, 2016
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.63 KB | None | 0 0
  1. @ClassRule public static final ResourceTestRule
  2. userResource =
  3. ResourceTestRule.builder().addResource(new UserResource(USER_DAO, PERSON_DAO)).build();
  4.  
  5. when(USER_DAO.save(any(User.class))).thenReturn(Optional.of(user));
  6. when(PERSON_DAO.save(any(Person.class))).thenReturn(Optional.of(person));
  7.  
  8. final Response
  9. response =
  10. userResource.client().target("/user").request(MediaType.APPLICATION_JSON_TYPE)
  11. .post(Entity.entity(user, MediaType.APPLICATION_JSON_TYPE));
  12.  
  13. assertThat(response.getStatusInfo()).isEqualTo(Response.Status.OK);
  14. verify(USER_DAO).save(userCaptor.capture());
  15. verify(PERSON_DAO).save(personCaptor.capture());
  16. User savedUser = userCaptor.getValue();
  17. assertThat(savedUser).isEqualToIgnoringGivenFields(user, "salt", "password", "person");
  18. assertThat(savedUser.getPerson()).isEqualToComparingFieldByFieldRecursively(user.getPerson());
  19.  
  20. @POST
  21. @Timed
  22. public User create(@NotNull @Valid User user) {
  23. if (user.getPerson() != null) {
  24. try {
  25. personDAO.save(user.getPerson());
  26. // todo justin - abstract this
  27. } catch (DuplicateKeyException e) {
  28. throw new WebApplicationException("That person exists already.", Response.SC_BAD_REQUEST);
  29. }
  30. }
  31.  
  32. try {
  33. user = userDAO.save(user).get();
  34. } catch (DuplicateKeyException e) {
  35. throw new WebApplicationException("That user exists already.", Response.SC_BAD_REQUEST);
  36. }
  37. return user;
  38. }
  39.  
  40. user = new User();
  41. user.setEmail("justin@email.com");
  42. user.setPassword("test");
  43.  
  44. person = new Person();
  45. person.setFirstName("Justin");
  46. person.setLastName("K");
  47.  
  48. user.setPerson(person);
  49.  
  50. public class User extends BaseModel implements Principal {
  51. @Id
  52. @JsonSerialize(using = ToStringSerializer.class)
  53. private ObjectId id = new ObjectId();
  54.  
  55. @Email
  56. @NotBlank
  57. private String email;
  58.  
  59. @NotEmpty
  60. private byte[] password;
  61.  
  62. private byte[] salt = Security.getSalt();
  63.  
  64. public ObjectId getId() {
  65. return id;
  66. }
  67.  
  68. public void setId(ObjectId id) {
  69. this.id = id;
  70. }
  71.  
  72. public String getEmail() {
  73. return email;
  74. }
  75.  
  76. public void setEmail(String email) {
  77. this.email = email;
  78. }
  79.  
  80. @JsonIgnore // ignored when serialized FROM object TO json
  81. public byte[] getPassword() {
  82. return password;
  83. }
  84.  
  85. @JsonProperty
  86. public void setPassword(byte[] password) {
  87. this.password = password;
  88. }
  89.  
  90. @JsonProperty
  91. public void setPassword(String password) {
  92. this.password = Security.hashPassword(password.toCharArray(), this.getSalt());
  93. }
  94.  
  95. @JsonIgnore // ignored when serialized FROM object TO json
  96. public byte[] getSalt() {
  97. return salt;
  98. }
  99.  
  100. @JsonProperty
  101. public void setSalt(byte[] salt) {
  102. this.salt = salt;
  103. }
Add Comment
Please, Sign In to add comment