From 5659f8fd24d920bd907a6fa4cb669d1703cea64b Mon Sep 17 00:00:00 2001 From: Sacha LEPAGE <lepages@acajou25.iut-infobio.priv.univ-lille1.fr> Date: Thu, 11 Feb 2021 16:32:51 +0100 Subject: [PATCH] tp fini --- .../java/fr/ulille/iut/todo/dao/TacheDAO.java | 11 ++- .../iut/todo/ressource/TodoRessource.java | 74 ++++++++++++++++++- .../ulille/iut/todo/service/TodoService.java | 6 +- .../iut/todo/ressource/TodoRessourceTest.java | 62 ++++++++++++---- 4 files changed, 135 insertions(+), 18 deletions(-) diff --git a/src/main/java/fr/ulille/iut/todo/dao/TacheDAO.java b/src/main/java/fr/ulille/iut/todo/dao/TacheDAO.java index d2c6b42..de6fc18 100644 --- a/src/main/java/fr/ulille/iut/todo/dao/TacheDAO.java +++ b/src/main/java/fr/ulille/iut/todo/dao/TacheDAO.java @@ -24,5 +24,14 @@ public interface TacheDAO { @SqlQuery("select * from taches") @RegisterBeanMapper(Tache.class) List<Tache> getAll(); + + @SqlQuery("select * from taches where id=?") + @RegisterBeanMapper(Tache.class) + Tache getById(String id); + + @SqlUpdate("delete from taches where id=?") + int delete(String id); + + @SqlUpdate("update taches set nom=:nom, description=:description where id=:id") + int update(@BindBean Tache task); } - diff --git a/src/main/java/fr/ulille/iut/todo/ressource/TodoRessource.java b/src/main/java/fr/ulille/iut/todo/ressource/TodoRessource.java index d05086a..6b5911e 100644 --- a/src/main/java/fr/ulille/iut/todo/ressource/TodoRessource.java +++ b/src/main/java/fr/ulille/iut/todo/ressource/TodoRessource.java @@ -1,6 +1,7 @@ package fr.ulille.iut.todo.ressource; import java.net.URI; +import java.util.HashMap; import java.util.List; import java.util.UUID; import java.util.logging.Logger; @@ -10,6 +11,7 @@ import fr.ulille.iut.todo.service.Tache; import fr.ulille.iut.todo.service.TodoService; import jakarta.ws.rs.Consumes; import jakarta.ws.rs.DELETE; +import jakarta.ws.rs.FormParam; import jakarta.ws.rs.GET; import jakarta.ws.rs.POST; import jakarta.ws.rs.PUT; @@ -17,9 +19,11 @@ import jakarta.ws.rs.Path; import jakarta.ws.rs.PathParam; import jakarta.ws.rs.Produces; import jakarta.ws.rs.WebApplicationException; +import jakarta.ws.rs.client.Entity; import jakarta.ws.rs.core.Context; import jakarta.ws.rs.core.EntityTag; import jakarta.ws.rs.core.MediaType; +import jakarta.ws.rs.core.MultivaluedHashMap; import jakarta.ws.rs.core.MultivaluedMap; import jakarta.ws.rs.core.Request; import jakarta.ws.rs.core.Response; @@ -64,4 +68,72 @@ public class TodoRessource { } return builder.build(); } -} + + @GET + @Path("{id}") + @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML }) + public Tache getById(@PathParam("id")UUID id) { + Tache test=todoService.getTache(id); + if(test == null) { + throw new WebApplicationException(404); + } + return test; + } + + @GET + @Path("{id}/description") + @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML }) + public String getDescription(@PathParam("id")UUID id) { + String test=todoService.getTache(id).getDescription(); + if(test == null) { + throw new WebApplicationException(404); + } + return test; + } + + @POST + @Consumes(MediaType.APPLICATION_FORM_URLENCODED) + public Response createTacheFromForm(@FormParam("nom") String nom, @FormParam("description") String description) { + LOGGER.info("createTacheFromForm()"); + + Tache task = new Tache(); + task.setNom(nom); + task.setDescription(description); + todoService.addTache(task); + URI location = uri.getAbsolutePathBuilder().path(task.getId().toString()).build(); + + EntityTag etag = new EntityTag(Integer.toString(task.hashCode())); + ResponseBuilder build = request.evaluatePreconditions(etag); + + if (build == null) { + build = Response.created(location); + build.tag(etag); + build.entity(task); + } + return build.build(); + } + + @DELETE + @Path("{id}") + public Response deleteTask(@PathParam("id")UUID id) { + int test=todoService.deleteTache(id.toString()); + if(test==0) { + throw new WebApplicationException(404); + } + return Response.status(204).build(); + } + + @PUT + @Path("{id}") + public Response putTache(CreationTacheDTO tacheDto) { + LOGGER.info("putTache()"); + if(tacheDto.getDescription()==null || tacheDto.getNom()==null) { + throw new WebApplicationException(400); + } + Tache task = Tache.fromCreationTacheDTO(tacheDto); + todoService.updateTache(task); + return Response.accepted(Entity.json(task)).build(); + } + + +} \ No newline at end of file diff --git a/src/main/java/fr/ulille/iut/todo/service/TodoService.java b/src/main/java/fr/ulille/iut/todo/service/TodoService.java index 654e7a2..8dc0665 100644 --- a/src/main/java/fr/ulille/iut/todo/service/TodoService.java +++ b/src/main/java/fr/ulille/iut/todo/service/TodoService.java @@ -15,7 +15,7 @@ public class TodoService { } public Tache getTache(UUID id) { - return null; + return taches.getById(id.toString()); } public List<Tache> getAll() { @@ -27,10 +27,10 @@ public class TodoService { } public int deleteTache(String id) { - return 0 ; + return taches.delete(id); } public void updateTache(Tache tache) { - return; + taches.update(tache); } } diff --git a/src/test/java/fr/ulille/iut/todo/ressource/TodoRessourceTest.java b/src/test/java/fr/ulille/iut/todo/ressource/TodoRessourceTest.java index 847c5ed..ec82cfc 100644 --- a/src/test/java/fr/ulille/iut/todo/ressource/TodoRessourceTest.java +++ b/src/test/java/fr/ulille/iut/todo/ressource/TodoRessourceTest.java @@ -1,23 +1,18 @@ package fr.ulille.iut.todo.ressource; import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; import java.util.ArrayList; import java.util.List; -import java.util.logging.Level; -import java.util.logging.Logger; +import java.util.UUID; -import org.glassfish.jersey.logging.LoggingFeature; import org.glassfish.jersey.server.ResourceConfig; import org.glassfish.jersey.test.JerseyTest; -import org.glassfish.jersey.test.TestProperties; import org.junit.After; import org.junit.Before; import org.junit.Test; import fr.ulille.iut.todo.BDDFactory; -import fr.ulille.iut.todo.DebugMapper; import fr.ulille.iut.todo.dao.TacheDAO; import fr.ulille.iut.todo.dto.CreationTacheDTO; import fr.ulille.iut.todo.service.Tache; @@ -84,45 +79,86 @@ public class TodoRessourceTest extends JerseyTest { @Test public void get_with_id_should_return_task() { - Tache tache = new Tache(); - tache.setNom("test"); - tache.setDescription("description"); - dao.insert(tache); + Tache task = new Tache(); + task.setNom("test"); + task.setDescription("description"); + dao.insert(task); - Tache returned = target("taches").path(tache.getId().toString()).request().get(Tache.class); + Tache taskRet = target("taches").path(task.getId().toString()).request().get(Tache.class); - assertEquals(tache, returned); + assertEquals(task, taskRet); } @Test public void get_with_wrong_id_should_return_404() { + Response rep = target("taches").path(UUID.randomUUID().toString()).request().get(); + assertEquals(rep.getStatus(), 404); } @Test public void get_for_description_should_work_with_existing_task() { + Tache task = new Tache(); + task.setNom("test"); + task.setDescription("description"); + dao.insert(task); + + String returned = target("taches").path(task.getId().toString()).path("description").request().get(String.class); + assertEquals(task.getDescription(),returned); } @Test public void get_for_description_with_wrong_id_should_return_404() { + Response rep = target("taches").path(UUID.randomUUID().toString()).path("descripion").request().get(); + assertEquals(rep.getStatus(), 404); } @Test public void delete_should_remove_task_and_return_204() { + Tache task = new Tache(); + task.setNom("test"); + task.setDescription("description"); + dao.insert(task); + Response response = target("taches").path(task.getId().toString()).request().delete(); + assertEquals(response.getStatus(), 204); } @Test public void delete_with_wrong_id_should_return_404() { + Response rep = target("taches").path(UUID.randomUUID().toString()).request().delete(); + assertEquals(rep.getStatus(), 404); } @Test public void put_should_replace_existing_task_values_return_200_and_task() { + Tache task = new Tache(); + task.setNom("test"); + task.setDescription("description"); + dao.insert(task); + Tache tache = new Tache(); + tache.setNom("test"); + tache.setDescription("description"); + Response response = target("taches").path(tache.getId().toString()).request().put(Entity.json(tache)); + assertEquals(response.getStatus(), 202); } @Test public void partial_put_should_fail_with_400() { + Tache task = new Tache(); + task.setNom("test"); + task.setDescription("description"); + dao.insert(task); + Tache tache = new Tache(); + tache.setNom("test"); + Response response = target("taches").path(tache.getId().toString()).request().put(Entity.json(tache)); + assertEquals(response.getStatus(), 400); } @Test public void post_with_form_data_should_return_201_location_and_task() { + Form form=new Form(); + form.param("nom", "tache 1"); + form.param("description", "value"); + Response response= target("taches").request().post(Entity.form(form)); + assertEquals(response.getStatus(),201); } -} +} \ No newline at end of file -- GitLab