brithwulf
brithwulf

Reputation: 548

Compare values gotten from request with database in Katalon Studio

I want to verify if the values from response is not different from database. I make a GET request and it gives me some information from database and I need to verify it somehow that the information is correct. I made a lot of things, here is my code.

@Keyword
    def getResource() {
        SQLHandler_test sql = new SQLHandler_test()
        SQLHandler_test1 sql_cards = new SQLHandler_test1()
        List<GroovyRowResult> res = sql.getSelectResults('SELECT TOP 2 PERSONAL_ID, CLIENT_NO, SMS_MOBILE_PHONE FROM CLIENTS WHERE PERSONAL_ID IS NOT NULL AND SMS_MOBILE_PHONE IS NOT NULL ')
        JsonSlurper slurper = new JsonSlurper()
        for(Map oneRow in res) {
            String personalId = oneRow.get("PERSONAL_ID")
            KeywordUtil.logInfo("Personal ID: " + personalId)
            println("Personal ID: " + personalId)

            String phone = oneRow.get("SMS_MOBILE_PHONE")

            KeywordUtil.logInfo("Phone number: " + phone )
            println("Mobile Phone: " + phone)
            // use variables in the GET request here
            RequestObject get_object = findTestObject('CUSTOMERS_Requests/Customer/CustomerByPersonalID/GET_CustomerByPersonalID')
            get_object.setRestUrl(String.format(get_object.getRestUrl(), personalId, phone))
            ResponseObject get_response = WS.sendRequestAndVerify(get_object)
            int statusCode = get_response.getStatusCode()

            KeywordUtil.logInfo("URL: " + get_object.getRestUrl())

            String getContent = get_response.getResponseBodyContent()
            KeywordUtil.logInfo("Status Code: " + statusCode + " Body Content: " + getContent)
            WS.verifyResponseStatusCode(get_response, 200)
            Map parsedJson = slurper.parseText(getContent)
            String parsed_personalID = parsedJson.get("personalId")
            String parsed_customerID = parsedJson.get("customerId")
            String parsed_mobilePN = parsedJson.get("mobilePhoneNumber")
            String parsed_canUseIB = parsedJson.get("cUIB")
            String client_no = oneRow.get("CLIENT_NO")
//          String parsed_expiryDate =  parsedJson.get("cardExpirationDate")
//          println(parsed_expiryDate)
            println(parsed_personalID)
            if(parsed_personalID == personalId){
                KeywordUtil.logInfo(parsed_personalID + " = " + personalId + " Matched !")
            }
            if(parsed_mobilePN == phone){
                KeywordUtil.logInfo(parsed_mobilePN + " = " + phone + " Matched !")
            }
            if(parsed_customerID == client_no) {
                KeywordUtil.logInfo(parsed_customerID + " = " + client_no + " Matched !")
            }
            List<GroovyRowResult> res_cards = sql_cards.getSelectResults('SELECT CONVERT(date, [EXPIRY_DATE]) "EXPIRY_DATE" FROM card.PCARDS WHERE CARD_CLIENT_NO = ' + client_no)
            for(Map oneRow_cards in res_cards) {
                String expiry_date = oneRow_cards.get("EXPIRY_DATE")
                println(expiry_date)
            }
        }
        return res
    }

It was not a problem with comparing phone, personal_id and customer_id, but I can't compare card information.

This is how response looks like.

{
  "customerId": 5555,
  "personalId": "1123",
  "mobilePhoneNumber": "2233",
  "cUIB": true,
  "cards": [
    {
      "cardExpirationDate": "2020-05-31",
      "cardNumber": "1"
    },
    {
      "cardExpirationDate": "2012-03-31",
      "cardNumber": "2"
    },
    {
      "cardExpirationDate": "2008-03-31",
      "cardNumber": "33"
    },
    {
      "cardExpirationDate": "2020-09-30",
      "cardNumber": "522"
    },
    {
      "cardExpirationDate": "2018-04-30",
      "cardNumber": "5"
    },
    {
      "cardExpirationDate": "2014-03-31",
      "cardNumber": "23"
    }
  ]
}

I made a code where I can compare only for one request, but I want to make so that it should compare for many requests and cards quantity will be different.

Upvotes: 1

Views: 586

Answers (1)

Mate Mrše
Mate Mrše

Reputation: 8424

You can get the card information with this loop (you can see how to parse a JSON with multiple levels here):

for (def i = 0; i<parsedJson.cards.size(); i++){

    println parsedJson.cards[i].cardExpirationDate
    println parsedJson.cards[i].cardNumber

}

Upvotes: 1

Related Questions