TodoResource { @GET @Path("{id}") public Todo get(@PathParam("id") String id) { Todo todo = dao.findById(id); if (todo == null) { throw new WebApplicationException(Response.Status.NOT_FOUND); } return todo; } TodoResource(TodoDao dao); @GET List<Todo> list(); @GET @Path("{id}") Todo get(@PathParam("id") String id); @POST @Consumes(MediaType.APPLICATION_FORM_URLENCODED) Response create(@FormParam("email") String email); @PUT @Path("{id}") Todo update(@PathParam("id") String id, @Valid Todo todo); @DELETE @Path("{id}") void delete(@PathParam("id") String id); @DELETE void deleteAll(); static final Item ITEM; static final Todo TODO; }
@Test public void testCreateNewTodoWithoutEmail() { ClientResponse response = client().resource("/todos").post(ClientResponse.class); assertThat(response.getStatus()).isEqualTo(Response.Status.CREATED.getStatusCode()); List<Todo> all = dao.findAll(); assertThat(all).hasSize(1); Todo todo = all.get(0); assertThat(response.getLocation().toASCIIString()).endsWith(todo.getId()); assertThat(todo.getEmail()).isNullOrEmpty(); }
@Test public void testCreateNewTodoWithEmail() { final String EMAIL = "john@example.com"; MultivaluedMap<String, String> arguments = new MultivaluedMapImpl(); arguments.add("email", EMAIL); ClientResponse response = client().resource("/todos") .type(MediaType.APPLICATION_FORM_URLENCODED) .post(ClientResponse.class, arguments); assertThat(response.getStatus()).isEqualTo(Response.Status.CREATED.getStatusCode()); Todo todo = dao.findAll().get(0); assertThat(todo.getEmail()).isEqualTo(EMAIL); }
@Test public void testRetrieveExistingTodoList() { final String id = UUID.randomUUID().toString(); final Todo todo = Todo.newBuilder().id(id).created(new Date()).build(); dao.insert(todo); Todo stored = client().resource("/todos/" + id).get(Todo.class); assertThat(stored).isEqualTo(todo); }