Commit c30d3852 authored by Sujatha Thippeswamy's avatar Sujatha Thippeswamy

Sujatha commited all the XML parser files

parent 47398163
......@@ -21,28 +21,35 @@ public class ParseXMLSteps{
private ValidatableResponse xml;
private Util util =new Util();
private ParseXML parsexml = new ParseXML();
String NodeNames[]=null;
@When("^employee XML Updated data is uploaded through webservice$")
public void employee_XML_Updated_data_is_uploaded_through_webservice() throws Throwable {
StringBuffer NodeNames = new StringBuffer();
//Reads the XML file
File reqPayload = new File(util.getValue("employee_XML_correct_req_payload_loc"));
parsexml.ReadXMLFileFromLocal(reqPayload);
parsexml.getXMLNodes();
parsexml.UpdateXMLContent();
response = util.post_XMLData_employee(reqPayload);
//Parses the XML file and gets all the Nodes names
NodeNames = parsexml.ReadXMLFileFromLocal(reqPayload);
//Updates the node "name" in the XML file, along with random generated value so that name is unique
//Updates the XL document with the new value and saves in the XML file
parsexml.UpdateNodeNameValue("name","DEF " + (int) Math.ceil(Math.random() * 1000));
parsexml.UpdateNodeNameValue("lat","657");
//Passes the updated XML file as payload to the API
response = util.post_XMLData_employee(reqPayload);
}
@Then("^verify the success status code (\\d+) is generated$")
public void verify_the_success_status_code_is_generated(int arg1) {
xml = response.then().statusCode(arg1);
int statusCode = response.getStatusCode();
Assert.assertEquals(statusCode, 200);
xml = response.then().statusCode(arg1);
int statusCode = response.getStatusCode();
Assert.assertEquals(statusCode, 200);
}
@Then("^verify success message is generated$")
public void verify_success_message_is_generated() {
String successCode = response.xmlPath().get("SuccessCode");
Assert.assertEquals( "Correct Success code was returned", successCode, "OPERATION_SUCCESS");
String successCode = response.getBody().asString();
System.out.println(" The Sucess Code is :--" + successCode );
Assert.assertEquals( true, successCode.contains("OK"));
}
......
package com.qa.utilities;
import java.io.File;
import java.util.Iterator;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
......@@ -21,10 +22,11 @@ public class ParseXML {
private Util util =new Util();
Document doc= null;
NodeList nList;
//Read the XML file
public void ReadXMLFileFromLocal(File file) {
NodeList nList1;
//Read the XML file
public StringBuffer ReadXMLFileFromLocal(File file) {
StringBuffer NodeNames = new StringBuffer();
try {
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
......@@ -34,64 +36,60 @@ public class ParseXML {
catch(Exception e)
{ e.printStackTrace();}
NodeNames = visit(doc ,0);
return NodeNames;
}
//Get the Root Tag Staff from the XML file
public void getXMLNodes()
{
doc.getDocumentElement().normalize();
System.out.println("Root element :" + doc.getDocumentElement().getNodeName());
nList = doc.getElementsByTagName("staff");
//Helper Method Update the XML file for the given Node name and new value
public void UpdateNodeNameValue (String NodeName, String NewValue) {
UpdateNameValue(doc,0,NodeName,NewValue);
}
public void UpdateXMLContent()
{
//Read all the sub nodes from the root Node staff
for (int temp = 0; temp < nList.getLength(); temp++) {
Node nNode = nList.item(temp);
System.out.println("\nCurrent Element :" + nNode.getNodeName());
if (nNode.getNodeType() == Node.ELEMENT_NODE) {
//Prints all the sub nodes
Element eElement = (Element) nNode;
System.out.println("Staff id : " + eElement.getAttribute("id"));
System.out.println("First Name : " + eElement.getElementsByTagName("firstname").item(0).getTextContent());
System.out.println("Last Name : " + eElement.getElementsByTagName("lastname").item(0).getTextContent());
System.out.println("Nick Name : " + eElement.getElementsByTagName("nickname").item(0).getTextContent());
System.out.println("Salary : " + eElement.getElementsByTagName("salary").item(0).getTextContent());
System.out.println("----------------------------");
//Updates the Firstname node
Node firstNameNode = eElement.getElementsByTagName("firstname").item(0).getFirstChild();
firstNameNode.setNodeValue("GHI");
try
{
//Writes the new content to the XML file
doc.getDocumentElement().normalize();
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(new File("/Users/nisum/eclipse-workspace/Cuke/Cucumber-SpringBoot-Nisum_Practise-Projects/ParseJSONProject/sample.xml"));
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.transform(source, result);
System.out.println("XML file updated successfully");
}
catch(Exception e)
{
e.printStackTrace();
}
//Method for Updating the XML file for the given Node name and new value
//Method Traverses the entire XML documment to search for the nnode
public void UpdateNameValue(Node node, int level, String NodeName, String NewValue) {
StringBuffer NodeNames = new StringBuffer();
if(NodeName.equalsIgnoreCase(node.getNodeName()))
{
node.setTextContent(NewValue);
try
{
//Writes the new content to the XML file
doc.getDocumentElement().normalize();
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(new File(util.getValue("employee_XML_correct_req_payload_loc")));
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.transform(source, result);
System.out.println("XML file updated successfully");
}
catch(Exception e)
{
e.printStackTrace();
}
}
//Traverses the entire XML document
NodeList list = node.getChildNodes();
for (int i = 0; i < list.getLength(); i++) {
Node childNode = list.item(i);
UpdateNameValue(childNode, level + 1, NodeName, NewValue);
}
}
// Method for Traversing the XML node and returning all the node names
public static StringBuffer visit(Node node, int level) {
StringBuffer NodeNames = new StringBuffer();
System.out.println("Name: is "+node.getNodeName());
NodeNames = NodeNames.append(node.getNodeName());
System.out.println("Value: is "+node.getNodeValue());
NodeList list = node.getChildNodes();
for (int i = 0; i < list.getLength(); i++) {
Node childNode = list.item(i);
visit(childNode, level + 1);
}
return NodeNames;
}
}
......@@ -108,3 +106,8 @@ public class ParseXML {
......@@ -40,15 +40,12 @@ public class Util {
}
public Response post_XMLData_employee(File file) {
RestUtil.setreqPayload(file);
RestUtil.setContentType(getValue("content_XML_type"));
Response response = RestUtil.setPostwebserviceUrl(getValue("employee_base_uri"), getValue("employee_service_resurce_POST"));
Response response = RestUtil.setPostwebserviceUrl(getValue("employee_base_uri_xml"), getValue("employee_service_resurce_xml_POST"));
return response;
}
}
......@@ -7,5 +7,8 @@ employee_service_resurce_GET = /api/v1/employees
employee_service_resurce_POST = /api/v1/create
driver_base_uri = http://ergast.com/
driver_resources_uri = api/f1/drivers
employee_XML_correct_req_payload_loc=/Users/ashrivastava/Documents/nisumpoc/src/test/resources/sample.xml
employee_XML_correct_req_payload_loc=src/test/resources/sample.xml
employee_base_uri_xml= http://216.10.245.166
employee_service_resurce_xml_POST =/maps/api/place/add/xml?key= qaclick123
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<company>
<staff id="1001">
<firstname>PRIYANKS</firstname>
<lastname>mook kim</lastname>
<nickname>mkyong</nickname>
<salary>100000</salary>
</staff>
<staff id="2001">
<firstname>PRIYANKS</firstname>
<lastname>yin fong</lastname>
<nickname>fong fong</nickname>
<salary>200000</salary>
</staff>
</company>
<root>
<location>
<lat>657</lat>
<lng>33.427362</lng>
</location>
<accuracy>50</accuracy>
<name>DEF 999</name>
<phone_number>(+91) 983 893 3937</phone_number>
<address>Anna Salai, Chennai</address>
<types>shoe park</types>
<types>kadai</types>
<website>http://google.com</website>
<language>tamil-IN</language>
</root>
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