Skip to content
Snippets Groups Projects
Commit 4cea773a authored by Hugo Wieder's avatar Hugo Wieder
Browse files

à faire : implémentation de la méthode POST

parent 14d3ee98
No related branches found
No related tags found
Loading
package fr.ulille.iut.pizzaland.beans;
import fr.ulille.iut.pizzaland.dto.IngredientDto;
public class Ingredient {
private long id;
private String name;
public Ingredient() {
}
public Ingredient(long id, String name) {
this.id = id;
this.name = name;
}
public void setId(long id) {
this.id = id;
}
public long getId() {
return id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public static IngredientDto toDto(Ingredient i) {
IngredientDto dto = new IngredientDto();
dto.setId(i.getId());
dto.setName(i.getName());
return dto;
}
public static Ingredient fromDto(IngredientDto dto) {
Ingredient ingredient = new Ingredient();
ingredient.setId(dto.getId());
ingredient.setName(dto.getName());
return ingredient;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Ingredient other = (Ingredient) obj;
if (id != other.id)
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
@Override
public String toString() {
return "Ingredient [id=" + id + ", name=" + name + "]";
}
}
\ No newline at end of file
package fr.ulille.iut.pizzaland.dao;
import java.util.List;
import org.jdbi.v3.sqlobject.config.RegisterBeanMapper;
import org.jdbi.v3.sqlobject.statement.GetGeneratedKeys;
import org.jdbi.v3.sqlobject.statement.SqlQuery;
import org.jdbi.v3.sqlobject.statement.SqlUpdate;
import fr.ulille.iut.pizzaland.beans.Ingredient;
public interface IngredientDao {
@SqlUpdate("CREATE TABLE IF NOT EXISTS ingredients (id INTEGER PRIMARY KEY, name VARCHAR UNIQUE NOT NULL)")
void createTable();
@SqlUpdate("DROP TABLE IF EXISTS ingredients")
void dropTable();
@SqlUpdate("INSERT INTO ingredients (name) VALUES (:name)")
@GetGeneratedKeys
long insert(String name);
@SqlQuery("SELECT * FROM ingredients")
@RegisterBeanMapper(Ingredient.class)
List<Ingredient> getAll();
@SqlQuery("SELECT * FROM ingredients WHERE id = :id")
@RegisterBeanMapper(Ingredient.class)
Ingredient findById(long id);
}
package fr.ulille.iut.pizzaland.dto; package fr.ulille.iut.pizzaland.dto;
public class IngredientDto { public class IngredientDto {
private long id;
private String name;
public IngredientDto() { public IngredientDto() {
} }
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
} }
package fr.ulille.iut.pizzaland.resources; package fr.ulille.iut.pizzaland.resources;
import java.net.URI;
import java.util.List; import java.util.List;
import java.util.ArrayList;
import java.util.logging.Logger; import java.util.logging.Logger;
import java.util.stream.Collectors;
import javax.ws.rs.GET; import javax.ws.rs.GET;
import javax.ws.rs.Path; import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Context; import javax.ws.rs.core.Context;
import javax.ws.rs.core.Response; import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriInfo; import javax.ws.rs.core.UriInfo;
import fr.ulille.iut.pizzaland.BDDFactory;
import fr.ulille.iut.pizzaland.beans.Ingredient;
import fr.ulille.iut.pizzaland.dao.IngredientDao;
import fr.ulille.iut.pizzaland.dto.IngredientDto; import fr.ulille.iut.pizzaland.dto.IngredientDto;
@Path("/ingredients") @Path("/ingredients")
public class IngredientResource { public class IngredientResource {
private static final Logger LOGGER = Logger.getLogger(IngredientResource.class.getName()); private static final Logger LOGGER = Logger.getLogger(IngredientResource.class.getName());
private IngredientDao ingredients;
@Context @Context
public UriInfo uriInfo; public UriInfo uriInfo;
public IngredientResource() { public IngredientResource() {
ingredients = BDDFactory.buildDao(IngredientDao.class);
ingredients.createTable();
} }
@GET @GET
public List<IngredientDto> getAll() { public List<IngredientDto> getAll() {
LOGGER.info("IngredientResource:getAll"); LOGGER.info("IngredientResource:getAll");
List<IngredientDto> l = ingredients.getAll().stream().map(Ingredient::toDto).collect(Collectors.toList());
return l;
}
return null;
@GET
@Path("{id}")
public IngredientDto getOneIngredient(@PathParam("id") long id) {
LOGGER.info("getOneIngredient(" + id + ")");
try {
Ingredient ingredient = ingredients.findById(id);
return Ingredient.toDto(ingredient);
} }
catch ( Exception e ) {
// Cette exception générera une réponse avec une erreur 404
throw new WebApplicationException(Response.Status.NOT_FOUND);
}
}
} }
package fr.ulille.iut.pizzaland; package fr.ulille.iut.pizzaland;
import fr.ulille.iut.pizzaland.ApiV1; import fr.ulille.iut.pizzaland.ApiV1;
import fr.ulille.iut.pizzaland.beans.Ingredient;
import fr.ulille.iut.pizzaland.dao.IngredientDao;
import fr.ulille.iut.pizzaland.dto.IngredientDto; import fr.ulille.iut.pizzaland.dto.IngredientDto;
import org.glassfish.jersey.test.JerseyTest; import org.glassfish.jersey.test.JerseyTest;
...@@ -26,9 +28,10 @@ import java.util.logging.Logger; ...@@ -26,9 +28,10 @@ import java.util.logging.Logger;
*/ */
public class IngredientResourceTest extends JerseyTest { public class IngredientResourceTest extends JerseyTest {
private static final Logger LOGGER = Logger.getLogger(IngredientResourceTest.class.getName()); private static final Logger LOGGER = Logger.getLogger(IngredientResourceTest.class.getName());
private IngredientDao dao;
@Override @Override
protected Application configure() { protected Application configure() {
BDDFactory.setJdbiForTests();
return new ApiV1(); return new ApiV1();
} }
...@@ -38,12 +41,14 @@ public class IngredientResourceTest extends JerseyTest { ...@@ -38,12 +41,14 @@ public class IngredientResourceTest extends JerseyTest {
// https://stackoverflow.com/questions/25906976/jerseytest-and-junit-throws-nullpointerexception // https://stackoverflow.com/questions/25906976/jerseytest-and-junit-throws-nullpointerexception
@Before @Before
public void setEnvUp() { public void setEnvUp() {
dao = BDDFactory.buildDao(IngredientDao.class);
dao.createTable();
} }
@After @After
public void tearEnvDown() throws Exception { public void tearEnvDown() throws Exception {
dao.dropTable();
} }
@Test @Test
...@@ -66,4 +71,27 @@ public class IngredientResourceTest extends JerseyTest { ...@@ -66,4 +71,27 @@ public class IngredientResourceTest extends JerseyTest {
assertEquals(0, ingredients.size()); assertEquals(0, ingredients.size());
} }
@Test
public void testGetExistingIngredient() {
Ingredient ingredient = new Ingredient();
ingredient.setName("mozzarella");
long id = dao.insert(ingredient.getName());
ingredient.setId(id);
Response response = target("/ingredients/" + id).request().get();
assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
Ingredient result = Ingredient.fromDto(response.readEntity(IngredientDto.class));
assertEquals(ingredient, result);
}
@Test
public void testGetNotExistingPizza() {
Response response = target("/pizzas/125").request().get();
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