0

I have a REST API that retrieves a list of values. I am performing a SQL Query that retrieves the values from the Oracle DB. I am trying to find a way to compare/assert that the retrieved values from the API are the same ones with the ones stored in DB. My code is:

import groovy.json.JsonSlurper
import groovy.sql.GroovyRowResult

context.setProperty("crudManager", new CrudManager())
CrudManager.log = log

// CrudManager
public class CrudManager {

// Check Retrieved Games 
    public void checkRetrievedGames(testRunner, testCase){
        // Get Test Step's Response.
        def slurper = new JsonSlurper()
    def response = testRunner.testCase.getTestStepByName("Get Games").getPropertyValue("response")
    def responseJson = slurper.parseText(response)

    //Get the Games stored in DB
    def gameCode = "GM_CD"
   def query = "SELECT $gameCode FROM SCHEMA.PGM"
   def array = new OracleConnector().select(query)
   System.out.println(array);
   def gameCodesList = Arrays.asList(array)

    // Assertions - Compare response with DB.
    assert responseJson == gameCodesList
    }

}

When calling checkRetrievedGames function I get error Message: {Assertion failed: assert responseJson == gameCodesList | | | | | [[[GM_CD:15000], [GM_CD:15100], [GM_CD:15200], [GM_CD:15300], [GM_CD:15550], [GM_CD:15600], [GM_CD:15900], [GM_CD:16000]]] | false [15000, 15100, 15200, 15300, 15900, 16000] error at line: 5}

The retrieved json file is: [ 15000, 15100, 15200, 15300, 15900, 16000 ]

And the sql query response is: GM_CD 15000 15100 15200 15300 15550 15600 15900 16000

From what I understand I have to somehow remove the "GM_CODE:" from the retrieved list of the sql query. Thank you for your time

  • Sample input data used for compare would help. – Rao Dec 01 '16 at 09:27
  • Sample data input added! – Babis Paidokoukis Dec 01 '16 at 09:30
  • Thank you for the update. Is sql result is string or list as well? Is `GM_CD` prefix for all values? – Rao Dec 01 '16 at 09:35
  • GM_CD is the Column Name where the values 15000, 15100, etc are. And it is unique. – Babis Paidokoukis Dec 01 '16 at 09:37
  • Ok, but was it a list ? – Rao Dec 01 '16 at 09:37
  • Yes, you can sort it however you want. a simple sql query. – Babis Paidokoukis Dec 01 '16 at 09:39
  • If you use groovy script, this does exactly what you want - http://stackoverflow.com/questions/5713665/opposite-of-intersect-in-groovy-collections – Rao Dec 01 '16 at 09:42
  • Thank you for pointing this to me, I've just read it. I guess the only assertion needed then is: assert responseJson == grr ? – Babis Paidokoukis Dec 01 '16 at 09:45
  • With the above assertion, when calling checkRetrievedGames function I get a response: {-> org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object '[{GM_CD=15000}, {GM_CD=15100}, {GM_CD=15200}, {GM_CD=15300}, {GM_CD=15550}, {GM_CD=15600}, {GM_CD=15900}, {GM_CD=16000}]' with class 'java.util.ArrayList' to class 'groovy.sql.GroovyRowResult' due to: groovy.lang.GroovyRuntimeException: Could not find matching constructor for: groovy.sql.GroovyRowResult(groovy.sql.GroovyRowResult, )} – Babis Paidokoukis Dec 01 '16 at 09:56
  • May be you can update the question with latest script along with stacktrace, probably that might help. – Rao Dec 01 '16 at 10:06
  • Thanks for the help, updated Original Post. – Babis Paidokoukis Dec 01 '16 at 10:16
  • It does not show the stacktrace properly to locate the problem in the script or not an updated script present in the question. – Rao Dec 01 '16 at 11:42
  • I updated the code and the new error message of the assertion that is being retrieved, hope that helps. – Babis Paidokoukis Dec 01 '16 at 13:09

0 Answers0