Skip to content
Snippets Groups Projects
Commit 383e4942 authored by Theo CANONNE's avatar Theo CANONNE
Browse files

Ajout pizza

parent b3f5ebe9
No related branches found
No related tags found
No related merge requests found
......@@ -2,8 +2,16 @@ package fr.ulille.iut.pizzaland;
import org.glassfish.jersey.server.ResourceConfig;
import fr.ulille.iut.pizzaland.beans.Ingredient;
import fr.ulille.iut.pizzaland.dao.IngredientDAO;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;
import javax.json.bind.Jsonb;
import javax.json.bind.JsonbBuilder;
import javax.ws.rs.ApplicationPath;
@ApplicationPath("api/v1/")
......@@ -12,5 +20,26 @@ public class ApiV1 extends ResourceConfig {
public ApiV1() {
packages("fr.ulille.iut.pizzaland");
String environment = System.getenv("PIZZAENV");
if ( environment != null && environment.equals("withdb") ) {
LOGGER.info("Loading with database");
Jsonb jsonb = JsonbBuilder.create();
try {
FileReader reader = new FileReader( getClass().getClassLoader().getResource("ingredients.json").getFile() );
List<Ingredient> ingredients = JsonbBuilder.create().fromJson(reader, new ArrayList<Ingredient>(){}.getClass().getGenericSuperclass());
IngredientDAO ingredientDao = BDDFactory.buildDao(IngredientDAO.class);
ingredientDao.dropTable();
ingredientDao.createTable();
for ( Ingredient ingredient: ingredients) {
ingredientDao.insert(ingredient.getName());
}
} catch ( Exception ex ) {
throw new IllegalStateException(ex);
}
}
}
}
......@@ -10,15 +10,12 @@ import org.jdbi.v3.sqlobject.SqlObjectPlugin;
public class BDDFactory {
private static Jdbi jdbi = null;
private static String dbPath = "jdbc:sqlite:"
+ System.getProperty("java.io.tmpdir")
+ System.getProperty("file.separator")
+ System.getProperty("user.name")
+ "_";
public static Jdbi getJdbi() {
if ( jdbi == null ) {
jdbi = Jdbi.create(dbPath + "pizza.db")
jdbi = Jdbi.create("jdbc:sqlite:"
+ System.getProperty("java.io.tmpdir")
+ System.getProperty("file.separator") + "pizza.db")
.installPlugin(new SQLitePlugin())
.installPlugin(new SqlObjectPlugin());
}
......@@ -27,7 +24,9 @@ public class BDDFactory {
public static void setJdbiForTests() {
if ( jdbi == null ) {
jdbi = Jdbi.create(dbPath + "pizza_test.db")
jdbi = Jdbi.create("jdbc:sqlite:"
+ System.getProperty("java.io.tmpdir")
+ System.getProperty("file.separator") + "pizza_test.db")
.installPlugin(new SQLitePlugin())
.installPlugin(new SqlObjectPlugin());
}
......@@ -44,4 +43,4 @@ public class BDDFactory {
public static <T> T buildDao(Class<T> daoClass) {
return getJdbi().onDemand(daoClass);
}
}
}
\ No newline at end of file
package fr.ulille.iut.pizzaland.dto;
public class IngredientDto {
long id;
String name;
public IngredientDto() {
}
public void setId(long id) {
this.id=id;
}
public long getId() {
return this.id;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return this.name;
}
}
......@@ -2,31 +2,116 @@ package fr.ulille.iut.pizzaland.resources;
import java.net.URI;
import java.util.List;
import java.util.ArrayList;
import java.util.logging.Logger;
import java.util.stream.Collectors;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
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.Response;
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.IngredientCreateDto;
import fr.ulille.iut.pizzaland.dto.IngredientDto;
@Path("/ingredients")
public class IngredientResource {
private static final Logger LOGGER = Logger.getLogger(IngredientResource.class.getName());
private IngredientDAO dao;
@Context
public UriInfo uriInfo;
public IngredientResource() {
dao = BDDFactory.buildDao(IngredientDAO.class);
dao.createTable();
}
/*@GET
@Path("{id}")
public IngredientDto getOneIngredient() {
Ingredient ingredient = new Ingredient();
ingredient.setId(1);
ingredient.setName("mozzarella");
return Ingredient.toDto(ingredient);
}*/
@GET
@Path("{id}")
public IngredientDto getOneIngredient(@PathParam("id") long id) {
LOGGER.info("getOneIngredient(" + id + ")");
try {
Ingredient ingredient = dao.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);
}
}
@GET
public List<IngredientDto> getAll() {
LOGGER.info("IngredientResource:getAll");
List<IngredientDto> l = dao.getAll().stream().map(Ingredient::toDto).collect(Collectors.toList());
return l;
}
@POST
public Response createIngredient(IngredientCreateDto ingredientCreateDto) {
Ingredient existing = dao.findByName(ingredientCreateDto.getName());
if ( existing != null ) {
throw new WebApplicationException(Response.Status.CONFLICT);
}
try {
Ingredient ingredient = Ingredient.fromIngredientCreateDto(ingredientCreateDto);
long id = dao.insert(ingredient.getName());
ingredient.setId(id);
IngredientDto ingredientDto = Ingredient.toDto(ingredient);
URI uri = uriInfo.getAbsolutePathBuilder().path("" + id).build();
return null;
return Response.created(uri).entity(ingredientDto).build();
}
catch ( Exception e ) {
e.printStackTrace();
throw new WebApplicationException(Response.Status.NOT_ACCEPTABLE);
}
}
@DELETE
@Path("{id}")
public Response deleteIngredient(@PathParam("id") long id) {
if ( dao.findById(id) == null ) {
throw new WebApplicationException(Response.Status.NOT_FOUND);
}
dao.remove(id);
return Response.status(Response.Status.ACCEPTED).build();
}
@GET
@Path("{id}/name")
public String getIngredientName(@PathParam("id") long id) {
Ingredient ingredient = dao.findById(id);
if ( ingredient == null ) {
throw new WebApplicationException(Response.Status.NOT_FOUND);
}
return ingredient.getName();
}
}
package fr.ulille.iut.pizzaland;
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.IngredientCreateDto;
import fr.ulille.iut.pizzaland.dto.IngredientDto;
import org.glassfish.jersey.test.JerseyTest;
......@@ -26,9 +29,12 @@ import java.util.logging.Logger;
*/
public class IngredientResourceTest extends JerseyTest {
private static final Logger LOGGER = Logger.getLogger(IngredientResourceTest.class.getName());
private IngredientDAO dao;
@Override
protected Application configure() {
BDDFactory.setJdbiForTests();
return new ApiV1();
}
......@@ -38,12 +44,14 @@ public class IngredientResourceTest extends JerseyTest {
// https://stackoverflow.com/questions/25906976/jerseytest-and-junit-throws-nullpointerexception
@Before
public void setEnvUp() {
dao = BDDFactory.buildDao(IngredientDAO.class);
dao.createTable();
}
@After
public void tearEnvDown() throws Exception {
dao.dropTable();
}
@Test
......@@ -66,4 +74,122 @@ public class IngredientResourceTest extends JerseyTest {
assertEquals(0, ingredients.size());
}
@Test
public void testGetExistingIngredient() {
/*Ingredient ingredient = new Ingredient();
ingredient.setId(1);
ingredient.setName("mozzarella");
Response response = target("/ingredients/1").request().get();
assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
Ingredient result = Ingredient.fromDto(response.readEntity(IngredientDto.class));
assertEquals(ingredient, result);*/
Ingredient ingredient = new Ingredient();
ingredient.setName("Chorizo");
ingredient.setId(1);
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());
}
@Test
public void testCreateIngredient() {
IngredientCreateDto ingredientCreateDto = new IngredientCreateDto();
ingredientCreateDto.setName("Chorizo");
Response response = target("/ingredients").request().post(Entity.json(ingredientCreateDto));
// On vérifie le code de status à 201
assertEquals(Response.Status.CREATED.getStatusCode(), response.getStatus());
IngredientDto returnedEntity = response.readEntity(IngredientDto.class);
// On vérifie que le champ d'entête Location correspond à
// l'URI de la nouvelle entité
assertEquals(target("/ingredients/" + returnedEntity.getId()).getUri(), response.getLocation());
// On vérifie que le nom correspond
assertEquals(returnedEntity.getName(), ingredientCreateDto.getName());
}
@Test
public void testCreateSameIngredient() {
IngredientCreateDto ingredientCreateDto = new IngredientCreateDto();
ingredientCreateDto.setName("Chorizo");
dao.insert(ingredientCreateDto.getName());
Response response = target("/ingredients").request().post(Entity.json(ingredientCreateDto));
assertEquals(Response.Status.CONFLICT.getStatusCode(), response.getStatus());
}
@Test
public void testCreateIngredientWithoutName() {
IngredientCreateDto ingredientCreateDto = new IngredientCreateDto();
Response response = target("/ingredients").request().post(Entity.json(ingredientCreateDto));
assertEquals(Response.Status.NOT_ACCEPTABLE.getStatusCode(), response.getStatus());
}
@Test
public void testDeleteExistingIngredient() {
Ingredient ingredient = new Ingredient();
ingredient.setName("Chorizo");
long id = dao.insert(ingredient.getName());
ingredient.setId(id);
Response response = target("/ingredients/" + id).request().delete();
assertEquals(Response.Status.ACCEPTED.getStatusCode(), response.getStatus());
Ingredient result = dao.findById(id);
assertEquals(result, null);
}
@Test
public void testDeleteNotExistingIngredient() {
Response response = target("/ingredients/125").request().delete();
assertEquals(Response.Status.NOT_FOUND.getStatusCode(),
response.getStatus());
}
@Test
public void testGetIngredientName() {
Ingredient ingredient = new Ingredient();
ingredient.setName("Chorizo");
long id = dao.insert(ingredient.getName());
Response response = target("ingredients/" + id + "/name").request().get();
assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
assertEquals("Chorizo", response.readEntity(String.class));
}
@Test
public void testGetNotExistingIngredientName() {
Response response = target("ingredients/125/name").request().get();
assertEquals(Response.Status.NOT_FOUND.getStatusCode(), response.getStatus());
}
}
File mode changed from 100644 to 100755
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment