Forum Discussion

amarnath1234's avatar
amarnath1234
Contributor
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.

 

EX:-

PromotionID-513 ,  categoryType-Marketing_Offers

PromotionID-390 ,  categoryType-Loyalty_Offers

PromotionID-234 ,  categoryType-Marketing_Offers

 

script:

 

//remove properties from DataSink
testRunner.testCase.getTestStepByName("DataSink").propertyList.forEach{
testRunner.testCase.getTestStepByName("DataSink").setPropertyValue(it.name,"")
}
//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)
//log.info categoryType

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

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

 

 

 

 

4 Replies

  • avidCoder's avatar
    avidCoder
    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>();
             arrayID.add(id);
             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.

    https://www.journaldev.com/2321/gson-example-tutorial-parse-json

    • amarnath1234's avatar
      amarnath1234
      Contributor

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

      • TanyaYatskovska's avatar
        TanyaYatskovska
        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.