From c26ce4dca25db1f0ba4c8dfa7244cb8adf735dfc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Delobel?= <jeremy.delobel.etu@univ-lille.fr> Date: Thu, 11 Feb 2021 11:58:07 +0100 Subject: [PATCH] =?UTF-8?q?Avancement=20du=20TP=20apr=C3=A8s=20le=20cours?= =?UTF-8?q?=20de=20PROG-REPARTIE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/fr/ulille/iut/todo/dao/TacheDAO.java | 4 + .../iut/todo/ressource/TodoRessource.java | 25 +++ .../ulille/iut/todo/service/TodoService.java | 3 +- .../iut/todo/ressource/TodoRessourceTest.java | 205 ++++++++++-------- 4 files changed, 140 insertions(+), 97 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..ff8a5a4 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,9 @@ 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); } 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..7c8c61e 100644 --- a/src/main/java/fr/ulille/iut/todo/ressource/TodoRessource.java +++ b/src/main/java/fr/ulille/iut/todo/ressource/TodoRessource.java @@ -24,6 +24,7 @@ import jakarta.ws.rs.core.MultivaluedMap; import jakarta.ws.rs.core.Request; import jakarta.ws.rs.core.Response; import jakarta.ws.rs.core.Response.ResponseBuilder; +import jakarta.ws.rs.core.Response.Status; import jakarta.ws.rs.core.UriInfo; @Path("taches") @@ -64,4 +65,28 @@ public class TodoRessource { } return builder.build(); } + + @GET + @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML }) + @Path("{id}") + public Tache getById(@PathParam("id") String id) { + LOGGER.info("getById()"); + Tache t1 = todoService.getTache(UUID.fromString(id)); + if(t1 == null) { + throw new WebApplicationException(Status.NOT_FOUND); + } + return todoService.getTache(UUID.fromString(id)); + } + + @GET + @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML }) + @Path("{id}/description") + public String getByDescription(@PathParam("id") String id) { + LOGGER.info("getById()"); + Tache t1 = todoService.getTache(UUID.fromString(id)); + if(t1 == null) { + throw new WebApplicationException(Status.NOT_FOUND); + } + return todoService.getTache(UUID.fromString(id)).getDescription(); + } } 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..fa94104 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() { @@ -33,4 +33,5 @@ public class TodoService { public void updateTache(Tache tache) { return; } + } 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..8e82dbb 100644 --- a/src/test/java/fr/ulille/iut/todo/ressource/TodoRessourceTest.java +++ b/src/test/java/fr/ulille/iut/todo/ressource/TodoRessourceTest.java @@ -5,6 +5,7 @@ import static org.junit.Assert.assertTrue; import java.util.ArrayList; import java.util.List; +import java.util.UUID; import java.util.logging.Level; import java.util.logging.Logger; @@ -29,100 +30,112 @@ import jakarta.ws.rs.core.MediaType; import jakarta.ws.rs.core.Response; public class TodoRessourceTest extends JerseyTest { - private TacheDAO dao; - - @Override - protected Application configure() { - BDDFactory.setJdbiForTests(); - ResourceConfig rc = new ResourceConfig(TodoRessource.class); - // Dé-commenter pour avoir la trace des requêtes et réponses - // rc.register(new - // LoggingFeature(Logger.getLogger(LoggingFeature.DEFAULT_LOGGER_NAME), - // Level.INFO, - // LoggingFeature.Verbosity.PAYLOAD_ANY, 10000)); - // Dé-commenter pour avoir une trace des erreurs internes serveur (les tests sur - // le code de retour vont échouer) - // rc.register(DebugMapper.class); - - return rc; - } - - @Before - public void setEnvUp() { - dao = BDDFactory.buildDao(TacheDAO.class); - dao.createTable(); - } - - @After - public void tearEnvDown() throws Exception { - dao.dropTable(); - } - - @Test - public void get_should_return_empty_list_at_startup() { - List<Tache> answer = target("taches").request(MediaType.APPLICATION_JSON).get(new GenericType<List<Tache>>() { - }); - assertEquals(new ArrayList<Tache>(), answer); - } - - @Test - public void post_should_return_201_location_and_task() { - CreationTacheDTO dto = new CreationTacheDTO(); - dto.setNom("test"); - dto.setDescription("description test"); - - Response response = target("taches").request().post(Entity.json(dto)); - assertEquals(Response.Status.CREATED.getStatusCode(), response.getStatus()); - - Tache tache = response.readEntity(Tache.class); - assertEquals(target("taches").getUri().toString() + "/" + tache.getId().toString(), - response.getHeaderString("Location")); - - assertEquals(dto.getNom(), tache.getNom()); - assertEquals(dto.getDescription(), tache.getDescription()); - } - - @Test - public void get_with_id_should_return_task() { - Tache tache = new Tache(); - tache.setNom("test"); - tache.setDescription("description"); - dao.insert(tache); - - Tache returned = target("taches").path(tache.getId().toString()).request().get(Tache.class); - - assertEquals(tache, returned); - } - - @Test - public void get_with_wrong_id_should_return_404() { - } - - @Test - public void get_for_description_should_work_with_existing_task() { - } - - @Test - public void get_for_description_with_wrong_id_should_return_404() { - } - - @Test - public void delete_should_remove_task_and_return_204() { - } - - @Test - public void delete_with_wrong_id_should_return_404() { - } - - @Test - public void put_should_replace_existing_task_values_return_200_and_task() { - } - - @Test - public void partial_put_should_fail_with_400() { - } - - @Test - public void post_with_form_data_should_return_201_location_and_task() { - } + private TacheDAO dao; + + @Override + protected Application configure() { + BDDFactory.setJdbiForTests(); + ResourceConfig rc = new ResourceConfig(TodoRessource.class); + // Dé-commenter pour avoir la trace des requêtes et réponses + // rc.register(new + // LoggingFeature(Logger.getLogger(LoggingFeature.DEFAULT_LOGGER_NAME), + // Level.INFO, + // LoggingFeature.Verbosity.PAYLOAD_ANY, 10000)); + // Dé-commenter pour avoir une trace des erreurs internes serveur (les tests sur + // le code de retour vont échouer) + // rc.register(DebugMapper.class); + + return rc; + } + + @Before + public void setEnvUp() { + dao = BDDFactory.buildDao(TacheDAO.class); + dao.createTable(); + } + + @After + public void tearEnvDown() throws Exception { + dao.dropTable(); + } + + @Test + public void get_should_return_empty_list_at_startup() { + List<Tache> answer = target("taches").request(MediaType.APPLICATION_JSON).get(new GenericType<List<Tache>>() { + }); + assertEquals(new ArrayList<Tache>(), answer); + } + + @Test + public void post_should_return_201_location_and_task() { + CreationTacheDTO dto = new CreationTacheDTO(); + dto.setNom("test"); + dto.setDescription("description test"); + + Response response = target("taches").request().post(Entity.json(dto)); + assertEquals(Response.Status.CREATED.getStatusCode(), response.getStatus()); + + Tache tache = response.readEntity(Tache.class); + assertEquals(target("taches").getUri().toString() + "/" + tache.getId().toString(), + response.getHeaderString("Location")); + + assertEquals(dto.getNom(), tache.getNom()); + assertEquals(dto.getDescription(), tache.getDescription()); + } + + @Test + public void get_with_id_should_return_task() { + Tache tache = new Tache(); + tache.setNom("test"); + tache.setDescription("description"); + dao.insert(tache); + + Tache returned = target("taches").path(tache.getId().toString()).request().get(Tache.class); + + assertEquals(tache, returned); + } + + @Test + public void get_with_wrong_id_should_return_404() { + Response response = target("taches").path(UUID.randomUUID().toString()).request().get(); + assertEquals(Response.Status.NOT_FOUND.getStatusCode(), response.getStatus()); + } + + @Test + public void get_for_description_should_work_with_existing_task() { + Tache tache = new Tache(); + tache.setNom("test"); + tache.setDescription("description"); + dao.insert(tache); + String returned = target("taches").path(tache.getId().toString()).path("description").request().get(String.class); + assertEquals(tache.getDescription(), returned); + } + + @Test + public void get_for_description_with_wrong_id_should_return_404() { + Response response = target("taches").path(UUID.randomUUID().toString()).path("description").request().get(); + assertEquals(Response.Status.NOT_FOUND.getStatusCode(), response.getStatus()); + } + + @Test + public void delete_should_remove_task_and_return_204() { + } + + @Test + public void delete_with_wrong_id_should_return_404() { + } + + @Test + public void put_should_replace_existing_task_values_return_200_and_task() { + } + + @Test + public void partial_put_should_fail_with_400() { + } + + @Test + public void post_with_form_data_should_return_201_location_and_task() { + Response response = target("taches").path(UUID.randomUUID().toString()).path("description").request().post(Form.class); + assertEquals(Response.Status.NOT_FOUND.getStatusCode(), response.getStatus()); + } } -- GitLab