0% found this document useful (0 votes)
59 views15 pages

API Testing

Testing

Uploaded by

MEERA SUPERSTORE
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
59 views15 pages

API Testing

Testing

Uploaded by

MEERA SUPERSTORE
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

API TESTING

API---------Application Programming Interface.


Local level interaction is nothing but API.
APITESTING:
API testing is a type of software testing that involves testing application
programming interfaces (APIs)
API TESTING

WEBSERVICE:
Throught the network interaction is nothing but webservices.
Eg:MakeMyTrip
API TESTING

XML (Extensible Markup Language)


JAXB is used for (Read & Write) the Xml without JAR
1. XML Read
2. XML Write
XML Read
1. Create Java Project.
2. Create POJO class for each parent tag and sub tags.
3. Mark the Parent tag as @XmlRootElement
4. Write a code to read the values from Xml.
4.1 Create the JAXB (Java Architecture Xml Binding)
4.2 Create the Unmarsheller
4.3 Mention the Xml path inside the unmarshal (M)
4.4 Get the values from Xml and Print the values
XML File
Project Right click other Xml File
<?xml version="1.0" encoding="UTF-8"?>
<productDetails>
<product>
<name>OnePlus TV</name>
<id>533</id>
<price>54000</price>
</product>
<product>
<name>Mi 6 Pro</name>
<id>633</id>
<price>10000</price>
</product>
<product>
<name>Leanovo Lap</name>
<id>952</id>
<price>43000</price>
</product>
<product>
<name>OnePlus7</name>
<id>123</id>
<price>38000</price>
</product>
</productDetails>
API TESTING

/*******************POJO CLASS FOR SUB TAG(Product)*************/


package org.test;

public class Product {


private String name;
private int id;
private int price;

// Generate getters&setters
public String getName() {
return name;
}

public void setName(String name) {


this.name = name;
}

public int getId() {


return id;
}

public void setId(int id) {


this.id = id;
}

public int getPrice() {


return price;
}

public void setPrice(int price) {


this.price = price;
}
}
}

/***********POJO CLASS FOR PARENT TAG(ProductDetails)*************/


package org.test;
import java.util.List;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
public class ProductDetails {

private List<Product> product;


// Generate getters&setters
public List<Product> getProduct() {
return product;
}

public void setProduct(List<Product> product) {


this.product = product;
}
}
API TESTING

MAIN CLASS

package org.test;

import java.io.File;
import java.util.List;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;

public class Conversion {


public static void main(String[] args) {
readXML();
}

public static void readXML() {

try {
// Create JAXBContext
JAXBContext context =
JAXBContext.newInstance(ProductDetails.class);
// Create Unmarshaller for Xml read
Unmarshaller un = context.createUnmarshaller();
// Create File and pass the Xml path
File xmlLoc = new File("C:\\Users\\k_sur\\eclipse-
workspace\\API\\src\\Product.xml");
// Pass the File location inside the unmarshal method
Object obj = un.unmarshal(xmlLoc);
// Type cast
ProductDetails p = (ProductDetails) obj;
List<Product> product = p.getProduct();
System.out.println("*****product details*****");
for (Product product1 : product) {
System.out.println("Nmae:" + product1.getName());
System.out.println("id:" + product1.getId());
System.out.println("Price:" + product1.getPrice());
}

} catch (JAXBException e) {
e.printStackTrace();
}
}
}
API TESTING

XML Write
1. Create Java project
2. Create POJO class for each parent tag and sub tags.
3. Mark the Parent tag as @XmlRootElement
4. Write a code to write the values in Xml.
4.1 Create the JAXB (Java Architecture Xml Binding)
4.2 Create the marsheller
4.3 Mention the Xml path (store)
4.4 Set the values to the Xml path
4.5 Inside the marsheller (M) we need to mention the Xml path
/*******************POJO CLASS FOR SUB TAG(Product)*************/
package org.test;

public class Product {


private String name;
private int id;
private int price;

// Generate getters&setters
public String getName() {
return name;
}

public void setName(String name) {


this.name = name;
}

public int getId() {


return id;
}

public void setId(int id) {


this.id = id;
}

public int getPrice() {


return price;
}

public void setPrice(int price) {


this.price = price;
}

}
API TESTING

/***********POJO CLASS FOR PARENT TAG(ProductDetails)*************/


package org.test;

import java.util.List;

import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
public class ProductDetails {

private List<Product> product;

// Generate getters&setters
public List<Product> getProduct() {
return product;
}

public void setProduct(List<Product> product) {


this.product = product;
}
}

MAIN CLASS
package org.test;

import java.io.File;
import java.util.LinkedList;
import java.util.List;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;

public class XmlWrite {


public static void main(String[] args) {
writeXML();
}

public static void writeXML() {

try {
// Create JAXBContext context
JAXBContext context =
JAXBContext.newInstance(ProductDetails.class);
// Create Marshaller for Xml Write
Marshaller mn = context.createMarshaller();
// Create File and for store the XmlFile
File f = new File("C:\\Users\\k_sur\\eclipse-
workspace\\API\\src\\output.xml");
// Set the values inside the Product
Product p = new Product();
p.setId(533);
API TESTING

p.setName("OnePlus TV");
p.setPrice(54000);
Product p1 = new Product();
p1.setId(633);
p1.setName("Mi 6 Pro");
p1.setPrice(10000);
Product p2 = new Product();
p2.setId(952);
p2.setName("Leanovo Lap");
p2.setPrice(43000);
Product p3 = new Product();
p3.setId(533);
p3.setName("OnePlus7");
p3.setPrice(38000);
// Create List add all the Product in List
List li = new LinkedList();
li.add(p);
li.add(p1);
li.add(p2);
li.add(p3);
// Add the list of Product into ProductDetails
ProductDetails pd = new ProductDetails();
pd.setProduct(li);
// Pass the object and File Location inside the marshal
mn.marshal(pd, f);

} catch (JAXBException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

WRITTED FILE
API TESTING

JSON (JavaScript Object Notation


JSON File is Keys&Value combination.
JSONObject: key, value (Map)
JSONArray: List
1. JSON Read
1. Create Maven project add one dependency json-simple (1.1.1)
2. Create JSON parser
3. Create FileReader and mention JSON path inside the FileReader
4. Pass the FileReader Location to inside the parse (M)
5. Type cast into JSONObject and get the values using key
6. If the values are inside the array means we need type cast JSONObject
into JSONArray and then get the values using index.
JSON FILE: ProjectRight clickFileFileNmae.json
{
"employee": [
{
"firstName": "Jhon",
"lastName": "Doe"
},
{
"firstName": "Anna",
"lastName": "Smith"
},
{
"firstName": "Peter",
"lastName": "Jones"
}
],
"manager": [
{
"firstName": "Jhon",
"lastName": "Doe"
},
{
"firstName": "Anna",
"lastName": "Smith"
},
{
"firstName": "Peter",
"lastName": "Jones"
}
]
}
API TESTING

MAIN CLASS
package org.test;

import java.io.FileReader;
import java.io.IOException;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;

public class JsonRead {


public static void main(String[] args) throws IOException, ParseException {
// Create JSONParser
JSONParser p = new JSONParser();
// Create FileReader for Read the JSON
FileReader f = new FileReader("C:\\Users\\k_sur\\eclipse-
workspace\\Test\\src\\test\\resources\\hello.json");
// Pass the FileReader Location in parser(M)
Object o = p.parse(f);
// Type cast object into JSONObject
JSONObject js = (JSONObject) o;
// get the value using key
Object o1 = js.get("employee");
System.out.println("****Employee****");
//Type cast JSONObject into JSONArray becase values are inside the array
JSONArray ja = (JSONArray) o1;
// get the 0th index
Object o11 = ja.get(0);
// Type cast the Object into JSONObject becase (keys,values)
JSONObject js11 = (JSONObject) o11;
System.out.println("firstName:" + js11.get("firstName"));
System.out.println("lastName:" + js11.get("lastName"));
// get the 1st index
Object o12 = ja.get(1);
// Type cast the Object into JSONObject becase (keys,values)
JSONObject js12 = (JSONObject) o12;
System.out.println("firstName:" + js12.get("firstName"));
System.out.println("lastName:" + js12.get("lastName"));
// get the 2nd index
Object o13 = ja.get(2);
// Type cast the Object into JSONObject becase (keys,values)
JSONObject js13 = (JSONObject) o13;
System.out.println("firstName:" + js13.get("firstName"));
System.out.println("lastName:" + js13.get("lastName"));
// get the value using key
Object o2 = js.get("manager");
System.out.println("****Manager****");
// Type cast JSONObject into JSONArray becase values are inside the array
JSONArray ja1 = (JSONArray) o2;
// get the 0th index
Object o21 = ja1.get(0);
// Type cast the Object into JSONObject becase (keys,values)
API TESTING

JSONObject js21 = (JSONObject) o21;


System.out.println("firstName:" + js21.get("firstName"));
System.out.println("lastName:" + js21.get("lastName"));
// get the 1st index
Object o22 = ja1.get(1);
// Type cast the Object into JSONObject becase (keys,values)
JSONObject js22 = (JSONObject) o22;
System.out.println("firstName:" + js22.get("firstName"));
System.out.println("lastName:" + js22.get("lastName"));
// get the 2nd index
Object o23 = ja1.get(2);
// Type cast the Object into JSONObject becase (keys,values)
JSONObject js23 = (JSONObject) o23;
System.out.println("firstName:" + js23.get("firstName"));
System.out.println("lastName:" + js23.get("lastName"));

2. JSON Write
1. Create Maven project add one dependency json-simple (1.1.1)
2. Create JSONObject and then put the key, value
3. If keys and values are inside the array means create JSONArray we need
to declare map Using map only put the keys, values and then add the map into
JSONArray.
4. Write the JSON in file
4.1 Create File
4.2 Create PrintWriter and pass the File location inside the
PrintWriter.
4.3 Write the JSON using write (M)
4.4 flush (M), close (M) is used for processing the file
MANI CLASS
package org.test;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.LinkedHashMap;
import java.util.Map;
import org.json.simple.JSONArray;
API TESTING

import org.json.simple.JSONObject;

public class JsonWrite {


public static void main(String[] args) throws FileNotFoundException {
// Create JSONObject for employee
JSONObject js = new JSONObject();
// Create JSONArray for employee
JSONArray ja = new JSONArray();
// Inside the JSONArray keys and values so declare Map
// Map for 1st index
Map m = new LinkedHashMap();
m.put("firstName", "Jhon");
m.put("lastName", "Doe");
// Map for 2nd index
Map m1 = new LinkedHashMap();
m1.put("firstName", "Anna");
m1.put("lastName", "Smith");
// Map for 3rd index
Map m2 = new LinkedHashMap();
m2.put("firstName", "Peter");
m2.put("lastName", "Jones");
// Add the Maps into JSONArray
ja.add(m);
ja.add(m1);
ja.add(m2);
js.put("employee", ja);
// Create JSONObject for manager
JSONObject js1 = new JSONObject();
// Create JSONArray for manger
JSONArray ja1 = new JSONArray();
// Inside the JSONArray keys and values so declare Map
// Map for 1st index
Map m11 = new LinkedHashMap();
m11.put("firstName", "Jhon");
m11.put("lastName", "Doe");
// Map for 2nd index
Map m12 = new LinkedHashMap();
m12.put("firstName", "Anna");
m12.put("lastName", "Smith");
// Map for 3rd index
Map m21 = new LinkedHashMap();
m21.put("firstName", "Peter");
m21.put("lastName", "Jones");
// Add the Maps into JSONArray
ja1.add(m11);
ja1.add(m12);
ja1.add(m21);
js1.put("manager", ja1);
System.out.println(js1);
// employee and manager both inside the JSONObject so create JSONObject
JSONObject js2 = new JSONObject();
// Using putAll put the values
js2.putAll(js);
js2.putAll(js1);
API TESTING

System.out.println(js2);
// Create File for storing the JSON
File loc = new File("C:\\Users\\k_sur\\eclipse-
workspace\\Test\\src\\test\\resources\\hello.json");
// PrintWriter for write the JSON inside the File
PrintWriter p = new PrintWriter(loc);
p.write(js2.toJSONString());
p.flush();
p.close();

}
}

WRITTED JSON FILE


API TESTING

1. URI – Universal Resource Identifier


2. URL – Uniform Resource Locator
3. Endpointes
4. Endpoints with parameters
Query parameter
Path parameter
5. HTTP Response
URI
https://reqres.in/api/users?page=2
URL
https://reqres.in
Endpoints
api/users?page=2
1. Query parameter
api/users?page=2
2. Path parameter
api/users/page=2
Request types
GET - Retrive (Facebook Login)
POST - Create (Create Facebook Id)
PUT - Update (Change password In Facebook)
DELETE - Delete (Facebook Account Delete)
Response code
1. Informational response (100-199)
2. Successful response (200-299)
3. Redirect (300-399)
4. Client Errors (400-499)
5. Server Errors (500-599)
API TESTING

You might also like