Skip to content

Commit

Permalink
Update GraphQL exercises, update slides
Browse files Browse the repository at this point in the history
  • Loading branch information
basdijkstra committed Dec 11, 2023
1 parent 4b27d61 commit fa8e2b0
Show file tree
Hide file tree
Showing 13 changed files with 159 additions and 29 deletions.
Binary file modified RestAssuredWorkshop.odp
Binary file not shown.
Binary file modified RestAssuredWorkshop.pdf
Binary file not shown.
Binary file modified RestAssuredWorkshop.pptx
Binary file not shown.
51 changes: 35 additions & 16 deletions src/test/java/answers/RestAssuredAnswers6Test.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package answers;

import com.github.tomakehurst.wiremock.junit5.WireMockTest;
import io.restassured.builder.RequestSpecBuilder;
import io.restassured.http.ContentType;
import io.restassured.specification.RequestSpecification;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
Expand All @@ -14,20 +17,35 @@
@WireMockTest(httpPort = 9876)
public class RestAssuredAnswers6Test {

private RequestSpecification requestSpec;

@BeforeEach
public void createRequestSpecification() {

requestSpec = new RequestSpecBuilder().
setBaseUri("http://localhost").
setPort(9876).
setContentType(ContentType.JSON).
build();
}

/*******************************************************
* Create a new payload for a GraphQL query using a
* HashMap and the specified query (with hardcoded ID)
*
* POST this object to https://fruits-api.netlify.app/graphql
*
* Assert that the name of the fruit is 'Manzana'
* POST this object to /graphql
*
* Assert that the name of the fruit is equal to "Apple"
* Use "data.fruit.fruit_name" as the GPath
* expression to extract the required value from the response
*
* Also, assert that the tree name is equal to "Malus"
* Use "data.fruit.tree_name" as the GPath
* expression to extract the required value from the response
******************************************************/

@Test
public void getFruitData_checkFruitName_shouldBeManzana() {
public void getFruitData_checkFruitAndTreeName_shouldBeAppleAndMalus() {

String queryString = """
{
Expand All @@ -43,15 +61,16 @@ public void getFruitData_checkFruitName_shouldBeManzana() {
graphQlQuery.put("query", queryString);

given().
contentType(ContentType.JSON).
spec(requestSpec).
body(graphQlQuery).
when().
post("https://fruits-api.netlify.app/graphql").
post("/graphql").
then().
assertThat().
statusCode(200).
and().
body("data.fruit.fruit_name", equalTo("Manzana"));
body("data.fruit.fruit_name", equalTo("Apple")).
body("data.fruit.tree_name", equalTo("Malus"));
}

/*******************************************************
Expand All @@ -60,16 +79,16 @@ public void getFruitData_checkFruitName_shouldBeManzana() {
* ---------------------------------
* fruit id | fruit name | tree name
* ---------------------------------
* 1 | Manzana | Manzano
* 2 | Pera | Peral
* 3 | Banana | Platano
* 1 | Apple | Malus
* 2 | Pear | Pyrus
* 3 | Banana | Musa
*
* Parameterize the test
*
* Create a new GraphQL query from the given query string
* Pass in the fruit id as a variable value
*
* POST this object to https://fruits-api.netlify.app/graphql
* POST this object to /graphql
*
* Assert that the HTTP response status code is 200
*
Expand All @@ -84,9 +103,9 @@ public void getFruitData_checkFruitName_shouldBeManzana() {

@ParameterizedTest
@CsvSource({
"1, Manzana, Manzano",
"2, Pera, Peral",
"3, Banana, Platano"
"1, Apple, Malus",
"2, Pear, Pyrus",
"3, Banana, Musa"
})
public void getFruitDataById_checkFruitNameAndTreeName(int fruitId, String expectedFruitName, String expectedTreeName) {

Expand All @@ -109,10 +128,10 @@ query GetFruit($id: ID!)
graphqlQuery.put("variables", variables);

given().
contentType(ContentType.JSON).
spec(requestSpec).
body(graphqlQuery).
when().
post("https://fruits-api.netlify.app/graphql").
post("/graphql").
then().
assertThat().
statusCode(200).
Expand Down
7 changes: 4 additions & 3 deletions src/test/java/examples/RestAssuredExamplesXmlTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasSize;

@Disabled
public class RestAssuredExamplesXmlTest {
Expand All @@ -28,7 +29,7 @@ public void checkYearForLastCar() {
get("http://path.to/cars/xml").
then().
assertThat().
body("cars.car[-1].year", equalTo("2012"));
body("cars.car[-1].modelYear", equalTo("2012"));
}

@Test
Expand All @@ -50,7 +51,7 @@ public void checkTheListContainsOneJapaneseCar() {
get("http://path.to/cars/xml").
then().
assertThat().
body("cars.car.findAll{it.country=='Japan'}.size()", equalTo(1));
body("cars.car.findAll{it.country=='Japan'}", hasSize(1));
}

@Test
Expand All @@ -61,7 +62,7 @@ public void checkTheListContainsTwoCarsWhoseMakeStartsWithAnA() {
get("http://path.to/cars/xml").
then().
assertThat().
body("cars.car.@make.grep(~/A.*/).size()", equalTo(2));
body("cars.car.@make.grep(~/A.*/)", hasSize(2));
}
}

39 changes: 29 additions & 10 deletions src/test/java/exercises/RestAssuredExercises6Test.java
Original file line number Diff line number Diff line change
@@ -1,28 +1,47 @@
package exercises;

import com.github.tomakehurst.wiremock.junit5.WireMockTest;
import io.restassured.builder.RequestSpecBuilder;
import io.restassured.http.ContentType;
import io.restassured.specification.RequestSpecification;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import static io.restassured.RestAssured.given;
import static io.restassured.RestAssured.request;

@WireMockTest(httpPort = 9876)
public class RestAssuredExercises6Test {

private RequestSpecification requestSpec;

@BeforeEach
public void createRequestSpecification() {

requestSpec = new RequestSpecBuilder().
setBaseUri("http://localhost").
setPort(9876).
setContentType(ContentType.JSON).
build();
}

/*******************************************************
* Create a new payload for a GraphQL query using a
* HashMap and the specified query (with hardcoded ID)
*
* POST this object to https://fruits-api.netlify.app/graphql
*
* Assert that the name of the fruit is 'Manzana'
* POST this object to /graphql
*
* Assert that the name of the fruit is equal to "Apple"
* Use "data.fruit.fruit_name" as the GPath
* expression to extract the required value from the response
*
* Also, assert that the tree name is equal to "Malus"
* Use "data.fruit.tree_name" as the GPath
* expression to extract the required value from the response
******************************************************/

@Test
public void getFruitData_checkFruitName_shouldBeManzana() {
public void getFruitData_checkFruitAndTreeName_shouldBeAppleAndMalus() {

String queryString = """
{
Expand All @@ -35,7 +54,7 @@ public void getFruitData_checkFruitName_shouldBeManzana() {
""";

given().
contentType(ContentType.JSON).
spec(requestSpec).
when().
then();
}
Expand All @@ -46,16 +65,16 @@ public void getFruitData_checkFruitName_shouldBeManzana() {
* ---------------------------------
* fruit id | fruit name | tree name
* ---------------------------------
* 1 | Manzana | Manzano
* 2 | Pera | Peral
* 3 | Banana | Platano
* 1 | Apple | Malus
* 2 | Pear | Pyrus
* 3 | Banana | Musa
*
* Parameterize the test
*
* Create a new GraphQL query from the given query string
* Pass in the fruit id as a variable value
*
* POST this object to https://fruits-api.netlify.app/graphql
* POST this object to /graphql
*
* Assert that the HTTP response status code is 200
*
Expand Down Expand Up @@ -83,7 +102,7 @@ query GetFruit($id: ID!)
""";

given().
contentType(ContentType.JSON).
spec(requestSpec).
when().
then();
}
Expand Down
9 changes: 9 additions & 0 deletions src/test/resources/__files/apple.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"data": {
"fruit": {
"id": 1,
"fruit_name": "Apple",
"tree_name": "Malus"
}
}
}
9 changes: 9 additions & 0 deletions src/test/resources/__files/banana.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"data": {
"fruit": {
"id": 2,
"fruit_name": "Banana",
"tree_name": "Musa"
}
}
}
9 changes: 9 additions & 0 deletions src/test/resources/__files/pear.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"data": {
"fruit": {
"id": 2,
"fruit_name": "Pear",
"tree_name": "Pyrus"
}
}
}
16 changes: 16 additions & 0 deletions src/test/resources/mappings/graphql_get_fruit.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"request": {
"method": "POST",
"url": "/graphql",
"bodyPatterns": [
{
"contains": "fruit(id: 1)"
}
]
},
"response": {
"status": 200,
"headers": { "Content-Type": "application/json" },
"bodyFileName": "apple.json"
}
}
16 changes: 16 additions & 0 deletions src/test/resources/mappings/graphql_get_fruit_1.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"request": {
"method": "POST",
"url": "/graphql",
"bodyPatterns": [
{
"matchesJsonPath": "$.variables[?(@.id == 1)]"
}
]
},
"response": {
"status": 200,
"headers": { "Content-Type": "application/json" },
"bodyFileName": "apple.json"
}
}
16 changes: 16 additions & 0 deletions src/test/resources/mappings/graphql_get_fruit_2.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"request": {
"method": "POST",
"url": "/graphql",
"bodyPatterns": [
{
"matchesJsonPath": "$.variables[?(@.id == 2)]"
}
]
},
"response": {
"status": 200,
"headers": { "Content-Type": "application/json" },
"bodyFileName": "pear.json"
}
}
16 changes: 16 additions & 0 deletions src/test/resources/mappings/graphql_get_fruit_3.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"request": {
"method": "POST",
"url": "/graphql",
"bodyPatterns": [
{
"matchesJsonPath": "$.variables[?(@.id == 3)]"
}
]
},
"response": {
"status": 200,
"headers": { "Content-Type": "application/json" },
"bodyFileName": "banana.json"
}
}

0 comments on commit fa8e2b0

Please sign in to comment.