BookmarkController { @GetMapping("{id}") public Resource<Bookmark> getBookmark(@PathVariable UUID id) { return assembler.toResource(bookmarkService.find(id)); }  @GetMapping("template") Resource<Bookmark> getBookmarkTemplate(); @GetMapping("{id}") Resource<Bookmark> getBookmark(@PathVariable UUID id); @PutMapping("{id}") @PreAuthorize("hasRole('ADMIN')") Resource<Bookmark> updateBookmark(@PathVariable UUID id, @RequestBody @Valid Bookmark bookmark); @DeleteMapping("{id}") @PreAuthorize("hasRole('ADMIN')") ResponseEntity<Void> deleteBookmark(@PathVariable UUID id);  }
@Test public void getABookmark() throws Exception { Bookmark input = getSimpleBookmark(); String location = addBookmark(input); Resource<Bookmark> output = getBookmark(location); assertNotNull(output.getContent().getUrl()); assertEquals(input.getUrl(), output.getContent().getUrl()); }
@Test public void updateABookmark() throws Exception { Bookmark input = getSimpleBookmark(); String location = addBookmark(input); Resource<Bookmark> output = getBookmark(location); String result = mvc.perform( put(output.getId().getHref()) .contentType(MediaType.APPLICATION_JSON_UTF8) .accept("application/hal+json;charset=UTF-8", "application/json;charset=UTF-8") .content(objectMapper.writeValueAsString(output.getContent().withUrl("http: .with(csrf()) ).andDo(print()).andExpect(status().isOk()) .andReturn().getResponse().getContentAsString(); output = objectMapper.readValue(result, new TypeReference<Resource<Bookmark>>() { }); assertEquals("http: }
@Test public void updateABookmarkFailsBecauseDescriptionIsNull() throws Exception { Bookmark input = getSimpleBookmark(); String location = addBookmark(input); Resource<Bookmark> output = getBookmark(location); output.getContent().setDescription(null); mvc.perform( put(output.getId().getHref()) .contentType(MediaType.APPLICATION_JSON_UTF8) .accept("application/hal+json;charset=UTF-8", "application/json;charset=UTF-8") .content(objectMapper.writeValueAsString(output.getContent())) .with(csrf()) ).andDo(print()) .andExpect(status().isBadRequest()) .andExpect(jsonPath("$[0].field").value("description")); }
@Test public void updateABookmarkFailsBecauseUrlIsNotValid() throws Exception { Bookmark input = getSimpleBookmark(); String location = addBookmark(input); Resource<Bookmark> output = getBookmark(location); output.getContent().setUrl("broken: mvc.perform( put(output.getId().getHref()) .contentType(MediaType.APPLICATION_JSON_UTF8) .accept("application/hal+json;charset=UTF-8", "application/json;charset=UTF-8") .content(objectMapper.writeValueAsString(output.getContent())) .with(csrf()) ).andDo(print()) .andExpect(status().isBadRequest()) .andExpect(jsonPath("$[0].field").value("url")); }
@Test public void updateABookmarkStaleFails() throws Exception { Bookmark input = getSimpleBookmark(); String location = addBookmark(input); Resource<Bookmark> output = getBookmark(location); mvc.perform( put(output.getId().getHref()) .contentType(MediaType.APPLICATION_JSON_UTF8) .accept("application/hal+json;charset=UTF-8", "application/json;charset=UTF-8") .content(objectMapper.writeValueAsString(output.getContent().withUrl("http: .with(csrf()) ).andDo(print()).andExpect(status().isOk()) .andReturn().getResponse().getContentAsString(); mvc.perform( put(output.getId().getHref()) .contentType(MediaType.APPLICATION_JSON_UTF8) .accept("application/hal+json;charset=UTF-8", "application/json;charset=UTF-8") .content(objectMapper.writeValueAsString(output.getContent().withUrl("http: .with(csrf()) ).andDo(print()).andExpect(status().isConflict()); }