Commit c30d3852 authored by Sujatha Thippeswamy's avatar Sujatha Thippeswamy

Sujatha commited all the XML parser files

parent 47398163
...@@ -21,14 +21,20 @@ public class ParseXMLSteps{ ...@@ -21,14 +21,20 @@ public class ParseXMLSteps{
private ValidatableResponse xml; private ValidatableResponse xml;
private Util util =new Util(); private Util util =new Util();
private ParseXML parsexml = new ParseXML(); private ParseXML parsexml = new ParseXML();
String NodeNames[]=null;
@When("^employee XML Updated data is uploaded through webservice$") @When("^employee XML Updated data is uploaded through webservice$")
public void employee_XML_Updated_data_is_uploaded_through_webservice() throws Throwable { 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")); File reqPayload = new File(util.getValue("employee_XML_correct_req_payload_loc"));
parsexml.ReadXMLFileFromLocal(reqPayload); //Parses the XML file and gets all the Nodes names
parsexml.getXMLNodes(); NodeNames = parsexml.ReadXMLFileFromLocal(reqPayload);
parsexml.UpdateXMLContent(); //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); response = util.post_XMLData_employee(reqPayload);
} }
...@@ -41,8 +47,9 @@ public class ParseXMLSteps{ ...@@ -41,8 +47,9 @@ public class ParseXMLSteps{
@Then("^verify success message is generated$") @Then("^verify success message is generated$")
public void verify_success_message_is_generated() { public void verify_success_message_is_generated() {
String successCode = response.xmlPath().get("SuccessCode"); String successCode = response.getBody().asString();
Assert.assertEquals( "Correct Success code was returned", successCode, "OPERATION_SUCCESS"); System.out.println(" The Sucess Code is :--" + successCode );
Assert.assertEquals( true, successCode.contains("OK"));
} }
......
package com.qa.utilities; package com.qa.utilities;
import java.io.File; import java.io.File;
import java.util.Iterator;
import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.DocumentBuilderFactory;
...@@ -21,10 +22,11 @@ public class ParseXML { ...@@ -21,10 +22,11 @@ public class ParseXML {
private Util util =new Util(); private Util util =new Util();
Document doc= null; Document doc= null;
NodeList nList; NodeList nList;
NodeList nList1;
//Read the XML file //Read the XML file
public void ReadXMLFileFromLocal(File file) { public StringBuffer ReadXMLFileFromLocal(File file) {
StringBuffer NodeNames = new StringBuffer();
try { try {
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
...@@ -34,39 +36,22 @@ public class ParseXML { ...@@ -34,39 +36,22 @@ public class ParseXML {
catch(Exception e) catch(Exception e)
{ e.printStackTrace();} { e.printStackTrace();}
NodeNames = visit(doc ,0);
return NodeNames;
} }
//Get the Root Tag Staff from the XML file //Helper Method Update the XML file for the given Node name and new value
public void getXMLNodes() public void UpdateNodeNameValue (String NodeName, String NewValue) {
{ UpdateNameValue(doc,0,NodeName,NewValue);
doc.getDocumentElement().normalize();
System.out.println("Root element :" + doc.getDocumentElement().getNodeName());
nList = doc.getElementsByTagName("staff");
} }
public void UpdateXMLContent() //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()))
{ {
//Read all the sub nodes from the root Node staff node.setTextContent(NewValue);
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 try
{ {
//Writes the new content to the XML file //Writes the new content to the XML file
...@@ -74,7 +59,7 @@ public class ParseXML { ...@@ -74,7 +59,7 @@ public class ParseXML {
TransformerFactory transformerFactory = TransformerFactory.newInstance(); TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer(); Transformer transformer = transformerFactory.newTransformer();
DOMSource source = new DOMSource(doc); DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(new File("/Users/nisum/eclipse-workspace/Cuke/Cucumber-SpringBoot-Nisum_Practise-Projects/ParseJSONProject/sample.xml")); StreamResult result = new StreamResult(new File(util.getValue("employee_XML_correct_req_payload_loc")));
transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.transform(source, result); transformer.transform(source, result);
System.out.println("XML file updated successfully"); System.out.println("XML file updated successfully");
...@@ -83,17 +68,35 @@ public class ParseXML { ...@@ -83,17 +68,35 @@ public class ParseXML {
{ {
e.printStackTrace(); 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;
} }
}
}
......
...@@ -40,15 +40,12 @@ public class Util { ...@@ -40,15 +40,12 @@ public class Util {
} }
public Response post_XMLData_employee(File file) {
public Response post_XMLData_employee(File file) {
RestUtil.setreqPayload(file); RestUtil.setreqPayload(file);
RestUtil.setContentType(getValue("content_XML_type")); RestUtil.setContentType(getValue("content_XML_type"));
Response response = RestUtil.setPostwebserviceUrl(getValue("employee_base_uri_xml"), getValue("employee_service_resurce_xml_POST"));
Response response = RestUtil.setPostwebserviceUrl(getValue("employee_base_uri"), getValue("employee_service_resurce_POST"));
return response; return response;
} }
} }
...@@ -7,5 +7,8 @@ employee_service_resurce_GET = /api/v1/employees ...@@ -7,5 +7,8 @@ employee_service_resurce_GET = /api/v1/employees
employee_service_resurce_POST = /api/v1/create employee_service_resurce_POST = /api/v1/create
driver_base_uri = http://ergast.com/ driver_base_uri = http://ergast.com/
driver_resources_uri = api/f1/drivers 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"?> <?xml version="1.0" encoding="UTF-8" standalone="no"?>
<company> <root>
<staff id="1001"> <location>
<firstname>PRIYANKS</firstname> <lat>657</lat>
<lastname>mook kim</lastname> <lng>33.427362</lng>
<nickname>mkyong</nickname> </location>
<salary>100000</salary> <accuracy>50</accuracy>
</staff> <name>DEF 999</name>
<staff id="2001"> <phone_number>(+91) 983 893 3937</phone_number>
<firstname>PRIYANKS</firstname> <address>Anna Salai, Chennai</address>
<lastname>yin fong</lastname> <types>shoe park</types>
<nickname>fong fong</nickname> <types>kadai</types>
<salary>200000</salary> <website>http://google.com</website>
</staff> <language>tamil-IN</language>
</company> </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