rest assured - I need this piece of Java code to send all the XML files in the directory, is there a way to loop through each XML file -
i have piece of java restassured code that pulls top 5 ids db , uses in xml file creating 5 xml files.
then picking each xml file directory individually , sending server need send them 1 one.
public class sendplaylistacknowledgment { properties prop = new properties(); // creating prop object global variable @beforetest public void getdata() throws ioexception{ fileinputstream f = new fileinputstream("d:\\tools\\workspace\\bxf\\src\\files\\config.properties"); //creating f object prop.load(f); // load file object prop file //in code below use config values set in .property file } @suppresswarnings("unused") @test public void postdata() throws ioexception{ string postdata = generatestringfromresource("c:\\users\\shussain\\desktop\\bxf_msg\\playlistack\\bxf_1501110072083.xml"); //baseurl restassured.baseuri= prop.getproperty("aishost"); //value populating property methord above response resp = given().log().all(). header("content-type", "application/xml; charset=utf-8"). body(postdata). when(). post("/bxfxml"). then().assertthat().statuscode(200).and().contenttype(contenttype.xml). extract().response(); //to convert raw data string xmlpath xmlresponse= reusablefunctions.rawtoxml(resp); string responsestring = resp.asstring(); system.out.println("xml response - "+ responsestring); } public static string generatestringfromresource(string path) throws ioexception{ return new string(files.readallbytes(paths.get(path))); } } //below code pulls data db , creates xml file public list <string> getexternalid() throws classnotfoundexception, sqlexception, invalidfileformatexception, ioexception{ fileinputstream file = new fileinputstream("d:\\tools\\workspace\\bxf\\src\\files\\config.properties"); properties prop = new properties(); prop.load(file); wini ini = new wini(new file(prop.getproperty("woini"))); string userid = ini.fetch("connectionstring", "user id"); string pwd = ini.fetch("connectionstring", "password"); string dbname = ini.fetch("connectionstring", "initial catalog"); string connectionurl = "jdbc:sqlserver://localhost" + ";databasename="+dbname + ";user=" +userid + ";password=" +pwd; class.forname("com.microsoft.sqlserver.jdbc.sqlserverdriver"); connection con = drivermanager.getconnection(connectionurl); system.out.println("driver version: " + con.getmetadata().getdriverversion()); //retrieve data statement st = con.createstatement(); resultset rs = st.executequery(prop.getproperty("select top 5 * wo_interface_queue action_info = 'playlist' order create_date desc")); list <string> externalid = new arraylist<string>(); while (rs.next()) { externalid.add(rs.getstring("interface_queue_id")); } return externalid; } public void createbxfackfxml(string id) { try { fileinputstream file = new fileinputstream("d:\\tools\\workspace\\bxf\\src\\files\\config.properties"); properties prop = new properties(); prop.load(file); documentbuilderfactory dbfactory = documentbuilderfactory.newinstance(); documentbuilder dbuilder = dbfactory.newdocumentbuilder(); document doc = dbuilder.newdocument(); // root element element rootelement = doc.createelement("bxfmessage"); doc.appendchild(rootelement); // setting attribute element rootelement.setattribute("xmlns:xsd", "http://www.w3.org/2001/xmlschema"); rootelement.setattribute("xmlns:xsi", "http://www.w3.org/2001/xmlschema-instance"); rootelement.setattribute("xmlns:xs", "http://www.w3.org/2001/xmlschema"); rootelement.setattribute("id", "urn:uuid:427c5fb4-987c-4718-8ace-12fcb835df21"); rootelement.setattribute("datetime", "2017-10-25t09:28:12.1074289-04:00"); rootelement.setattribute("messagetype", "acknowledgement"); rootelement.setattribute("origin", "adc"); rootelement.setattribute("origintype", "automation system"); rootelement.setattribute("username", "adc_user"); rootelement.setattribute("destination", "wideorbit"); rootelement.setattribute("originmessageid", id); rootelement.setattribute("status", "ok"); rootelement.setattribute("ext:usage", "application acknowledgement"); rootelement.setattribute("xsi:schemalocation", "http://smpte-ra.org/schemas/2021/2008/bxf bxfschema.xsd http://smpte-ra.org/schemas/2021/2008/bxf/extension bxfschema-extension.xsd http://www.atsc.org/xmlschemas/pmcp/2007/3.1 pmcp31.xsd"); rootelement.setattribute("xmlns:ext", "http://smpte-ra.org/schemas/2021/2008/bxf/extension"); rootelement.setattribute("xmlns", "http://smpte-ra.org/schemas/2021/2008/bxf"); // write content xml file transformerfactory transformerfactory = transformerfactory.newinstance(); transformer transformer = transformerfactory.newtransformer(); domsource source = new domsource(doc); doc.setxmlstandalone(true); string filename = prop.getproperty("playlistack_endpoint")+ "bxf_"+system.currenttimemillis()+".xml"; streamresult result = new streamresult(new file(filename)); transformer.transform(source, result); // output console testing streamresult consoleresult = new streamresult(system.out); transformer.transform(source, consoleresult); } catch (exception e) { e.printstacktrace(); }
on next run of xml file creating program can change extention of sent data else after can loop through xml files , can send new data
files.newdirectorystream(paths.get("."), path -> path.tostring().endswith(".xml")) .foreach(data -> { // send data here });
Comments
Post a Comment