From f32548ef3945b325ac546a08094eacf6f3fc2da5 Mon Sep 17 00:00:00 2001 From: Yvan Peter <yvan.peter@univ-lille.fr> Date: Sun, 9 Feb 2020 18:50:25 +0100 Subject: [PATCH] =?UTF-8?q?corrections=20pour=20aller=20jusqu'=C3=A0=20la?= =?UTF-8?q?=20persistence?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 101 ++++++++++-------- .../iut/pizzaland/IngredientResourceTest.java | 2 +- 2 files changed, 60 insertions(+), 43 deletions(-) diff --git a/README.md b/README.md index 0ba9996..dd5c7e2 100644 --- a/README.md +++ b/README.md @@ -262,16 +262,18 @@ Nous allons continuer en ajoutant la possibilité de récupérer un ingrédient particulier à partir de son identifiant. Pour cela voici un premier test qui permettra de vérifier cela : + import fr.ulille.iut.pizzaland.beans.Ingredient; + @Test public void testGetExistingIngredient() { - IngredientDto ingredient = new IngredientDto(); + Ingredient ingredient = new Ingredient(); ingredient.setId(1); ingredient.setName("mozzarella"); Response response = target("/ingredients/1").request().get(); assertEquals(Response.Status.OK.getStatusCode(), response.getStatus()); - IngredienDto result = response.readEntity(IngredientDto.class); + Ingredient result = Ingredient.fromDto(response.readEntity(IngredientDto.class)); assertEquals(ingredient, result); } @@ -282,7 +284,7 @@ avec les getter/setter correspondant aux propriétés de l'object JSON. public class IngredientDto { private long id; - private String nom; + private String name; public IngredientDto() {} @@ -294,19 +296,19 @@ avec les getter/setter correspondant aux propriétés de l'object JSON. this.id = id; } - public void setNom(String nom) { - this.nom = nom; + public void setName(String name) { + this.name = name; } - public String getNom() { - return nom; + public String getName() { + return name; } } Du côté de la ressource, on peut fournir une première implémentation : import javax.ws.rs.PathParam; - + import fr.ulille.iut.pizzaland.beans.Ingredient; @GET @Path("{id}") @@ -322,63 +324,78 @@ Pour cette méthode, nous avons introduit la classe `Ingredient`. Ce JavaBean représente un ingrédient manipulé par la ressource. Voici une implémentation pour cette classe : -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; + package fr.ulille.iut.pizzaland.beans; + + import fr.ulille.iut.pizzaland.dto.IngredientDto; + + public class Ingredient { + private long id; + private String name; - public Ingredient() { - } + public Ingredient() { + } - public Ingredient(long id, String name) { + public Ingredient(long id, String name) { this.id = id; this.name = name; - } + } - public void setId(long id) { + public void setId(long id) { this.id = id; - } + } - public long getId() { + public long getId() { return id; - } + } - public String getName() { + public String getName() { return name; - } + } - public void setName(String 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 IngredientDto toDto(Ingredient i) { + public static IngredientDto toDto(Ingredient i) { IngredientDto dto = new IngredientDto(); dto.setId(i.getId()); dto.setName(i.getName()); - return dto; - } + return dto; + } - public static Ingredient fromDto(IngredientDto 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 + "]"; + } + } Le test devrait maintenant réussir : diff --git a/src/test/java/fr/ulille/iut/pizzaland/IngredientResourceTest.java b/src/test/java/fr/ulille/iut/pizzaland/IngredientResourceTest.java index a5f4185..2c8b1be 100644 --- a/src/test/java/fr/ulille/iut/pizzaland/IngredientResourceTest.java +++ b/src/test/java/fr/ulille/iut/pizzaland/IngredientResourceTest.java @@ -59,7 +59,7 @@ public class IngredientResourceTest extends JerseyTest { // 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. + // de la réponse lue quand on a un type complexe (typiquement une liste). List<IngredientDto> ingredients; ingredients = response.readEntity(new GenericType<List<IngredientDto>>(){}); -- GitLab