web-servicesintegrationapexchallenge-response

Apex Web Services Superbadge Unit - Find the Permissible Fly Zone


Find the Permissible Fly Zone ATS aims to enhance the user experience on its website by providing information about fly zones of its products based on the product code and the country where it’s intended to be operated. This requires the development and integration of a custom API that will fetch product family for the product code and process data to determine the product's fly zone for a country. The Product Geo Mapping custom metadata type configures the mapping of the permissible fly zones between the Product Family (API Name Family) field in the Product object and a country code. Countries covered for this requirement are US, AU, DE, and FR. You may assume that if a country value is specified, it will be one of these.

The system automatically detects the country and sets that value in the request headers. Design and implement an Apex REST service class (named ProductZoningService) configured to use the endpoint path ProductZoning with wildcard. The API should accept CountryCode as a custom header and the ProductCode as an input parameter to return a string value of the permissible fly zones or appropriate response mentioned below. If CountryCode is not found in the request headers, the implementation should default the country code to US.

Tip: To understand the mapping between the Family field and a country code, review the Product Geo Mapping custom metadata type and the Product object in Setup, and examine the sample records.

Test this API by implementing a comprehensive test class that covers various scenarios to ensure robustness and accuracy. Your solution should account for the three specific outcomes.

Permissible Fly Zones: Test with valid product codes and countries that should return specific permissible fly zones. The API should accurately return [Permissible_Fly_Zone__c] zones only where Permissible_Fly_Zone__c is the value associated with the input. Check with Local Authorities: Include scenarios where the outcome instructs the user to confirm with local authorities. This applies to situations where a country in the system does not have defined regulations for a particular product family. Ensure the API provides a response of Confirm with the local authorities under the correct conditions. Invalid or Missing Product Code: Test with scenarios where the ProductCode is either missing or does not exist in the system. The API should handle these cases and return ProductCode is missing or doesn't exist in response. Tip: Test the API using Postman or a similar tool to verify the above outcomes. Do not create additional Product Geo Mapping custom metadata type records for testing purposes.

Aim for a test class that achieves at least 90% code coverage, ensuring that the scenarios are tested thoroughly. This includes positive tests with expected results, as well as negative tests handling errors or exceptional cases.


Solution

  • SOLUTION ==>

    Apex Class - ProductZoningService

    @RestResource(urlMapping='/ProductZoning/*')
    global class ProductZoningService {
        @HttpGet
        global static String getFlyZone() {
                    RestRequest req = RestContext.request;
            system.debug('req=> '+ req);
            Map<String, String> queryParams = req.params;
            String productCode = queryParams.get('ProductCode');
            
            system.debug('Request URI: ' + req.requestURI);        
            String countryCode = req.headers.get('CountryCode');
            
            if (countryCode == null) {
                countryCode = 'US'; // Use 'US' as the default country code
            }
            
            system.debug('ProductCode: '+ productCode);
            system.debug('CountryCode: '+ countryCode);
            
            if (String.isBlank(productCode)) {
                return 'ProductCode is missing or doesn\'t exist';
            }
            
            List<Product2> products = [SELECT Id, Family FROM Product2 WHERE ProductCode = :productCode LIMIT 1];
            if (products.isEmpty()) {
                return 'ProductCode is missing or doesn\'t exist';
            }
            
            String productFamily = products[0].Family;
            
            system.debug('ProductFamily: '+ productFamily);
            
            List<Product_Geo_Mapping__mdt> mappings = [
                SELECT Permissible_Fly_Zone__c 
                FROM Product_Geo_Mapping__mdt 
                WHERE Family__c = :productFamily 
                AND Country_Code__c = :countryCode 
                LIMIT 1
            ];
            
            if (!mappings.isEmpty()) {
                system.debug('mappings[0].Permissible_Fly_Zone__c => '+ mappings[0].Permissible_Fly_Zone__c);
                return mappings[0].Permissible_Fly_Zone__c;
            } else {
                return 'Confirm with the local authorities';
            }
        }
    }
    

    Test Class - ProductZoningServiceTest

    @isTest
    private class ProductZoningServiceTest {
        @isTest
        static void testPermissibleFlyZone() {
            Product2 product = new Product2(ProductCode = 'AMER_Fixed_Wing', Family = 'Fixed-Wing', Name = 'Test Product');
            insert product;
            
            Test.startTest();
            RestRequest req = new RestRequest();
            req.requestURI = '/services/apexrest/ProductZoning/';
            req.params.put('ProductCode', product.ProductCode);
            req.headers.put('CountryCode', 'US');
            RestContext.request = req;
            String result = ProductZoningService.getFlyZone();
            Test.stopTest();
            
            System.assertEquals('Regulated', result);
        }
        
        @isTest
        static void testConfirmWithAuthorities() {
            Product2 product = new Product2(ProductCode = 'AMER_Fixed_Wing', Family = 'UnknownFamily', Name = 'Test Product 2');
            insert product;
            
            Test.startTest();
            RestRequest req = new RestRequest();
            req.requestURI = '/services/apexrest/ProductZoning/';
            req.params.put('ProductCode', product.ProductCode);
            req.headers.put('CountryCode', 'US');
            RestContext.request = req;
            String result = ProductZoningService.getFlyZone();
            Test.stopTest();
            
            System.assertEquals('Confirm with the local authorities', result);
        }
        
        @isTest
        static void testInvalidProductCode() {
            Test.startTest();
            RestRequest req = new RestRequest();
            req.requestURI = '/services/apexrest/ProductZoning/';
            req.params.put('ProductCode', '');
            req.headers.put('CountryCode', 'US');
            RestContext.request = req;
            String result = ProductZoningService.getFlyZone();
            Test.stopTest();
            
            System.assertEquals('ProductCode is missing or doesn\'t exist', result);
        }
        
        @isTest
        static void testDefaultCountryCode() {
            Product2 product = new Product2(ProductCode = 'AMER_Fixed_Wing', Family = 'Fixed-Wing', Name = 'Test Product 3');
            insert product;
            
            Test.startTest();
            RestRequest req = new RestRequest();
            req.requestURI = '/services/apexrest/ProductZoning/';
            req.params.put('ProductCode', product.ProductCode);
            RestContext.request = req;`your text`
            String result = ProductZoningService.getFlyZone();
            Test.stopTest();
            
            System.assertEquals('Regulated', result);
        }
    }