Dynamic JSON Request based on previous request.

 This explains the Dynamic Request based on the previous requests.




Here is the second request based on the previous response. 

In request 1: the response was like this

Response: In the below response we have 5 collections

[{"dataCollectionId":"3640e3ab-79d8-4d5c-a416-0381091349a2","dataCollectionName":"DC_02_042","orderCount":1},{"dataCollectionId":"6008171e-a070-49ba-828d-093e6336482f","dataCollectionName":"DC_03_013","orderCount":1},{"dataCollectionId":"7ec9ef40-f349-47ff-8699-ad8d1babe7e6","dataCollectionName":"DC_03_078","orderCount":1},{"dataCollectionId":"ac163d5f-ec0b-4635-aff8-e7a4533d2f1d","dataCollectionName":"DC_01_046","orderCount":1},{"dataCollectionId":"c33ebafd-0be4-41c2-ad3a-b906d7af1a1c","dataCollectionName":"DC_04_081","orderCount":1}]

as captured like this:





Json Requst data for another request:

{"ref":null,"name":null,"description":null,"searchGroups":[{"operator":"START","active":true,"searches":[{"operator":"START","active":true,"facetId":"MARKETPLACE_DATACOLLECTION","filterGroups":[{"operator":"START","filters":[{"operator":"START","type":"ID","properties":{"field":"id","value":"682c74b2-73a8-4f3d-b2b4-e47b67392bad"}}]}]},{"operator":"OR","active":true,"facetId":"MARKETPLACE_DATACOLLECTION","filterGroups":[{"operator":"START","filters":[{"operator":"START","type":"ID","properties":{"field":"id","value":"c3a90fac-a289-4ecf-8170-24629ec19bd4"}}]}]}]}],"searchScopes":[{"facetId":"MARKETPLACE_DATACOLLECTION","fields":["id","name","categoryName","dataSetName","dqScore","dqGreenTarget","dqAmberTarget","rating"],"orderList":[{"field":"id","type":"ASC"}],"properties":{"offset":0,"limit":36}},{"facetId":"MARKETPLACE_DATACOLLECTION","fields":["id","name","categoryName","dataSetName","dqScore","dqGreenTarget","dqAmberTarget","rating"],"orderList":[{"field":"id","type":"ASC"}],"properties":{"offset":0,"limit":36}}]}


Here we need to update the request dynamically based on the number of data collection values.


Here is the JSR223Post processer with the groovy script:


int count=Integer.parseInt(vars.get('dataCollectionIds_matchNr'));

def  lb1 ="{\"operator\":\"START\",\"active\":true,\"facetId\":\"MARKETPLACE_DATACOLLECTION\",\"filterGroups\":[{\"operator\":\"START\",\"filters\":[{\"operator\":\"START\",\"type\":\"ID\",\"properties\":{\"field\":\"id\",\"value\":\"";

def  lb2 ="{\"operator\":\"OR\",\"active\":true,\"facetId\":\"MARKETPLACE_DATACOLLECTION\",\"filterGroups\":[{\"operator\":\"START\",\"filters\":[{\"operator\":\"START\",\"type\":\"ID\",\"properties\":{\"field\":\"id\",\"value\":\"";

def rb = "\"}}]}]}";

log.info("count" + count);

log.info("lb1" + lb1);

log.info("rb" + rb);

String final2= "";

if (count < 1 )

{

Result = lb1 + rb ;

vars.put("Result",Result);

}

else if (count == 1)

{

String response1 = vars.get('dataCollectionIds_'+1);

Result = lb1 + response1 + rb ;

vars.put("Result",Result);

}

else

{

String response1 =vars.get('dataCollectionIds_'+1);

String str = lb1 + response1 + rb ;

for(int i=2;i<=count;i++)

{

String response =vars.get('dataCollectionIds_'+i); 

String final1 = lb2 + response + rb ;

 log.info("the final resulsyt is "+ final1);

final2 = final2 + ',' + final1 ;

 }

Result = str  + final2

vars.put("Result",Result); 

 log.info("the final resulsyt is final22 "+ final2);

  log.info("the final resulsyt is final22 "+ Result);

 //log.info("data ids"+response);

}


Finally request body for the second request

{"ref":null,"name":null,"description":null,"searchGroups":[{"operator":"START","active":true,"searches":[${Result}]}],"searchScopes":[{"facetId":"MARKETPLACE_DATACOLLECTION","fields":["id","name","categoryName","dataSetName","dqScore","dqGreenTarget","dqAmberTarget","rating"],"orderList":[{"field":"id","type":"ASC"}],"properties":{"offset":0,"limit":36}},{"facetId":"MARKETPLACE_DATACOLLECTION","fields":["id","name","categoryName","dataSetName","dqScore","dqGreenTarget","dqAmberTarget","rating"],"orderList":[{"field":"id","type":"ASC"}],"properties":{"offset":0,"limit":36}},{"facetId":"MARKETPLACE_DATACOLLECTION","fields":["id","name","categoryName","dataSetName","dqScore","dqGreenTarget","dqAmberTarget","rating"],"orderList":[{"field":"id","type":"ASC"}],"properties":{"offset":0,"limit":36}},{"facetId":"MARKETPLACE_DATACOLLECTION","fields":["id","name","categoryName","dataSetName","dqScore","dqGreenTarget","dqAmberTarget","rating"],"orderList":[{"field":"id","type":"ASC"}],"properties":{"offset":0,"limit":36}},{"facetId":"MARKETPLACE_DATACOLLECTION","fields":["id","name","categoryName","dataSetName","dqScore","dqGreenTarget","dqAmberTarget","rating"],"orderList":[{"field":"id","type":"ASC"}],"properties":{"offset":0,"limit":36}}]}


Comments

Post a Comment

Popular posts from this blog

Convert Swagger apis into jmeter script

Script for replace the data into the file content using groovy with jsr223

Jmeter Preprocessor to add Dynamic parameters