fork download
  1. //Using the fixed values in below for demonstration only. Assign the data from variables / properties as needed
  2.  
  3. //Assign the response dynamically from previous step
  4. def jsonString = '''[
  5. {
  6. "SourceId": "bc3cef1e-a9f1-46df-a4f0-c1131357ea57",
  7. "SourceCode": "ABC",
  8. "CodeTier": "123",
  9. "SourceDescription": "ABC Desc"
  10. },
  11. {
  12. "SourceId": "b4035134-ca33-4b33-b3c7-06ea880f1f28",
  13. "SourceCode": "DEF",
  14. "CodeTier": "456",
  15. "SourceDescription": "DEF Descr"
  16. },
  17. {
  18. "SourceId": "9666bd19-1916-4052-9044-06b0e38e9175",
  19. "SourceCode": "GHI",
  20. "SourceDescription": "GHI Descr"
  21. }
  22. ]'''
  23.  
  24.  
  25. def json = new groovy.json.JsonSlurper().parseText(jsonString)
  26.  
  27. def getSourceIdBySourceCode = { code -> json.find {it?.SourceCode == code}?.SourceId }
  28.  
  29. println "SourceId for ABC is : ${getSourceIdBySourceCode('ABC')}"
  30.  
  31.  
  32. //Test all the source id and codes
  33.  
  34. def expectedCodeIdMap = [ ABC: 'bc3cef1e-a9f1-46df-a4f0-c1131357ea57', DEF: 'b4035134-ca33-4b33-b3c7-06ea880f1f28', GHI: '9666bd19-1916-4052-9044-06b0e38e9175']
  35. expectedCodeIdMap.each { code, id ->
  36. println "verifying $code with expected $id"
  37. assert id == getSourceIdBySourceCode(code)
  38. }
  39.  
  40. println getSourceIdBySourceCode('123')
Success #stdin #stdout 2.5s 140780KB
stdin
Standard input is empty
stdout
SourceId for ABC is : bc3cef1e-a9f1-46df-a4f0-c1131357ea57
verifying ABC with expected bc3cef1e-a9f1-46df-a4f0-c1131357ea57
verifying DEF with expected b4035134-ca33-4b33-b3c7-06ea880f1f28
verifying GHI with expected 9666bd19-1916-4052-9044-06b0e38e9175
null