Skip to content
Snippets Groups Projects
Commit c26ce4dc authored by Jeremy Delobel's avatar Jeremy Delobel
Browse files

Avancement du TP après le cours de PROG-REPARTIE

parent c42d2483
No related branches found
No related tags found
No related merge requests found
...@@ -24,5 +24,9 @@ public interface TacheDAO { ...@@ -24,5 +24,9 @@ public interface TacheDAO {
@SqlQuery("select * from taches") @SqlQuery("select * from taches")
@RegisterBeanMapper(Tache.class) @RegisterBeanMapper(Tache.class)
List<Tache> getAll(); List<Tache> getAll();
@SqlQuery("select * from taches where id = ?")
@RegisterBeanMapper(Tache.class)
Tache getById(String id);
} }
...@@ -24,6 +24,7 @@ import jakarta.ws.rs.core.MultivaluedMap; ...@@ -24,6 +24,7 @@ import jakarta.ws.rs.core.MultivaluedMap;
import jakarta.ws.rs.core.Request; import jakarta.ws.rs.core.Request;
import jakarta.ws.rs.core.Response; import jakarta.ws.rs.core.Response;
import jakarta.ws.rs.core.Response.ResponseBuilder; import jakarta.ws.rs.core.Response.ResponseBuilder;
import jakarta.ws.rs.core.Response.Status;
import jakarta.ws.rs.core.UriInfo; import jakarta.ws.rs.core.UriInfo;
@Path("taches") @Path("taches")
...@@ -64,4 +65,28 @@ public class TodoRessource { ...@@ -64,4 +65,28 @@ public class TodoRessource {
} }
return builder.build(); 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();
}
} }
...@@ -15,7 +15,7 @@ public class TodoService { ...@@ -15,7 +15,7 @@ public class TodoService {
} }
public Tache getTache(UUID id) { public Tache getTache(UUID id) {
return null; return taches.getById(id.toString());
} }
public List<Tache> getAll() { public List<Tache> getAll() {
...@@ -33,4 +33,5 @@ public class TodoService { ...@@ -33,4 +33,5 @@ public class TodoService {
public void updateTache(Tache tache) { public void updateTache(Tache tache) {
return; return;
} }
} }
...@@ -5,6 +5,7 @@ import static org.junit.Assert.assertTrue; ...@@ -5,6 +5,7 @@ import static org.junit.Assert.assertTrue;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.UUID;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
...@@ -29,100 +30,112 @@ import jakarta.ws.rs.core.MediaType; ...@@ -29,100 +30,112 @@ import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.Response; import jakarta.ws.rs.core.Response;
public class TodoRessourceTest extends JerseyTest { public class TodoRessourceTest extends JerseyTest {
private TacheDAO dao; private TacheDAO dao;
@Override @Override
protected Application configure() { protected Application configure() {
BDDFactory.setJdbiForTests(); BDDFactory.setJdbiForTests();
ResourceConfig rc = new ResourceConfig(TodoRessource.class); ResourceConfig rc = new ResourceConfig(TodoRessource.class);
// Dé-commenter pour avoir la trace des requêtes et réponses // Dé-commenter pour avoir la trace des requêtes et réponses
// rc.register(new // rc.register(new
// LoggingFeature(Logger.getLogger(LoggingFeature.DEFAULT_LOGGER_NAME), // LoggingFeature(Logger.getLogger(LoggingFeature.DEFAULT_LOGGER_NAME),
// Level.INFO, // Level.INFO,
// LoggingFeature.Verbosity.PAYLOAD_ANY, 10000)); // LoggingFeature.Verbosity.PAYLOAD_ANY, 10000));
// Dé-commenter pour avoir une trace des erreurs internes serveur (les tests sur // Dé-commenter pour avoir une trace des erreurs internes serveur (les tests sur
// le code de retour vont échouer) // le code de retour vont échouer)
// rc.register(DebugMapper.class); // rc.register(DebugMapper.class);
return rc; return rc;
} }
@Before @Before
public void setEnvUp() { public void setEnvUp() {
dao = BDDFactory.buildDao(TacheDAO.class); dao = BDDFactory.buildDao(TacheDAO.class);
dao.createTable(); dao.createTable();
} }
@After @After
public void tearEnvDown() throws Exception { public void tearEnvDown() throws Exception {
dao.dropTable(); dao.dropTable();
} }
@Test @Test
public void get_should_return_empty_list_at_startup() { public void get_should_return_empty_list_at_startup() {
List<Tache> answer = target("taches").request(MediaType.APPLICATION_JSON).get(new GenericType<List<Tache>>() { List<Tache> answer = target("taches").request(MediaType.APPLICATION_JSON).get(new GenericType<List<Tache>>() {
}); });
assertEquals(new ArrayList<Tache>(), answer); assertEquals(new ArrayList<Tache>(), answer);
} }
@Test @Test
public void post_should_return_201_location_and_task() { public void post_should_return_201_location_and_task() {
CreationTacheDTO dto = new CreationTacheDTO(); CreationTacheDTO dto = new CreationTacheDTO();
dto.setNom("test"); dto.setNom("test");
dto.setDescription("description test"); dto.setDescription("description test");
Response response = target("taches").request().post(Entity.json(dto)); Response response = target("taches").request().post(Entity.json(dto));
assertEquals(Response.Status.CREATED.getStatusCode(), response.getStatus()); assertEquals(Response.Status.CREATED.getStatusCode(), response.getStatus());
Tache tache = response.readEntity(Tache.class); Tache tache = response.readEntity(Tache.class);
assertEquals(target("taches").getUri().toString() + "/" + tache.getId().toString(), assertEquals(target("taches").getUri().toString() + "/" + tache.getId().toString(),
response.getHeaderString("Location")); response.getHeaderString("Location"));
assertEquals(dto.getNom(), tache.getNom()); assertEquals(dto.getNom(), tache.getNom());
assertEquals(dto.getDescription(), tache.getDescription()); assertEquals(dto.getDescription(), tache.getDescription());
} }
@Test @Test
public void get_with_id_should_return_task() { public void get_with_id_should_return_task() {
Tache tache = new Tache(); Tache tache = new Tache();
tache.setNom("test"); tache.setNom("test");
tache.setDescription("description"); tache.setDescription("description");
dao.insert(tache); dao.insert(tache);
Tache returned = target("taches").path(tache.getId().toString()).request().get(Tache.class); Tache returned = target("taches").path(tache.getId().toString()).request().get(Tache.class);
assertEquals(tache, returned); assertEquals(tache, returned);
} }
@Test @Test
public void get_with_wrong_id_should_return_404() { 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() {
} @Test
public void get_for_description_should_work_with_existing_task() {
@Test Tache tache = new Tache();
public void get_for_description_with_wrong_id_should_return_404() { tache.setNom("test");
} tache.setDescription("description");
dao.insert(tache);
@Test String returned = target("taches").path(tache.getId().toString()).path("description").request().get(String.class);
public void delete_should_remove_task_and_return_204() { assertEquals(tache.getDescription(), returned);
} }
@Test @Test
public void delete_with_wrong_id_should_return_404() { 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 put_should_replace_existing_task_values_return_200_and_task() {
} @Test
public void delete_should_remove_task_and_return_204() {
@Test }
public void partial_put_should_fail_with_400() {
} @Test
public void delete_with_wrong_id_should_return_404() {
@Test }
public void post_with_form_data_should_return_201_location_and_task() {
} @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());
}
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment