Skip to content
Snippets Groups Projects
Commit b255b441 authored by Thomas Shipman's avatar Thomas Shipman
Browse files

Fin

parent b3f5ebe9
No related branches found
No related tags found
No related merge requests found
Showing
with 933 additions and 13 deletions
...@@ -2,8 +2,16 @@ package fr.ulille.iut.pizzaland; ...@@ -2,8 +2,16 @@ package fr.ulille.iut.pizzaland;
import org.glassfish.jersey.server.ResourceConfig; 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 java.util.logging.Logger;
import javax.json.bind.Jsonb;
import javax.json.bind.JsonbBuilder;
import javax.ws.rs.ApplicationPath; import javax.ws.rs.ApplicationPath;
@ApplicationPath("api/v1/") @ApplicationPath("api/v1/")
...@@ -12,5 +20,26 @@ public class ApiV1 extends ResourceConfig { ...@@ -12,5 +20,26 @@ public class ApiV1 extends ResourceConfig {
public ApiV1() { public ApiV1() {
packages("fr.ulille.iut.pizzaland"); 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; ...@@ -10,15 +10,12 @@ import org.jdbi.v3.sqlobject.SqlObjectPlugin;
public class BDDFactory { public class BDDFactory {
private static Jdbi jdbi = null; 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() { public static Jdbi getJdbi() {
if ( jdbi == null ) { 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 SQLitePlugin())
.installPlugin(new SqlObjectPlugin()); .installPlugin(new SqlObjectPlugin());
} }
...@@ -27,7 +24,9 @@ public class BDDFactory { ...@@ -27,7 +24,9 @@ public class BDDFactory {
public static void setJdbiForTests() { public static void setJdbiForTests() {
if ( jdbi == null ) { 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 SQLitePlugin())
.installPlugin(new SqlObjectPlugin()); .installPlugin(new SqlObjectPlugin());
} }
......
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;
}
}
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;
}
}
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);
}
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);
}
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;
}
}
...@@ -2,7 +2,26 @@ package fr.ulille.iut.pizzaland.dto; ...@@ -2,7 +2,26 @@ package fr.ulille.iut.pizzaland.dto;
public class IngredientDto { public class IngredientDto {
long id;
String name;
public IngredientDto() { 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;
}
} }
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;
}
}
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;
}
}
...@@ -2,31 +2,116 @@ package fr.ulille.iut.pizzaland.resources; ...@@ -2,31 +2,116 @@ package fr.ulille.iut.pizzaland.resources;
import java.net.URI; 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.DELETE;
import javax.ws.rs.GET; import javax.ws.rs.GET;
import javax.ws.rs.POST;
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.IngredientCreateDto;
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 dao;
@Context @Context
public UriInfo uriInfo; public UriInfo uriInfo;
public IngredientResource() { 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 @GET
public List<IngredientDto> getAll() { public List<IngredientDto> getAll() {
LOGGER.info("IngredientResource: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.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);
}
}
}
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.IngredientCreateDto;
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 +29,12 @@ import java.util.logging.Logger; ...@@ -26,9 +29,12 @@ 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 +44,14 @@ public class IngredientResourceTest extends JerseyTest { ...@@ -38,12 +44,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 +74,122 @@ public class IngredientResourceTest extends JerseyTest { ...@@ -66,4 +74,122 @@ public class IngredientResourceTest extends JerseyTest {
assertEquals(0, ingredients.size()); 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());
}
} }
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());
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment