You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何从YAML测试数据提取限额信息并映射到Money对象?

问题描述

我有一份YAML格式的测试数据,其中restriction节点下的fundinglimit包含CURRENCY和VALUE信息。希望将这两个值以字符串类型分别存入Money对象的amount和currency属性。现有测试代码片段如下,同时提供了参考的Postman请求JSON结构,请问如何正确提取并赋值?

YAML测试数据

SEARCH_CREDENTIALS_TEST:
  testName: FB_0001
  nttProductName: TESTNTT
  tpanProductName: TESTTPAN
  userProfile: USERPROFILE
  credentialType: E_COMMERCE_SHORT_FORM_DYNAMIC
  tsp: AETS
  scProfile: Internal
  otherCredentialTypes:
    - E_COMMERCE_LONG_FORM
  maxCachedCredentials: 2
  metadata:
    - HASHED=CUSTOMER_EMAIL
    - UNHASHED=MERCHANT_ID|AMOUNT
  secondRequestMedata:
    - HASHED=CUSTOMER_EMAIL|CUSTOMER_PHONE
    - UNHASHED=MERCHANT_ID|AMOUNT
  restriction:
    fundinglimit:
      - CURRENCY=USD
      - VALUE=10.00

(注:原YAML中-CURRENCY=USD缺少空格,修正为标准格式- CURRENCY=USD才能被正常解析)

现有代码片段

@Test(dataProvider = "NetworkTokens", groups = { "p0", "p1", "p2", "p3" })
public void searchCredentialTest(LinkedHashMap testData) throws Exception {
    String credentialType = (String) testData.get("credentialType");

    User user = userUtil.createUser((String) testData.get("userProfile"));
    SecurityContext securityContext = securityContextUtil.createSecurityContext("Default", user);
    Token ntt = createOnlineToken.createNtt(user, securityContext, (String) testData.get("nttProductName"),(String) testData.get("testname"));

    Money m = new Money();
    m.setAmount((String)testData.get("restriction"));
    m.setCurrency((String) testData.get("restriction"));
}

参考Postman请求JSON

{
    "product_name":"TEST_PRODUCT",
    "value_to_tokenize":"TEST_VALUE",
    "wallet_id": "TEST_WALLET",
    "restriction" : {
        "funding_limit" : {"currency_code" : "USD", "value" : "10.00"}
    }
}
解决方案

要正确提取fundinglimit中的值,需要逐层解析testData的嵌套结构,具体步骤和修改后的代码如下:

@Test(dataProvider = "NetworkTokens", groups = { "p0", "p1", "p2", "p3" })
public void searchCredentialTest(LinkedHashMap testData) throws Exception {
    String credentialType = (String) testData.get("credentialType");

    User user = userUtil.createUser((String) testData.get("userProfile"));
    SecurityContext securityContext = securityContextUtil.createSecurityContext("Default", user);
    Token ntt = createOnlineToken.createNtt(user, securityContext, (String) testData.get("nttProductName"),(String) testData.get("testName"));

    Money m = new Money();
    // 解析restriction节点
    LinkedHashMap<String, Object> restriction = (LinkedHashMap<String, Object>) testData.get("restriction");
    if (restriction != null) {
        // 解析fundinglimit列表
        List<String> fundingLimitList = (List<String>) restriction.get("fundinglimit");
        if (fundingLimitList != null && !fundingLimitList.isEmpty()) {
            for (String entry : fundingLimitList) {
                // 按等号拆分键值对,限制拆分次数避免值中含等号的情况
                String[] parts = entry.split("=", 2);
                if (parts.length == 2) {
                    String key = parts[0].trim();
                    String value = parts[1].trim();
                    // 匹配键名赋值到对应属性
                    if ("CURRENCY".equals(key)) {
                        m.setCurrency(value);
                    } else if ("VALUE".equals(key)) {
                        m.setAmount(value);
                    }
                }
            }
        }
    }
}

关键说明

  • 修复了原YAML的格式错误,确保列表元素能被正确解析
  • 增加空值判断,避免运行时出现NullPointerException
  • 使用split("=", 2)拆分键值对,增强代码鲁棒性
  • 对应Postman结构,将VALUE值映射到Money的amount属性,CURRENCY值映射到currency属性

内容的提问来源于stack exchange,提问作者William Johnson

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.30 02:37:44