Hi,
This took a bit longer than expected. Bit of pain marshalling objects, JSON objects, string etc.
To model the scenario, I created three Groovy Script steps in a Test Case. The first two create an object and converts to JSON. These mimic your responses.
The names of the first two scripts are important as they used used in the last step.
E.g. Get Response 1 JSON - Groovy Script
// We need this to create our JSON object.
import groovy.json.*;
// Define a play class
class ResponseOneClass {
String propOne = null;
String propTwo = null;
String propThree = null;
String propFour = null;
String propFive = null;
ResponseOneClass(valueOne, valueTwo, valueThree, valueFour, valueFive) {
this.propOne = valueOne;
this.propTwo = valueTwo;
this.propThree = valueThree;
this.propFour = valueFour;
this.propFive = valueFive;
}
}
// Create an object.
def responseOneObj = new ResponseOneClass("One", "Two", "Three", "Four", "Five");
log.info(responseOneObj.propOne);
// Create JSON String.
def responseOneJson = JsonOutput.toJson(responseOneObj);
log.info(responseOneJson);
return(responseOneJson.toString());
And now our second response...
E.g. Get Response 2 JSON - Groovy Script
// We need this to create our JSON object.
import groovy.json.*;
// Define a play class
class ResponseTwoClass {
String propThree = null;
String propFour = null;
String propFive = null;
String propSix = null;
String propSeven = null;
ResponseTwoClass(valueThree, valueFour, valueFive, valueSix, valueSeven) {
this.propThree = valueThree;
this.propFour = valueFour;
this.propFive = valueFive;
this.propSix = valueSix;
this.propSeven = valueSeven;
}
}
// Create an object.
def responseTwoObj = new ResponseTwoClass( "Three", "Four", "Five", "Six", "Seven");
log.info(responseTwoObj.propSeven);
// Create JSON String.
def responseTwoJson = JsonOutput.toJson(responseTwoObj);
log.info(responseTwoJson);
return(responseTwoJson.toString());
Now, the final script.
// We need this to create our JSON object.
import groovy.json.*;
// Get our two responses
def responseOneJson = context.expand( '${Get Response 1 JSON - Groovy Script#result}' );
def responseTwoJson = context.expand( '${Get Response 2 JSON - Groovy Script#result}' );
// Bit of logging
log.info(responseOneJson.toString());
log.info(responseTwoJson.toString());
// Use JSON Slurper to create objects.
def jsonSlurper = new JsonSlurper();
def responseOneObj = jsonSlurper.parseText( responseOneJson.toString() );
def responseTwoObj = jsonSlurper.parseText( responseTwoJson.toString() );
log.info(responseOneObj.propOne); // Ooo, we can access it like an object.
// Let's get the keys from our objects as we don't know what props are in our objects!
def responseOneKeys = responseOneObj.keySet();
log.info(responseOneKeys);
def responseTwoKeys = responseTwoObj.keySet();
log.info(responseTwoKeys);
// For each key in response 1, see if it exists in response 2. If it does, run an assertion.
for (key in responseOneKeys) {
if (responseTwoKeys.contains(key)){
// Common property. Now we can access our obj property by a variable!!
// Try switching the == to != to show failure.
assert responseOneObj[key] == responseTwoObj[key];
} else {
log.info("${key} is not in responseTwo");
}
}