SoapUI / Groovy动态地将JSON响应与数组进行比较

时间:2015-10-22 15:40:47

标签: arrays json groovy soapui

我正在尝试将JSON响应中的节点值与从我为其构建数组的excel电子表格中收集的值进行比较。该数组包含多列的值,但我想查看JSON节点值是否在数组中。最终我需要将多个节点值与数组进行比较。我现在似乎无法让这个只为一个人工作。我做错了什么?

import com.eviware.soapui.*;
import java.lang.*;
import java.util.*;
import java.io.*;
import java.net.*;
import groovy.lang.*;
import groovy.util.*;
import groovy.json.JsonSlurper;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

//Create an array to hold the cell values
def cellValues = [];
//Get the JSON in a format to easily access
def groovyUtils = new com.eviware.soapui.support.GroovyUtils(context);
def ResponseMessage = testRunner.testCase.testSteps["ProductSkuLookupService"].getPropertyValue("response");
def jsonResult = new JsonSlurper().parseText(ResponseMessage);


//Get the number of recommended expressions nodes
def rspnsNumRcmndExprsns = jsonResult.product.recommendedExpressions.size();
//Get the file from the location specified in the test case custom properties
def String dataFilename = context.expand( '${#TestCase#dataFilename}' );

if(dataFilename != null && !dataFilename.equals(""))
{
    XSSFWorkbook wb = new XSSFWorkbook(dataFilename);
    XSSFSheet sheet = wb.getSheet("exprsnList");
    XSSFRow row; 
    XSSFCell cell;

    def totalNumRows;
    def numRows;
    //Get the total number of rows with data on the sheet
    totalNumRows = sheet.getPhysicalNumberOfRows();
    //log.info " The total number of rows are " + totalNumRows;
    //Get the number of rows without the header row
    numRows = totalNumRows - 1;
    //log.info " The number of rows to use are " + rows;

    def cols = 0;

    cols = sheet.getRow(0).getPhysicalNumberOfCells();
    //log.info " The total number of columns are " + cols;

    if(numRows == rspnsNumRcmndExprsns)
    {
        for(int i = 1; i < totalNumRows; i++)
        {
            row = sheet.getRow(i);
            //log.info " Row # " + i;

            for(int j = 0; j < cols; j++)
            {
                //log.info "  Cell # " + j;
                cell = row.getCell(j);
                //log.info "  The value in the cell is " + cell;

                cellValues.push(cell);
            }

        }

        log.info jsonResult.product["recommendedExpressions"]["imageUrl"].every{it in [cellValues]}

    }

    else
    {
        assert false, "The number of nodes does not match the number of rows.";
    }
}
else
{
    assert false, "The file name is missing.";
}

return;

这里是JSON的一部分(JSON中有多个子数组):

{
   "responseHeader":    {
      "status": "SUCCESS",
      "messages": []
   },
   "product":    {
      "recommendedExpressions":       [
                  {
            "imageUrl": "/images/products/expressions/ex015.jpg",
            "id": "sku83283exp",
            "description": " ",
            "code": "EX015"
         },
                  {
            "imageUrl": "/images/products/expressions/ex100.jpg",
            "id": "sku81486exp",
            "description": " ",
            "code": "EX100"
         },
                  {
            "imageUrl": "/images/products/expressions/ex036.jpg",
            "id": "sku82518exp",
            "description": " ",
            "code": "EX036"
         }
      ]
   }
}

1 个答案:

答案 0 :(得分:0)

您正在将Cell推送到阵列。我认为你的要求是推送单元内容到数组。

取代这个:

for(int j = 0; j < cols; j++)
            {
                //log.info "  Cell # " + j;
                cell = row.getCell(j);
                //log.info "  The value in the cell is " + cell;

                cellValues.push(cell);
            }

使用以下代码

for(int j = 0; j < cols; j++)
            {
                //log.info "  Cell # " + j;
                cell = row.getCell(j);
                //log.info "  The value in the cell is " + cell;

                cellValues.push(cell.getStringCellValue());
            }