Как проверить и сопоставить столбцы запросов из запросов БД с объектом ответа JSON

#junit #java-8 #testng #assert #rest-assured-jsonpath

Вопрос:

В настоящее время существует процесс, в котором столбец запроса и значение из файла свойств проверяются по полю в теле ответа json, т. е. столбец ITM_NBR в запросе сопоставляется с responseString.item_NBR в ответе json. Этот процесс получает запрос по указанному индексу (имя запроса из файла свойств).

Проблема в том, что он не является динамическим, и если было добавлено больше столбцов или api был изменен, чтобы включить новые или удаленные поля, то этот метод необходимо обновлять каждый раз. Я хотел бы найти способ динамического сопоставления запроса и его столбцов с полями ответа соответственно. Я не уверен, как этого добиться. Ниже приведен код из метода:

     @Then("Verify the accuracy table data from response {string}")
    public void verify_the_accuracy_table_data(String queryKey) {
        List<Object> expResponseFiscWkEndDateList = new ArrayList<Object>();
        List<Object> expResponseLagList = new ArrayList<Object>();
        List<Object> expResponseYearList = new ArrayList<Object>();
        List<Object> expResponseWeekList = new ArrayList<Object>();
        List<Object> expResponseProduct = new ArrayList<Object>();
        List<Object> expResponseLocation = new ArrayList<Object>();
        String query = sqlProps.getProperty(queryKey);
        expResponseFiscWkEndDateList = HBaseDatabaseConfig.getDataFromDb(query, 1);
        expResponseLagList = HBaseDatabaseConfig.getDataFromDb(query, 2);
        expResponseYearList = HBaseDatabaseConfig.getDataFromDb(query, 3);
        expResponseWeekList = HBaseDatabaseConfig.getDataFromDb(query, 4);
        expResponseProduct = HBaseDatabaseConfig.getDataFromDb(query, 5);
        expResponseLocation = HBaseDatabaseConfig.getDataFromDb(query, 6);
    
        String response = gResponse.asString();
        String str = response.split(":")[1].replace("}", "");
        try {
            if (!str.equals("[]")) {
                JsonPath json = new JsonPath(response);
                for (int i = 0; i < expResponseYearList.size(); i  ) {
                    
                    switch(query) {
                    case "getAcc_Merch_MerchDivision_NatlTableQuery":
                        sortAndAssertForString(expResponseProduct, "accuracyTableResponse.mer_SBO_DVS_NBR", json, i);
                        break;
                        
                    case "getAcc_Merch_MerchSubDivision_NatlTableQuery":
                        sortAndAssertForString(expResponseProduct, "accuracyTableResponse.prd_GRP_NBR", json, i);
                        break;
                        
                    case "getAcc_Merch_ProductGroup_NatlTableQuery":
                        sortAndAssertForString(expResponseProduct, "accuracyTableResponse.asr_NBR", json, i);
                        break;
                        
                    case "getAcc_Merch_Assortment_NatlTableQuery":
                        sortAndAssertForString(expResponseProduct, "accuracyTableResponse.itm_NBR", json, i);
                        break;
                        
                    case "getAcc_Merch_Item_NatlTableQuery":
                        sortAndAssertForString(expResponseProduct, "accuracyTableResponse.itm_NBR", json, i);
                        break;
                        
                    default:
                        Reporter.log("Querykey should not be empty", true);
                        break;
                    }
                    
                    /*sortAndAssertForString(expResponseLagList, "accuracyTableResponse.lag", json, i);
                    sortAndAssertForString(expResponseWeekList, "accuracyTableResponse.fsc_WK_NBR", json, i);
                    sortAndAssertForString(expResponseYearList, "accuracyTableResponse.fsc_YR_NBR", json, i);
                    sortAndAssertForString(expResponseLocationList, "accuracyTableResponse.loc_NBR", json, i);*/
                }
            }
            else {
                Assert.fail();
        }
        } catch (Exception e) {
            Reporter.log("The response is empty");
        }
    }
 

