Forum Discussion

rg2018's avatar
rg2018
Occasional Contributor
6 years ago

Help with groovy script

According to the log, parsedJson.criteriaList[count] contains 

INFO:Combined: [criteriaName:Impact_On_Functioning, outcome:Met]}

and I want to match "Impact_On_Functioning, outcome:Met" however in my case statement below

"~/.*(?i)Impact_On_Functioning, outcome.*/:"  I can match only up to the colon, if I include the colon it comes back not matching.  I can not get the right regex to match the complete string(Impact_On_Functioning, outcome:Met).  Anyone have any suggestions?

 

groovy script assertion and log below in SoapUI (free version)

---------------------------------------------------------------
//grab the response to parse out json
def responseMsg = messageExchange.response.responseContent
def parsedJson = new groovy.json.JsonSlurper().parseText(context.response)
def int count = 0;
def int success;

parsedJson.criteriaList.each
{
log.info "Combined: ${parsedJson.criteriaList[count]}}"

switch (parsedJson.criteriaList[count]) {
case ~/.*(?i)Impact_On_Functioning, outcome.*/:
log.info("pass")
success = 1
break;
default:
log.info("Doesn't work!")
success = 0;
}
log.info "Item: $count"
log.info "done in each"
count++;


}

log.info "Success: $success";
if(success == 1) {
log.info "Pass123";
log.info "Pass123: $success"
} else{
log.info "Fail123";
log.info "Fail123: $success"
assert success == 1 : "success should be 1";
}

log.info "done"

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

Log:

