import com.eviware.soapui.model.testsuite.TestRunner.Status
import java.io.File;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import java.io.*;
def tC = testRunner.getTestCase();
def tS = tC.getTestSuite();
def tP = tS.getProject();
def randomNumber = Math.random();
def callTSname = tC.getPropertyValue("sName");
def callTCname = tC.getPropertyValue("tName");
def callTC = tP.getTestSuiteByName(callTSname).getTestCaseByName(callTCname);
String oldtext = "";
try {
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
// oadrEvent
Document doc = docBuilder.newDocument();
Element rootElements = doc.createElement("ns6:oadrDistributeEvent");
rootElements.setAttribute("xmlns:ns6", "http://openadr.org/oadr-2.0a/2012/07");
rootElements.setAttribute("xmlns:ns2", "http://docs.oasis-open.org/ns/energyinterop/201110/payloads");
rootElements.setAttribute("xmlns", "http://docs.oasis-open.org/ns/energyinterop/201110");
rootElements.setAttribute("xmlns:ns4", "urn:ietf:params:xml:ns:icalendar-2.0");
rootElements.setAttribute("xmlns:ns3", "http://docs.oasis-open.org/ns/emix/2011/06");
rootElements.setAttribute("xmlns:ns5", "urn:ietf:params:xml:ns:icalendar-2.0:stream");
doc.appendChild(rootElements);
for(def i=1;i<=Integer.valueOf(callTC.getPropertyValue("EventNumber"));i++)
{
if(i==1)
{
//eiResponse
Element eiResponse = doc.createElement("eiResponse");
rootElements.appendChild(eiResponse);
//responseCode
Element responseCode = doc.createElement("responseCode");
responseCode.appendChild(doc.createTextNode(callTC.getPropertyValue("RESPONSECODE")));
eiResponse.appendChild(responseCode);
//responseDescription
Element responseDescription = doc.createElement("responseDescription");
responseDescription.appendChild(doc.createTextNode("OK"));
eiResponse.appendChild(responseDescription);
//requestID
Element requestID = doc.createElement("ns2:requestID");
requestID.appendChild(doc.createTextNode(callTC.getPropertyValue("REQUESTID")));
eiResponse.appendChild(requestID);
callTC.setPropertyValue("RequestIDFirst",callTC.getPropertyValue("REQUESTID"));
//requestID1
Element requestID1 = doc.createElement("ns2:requestID");
requestID1.appendChild(doc.createTextNode(callTC.getPropertyValue("REQUESTID")));
rootElements.appendChild(requestID1);
Element vtnID = doc.createElement("vtnID");
vtnID.appendChild(doc.createTextNode(callTC.getPropertyValue("VTNID")));
rootElements.appendChild(vtnID);
}
Element rootElement = doc.createElement("ns6:oadrEvent");
rootElements.appendChild(rootElement);
//Event
Element event = doc.createElement("eiEvent");
rootElement.appendChild(event);
// Event Descriptor
Element eventdescriptor = doc.createElement("eventDescriptor");
event.appendChild(eventdescriptor);
// Event ID
Element eventid = doc.createElement("eventID");
eventid.appendChild(doc.createTextNode(callTC.getPropertyValue("EVENTID_"+i)));
eventdescriptor.appendChild(eventid);
// modificationNumber
Element modificationNumber = doc.createElement("modificationNumber");
modificationNumber.appendChild(doc.createTextNode(callTC.getPropertyValue("MODIFICATION_NO_"+i)));
eventdescriptor.appendChild(modificationNumber);
// priority
Element priority = doc.createElement("priority");
priority.appendChild(doc.createTextNode(callTC.getPropertyValue("PRIORITY_"+i)));
eventdescriptor.appendChild(priority);
//MarketContext
Element eiMarketContext = doc.createElement("eiMarketContext");
eventdescriptor.appendChild(eiMarketContext);
// MarketContext
Element marketcontext = doc.createElement("ns3:marketContext");
marketcontext.appendChild(doc.createTextNode(callTC.getPropertyValue("MARKETCONTEXT_1")));
eiMarketContext.appendChild(marketcontext);
// createdDateTime
Element createdDateTime = doc.createElement("createdDateTime");
createdDateTime.appendChild(doc.createTextNode(callTC.getPropertyValue("CREATEDDATETIME_"+i)));
eventdescriptor.appendChild(createdDateTime);
// eventStatus
Element eventStatus = doc.createElement("eventStatus");
eventStatus.appendChild(doc.createTextNode(callTC.getPropertyValue("EVENTSTATUS_"+i)));
eventdescriptor.appendChild(eventStatus);
// testEvent
Element testEvent = doc.createElement("testEvent");
testEvent.appendChild(doc.createTextNode(callTC.getPropertyValue("TESTEVENT_"+i)));
eventdescriptor.appendChild(testEvent);
// vtnComment
Element vtnComment = doc.createElement("vtnComment");
vtnComment.appendChild(doc.createTextNode(callTC.getPropertyValue("COMMENTS_"+i)));
eventdescriptor.appendChild(vtnComment);
//eiActivePeriod
Element eiActivePeriod = doc.createElement("eiActivePeriod");
event.appendChild(eiActivePeriod);
//properties
Element properties = doc.createElement("ns4:properties");
eiActivePeriod.appendChild(properties);
//dtstart
Element dtstart = doc.createElement("ns4:dtstart");
properties.appendChild(dtstart);
// date-time
Element datetime = doc.createElement("ns4:date-time");
datetime.appendChild(doc.createTextNode(callTC.getPropertyValue("STARTDATETIME_"+i)));
dtstart.appendChild(datetime);
//dtstart
Element duration = doc.createElement("ns4:duration");
properties.appendChild(duration);
// durationsub
Element durationsub = doc.createElement("ns4:duration");
durationsub.appendChild(doc.createTextNode(callTC.getPropertyValue("DURATION_"+i)));
duration.appendChild(durationsub);
//tolerance
Element tolerance = doc.createElement("ns4:tolerance");
properties.appendChild(tolerance);
//tolerate
Element tolerate = doc.createElement("ns4:tolerate");
tolerance.appendChild(tolerate);
// startafter
Element startafter = doc.createElement("ns4:startafter");
startafter.appendChild(doc.createTextNode(callTC.getPropertyValue("STARTAFTER_"+i)));
tolerate.appendChild(startafter);
//x-eiNotification
Element eiNotification = doc.createElement("x-eiNotification");
properties.appendChild(eiNotification);
// duration
Element durationnotification = doc.createElement("ns4:duration");
durationnotification.appendChild(doc.createTextNode(callTC.getPropertyValue("NOTIFICATIONDURATION_"+i)));
eiNotification.appendChild(durationnotification);
//x-eiRampUp
Element eiRampUp = doc.createElement("x-eiRampUp");
properties.appendChild(eiRampUp);
// duration
Element durationrampup = doc.createElement("ns4:duration");
durationrampup.appendChild(doc.createTextNode(callTC.getPropertyValue("RAMPUPDURATION_"+i)));
eiRampUp.appendChild(durationrampup);
//x-eiRecovery
Element eiRecovery = doc.createElement("x-eiRecovery");
properties.appendChild(eiRecovery);
// duration
Element durationrecovery = doc.createElement("ns4:duration");
durationrecovery.appendChild(doc.createTextNode(callTC.getPropertyValue("RECOVERYDURATION_"+i)));
eiRecovery.appendChild(durationrecovery);
//components
Element components = doc.createElement("ns4:components");
components.setAttribute("xsi:nil", "true");
components.setAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");
eiActivePeriod.appendChild(components);
//eiEventSignals
Element eiEventSignals = doc.createElement("eiEventSignals");
event.appendChild(eiEventSignals);
//eiEventSignal
Element eiEventSignal = doc.createElement("eiEventSignal");
eiEventSignals.appendChild(eiEventSignal);
//intervals
Element intervals = doc.createElement("ns5:intervals");
eiEventSignal.appendChild(intervals);
//interval
Element interval = doc.createElement("interval");
intervals.appendChild(interval);
//duration
Element durationinterval = doc.createElement("ns4:duration");
interval.appendChild(durationinterval);
// duration
Element durationintervalsub = doc.createElement("ns4:duration");
durationintervalsub.appendChild(doc.createTextNode(callTC.getPropertyValue("INTERVALDURATION_"+i)));
durationinterval.appendChild(durationintervalsub);
//uid
Element uid = doc.createElement("ns4:uid");
interval.appendChild(uid);
// text
Element text = doc.createElement("ns4:text");
text.appendChild(doc.createTextNode(callTC.getPropertyValue("UID_"+i)));
uid.appendChild(text);
//SignalPayload
Element signalPayload = doc.createElement("signalPayload");
interval.appendChild(signalPayload);
//PayloadFloat
Element PayloadFloat = doc.createElement("payloadFloat");
signalPayload.appendChild(PayloadFloat);
// payloadfloatvalue
Element payloadfloatvalue = doc.createElement("value");
payloadfloatvalue.appendChild(doc.createTextNode(callTC.getPropertyValue("PayloadValue_"+i)));
PayloadFloat.appendChild(payloadfloatvalue);
// signalname
Element signalname = doc.createElement("signalName");
signalname.appendChild(doc.createTextNode(callTC.getPropertyValue("SIGNALNAME_"+i)));
eiEventSignal.appendChild(signalname);
// signalType
Element signalType = doc.createElement("signalType");
signalType.appendChild(doc.createTextNode(callTC.getPropertyValue("SIGNALTYPE_"+i)));
eiEventSignal.appendChild(signalType);
// signalID
Element signalID = doc.createElement("signalID");
signalID.appendChild(doc.createTextNode(callTC.getPropertyValue("SIGNALID_"+i)));
eiEventSignal.appendChild(signalID);
// currentValue
Element currentValue = doc.createElement("currentValue");
eiEventSignal.appendChild(currentValue);
//currentPayloadFloat
Element currentPayloadFloat = doc.createElement("payloadFloat");
currentValue.appendChild(currentPayloadFloat);
// currentpayloadfloatvalue
Element currentpayloadfloatvalue = doc.createElement("value");
currentpayloadfloatvalue.appendChild(doc.createTextNode(callTC.getPropertyValue("CurrentPayloadValue_"+i)));
currentPayloadFloat.appendChild(currentpayloadfloatvalue);
//eiTarget
Element eiTarget = doc.createElement("eiTarget");
event.appendChild(eiTarget);
Element groupID = doc.createElement("groupID");
groupID.appendChild(doc.createTextNode(callTC.getPropertyValue("GROUPID")));
eiTarget.appendChild(groupID);
//Resource Id
log.info "The value for the Resource Id is : : :"+callTC.getPropertyValue("ResourceId")
log.info "The Integer Value is "+Integer.valueOf(callTC.getPropertyValue("ResourceId"));
for(def j=1;j<=Integer.valueOf(callTC.getPropertyValue("ResourceId"));j++)
{
Element resourceID = doc.createElement("resourceID");
resourceID.appendChild(doc.createTextNode(callTC.getPropertyValue("RESOURCEID_"+j)));
eiTarget.appendChild(resourceID);
}
// venID
Element venID = doc.createElement("venID");
venID.appendChild(doc.createTextNode(callTC.getPropertyValue("VENID_"+i)));
eiTarget.appendChild(venID);
Element partyID = doc.createElement("partyID");
partyID.appendChild(doc.createTextNode(callTC.getPropertyValue("PARTYID")));
eiTarget.appendChild(partyID);
// oadrResponseRequired
Element oadrResponseRequired = doc.createElement("ns6:oadrResponseRequired");
oadrResponseRequired.appendChild(doc.createTextNode(callTC.getPropertyValue("OADRRESPONSEREQUIRED_"+i)));
rootElement.appendChild(oadrResponseRequired);
}
// write the content into xml file
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
DOMSource source = new DOMSource(doc);
StringWriter stringWriter = new StringWriter();
StreamResult result = new StreamResult(stringWriter);
//def testcasename = tC.getName
StreamResult result1 = new StreamResult(new File(tP.getPropertyValue("BackUpXMLPath")+callTC.getName()+"_"+new Date().format( 'yyyy-MM-dd_hhmms' )+".xml"));
log.info callTC.getName();
transformer.transform(source, result1);
transformer.transform(source, result);
oldtext = stringWriter.toString();
} catch (ParserConfigurationException pce) {
pce.printStackTrace();
} catch (TransformerException tfe) {
tfe.printStackTrace();
}
log.info oldtext
String newtext = oldtext.replaceAll(" xmlns="+'"'+'"',"");
log.info newtext
tC.setPropertyValue("GeneratedXML", newtext);
log.info "Extra tags removed"
log.info "XML Created!!!";
sleep 3000;