Метод сортировки и утверждения:

 public void sortAndAssertForString(List<Object> expResponseList, String responseNode, JsonPath json, int index) {
        List<Object> actualStringResponse = new ArrayList<Object>();
        actualStringResponse = json.get(responseNode);
        if (actualStringResponse == null) {
            Reporter.log("The response is empty", true);
            Assert.fail();

        } 
        
        else {
            System.out.println("Getting response string as "   actualStringResponse.get(index));
            AsserEquals_Object(actualStringResponse.get(index), expResponseList.get(index),
                    "Actual and Expected "   responseNode   " are same");
        }

    }
 

Query.properties example (shortened):

 
getAcc_Merch_MerchDivision_NatlTableQuery = SELECT t1.MER_SBO_DVS_NBR AS MER_SBO_DVS_NBR, t1.FSC_WK_END_DT AS FSC_WK_END_DT, t1.LAG AS LAG, t1.FSC_YR_NBR AS FSC_YR_NBR, t1.FSC_WK_NBR AS FSC_WK_NBR, t1.TOT_SALES_UNITS AS TOT_SALES_UNITS, t1.AVG_SALES_UNITS AS AVG_SALES_UNITS, t1.TOT_FC_UNITS_LIFT AS TOT_FC_UNITS_LIFT, t1.TOT_FC_UNITS_BY AS TOT_FC_UNITS_BY, t1.AVG_FC_UNITS_LIFT AS AVG_FC_UNITS_LIFT, t1.AVG_FC_UNITS_BY AS AVG_FC_UNITS_BY, t1.MAE_LIFT AS MAE_LIFT, t1.MAE_BY AS MAE_BY, t1.MAPE_LIFT AS MAPE_LIFT, t1.MAPE_BY AS MAPE_BY, t1.BIAS_LIFT AS BIAS_LIFT, t1.BIAS_BY AS BIAS_BY FROM FC.ACCURACY_MER_SBO_DVS_NBR_NATL t1 WHERE EXISTS (SELECT 1 FROM L_TABLES.I0044_MER_SBO_DVS t2 WHERE t2.MER_DVS_NBR = 21 AND t2.MER_SBO_DVS_NBR = t1.MER_SBO_DVS_NBR) AND t1.LAG IN (0,1,2,4,6,8,12,26) AND substr(to_char(t1.FSC_WK_END_DT), 1, 10) = 'LastFscWkEdDate' ORDER BY t1.FSC_YR_NBR, t1.FSC_WK_NBR

getAcc_Merch_MerchSubDivision_NatlTableQuery = SELECT t1.PRD_GRP_NBR AS PRD_GRP_NBR, t1.FSC_WK_END_DT AS FSC_WK_END_DT, t1.LAG AS LAG, t1.FSC_YR_NBR AS FSC_YR_NBR, t1.FSC_WK_NBR AS FSC_WK_NBR, t1.TOT_SALES_UNITS AS TOT_SALES_UNITS, t1.AVG_SALES_UNITS AS AVG_SALES_UNITS, t1.TOT_FC_UNITS_LIFT AS TOT_FC_UNITS_LIFT, t1.TOT_FC_UNITS_BY AS TOT_FC_UNITS_BY, t1.AVG_FC_UNITS_LIFT AS AVG_FC_UNITS_LIFT, t1.AVG_FC_UNITS_BY AS AVG_FC_UNITS_BY, t1.MAE_LIFT AS MAE_LIFT, t1.MAE_BY AS MAE_BY, t1.MAPE_LIFT AS MAPE_LIFT, t1.MAPE_BY AS MAPE_BY, t1.BIAS_LIFT AS BIAS_LIFT, t1.BIAS_BY AS BIAS_BY FROM FC.ACCURACY_PRD_GRP_NBR_NATL t1 WHERE EXISTS (SELECT 1 FROM L_TABLES.I0045_PRD_GRP t2 WHERE t2.MER_SBO_DVS_NBR = merSboDvsNbr AND t2.PRD_GRP_NBR = t1.PRD_GRP_NBR) AND t1.LAG IN (0,1,2,4,6,8,12,26) AND substr(to_char(t1.FSC_WK_END_DT), 1, 10) = 'LastFscWkEdDate' ORDER BY t1.FSC_YR_NBR, t1.FSC_WK_NBR

