Skip to content
Snippets Groups Projects
Commit d71b1de4 authored by Felicien DELANNOY's avatar Felicien DELANNOY
Browse files

end

parent c42d2483
No related branches found
No related tags found
No related merge requests found
Pipeline #8334 passed
......@@ -24,5 +24,16 @@ 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 tache);
}
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,69 @@ public class TodoRessource {
}
return builder.build();
}
@GET
@Path("{id}")
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
public Tache getById(@PathParam("id")UUID id) {
Tache res=todoService.getTache(id);
if(res == null) {
throw new WebApplicationException(404);
}
return res;
}
@GET
@Path("{id}/description")
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
public String getDescription(@PathParam("id")UUID id) {
String res=todoService.getTache(id).getDescription();
if(res == null) {
throw new WebApplicationException(404);
}
return res;
}
@POST
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
public Response createTacheFromForm(@FormParam("nom") String nom, @FormParam("description") String description) {
LOGGER.info("createTacheFromForm()");
Tache tache = new Tache();
tache.setNom(nom);
tache.setDescription(description);
todoService.addTache(tache);
URI location = uri.getAbsolutePathBuilder().path(tache.getId().toString()).build();
EntityTag etag = new EntityTag(Integer.toString(tache.hashCode()));
ResponseBuilder builder = request.evaluatePreconditions(etag);
if (builder == null) {
builder = Response.created(location);
builder.tag(etag);
builder.entity(tache);
}
return builder.build();
}
@DELETE
@Path("{id}")
public Response deleteTask(@PathParam("id")UUID id) {
int res=todoService.deleteTache(id.toString());
if(res==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 tache = Tache.fromCreationTacheDTO(tacheDto);
todoService.updateTache(tache);
return Response.accepted(Entity.json(tache)).build();
}
}
......@@ -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);
}
}
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;
......@@ -96,33 +91,74 @@ public class TodoRessourceTest extends JerseyTest {
@Test
public void get_with_wrong_id_should_return_404() {
Response response = target("taches").path(UUID.randomUUID().toString()).request().get();
assertEquals(response.getStatus(), 404);
}
@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("descripion").request().get();
assertEquals(response.getStatus(), 404);
}
@Test
public void delete_should_remove_task_and_return_204() {
Tache tache = new Tache();
tache.setNom("test");
tache.setDescription("description");
dao.insert(tache);
Response response = target("taches").path(tache.getId().toString()).request().delete();
assertEquals(response.getStatus(), 204);
}
@Test
public void delete_with_wrong_id_should_return_404() {
Response response = target("taches").path(UUID.randomUUID().toString()).request().delete();
assertEquals(response.getStatus(), 404);
}
@Test
public void put_should_replace_existing_task_values_return_200_and_task() {
Tache t = new Tache();
t.setNom("test");
t.setDescription("description");
dao.insert(t);
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 t = new Tache();
t.setNom("test");
t.setDescription("description");
dao.insert(t);
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);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment