Calling an application API from Apex

The code used in this section uses Apex test methods (not included in the FormulaForce package) as an easy way to run the API code example in this chapter. We will be using an Apex test to illustrate the use of the Apex APIs included in the sample code in this chapter. The two test methods we will look at call the Compliance API after calling the season API to create some test data (which, of course, only exists on the database within the scope of the test execution).

In the following examples, the Driver records created by the SeasonService.createTestSeason API do not indicate that the drivers hold an FIA Super License. Thus, the expectation is that the ComplianceService.verify method will throw an exception reporting this problem. The following example test code asserts this behavior purely for illustration purposes in this chapter:

@IsTest 
private class ApplicationAPITest { 
    @IsTest 
    private static void demoComplianceVerifyAPI() {

// Create some test data via the SeasonService fforce.SeasonService.createTestSeason(); // Query Drivers to test compliance against Map<Id, fforce__Driver__c> driversById = new Map<Id, fforce__Driver__c>(
[select Id from fforce__Driver__c]); try { // Invoke the compliance engine for the drivers fforce.ComplianceService.verify(
driversById.keySet()); System.assert(false, 'Expected failures'); } catch (fforce.ComplianceService.ComplianceException e) { // Check the results System.assertEquals(
'4.1', e.failures[0].complianceCode);
System.assertEquals(
'Driver must have a FIA Super License.',
e.failures[0].failureReason); } } }
The namespace used in this sample is fforce. Your namespace will differ. Replace the namespace to get the code sample to compile (you can also find this class in the sample code for this chapter).

Run the test to confirm whether the API is working as expected.

As you have used the same Service layer method that's used by the rest of the application, your existing unit and integration tests around this class should suffice in providing you confidence in the robustness of this as an API to your application. If not, improving your testing around your Service layer benefits both external and internal callers equally.

Notice that when you click on the Show Dependencies button in the ApplicationAPITest class, it shows the API and Custom Objects being referenced from the package:

Now that you understand how the platform exposes your APIs to your customers' developers, the next section goes into more detail about the implications and restrictions in modifying it.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.15.237.164