API Automation

Now let’s get going with API Automation:

1.      Download the following jars:

      b. jackson-core-2.8.3jar

2.      Load the jar files into the project.

3.      Now time to write the code.


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
//STEP 1. Import required packages
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Map;

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

import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

//STEP 2: Create class and variables
public class APIautomation {

 private static final String filePath = "path to .json file"; 

 public static void main(String[] args) throws IOException, ParseException {
  try{
   ArrayList<String> allDistinctKeys = new ArrayList<String>();
   FileReader reader = new FileReader(filePath);

   JSONParser jsonParser = new JSONParser();
   JSONObject jsonObject = (JSONObject) jsonParser.parse(reader);
   System.out.println("jsonObject"+jsonObject);

   String objectString = jsonObject.toString();
   System.out.println("objectString"+objectString);

   //STEP 4: calling parser and iterating thru
   parse(objectString,allDistinctKeys,"");

   for(int i = 0; i < allDistinctKeys.size(); i++){
    System.out.println(allDistinctKeys.get(i));
   }
  }catch(Exception e){
   System.out.println("error e: "+e);
  }
 }

 //STEP 3: Create parser
 public static void parse(String json, ArrayList<String> allDistinctKeys, String parentName) 
   throws JsonProcessingException, IOException  {
  // this is the recursive call ...  

  JsonFactory factory = new JsonFactory();

  ObjectMapper mapper = new ObjectMapper(factory);
  JsonNode rootNode = mapper.readTree(json);  

  Iterator<Map.Entry<String,JsonNode>> fieldsIterator = rootNode.fields();
  if(parentName != "")
   parentName += ".";

  while(fieldsIterator.hasNext()){

   Map.Entry<String,JsonNode> field = fieldsIterator.next();
   String key = field.getKey();
   System.out.println("^Key-: " + key );
   JsonNode value = field.getValue();

   allDistinctKeys.add(parentName + key );

   if (value.isArray()){
    System.out.println("V-----> "+value.get(0).toString());       
    System.out.println("someKey in Array loop: "+key+"."+value.get(0).toString());

    parse(value.get(0).toString(),allDistinctKeys,parentName + key);
   } else if (value.isContainerNode()) {

    System.out.println("V*****> "+value.toString());
    System.out.println("someKey in Object loop: "+key);

    parse(value.toString(),allDistinctKeys,parentName + key); // RECURSIVE CALL
   } else {
    System.out.println("V~~~~~> " + value);
   }
  }
 }
}
 



4.      For the API URL, use the following code to convert it into readable file:


1
2
3
String url = "api url";
JSONParser jsonParser = new JSONParser();
JSONObject object = jsonParser.getJSONFromUrl(url);

5.      json-simple javadocs can be found here.

No comments:

Post a Comment