-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCreateNewProduct.java
161 lines (127 loc) · 6.18 KB
/
CreateNewProduct.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package testCases;
import org.testng.Assert;
import org.testng.annotations.Test;
import io.restassured.path.json.JsonPath;
import io.restassured.response.Response;
import static io.restassured.RestAssured.*;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;
public class CreateNewProduct {
//creating a global variable
HashMap<String,String> payloadObj;
String firstProductID;
//Creating a method that returns a HashMap. Inside this, we create a Hashmap variable to store the payload key and value pairs
public Map<String,String> createPayLoadMap() {
payloadObj = new HashMap<String,String>();
payloadObj.put("name", "Nicholas iWatch");
payloadObj.put("price", "1399");
payloadObj.put("description", "watch series 7");
payloadObj.put("category_id", "3");
payloadObj.put("category_name", "Electronics");
return payloadObj;
}
@Test(priority=1)
public void createNewProduct() {
/*
given: all input details (base URI, Headers, Payload/Body, QueryParameters, Authorization)
when: submit api requests (HTTP method, Endpoint/Resource)
then: validate response (status code, responseHeaders, responseTime, Payload/Body)
*/
Response response = given()
.baseUri("https://techfios.com/api-prod/api/product")
.header("Content-Type","application/json; charset=UTF-8")
.header("Authorization","Bearer 0987654321") //using bearer token as authorization
.body(createPayLoadMap()) //taking the method that holds our stored hashmap
.relaxedHTTPSValidation().
when()
.post("/create.php"). //HTTP method: POST and the end point is: /create.php
then()
.extract().response();
//to validate response status
int actualResponseStatus = response.getStatusCode();
System.out.println("Response Status : " + actualResponseStatus);
Assert.assertEquals(actualResponseStatus, 201);
//to validate response header content type
String actualResponseContentType = response.getHeader("Content-Type");
System.out.println("Response Content Type : " + actualResponseContentType);
Assert.assertEquals(actualResponseContentType, "application/json; charset=UTF-8");
//to validate the response time
long actualResponseTime= response.getTimeIn(TimeUnit.MILLISECONDS);
System.out.println("Response Time : " + actualResponseTime);
if(actualResponseTime <=2000) {
System.out.println("Response time is within range");
}
else {
System.out.println("Response time is out of range !!!");
}
//to show the response body on the console
String actualResponseBody = response.getBody().asString();
System.out.println("Response Body: " + actualResponseBody);
JsonPath jp = new JsonPath(actualResponseBody);
//to validate the response message
String productMessage = jp.get("message");
System.out.println("Product Message : " + productMessage);
Assert.assertEquals(productMessage, "Product was created.");
}
@Test(priority=2)
public void getFirstProductID() {
Response response = given()
.baseUri("https://techfios.com/api-prod/api/product")
.header("Content-Type","application/json; charset=UTF-8")
.auth().basic("demo@techfios.com","abc123") //using basic authorization as username & password
.relaxedHTTPSValidation().
when()
.get("/read.php").
then()
.extract().response();
//to validate response status
int actualResponseStatus = response.getStatusCode();
System.out.println("Response Status : " + actualResponseStatus);
Assert.assertEquals(actualResponseStatus, 200);
//to validate the response body and show the first product ID is not null
String actualResponseBody = response.getBody().asString();
System.out.println("Response Body: " + actualResponseBody);
JsonPath jp = new JsonPath(actualResponseBody);
firstProductID = jp.get("records[0].id");
System.out.println("First Product ID : " + firstProductID);
}
@Test(priority=3)
public void validateProductDetails() {
Response response = given()
.baseUri("https://techfios.com/api-prod/api/product")
.header("Content-Type","application/json; charset=UTF-8")
.header("Authorization","Bearer 0987654321") //using bearer token as authorization
.queryParam("id", firstProductID)
.relaxedHTTPSValidation().
when()
.get("/read_one.php").
then()
.extract().response();
//to validate response status code
int actualResponseStatus = response.getStatusCode();
System.out.println("Actual Response Status : " + actualResponseStatus);
Assert.assertEquals(actualResponseStatus, 200);
//to validate the response body
String actualResponseBody = response.getBody().asString();
System.out.println("Actual Response Body: " + actualResponseBody);
//Created a JsonPath object: jp so that we can use this object later on to plug our key and return the corresponding value of the key
JsonPath jp = new JsonPath(actualResponseBody);
//List of variables coming from createPayLoadMap() method, that we will use later to assert (expected vs actual)
String expectedProductName = createPayLoadMap().get("name");
String expectedProductDescription = createPayLoadMap().get("description");
String expectedProductPrice = createPayLoadMap().get("price");
//Assertion of product name (name that we are reading from the system vs. name that we have entered into the system)
String actualProductName = jp.get("name");
System.out.println("Product Name : " + actualProductName);
Assert.assertEquals(actualProductName, expectedProductName);
//Assertion of Product Description (Description that we are reading from the system vs. Description that we have entered into the system)
String actualProductDescription = jp.get("description");
System.out.println("Product Description : " + actualProductDescription);
Assert.assertEquals(actualProductDescription, expectedProductDescription);
//Assertion of Product Price (Price that we are reading from the system vs. Price that we have entered into the system)
String actualProductPrice = jp.get("price");
System.out.println("Product Price : " + actualProductPrice);
Assert.assertEquals(actualProductPrice, expectedProductPrice);
}
}