Pages

Thursday, January 5, 2017

Apex Integration Services "Apex SOAP Callouts"

Generate an Apex class using WSDL2Apex and write a test class.

Generate an Apex class using WSDL2Apex for a SOAP web service, write unit tests that achieve 100% code coverage for the class using a mock response, and run your Apex tests.
  • Use WSDL2Apex to generate a class called 'ParkService' in public scope using this WSDL file. After you click the 'Parse WSDL' button don't forget to change the name of the Apex Class Name from 'parksServices' to 'ParkService'.
  • Create a class called 'ParkLocator' that has a 'country' method that uses the 'ParkService' class and returns an array of available park names for a particular country passed to the web service. Possible country names that can be passed to the web service include Germany, India, Japan and United States.
  • Create a test class named ParkLocatorTest that uses a mock class called ParkServiceMock to mock the callout response.
  • The unit tests must cover all lines of code included in the ParkLocator class, resulting in 100% code coverage.
  • Run your test class at least once (via 'Run All' tests the Developer Console) before attempting to verify this challenge.
ParkService Class generated from WSDL:

//Generated by wsdl2apex

public class ParkService {
    public class byCountryResponse {
        public String[] return_x;
        private String[] return_x_type_info = new String[]{'return','http://parks.services/',null,'0','-1','false'};
        private String[] apex_schema_type_info = new String[]{'http://parks.services/','false','false'};
        private String[] field_order_type_info = new String[]{'return_x'};
    }
    public class byCountry {
        public String arg0;
        private String[] arg0_type_info = new String[]{'arg0','http://parks.services/',null,'0','1','false'};
        private String[] apex_schema_type_info = new String[]{'http://parks.services/','false','false'};
        private String[] field_order_type_info = new String[]{'arg0'};
    }
    public class ParksImplPort {
        public String endpoint_x = 'https://th-apex-soap-service.herokuapp.com/service/parks';
        public Map<String,String> inputHttpHeaders_x;
        public Map<String,String> outputHttpHeaders_x;
        public String clientCertName_x;
        public String clientCert_x;
        public String clientCertPasswd_x;
        public Integer timeout_x;
        private String[] ns_map_type_info = new String[]{'http://parks.services/', 'ParkService'};
        public String[] byCountry(String arg0) {
            ParkService.byCountry request_x = new ParkService.byCountry();
            request_x.arg0 = arg0;
            ParkService.byCountryResponse response_x;
            Map<String, ParkService.byCountryResponse> response_map_x = new Map<String, ParkService.byCountryResponse>();
            response_map_x.put('response_x', response_x);
            WebServiceCallout.invoke(
              this,
              request_x,
              response_map_x,
              new String[]{endpoint_x,
              '',
              'http://parks.services/',
              'byCountry',
              'http://parks.services/',
              'byCountryResponse',
              'ParkService.byCountryResponse'}
            );
            response_x = response_map_x.get('response_x');
            return response_x.return_x;
        }
    }
}

ParkLocator Class:

public class ParkLocator { public static String[] country(String country){ ParkService.ParksImplPort Locator = new ParkService.ParksImplPort(); return Locator.byCountry(country); } }

ParkServiceMock class:

@isTest global class ParkServiceMock implements WebServiceMock{ global void doInvoke( Object stub, Object request, Map<String,Object> response, String endpoint, String soapAction, String requestName, String responseNS, String responseName, String responseType) { ParkService.byCountryResponse response_x = new ParkService.byCountryResponse(); response_x.return_x = new List<String>{'Garner State Park', 'Fowler Park', 'Hoosier National Forest Park'}; response.put('response_x',response_x); } }

ParkLocatorTest class:

@isTest private class ParkLocatorTest { testMethod static void testCallout(){ Test.setMock(WebServiceMock.class, new ParkServiceMock()); String country = 'United States'; String[] result = ParkLocator.country(country); System.assertEquals(new List<String>{'Garner State Park', 'Fowler Park', 'Hoosier National Forest Park'}, result); } }

8 comments:

  1. Hi, I have read this blog. There are many informative information for programmers. Salesforce Integration Services

    ReplyDelete
  2. Hi,
    thanks for this. i have written somthing similar to this, but its returmomg error. please take a look below:


    public class ParkLocator {
    public List Country(string countryname){
    ParkService.ParksImplPort parknames = new parkService.ParksImplPort();
    List Availablecountrynames = new List{'India', 'Germany', 'Japan', 'United States'};
    for (string i: availablecountrynames){
    if (i =:country);
    system.adderror(' select an apropriate country');
    else
    List result = parknames.bycountry(countryname);
    system.debug(result);
    }
    }return result;
    }

    ReplyDelete
  3. public class ParkLocator {
    public static String[] country(String country){
    ParkService.ParksImplPort Locator = new ParkService.ParksImplPort();
    return Locator.Country(country);
    }
    }


    This worked

    ReplyDelete
  4. and add Remote site settings
    with url- https://th-apex-soap-service.herokuapp.com


    ReplyDelete
  5. use this code line for "debug"-"open execute anonymous window"
    ( system.debug(ParkLocator.country('Japan')); )


    ParkService

    public class ParkLocator {
    public static list country(string country) {
    ParkService.ParksImplPort parkservice = new ParkService.ParksImplPort();
    return Parkservice.byCountry(country);
    }
    }
    --------------------------------------------------------
    ParkLocaterTest

    @isTest
    private class ParkLocatorTest {
    testMethod static void testCallout(){
    Test.setMock(WebServiceMock.class, new ParkServiceMock());
    String country = 'United States';
    String[] result = ParkLocator.country(country);
    System.assertEquals(new List{'Garner State Park', 'Fowler Park', 'Hoosier National Forest Park'}, result);
    }
    }

    ----------------------------------------
    ParkServiceMoak

    @isTest
    global class ParkServiceMock implements WebServiceMock{
    global void doInvoke(
    Object stub,
    Object request,
    Map response,
    String endpoint,
    String soapAction,
    String requestName,
    String responseNS,
    String responseName,
    String responseType) {
    ParkService.byCountryResponse response_x = new ParkService.byCountryResponse();
    response_x.return_x = new List{'Garner State Park', 'Fowler Park', 'Hoosier National Forest Park'};
    response.put('response_x',response_x);
    }

    }

    ReplyDelete
  6. Hi, I am using your code to solve trailhead challenge but unfortunatley there is an error occouced on "ParkServiceMock" class which is stated as "Invalid interface: WebServiceMock". How can I resolve this?

    ReplyDelete