Commit f3e1cd4f authored by Sujatha Thippeswamy's avatar Sujatha Thippeswamy

Sujatha Updated files for XML payload

parent 30e2a4a9
...@@ -21,28 +21,35 @@ public class ParseXMLSteps{ ...@@ -21,28 +21,35 @@ 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
response = util.post_XMLData_employee(reqPayload); //Updates the XL document with the new value and saves in the XML file
parsexml.UpdateNodeNameValue("name","ABC " + (int) Math.ceil(Math.random() * 1000));
parsexml.UpdateNodeNameValue("lat","420");
//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$") @Then("^verify the success status code (\\d+) is generated$")
public void verify_the_success_status_code_is_generated(int arg1) { public void verify_the_success_status_code_is_generated(int arg1) {
xml = response.then().statusCode(arg1); xml = response.then().statusCode(arg1);
int statusCode = response.getStatusCode(); int statusCode = response.getStatusCode();
Assert.assertEquals(statusCode, 200); Assert.assertEquals(statusCode, 200);
} }
@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
public void ReadXMLFileFromLocal(File file) {
//Read the XML file
public StringBuffer ReadXMLFileFromLocal(File file) {
StringBuffer NodeNames = new StringBuffer();
try { try {
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
...@@ -34,64 +36,60 @@ public class ParseXML { ...@@ -34,64 +36,60 @@ 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
//Read all the sub nodes from the root Node staff public void UpdateNameValue(Node node, int level, String NodeName, String NewValue) {
for (int temp = 0; temp < nList.getLength(); temp++) { StringBuffer NodeNames = new StringBuffer();
Node nNode = nList.item(temp); if(NodeName.equalsIgnoreCase(node.getNodeName()))
System.out.println("\nCurrent Element :" + nNode.getNodeName()); {
node.setTextContent(NewValue);
if (nNode.getNodeType() == Node.ELEMENT_NODE) { try
//Prints all the sub nodes {
Element eElement = (Element) nNode; //Writes the new content to the XML file
System.out.println("Staff id : " + eElement.getAttribute("id")); doc.getDocumentElement().normalize();
System.out.println("First Name : " + eElement.getElementsByTagName("firstname").item(0).getTextContent()); TransformerFactory transformerFactory = TransformerFactory.newInstance();
System.out.println("Last Name : " + eElement.getElementsByTagName("lastname").item(0).getTextContent()); Transformer transformer = transformerFactory.newTransformer();
System.out.println("Nick Name : " + eElement.getElementsByTagName("nickname").item(0).getTextContent()); DOMSource source = new DOMSource(doc);
System.out.println("Salary : " + eElement.getElementsByTagName("salary").item(0).getTextContent()); StreamResult result = new StreamResult(new File(util.getValue("employee_XML_correct_req_payload_loc")));
System.out.println("----------------------------"); transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.transform(source, result);
//Updates the Firstname node System.out.println("XML file updated successfully");
Node firstNameNode = eElement.getElementsByTagName("firstname").item(0).getFirstChild(); }
firstNameNode.setNodeValue("GHI"); catch(Exception e)
{
try e.printStackTrace();
{
//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();
}
} }
}
//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 { ...@@ -108,3 +106,8 @@ public class ParseXML {
...@@ -40,11 +40,10 @@ public class Util { ...@@ -40,11 +40,10 @@ 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"), getValue("employee_service_resurce_POST")); Response response = RestUtil.setPostwebserviceUrl(getValue("employee_base_uri_xml"), getValue("employee_service_resurce_xml_POST"));
return response; return response;
} }
} }
...@@ -7,5 +7,10 @@ employee_service_resurce_GET = /api/v1/employees ...@@ -7,5 +7,10 @@ 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=/com-nisum-poc/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>420</lat>
<lastname>mook kim</lastname> <lng>33.427362</lng>
<nickname>mkyong</nickname> </location>
<salary>100000</salary> <accuracy>50</accuracy>
</staff> <name>ABC 419</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