Skip to content

Commit fbc91fb

Browse files
VinayKumar VVSVinayKumar VVS
authored andcommitted
Added Chapter05
1 parent 32d60d0 commit fbc91fb

File tree

4 files changed

+132
-2
lines changed

4 files changed

+132
-2
lines changed

ReadMe.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,7 @@ Throughout this tutorial, I am going to use [PetStore-Swagger](http://petstore.s
4545
1. **[Test-1](https://github.com/vinaykumarvvs/api-automation-tutorial/tree/master/src/test/java/Chapters/Chapter03/ThirdChapterTests.java):** Creating the POST Request and validating the Response Code
4646

4747
#### [Chapter 4](https://github.com/vinaykumarvvs/api-automation-tutorial/tree/master/src/test/java/Chapters/Chapter04/Chapter04.md)
48-
1. **[Test-1](https://github.com/vinaykumarvvs/api-automation-tutorial/tree/master/src/test/java/Chapters/Chapter04/FourthChapterTests.java):** Creating the POST Request and validating the RequestBody & ResponseBody
48+
1. **[Test-1](https://github.com/vinaykumarvvs/api-automation-tutorial/tree/master/src/test/java/Chapters/Chapter04/FourthChapterTests.java):** Creating the POST Request and validating the RequestBody & ResponseBody
49+
50+
#### [Chapter 5](https://github.com/vinaykumarvvs/api-automation-tutorial/tree/master/src/test/java/Chapters/Chapter05/Chapter05.md)
51+
1. **[Test-1](https://github.com/vinaykumarvvs/api-automation-tutorial/tree/master/src/test/java/Chapters/Chapter05/FifthChapterTests.java)** Creating a Pet using POST method and verifying it by using GET method
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
## Chapter 5
2+
3+
#### **Pre-requisites**
4+
* To get started with the <b>Fourth Chapter</b>, I recommend you to go through the [Chapter 4](https://github.com/vinaykumarvvs/api-automation-tutorial/tree/master/src/test/java/Chapters/Chapter04/Chapter04.md).
5+
* If you are reading this line then you are good to go. Let's get started with the <b>Fifth Chapter</b>
6+
7+
#### **Quick Recap**
8+
Here are some listing, we have learned till now
9+
1. How to send a GET Request
10+
2. How to validate the Response
11+
3. How to segregate the things in order to maintain `Single Responsibility Principle`.
12+
4. How to send the POST Request using Entity-Builder pattern and validate the Response Code.
13+
5. And also, How to validate RequestBody & ResponseBody
14+
15+
#### **Agenda for this Chapter**
16+
* Till now we have seen how to send a Request and receive the Response, and their validations. Now we are going to see
17+
<b>How to Chain the API's</b>. What is Chaining? It is a process where we can pass
18+
the information from one HTTP method to other HTTP method.
19+
20+
#### **Tests**
21+
This Chapter consists only one test
22+
1. **Test-1:** Creating a Pet using POST method and verifying it by using GET method
23+
24+
In this test, we are doing the following actions:<br/>
25+
<b>POST Request</b><br/>
26+
a. Creating an object for <b>Category</b> with some data <br/>
27+
b. Creating an object for <b>Tags</b> with some data and inserting in the TagsList<br/>
28+
c. Now we are creating the main object <b>CreatePetRequest</b><br/>
29+
d. After creating the all objects, we are converting the object into String and sending it to the server<br/>
30+
e. Once we receive the Response, with the help of <b>ResponseHelper</b> we can map JSON String to Java Object<br/>
31+
f. Verifying the attributes from <b>RequestBody</b> & <b>ResponseBody</b>
32+
33+
<b>GET Request</b><br/>
34+
g. Creating a GET Request using the PetId and verifying the other data
35+
36+
```Similarly we can chain the other HTTP Methods as well. Like POST -> GET -> PUT -> DELETE ..```
37+
38+
#### **Summary**
39+
These are the things that we have learned till now
40+
1. How to send the GET Request and receive the Response.
41+
2. How to segregate the code based on their functionality by following `Single Responsibility Principle`.
42+
3. How to create and send the POST Request using `Entity-Builder` pattern.
43+
4. How to verify the RequestBody & ResponseBody
44+
5. How to chain the API's
45+
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
package Chapters.Chapter05;
2+
3+
import builders.CategoryBuilder;
4+
import builders.CreatePetRequestBuilder;
5+
import builders.TagsBuilder;
6+
import entities.requests.Category;
7+
import entities.requests.CreatePetRequest;
8+
import entities.requests.Tags;
9+
import entities.responses.CreatePetResponse;
10+
import io.restassured.response.Response;
11+
import org.testng.Assert;
12+
import org.testng.annotations.Test;
13+
import utils.BaseTest;
14+
import utils.RequestHelper;
15+
import utils.ResourceHelper;
16+
import utils.ResponseHelper;
17+
18+
import java.io.IOException;
19+
20+
public class FifthChapterTests extends BaseTest {
21+
22+
@Test
23+
public void createPetAndVerifyTheCreation() throws IOException {
24+
25+
// Creating the Category Object
26+
Category category = new CategoryBuilder()
27+
.withId(1)
28+
.withName("Cats")
29+
.build();
30+
31+
// Creating the Tags Object
32+
Tags tags = new TagsBuilder()
33+
.withId(1)
34+
.withName("Tag 1")
35+
.build();
36+
37+
// Inserting the above created Tags object in the TagsList, because main object accepts as an array
38+
Tags[] tagsList = new Tags[1];
39+
tagsList[0] = tags;
40+
41+
// Creating the Main Object - CreatePetRequest
42+
String[] photoUrls = {"Photo Url"}; // Create an array with some URL's
43+
CreatePetRequest createPetRequest = new CreatePetRequestBuilder()
44+
.withCategory(category)
45+
.withTags(tagsList)
46+
.withPhotoUrls(photoUrls)
47+
.withId(get3DigitRandomInt()) // This `get3DigitRandomInt()` will generate the random 3 digit number, coming from BaseTest
48+
.withName("Testing + " + get3DigitRandomInt())
49+
.withStatus("available")
50+
.build();
51+
52+
// Sending a Request
53+
String url = propertiesReader.getEndPointUrl("create_pet"); // Fetching url from Properties file
54+
String json = RequestHelper.getJsonString(createPetRequest); // Convert above created object into a String
55+
Response response = ResourceHelper.create(url, json);
56+
57+
// Binding Response body to the Java Object
58+
CreatePetResponse createPetResponse = (CreatePetResponse)
59+
ResponseHelper.getResponseAsObject(response.asString(), CreatePetResponse.class);
60+
61+
// Validating the Response Code
62+
Assert.assertEquals(response.getStatusCode(), 200);
63+
64+
// Validating the RequestBody & ResponseBody
65+
Assert.assertEquals(createPetRequest.getName(), createPetResponse.getName());
66+
Assert.assertEquals(createPetRequest.getStatus(), createPetResponse.getStatus());
67+
Assert.assertEquals(createPetRequest.getTags()[0].getName(), createPetResponse.getTags()[0].getName());
68+
69+
// Verifying the Created Pet using GET Method
70+
String url1 = propertiesReader.getEndPointUrl("get_animal_based_on_pet_id") + createPetRequest.getId();// Concatenating the EndPoint & PetId
71+
Response findSpecificPetResponse = ResourceHelper.get(url1);
72+
73+
// Binding Response body to the Java Object
74+
CreatePetResponse getPetResponseBasedOnId = (CreatePetResponse)
75+
ResponseHelper.getResponseAsObject(findSpecificPetResponse.asString(), CreatePetResponse.class);
76+
77+
Assert.assertEquals(findSpecificPetResponse.getStatusCode(), 200);
78+
Assert.assertEquals(createPetRequest.getId(), getPetResponseBasedOnId.getId());
79+
Assert.assertEquals(createPetRequest.getName(), getPetResponseBasedOnId.getName());
80+
}
81+
}

src/test/resources/url.properties

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
base_url=http://petstore.swagger.io/v2
22
get_animals=/pet/findByStatus?status=available
3-
create_pet=/pet
3+
create_pet=/pet
4+
get_animal_based_on_pet_id=/pet/

0 commit comments

Comments
 (0)