All Downloads are FREE. Search and download functionalities are using the official Maven repository.

automation.library.api.core.APIHelper Maven / Gradle / Ivy

Go to download

The 'api' library contains a set of java helper classes that support api testing by leveraging Rest-Assured.

The newest version!
package automation.library.api.core;


import com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.testng.Assert;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathFactory;
import java.io.File;
import java.io.StringWriter;


public class APIHelper {
	
	public APIHelper(){}

    public static Document setDoc(String path) throws Exception{
	    DocumentBuilderFactory dbf = new DocumentBuilderFactoryImpl();
	    Document doc = dbf.newDocumentBuilder().parse(new File(path));
	    return doc;
    }

    public String getElement(Document doc, String path) throws Exception{
	    String val=null;
	    XPathFactory factory = XPathFactory.newInstance();
	    XPath xpath = factory.newXPath();
	    XPathExpression expr = xpath.compile(path);
	    Node el = (Node) expr.evaluate(doc, XPathConstants.NODE);
	
	    if (el !=null){
	                    val = el.getTextContent();
	    }
	
	    return val;
    }

    public static Document setElement(Document doc, String path, String val) throws Exception{
    	
    	XPathFactory factory = XPathFactory.newInstance();
        XPath xpath = factory.newXPath();
        XPathExpression expr = xpath.compile(path);
        Node el = (Node) expr.evaluate(doc, XPathConstants.NODE);


        if (el !=null){
	        if (val.equalsIgnoreCase("")){
	                        el.getParentNode().removeChild(el);
	        }else{
	                        el.setTextContent(val);
	        }
        }else {
            // int numnodes = getOccurences(path, "/");
            String parent = getParent(path);
            String child = getChild(path);
                            
            xpath = factory.newXPath();
            expr = xpath.compile(parent);
            el = (Node) expr.evaluate(doc, XPathConstants.NODE);
            
            Element el1 = doc.createElement(child);
            el.appendChild(el1);
            el1.setTextContent(val);

        }
        return doc;
        
    }

    public static String createRequestXML(Document doc) throws Exception {
        TransformerFactory tf = TransformerFactory.newInstance();
        Transformer transformer = tf.newTransformer();
        StringWriter writer = new StringWriter();
        transformer.transform(new DOMSource(doc),  new StreamResult(writer));
        return writer.getBuffer().toString();
    }

    public static JSONObject jsonMerge(JSONObject source, JSONObject target) throws JSONException {
        String[] keys = JSONObject.getNames(source);
            if (keys !=null){
                for (String key: keys) {
                	
                    Object value = source.get(key);
                    
                    if (!target.has(key)) {
                        target.put(key, value);
                    } else {
                    	if (value instanceof JSONObject) {
                    		JSONObject valueJson = (JSONObject)value;
                            jsonMerge(valueJson, target.getJSONObject(key));
                        } else {
                            target.put(key, value);
                        }
                    }
                }
            }
            return target;
    }

    public static JSONObject jsonToXML(Document doc, JSONObject source, String xpath) throws Exception {
        String[] keys = JSONObject.getNames(source);
        for (String key: keys) {   
            Object value = source.get(key);
            if (value instanceof JSONObject) {
                JSONObject valueJson = (JSONObject)value;
                jsonToXML(doc, valueJson, xpath+"/"+key);
            }else if (value instanceof JSONArray) {
                JSONArray array = (JSONArray) value;
                for (int i=0;i 0 && pos != -1)
            pos = str.indexOf(substr, pos + 1);
        return pos;
    }
                
    public static int getOccurences(String str, String substr){
	    int occurrences = 0;
	    for(char c : str.toCharArray()){
	       if(c == '/'){
	          occurrences++;
	       }
	    }
	    
	    return occurrences;
    }
    
    public static String getParent(String path){
        int occurences = getOccurences(path, "/");
        
        String parent = path.substring(0,ordinalIndexOf(path, "/", occurences));
        
        return parent;
    }
    
    public static String getChild(String path){
	    int occurences = getOccurences(path, "/");
	    
	    String child = path.substring(ordinalIndexOf(path, "/", occurences)+1,path.length());
	    
	    return child;
    }
                
                
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy