Notes![what is notes.io? What is notes.io?](/theme/images/whatisnotesio.png)
![]() ![]() Notes - notes.io |
package ApiTesting;
import org.json.JSONArray;
import org.json.JSONObject;
import org.testng.annotations.Test;
import io.restassured.http.ContentType;
import io.restassured.response.Response;
import junit.framework.Assert;
import org.testng.annotations.Test;
import static io.restassured.RestAssured.*;
import static io.restassured.matcher.RestAssuredMatchers.*;
import static org.hamcrest.Matchers.*;
import static org.testng.Assert.assertEquals;
import java.util.HashMap;
public class ApiwithRest {
@Test(priority=1)
void getUsers() {
//get request
given()
.contentType("ContentType/JSON")
.when ()
.get("http://localhost:3000/books")
.then ()
.statusCode(200)
.body("[1].author",equalTo("George Orwell"))
.body("[2].id",equalTo("3"));
//Approach 2 // to capture the response in the variable and then work on it with different validations
@Test(priority=1)
void getUsers() {
Response res=given()
.contentType("ContentType.JSON")
.when ()
.get("http://localhost:3000/books");
Assert.assertEquals(res.getStatusCode(), 200); //Validation one
// get validation of status code
// verify header from response
Assert.assertEquals(res.header("Content-Type"), "application.json");
//verify json path
String bookname= res.jsonPath().get("[1].author").toString(); //This is in the object format and converted to string
Assert.assertEquals(bookname, "George Orwell");
//Approach 3 // Front each object/json file capture the title and check the title in json or validation basically
//JSONOBJECT dependency
@Test(priority=1)
void Getusersinfo() {
Response res=given() // this is reponse type and need to convert it to string
.contentType(ContentType.JSON)
.when ()
.get("http://localhost:3000/books"); // a box full of boks go fetch it
// TO GET ALL THE VALUES OF A TITLE, OR ANYTHING PRESENT
JSONArray jsonArray = new JSONArray(res.asString());//Open the Box: Once we get the box, we open it and look inside to see all the books.
// Print all the titles from JSON array //Look at Each Book's Title: We then look at each book in the box and read its title out loud.
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject book = jsonArray.getJSONObject(i);
String bookTitle = book.getString("author");
System.out.println(bookTitle);
//approach 4
// to compare the title found in json
// TO GET OR COMPARE THE VALUE OF JSON OBJECT OR THE OBJECT ON SHEET
JSONArray jsonArray = new JSONArray(res.asString());
boolean status = false;
// Loop through the JSON array to check if any book has the title "1984"
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject book = jsonArray.getJSONObject(i);
if (book.getString("title").equals("1994")) {
status = true;
break;
}
}
// Assert that the title "1984" is found
Assert.assertEquals(status, true);
//approach5
//Validate total price of books
// Convert the response to a JSON array
JSONArray jsonArray1 = new JSONArray(res.asString());
double totalYears = 0;
// Loop through the JSON array to sum up the 'year' values
for (int i = 0; i < jsonArray1.length(); i++) {
JSONObject book = jsonArray1.getJSONObject(i);
int year = book.getInt("year"); // Directly get the year as an integer
totalYears = totalYears + year;
}
// Print the total sum of the 'year' values
System.out.println(totalYears);
Assert.assertEquals(totalYears, 7785);
}
}
![]() |
Notes is a web-based application for online taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000+ notes created and continuing...
With notes.io;
- * You can take a note from anywhere and any device with internet connection.
- * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
- * You can quickly share your contents without website, blog and e-mail.
- * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
- * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.
Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.
Easy: Notes.io doesn’t require installation. Just write and share note!
Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )
Free: Notes.io works for 14 years and has been free since the day it was started.
You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;
Email: [email protected]
Twitter: http://twitter.com/notesio
Instagram: http://instagram.com/notes.io
Facebook: http://facebook.com/notesio
Regards;
Notes.io Team