12 years ago
context Variables in Mock Services
I'm having a mock to receive an asynchronous answer from a web server.
Dough I'm able to send the XML answer from the server to the log, i'm not able to store it in a "global" variable so i could automatic check if the answer is as I'm expecting.
I tried to use context variables... but their always null!
I'm using this code on the OnRequest Script from my mock:
import javax.servlet.http.HttpServletResponse
import java.text.SimpleDateFormat
// Where do I set the context variables?? Its returning null.
def properties = context.getPropertyNames()
log.info(properties.length)
for ( e in properties ) {
log.info(e)
}
log.info ".......................... Sender: Received Ansewr from Servr."
if(mockRequest.method == "POST") {
log.info ".......................... Answer From Server content is " + mockRequest.requestContent
// Up to here OK
def response = new XmlSlurper().parseText(mockRequest.requestContent)
def requestNode = response.Body.children()[0]
def msrefid = response.Body.IncidentReportAcknowledged.@IDofIRupdate.text()
def receipt = "<Ack>${msrefid}</Ack>"
try {
def monitor = context.getProperty("ID")
log.info '@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ ' + monitor.toString()
// Here is where monitor is Null
if(monitor == null) {
sleep(10000) // testCase thread got slow, Server was fast
monitor = context.getProperty("ID")
}
log.info "onRequest: got property $monitor, will notify"
synchronized (monitor) {
log.info '+++++++++++++++++++++++++++++++'
context.setProperty(msrefid, mockRequest.requestContent)
log.info '>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>'
monitor.notifyAll()
}
log.info "notified " + msrefid
} catch (Exception e) {
log.error "onRequest: caught " + e
log.error e
}
return new com.eviware.soapui.impl.wsdl.mock.WsdlMockResult(mockRequest)
}
log.info ".......................... request not POST, continuing along handler chain"
Dough I'm able to send the XML answer from the server to the log, i'm not able to store it in a "global" variable so i could automatic check if the answer is as I'm expecting.
I tried to use context variables... but their always null!
I'm using this code on the OnRequest Script from my mock:
import javax.servlet.http.HttpServletResponse
import java.text.SimpleDateFormat
// Where do I set the context variables?? Its returning null.
def properties = context.getPropertyNames()
log.info(properties.length)
for ( e in properties ) {
log.info(e)
}
log.info ".......................... Sender: Received Ansewr from Servr."
if(mockRequest.method == "POST") {
log.info ".......................... Answer From Server content is " + mockRequest.requestContent
// Up to here OK
def response = new XmlSlurper().parseText(mockRequest.requestContent)
def requestNode = response.Body.children()[0]
def msrefid = response.Body.IncidentReportAcknowledged.@IDofIRupdate.text()
def receipt = "<Ack>${msrefid}</Ack>"
try {
def monitor = context.getProperty("ID")
log.info '@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ ' + monitor.toString()
// Here is where monitor is Null
if(monitor == null) {
sleep(10000) // testCase thread got slow, Server was fast
monitor = context.getProperty("ID")
}
log.info "onRequest: got property $monitor, will notify"
synchronized (monitor) {
log.info '+++++++++++++++++++++++++++++++'
context.setProperty(msrefid, mockRequest.requestContent)
log.info '>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>'
monitor.notifyAll()
}
log.info "notified " + msrefid
} catch (Exception e) {
log.error "onRequest: caught " + e
log.error e
}
return new com.eviware.soapui.impl.wsdl.mock.WsdlMockResult(mockRequest)
}
log.info ".......................... request not POST, continuing along handler chain"