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

Apex Web Services超级徽章:合规飞行区域查询API开发与测试

产品飞行区域查询Apex REST服务实现

核心需求回顾

ATS需通过API实现:根据产品代码和目标国家返回合规飞行区域信息,核心逻辑如下:

  • 通过产品代码获取Product对象的Family字段
  • 结合自定义元数据类型Product Geo Mapping(存储产品族与国家代码的飞行区域映射)返回结果
  • 处理三类响应场景:有效参数返回对应区域、无匹配规则返回提示、产品代码无效返回提示

Apex REST服务类实现

@RestResource(urlMapping='/ProductZoning/*')
global with sharing class ProductZoningService {
    @HttpGet
    global static String getPermissibleFlyZone() {
        // 处理请求头:获取CountryCode,为空则默认US并统一转为大写
        RestRequest req = RestContext.request;
        String countryCode = req.headers.get('CountryCode');
        countryCode = String.isBlank(countryCode) ? 'US' : countryCode.toUpperCase();

        // 校验ProductCode参数
        String productCode = req.params.get('ProductCode');
        if (String.isBlank(productCode)) {
            return 'ProductCode is missing or doesn\'t exist';
        }

        // 查询对应Product记录的Family字段
        Product2 product;
        try {
            product = [SELECT Family FROM Product2 WHERE ProductCode = :productCode LIMIT 1];
        } catch (QueryException e) {
            return 'ProductCode is missing or doesn\'t exist';
        }

        // 查询自定义元数据中的映射规则
        List<Product_Geo_Mapping__mdt> geoMappings = [
            SELECT Permissible_Fly_Zone__c
            FROM Product_Geo_Mapping__mdt
            WHERE Product_Family__c = :product.Family
            AND Country_Code__c = :countryCode
            LIMIT 1
        ];

        // 返回对应结果
        return geoMappings.isEmpty() 
            ? 'Confirm with the local authorities' 
            : geoMappings[0].Permissible_Fly_Zone__c;
    }
}

代码说明

  1. 请求头兼容:自动处理CountryCode缺失场景,统一转为大写避免大小写匹配问题
  2. 异常处理:通过try-catch捕获Product查询异常,确保无效产品代码的场景覆盖
  3. 元数据查询:精准匹配产品族与国家代码,返回对应飞行区域或规则缺失提示

测试类实现(覆盖率≥90%)

@isTest
private class ProductZoningServiceTest {
    @testSetup
    static void setupTestData() {
        // 预创建测试用Product记录
        insert new Product2(
            Name = 'Test Drone',
            ProductCode = 'DRN-001',
            Family = 'Consumer Drones'
        );
    }

    // 场景1:有效参数返回正确飞行区域
    static testMethod void testValidRequest() {
        RestContext.request = new RestRequest();
        RestContext.response = new RestResponse();

        RestContext.request.headers.put('CountryCode', 'US');
        RestContext.request.params.put('ProductCode', 'DRN-001');

        String result = ProductZoningService.getPermissibleFlyZone();
        System.assertNotEquals('ProductCode is missing or doesn\'t exist', result);
        System.assertNotEquals('Confirm with the local authorities', result);
    }

    // 场景2:产品族无对应国家规则
    static testMethod void testNoMappingRule() {
        RestContext.request = new RestRequest();
        RestContext.response = new RestResponse();

        RestContext.request.headers.put('CountryCode', 'JP');
        RestContext.request.params.put('ProductCode', 'DRN-001');

        String result = ProductZoningService.getPermissibleFlyZone();
        System.assertEquals('Confirm with the local authorities', result);
    }

    // 场景3:ProductCode缺失或不存在
    static testMethod void testInvalidProductCode() {
        // 测试参数缺失
        RestContext.request = new RestRequest();
        RestContext.response = new RestResponse();
        RestContext.request.params.put('ProductCode', '');
        System.assertEquals('ProductCode is missing or doesn\'t exist', ProductZoningService.getPermissibleFlyZone());

        // 测试无效产品代码
        RestContext.request.params.put('ProductCode', 'INVALID-001');
        System.assertEquals('ProductCode is missing or doesn\'t exist', ProductZoningService.getPermissibleFlyZone());
    }

    // 场景4:CountryCode缺失,使用默认值
    static testMethod void testDefaultCountryCode() {
        RestContext.request = new RestRequest();
        RestContext.response = new RestResponse();

        RestContext.request.params.put('ProductCode', 'DRN-001');
        String result = ProductZoningService.getPermissibleFlyZone();
        System.assertNotEquals('ProductCode is missing or doesn\'t exist', result);
    }
}

测试说明

  • @testSetup:预创建测试数据,提升测试效率
  • 覆盖所有核心场景:有效请求、无映射规则、无效产品代码、默认国家代码
  • 未新增自定义元数据记录,依赖测试环境现有配置,符合要求

验证方法(Postman)

  1. 请求方法设为GET
  2. 端点URL:https://<你的Salesforce实例域名>/services/apexrest/ProductZoning
  3. 请求头:可选添加CountryCode(不填则默认US)
  4. 请求参数:必填ProductCode
  5. 发送请求,验证返回结果是否匹配对应场景

内容的提问来源于stack exchange,提问作者Vishal Mankar

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.20 23:06:13