Forum Discussion
12 Replies
Sort By
- SmartBear_SuppoSmartBear Alumni (Retired)Hi Gilbert,
Unfortunately, there's no easy way to do this kind of assertion on incoming requests. However, you can validate the request using groovy in the "OnRequest Script" in the MockService editor.
Here's an article regarding MockService scripting on soapui.org
http://www.soapui.org/Service-Mocking/c ... vices.html
Regards,
Arian
SmartBear Sweden - ITAbteilungContributorOkay, so I'll validate it programatically. We use composite projects with "Cache Definitions = true". Is there an easy way to access these cached definitions? I couldn't find a fitting method in com.eviware.soapui.impl.wsdl.WsdlProject.
Thanks in advance,
Gilbert - SmartBear_SuppoSmartBear Alumni (Retired)Hey Gilbert,
I haven't been able to find an easy way to access the definition cache. I'll do some more investigation and let you know if I come up with anything.
Have a great weekend!
Regards,
Arian
SmartBear Sweden - ITAbteilungContributorOk, thank you.
- ITAbteilungContributorNow I've solved it that way: in the onRequest script of the MockUp I execute the following code, which works generically for all MockUps that caches its definition:
import com.eviware.soapui.impl.wsdl.WsdlOperation
import com.eviware.soapui.impl.wsdl.support.wsdl.WsdlValidator;
import com.eviware.soapui.impl.wsdl.support.wsdl.WsdlContext;
import com.eviware.soapui.impl.wsdl.mock.WsdlMockOperation
import com.eviware.soapui.impl.wsdl.panels.mockoperation.WsdlMockRequestMessageExchange;
import com.eviware.soapui.impl.wsdl.mock.WsdlMockResult;
WsdlContext wsdlcontext = context.mockService.getMockedInterfaces()[0].getDefinitionContext();
WsdlValidator validator = new WsdlValidator(wsdlcontext);
String operationName = mockRequest.soapAction.substring(mockRequest.soapAction.lastIndexOf('/') +1).split("#")[1];
WsdlMockOperation operation = context.mockService.getMockOperationByName( operationName);
WsdlMockRequestMessageExchange msgExchange = new WsdlMockRequestMessageExchange(mockRequest, operation);
def errors = validator.assertRequest(msgExchange, false);
if (errors.length > 0 ){
log.info( "returning ValidationError");
String validationErrors = "VALIDATION ERRORS: " + errors.collect(){ '\n' + it };
String content = '''<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Body>
<soapenv:Fault>
<faultcode>Server</faultcode>
<faultstring>''' + validationErrors + '''</faultstring>
</soapenv:Fault>
</soapenv:Body>
</soapenv:Envelope>''';
WsdlMockResult mr = new WsdlMockResult(mockRequest);
mockRequest.httpResponse.writer << content;
mockRequest.httpResponse.status = 200;
return mr;
}
In general it works. There is only one curious bug: We are deploying the mock on a server and using the WebUI (the web interface of the mockup where requests, responses and scriptlog can be viewed). Normally, when I send a request to the mock and it sends the response, the whole conversation can be seen at the WebUI. BUT: when the request is invalid and the script returns the WsdlMockResult, nothing but the scriptlog can be seen and WORSE: the upper third part of the WebUI does not work anymore until redeploying the mockup!!! Instead of showing timestamp, time taken, mockoperation and so on, an Exception appears:
java.lang.NullPointerException:
com.eviware.soapui.mockaswar.MockAsWarServlet.printMaster(MockAsWarServlet.java:422)
com.eviware.soapui.mockaswar.MockAsWarProServlet$MockServletSoapUIProCore.dispatchRequest(SourceFile:128)
com.eviware.soapui.mockaswar.MockAsWarServlet.service(MockAsWarServlet.java:190)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
We are using SoapUI-Pro4.5.1. Please could you fix that Exception! It would be an enormous help for us and all other developers who want to use request validation!
Thanks in advance,
Gilbert Fritz - SmartBear_SuppoSmartBear Alumni (Retired)Hi,
Can you check if the operation that you lookup by name is not null?
These two lines specifically:
String operationName = mockRequest.soapAction.substring(mockRequest.soapAction.lastIndexOf('/') +1).split("#")[1];
WsdlMockOperation operation = context.mockService.getMockOperationByName( operationName);
If the operation is null at this point it is probably not a good idea to continue.
If it is not null at that point then I think we have to patch the code in some way.
We may possibly have to do it anyway but it would be good to know in any case.
Regards,
Henrik
SmartBear Software - ITAbteilungContributorDear Henrik,
the operation is definitely not null, since everything works fine, except of the WebUI! The mock returns the desired responses: a valid answer if the request is valid, a soapfault (as defined in the code above) if the request is invalid. When the mock is deployed, the WebUI behaves like that: everything works normally until a request is invalid, then the WebUI displays the Exception posted above. The mockup itself keeps on running normally and behaves as desired!
I think it has something to do with the implementation of the WebUI. I guess that it can not handle that the script-chain is aborted (onrequestscript -> dispatcherscript -> operationscript) and the onrequestscript sends the return.
Best regards,
Gilbert - ITAbteilungContributor...but you are right, it is not a good idea to build on the "soapAction" from the SOAP-Header, since it can be an abitrary string.
Anyway, to reconstruct the WebUI-bug, please try to hardcode the operationname:
WsdlMockOperation operation = context.mockService.getMockOperationByName( "hardcoded_operationname");
Thank you,
best regards,
Gilbert - ITAbteilungContributorDear Henrik,
should I email you the SoapUI project that contains the case? Safes reconstruction work for you..
Regards,
Gilbert - SmartBear_SuppoSmartBear Alumni (Retired)Hi Gilbert,
Either you can attach it here in the forums, or email it to SmartBear-Sweden-support@smartbear.com and we'll try to reproduce.
Regards,
Arian
SmartBear Sweden