diff --git a/src/main/java/fr/univtln/bruno/samples/jaxrs/model/Library.java b/src/main/java/fr/univtln/bruno/samples/jaxrs/model/Library.java index c42b95e416ad4b04ffb2a4190e6ecb0688d5a9b3..599c9328a7b82f4bc19a7d06903baefe22c5d6d1 100644 --- a/src/main/java/fr/univtln/bruno/samples/jaxrs/model/Library.java +++ b/src/main/java/fr/univtln/bruno/samples/jaxrs/model/Library.java @@ -77,7 +77,7 @@ public class Library { * @throws BusinessException if the format is incorrect. */ public Author addAuthor(Author author) throws BusinessException { - if (author.id != 0) throw new BusinessException(Response.Status.INTERNAL_SERVER_ERROR, "Id shouldn't be given"); + if (author.id != 0) throw new BusinessException(Response.Status.NOT_ACCEPTABLE, "Id shouldn't be given"); author.id = lastAuthorId.incrementAndGet(); authors.put(author.id, author); @@ -95,7 +95,7 @@ public class Library { if (book.id != 0) throw new BusinessException(Response.Status.INTERNAL_SERVER_ERROR, "Id shouldn't be given"); if (book.authors == null || book.authors.isEmpty()) throw new BusinessException(Response.Status.INTERNAL_SERVER_ERROR, "Author set is mandatory"); - book.id = lastAuthorId.incrementAndGet(); + book.id = lastBookId.incrementAndGet(); book.authors.stream().forEach(auteur -> { if (auteur.books == null) auteur.books = new HashSet<>(); auteur.books.add(book); @@ -114,7 +114,7 @@ public class Library { */ public Author updateAuteur(long id, Author author) throws BusinessException { if (author.id != 0) - throw new BusinessException(Response.Status.INTERNAL_SERVER_ERROR, "Id shouldn't be given in data"); + throw new BusinessException(Response.Status.NOT_ACCEPTABLE, "Id shouldn't be given in data"); author.id = id; if (!authors.containsKey(id)) throw new BusinessException(Response.Status.NOT_FOUND, AUTHOR_NOT_FOUND); authors.put(id, author); @@ -206,7 +206,9 @@ public class Library { */ public void removesAuthors() { authors.clear(); + books.clear(); lastAuthorId.set(0); + lastBookId.set(0); } /** diff --git a/src/test/java/fr/univtln/bruno/samples/jaxrs/LibraryModelAuthorTest.java b/src/test/java/fr/univtln/bruno/samples/jaxrs/LibraryModelAuthorTest.java index d0014e5bb0e83a4433ceadc48af10e0279035279..4fa441f9c53fc68d00c2959a48309187687a3578 100644 --- a/src/test/java/fr/univtln/bruno/samples/jaxrs/LibraryModelAuthorTest.java +++ b/src/test/java/fr/univtln/bruno/samples/jaxrs/LibraryModelAuthorTest.java @@ -18,9 +18,9 @@ public class LibraryModelAuthorTest { .firstname(prenom) .biography(bio) .build(); - assertThat(author, allOf(hasProperty("nom", is(nom)), - hasProperty("prenom", is(prenom)), - hasProperty("biographie", is(bio)))); + assertThat(author, allOf(hasProperty("name", is(nom)), + hasProperty("firstname", is(prenom)), + hasProperty("biography", is(bio)))); } } diff --git a/src/test/java/fr/univtln/bruno/samples/jaxrs/ServerIT.java b/src/test/java/fr/univtln/bruno/samples/jaxrs/ServerIT.java index 640b77d0f2f21cf0fccfd0c5581b6f65068bd9f0..093b4cf08347a95cb587ca83db00f5f47c54bbaf 100644 --- a/src/test/java/fr/univtln/bruno/samples/jaxrs/ServerIT.java +++ b/src/test/java/fr/univtln/bruno/samples/jaxrs/ServerIT.java @@ -62,7 +62,7 @@ public class ServerIT { */ @Before public void beforeEach() { - webTarget.path("biblio/init").request().put(Entity.entity("", MediaType.TEXT_PLAIN)); + webTarget.path("library/init").request().put(Entity.entity("", MediaType.TEXT_PLAIN)); } /** @@ -70,12 +70,12 @@ public class ServerIT { */ @After public void afterEach() { - webTarget.path("biblio/auteurs").request().delete(); + webTarget.path("authors").request().delete(); } @Test public void testHello() { - String hello = webTarget.path("biblio").request(MediaType.TEXT_PLAIN).get(String.class); + String hello = webTarget.path("library").request(MediaType.TEXT_PLAIN).get(String.class); assertEquals("hello", hello); } @@ -84,7 +84,7 @@ public class ServerIT { */ @Test public void testGetAuteurJSON() { - Library.Author responseAuthor = webTarget.path("biblio/auteurs/1").request(MediaType.APPLICATION_JSON).get(Library.Author.class); + Library.Author responseAuthor = webTarget.path("authors/1").request(MediaType.APPLICATION_JSON).get(Library.Author.class); assertNotNull(responseAuthor); assertEquals("Alfred", responseAuthor.getFirstname()); assertEquals("Martin", responseAuthor.getName()); @@ -95,7 +95,7 @@ public class ServerIT { */ @Test public void testGetAuteurXML() { - Library.Author responseAuthor = webTarget.path("biblio/auteurs/1").request(MediaType.TEXT_XML).get(Library.Author.class); + Library.Author responseAuthor = webTarget.path("authors/1").request(MediaType.TEXT_XML).get(Library.Author.class); assertNotNull(responseAuthor); assertEquals("Alfred", responseAuthor.getFirstname()); assertEquals("Martin", responseAuthor.getName()); @@ -106,7 +106,7 @@ public class ServerIT { */ @Test public void testGetAuteurJSONNotFoundException() { - Response response = webTarget.path("biblio/auteurs/10").request(MediaType.APPLICATION_JSON).get(); + Response response = webTarget.path("authors/10").request(MediaType.APPLICATION_JSON).get(); assertEquals(Response.Status.NOT_FOUND.getStatusCode(), response.getStatus()); } @@ -115,7 +115,7 @@ public class ServerIT { */ @Test public void testGetAuteurs() { - Collection<Library.Author> responseAuthors = webTarget.path("biblio/auteurs").request(MediaType.APPLICATION_JSON).get(new GenericType<>() { + Collection<Library.Author> responseAuthors = webTarget.path("authors").request(MediaType.APPLICATION_JSON).get(new GenericType<>() { }); assertEquals(2, responseAuthors.size()); } @@ -125,8 +125,8 @@ public class ServerIT { */ @Test public void deleteAuteurs() { - webTarget.path("biblio/auteurs").request().delete(); - Collection<Library.Author> responseAuthors = webTarget.path("biblio/auteurs").request(MediaType.APPLICATION_JSON).get(new GenericType<>() { + webTarget.path("authors").request().delete(); + Collection<Library.Author> responseAuthors = webTarget.path("authors").request(MediaType.APPLICATION_JSON).get(new GenericType<>() { }); assertEquals(0, responseAuthors.size()); } @@ -136,8 +136,8 @@ public class ServerIT { */ @Test public void deleteAuteur() { - webTarget.path("biblio/auteurs/1").request().delete(); - Collection<Library.Author> responseAuthors = webTarget.path("biblio/auteurs").request(MediaType.APPLICATION_JSON).get(new GenericType<>() { + webTarget.path("authors/1").request().delete(); + Collection<Library.Author> responseAuthors = webTarget.path("authors").request(MediaType.APPLICATION_JSON).get(new GenericType<>() { }); assertEquals(1, responseAuthors.size()); assertEquals(2, responseAuthors.iterator().next().getId()); @@ -149,14 +149,14 @@ public class ServerIT { */ @Test public void addAuteur() { - webTarget.path("biblio/auteurs") + webTarget.path("authors") .request(MediaType.APPLICATION_JSON) .accept(MediaType.APPLICATION_JSON) - .post(Entity.entity("{\"nom\":\"Smith\",\"prenom\":\"John\",\"biographie\":\"My life\"}", MediaType.APPLICATION_JSON)); - Collection<Author> responseAuthors = webTarget.path("biblio/auteurs").request(MediaType.APPLICATION_JSON).get(new GenericType<>() { + .post(Entity.entity("{\"name\":\"Smith\",\"firstname\":\"John\",\"biography\":\"My life\"}", MediaType.APPLICATION_JSON)); + Collection<Author> responseAuthors = webTarget.path("authors").request(MediaType.APPLICATION_JSON).get(new GenericType<>() { }); assertEquals(3, responseAuthors.size()); - Library.Author responseAuthor = webTarget.path("biblio/auteurs/3").request(MediaType.APPLICATION_JSON).get(Library.Author.class); + Library.Author responseAuthor = webTarget.path("authors/3").request(MediaType.APPLICATION_JSON).get(Library.Author.class); assertNotNull(responseAuthor); assertEquals("John", responseAuthor.getFirstname()); assertEquals("Smith", responseAuthor.getName()); @@ -168,11 +168,11 @@ public class ServerIT { */ @Test public void updateAuteur() { - webTarget.path("biblio/auteurs/1") + webTarget.path("authors/1") .request(MediaType.APPLICATION_JSON) .accept(MediaType.APPLICATION_JSON) - .put(Entity.entity("{\"nom\":\"Doe\",\"prenom\":\"Jim\",\"biographie\":\"My weird life\"}", MediaType.APPLICATION_JSON)); - Author responseAuthor = webTarget.path("biblio/auteurs/1").request(MediaType.APPLICATION_JSON).get(Library.Author.class); + .put(Entity.entity("{\"name\":\"Doe\",\"firstname\":\"Jim\",\"biography\":\"My weird life\"}", MediaType.APPLICATION_JSON)); + Author responseAuthor = webTarget.path("authors/1").request(MediaType.APPLICATION_JSON).get(Library.Author.class); assertNotNull(responseAuthor); assertEquals("Jim", responseAuthor.getFirstname()); assertEquals("Doe", responseAuthor.getName()); @@ -184,10 +184,11 @@ public class ServerIT { */ @Test public void updateAuteurIllegalArgument() { - Response response = webTarget.path("biblio/auteurs/1") + Response response = webTarget.path("authors/1") .request(MediaType.APPLICATION_JSON) .accept(MediaType.APPLICATION_JSON) - .put(Entity.entity("{\"id\":\"1\",\"nom\":\"Doe\",\"prenom\":\"Jim\",\"biographie\":\"My weird life\"}", MediaType.APPLICATION_JSON)); + .put(Entity.entity(""" + {"id":"1","name":"Doe","firstname":"Jim","biography":"My weird life"}""", MediaType.APPLICATION_JSON)); assertEquals(Response.Status.NOT_ACCEPTABLE.getStatusCode(), response.getStatus()); } @@ -208,8 +209,8 @@ public class ServerIT { */ @Test public void filter() { - List<Library.Author> authors = webTarget.path("biblio/auteurs/filter") - .queryParam("prenom","Marie") + List<Library.Author> authors = webTarget.path("authors/filter") + .queryParam("firstname","Marie") .request(MediaType.APPLICATION_JSON) .get(new GenericType<>() {}); @@ -219,7 +220,7 @@ public class ServerIT { @Test public void refusedLogin() { - Response result = webTarget.path("biblio/login") + Response result = webTarget.path("setup/login") .request() .get(); assertEquals(Response.Status.UNAUTHORIZED.getStatusCode(), result.getStatus()); @@ -229,7 +230,7 @@ public class ServerIT { public void acceptedLogin() { String email="john.doe@nowhere.com"; String password="admin"; - Response result = webTarget.path("biblio/login") + Response result = webTarget.path("setup/login") .request() .accept(MediaType.TEXT_PLAIN) .header("Authorization", "Basic "+java.util.Base64.getEncoder().encodeToString((email+":"+password).getBytes())) @@ -249,14 +250,14 @@ public class ServerIT { //Log in to get the token String email="john.doe@nowhere.com"; String password="admin"; - String token = webTarget.path("biblio/login") + String token = webTarget.path("setup/login") .request() .accept(MediaType.TEXT_PLAIN) .header("Authorization", "Basic "+java.util.Base64.getEncoder().encodeToString((email+":"+password).getBytes())) .get(String.class); //We access a JWT protected URL with the token - Response result = webTarget.path("biblio/secured") + Response result = webTarget.path("setup/secured") .request() .header( "Authorization", "Bearer "+token) .get(); @@ -277,7 +278,7 @@ public class ServerIT { .signWith( Keys.secretKeyFor(SignatureAlgorithm.HS256)).compact(); //We access a JWT protected URL with the token - Response result = webTarget.path("biblio/secured") + Response result = webTarget.path("setup/secured") .request() .header( "Authorization", "Bearer "+forgedToken) .get(); diff --git a/src/test/java/fr/univtln/bruno/samples/jaxrs/model/LibraryModelTest.java b/src/test/java/fr/univtln/bruno/samples/jaxrs/model/LibraryModelTest.java index 6a93869fc7648b994289d7fd93e1c7d8aede6ee8..f9b603cba0f66843aad73a6f1c2e9cbefad197d6 100644 --- a/src/test/java/fr/univtln/bruno/samples/jaxrs/model/LibraryModelTest.java +++ b/src/test/java/fr/univtln/bruno/samples/jaxrs/model/LibraryModelTest.java @@ -38,7 +38,7 @@ public class LibraryModelTest { assertThat(author, samePropertyValuesAs(modeleBibliotheque.getAuthor(3), "id")); } - @Test(expected = NotFoundException.class) + @Test(expected = BusinessException.class) public void addAuteurException() throws BusinessException { Library.Author author = Library.Author.builder().firstname("John").name("Doe").build(); modeleBibliotheque.addAuthor(SerializationUtils.clone(author));