Forum Discussion

luluberlu's avatar
luluberlu
Occasional Contributor
7 years ago

Add a check of non existence in a API response

i have this kind of API response : 

 

{"response": {"responsesRecherche": {
   "1":    {
      "consultation":       {
         "Id": "16391821108191",
         "referenceFonctionnelle": "Dume OE US 78 - CTR016",
         "idTypeProcedure": "01",
         "idNatureMarket": "01",
         "idTypeMarket": "01"
      },
      "lot": [{"numeroLot": "016"}],
      "Opérateur Economique": {"id": "38103128500491"},
      "Dume OE":       {
         "dumeOEId": "mybmttmy",
         "statutDume": "REMPLACE",
         "nouveauDume":          {
            "dumeOEId": "uxudjs9s",
            "statutDume": "VALIDE",
            "accessible": true,
            "Date de création": "2018-09-11 04:57:22",
            "Date de modification": "2018-09-11 04:57:22"
         },
         "accessible": true,
         "Date de création": "2018-09-11 04:57:00",
         "Date de modification": "2018-09-11 04:57:22"
      }
   },
   "2":    {
      "consultation":       {
         "Id": "56559466095920",
         "referenceFonctionnelle": "Dume OE US 78 - CTR016",
         "idTypeProcedure": "01",
         "idNatureMarket": "01",
         "idTypeMarket": "01"
      },
      "lot": [{"numeroLot": "03"}],
      "Opérateur Economique": 18,
      "Dume OE":       {
         "dumeOEId": "qo92j8tt",
         "statutDume": "VALIDE",
         "accessible": true,
         "Date de création": "2018-09-11 04:12:02",
         "Date de modification": "2018-09-11 04:12:02"
      }
   },
...

...

   "50":    {
      "consultation":       {
         "Id": "31739414944098",
         "referenceFonctionnelle": "Dume A - CTR021",
         "idTypeProcedure": "01",
         "idNatureMarket": "01",
         "idTypeMarket": "01"
      },
      "lot": [{"numeroLot": "78"}],
      "Opérateur Economique": 18,
      "Dume OE":       {
         "dumeOEId": "7bewwsrj",
         "statutDume": "VALIDE",
         "accessible": true,
         "Date de création": "2018-09-13 08:41:44",
         "Date de modification": "2018-09-13 08:41:44"
      }
   },
}}}

 

I have to check creation dates of the Dume OE object. "Date de création" in french. 

So my following script works perfectly when all Dume OE objects are complete recorded.

But we have new problem, API returns incomplete response for some Dume OE : 

   "42":    {
      "consultation": "16391821108191",
      "lot": [{"numeroLot": "03"}],
      "Opérateur Economique": 18,
      "Dume OE": "uxudjs9s"
   },
there is no information on Dume OE object. I have to add a IF which check "date de création" existence,

before check his format and value.. I try "contains" method, but i have groovy error :(

 

My script : 

 

import net.sf.*
import net.sf.json.*
import net.sf.json.groovy.*
import com.eviware.soapui.support.XmlHolder
def GU = new com.eviware.soapui.support.GroovyUtils( context )

def Response = context.expand ( '${CP_CTR006_DateDebutSansDateFin#Response}').toString()
def Slurp = new JsonSlurper().parseText(Response)
 .response.responsesRecherche
 .collect {keyname, keyvalue -> keyvalue } // créé la liste de valeurs derrière le compteur
 .collect { it."Dume OE"}
 
def cpt = Slurp.size()
log.info " Dates trouvées = " + cpt

def i=0
checkdate = true
def testdate = testRunner.testCase.testSuite.getProperty("DateCreation").value
testdate = testdate.substring(1,11)
testdate = testdate + " 00.00.00"
while (i<cpt && checkdate==true)
{

// TO DO : Add condition IF "Date de création" not exist in Slurp[i] object before check date
 if (Slurp[i]."Date de création" < testdate) {checkdate = false }
  assert checkdate
   i++
}
if (checkdate) {log.info "Toutes les dates sont postérieures à " + testdate }

 

It is not hard but i am weak with collection objects :(

  • hi.

    I try but it don't work. same error "groovy.lang.MissingPropertyException: No such property: Date de création for class: java.lang.String

    I try this just after : 

    if (Slurp[i].("Date de création")) {
      if (Slurp[i]."Date de création" < testdate) {checkdate = false }
      assert checkdate
       i++}
    }

    but i got the same error :( 

    i think i should use "contains" method rightly. 

  • JHunt's avatar
    JHunt
    Community Hero

    Hi,

    You can use this, and this pattern is used quite often:

     if (Slurp[i]."Date de création" && Slurp[i]."Date de création" < testdate) {checkdate = false }

    First it will check the conditiion on the left - if there's a value for the date, the left will be satisfied and then it goes on to check the date dagainst the test date.

     

    But if there's no date returned, the condition on the left will fail, and so the whole thing will be false. In that case, the check on the right won't actually be calculated (which is good, because it means it won't throw an exception for trying to compare nothing to the testdate).

    • luluberlu's avatar
      luluberlu
      Occasional Contributor

      hi.

      I try but it don't work. same error "groovy.lang.MissingPropertyException: No such property: Date de création for class: java.lang.String

      I try this just after : 

      if (Slurp[i].("Date de création")) {
        if (Slurp[i]."Date de création" < testdate) {checkdate = false }
        assert checkdate
         i++}
      }

      but i got the same error :( 

      i think i should use "contains" method rightly. 

      • luluberlu's avatar
        luluberlu
        Occasional Contributor

        i found solution this noon.

        i coudld not test key existence correctly. i try with Slurp[i].containsKey("Date de création"] and it dont works also :(

        So i convert my object into a string and use indexof method, and it is ok now. not perfect but it works.

         

        def Str = Slurp[i].toString()

        def DC = Str.indexOf("Date de création")

        if (DC == -1) { log.info "ATTENTION..."

        else { 

                if Slurp[i]."Date de création" < tesdate) {

        ...

        }}