@Test public void shouldCreateAccount() throws Exception { String description = "Cash"; boolean onBudget = true; long userId = 42L; AccountResource accountResource = new AccountResource(description, onBudget); when(accountServiceMock.create(anyLong(), anyString(), anyBoolean())).thenAnswer(invocation -> new Account(invocation.getArgument(1), invocation.getArgument(2), new User().setId(invocation.getArgument(0))).setId(1L)); final MockHttpServletRequestBuilder requestBuilder = post("/users/{userId}/accounts", userId).accept(MediaType.APPLICATION_JSON) .content(JsonHelper.toJson(accountResource)).contentType(MediaType.APPLICATION_JSON); final MvcResult mvcResult = mockMvc.perform(requestBuilder).andReturn(); final MockHttpServletResponse response = mvcResult.getResponse(); assertThat(response.getStatus()).isEqualTo(HttpStatus.OK.value()); AccountResource result = JsonHelper.fromJson(response.getContentAsString(), AccountResource.class); assertThat(result.getDescription()).isEqualTo(description); assertThat(result.getOnBudget()).isEqualTo(onBudget); verify(accountServiceMock).create(userId, description, onBudget); }
@PostMapping(consumes = MediaType.APPLICATION_JSON_VALUE) public ResponseEntity<AccountResource> create(@PathVariable("userId") long userId, @RequestBody @Valid AccountResource accountResource) { Account account = accountService.create(userId, accountResource.getDescription(), accountResource.getOnBudget()); final AccountResource createdAccountResource = accountResourceAssembler.toResource(account); return new ResponseEntity<>(createdAccountResource, HttpStatus.OK); }
AccountController { @PostMapping(consumes = MediaType.APPLICATION_JSON_VALUE) public ResponseEntity<AccountResource> create(@PathVariable("userId") long userId, @RequestBody @Valid AccountResource accountResource) { Account account = accountService.create(userId, accountResource.getDescription(), accountResource.getOnBudget()); final AccountResource createdAccountResource = accountResourceAssembler.toResource(account); return new ResponseEntity<>(createdAccountResource, HttpStatus.OK); } }
AccountController { @PostMapping(consumes = MediaType.APPLICATION_JSON_VALUE) public ResponseEntity<AccountResource> create(@PathVariable("userId") long userId, @RequestBody @Valid AccountResource accountResource) { Account account = accountService.create(userId, accountResource.getDescription(), accountResource.getOnBudget()); final AccountResource createdAccountResource = accountResourceAssembler.toResource(account); return new ResponseEntity<>(createdAccountResource, HttpStatus.OK); } @Autowired  AccountController(AccountService accountService, AccountResourceAssembler accountResourceAssembler); }
AccountController { @PostMapping(consumes = MediaType.APPLICATION_JSON_VALUE) public ResponseEntity<AccountResource> create(@PathVariable("userId") long userId, @RequestBody @Valid AccountResource accountResource) { Account account = accountService.create(userId, accountResource.getDescription(), accountResource.getOnBudget()); final AccountResource createdAccountResource = accountResourceAssembler.toResource(account); return new ResponseEntity<>(createdAccountResource, HttpStatus.OK); } @Autowired  AccountController(AccountService accountService, AccountResourceAssembler accountResourceAssembler); @GetMapping Resources<AccountResource> list(@PathVariable("userId") long userId); @GetMapping("/{accountId}") Resource<AccountResource> get(@PathVariable("userId") long userId, @PathVariable("accountId") long accountId); @PostMapping(consumes = MediaType.APPLICATION_JSON_VALUE) ResponseEntity<AccountResource> create(@PathVariable("userId") long userId, @RequestBody @Valid AccountResource accountResource); }
AccountController { @PostMapping(consumes = MediaType.APPLICATION_JSON_VALUE) public ResponseEntity<AccountResource> create(@PathVariable("userId") long userId, @RequestBody @Valid AccountResource accountResource) { Account account = accountService.create(userId, accountResource.getDescription(), accountResource.getOnBudget()); final AccountResource createdAccountResource = accountResourceAssembler.toResource(account); return new ResponseEntity<>(createdAccountResource, HttpStatus.OK); } @Autowired  AccountController(AccountService accountService, AccountResourceAssembler accountResourceAssembler); @GetMapping Resources<AccountResource> list(@PathVariable("userId") long userId); @GetMapping("/{accountId}") Resource<AccountResource> get(@PathVariable("userId") long userId, @PathVariable("accountId") long accountId); @PostMapping(consumes = MediaType.APPLICATION_JSON_VALUE) ResponseEntity<AccountResource> create(@PathVariable("userId") long userId, @RequestBody @Valid AccountResource accountResource);  }