From b255b441f96c7decea9304cab295be86c78d6959 Mon Sep 17 00:00:00 2001
From: Thomas Shipman <thomas.shipman.etu@univ-lille.fr>
Date: Fri, 6 Mar 2020 10:20:40 +0100
Subject: [PATCH] Fin
---
.../java/fr/ulille/iut/pizzaland/ApiV1.java | 29 ++++
.../fr/ulille/iut/pizzaland/BDDFactory.java | 17 +-
.../iut/pizzaland/beans/Ingredient.java | 93 +++++++++++
.../fr/ulille/iut/pizzaland/beans/Pizza.java | 99 ++++++++++++
.../iut/pizzaland/dao/IngredientDAO.java | 39 +++++
.../fr/ulille/iut/pizzaland/dao/PizzaDAO.java | 60 +++++++
.../pizzaland/dto/IngredientCreateDto.java | 20 +++
.../iut/pizzaland/dto/IngredientDto.java | 19 +++
.../iut/pizzaland/dto/PizzaCreateDto.java | 36 +++++
.../fr/ulille/iut/pizzaland/dto/PizzaDto.java | 42 +++++
.../resources/IngredientResource.java | 89 ++++++++++-
.../pizzaland/resources/PizzaResource.java | 124 +++++++++++++++
.../iut/pizzaland/IngredientResourceTest.java | 130 ++++++++++++++-
.../iut/pizzaland/PizzaResourceTest.java | 149 ++++++++++++++++++
14 files changed, 933 insertions(+), 13 deletions(-)
create mode 100644 src/main/java/fr/ulille/iut/pizzaland/beans/Ingredient.java
create mode 100644 src/main/java/fr/ulille/iut/pizzaland/beans/Pizza.java
create mode 100644 src/main/java/fr/ulille/iut/pizzaland/dao/IngredientDAO.java
create mode 100644 src/main/java/fr/ulille/iut/pizzaland/dao/PizzaDAO.java
create mode 100644 src/main/java/fr/ulille/iut/pizzaland/dto/IngredientCreateDto.java
create mode 100644 src/main/java/fr/ulille/iut/pizzaland/dto/PizzaCreateDto.java
create mode 100644 src/main/java/fr/ulille/iut/pizzaland/dto/PizzaDto.java
create mode 100644 src/main/java/fr/ulille/iut/pizzaland/resources/PizzaResource.java
create mode 100644 src/test/java/fr/ulille/iut/pizzaland/PizzaResourceTest.java
diff --git a/src/main/java/fr/ulille/iut/pizzaland/ApiV1.java b/src/main/java/fr/ulille/iut/pizzaland/ApiV1.java
index b565c83..f40e989 100644
--- a/src/main/java/fr/ulille/iut/pizzaland/ApiV1.java
+++ b/src/main/java/fr/ulille/iut/pizzaland/ApiV1.java
@@ -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);
+ }
+ }
+
}
}
diff --git a/src/main/java/fr/ulille/iut/pizzaland/BDDFactory.java b/src/main/java/fr/ulille/iut/pizzaland/BDDFactory.java
index 31845d1..9f601c1 100644
--- a/src/main/java/fr/ulille/iut/pizzaland/BDDFactory.java
+++ b/src/main/java/fr/ulille/iut/pizzaland/BDDFactory.java
@@ -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
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 100644
index 0000000..4f5831a
--- /dev/null
+++ b/src/main/java/fr/ulille/iut/pizzaland/beans/Ingredient.java
@@ -0,0 +1,93 @@
+package fr.ulille.iut.pizzaland.beans;
+
+import fr.ulille.iut.pizzaland.dto.IngredientCreateDto;
+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 this.id;
+ }
+
+ public void setName(String name) {
+ this.name=name;
+ }
+
+ public String getName() {
+ return this.name;
+ }
+
+ public static Ingredient fromDto(IngredientDto dto) {
+ Ingredient ingredient = new Ingredient();
+ ingredient.setId(dto.getId());
+ ingredient.setName(dto.getName());
+
+ return ingredient;
+ }
+
+ public static IngredientDto toDto(Ingredient ingredient) {
+ IngredientDto dto = new IngredientDto();
+ dto.setId(ingredient.getId());
+ dto.setName(ingredient.getName());
+
+ return dto;
+ }
+
+ @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 100644
index 0000000..215cd4f
--- /dev/null
+++ b/src/main/java/fr/ulille/iut/pizzaland/beans/Pizza.java
@@ -0,0 +1,99 @@
+package fr.ulille.iut.pizzaland.beans;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import fr.ulille.iut.pizzaland.dto.PizzaCreateDto;
+import fr.ulille.iut.pizzaland.dto.PizzaDto;
+
+public class Pizza {
+
+ private long id;
+ private String name;
+ private List<Ingredient> ingredients = new ArrayList<Ingredient>();
+
+ public Pizza(long id, String name) {
+ this.id = id;
+ this.name = name;
+ }
+
+ public Pizza() {
+
+ }
+
+ 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;
+ }
+
+ public List<Ingredient> getIngredients(){
+ return this.ingredients;
+ }
+
+ public void addIngredient(Ingredient ingredient) {
+ this.ingredients.add(ingredient);
+ }
+
+ private void setIngredient(List<Ingredient> ingredients) {
+ this.ingredients = ingredients;
+ }
+
+ public static PizzaDto toDto(Pizza pizza) {
+ PizzaDto dto = new PizzaDto();
+ dto.setId(pizza.getId());
+ dto.setName(pizza.getName());
+
+ return dto;
+ }
+
+ public static Pizza fromDto(PizzaDto dto) {
+ Pizza pizza = new Pizza();
+ pizza.setId(dto.getId());
+ pizza.setName(dto.getName());
+
+ return pizza;
+ }
+
+ @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 (name == null) {
+ if (other.name != null)
+ return false;
+ } else if (!name.equals(other.name))
+ return false;
+ return true;
+ }
+
+ @Override
+ public String toString() {
+ return "Pizza [id=" + id + ", name=" + name + "]";
+ }
+
+ public static Pizza fromPizzaCreateDto(PizzaCreateDto dtoCreate) {
+ Pizza pizza = new Pizza();
+ pizza.setName(dtoCreate.getName());
+ pizza.setIngredient(dtoCreate.getIngredients());
+ return pizza;
+ }
+}
+
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 100644
index 0000000..fde2edd
--- /dev/null
+++ b/src/main/java/fr/ulille/iut/pizzaland/dao/IngredientDAO.java
@@ -0,0 +1,39 @@
+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 100644
index 0000000..94d5357
--- /dev/null
+++ b/src/main/java/fr/ulille/iut/pizzaland/dao/PizzaDAO.java
@@ -0,0 +1,60 @@
+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 org.jdbi.v3.sqlobject.transaction.Transaction;
+
+import fr.ulille.iut.pizzaland.beans.Ingredient;
+import fr.ulille.iut.pizzaland.beans.Pizza;
+
+public interface PizzaDAO {
+
+ @SqlUpdate("CREATE TABLE IF NOT EXISTS pizzas (id INTEGER PRIMARY KEY, name VARCHAR UNIQUE NOT NULL)")
+ void createTablePizza();
+
+ @SqlUpdate("CREATE TABLE IF NOT EXISTS pizzasIngredients (idPizza INTEGER, idIngredient INTEGER, CONSTRAINT pk_pizzasIngredients PRIMARY KEY (idPizza,idIngredient))")
+ void createTableIngredients();
+
+ @Transaction
+ default void createTablePizzaIngredients() {
+ createTablePizza();
+ createTableIngredients();
+
+ }
+
+ @SqlUpdate("DROP TABLE IF EXISTS pizzas")
+ void dropTable();
+
+ @SqlUpdate("INSERT INTO pizzas (name) VALUES (:name)")
+ @GetGeneratedKeys
+ long insert(String name);
+
+ @SqlUpdate("INSERT INTO pizzasIngredients VALUES (:idPizza,:idIngredient)")
+ void insertIngredient(long idPizza, long idIngredient);
+
+ @SqlQuery("SELECT * FROM pizzas")
+ @RegisterBeanMapper(Pizza.class)
+ List<Pizza> getAll();
+
+ @SqlQuery("SELECT * FROM pizzasIngredients WHERE idPizza = :idPizza")
+ List<Ingredient> getIngredient(long idPizza);
+
+ @SqlQuery("SELECT * FROM pizzas WHERE id = :id")
+ @RegisterBeanMapper(Pizza.class)
+ Pizza findById(long id);
+
+ @SqlQuery("SELECT * FROM pizzas WHERE name = :name")
+ @RegisterBeanMapper(Pizza.class)
+ Pizza findByName(String name);
+
+ @SqlUpdate("DELETE FROM pizzasIngredients WHERE idPizza = :idPizza")
+ void removeFromPizzasIngredients(long idPizza);
+
+ @SqlUpdate("DELETE FROM pizzas WHERE idPizza = :idPizza")
+ void removeFromPizzas(long idPizza);
+
+}
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 100644
index 0000000..65065bb
--- /dev/null
+++ b/src/main/java/fr/ulille/iut/pizzaland/dto/IngredientCreateDto.java
@@ -0,0 +1,20 @@
+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
index 1a9360b..198bfd7 100644
--- a/src/main/java/fr/ulille/iut/pizzaland/dto/IngredientDto.java
+++ b/src/main/java/fr/ulille/iut/pizzaland/dto/IngredientDto.java
@@ -1,8 +1,27 @@
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;
+ }
}
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 100644
index 0000000..4a3a7cc
--- /dev/null
+++ b/src/main/java/fr/ulille/iut/pizzaland/dto/PizzaCreateDto.java
@@ -0,0 +1,36 @@
+package fr.ulille.iut.pizzaland.dto;
+
+import java.util.ArrayList;
+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 this.name;
+ }
+
+ public void addIngredient(Ingredient ingredient) {
+ if(ingredients == null) {
+ this.ingredients= new ArrayList<Ingredient>();
+ }
+ ingredients.add(ingredient);
+ }
+
+ 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 100644
index 0000000..141627c
--- /dev/null
+++ b/src/main/java/fr/ulille/iut/pizzaland/dto/PizzaDto.java
@@ -0,0 +1,42 @@
+package fr.ulille.iut.pizzaland.dto;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import fr.ulille.iut.pizzaland.beans.Ingredient;
+
+public class PizzaDto {
+
+ long id;
+ String name;
+ List<Ingredient> ingredients = new ArrayList<Ingredient>();
+
+ public PizzaDto() {
+
+ }
+
+ 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;
+ }
+
+ public void addIngredient(Ingredient ingredient) {
+ ingredients.add(ingredient);
+ }
+
+ public List<Ingredient> getIngredients(){
+ return this.ingredients;
+ }
+
+}
diff --git a/src/main/java/fr/ulille/iut/pizzaland/resources/IngredientResource.java b/src/main/java/fr/ulille/iut/pizzaland/resources/IngredientResource.java
index bc19082..2fd185a 100644
--- a/src/main/java/fr/ulille/iut/pizzaland/resources/IngredientResource.java
+++ b/src/main/java/fr/ulille/iut/pizzaland/resources/IngredientResource.java
@@ -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();
+ }
+
+
}
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 100644
index 0000000..8a90f71
--- /dev/null
+++ b/src/main/java/fr/ulille/iut/pizzaland/resources/PizzaResource.java
@@ -0,0 +1,124 @@
+package fr.ulille.iut.pizzaland.resources;
+
+import java.net.URI;
+import java.util.List;
+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.Response.Status;
+import javax.ws.rs.core.UriInfo;
+
+import fr.ulille.iut.pizzaland.BDDFactory;
+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.dto.IngredientDto;
+import fr.ulille.iut.pizzaland.dto.PizzaCreateDto;
+import fr.ulille.iut.pizzaland.dto.PizzaDto;
+
+@Path("/pizzas")
+public class PizzaResource {
+ private static final Logger LOGGER = Logger.getLogger(IngredientResource.class.getName());
+ private PizzaDAO dao;
+
+ @Context
+ public UriInfo uriInfo;
+
+ public PizzaResource() {
+ dao = BDDFactory.buildDao(PizzaDAO.class);
+ dao.createTablePizzaIngredients();
+ }
+
+ @GET
+ public List<PizzaDto> getAll() {
+
+ LOGGER.info("IngredientResource:getAll");
+
+ List<PizzaDto> l = dao.getAll().stream().map(Pizza::toDto).collect(Collectors.toList());
+ return l;
+ }
+
+ @GET
+ @Path("{id}/name")
+ public String getPizzaName(@PathParam("id") long id) {
+ LOGGER.info("getPizzaName("+id+")");
+ Pizza pizza = dao.findById(id);
+ if(pizza == null) {
+ throw new WebApplicationException(Response.Status.NOT_FOUND);
+ }
+ return pizza.getName();
+ }
+
+ @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) {
+ throw new WebApplicationException(Response.Status.NOT_FOUND);
+ } */
+ Pizza pizza = dao.findById(id);
+ if(pizza == null) {
+ throw new WebApplicationException(Response.Status.NOT_FOUND);
+ }
+ return Pizza.toDto(pizza);
+ }
+
+ @GET
+ @Path("{id}/ingredients)")
+ public List<Ingredient> getIngredients(@PathParam("id") long id){
+ LOGGER.info("getIngredients(" + id + ")");
+ try {
+ return dao.getIngredient(id);
+ } catch(Exception e) {
+ throw new WebApplicationException(Response.Status.NOT_FOUND);
+ }
+ }
+
+ @DELETE
+ @Path("{id}")
+ public Response deletePizza(@PathParam("id") long id){
+ LOGGER.info("deletePizza("+id+")");
+ if(dao.findById(id) == null) {
+ throw new WebApplicationException(Response.Status.NOT_FOUND);
+ }
+ dao.removeFromPizzas(id);
+
+ return Response.status(Status.ACCEPTED).build();
+ }
+
+ @POST
+ @Path("{id}")
+ public Response createPizza(PizzaCreateDto dtoCreate) {
+ Pizza existing = dao.findByName(dtoCreate.getName());
+ if ( existing != null ) {
+ throw new WebApplicationException(Response.Status.CONFLICT);
+ }
+
+ try {
+ Pizza pizza = Pizza.fromPizzaCreateDto(dtoCreate);
+ long id = dao.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);
+ }
+ }
+
+}
diff --git a/src/test/java/fr/ulille/iut/pizzaland/IngredientResourceTest.java b/src/test/java/fr/ulille/iut/pizzaland/IngredientResourceTest.java
index 2c8b1be..7dc67eb 100644
--- a/src/test/java/fr/ulille/iut/pizzaland/IngredientResourceTest.java
+++ b/src/test/java/fr/ulille/iut/pizzaland/IngredientResourceTest.java
@@ -1,6 +1,9 @@
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());
+ }
+
+
}
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 100644
index 0000000..fb01ea8
--- /dev/null
+++ b/src/test/java/fr/ulille/iut/pizzaland/PizzaResourceTest.java
@@ -0,0 +1,149 @@
+package fr.ulille.iut.pizzaland;
+
+import static org.junit.Assert.assertEquals;
+
+import java.util.List;
+
+import javax.ws.rs.client.Entity;
+import javax.ws.rs.core.Application;
+import javax.ws.rs.core.GenericType;
+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 fr.ulille.iut.pizzaland.beans.Ingredient;
+import fr.ulille.iut.pizzaland.beans.Pizza;
+import fr.ulille.iut.pizzaland.dao.IngredientDAO;
+import fr.ulille.iut.pizzaland.dao.PizzaDAO;
+import fr.ulille.iut.pizzaland.dto.IngredientCreateDto;
+import fr.ulille.iut.pizzaland.dto.IngredientDto;
+import fr.ulille.iut.pizzaland.dto.PizzaCreateDto;
+import fr.ulille.iut.pizzaland.dto.PizzaDto;
+
+public class PizzaResourceTest extends JerseyTest{
+
+ private PizzaDAO dao;
+ private IngredientDAO daoIng;
+
+ @Override
+ protected Application configure() {
+ BDDFactory.setJdbiForTests();
+ return new ApiV1();
+ }
+
+ @Before
+ public void setEnvUp() {
+ dao = BDDFactory.buildDao(PizzaDAO.class);
+ dao.createTablePizzaIngredients();
+ daoIng = BDDFactory.buildDao(IngredientDAO.class);
+ daoIng.createTable();
+ }
+
+ @After
+ public void tearEnvDown() throws Exception {
+ dao.dropTable();
+ }
+
+ @Test
+ public void testGetEmptyList() {
+ Response response = target("/pizzas").request().get();
+ assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
+ List<PizzaDto> pizzas;
+ pizzas = response.readEntity(new GenericType<List<PizzaDto>>(){});
+ assertEquals(0, pizzas.size());
+ }
+
+ @Test
+ public void testGetExistingPizza() {
+ Pizza pizza = new Pizza();
+ pizza.setName("Chevre");
+
+ 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));
+ assertEquals(pizza, result);
+ }
+
+ public void testGetNotExistingPizza() {
+ Response response = target("/pizzas/12").request().get();
+ assertEquals(Response.Status.NOT_FOUND.getStatusCode(), response.getStatus());
+ }
+
+ //TODO réparer erreur
+ @Test
+ public void testGetIngredients() {
+ Pizza pizza = new Pizza();
+ pizza.setName("Chevre");
+ long idIng = daoIng.insert("Chevre");
+ long id = dao.insert(pizza.getName());
+ pizza.setId(id);
+ dao.insertIngredient(pizza.getId(), idIng);
+
+ String target = "/pizzas/" + id +"/ingredients";
+ Response response = target(target).request().get();
+
+ assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
+
+ Pizza result = Pizza.fromDto(response.readEntity(PizzaDto.class));
+ assertEquals(pizza, result);
+ }
+
+ @Test
+ public void testGetPizzaName() {
+ Pizza pizza = new Pizza();
+ pizza.setName("Chèvre");
+ long id = dao.insert(pizza.getName());
+
+ Response response = target("pizzas/" + id + "/name").request().get();
+
+ assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
+
+ assertEquals("Chèvre", response.readEntity(String.class));
+ }
+
+ //TODO réparer erreur
+ @Test
+ public void testDeleteExistingPizza() {
+ Pizza pizza = new Pizza();
+ pizza.setName("Chevre");
+
+ long id = dao.insert(pizza.getName());
+ pizza.setId(id);
+
+ Response response = target("/pizzas/" + id).request().get();
+
+ assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
+
+ response = target("/pizzas/" + id).request().delete();
+
+ assertEquals(Response.Status.ACCEPTED.getStatusCode(), response);
+
+ Pizza result = dao.findById(id);
+ assertEquals(result, null);
+ }
+
+ @Test
+ public void testCreatePizza() {
+ PizzaCreateDto pizzaCreateDto = new PizzaCreateDto();
+ pizzaCreateDto.setName("Chèvre");
+
+ Response response = target("/pizzas").request().post(Entity.json(pizzaCreateDto));
+
+ assertEquals(Response.Status.CREATED.getStatusCode(), response.getStatus());
+
+ PizzaDto returnedEntity = response.readEntity(PizzaDto.class);
+
+ assertEquals(target("/pizzas/" + returnedEntity.getId()).getUri(), response.getLocation());
+
+ assertEquals(returnedEntity.getName(), pizzaCreateDto.getName());
+ }
+
+}
--
GitLab