Forum Discussion

ebudihar's avatar
ebudihar
Occasional Contributor
7 years ago
Solved

REST api

dear all,

I have a question. I would like to write a groovy script to call REST using REST API. The same groovy script I used for SOAP request, it is working already. When I implement it and change the test case name, I got an error message "[{
   "errorCode": "xxx_ERROR",
   "message": "System.JSONException: Unexpected character (',' (code 44)): expected a valid value (number, String, array, object, 'true', 'false' or 'null') at [line:7, column:33]\n\n(System Code)\n"
}]"

 

Please help me. Thank you.

 

my REST request :

{   
   "INTEGRATIONID" : "testIntegration",
   "INTERFACENAME" : "testInterface",
   "messages": [
          {
               
                "AccountName": ${Property - batch-account-contact#AccountName},
                "Billing_Street": ${Property - batch-account-contact#Billing_Street},
                "AccountNo": ${Property - batch-account-contact#AccountNo},
                "Billing_City": ${Property - batch-account-contact#Billing_City},
                "Billing_Country": ${Property - batch-account-contact#Billing_Country}
                 
           }                                                                                         
   ]
}

 

my groovy script :

//grasping data from excel
import com.eviware.soapui.support.GroovyUtils;
import org.apache.poi.ss.usermodel.*
import org.apache.poi.hssf.usermodel.*
import org.apache.poi.xssf.usermodel.*
import org.apache.poi.ss.util.*  

//myTestCase contains the test case
def myTestCase = context.testCase   

//open an excel file
def fs = new FileInputStream("/xxx/batch-account.xlsx");

//def a workbook
Workbook wb = WorkbookFactory.create(fs)  

//def the worksheet location in the excel file
def ws = wb.getSheet("Sheet1")  

//count the rows
def r = ws.getPhysicalNumberOfRows()  
log.info "number of rows : " + r

// get the Property TestStep object
propTestStep = myTestCase.getTestStepByName("Property - batch-account")   
project = testRunner.getTestCase().getTestSuite().getProject().getWorkspace().getProjectByName("SkyvvaService")
testSuite = project.getTestSuiteByName("testSuite");
testCase = testSuite.getTestCaseByName("TestCase");   

//read the cell
for (def i=1; i<r; i++) {     
    def row = ws.getRow(i)     
    def c = row.getPhysicalNumberOfCells()           
    c=c+1
    for(def j=0; j<=c;j++) {     
        log.info "row : " + i    
        def cell = row.getCell(j)         
        if (j == 0) {             
            log.info "column : " + j            
            AccountNo = cell.getStringCellValue()             
            propTestStep.setPropertyValue("AccountNo", AccountNo)
            
            //the value is saved in the property             
            log.info AccountNo             
        }
        
        if (j == 1) {             
            log.info "column : " + j            
            AccountName = cell.getStringCellValue()             
            propTestStep.setPropertyValue("AccountName", AccountName)
            
            //the value is saved in the property             
            log.info AccountName         
        }

        if (j == 2) {             
            log.info "column : " + j            
            Billing_Street = cell.getStringCellValue()             
            propTestStep.setPropertyValue("Billing_Street", Billing_Street)
            
            //the value is saved in the property             
            log.info Billing_Street         
        }     

        if (j == 3) {             
            log.info "column : " + j            
            Billing_City = cell.getStringCellValue()             
            propTestStep.setPropertyValue("Billing_City", Billing_City)
            
            //the value is saved in the property             
            log.info Billing_City        
        }

        
        if (j == 4) {             
            log.info "column : " + j            
            Billing_Country = cell.getStringCellValue()             
            propTestStep.setPropertyValue("Billing_Country", Billing_Country)
            
            //the value is saved in the property             
            log.info Billing_Country         
        }

    }          
    log.info "Row #. " + r    
    // to run the SOAP request
    def step = context.testCase.testSteps['REST-integrateBatch']
    step.run(testRunner, context)
}

wb.close()

  • Property expansions are those times you use the ${ } notation inside some string in SoapUI. It's made to look similar to the groovy syntax for Strings (GStrings), but it's not quite the same thing. Property expansions can more easily find some things, like how you are using it to get a property belonging to a on another test step. This functionality was created inside SoapUI.

     

    As for your request, the data inside the messages object looks correct. But you have changed it from your first version which had an array of objects, similar to (1):

     

    "messages": [ { "abc" = "def" } ]

     

    But what you have now is not valid, similar to (2):

     

    "messages": [ "abc": "def "]

     

    Either go back to the first type (an array of objects) or you can just use a single object (3):

     

    "messages": { "abc":"def" }

    This will depend on what your API is expecting, either like (1) or like (3).

     

    {   
       "INTEGRATIONID" : "testIntegration",
       "INTERFACENAME" : "testInterface",
       "messages": [{
           "AccountName": "${Property - batch-account-contact#AccountName}",
           "Billing_Street": "${Property - batch-account-contact#Billing_Street}",
           "AccountNo": "${Property - batch-account-contact#AccountNo}",
           "Billing_City": "${Property - batch-account-contact#Billing_City}",
           "Billing_Country": "${Property - batch-account-contact#Billing_Country}"                                                                                      
       }]
    }

     

7 Replies

  • JHunt's avatar
    JHunt
    Community Hero

    When your data is being expanded, it's not being quoted.

     

    {
    "AccountName": Microsoft, "Billing_Street": 1 Microsoft Way, "AccountNo": 98052, "Billing_City": Redmond, WA, "Billing_Country": United States }

    Add quotes:

     

    {
    "AccountName": "Microsoft", "Billing_Street": "1 Microsoft Way", "AccountNo": "98052", "Billing_City": "Redmond, WA", "Billing_Country": "United States" }

     

    • ebudihar's avatar
      ebudihar
      Occasional Contributor

      dear JHunt,

      the reason I write like this

       

      "AccountName": ${Property - batch-account-contact#AccountName}

       

      because I retrieve the data from an excel file, and that is the way to retrieve data (variable syntax in soapUI).

       

      if using the real data, my rest request is working already.

       

  • JHunt's avatar
    JHunt
    Community Hero

    You need to do it like this, with quote marks around your property expansions.

     

       "messages": [
              {
                   
                    "AccountName": "${Property - batch-account-contact#AccountName}",
                    "Billing_Street": "${Property - batch-account-contact#Billing_Street}",
                    "AccountNo": "${Property - batch-account-contact#AccountNo}",
                    "Billing_City": "${Property - batch-account-contact#Billing_City}",
                    "Billing_Country": "${Property - batch-account-contact#Billing_Country}"
                     
               }                                                                                         
       ]
    • ebudihar's avatar
      ebudihar
      Occasional Contributor

      dear JHunt,

      a question, what is property expansions?

       

    • ebudihar's avatar
      ebudihar
      Occasional Contributor

      dear Jhunt,

      after I implement your suggestion. my REST request like this

      {   
         "INTEGRATIONID" : "testIntegration",
         "INTERFACENAME" : "testInterface",
         "messages": [
                "AccountName": "${Property - batch-account-contact#AccountName}",
                      "Billing_Street": "${Property - batch-account-contact#Billing_Street}",
                      "AccountNo": "${Property - batch-account-contact#AccountNo}",
                      "Billing_City": "${Property - batch-account-contact#Billing_City}",
                      "Billing_Country": "${Property - batch-account-contact#Billing_Country}"                                                                                      
         ]
      }

       

      I got another error message: "[{
         "errorCode": "APEX_ERROR",
         "message": "System.JSONException: Unexpected character (':' (code 58)): was expecting comma to separate ARRAY entries at [line:5, column:25]\n\n(System Code)\n"
      }]"

       

       

      I am sorry I still confuse where I should put your code.

       

       

    • ebudihar's avatar
      ebudihar
      Occasional Contributor

      dear JHunt,

      it is working now. Thanks a lot.

  • JHunt's avatar
    JHunt
    Community Hero

    Property expansions are those times you use the ${ } notation inside some string in SoapUI. It's made to look similar to the groovy syntax for Strings (GStrings), but it's not quite the same thing. Property expansions can more easily find some things, like how you are using it to get a property belonging to a on another test step. This functionality was created inside SoapUI.

     

    As for your request, the data inside the messages object looks correct. But you have changed it from your first version which had an array of objects, similar to (1):

     

    "messages": [ { "abc" = "def" } ]

     

    But what you have now is not valid, similar to (2):

     

    "messages": [ "abc": "def "]

     

    Either go back to the first type (an array of objects) or you can just use a single object (3):

     

    "messages": { "abc":"def" }

    This will depend on what your API is expecting, either like (1) or like (3).

     

    {   
       "INTEGRATIONID" : "testIntegration",
       "INTERFACENAME" : "testInterface",
       "messages": [{
           "AccountName": "${Property - batch-account-contact#AccountName}",
           "Billing_Street": "${Property - batch-account-contact#Billing_Street}",
           "AccountNo": "${Property - batch-account-contact#AccountNo}",
           "Billing_City": "${Property - batch-account-contact#Billing_City}",
           "Billing_Country": "${Property - batch-account-contact#Billing_Country}"                                                                                      
       }]
    }