Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
M
m4102_tp3
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Hugo Blanquart
m4102_tp3
Commits
58b7dc28
Commit
58b7dc28
authored
5 years ago
by
Yvan Peter
Browse files
Options
Downloads
Patches
Plain Diff
DELETE et name
parent
5815f22b
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
README.md
+89
-1
89 additions, 1 deletion
README.md
with
89 additions
and
1 deletion
README.md
+
89
−
1
View file @
58b7dc28
...
...
@@ -562,13 +562,14 @@ L'implémentation de la classe devrait fonctionner avec le test suivant
assertEquals(Response.Status.NOT_FOUND.getStatusCode(),response.getStatus());
}
$ mvn test
Results :
Tests run: 3, Failures: 0, Errors: 0, Skipped: 0
###
Test de création d'ingrédient
###
Implementation de la méthode POST
Il va falloir implémenter la méthode POST pour la création des
ingrédients. Commençons par les différents tests : création, création
de deux ingrédients identiques et création d'ingrédient sans nom.
...
...
@@ -728,3 +729,90 @@ Vous aurez peut-être un affichage d'exception liée au test de création
de doublon, toutefois le test est réussi puisqu'il a levé une
exception qui a été traduite par un code d'erreur HTTP 406.
### Implémentation de la méthode DELETE
Les tests liés à la méthode DELETE sont les suivants :
@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());
}
Après avoir constaté que ces tests échouent, nous pouvons fournir une
implémentation pour la méthode DELETE :
import javax.ws.rs.DELETE;
@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();
}
Nous devons également implémenter la méthode remove dans
`IngredientDao`
:
@SqlUpdate("DELETE FROM ingredients WHERE id = :id")
void remove(long id);
Avec cette implémentation, nos tests réussissent.
### Implémentation de la méthode GET pour récupérer le nom de l'ingrédient
Commençons par les tests correspondant à cette URI (GET
/ingredients/{id}/name)
@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());
}
L'implémentation correspondant à ce test est simple :
@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();
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment