Thursday, 3 April 2014

SoapUI: Property Based Assertions

In many cases you need to or are allowed to send a messageId and a correlationId to a service. In the response these Id's are mirrored back. In SoapUI it is pretty easy to generate those ID's using Groovy. The code for this is as follows:
def guidVal = "${java.util.UUID.randomUUID()}";
def testCase = testRunner.testCase;
testCase.setPropertyValue("MessageId", guidVal);
def msgDate = new Date() ;
def msgDateStr =  msgDate.format("yyyy-MM-dd'T'HH:mm:ss");
testCase.setPropertyValue("MessageTimeStamp",msgDateStr);
This as you can conclude from the code this is from a Groovy test step in a test Case. In the succeeding soap request you can have something like the following to have the property values embedded in the message:
        <ber:berichtHeader>
            <ber:messageData>
               <ber:messageId>${#TestCase#MessageId}
<ber:messageTimestamp>${#TestCase#MessageTimeStamp} </ber:messageData> <ber:correlatieId>${#TestCase#MessageId} </ber:berichtHeader> Here you see that I use the same id for both messageId as well as the correlationId. A correlationId might have a longer lifespan as a messageId. In my (simple) case we have just one-to-one conversations. In the response of the message you might find something like the following:
         <abct:berichtHeader>
            <abct:messageData>
               <abct:messageId>23c20898-9164-449d-87ef-3d9ed96ba946
<abct:messageTimestamp>2014-04-03T13:40:25.147+02:00 <abct:refToMessageId>23bf6b7a-61f9-4c91-b932-b288c5e358be </abct:messageData> <abct:correlatieId>23bf6b7a-61f9-4c91-b932-b288c5e358be </abct:berichtHeader> Nice. Apparently SoapUI generated global unique message id's and apparently my service mirrored them back. But how do I test that automatically? The thing is in this that I don't know the expected value at designtime, since the messageId's are generated at runtime. But the nice thing in SoapUI is that almost everywhere you can define properties at several level's (General, Project, TestSuite, TestCase, etc.) and reference them almost every where. For instance, you can use properties to buildup a soap-endpoint, using properties at project level. So you could define an endpoint as follows:
http://${#Project#ServiceEndpoint}/${#Project#ServiceURI}
This can be used in assertions as well. So define an Xpath-Match assertion on your response and instead of a fixed expected value, give in
${#TestCase#messageId}
Like:
As you can see you reference the TestCase property by prefixing it with #TestCase. This refers to the current, running TestCase. For the sharp readers: my property reference in the assert is lower-init-case, where in the groovy script and the message it has an initial capital. I found though that the property is apparently not case-sensitive.

2 comments :

Anonymous said...

Thanks a bunch. I was struggling to figure out how to set up dynamic assertions and this helped.

Anonymous said...

Nice,

However, as a tip from me to you: I think anno 2016 it is not so professional to record your tutorials with Windows XP...

Regards,
Martien