getAcc_Merch_ProductGroup_NatlTableQuery = SELECT t1.ASR_NBR AS ASR_NBR, t1.FSC_WK_END_DT AS FSC_WK_END_DT, t1.LAG AS LAG, t1.FSC_YR_NBR AS FSC_YR_NBR, t1.FSC_WK_NBR AS FSC_WK_NBR, t1.TOT_SALES_UNITS AS TOT_SALES_UNITS, t1.AVG_SALES_UNITS AS AVG_SALES_UNITS, t1.TOT_FC_UNITS_LIFT AS TOT_FC_UNITS_LIFT, t1.TOT_FC_UNITS_BY AS TOT_FC_UNITS_BY, t1.AVG_FC_UNITS_LIFT AS AVG_FC_UNITS_LIFT, t1.AVG_FC_UNITS_BY AS AVG_FC_UNITS_BY, t1.MAE_LIFT AS MAE_LIFT, t1.MAE_BY AS MAE_BY, t1.MAPE_LIFT AS MAPE_LIFT, t1.MAPE_BY AS MAPE_BY, t1.BIAS_LIFT AS BIAS_LIFT, t1.BIAS_BY AS BIAS_BY FROM FC.ACCURACY_ASR_NBR_NATL t1 WHERE EXISTS (SELECT 1 FROM L_TABLES.I0046_ASR t2 WHERE t2.PRD_GRP_NBR = prdGrpNbr AND t2.ASR_NBR = t1.ASR_NBR) AND t1.LAG IN (0,1,2,4,6,8,12,26) AND substr(to_char(t1.FSC_WK_END_DT), 1, 10) = 'LastFscWkEdDate' ORDER BY t1.FSC_YR_NBR, t1.FSC_WK_NBR

getAcc_Merch_Assortment_NatlTableQuery = SELECT t1.ITM_NBR AS ITM_NBR, t1.FSC_WK_END_DT AS FSC_WK_END_DT, t1.LAG AS LAG, t1.FSC_YR_NBR AS FSC_YR_NBR, t1.FSC_WK_NBR AS FSC_WK_NBR, t1.TOT_SALES_UNITS AS TOT_SALES_UNITS, t1.AVG_SALES_UNITS AS AVG_SALES_UNITS, t1.TOT_FC_UNITS_LIFT AS TOT_FC_UNITS_LIFT, t1.TOT_FC_UNITS_BY AS TOT_FC_UNITS_BY, t1.AVG_FC_UNITS_LIFT AS AVG_FC_UNITS_LIFT, t1.AVG_FC_UNITS_BY AS AVG_FC_UNITS_BY, t1.MAE_LIFT AS MAE_LIFT, t1.MAE_BY AS MAE_BY, t1.MAPE_LIFT AS MAPE_LIFT, t1.MAPE_BY AS MAPE_BY, t1.BIAS_LIFT AS BIAS_LIFT, t1.BIAS_BY AS BIAS_BY FROM FC.ACCURACY_ITM_NBR_NATL t1 WHERE EXISTS (SELECT 1 FROM L_TABLES.I0041_ITM_HRC t2 WHERE t2.ASR_NBR = asrNbr AND t2.ITM_NBR = t1.ITM_NBR) AND t1.LAG IN (0,1,2,4,6,8,12,26) AND substr(to_char(t1.FSC_WK_END_DT), 1, 10) = 'LastFscWkEdDate' ORDER BY t1.FSC_YR_NBR, t1.FSC_WK_NBR

getAcc_Merch_Item_NatlTableQuery = SELECT ITM_NBR, FSC_WK_END_DT, LAG, FSC_YR_NBR, FSC_WK_NBR, TOT_SALES_UNITS, AVG_SALES_UNITS, TOT_FC_UNITS_LIFT, TOT_FC_UNITS_BY, AVG_FC_UNITS_LIFT, AVG_FC_UNITS_BY, MAE_LIFT, MAE_BY, MAPE_LIFT, MAPE_BY, BIAS_LIFT, BIAS_BY FROM FC.ACCURACY_ITM_NBR_NATL WHERE LAG IN (0,1,2,4,6,8,12,26) AND ITM_NBR = itmNbr AND substr(to_char(FSC_WK_END_DT), 1, 10) = 'LastFscWkEdDate' ORDER BY FSC_YR_NBR, FSC_WK_NBR