@Test public void shouldReturnCreatedOnSuccess() throws Exception { when(userService.getUserByEmail(any())).thenReturn(Optional.empty()); when(userService.createUser(any())).then(invocation -> { final UserForm userForm = invocation.getArgument(0); final User user = new User(); user.setId(RandomUtils.nextLong()); user.setToken(userForm.getEmail()); user.setFirstName(userForm.getFirstName()); user.setLastName(userForm.getLastName()); user.setEmail(userForm.getEmail()); user.setBirthDate(userForm.getBirthDate()); user.setSsn(userForm.getSsn()); return user; }); final Card card = new Card(); card.setId(RandomUtils.nextLong()); card.setToken(UUID.randomUUID().toString()); card.setCardProductToken(UUID.randomUUID().toString()); card.setPan("1111222233334444"); card.setCvvNumber("123"); card.setExpirationTime(LocalDateTime.of(2025, Month.JANUARY, 1, 12, 30)); when(cardService.createCard(any())).then(invocation -> { final String userToken = invocation.getArgument(0); card.setUserToken(userToken); return card; }); mockMvc .perform(createCard(new ClassPathResource("CardControllerTest/UserForm.json"))) .andDo(print()) .andExpect(status().isCreated()) .andExpect(jsonPath("$.token").value(equalTo(card.getToken()))) .andExpect(jsonPath("$.card_product_token").value(equalTo(card.getCardProductToken()))) .andExpect(jsonPath("$.user_token").value(USER_EMAIL)) .andExpect(jsonPath("$.pan").value(equalTo(card.getPan()))) .andExpect(jsonPath("$.cvv_number").value(equalTo(card.getCvvNumber()))) .andExpect(jsonPath("$.expiration_time").value(equalTo("2025-01-01T12:30:00"))); verify(userService).getUserByEmail(USER_EMAIL); verify(cardService).createCard(USER_EMAIL); }
@PostMapping("/cards") @ResponseStatus(HttpStatus.CREATED) public CreateCardResponse createCard(@RequestBody UserForm userForm) { final User user = userService .getUserByEmail(userForm.getEmail()) .orElseGet(() -> userService.createUser(userForm)); final Card card = cardService.createCard(user.getToken()); return CreateCardResponse.fromCardEntity(card); }
CardController { @PostMapping("/cards") @ResponseStatus(HttpStatus.CREATED) public CreateCardResponse createCard(@RequestBody UserForm userForm) { final User user = userService .getUserByEmail(userForm.getEmail()) .orElseGet(() -> userService.createUser(userForm)); final Card card = cardService.createCard(user.getToken()); return CreateCardResponse.fromCardEntity(card); } }
CardController { @PostMapping("/cards") @ResponseStatus(HttpStatus.CREATED) public CreateCardResponse createCard(@RequestBody UserForm userForm) { final User user = userService .getUserByEmail(userForm.getEmail()) .orElseGet(() -> userService.createUser(userForm)); final Card card = cardService.createCard(user.getToken()); return CreateCardResponse.fromCardEntity(card); } CardController(CardService cardService, UserService userService); }
CardController { @PostMapping("/cards") @ResponseStatus(HttpStatus.CREATED) public CreateCardResponse createCard(@RequestBody UserForm userForm) { final User user = userService .getUserByEmail(userForm.getEmail()) .orElseGet(() -> userService.createUser(userForm)); final Card card = cardService.createCard(user.getToken()); return CreateCardResponse.fromCardEntity(card); } CardController(CardService cardService, UserService userService); @PostMapping("/cards") @ResponseStatus(HttpStatus.CREATED) CreateCardResponse createCard(@RequestBody UserForm userForm); }
CardController { @PostMapping("/cards") @ResponseStatus(HttpStatus.CREATED) public CreateCardResponse createCard(@RequestBody UserForm userForm) { final User user = userService .getUserByEmail(userForm.getEmail()) .orElseGet(() -> userService.createUser(userForm)); final Card card = cardService.createCard(user.getToken()); return CreateCardResponse.fromCardEntity(card); } CardController(CardService cardService, UserService userService); @PostMapping("/cards") @ResponseStatus(HttpStatus.CREATED) CreateCardResponse createCard(@RequestBody UserForm userForm);  }