Wed Aug 29 06:25:29 CDT 2018:INFO:Combined: [criteriaName:Impact_On_Functioning, outcome:Met]}
Wed Aug 29 06:25:29 CDT 2018:INFO:pass
Wed Aug 29 06:25:29 CDT 2018:INFO:Item: 0
Wed Aug 29 06:25:29 CDT 2018:INFO:done in each
Wed Aug 29 06:25:29 CDT 2018:INFO:Combined: [criteriaName:Diagnostic_Determination, outcome:Met]}
Wed Aug 29 06:25:29 CDT 2018:INFO:Doesn't work!
Wed Aug 29 06:25:29 CDT 2018:INFO:Item: 1
Wed Aug 29 06:25:29 CDT 2018:INFO:done in each
Wed Aug 29 06:25:29 CDT 2018:INFO:Combined: [criteriaName:Specialized_Support_Need, outcome:Met]}
Wed Aug 29 06:25:29 CDT 2018:INFO:Doesn't work!
Wed Aug 29 06:25:29 CDT 2018:INFO:Item: 2
Wed Aug 29 06:25:29 CDT 2018:INFO:done in each
Wed Aug 29 06:25:29 CDT 2018:INFO:Combined: [criteriaName:Vulnerability_Requiring_24_Hour_Plan_Of_Care, outcome:Met]}
Wed Aug 29 06:25:29 CDT 2018:INFO:Doesn't work!
Wed Aug 29 06:25:29 CDT 2018:INFO:Item: 3
Wed Aug 29 06:25:29 CDT 2018:INFO:done in each
Wed Aug 29 06:25:29 CDT 2018:INFO:Combined: [criteriaName:Vulnerability_Requiring_24_Hour_Plan_Of_Care, outcome:Met]}
Wed Aug 29 06:25:29 CDT 2018:INFO:Doesn't work!
Wed Aug 29 06:25:29 CDT 2018:INFO:Item: 4
Wed Aug 29 06:25:29 CDT 2018:INFO:done in each
Wed Aug 29 06:25:29 CDT 2018:INFO:Combined: [criteriaName:Medical_Assistance, outcome:Not_Met]}
Wed Aug 29 06:25:29 CDT 2018:INFO:Doesn't work!
Wed Aug 29 06:25:29 CDT 2018:INFO:Item: 5
Wed Aug 29 06:25:29 CDT 2018:INFO:done in each
Wed Aug 29 06:25:29 CDT 2018:INFO:Combined: [criteriaName:Residential_Habitation, outcome:Not_Met]}
Wed Aug 29 06:25:29 CDT 2018:INFO:Doesn't work!
Wed Aug 29 06:25:29 CDT 2018:INFO:Item: 6
Wed Aug 29 06:25:29 CDT 2018:INFO:done in each
Wed Aug 29 06:25:29 CDT 2018:INFO:Combined: [criteriaName:Developmental_Disability_Or_Related_Condition, outcome:Met]}
Wed Aug 29 06:25:29 CDT 2018:INFO:Doesn't work!
Wed Aug 29 06:25:29 CDT 2018:INFO:Item: 7
Wed Aug 29 06:25:29 CDT 2018:INFO:done in each
Wed Aug 29 06:25:29 CDT 2018:INFO:Combined: [criteriaName:Activities_Of_Daily_Living_ADLs, outcome:Not_Met]}
Wed Aug 29 06:25:29 CDT 2018:INFO:Doesn't work!
Wed Aug 29 06:25:29 CDT 2018:INFO:Item: 8
Wed Aug 29 06:25:29 CDT 2018:INFO:done in each
Wed Aug 29 06:25:29 CDT 2018:INFO:Combined: [criteriaName:Activities_Of_Daily_Living_ADLs, outcome:Not_Met]}
Wed Aug 29 06:25:29 CDT 2018:INFO:Doesn't work!
Wed Aug 29 06:25:29 CDT 2018:INFO:Item: 9
Wed Aug 29 06:25:29 CDT 2018:INFO:done in each
Wed Aug 29 06:25:29 CDT 2018:INFO:Combined: [criteriaName:Instrumental_Activities_Of_Daily_Living_IADLs, outcome:Does_not_meet_Criteria_of_Instrumental_Activites_Of_Daily_Living]}
Wed Aug 29 06:25:29 CDT 2018:INFO:Doesn't work!
Wed Aug 29 06:25:29 CDT 2018:INFO:Item: 10
Wed Aug 29 06:25:29 CDT 2018:INFO:done in each
Wed Aug 29 06:25:29 CDT 2018:INFO:Combined: [criteriaName:Medical_Assistance, outcome:Not_Met]}
Wed Aug 29 06:25:29 CDT 2018:INFO:Doesn't work!
Wed Aug 29 06:25:29 CDT 2018:INFO:Item: 11
Wed Aug 29 06:25:29 CDT 2018:INFO:done in each
Wed Aug 29 06:25:29 CDT 2018:INFO:Combined: [criteriaName:Program_Access_Criteria, outcome:Not_Met]}
Wed Aug 29 06:25:29 CDT 2018:INFO:Doesn't work!
Wed Aug 29 06:25:29 CDT 2018:INFO:Item: 12
Wed Aug 29 06:25:29 CDT 2018:INFO:done in each
Wed Aug 29 06:25:29 CDT 2018:INFO:Combined: [criteriaName:Twenty_Four_Hour_Plan_Of_Care, outcome:Met]}
Wed Aug 29 06:25:29 CDT 2018:INFO:Doesn't work!
Wed Aug 29 06:25:29 CDT 2018:INFO:Item: 13
Wed Aug 29 06:25:29 CDT 2018:INFO:done in each
Wed Aug 29 06:25:29 CDT 2018:INFO:Combined: [criteriaName:Impact_On_Functioning, outcome:Not_Met]}
Wed Aug 29 06:25:29 CDT 2018:INFO:pass
Wed Aug 29 06:25:29 CDT 2018:INFO:Item: 14
Wed Aug 29 06:25:29 CDT 2018:INFO:done in each
Wed Aug 29 06:25:29 CDT 2018:INFO:Success: 1
Wed Aug 29 06:25:29 CDT 2018:INFO:Pass123
Wed Aug 29 06:25:29 CDT 2018:INFO:Pass123: 1
Wed Aug 29 06:25:29 CDT 2018:INFO:done

  • Use

     

    case ~/.*Impact_On_Functioning, outcome=Met.*/:

    When you are using "case" with a regex pattern, Groovy will invoke Pattern.isCase(String) method. Looks like the implementation must rely on toString(), while your log statement using ${ } syntax is relying on as String.

     

    String json = '''{
        "criteriaList": [
             {
                 "criteriaName": "Impact_On_Functioning",
                 "outcome": "Met"
             }
        ]
    }'''
    
    def parsedJson = new groovy.json.JsonSlurper().parseText(json)
    
    assert (parsedJson.criteriaList[0] as String) == '[criteriaName:Impact_On_Functioning, outcome:Met]'
    assert "${parsedJson.criteriaList[0]}" == (parsedJson.criteriaList[0] as String)
    assert parsedJson.criteriaList[0] ==~ ~/.*Impact_On_Functioning, outcome:Met.*/
    
    assert parsedJson.criteriaList[0].toString() == '{criteriaName=Impact_On_Functioning, outcome=Met}'
    assert parsedJson.criteriaList[0].toString() ==~ ~/.*Impact_On_Functioning, outcome=Met.*/
    assert (~/.*Impact_On_Functioning, outcome=Met.*/).isCase( parsedJson.criteriaList[0] )
    
    switch (parsedJson.criteriaList[0]) {
        case ~/.*Impact_On_Functioning, outcome=Met.*/:
        break
        default:
            assert false
        break
    }

     

    Also, consider whether something like this is simpler for you than using Regex at all.

    def expectedResults = [
        [criteriaName: "Impact_On_Functioning", outcome: "Met"],
        [criteriaName: "Diagnostic_Determination", outcome: "Met"],
        [criteriaName: "Specialized_Support_Need", outcome: "Met"]
        //...
    ]
    
    expectedResults.each { assert it in parsedJson.criteriaList }

     

