Forum Discussion

Nmenon81's avatar
Nmenon81
Occasional Contributor
6 years ago
Solved

Decimal values in JDBC and JSON

I am trying to compare 2 responses - one JDBC and other json. However, the decimal values in my JDBC show up like this:

 <NUMBER_OF_UNITS>0.00000</NUMBER_OF_UNITS>

<MODE_PREMIUM>1200.00</MODE_PREMIUM>
 <FACE_AMOUNT>50000.00</FACE_AMOUNT>

and the JSON shows up like this:

"NumberOfUnits" : 0.0,

 "ModePremium" : 1200.0,
  "FaceAmount" : 50000.0,

 

How can I compare these fields since the decimal places do not match?

 

  • Radford's avatar
    Radford
    6 years ago

    Without seeing your full JDC & JSON responses I'm not sure what more advice I can give. I recomend that you read up on the XMLSlurper (to understand how it returns GPaths) and the the JsonSlurper (to understand how returns Maps and/or Lists).

     

    I did notice that the NodeChildren object has a text() method. You could try to see what this gives you, I've also added some log statements to help debugging:

     

    class Model { 
         def buildjdbcData(row){
            log.info('Calling buildjdbcData')
            log.info(row.MODE_PREMIUM.text())
            def FIELD7 = row.MODE_PREMIUM ? new BigDecimal(row.MODE_PREMIUM.text()) : row.MODE_PREMIUM
            return FIELD7
         }
    
         def buildJsonData(tagInfo){
             log.info('Calling buildJsonData')
             log.info(tagInfo.ModePremium.text())
             def FIELD7 = tagInfo.ModePremium ? new BigDecimal(tagInfo.ModePremium.text()) : tagInfo.ModePremium
             return FIELD7
         }
    }

6 Replies

  • Radford's avatar
    Radford
    Super Contributor

    (Edit: Sorry for the initial blank reply, my mistake)

     

    Your assertion is matching the string values, thus they are different. If you are only interested in the actual values I would add a Script Assertion to your request with something like this:

     

    // Obtain your values from the respective test steps.
    def jdbcText = '1200.00'
    def jsonText = '1200.0'
    
    // Convert to a numerical value.
    def jdbcValue = new BigDecimal(jdbcText)
    def jsonValue = new BigDecimal(jsonText)
    
    assert jdbcValue == jsonValue

     

    • Nmenon81's avatar
      Nmenon81
      Occasional Contributor

      Thank you - this works when I add the values that I want to convert to text as a variable and then do an assert check. But my script takes each row in the JDBC response/JSON response and compares them together. I am unsure where should I convert these values? This is how my script looks today -

      I tried few ways but I received the - java.math.BigDecimal(groovy.util.slurpersupport.NodeChildren error.

       

      class Model { 
           def buildjdbcData(row){
             
              def FIELD7 = row.MODE_PREMIUM
                     
                return FIELD7
      }


      def buildJsonData(tagInfo){
       
                def FIELD7 = tagInfo.ModePremium
                   
      return FIELD7
      }
      }
       
      def jdbcResponse = context.expand( '${JDBC Request#ResponseAsXml}' )
      def results = new XmlSlurper().parseText(jdbcResponse)
      def jdbcDataObjects = []
      results.ResultSet.Row.each { row ->
      jdbcDataObjects.add(new Model().buildjdbcData(row))
      }


      log.info "JDBC "+ jdbcDataObjects
       


      def restResponse = context.expand( '${Get Benefit summary#Response}' )

       

      def arrayOfTagInfo = new JsonSlurper().parseText(restResponse)

      def jsonDataObjects = []


      arrayOfTagInfo.GetBenefitSummaryResult.getBenefitSummaryRes.each{ tagInfo ->

      jsonDataObjects.add(new Model().buildJsonData(tagInfo))
      }

      log.info "JSON "+jsonDataObjects

      if (jdbcDataObjects.size() != jsonDataObjects.size()) {
      System.err.println("Jdbc resultset size is : ${jdbcDataObjects.size()} and Json result size is : ${jsonDataObjects.size()}")
      }
      assert jdbcDataObjects == jsonDataObjects

      • Radford's avatar
        Radford
        Super Contributor

        I'm finding it a little difficult to understand exactly what you are trying to achieve, but I think that the following amendment to the initial "Model" class should give you what you want. Note I've added a null check using the ternary operator (taking advantage of Groovy truth) so the behaviour of null values and empty strings will be the same as before (and stopping the BigDecimal constructor throwing an error).

         

        class Model { 
             def buildjdbcData(row){
                def FIELD7 = row.MODE_PREMIUM ? new BigDecimal(row.MODE_PREMIUM) : row.MODE_PREMIUM
                return FIELD7
             }
        
             def buildJsonData(tagInfo){
                 def FIELD7 = tagInfo.ModePremium ? new BigDecimal(tagInfo.ModePremium) : tagInfo.ModePremium
                 return FIELD7
             }
        }

         

        Note: The above code always assumes that the "mode premium" values are valid number, should it be possible for it to be not a number then you should add further checking code.