Pages

Showing posts with label Apex Integration Services. Show all posts
Showing posts with label Apex Integration Services. Show all posts

Thursday, January 12, 2017

Apex Integration Services "Apex Web Services"

Create an Apex REST service that returns an account and it's contacts.

To pass this challenge, create an Apex REST class that is accessible at '/Accounts/<Account_ID>/contacts'. The service will return the account's ID and Name plus the ID and Name of all contacts associated with the account. Write unit tests that achieve 100% code coverage for the class and run your Apex tests.
  • The Apex class must be called 'AccountManager'.
  • The Apex class must have a method called 'getAccount' that is annotated with @HttpGet
  • The method must return the ID and Name for the requested record and all associated contacts with their ID and Name.
  • The unit tests must be in a separate Apex class called 'AccountManagerTest'.
  • The unit tests must cover all lines of code included in the AccountManager 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.
AccountManager Class:


@RestResource(urlMapping='/Accounts/*/contacts')
global class AccountManager {
    @HttpGet
    global static Account getAccount(){
        RestRequest request = RestContext.request;
        String accountId = request.requestURI.substringBetween('Accounts/','/contacts');
        Account Result = [SELECT Id, Name, (SELECT Id, Name From Contacts) From Account Where Id =: accountId];
        system.debug('Result is :' +Result);
        Return Result;
    }
}

AccountManagerTest Class:

@isTest
private class AccountManagerTest {
    private static testMethod void testGetAccount(){
        Id recordId = createTestRecord();
        RestRequest request = new RestRequest();
        request.requestUri = 'https://na35.salesforce.com/services/apexrest/Accounts/'+recordId+'/Contacts';
        request.httpMethod = 'GET';
        RestContext.request = request;
        Account thisAccount = AccountManager.getAccount();
        System.assert(thisAccount != null);
        System.assertEquals('Test record', thisAccount.Name);
    }
    static Id createTestRecord(){
         Account TestAcc = new Account(
          Name='Test record');
        insert TestAcc;
        Contact TestCon= new Contact(
        LastName='Test', 
        AccountId = TestAcc.id);
        return TestAcc.Id;
    }
}

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); } }

Tuesday, January 3, 2017

Apex Integration Services "Apex REST Callouts"

Create an Apex class that calls a REST endpoint and write a test class.

To pass this challenge, create an Apex class that calls a REST endpoint to return the name of an animal, write unit tests that achieve 100% code coverage for the class using a mock response, and run your Apex tests.
  • The Apex class must be called 'AnimalLocator', have a 'getAnimalNameById' method that accepts an Integer and returns a String.
  • The 'getAnimalNameById' method must call https://th-apex-http-callout.herokuapp.com/animals/:id, using the ID passed into the method. The method returns the value of the 'name' property (i.e., the animal name).
  • Create a test class named AnimalLocatorTest that uses a mock class called AnimalLocatorMock to mock the callout response.
  • The unit tests must cover all lines of code included in the AnimalLocator 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.
AnimalLocator Class with getAnimalNameById method:

public class AnimalLocator
{
  public static String getAnimalNameById(Integer id){
        Http http = new Http();
        HttpRequest request = new HttpRequest();
        request.setEndpoint('https://th-apex-http-callout.herokuapp.com/animals/'+id);
        request.setMethod('GET');
        HttpResponse response = http.send(request);
          String strResp = '';
           system.debug('******response '+response.getStatusCode());
           system.debug('******response '+response.getBody());
        // If the request is successful, parse the JSON response.
        if (response.getStatusCode() == 200) 
        {
            // Deserializes the JSON string into collections of primitive data types.
           Map<String, Object> results = (Map<String, Object>) JSON.deserializeUntyped(response.getBody());
            // Cast the values in the 'animals' key as a list
           Map<string,object> animals = (map<string,object>) results.get('animal');
            System.debug('Received the following animals:' + animals );
            strResp = string.valueof(animals.get('name'));
            System.debug('strResp >>>>>>' + strResp );
        }
        return strResp ;
   }
  
}

Mock Callout Response : AnimalLocatorMock

@isTest
global class AnimalLocatorMock implements HttpCalloutMock {
    global HTTPResponse respond(HTTPRequest request) {
        HttpResponse response = new HttpResponse ();
        response.setHeader('Content-Type', 'application/json');
        response.setBody('{"animal":{"id":1,"name":"chicken","eats":"chiken food","says":"cluck cluck"}}');
        response.setStatusCode(200);
        return response;
    }

}

AnimalLocatorTest Class:
@isTest
private class AnimalLocatorTest {
    @isTest static void AnimalLocatorMock1(){
        Test.setMock(HttpCalloutMock.class, new AnimalLocatorMock());
        string result = AnimalLocator.getAnimalNameById(1);
        string expectedResult = 'chicken';
        System.assertEquals(result, expectedResult);
    }

}