From 9e68076fed08768534a4b77bc250eedfb75568cf Mon Sep 17 00:00:00 2001 From: deksor <dxceor@gmail.com> Date: Fri, 6 Mar 2020 16:44:02 +0100 Subject: [PATCH] =?UTF-8?q?Tuto=20termin=C3=A9=20et=20Pizza=20en=20cours?= =?UTF-8?q?=20d'ajout?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 0 architecture.svg | 0 pom.xml | 0 .../java/fr/ulille/iut/pizzaland/ApiV1.java | 33 ++++ .../fr/ulille/iut/pizzaland/BDDFactory.java | 0 .../fr/ulille/iut/pizzaland/beans/.gitkeep | 0 .../iut/pizzaland/beans/Ingredient.java | 88 +++++++++ .../fr/ulille/iut/pizzaland/beans/Pizza.java | 123 +++++++++++++ .../java/fr/ulille/iut/pizzaland/dao/.gitkeep | 0 .../iut/pizzaland/dao/IngredientDao.java | 41 +++++ .../fr/ulille/iut/pizzaland/dao/PizzaDao.java | 77 ++++++++ .../pizzaland/dto/IngredientCreateDto.java | 16 ++ .../iut/pizzaland/dto/IngredientDto.java | 22 ++- .../iut/pizzaland/dto/PizzaCreateDto.java | 28 +++ .../fr/ulille/iut/pizzaland/dto/PizzaDto.java | 43 +++++ .../resources/IngredientResource.java | 133 +++++++++++++- .../pizzaland/resources/PizzaResource.java | 86 +++++++++ src/main/resources/ingredients.json | 0 src/main/resources/logback.xml | 0 src/main/webapp/WEB-INF/.gitkeep | 0 .../iut/pizzaland/IngredientResourceTest.java | 170 ++++++++++++++---- .../iut/pizzaland/PizzaResourceTest.java | 128 +++++++++++++ src/test/resources/logback-test.xml | 0 23 files changed, 951 insertions(+), 37 deletions(-) mode change 100644 => 100755 README.md mode change 100644 => 100755 architecture.svg mode change 100644 => 100755 pom.xml mode change 100644 => 100755 src/main/java/fr/ulille/iut/pizzaland/ApiV1.java mode change 100644 => 100755 src/main/java/fr/ulille/iut/pizzaland/BDDFactory.java mode change 100644 => 100755 src/main/java/fr/ulille/iut/pizzaland/beans/.gitkeep create mode 100755 src/main/java/fr/ulille/iut/pizzaland/beans/Ingredient.java create mode 100755 src/main/java/fr/ulille/iut/pizzaland/beans/Pizza.java mode change 100644 => 100755 src/main/java/fr/ulille/iut/pizzaland/dao/.gitkeep create mode 100755 src/main/java/fr/ulille/iut/pizzaland/dao/IngredientDao.java create mode 100755 src/main/java/fr/ulille/iut/pizzaland/dao/PizzaDao.java create mode 100755 src/main/java/fr/ulille/iut/pizzaland/dto/IngredientCreateDto.java mode change 100644 => 100755 src/main/java/fr/ulille/iut/pizzaland/dto/IngredientDto.java create mode 100755 src/main/java/fr/ulille/iut/pizzaland/dto/PizzaCreateDto.java create mode 100755 src/main/java/fr/ulille/iut/pizzaland/dto/PizzaDto.java mode change 100644 => 100755 src/main/java/fr/ulille/iut/pizzaland/resources/IngredientResource.java create mode 100755 src/main/java/fr/ulille/iut/pizzaland/resources/PizzaResource.java mode change 100644 => 100755 src/main/resources/ingredients.json mode change 100644 => 100755 src/main/resources/logback.xml mode change 100644 => 100755 src/main/webapp/WEB-INF/.gitkeep mode change 100644 => 100755 src/test/java/fr/ulille/iut/pizzaland/IngredientResourceTest.java create mode 100755 src/test/java/fr/ulille/iut/pizzaland/PizzaResourceTest.java mode change 100644 => 100755 src/test/resources/logback-test.xml diff --git a/README.md b/README.md old mode 100644 new mode 100755 diff --git a/architecture.svg b/architecture.svg old mode 100644 new mode 100755 diff --git a/pom.xml b/pom.xml old mode 100644 new mode 100755 diff --git a/src/main/java/fr/ulille/iut/pizzaland/ApiV1.java b/src/main/java/fr/ulille/iut/pizzaland/ApiV1.java old mode 100644 new mode 100755 index b565c83..783f18f --- a/src/main/java/fr/ulille/iut/pizzaland/ApiV1.java +++ b/src/main/java/fr/ulille/iut/pizzaland/ApiV1.java @@ -5,6 +5,18 @@ import org.glassfish.jersey.server.ResourceConfig; import java.util.logging.Logger; import javax.ws.rs.ApplicationPath; +import fr.ulille.iut.pizzaland.beans.Ingredient; +import fr.ulille.iut.pizzaland.dao.IngredientDao; + +import java.io.FileReader; +import java.io.IOException; +import java.nio.file.Paths; +import java.util.ArrayList; +import java.util.List; + +import javax.json.bind.Jsonb; +import javax.json.bind.JsonbBuilder; + @ApplicationPath("api/v1/") public class ApiV1 extends ResourceConfig { @@ -12,5 +24,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); + } + } } } diff --git a/src/main/java/fr/ulille/iut/pizzaland/BDDFactory.java b/src/main/java/fr/ulille/iut/pizzaland/BDDFactory.java old mode 100644 new mode 100755 diff --git a/src/main/java/fr/ulille/iut/pizzaland/beans/.gitkeep b/src/main/java/fr/ulille/iut/pizzaland/beans/.gitkeep old mode 100644 new mode 100755 diff --git a/src/main/java/fr/ulille/iut/pizzaland/beans/Ingredient.java b/src/main/java/fr/ulille/iut/pizzaland/beans/Ingredient.java new file mode 100755 index 0000000..3670087 --- /dev/null +++ b/src/main/java/fr/ulille/iut/pizzaland/beans/Ingredient.java @@ -0,0 +1,88 @@ +package fr.ulille.iut.pizzaland.beans; + +import fr.ulille.iut.pizzaland.dto.IngredientDto; +import fr.ulille.iut.pizzaland.dto.IngredientCreateDto; + +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 + "]"; + } + + public static IngredientCreateDto toCreateDto(Ingredient ingredient) { + IngredientCreateDto dto = new IngredientCreateDto(); + dto.setName(ingredient.getName()); + + return dto; +} + +public static Ingredient fromIngredientCreateDto(IngredientCreateDto dto) { + Ingredient ingredient = new Ingredient(); + ingredient.setName(dto.getName()); + + return ingredient; +} + +} diff --git a/src/main/java/fr/ulille/iut/pizzaland/beans/Pizza.java b/src/main/java/fr/ulille/iut/pizzaland/beans/Pizza.java new file mode 100755 index 0000000..57f041b --- /dev/null +++ b/src/main/java/fr/ulille/iut/pizzaland/beans/Pizza.java @@ -0,0 +1,123 @@ +package fr.ulille.iut.pizzaland.beans; + +import java.util.List; +import java.util.ArrayList; +import fr.ulille.iut.pizzaland.dto.PizzaDto; +import fr.ulille.iut.pizzaland.dto.PizzaCreateDto; + +public class Pizza { + + private long id; + private String name; + private List<Ingredient> ingredients; + + public Pizza() { + this.ingredients = new ArrayList<Ingredient>(); + } + + public Pizza(long id, String name) { + this.id = id; + this.name = name; + this.ingredients = new ArrayList<Ingredient>(); + } + + public long getId() { + return id; + } + + public void setId(long id) { + this.id = id; + } + + public void setName(String name) { + this.name = name; + } + + public String getName() { + return name; + } + + public List<Ingredient> getIngredients() { + return this.ingredients; + } + + public void setIngredients(List<Ingredient> ingredients) { + this.ingredients = ingredients; + } + + public void addIngredients(Ingredient ingredient) { + this.ingredients.add(ingredient); + } + + public static PizzaDto toDto(Pizza p) { + PizzaDto dto = new PizzaDto(); + dto.setId(p.getId()); + dto.setName(p.getName()); + dto.setIngredients(p.getIngredients()); + + return dto; + } + + public static Pizza fromDto(PizzaDto dto) { + Pizza pizza = new Pizza(); + pizza.setId(dto.getId()); + pizza.setName(dto.getName()); + pizza.setIngredients(dto.getIngredients()); + + return pizza; + } + + @Override + public String toString() { + return "Pizza [id=" + id + ", ingredients=" + ingredients + ", name=" + name + "]"; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + (int) (id ^ (id >>> 32)); + result = prime * result + ((ingredients == null) ? 0 : ingredients.hashCode()); + result = prime * result + ((name == null) ? 0 : name.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + Pizza other = (Pizza) obj; + if (id != other.id) + return false; + if (ingredients == null) { + if (other.ingredients != null) + return false; + } else if (!ingredients.equals(other.ingredients)) + return false; + if (name == null) { + if (other.name != null) + return false; + } else if (!name.equals(other.name)) + return false; + return true; + } + + public static PizzaCreateDto toCreateDto(Pizza pizza) { + PizzaCreateDto dto = new PizzaCreateDto(); + dto.setName(pizza.getName()); + + return dto; + } + + public static Pizza fromPizzaCreateDto(PizzaCreateDto dto) { + Pizza pizza = new Pizza(); + pizza.setName(dto.getName()); + + return pizza; + } + +} diff --git a/src/main/java/fr/ulille/iut/pizzaland/dao/.gitkeep b/src/main/java/fr/ulille/iut/pizzaland/dao/.gitkeep old mode 100644 new mode 100755 diff --git a/src/main/java/fr/ulille/iut/pizzaland/dao/IngredientDao.java b/src/main/java/fr/ulille/iut/pizzaland/dao/IngredientDao.java new file mode 100755 index 0000000..20cb539 --- /dev/null +++ b/src/main/java/fr/ulille/iut/pizzaland/dao/IngredientDao.java @@ -0,0 +1,41 @@ + +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); + + @SqlQuery("SELECT * FROM ingredients WHERE name = :name") + @RegisterBeanMapper(Ingredient.class) + Ingredient findByName(String name); + + @SqlUpdate("DELETE FROM ingredients WHERE id = :id") + void remove(long id); + + +} diff --git a/src/main/java/fr/ulille/iut/pizzaland/dao/PizzaDao.java b/src/main/java/fr/ulille/iut/pizzaland/dao/PizzaDao.java new file mode 100755 index 0000000..802a745 --- /dev/null +++ b/src/main/java/fr/ulille/iut/pizzaland/dao/PizzaDao.java @@ -0,0 +1,77 @@ +package fr.ulille.iut.pizzaland.dao; +import org.jdbi.v3.sqlobject.transaction.Transaction; +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 java.util.List; + +import fr.ulille.iut.pizzaland.beans.Pizza; +import fr.ulille.iut.pizzaland.beans.Ingredient; + + +public interface PizzaDao { + + @SqlUpdate("CREATE TABLE IF NOT EXISTS pizza (id INTEGER PRIMARY KEY, name VARCHAR UNIQUE NOT NULL)") + void createPizzaTable(); + + @SqlUpdate("CREATE TABLE IF NOT EXISTS PizzaIngredientsAssociation (idPizza INTEGER NOT NULL, idIngredient INTEGER NOT NULL, CONSTRAINT PizzaIngredientsAssociation_pk PRIMARY KEY (idPizza, idIngredient), FOREIGN KEY (idIngredient) REFERENCES Ingredient(id), FOREIGN KEY (idPizza) REFERENCES pizza(id))") + void createAssociationTable(); + + @SqlUpdate("DROP TABLE IF EXISTS pizza") + void dropPizzaTable(); + + @SqlUpdate("DROP TABLE IF EXISTS PizzaIngredientsAssociation") + void dropAssociationTable(); + + @Transaction + default void createTableAndIngredientAssociation() { + createPizzaTable(); + createAssociationTable(); + } + + @Transaction + default void dropTableAndIngredientAssociation() { + dropAssociationTable(); + dropPizzaTable(); + } + + @Transaction + default void removePizza(long id) { + removePizzaFromAssociationTable(id); + removePizza(id); + } + + @SqlUpdate("INSERT INTO pizza (name) VALUES (:name)") + @GetGeneratedKeys + long insert(String name); + + @SqlUpdate("INSERT INTO PizzaIngredientsAssociation VALUES (:pizzaId, :ingredientId)") + long addIngredient(long pizzaId, long ingredientId); + + @SqlQuery("SELECT * FROM pizza") + @RegisterBeanMapper(Pizza.class) + List<Pizza> getAll(); + + @SqlQuery("SELECT ingredients.* FROM ingredients JOIN PizzaIngredientsAssociation ON idIngredient = ingredients.id WHERE pizzaId = :id") + @RegisterBeanMapper(Pizza.class) + List<Ingredient> getIngredients(long id); + + @SqlQuery("SELECT * FROM pizza WHERE id = :id") + @RegisterBeanMapper(Pizza.class) + Pizza findById(long id); + + @SqlQuery("SELECT * FROM ingredients WHERE name = :name") + @RegisterBeanMapper(Pizza.class) + Pizza findByName(String name); + + @SqlUpdate("DELETE FROM pizza WHERE id = :id") + void removePizzaFromTable(long id); + + @SqlUpdate("DELETE FROM PizzaIngredientsAssociation WHERE idPizza = :id") + void removePizzaFromAssociationTable(long id); + + @SqlUpdate("DELETE FROM PizzaIngredientsAssociation WHERE idPizza = :idPizza AND idIngredient = :idIngredient") + void removeIngredient(long idPizza, long idIngredient); +} diff --git a/src/main/java/fr/ulille/iut/pizzaland/dto/IngredientCreateDto.java b/src/main/java/fr/ulille/iut/pizzaland/dto/IngredientCreateDto.java new file mode 100755 index 0000000..0056f9f --- /dev/null +++ b/src/main/java/fr/ulille/iut/pizzaland/dto/IngredientCreateDto.java @@ -0,0 +1,16 @@ +package fr.ulille.iut.pizzaland.dto; + +public class IngredientCreateDto { + private String name; + + public IngredientCreateDto() {} + + public void setName(String name) { + this.name = name; + } + + public String getName() { + return name; + } + +} diff --git a/src/main/java/fr/ulille/iut/pizzaland/dto/IngredientDto.java b/src/main/java/fr/ulille/iut/pizzaland/dto/IngredientDto.java old mode 100644 new mode 100755 index 1a9360b..893401f --- a/src/main/java/fr/ulille/iut/pizzaland/dto/IngredientDto.java +++ b/src/main/java/fr/ulille/iut/pizzaland/dto/IngredientDto.java @@ -2,7 +2,25 @@ package fr.ulille.iut.pizzaland.dto; public class IngredientDto { - public IngredientDto() { - + private long id; + private String name; + + public IngredientDto() {} + + public long getId() { + return id; } + + public void setId(long id) { + this.id = id; + } + + public void setName(String name) { + this.name = name; + } + + public String getName() { + return name; + } + } diff --git a/src/main/java/fr/ulille/iut/pizzaland/dto/PizzaCreateDto.java b/src/main/java/fr/ulille/iut/pizzaland/dto/PizzaCreateDto.java new file mode 100755 index 0000000..6a6abfb --- /dev/null +++ b/src/main/java/fr/ulille/iut/pizzaland/dto/PizzaCreateDto.java @@ -0,0 +1,28 @@ +package fr.ulille.iut.pizzaland.dto; + +import java.util.List; +import fr.ulille.iut.pizzaland.beans.Ingredient; + +public class PizzaCreateDto { + private String name; + private List<Ingredient> ingredients; + + public PizzaCreateDto() {} + + public void setName(String name) { + this.name = name; + } + + public String getName() { + return name; + } + + public void setIngredients(List<Ingredient> ingredients) { + this.ingredients = ingredients; + } + + public List<Ingredient> getIngredients() { + return this.ingredients; + } + +} diff --git a/src/main/java/fr/ulille/iut/pizzaland/dto/PizzaDto.java b/src/main/java/fr/ulille/iut/pizzaland/dto/PizzaDto.java new file mode 100755 index 0000000..2b368bb --- /dev/null +++ b/src/main/java/fr/ulille/iut/pizzaland/dto/PizzaDto.java @@ -0,0 +1,43 @@ +package fr.ulille.iut.pizzaland.dto; + +import java.util.List; +import fr.ulille.iut.pizzaland.beans.Ingredient; + +public class PizzaDto { + + private long id; + private String name; + private List<Ingredient> ingredients; + + public PizzaDto() { + } + + public long getId() { + return id; + } + + public void setId(long id) { + this.id = id; + } + + public void setName(String name) { + this.name = name; + } + + public String getName() { + return name; + } + + public List<Ingredient> getIngredients() { + return this.ingredients; + } + + public void setIngredients(List<Ingredient> ingredients) { + this.ingredients = ingredients; + } + + public void addIngredients(Ingredient ingredient) { + this.ingredients.add(ingredient); + } + +} diff --git a/src/main/java/fr/ulille/iut/pizzaland/resources/IngredientResource.java b/src/main/java/fr/ulille/iut/pizzaland/resources/IngredientResource.java old mode 100644 new mode 100755 index bc19082..d90a771 --- a/src/main/java/fr/ulille/iut/pizzaland/resources/IngredientResource.java +++ b/src/main/java/fr/ulille/iut/pizzaland/resources/IngredientResource.java @@ -1,19 +1,30 @@ package fr.ulille.iut.pizzaland.resources; +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; 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.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 javax.ws.rs.Consumes; +import javax.ws.rs.DELETE; +import javax.ws.rs.FormParam; -import fr.ulille.iut.pizzaland.dto.IngredientDto; -@Path("/ingredients") + + +/*@Path("/ingredients") public class IngredientResource { private static final Logger LOGGER = Logger.getLogger(IngredientResource.class.getName()); @@ -29,4 +40,118 @@ public class IngredientResource { return null; } +}*/ + + + + + +@Path("ingredients") +public class IngredientResource { + private IngredientDao ingredients; + private static final Logger LOGGER = Logger.getLogger(IngredientResource.class.getName()); + + @Context + public UriInfo uriInfo; + + + public IngredientResource() { + ingredients = BDDFactory.buildDao(IngredientDao.class); + ingredients.createTable(); + } + + @GET + public List<IngredientDto> getAll() { + LOGGER.info("IngredientResource:getAll"); + + List<IngredientDto> l = ingredients.getAll().stream().map(Ingredient::toDto).collect(Collectors.toList()); + return l; + } + + @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); + } + } + + @POST +public Response createIngredient(IngredientCreateDto ingredientCreateDto) { + Ingredient existing = ingredients.findByName(ingredientCreateDto.getName()); + if ( existing != null ) { + throw new WebApplicationException(Response.Status.CONFLICT); + } + + try { + Ingredient ingredient = Ingredient.fromIngredientCreateDto(ingredientCreateDto); + long id = ingredients.insert(ingredient.getName()); + ingredient.setId(id); + IngredientDto ingredientDto = Ingredient.toDto(ingredient); + + URI uri = uriInfo.getAbsolutePathBuilder().path("" + id).build(); + + 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 ( ingredients.findById(id) == null ) { + throw new WebApplicationException(Response.Status.NOT_FOUND); + } + + ingredients.remove(id); + + return Response.status(Response.Status.ACCEPTED).build(); +} + +@GET +@Path("{id}/name") +public String getIngredientName(@PathParam("id") long id) { + Ingredient ingredient = ingredients.findById(id); + if ( ingredient == null ) { + throw new WebApplicationException(Response.Status.NOT_FOUND); + } + + return ingredient.getName(); +} + +@POST +@Consumes("application/x-www-form-urlencoded") +public Response createIngredient(@FormParam("name") String name) { + Ingredient existing = ingredients.findByName(name); + if ( existing != null ) { + throw new WebApplicationException(Response.Status.CONFLICT); + } + + try { + Ingredient ingredient = new Ingredient(); + ingredient.setName(name); + + long id = ingredients.insert(ingredient.getName()); + ingredient.setId(id); + IngredientDto ingredientDto = Ingredient.toDto(ingredient); + + URI uri = uriInfo.getAbsolutePathBuilder().path("" + id).build(); + + return Response.created(uri).entity(ingredientDto).build(); + } + catch ( Exception e ) { + e.printStackTrace(); + throw new WebApplicationException(Response.Status.NOT_ACCEPTABLE); + } +} + } diff --git a/src/main/java/fr/ulille/iut/pizzaland/resources/PizzaResource.java b/src/main/java/fr/ulille/iut/pizzaland/resources/PizzaResource.java new file mode 100755 index 0000000..4f11894 --- /dev/null +++ b/src/main/java/fr/ulille/iut/pizzaland/resources/PizzaResource.java @@ -0,0 +1,86 @@ +package fr.ulille.iut.pizzaland.resources; + +import fr.ulille.iut.pizzaland.BDDFactory; +import fr.ulille.iut.pizzaland.beans.Pizza; +import fr.ulille.iut.pizzaland.dao.PizzaDao; +import fr.ulille.iut.pizzaland.dto.PizzaCreateDto; +import fr.ulille.iut.pizzaland.dto.PizzaDto; +import java.net.URI; +import java.util.List; +import java.util.logging.Logger; +import java.util.stream.Collectors; +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 javax.ws.rs.Consumes; +import javax.ws.rs.DELETE; +import javax.ws.rs.FormParam; + + + + +@Path("pizzas") +public class PizzaResource { + private static final Logger LOGGER = Logger.getLogger(PizzaResource.class.getName()); + private PizzaDao pizzas; + + + @Context + public UriInfo uriInfo; + + public PizzaResource() { + pizzas = BDDFactory.buildDao(PizzaDao.class); + pizzas.createTableAndIngredientAssociation(); + } + + + @GET + public List<PizzaDto> getAll() { + LOGGER.info("PizzaResource:getAll"); + + return null; + } + + @GET + @Path("{id}") + public PizzaDto getOnePizza(@PathParam("id") long id) { + LOGGER.info("getOnePizza(" + id + ")"); + try { + Pizza pizza = pizzas.findById(id); + return Pizza.toDto(pizza); + } + catch ( Exception e ) { + // Cette exception générera une réponse avec une erreur 404 + throw new WebApplicationException(Response.Status.NOT_FOUND); + } + } + + @POST + public Response createPizza(PizzaCreateDto pizzaCreateDto) { + Pizza existing = pizzas.findByName(pizzaCreateDto.getName()); + if ( existing != null ) { + throw new WebApplicationException(Response.Status.CONFLICT); + } + + try { + Pizza pizza = Pizza.fromPizzaCreateDto(pizzaCreateDto); + long id = pizzas.insert(pizza.getName()); + pizza.setId(id); + PizzaDto pizzaDto = Pizza.toDto(pizza); + + URI uri = uriInfo.getAbsolutePathBuilder().path("" + id).build(); + + return Response.created(uri).entity(pizzaDto).build(); + } + catch ( Exception e ) { + e.printStackTrace(); + throw new WebApplicationException(Response.Status.NOT_ACCEPTABLE); + } + } + +} \ No newline at end of file diff --git a/src/main/resources/ingredients.json b/src/main/resources/ingredients.json old mode 100644 new mode 100755 diff --git a/src/main/resources/logback.xml b/src/main/resources/logback.xml old mode 100644 new mode 100755 diff --git a/src/main/webapp/WEB-INF/.gitkeep b/src/main/webapp/WEB-INF/.gitkeep old mode 100644 new mode 100755 diff --git a/src/test/java/fr/ulille/iut/pizzaland/IngredientResourceTest.java b/src/test/java/fr/ulille/iut/pizzaland/IngredientResourceTest.java old mode 100644 new mode 100755 index 2c8b1be..e913397 --- a/src/test/java/fr/ulille/iut/pizzaland/IngredientResourceTest.java +++ b/src/test/java/fr/ulille/iut/pizzaland/IngredientResourceTest.java @@ -1,69 +1,177 @@ 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.IngredientDto; - +import fr.ulille.iut.pizzaland.dto.IngredientCreateDto; +import javax.ws.rs.core.Application; +import javax.ws.rs.core.Response; 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 javax.ws.rs.client.Entity; -import javax.ws.rs.core.GenericType; -import javax.ws.rs.core.Application; -import javax.ws.rs.core.Response; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Form; +import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertEquals; -import java.util.List; -import java.util.logging.Logger; /* * JerseyTest facilite l'écriture des tests en donnant accès aux * méthodes de l'interface javax.ws.rs.client.Client. * la méthode configure() permet de démarrer la ressource à tester */ + public class IngredientResourceTest extends JerseyTest { - private static final Logger LOGGER = Logger.getLogger(IngredientResourceTest.class.getName()); - + private IngredientDao dao; + @Override protected Application configure() { - return new ApiV1(); + BDDFactory.setJdbiForTests(); + return new ApiV1(); } - // Les méthodes setEnvUp() et tearEnvDown() serviront à terme à initialiser la base de données - // et les DAO - - // 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 + public void testGetExistingIngredient() { + + Ingredient ingredient = new Ingredient(); + ingredient.setName("ehfehehehehfheehehfheh"); + + 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 testGetEmptyList() { - // La méthode target() permet de préparer une requête sur une URI. - // La classe Response permet de traiter la réponse HTTP reçue. - Response response = target("/ingredients").request().get(); + public void testGetNotExistingIngredient() { + Response response = target("/ingredients/125").request().get(); + assertEquals(Response.Status.NOT_FOUND.getStatusCode(),response.getStatus()); + } - // On vérifie le code de la réponse (200 = OK) - assertEquals(Response.Status.OK.getStatusCode(), response.getStatus()); + @Test +public void testCreateIngredient() { + IngredientCreateDto ingredientCreateDto = new IngredientCreateDto(); + ingredientCreateDto.setName("ehfehehehehfheehehfheh"); - - // On vérifie la valeur retournée (liste vide) - // L'entité (readEntity() correspond au corps de la réponse HTTP. - // La classe javax.ws.rs.core.GenericType<T> permet de définir le type - // de la réponse lue quand on a un type complexe (typiquement une liste). - List<IngredientDto> ingredients; - ingredients = response.readEntity(new GenericType<List<IngredientDto>>(){}); + 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("ehfehehehehfheehehfheh"); + 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("ehfehehehehfheehehfheh"); + 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("ehfehehehehfheehehfheh"); + long id = dao.insert(ingredient.getName()); + + Response response = target("ingredients/" + id + "/name").request().get(); + + assertEquals(Response.Status.OK.getStatusCode(), response.getStatus()); + + assertEquals("ehfehehehehfheehehfheh", response.readEntity(String.class)); +} + +@Test +public void testGetNotExistingIngredientName() { + Response response = target("ingredients/125/name").request().get(); + + assertEquals(Response.Status.NOT_FOUND.getStatusCode(), response.getStatus()); +} + +@Test +public void testCreateWithForm() { + Form form = new Form(); + form.param("name", "chorizo"); + + Entity<Form> formEntity = Entity.entity(form, MediaType.APPLICATION_FORM_URLENCODED_TYPE); + Response response = target("ingredients").request().post(formEntity); + + assertEquals(Response.Status.CREATED.getStatusCode(), response.getStatus()); + String location = response.getHeaderString("Location"); + long id = Integer.parseInt(location.substring(location.lastIndexOf('/') + 1)); + Ingredient result = dao.findById(id); + + assertNotNull(result); +} - assertEquals(0, ingredients.size()); - } } diff --git a/src/test/java/fr/ulille/iut/pizzaland/PizzaResourceTest.java b/src/test/java/fr/ulille/iut/pizzaland/PizzaResourceTest.java new file mode 100755 index 0000000..4658e5b --- /dev/null +++ b/src/test/java/fr/ulille/iut/pizzaland/PizzaResourceTest.java @@ -0,0 +1,128 @@ +package fr.ulille.iut.pizzaland; + +import fr.ulille.iut.pizzaland.ApiV1; +import fr.ulille.iut.pizzaland.beans.Ingredient; +import fr.ulille.iut.pizzaland.beans.Pizza; +import fr.ulille.iut.pizzaland.dao.PizzaDao; +import fr.ulille.iut.pizzaland.dao.IngredientDao; +import fr.ulille.iut.pizzaland.dto.PizzaDto; +import fr.ulille.iut.pizzaland.dto.PizzaCreateDto; +import javax.ws.rs.core.Application; +import javax.ws.rs.core.Response; +import org.glassfish.jersey.test.JerseyTest; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import javax.ws.rs.client.Entity; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Form; + +import static org.junit.Assert.assertNotNull; + +import java.util.ArrayList; +import java.util.List; + +import static org.junit.Assert.assertEquals; + + +/* + * JerseyTest facilite l'écriture des tests en donnant accès aux + * méthodes de l'interface javax.ws.rs.client.Client. + * la méthode configure() permet de démarrer la ressource à tester + */ + +public class PizzaResourceTest extends JerseyTest { + private PizzaDao dao; + private IngredientDao daoIngredient; + + @Override + protected Application configure() { + BDDFactory.setJdbiForTests(); + return new ApiV1(); + } + + @Before + public void setEnvUp() { + daoIngredient = BDDFactory.buildDao(IngredientDao.class); + daoIngredient.createTable(); + dao = BDDFactory.buildDao(PizzaDao.class); + dao.createTableAndIngredientAssociation(); + } + + @After + public void tearEnvDown() throws Exception { + dao.dropTableAndIngredientAssociation(); + daoIngredient.dropTable(); + } + + @Test + public void testGetExistingPizza() { + + Pizza pizza = new Pizza(); + pizza.setName("Pizza ehfehhehfheehehfheh"); + + long id = dao.insert(pizza.getName()); + pizza.setId(id); + + Response response = target("/pizzas/" + id).request().get(); + + assertEquals(Response.Status.OK.getStatusCode(), response.getStatus()); + + Pizza result = Pizza.fromDto(response.readEntity(PizzaDto.class)); + System.out.println(result.getName()); + assertEquals(pizza, result); + } + + @Test + public void testGetNotExistingPizza() { + Response response = target("/pizzas/125").request().get(); + assertEquals(Response.Status.NOT_FOUND.getStatusCode(),response.getStatus()); + } + + public void testCreatePizza() { + PizzaCreateDto pizzaCreateDto = new PizzaCreateDto(); + pizzaCreateDto.setName("ehfehehehehfheehehfheh"); + + Response response = target("/pizzas") + .request() + .post(Entity.json(pizzaCreateDto)); + + // On vérifie le code de status à 201 + assertEquals(Response.Status.CREATED.getStatusCode(), response.getStatus()); + + PizzaDto returnedEntity = response.readEntity(PizzaDto.class); + + // On vérifie que le champ d'entête Location correspond à + // l'URI de la nouvelle entité + assertEquals(target("/pizzas/" + + returnedEntity.getId()).getUri(), response.getLocation()); + + // On vérifie que le nom correspond + assertEquals(returnedEntity.getName(), pizzaCreateDto.getName()); + } + + @Test + public void testCreateSamePizza() { + PizzaCreateDto pizzaCreateDto = new PizzaCreateDto(); + pizzaCreateDto.setName("ehfehehehehfheehehfheh"); + dao.insert(pizzaCreateDto.getName()); + + Response response = target("/pizzas") + .request() + .post(Entity.json(pizzaCreateDto)); + + assertEquals(Response.Status.CONFLICT.getStatusCode(), response.getStatus()); + } + + @Test + public void testCreatePizzaWithoutName() { + PizzaCreateDto pizzaCreateDto = new PizzaCreateDto(); + + Response response = target("/pizzas") + .request() + .post(Entity.json(pizzaCreateDto)); + + assertEquals(Response.Status.NOT_ACCEPTABLE.getStatusCode(), response.getStatus()); + } + +} \ No newline at end of file diff --git a/src/test/resources/logback-test.xml b/src/test/resources/logback-test.xml old mode 100644 new mode 100755 -- GitLab