Sunday, 6 August 2023

API testing

* Difference between API ( jar files accessed locally)  and Web Service ( API accessed over the internet are web services)

* Difference between REST ( ?  ) and SOAP ( ? ) 

https://jsonEditorOnline.org/

https://www.jsonSchema2pojo.org/

https://jsonDiff.org/

Maven dependencies :

  1. HTTPclient ? 
  2. HTTPcore  ? 
  3. JSON
  4. TestNG

Static Imports for RestAssured:

import static io.restassured.RestAssured.* ;

import static io.restassured.matcher.RestAssuredMatchers.* ;

import static org.hamcrest.Matchers.* ;


Standard Script:

Response response =

         given().auth().contentType(ContentType.JSON)

.basic("sk_test_51HUS4uHSHj7PrP7O8TJxytR1TfTICVAvuHm", "") // token , pswd

        .header("Authorization", "Bearer sk_test_51HUS4uH 7O8TJxytR1")

.formParam("' ,"") // as per the api documentation only

.formParam("' ,"") // as per the api documentation only

        .param("limit", 3)

.get("https://api.stripe.com/v1/customers");


response.prettyPrint();

//System.out.println(response.asString());

System.out.println(response.getStatusCode());

Two ways to print Response : 

  1. response.prettyPrint();
  2. System.out.println(response.asString()); 
  3. String access_token = response.jsonPath().get("access_token").toString();
  4. String actual_ID = jsonObject.get("id").toString();  ?

Authentication Types:

  1. Basic Auth
  2. Bearer Token 
  3. OAuth 2.0


Maven Dependencies :

<dependency>

<groupId>io.rest-assured</groupId>

<artifactId>rest-assured</artifactId>

<version>4.1.2</version>

<scope>test</scope>

</dependency>


<dependency>

<groupId>javax.mail</groupId>

<artifactId>mail</artifactId>

<version>1.4.7</version>

</dependency>


<dependency>

<groupId>org.testng</groupId>

<artifactId>testng</artifactId>

<version>6.14.3</version>

<scope>test</scope>

</dependency>


<dependency>

<groupId>org.json</groupId>

<artifactId>json</artifactId>

<version>20190722</version>

</dependency>


<dependency>

<groupId>com.fasterxml.jackson.core</groupId>

<artifactId>jackson-databind</artifactId>

<version>2.10.0</version>

</dependency>


json obj {} --> hashMap   --> create class
Json Array [] --> ArrayList

Methods to create complex JSON file on run time :
1. HashMAP and ArrayList
2. JSONObject
3. POJO class  -  plain old java object     --> Best Approach  -- video #98  
 

Validation :

JSONObject jsonObject = new JSONObject( response.asString());
Assert.assertTrue(TestUtil.jsonHasKey(response.asString(), "id"), "ID key is not present in the JSON response"); 

Response Codes range:
1. Information
2. Success
3. Redirection
4. Client Error
5. Server Error 

* it is most important to have an "exact" version of poi dependencies if you are taking  ExceReader.java from another project   
* it is important to have all the required dependencies 



POJO Class: 
//1. create user.java class ;  
private String name;   //2. define instance private variables  
private String roll;
private Address address;
private ArrayList <Integer> mobile;
//3.  create class constructors with input parameters as variables 
public user( String name, String roll, String flatNo, String City)
{ this.name = name ;
this.roll = roll;
this.mobile = new ArrayList<Integer>() ;
this.address = new Address(flatNo, City);  }

public void setMobileNumbers( int ... mobileNumbers)  //4.  define arrayList if needed
{  for( int i=0; i <mobileNumbers.length ; i++)
       { this.mobile .add(mobileNumbers[i]); }
}

// 5. create one class for one json object  

public String getName()   //6. define getter  
{ return name; }
public void setName(String name) //7. define  setter functions
{ this.name = name; }

// 8. create PassJSONUsingPOJP.java class
User user = new User( "ravi", 101);
Response response = given().contentType(ContentType.JSON).body(user).log().all().post("https://reqres.in");
 System.out.println(response.getString());

No comments:

Post a Comment