Skip to content
Snippets Groups Projects
Commit 2cd818e1 authored by Hugo Dutoit's avatar Hugo Dutoit
Browse files

Create TACHE from FORM

parent c42d2483
No related branches found
No related tags found
No related merge requests found
Pipeline #8332 passed
......@@ -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);
}
......@@ -46,6 +46,51 @@ public class TodoRessource {
return todoService.getAll();
}
@GET
@Path("{id}")
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
public Tache getById(@PathParam("id") String id){
LOGGER.info("getId()");
Tache res = todoService.getTache(UUID.fromString(id));
if(res == null){
throw new WebApplicationException(Response.Status.NOT_FOUND);
}
return res;
}
@GET
@Path("description/{id}")
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
public String getDescriptionById(@PathParam("id") String id){
LOGGER.info("getId()");
Tache res = todoService.getTache(UUID.fromString(id));
if(res == null){
throw new WebApplicationException(Response.Status.NOT_FOUND);
}
return res.getDescription();
}
@POST
@Consumes({MediaType.APPLICATION_FORM_URLENCODED})
public Response createTacheFromForm(MultivaluedMap<String, String> formParams) {
Tache tache = new Tache();
tache.setNom(formParams.getFirst("nom"));
tache.setDescription(formParams.getFirst("description"));
tache.setId(UUID.fromString(formParams.getFirst("id")));
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();
}
@POST
public Response createTache(CreationTacheDTO tacheDto) {
LOGGER.info("createTache()");
......
......@@ -15,7 +15,7 @@ public class TodoService {
}
public Tache getTache(UUID id) {
return null;
return taches.getById(id.toString());
}
public List<Tache> getAll() {
......
......@@ -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;
......@@ -88,7 +89,6 @@ public class TodoRessourceTest extends JerseyTest {
tache.setNom("test");
tache.setDescription("description");
dao.insert(tache);
Tache returned = target("taches").path(tache.getId().toString()).request().get(Tache.class);
assertEquals(tache, returned);
......@@ -96,14 +96,25 @@ public class TodoRessourceTest extends JerseyTest {
@Test
public void get_with_wrong_id_should_return_404() {
Response res = target("taches").path(UUID.randomUUID().toString()).request().get();
assertEquals(res.getStatus(),Response.Status.NOT_FOUND.getStatusCode());
}
@Test
public void get_for_description_should_work_with_existing_task() {
Tache tache = new Tache();
tache.setNom("test");
tache.setDescription("descriptionTest");
dao.insert(tache);
String returnedDecript = target("taches/description").path(tache.getId().toString()).request().get(String.class);
assertEquals(returnedDecript,tache.getDescription());
}
@Test
public void get_for_description_with_wrong_id_should_return_404() {
Response res = target("taches/description").path(UUID.randomUUID().toString()).request().get();
assertEquals(res.getStatus(),Response.Status.NOT_FOUND.getStatusCode());
}
@Test
......@@ -124,5 +135,34 @@ public class TodoRessourceTest extends JerseyTest {
@Test
public void post_with_form_data_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());
Tache tache = new Tache();
tache.setNom("plop");
tache.setDescription("descriptionPLOP");
Form form = new Form();
form.param("nom",tache.getNom());
form.param("description",tache.getDescription());
form.param("id",tache.getId().toString());
Response res = target("taches").request().post(Entity.form(form));
assertEquals(res.getStatus(),Response.Status.CREATED.getStatusCode());
assertEquals((Tache)res.readEntity(Tache.class),tache);
System.out.println(res.getLocation());
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment