Forum Discussion

rg2018's avatar
rg2018
Occasional Contributor
7 years ago

Script Assertion each statement

I'm trying to assert 0 position in file with my expected results. Within the log expected[0] appears to = filtered[0] which is the filtered parsed json file.  When I do an assert within each statement it appears instead of comparing expected[0] = filtered[0] it's actually trying to compare all the results returned within filtered[0] (filtered json file)??? Please help....

 

//////Script

def responseMsg = messageExchange.response.responseContent
def parsedJson = new groovy.json.JsonSlurper().parseText(context.response)
//new variable to reduce the response list specific to levelOfCareName == 'Hospital_Level_of_Care'
def parsedJsonFiltered = parsedJson.criteria_LIST.findAll{it.levelOfCareName == 'Hospital_Level_of_Care'}

//def expectedResults Array
def expectedResults = [
[criteriaName: "Impact_On_Functioning", levelOfCareName: "Hospital_Level_of_Care", outcome: "Met", parentCriteria: "null", parentCriteriaName: "null", programName: "Hospital_Level_of_Care"]
//will be adding more to array after I get first assert to pass

]
log.info "ParsedJson Filtered: ${parsedJsonFiltered}}"

//go through each and assert
expectedResults.each
assert parsedJsonFiltered[0] == expectedResults[0]

//show filtered json in log
log.info "parsedJsonFiltered[0]: ${parsedJsonFiltered[0]}}"
log.info "expectedResults[0]: ${expectedResults[0]}}"

 

//////////////////////////////////////////////////////////////
//log output
Tue Sep 25 09:03:59 CDT 2018:INFO:ParsedJson Filtered: [[criteriaName:Impact_On_Functioning, levelOfCareName:Hospital_Level_of_Care, outcome:Met, parentCriteria:null, parentCriteriaName:null, programName:Hospital_Level_of_Care], [criteriaName:Physician_Determined, levelOfCareName:Hospital_Level_of_Care, outcome:Met, parentCriteria:null, parentCriteriaName:null, programName:Hospital_Level_of_Care], [criteriaName:Vulnerability_Requiring_24_Hour_Plan_Of_Care, levelOfCareName:Hospital_Level_of_Care, outcome:Met, parentCriteria:null, parentCriteriaName:null, programName:Hospital_Level_of_Care], [criteriaName:Specialized_Support_Need, levelOfCareName:Hospital_Level_of_Care, outcome:Met, parentCriteria:null, parentCriteriaName:null, programName:Hospital_Level_of_Care]]}

Tue Sep 25 09:03:59 CDT 2018:INFO:parsedJsonFiltered[0]: [criteriaName:Impact_On_Functioning, levelOfCareName:Hospital_Level_of_Care, outcome:Met, parentCriteria:null, parentCriteriaName:null, programName:Hospital_Level_of_Care]}

Tue Sep 25 09:03:59 CDT 2018:INFO:expectedResults[0]: [criteriaName:Impact_On_Functioning, levelOfCareName:Hospital_Level_of_Care, outcome:Met, parentCriteria:null, parentCriteriaName:null, programName:Hospital_Level_of_Care]}

//////////////////////////////////////////////////////////////
Assert Error:
assert parsedJsonFiltered[0] == expectedResults[0] | | | | | | | | | [criteriaName:Impact_On_Functioning, levelOfCareName:Hospital_Level_of_Care, outcome:Met, parentCriteria:null, parentCriteriaName:null, programName:Hospital_Level_of_Care] | | | [[criteriaName:Impact_On_Functioning, levelOfCareName:Hospital_Level_of_Care, outcome:Met, parentCriteria:null, parentCriteriaName:null, programName:Hospital_Level_of_Care]] | | false | [criteriaName:Impact_On_Functioning, levelOfCareName:Hospital_Level_of_Care, outcome:Met, parentCriteria:null, parentCriteriaName:null, programName:Hospital_Level_of_Care] [[criteriaName:Impact_On_Functioning, levelOfCareName:Hospital_Level_of_Care, outcome:Met, parentCriteria:null, parentCriteriaName:null, programName:Hospital_Level_of_Care], [criteriaName:Physician_Determined, levelOfCareName:Hospital_Level_of_Care, outcome:Met, parentCriteria:null, parentCriteriaName:null, programName:Hospital_Level_of_Care], [criteriaName:Vulnerability_Requiring_24_Hour_Plan_Of_Care, levelOfCareName:Hospital_Level_of_Care, outcome:Met, parentCriteria:null, parentCriteriaName:null, programName:Hospital_Level_of_Care], [criteriaName:Specialized_Support_Need, levelOfCareName:Hospital_Level_of_Care, outcome:Met, parentCriteria:null, parentCriteriaName:null, programName:Hospital_Level_of_Care]]

  • This is just a guess from looking at you post above, where you define your expected results (I've reformatted for clarity):

     

    def expectedResults = [
                           [
                            criteriaName: "Impact_On_Functioning", 
                            levelOfCareName: "Hospital_Level_of_Care", 
                            outcome: "Met", 
                            parentCriteria: "null", 
                            parentCriteriaName: "null", 
                            programName: "Hospital_Level_of_Care"
                           ]
                          ]

    I see that you are representing your null values as the string "null" which is different to a true null value. But when logged out would look the same. Try changing your expected reply to:

     

     

    def expectedResults = [
                           [
                            criteriaName: "Impact_On_Functioning", 
                            levelOfCareName: "Hospital_Level_of_Care", 
                            outcome: "Met", 
                            parentCriteria: null, 
                            parentCriteriaName: null, 
                            programName: "Hospital_Level_of_Care"
                           ]
                          ]

     

    Note: How I've dropped the quotes around the two null values.

     

    As I mentioned this is just a guess but could be worth a try.

  • tomovermeers's avatar
    tomovermeers
    Occasional Contributor

    Can you try ${expectedResults[0]}  instead of ${expectedResults[0]}} ?

    • rg2018's avatar
      rg2018
      Occasional Contributor

      ok, tried that, didn't change anything.....

  • try

     

    def expectedResults = [criteriaName: "Impact_On_Functioning", levelOfCareName: "Hospital_Level_of_Care", outcome: "Met", parentCriteria: "null", parentCriteriaName: "null", programName: "Hospital_Level_of_Care"]

     

    Remove extra []

     

    Click "Accept as Solution" if my answer has helped, and remember to give "kudos" :)

     

    Thanks and Regards,

    Himanshu Tayal

    • rg2018's avatar
      rg2018
      Occasional Contributor

      So I tried a few differant combos that were mentioned above.  Below I just commented out the each statement and just tried to compare via assert the parsedJsonFiltered (which I have limited down to just one of the criteria I need to look for, there are others but for the purpose of this debug I've just limited it to one) with the expected results. The Assert fails as if the compare between parsedJsonFiltered and expectedResults don't match but when I log the output or even look at the assert error the results do match.  Can't figure out what the issue is..????

       

       

      ------------------------

      Script:

      def responseMsg = messageExchange.response.responseContent
      def parsedJson = new groovy.json.JsonSlurper().parseText(context.response)
      //new variable to reduce the response list specific to levelOfCareName == 'Hospital_Level_of_Care'
      def parsedJsonFiltered = parsedJson.criteria_LIST.findAll{it.programName == 'Hospital_Level_of_Care' && it.criteriaName == 'Impact_On_Functioning'}

      //def expectedResults Array
      def expectedResults = [
      [criteriaName: "Impact_On_Functioning", levelOfCareName: "Hospital_Level_of_Care", outcome: "Met", parentCriteria: "null", parentCriteriaName: "null", programName: "Hospital_Level_of_Care"]
      // [levelOfCareName: "Hospital_Level_of_Care", criteriaName: "Impact_On_Functioning", outcome: "Met"],
      // [levelOfCareName: "Hospital_Level_of_Care", criteriaName: "Physician_Determined", outcome: "Met"],
      // [levelOfCareName: "Hospital_Level_of_Care", criteriaName: "Specialized_Support_Need", outcome: "Met"],
      // [levelOfCareName: "Hospital_Level_of_Care", criteriaName: "Clinical_Monitoring", outcome: "Met"],
      // [levelOfCareName: "Hospital_Level_of_Care", criteriaName: "Vulnerability_Requiring_24_Hour_Plan_Of_Care", outcome: "Met"]
      ]
      log.info "ParsedJson Filtered: ${parsedJsonFiltered}"
      log.info "expectedResults: ${expectedResults}"

      assert parsedJsonFiltered == expectedResults

      //go through each and assert
      //expectedResults.each
      //assert parsedJsonFiltered[0] == expectedResults
      //assert parsedJsonFiltered[1] == expectedResults[0]
      //assert parsedJsonFiltered[2] == expectedResults[1]
      //assert parsedJsonFiltered[3] == expectedResults[2]
      //assert parsedJsonFiltered[4] == expectedResults[4]
      //show filtered json in log
      //log.info "parsedJsonFiltered[0]: ${parsedJsonFiltered[0]}"
      //log.info "expectedResults[0]: ${expectedResults}"
      //log.info "parsedJsonFiltered[1]: ${parsedJsonFiltered[1]}}"
      //log.info "parsedJsonFiltered[2]: ${parsedJsonFiltered[2]}}"
      //log.info "parsedJsonFiltered[3]: ${parsedJsonFiltered[3]}}"
      //log.info "parsedJsonFiltered[4]: ${parsedJsonFiltered[4]}}"

      ----------------------------------

      log output:
      Thu Sep 27 07:34:00 CDT 2018:INFO:ParsedJson Filtered: [[criteriaName:Impact_On_Functioning, levelOfCareName:Hospital_Level_of_Care, outcome:Met, parentCriteria:null, parentCriteriaName:null, programName:Hospital_Level_of_Care]]
      Thu Sep 27 07:34:00 CDT 2018:INFO:expectedResults: [[criteriaName:Impact_On_Functioning, levelOfCareName:Hospital_Level_of_Care, outcome:Met, parentCriteria:null, parentCriteriaName:null, programName:Hospital_Level_of_Care]]

      -------------------------------------

      Assert Error:

      assert parsedJsonFiltered == expectedResults | | | | | [[criteriaName:Impact_On_Functioning, levelOfCareName:Hospital_Level_of_Care, outcome:Met, parentCriteria:null, parentCriteriaName:null, programName:Hospital_Level_of_Care]] | false [[criteriaName:Impact_On_Functioning, levelOfCareName:Hospital_Level_of_Care, outcome:Met, parentCriteria:null, parentCriteriaName:null, programName:Hospital_Level_of_Care]]

      -------------------------------------

      • Radford's avatar
        Radford
        Super Contributor

        This is just a guess from looking at you post above, where you define your expected results (I've reformatted for clarity):

         

        def expectedResults = [
                               [
                                criteriaName: "Impact_On_Functioning", 
                                levelOfCareName: "Hospital_Level_of_Care", 
                                outcome: "Met", 
                                parentCriteria: "null", 
                                parentCriteriaName: "null", 
                                programName: "Hospital_Level_of_Care"
                               ]
                              ]

        I see that you are representing your null values as the string "null" which is different to a true null value. But when logged out would look the same. Try changing your expected reply to:

         

         

        def expectedResults = [
                               [
                                criteriaName: "Impact_On_Functioning", 
                                levelOfCareName: "Hospital_Level_of_Care", 
                                outcome: "Met", 
                                parentCriteria: null, 
                                parentCriteriaName: null, 
                                programName: "Hospital_Level_of_Care"
                               ]
                              ]

         

        Note: How I've dropped the quotes around the two null values.

         

        As I mentioned this is just a guess but could be worth a try.