• D George
  • NEWBIE
  • 0 Points
  • Member since 2015

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
    Questions
  • 1
    Replies
Hi,

Need to help for code coverage i got 66% but json part not coveing in my controller please look into my code
================Apex Class====================
public class LocationCallouts {

     @future (callout=true)  // future method needed to run callouts from Triggers
      static public void getLocation(id accountId){
        // gather account info
        Account a = [SELECT BillingCity,BillingCountry,BillingPostalCode,BillingState,BillingStreet FROM Account WHERE id =: accountId];
 
        // create an address string
        String address = '';
        if (a.BillingStreet != null)
            address += a.BillingStreet +', ';
        if (a.BillingCity != null)
            address += a.BillingCity +', ';
        if (a.BillingState != null)
            address += a.BillingState +' ';
        if (a.BillingPostalCode != null)
            address += a.BillingPostalCode +', ';
        if (a.BillingCountry != null)
            address += a.BillingCountry;
 
        address = EncodingUtil.urlEncode(address, 'UTF-8');
        
        General_Settings__c ge= General_Settings__c.getValues('API Key');
        General_Settings__c eurl= General_Settings__c.getValues('Geocoding URL');        
        System.debug('##########'+eurl);
        System.debug('@@@@@@@@@@'+ge);
        String geocodingKey = ge.Value__c;
        String endpointurl= eurl.Value__c;
 
        // build callout
        Http h = new Http();
        HttpRequest req = new HttpRequest();
      //req.setEndpoint('http://maps.googleapis.com/maps/api/geocode/json?address='+address+'&sensor=false');
        req.setEndpoint(endpointurl+'?address='+ address + '&key=' + geocodingKey + '&sensor=false');
        req.setMethod('GET');
        req.setTimeout(60000);
 
        try{
            // callout
            HttpResponse res = h.send(req);
             System.debug('&&&&&'+res.getBody());
             System.debug('#####');
            // parse coordinates from response
            JSONParser parser = JSON.createParser(res.getBody());
            double lat = null;
            double lon = null;
            while (parser.nextToken() != null) {
                if ((parser.getCurrentToken() == JSONToken.FIELD_NAME) &&
                    (parser.getText() == 'location')){
                       parser.nextToken(); // object start
                       while (parser.nextToken() != JSONToken.END_OBJECT){
                           String txt = parser.getText();
                           parser.nextToken();
                           if (txt == 'lat')
                               lat = parser.getDoubleValue();
                           else if (txt == 'lng')
                               lon = parser.getDoubleValue();
                       }
 
                }
            }
 
            // update coordinates if we get back
            if (lat != null){
                a.GeoLocations__Latitude__s = lat;
                a.Location_Latitude__c = lat;
                a.GeoLocations__Longitude__s = lon;
                a.Location_Longitude__c = lon;
                Lat= lat;
                Lon= lon;
                update a;
            }
 
        } catch (Exception e) {
        }
    }
}
===================ApexTest Class===================
@IsTest(SeeAllData=False)
public class Test_LocationCallouts {
    static testMethod void validateLocationCallouts() {
   
    General_Settings__c setting1 = new  General_Settings__c();
    setting1.Name = 'Geocoding URL';
    setting1.Value__c = 'https://maps.googleapis.com/maps/api/geocode/json';
    insert setting1;
    General_Settings__c setting2 = new  General_Settings__c();
    setting2.Name = 'API Key';
    setting2.Value__c = 'AIzaSyBdTDHIFZgg1lG_p9fCg5QYcWWKrWe6K_E';
    insert setting2;
   
    Account a = new Account(Name='Testsup', BillingCountry='USA',BillingState='New Jersey',BillingCity='Cliff Wood',
                            BillingPostalCode='07010',BillingStreet='cliff wood');
    insert a;

        LocationCallouts.getLocation(a.id);
        Test.startTest();
        Test.setMock(HttpCalloutMock.class, new MockHttpResponseGenerator());
        Test.stopTest();
        HttpResponse res = CalloutClass.getInfoFromExternalService(); 
    }
}
===============Coverage Status===============
User-added imageUser-added imageUser-added image

Any one help?