Skip to content

Commit fe7732d

Browse files
updated sad path scenarios for the update order PUT request (#30)
* added 4 new tests for update order sad paths * readme updated and updated the testng xml restful ecommerce * added new test block in testng xml to run sad path of update order that needs orders available in system
1 parent a68ab11 commit fe7732d

File tree

3 files changed

+89
-2
lines changed

3 files changed

+89
-2
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ This project is the outcome of my self-learning about the API Testing Automation
1515
- [Lombok](https://projectlombok.org/) has been used to generate Getter and Setters automatically for post body requests.
1616
- FAKE Rest APIs on [Reqres.in](https://reqres.in/) has been used for testing.
1717
- End to End scenarios have been added for the [restful booker APIs](https://restful-booker.herokuapp.com/apidoc/index.html).
18+
- Happy and Sad Path Scenarios have been added for the [restful-ecommerce APIs](https://github.com/mfaisalkhatri/restful-ecommerce)
1819

1920
## :hammer_and_wrench: Talking more about the Scenarios Covered in this project:
2021
You will get the answers to the following questions and its respective working code example with [Playwright Java](https://playwright.dev/java/docs/api-testing#writing-api-test) framework in this repository:
@@ -32,10 +33,14 @@ You will get the answers to the following questions and its respective working c
3233
- How to use `Lombok` for writing the builder pattern code?
3334
- How to use Builder Pattern for test data generation using [Data Faker](https://github.com/datafaker-net/datafaker)?
3435
- How to write end-to-end api tests?
36+
- How to write Happy Path scenarios for the APIs?
37+
- How to write Sad Path scenarios for the APIs?
3538

3639
## :writing_hand: Blog Links
3740
- [What is API Testing?](https://mfaisalkhatri.github.io/2020/08/08/apitesting/)
3841
- [How to perform End to End API Testing using Playwright with Java and TestNG](https://medium.com/@iamfaisalkhatri/how-to-perform-end-to-end-api-testing-using-playwright-with-java-and-testng-26b318927115)
42+
- [Playwright Java API Testing | How to test POST requests?](https://medium.com/@iamfaisalkhatri/playwright-java-api-testing-how-to-test-post-requests-4c9102d3ab03)
43+
- [Playwright Java API Testing | How to test GET requests?](https://medium.com/@iamfaisalkhatri/playwright-java-api-testing-how-to-test-get-requests-c036b984cc6d)
3944

4045
## :question: Need Assistance?
4146

src/test/java/io/github/mfaisalkhatri/api/restfulecommerce/SadPathTests.java

Lines changed: 72 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
import java.util.ArrayList;
1010
import java.util.List;
1111

12-
import static io.github.mfaisalkhatri.api.restfulecommerce.testdata.OrderDataBuilder.getNewOrder;
13-
import static io.github.mfaisalkhatri.api.restfulecommerce.testdata.OrderDataBuilder.getOrderDataWithMissingProductId;
12+
import static io.github.mfaisalkhatri.api.restfulecommerce.testdata.OrderDataBuilder.*;
13+
import static io.github.mfaisalkhatri.api.restfulecommerce.testdata.TokenBuilder.getCredentials;
1414
import static org.testng.Assert.assertEquals;
1515

1616
public class SadPathTests extends BaseTest {
@@ -103,4 +103,74 @@ public void testShouldNotFetchOrder_WhenNoOrderExistsForProductId() {
103103
assertEquals(responseObject.get("message"), "No Order found with the given parameters!");
104104
}
105105

106+
@Test
107+
public void testShouldNotUpdateOrder_WhenTokenIsMissing() {
108+
109+
int orderId = 1;
110+
111+
final OrderData updatedOrder = getUpdatedOrder();
112+
113+
final APIResponse response = this.request.put("/updateOrder/" + orderId, RequestOptions.create()
114+
.setData(updatedOrder));
115+
116+
final JSONObject responseObject = new JSONObject(response.text());
117+
118+
assertEquals(response.status(), 403);
119+
assertEquals(responseObject.get("message"), "Forbidden! Token is missing!");
120+
}
121+
122+
@Test
123+
public void testShouldNotUpdateOrder_WhenOrderIdIsNotFound() {
124+
final APIResponse authResponse = this.request.post("/auth", RequestOptions.create().setData(getCredentials()));
125+
126+
final JSONObject authResponseObject = new JSONObject(authResponse.text());
127+
final String token = authResponseObject.get("token").toString();
128+
129+
final OrderData updatedOrder = getUpdatedOrder();
130+
131+
final int orderId = 90;
132+
133+
final APIResponse response = this.request.put("/updateOrder/" + orderId, RequestOptions.create()
134+
.setHeader("Authorization", token)
135+
.setData(updatedOrder));
136+
137+
138+
final JSONObject responseObject = new JSONObject(response.text());
139+
140+
assertEquals(response.status(), 404);
141+
assertEquals(responseObject.get("message"), "No Order found with the given Order Id!");
142+
143+
}
144+
145+
@Test
146+
public void testShouldNotUpdateOrder_WhenOrderDetailsAreNotProvided() {
147+
final APIResponse authResponse = this.request.post("/auth", RequestOptions.create().setData(getCredentials()));
148+
149+
final JSONObject authResponseObject = new JSONObject(authResponse.text());
150+
final String token = authResponseObject.get("token").toString();
151+
152+
final int orderId = 2;
153+
154+
final APIResponse response = this.request.put("/updateOrder/" + orderId, RequestOptions.create()
155+
.setHeader("Authorization", token));
156+
157+
final JSONObject responseObject = new JSONObject(response.text());
158+
159+
assertEquals(response.status(), 400);
160+
assertEquals(responseObject.get("message"), "Each Order must have user_id, product_id, product_name, product_amount, qty, tax_amt, and total_amt!");
161+
}
162+
163+
@Test
164+
public void testShouldNotUpdateOrderWithInvalidToken() {
165+
final int orderId = 2;
166+
167+
final APIResponse response = this.request.put("/updateOrder/" + orderId, RequestOptions.create()
168+
.setHeader("Authorization", "token273678"));
169+
170+
final JSONObject responseObject = new JSONObject(response.text());
171+
172+
assertEquals(response.status(), 400);
173+
assertEquals(responseObject.get("message"), "Failed to authenticate token!");
174+
}
175+
106176
}

test-suite/testng-restfulecommerce.xml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111
<include name="testShouldNotFetchOrder_WhenNoOrderExistsForOrderId"/>
1212
<include name="testShouldNotFetchOrder_WhenNoOrderExistsForUserId"/>
1313
<include name="testShouldNotFetchOrder_WhenNoOrderExistsForProductId"/>
14+
<include name="testShouldNotUpdateOrder_WhenTokenIsMissing"/>
15+
<include name="testShouldNotUpdateOrder_WhenOrderIdIsNotFound"/>
16+
<include name="testShouldNotUpdateOrderWithInvalidToken"/>
1417
</methods>
1518
</class>
1619
</classes>
@@ -35,4 +38,13 @@
3538
</class>
3639
</classes>
3740
</test>
41+
<test name="Testing Sad Path Scenarios of Restful E-Commerce APIs after orders are created">
42+
<classes>
43+
<class name="io.github.mfaisalkhatri.api.restfulecommerce.SadPathTests">
44+
<methods>
45+
<include name="testShouldNotUpdateOrder_WhenOrderDetailsAreNotProvided"/>
46+
</methods>
47+
</class>
48+
</classes>
49+
</test>
3850
</suite>

0 commit comments

Comments
 (0)