2 Replies

  • JHunt's avatar
    JHunt
    Community Hero

    Use

     

    case ~/.*Impact_On_Functioning, outcome=Met.*/:

    When you are using "case" with a regex pattern, Groovy will invoke Pattern.isCase(String) method. Looks like the implementation must rely on toString(), while your log statement using ${ } syntax is relying on as String.

     

    String json = '''{
        "criteriaList": [
             {
                 "criteriaName": "Impact_On_Functioning",
                 "outcome": "Met"
             }
        ]
    }'''
    
    def parsedJson = new groovy.json.JsonSlurper().parseText(json)
    
    assert (parsedJson.criteriaList[0] as String) == '[criteriaName:Impact_On_Functioning, outcome:Met]'
    assert "${parsedJson.criteriaList[0]}" == (parsedJson.criteriaList[0] as String)
    assert parsedJson.criteriaList[0] ==~ ~/.*Impact_On_Functioning, outcome:Met.*/
    
    assert parsedJson.criteriaList[0].toString() == '{criteriaName=Impact_On_Functioning, outcome=Met}'
    assert parsedJson.criteriaList[0].toString() ==~ ~/.*Impact_On_Functioning, outcome=Met.*/
    assert (~/.*Impact_On_Functioning, outcome=Met.*/).isCase( parsedJson.criteriaList[0] )
    
    switch (parsedJson.criteriaList[0]) {
        case ~/.*Impact_On_Functioning, outcome=Met.*/:
        break
        default:
            assert false
        break
    }

     

    Also, consider whether something like this is simpler for you than using Regex at all.

    def expectedResults = [
        [criteriaName: "Impact_On_Functioning", outcome: "Met"],
        [criteriaName: "Diagnostic_Determination", outcome: "Met"],
        [criteriaName: "Specialized_Support_Need", outcome: "Met"]
        //...
    ]
    
    expectedResults.each { assert it in parsedJson.criteriaList }

     

    • rg2018's avatar
      rg2018
      Occasional Contributor

      Thanks so much! I was making it way too complicated!

      I now have this and it's working great!

       

      def responseMsg = messageExchange.response.responseContent
      def parsedJson = new groovy.json.JsonSlurper().parseText(context.response)
      def expectedResults = [
      [criteriaName: "Impact_On_Functioning", outcome: "Met"],
      [criteriaName: "Diagnostic_Determination", outcome: "Met"],
      [criteriaName: "Specialized_Support_Need", outcome: "Met"]
      ]
      expectedResults.each
      assert parsedJson.criteriaList[0] == expectedResults[0]
      assert parsedJson.criteriaList[1] == expectedResults[1]
      assert parsedJson.criteriaList[2] == expectedResults[2]

       

      One more question, if the json response is still in dev(so changes are happening) what's the best way to parameterize assertions so you don't have to go into the assertion, and say for this example above modify the:

      def expectedResults = [
      [criteriaName: "Impact_On_Functioning", outcome: "Met"],
      [criteriaName: "Diagnostic_Determination", outcome: "Met"],
      [criteriaName: "Specialized_Support_Need", outcome: "Met"]
      ]

      I'd like to just go into the test case or some file and update the expected results for the assertion in the test case.