Forum Discussion

amarnath1234's avatar
6 years ago

Datasink issue

I have scenario where I need to sink different Promotion IDs CategoryType data from Response to datasource.If it is 1 Promotion then I am able to sink but for multiple promotions script is not working properly, reason CategoryType field is same for both Promotion IDs and scripts unable to identify properly.I have attached my response and script detail for reference.I need Help.



PromotionID-513 ,  categoryType-Marketing_Offers

PromotionID-390 ,  categoryType-Loyalty_Offers

PromotionID-234 ,  categoryType-Marketing_Offers




//remove properties from DataSink
//Transfer and sink properties
def categoryType = context.expand( '${valid Loyalty offers_issued from ODM#Response#$[\'personalizedOffers\'][0][\'validCoupon\'][\'promotion\'][\'categoryType\']}' )
testRunner.testCase.getTestStepByName("DataSink").setPropertyValue("categoryType", categoryType)
// categoryType

def categoryType1 = context.expand( '${valid Loyalty offers_issued from ODM#Response#$[\'personalizedOffers\'][0][\'validCoupon\'][\'promotion\'][\'categoryType\']}' )
testRunner.testCase.getTestStepByName("DataSink").setPropertyValue("categoryType", categoryType1)
// categoryType1

def globalOfferOverride = context.expand( '${valid Loyalty offers_issued from ODM#Response#$[\'customer\'][\'customerAttributes\'][\'offerStrategy\'][\'globalOfferOverride\']}' )
testRunner.testCase.getTestStepByName("DataSink").setPropertyValue("globalOfferOverride", globalOfferOverride)
// globalOfferOverride
def noOfProductCoupons = context.expand( '${valid Loyalty offers_issued from ODM#Response#$[\'configuration\'][\'noOfProductCoupons\']}' )
testRunner.testCase.getTestStepByName("DataSink").setPropertyValue("noOfProductCoupons", noOfProductCoupons)
// noOfProductCoupons
def noOfGlobalCoupons = context.expand( '${valid Loyalty offers_issued from ODM#Response#$[\'configuration\'][\'noOfGlobalCoupons\']}' )
testRunner.testCase.getTestStepByName("DataSink").setPropertyValue("noOfGlobalCoupons", noOfGlobalCoupons)
// noOfGlobalCoupons
//Extract Response
def response = context.expand( '${valid Loyalty offers_issued from ODM#Response}' )
testRunner.testCase.getTestStepByName("DataSink").setPropertyValue("Response", response)
// response





4 Replies

  • avidCoder's avatar
    Super Contributor

    You can think of another approach which is using HashMap in groovy. This will make it easier to set the key and value corresponding to each other and hence writing it to datasink.


    String jsonString = "your response as JSON"
    HashMap<String,List<String>> myMap = new HashMap<String,List<String>>();
    try { 
    JSONObject json = new JSONObject(jsonString);
    JSONArray dataArray = json.getJSONArray("personalizedOffers");
    for(int i = 0; i < dataArray.length(); i++) {
        JSONObject tagObject = dataArray.getJSONObject(i);
    //Write code to pass till to reach "promotion" object. since, "categoryType" and "id" exists inside "promotion" object
        String category = tagObject.getString("categoryType");
        String id = tagObject.getString("id");
        if(myMap.containsKey(id)) {
             List<String> arrayID = new ArrayList<String>();
             myMap.put(category, arrayID);

    This way, you can assign each "id" corresponding to "categoryType" and then write all together using for loop to DataSink.


    This is a bot of tricky aproach and also please go through JSON manipulation. How to handle JSON.

    • amarnath1234's avatar

      not sure how its work but i am unable to do this in my script.

      • TanyaYatskovska's avatar
        SmartBear Alumni (Retired)

        Hi Amarnath1234,

        What exactly doesn't work? if you give us more details on how you are trying to implement the suggestion, perhaps, we can help.