Last active
October 15, 2016 14:02
-
-
Save jy95/697e32e5a7ca2e2f3209bc1fe2ae9bfe to your computer and use it in GitHub Desktop.
RestTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import static org.testng.Assert.*; | |
| import java.util.List; | |
| import javax.ws.rs.client.Client; | |
| import javax.ws.rs.client.ClientBuilder; | |
| import javax.ws.rs.client.Invocation; | |
| import javax.ws.rs.client.WebTarget; | |
| import javax.ws.rs.core.GenericType; | |
| import javax.ws.rs.core.MediaType; | |
| import javax.ws.rs.core.Response; | |
| import org.testng.annotations.*; | |
| import be.ipl.ebar.usecases.Biere; | |
| import be.ipl.ebar.usecases.Commande; | |
| public class RestTest { | |
| Client client; | |
| WebTarget target; | |
| @BeforeTest | |
| public void set() { | |
| client = ClientBuilder.newClient(); | |
| target = client.target("http://localhost:8080"); | |
| } | |
| @Test(groups = "a") | |
| public void chercherBieresXML() { | |
| GenericType<List<Biere>> lb = new GenericType<List<Biere>>() {}; | |
| Invocation.Builder invocationBuilder = target | |
| .path("/eBarRest/rest/eBar/bieres/Chimay") | |
| .request().accept(MediaType.APPLICATION_XML_TYPE); | |
| Response resp = invocationBuilder.get(); | |
| List<Biere> recu = resp.readEntity(lb); | |
| assertEquals(resp.getStatus() , Response.Status.OK.getStatusCode()); | |
| assertEquals(resp.getMediaType() , MediaType.APPLICATION_XML_TYPE); | |
| assertEquals(recu.size() , 2); | |
| assertEquals(recu.get(0).getId() , 1); | |
| assertEquals(recu.get(1).getId() , 2); | |
| } | |
| @Test(groups = "a") | |
| public void chercherBieresJson() { | |
| GenericType<List<Biere>> lb = new GenericType<List<Biere>>() {}; | |
| Invocation.Builder invocationBuilder = target | |
| .path("/eBarRest/rest/eBar/bieres/Chimay") | |
| .request().accept(MediaType.APPLICATION_JSON_TYPE); | |
| Response resp = invocationBuilder.get(); | |
| List<Biere> recu = resp.readEntity(lb); | |
| assertEquals(Response.Status.OK.getStatusCode(), resp.getStatus()); | |
| assertEquals(MediaType.APPLICATION_JSON_TYPE, resp.getMediaType()); | |
| assertEquals(2, recu.size()); | |
| assertEquals(1, recu.get(0).getId()); | |
| assertEquals(2, recu.get(1).getId()); | |
| } | |
| @Test(groups = "a") | |
| public void chercherBieresQueryXML() { | |
| GenericType<List<Biere>> lb = new GenericType<List<Biere>>() {}; | |
| Invocation.Builder invocationBuilder = target | |
| .path("/eBarRest/rest/eBar/bieres") | |
| .queryParam("mot", "Chimay") | |
| .request().accept(MediaType.APPLICATION_XML_TYPE); | |
| Response resp = invocationBuilder.get(); | |
| List<Biere> recu = resp.readEntity(lb); | |
| assertEquals(resp.getStatus() , Response.Status.OK.getStatusCode()); | |
| assertEquals(resp.getMediaType() , MediaType.APPLICATION_XML_TYPE); | |
| assertEquals(recu.size() , 2); | |
| assertEquals(recu.get(0).getId() , 1); | |
| assertEquals(recu.get(1).getId() , 2); | |
| } | |
| @Test(groups = "a") | |
| public void chercherBieresQueryJson() { | |
| GenericType<List<Biere>> lb = new GenericType<List<Biere>>() {}; | |
| Invocation.Builder invocationBuilder = target | |
| .path("/eBarRest/rest/eBar/bieres") | |
| .queryParam("mot", "Chimay") | |
| .request().accept(MediaType.APPLICATION_JSON_TYPE); | |
| Response resp = invocationBuilder.get(); | |
| List<Biere> recu = resp.readEntity(lb); | |
| assertEquals(resp.getStatus() , Response.Status.OK.getStatusCode()); | |
| assertEquals(resp.getMediaType() , MediaType.APPLICATION_JSON_TYPE); | |
| assertEquals(recu.size() , 2); | |
| assertEquals(recu.get(0).getId() , 1); | |
| assertEquals(recu.get(1).getId() , 2); | |
| } | |
| @Test(groups = "a") | |
| public void chercherCarteXML() { | |
| GenericType<List<Biere>> lb = new GenericType<List<Biere>>() {}; | |
| Invocation.Builder invocationBuilder = target | |
| .path("/eBarRest/rest/eBar/bieres") | |
| .queryParam("mot", "") | |
| .request().accept(MediaType.APPLICATION_XML_TYPE); | |
| Response resp = invocationBuilder.get(); | |
| List<Biere> recu = resp.readEntity(lb); | |
| assertEquals(resp.getStatus() , Response.Status.OK.getStatusCode()); | |
| assertEquals(resp.getMediaType() , MediaType.APPLICATION_XML_TYPE); | |
| assertEquals(recu.size() , 9); | |
| } | |
| @Test(groups = "a") | |
| public void chercherCarteJson() { | |
| GenericType<List<Biere>> lb = new GenericType<List<Biere>>() {}; | |
| Invocation.Builder invocationBuilder = target | |
| .path("/eBarRest/rest/eBar/bieres") | |
| .queryParam("mot", "") | |
| .request().accept(MediaType.APPLICATION_JSON_TYPE); | |
| Response resp = invocationBuilder.get(); | |
| List<Biere> recu = resp.readEntity(lb); | |
| assertEquals(resp.getStatus() , Response.Status.OK.getStatusCode()); | |
| assertEquals(resp.getMediaType() , MediaType.APPLICATION_JSON_TYPE); | |
| assertEquals(recu.size() , 9); | |
| } | |
| @Test(groups = "b") | |
| public void ajouterCommande() { | |
| Invocation.Builder invocationBuilder = target | |
| .path("/eBarRest/rest/eBar/commande") | |
| .request().accept(MediaType.TEXT_PLAIN); | |
| Response resp = invocationBuilder.method("post"); | |
| int numeroCommande = Integer.parseInt(resp.readEntity(String.class)); | |
| assertEquals(resp.getStatus(), Response.Status.CREATED.getStatusCode()); | |
| assertEquals(resp.getMediaType() , MediaType.TEXT_PLAIN_TYPE); | |
| assertEquals(numeroCommande , 0); | |
| } | |
| @Test(dependsOnGroups = "b" , dependsOnMethods = {"ajouterCommande"}) | |
| public void ajouterBiere() { | |
| Invocation.Builder invocationBuilder = target | |
| .path("/commande/0/biere/2") | |
| .request(); | |
| Response resp = invocationBuilder.method("post"); | |
| assertEquals(resp.getStatus() , Response.Status.OK.getStatusCode()); | |
| invocationBuilder = target | |
| .path("/commande/0/biere/4") | |
| .request(); | |
| Response resp2 = invocationBuilder.method("post"); | |
| assertEquals(resp2.getStatus(),Response.Status.OK.getStatusCode()); | |
| } | |
| @Test(dependsOnMethods = {"ajouterBiere"}) | |
| public void rechercherCommandeXML() { | |
| Invocation.Builder invocationBuilder = target | |
| .path("/eBarRest/rest/eBar/commande/0") | |
| .request().accept(MediaType.APPLICATION_XML_TYPE); | |
| Response resp = invocationBuilder.get(); | |
| Commande recu = resp.readEntity(Commande.class); | |
| assertEquals(resp.getStatus(), Response.Status.OK.getStatusCode()); | |
| assertEquals(resp.getMediaType(), MediaType.APPLICATION_XML_TYPE); | |
| assertEquals(recu.getBieres().size(), 2); | |
| assertEquals(recu.getBieres().get(0).getId() , 2); | |
| assertEquals(recu.getBieres().get(1).getId() , 4); | |
| } | |
| @Test(dependsOnMethods = {"rechercherCommandeXML"}) | |
| public void rechercherCommandeJson() { | |
| Invocation.Builder invocationBuilder = target | |
| .path("/eBarRest/rest/eBar/commande/0") | |
| .request().accept(MediaType.APPLICATION_JSON_TYPE); | |
| Response resp = invocationBuilder.get(); | |
| Commande recu = resp.readEntity(Commande.class); | |
| assertEquals(resp.getStatus(), Response.Status.OK.getStatusCode()); | |
| assertEquals(resp.getMediaType(), MediaType.APPLICATION_JSON_TYPE); | |
| assertEquals(recu.getBieres().size(), 2); | |
| assertEquals(recu.getBieres().get(0).getId() , 2); | |
| assertEquals(recu.getBieres().get(1).getId() , 4); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment