initial commit

parents
# Default ignored files
/shelf/
/workspace.xml
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="CompilerConfiguration">
<annotationProcessing>
<profile name="Maven default annotation processors profile" enabled="true">
<sourceOutputDir name="target/generated-sources/annotations" />
<sourceTestOutputDir name="target/generated-test-sources/test-annotations" />
<outputRelativeToContentRoot value="true" />
<module name="rest-assured-testing" />
</profile>
</annotationProcessing>
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="RemoteRepositoriesConfiguration">
<remote-repository>
<option name="id" value="central" />
<option name="name" value="Central Repository" />
<option name="url" value="https://repo.maven.apache.org/maven2" />
</remote-repository>
<remote-repository>
<option name="id" value="central" />
<option name="name" value="Maven Central repository" />
<option name="url" value="https://repo1.maven.org/maven2" />
</remote-repository>
<remote-repository>
<option name="id" value="jboss.community" />
<option name="name" value="JBoss Community repository" />
<option name="url" value="https://repository.jboss.org/nexus/content/repositories/public/" />
</remote-repository>
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ExternalStorageConfigurationManager" enabled="true" />
<component name="MavenProjectsManager">
<option name="originalFiles">
<list>
<option value="$PROJECT_DIR$/pom.xml" />
</list>
</option>
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" default="true" project-jdk-name="corretto-1.8" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
<component name="ProjectType">
<option name="id" value="jpab" />
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>rest-assured-testing</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>rest-assured</artifactId>
<version>5.1.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>7.4.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>RELEASE</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.9.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.vaadin.external.google</groupId>
<artifactId>android-json</artifactId>
<version>0.0.20131108.vaadin1</version>
<scope>test</scope>
</dependency>
</dependencies>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
</project>
\ No newline at end of file
import io.restassured.RestAssured;
import io.restassured.path.json.JsonPath;
import io.restassured.response.Response;
import io.restassured.specification.RequestSpecification;
import org.json.JSONException;
import org.json.JSONObject;
import org.testng.annotations.Test;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.is;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class RestAssuredTests {
// @Test
// void test1(){
// Response response = RestAssured.get("https://reqres.in/api/users?page=2");
// System.out.println("Response: " + response.asString());
// System.out.println("Status Code:" + response.getStatusCode());
// int statusCode = response.getStatusCode();
// Assert.assertEquals(statusCode, 200);
// }
//
// @Test
// void test2() {
// given().get("https://reqres.in/api/users?page=2").then().statusCode(200).log().all();
// }
@Test
void getStudentTest() {
given().get("http://localhost:8081/students/3").then().statusCode(200).log().all();
}
@Test
void getStudentsTest() {
RequestSpecification request = RestAssured.given();
Response response = request.get("http://localhost:8081/students/3");
System.out.println(response.getBody().asPrettyString());
}
@Test
void createStudentTest() throws JSONException {
RestAssured.baseURI = "http://localhost:8081/students";
RequestSpecification request = RestAssured.given();
JSONObject requestParams = new JSONObject();
requestParams.put("name", "Qadeer");
requestParams.put("rollNo", "57");
requestParams.put("age", "24");
requestParams.put("courseId", "5");
request.header("Content-Type", "application/json");
request.body(requestParams.toString());
Response response = request.post();
System.out.println(response.statusCode());
assertEquals(response.statusCode(), 200);
}
@Test
void updateStudentTest() throws JSONException {
RestAssured.baseURI = "http://localhost:8081/students";
RequestSpecification request = RestAssured.given();
JSONObject requestParams = new JSONObject();
requestParams.put("name", "Qadeer updated");
requestParams.put("rollNo", "57");
requestParams.put("age", "24");
requestParams.put("courseId", "5");
request.body(requestParams.toString());
request.header("Content-Type", "application/json");
Response response = request.put("/2");
System.out.println(response.statusCode());
}
@Test
void deleteStudentTest() throws JSONException {
RestAssured.baseURI = "http://localhost:8081/students";
RequestSpecification request = RestAssured.given();
Response response = request.delete("/2");
System.out.println(response.statusCode());
}
@Test
void getNonExistentStudentTest() {
given().get("http://localhost:8081/students/3").then().body("name", is("Qadeer"));
}
@Test
void updateNewStudentTest() throws JSONException {
RestAssured.baseURI = "http://localhost:8081/students";
RequestSpecification request = RestAssured.given();
JSONObject requestParams = new JSONObject();
requestParams.put("name", "Qadeer updated with patch");
requestParams.put("rollNo", "57");
requestParams.put("age", "24");
requestParams.put("courseId", "5");
request.body(requestParams.toString());
request.header("Content-Type", "application/json");
Response response = request.patch("/3");
System.out.println(response.statusCode());
assertEquals(response.statusCode(), 200);
}
@Test
public void CarMessageBodyTest()
{
RestAssured.baseURI = "https://vpic.nhtsa.dot.gov/api/vehicles/getallmakes?format=json";
RequestSpecification httpRequest = RestAssured.given();
Response response = httpRequest.get("");
// Retrieve the body of the Response
//
// given().get("https://vpic.nhtsa.dot.gov/api/vehicles/getallmakes?format=json").then().statusCode(200).log().all();
// System.out.println(response.then().body("Results.Make_ID", equalTo(440)));
JsonPath jsonPath = response.jsonPath();
String city = jsonPath.get("Message");
// System.out.println(body);
//// JSONObject myObject = new JSONObject(body.asPrettyString());
//
//
// // By using the ResponseBody.asString() method, we can convert the body
// // into the string representation.
// System.out.println("Response Body is: " + body.asPrettyString());
}
@Test
public void messageTest()
{
RestAssured.baseURI = "https://vpic.nhtsa.dot.gov/api/vehicles";
Response response = given().get("/getallmakes?format=json");
// First get the JsonPath object instance from the Response interface
JsonPath jsonPathEvaluator = response.jsonPath();
// Then simply query the JsonPath object to get a String value of the node
// specified by JsonPath: {"NodeName"}
ArrayList message = jsonPathEvaluator.get("Results");
String carName = null;
for (Object x: message) {
Integer y = (Integer) ((LinkedHashMap) x).get("Make_ID");
if (y == 446) {
carName = (String) ((LinkedHashMap) x).get("Make_Name");
}
}
assertEquals(carName, "EBR");
// Let us print the city variable to see what we got
// Validate the response
}
}
JSONObject requestParams = new JSONObject();
requestParams.put("id", "2");
requestParams.put("name", "Qadeer");
requestParams.put("rollNo", "2");
requestParams.put("age", "24");
requestParams.put("courseId", "5");
\ No newline at end of file
JSONObject requestParams = new JSONObject();
requestParams.put("id", "2");
requestParams.put("name", "Qadeer");
requestParams.put("rollNo", "2");
requestParams.put("age", "24");
requestParams.put("courseId", "5");
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment