0

I have webservice class which have multi callout.For the first callout i have some list of URl's.From the list of URL's I want to do the second callout and so on. apexCode:

global with sharing class Sample_C2GUpdateVersionPrice {
    static Double dbPriceHT;
    static Double dbTVNAmount;
    webservice static String VersionPrice(String oppCarid) {
        Sample_DZC2GVersionPriceDetails_CLS versionPrice=new Sample_DZC2GVersionPriceDetails_CLS(oppCarid);
        List<double> lstPriceValues =versionPrice.getVersionPrice();
        if (lstPriceValues!=null && lstPriceValues.size()>0) {
            dbPriceHT     = lstPriceValues.get(0);
            dbTVNAmount   = lstPriceValues.get(1);  
        }
        Opportunity_car_set__c oppCarset = [SELECT Price_HT__c FROM Opportunity_car_set__c where id=:oppCarid];
        oppCarset .Price_HT__c=dbPriceHT;
        oppCarset .Sample_TVN_Amount__c=dbTVNAmount;
        update oppCarset ;
        return ('/' + oppCarset.id);
    } 

}
Weservice class:

public class Sample_DZC2GVersionPriceDetails_CLS {
    public String strJSON {get;set;}
    public String strGeturl {get;set;}
    public String strGetcountrycode {get;set;}
    public String strGetCurrency {get;set;}
    public String strModelCode {get;set;}
    public String strVersionCode {get;set;}
    public String strVersionDocUrl {get;set;}
    public String strPriceListURL {get;set;}
    public String strOppid {get;set;}
    public double strversionPrice{get;set;}
    public double strversionPriceTVN{get;set;}
    public List<String> lstModelcalOut = new List<String>(); //to get list of model code
    public List<String> lstDoCcalOut = new List<String>(); //to get list of docurls
    public Map<String,String> mVersion     = new map<String,String>();  //Contains model code+doc url
    public List<double> lstVersionPrices = new List<double>();
    Sample_JSON2ApexC2G_CLS obj = null;
    public string strModel{get;set;}
    public string strVersion{get;set;} 
    public string strid{get;set;} 
    public Sample_JSON2C2GMainURL_CLS objUrl = null;
    public Sample_JSONC2GDocURL_CLS docObj=null;

    public Sample_DZC2GVersionPriceDetails_CLS(string strOppid,String strModelName, String strVersionName) {

        Sample_CountryInfo__c cs = Sample_CountryInfo__c.getInstance('Algeria');
        strGeturl         =    cs.Sample_C2GURL__c;
        strGetcountrycode =    cs.Sample_CountryCode__c;
        strGetCurrency    =    cs.Sample_Currency__c;
        strModel=strModelName;
        strVersion=strVersionName;
        strid=strOppid;
        Opportunity_car_set__c opp=[SELECT Model__r.ProductCode,Version__r.Sample_C2G_version_code__c FROM Opportunity_car_set__c where Model__r.name =: strModel and Version__r.name =: strVersion and id=:strid];    
        strModelCode    = opp.Model__r.ProductCode;
        strVersionCode  = opp.Version__r.Sample_C2G_version_code__c;
        strOppid        = opp.id;
        system.debug('<<strOppid'+ strOppid);

    }
    public Sample_DZC2GVersionPriceDetails_CLS(string strOppid) {

        Sample_CountryInfo__c cs = Sample_CountryInfo__c.getInstance('Algeria');
        strGeturl         =    cs.Sample_C2GURL__c;
        strGetcountrycode =    cs.Sample_CountryCode__c;
        strGetCurrency    =    cs.Sample_Currency__c;
        Opportunity_car_set__c opp=[SELECT Model__r.ProductCode,Version__r.Sample_C2G_version_code__c FROM Opportunity_car_set__c where id=:strOppid];    
        strModelCode    = opp.Model__r.ProductCode;
        strVersionCode  = opp.Version__r.Sample_C2G_version_code__c;
        strOppid        = opp.id;
        system.debug('strOppid<<<<<<<'+ strOppid);

    }

    //Common method to Parse the JSON
    public String init(String strVersionURL) {

        try {
            Http http = new Http();
            HttpRequest httpReq = new HttpRequest();
            httpReq.setEndpoint(strVersionURL);
            httpReq.setHeader('Accept','application/JSON');
            httpReq.setMethod('GET');
            HttpResponse response = http.send(httpReq);
            strJSON= response.getBody();

        } catch (Exception ex) {
            system.debug('<<Method: init Exception ::'+ ex);
        } 
        return  strJSON;     
    }

    //Used for Getting ModelCode and Doc URL in Map
    public Map<String, String> getJSONFromREST() {

        strJSON = init(strGeturl); 
        objUrl = Sample_JSON2C2GMainURL_CLS.parse(strJSON);
        List<Sample_JSON2C2GMainURL_CLS.Docs> lstDocs;
        lstDocs=objUrl .Docs; 
        for(Sample_JSON2C2GMainURL_CLS.Docs strDocs:lstDocs){
            string strModelIdCode=strDocs.docId.modelSpecCode;
            lstModelcalOut.add(strModelIdCode);
            string strDocUrl=strDocs.doc;
            lstDoCcalOut .add(strDocUrl);
        }
        for(integer i=0;i<lstModelcalOut.size();i++) {
            mVersion.put(lstModelcalOut.get(i),lstDoCcalOut.get(i));
        }
        system.debug('mVersion===>'+mVersion);
        return mVersion;       
    }

    //Used to get Version Price URL
    public String getVersionPriceURL() {

        Map<string,String> mVersionVal=getJSONFromREST();
        if(mVersionVal.containsKey(strModelCode)) {
            strVersionDocUrl= mVersionVal.get(strModelCode);
        }
        strJSON = init(strVersionDocUrl); 
        docObj= Sample_JSONC2GDocURL_CLS.parse(strJSON);
        system.debug('docObj<<<<<<<'+ docObj);

        strPriceListURL=docObj.PricesData.pricesList;
        return strPriceListURL;         
    }


    public List<double> getVersionPrice() {

           String strVersionPriceURL=getVersionPriceURL();
           String strJSON = init(strVersionPriceURL);
           obj = Sample_JSON2ApexC2G_CLS.parse(strJSON);
           /*some code------------

           */
            lstVersionPrices.add(strversionPriceTVN);
            return lstVersionPrices;

    } 

}

    Test class:

@isTest
public with sharing class Sample_C2GUpdateVersionPrice_Test {
    static TestMethod void versionPriceTest() {
    Sample_CountryInfo__c cs = new Sample_CountryInfo__c(Name = 'Algeria' , Sample_C2GURL__c = 'http://dz.co.rplug.renault.com/range',Sample_CountryCode__c = 'DZ' , Sample_Currency__c = 'DZD');
    insert cs;

        Opportunity opp = new Opportunity();
        opp.name = 'test1';
        opp.stagename = 'Draft';
        opp.closedate = System.Today();
        insert opp;
        Product2 pModel=new Product2();
        pModel.Brand__c='Renault';
        pModel.Name='DZ-Captur';
        pModel.Country_code__c='DZ';
        pModel.Parent_Product__c=null;
        pModel.IsActive = true;
        pModel.Market__c='Test';
        insert pModel;
        Product2 pVersion=new Product2();
        pVersion.Name='DZ-Captur';
        pVersion.Parent_Product__c=pModel.id;
        pVersion.IsActive = true;
        insert pVersion;
        Opportunity_car_set__c oppCarSet = new Opportunity_car_set__c();
        oppCarSet.name = 'uma';
        oppCarSet.Brand__c=pModel.Brand__c;
        oppCarSet.Model__c=pModel.id;
        oppCarSet.Version__c=pVersion.id;
        oppCarSet.opportunity__c = opp.id;
        oppCarSet.Quantity__c = 2;
        oppCarSet.Price_HT__c =420.0;
        oppCarSet.Sample_TVN_Amount__c=89.7;
        insert oppCarSet;

        Test.Starttest();
        Sample_DZC2GVersionPriceDetails_CLS versionPrice1=new Sample_DZC2GVersionPriceDetails_CLS(oppCarSet.id);

        Test.setMock(HttpCalloutMock.class, new Sample_C2GMain_MockHttpResponseGenerator ());    
        Map<String, String> mapVal=new  Map<String, String>();
        mapVal=versionPrice1.getJSONFromREST();
        system.debug('lstcalOut>>>>>>>>>>>>>>>>'+mapVal);
        string strURL=mapVal.get('SFN');
        versionPrice1.init(strURL);
        String strDoc=versionPrice1.getVersionPriceURL();


        Test.setMock(HttpCalloutMock.class, new Sample_C2GDoc_MockHttpResponseGenerator ());



        List<double> tstList = versionPrice1.getVersionPrice ();
        Sample_C2GUpdateVersionPrice.VersionPrice(cs.Sample_C2GURL__c);
        Test.StopTest();


    }
}

    Mock class:



 @isTest
    global class Rfleet_C2GDoc_MockHttpResponseGenerator implements HttpCalloutMock {
        // Implement this interface method
        global HTTPResponse respond(HTTPRequest req) {
           System.assertEquals('GET', req.getMethod());
            HttpResponse res = new HttpResponse();
            res.setHeader('Content-Type', 'application/json');
            res.setBody('{\"requestedURI\":\"http://dz.co.rplug.re.com/doc/baa\",\"URI\":\"http://dz.co.rplug.ras.com/doc/baa\",\"canonicalURI\":\"http://dz.co.rplug.ras.com/doc/baa#this\",\"configuration\":\"http://dz.co.rplug.ras.com/c/baa/A\",\"docId\":{\"program\":\"X43\",\"phase\":\"2\",\"modelSpecCode\":\"SFN\",\"modelKind\":\"TR\",\"market\":\"ALGERI\",\"tariffNumber\":\"9347\",\"tariffAdditiveNumber\":\"00\",\"commercialKind\":\"VP\",\"creationTimeStamp\":\"1444119901628\",\"brand\":\"APL03\",\"clientId\":\"TARIF\",\"clientType\":\"GP\",\"localSemiclair\":\"SFN\"},\"validity\":{\"applicationDate\":\"2015-10-07\",\"expirationDate\":null},\"diversityData\":{\"localSemiClair\":\"http://dz.co.rplug.ras.com/localsemiclair/baa\",\"specificationCategories\":\"http://dz.co.rplug.ras.com/speccats/baa\",\"versionsCategories\":\"http://dz.co.rplug.ras.com/category/baa\"},\"presentationData\":{\"marketingModelPresentation\":\"http://dz.co.rplug.ras.com/pres/baa\"},\"pricesData\":{\"priceType\":[{\"reference\":\"PVCHT\",\"currency\":\"DZD\"},{\"reference\":\"TAUX1\",\"currency\":\"%\"},{\"reference\":\"TVAHRK\",\"currency\":\"DZD\"},{\"reference\":\"PVCDEM\",\"currency\":\"DZD\"},{\"reference\":\"PVCTTC\",\"currency\":\"DZD\"}],\"defaultPriceType\":{\"reference\":\"PVCTTC\",\"currency\":\"DZD\"},\"defaultPriceTypeVersion\":{\"reference\":\"PVCTTC\",\"currency\":\"DZD\"},\"defaultPriceTypeOption\":{\"reference\":\"PVCTTC\",\"currency\":\"DZD\"},\"pricesList\":\"http://dz.co.rplug.ras.com/pricelist/baa\"}}');
            res.setStatusCode(200);
            return res;
        }
    }

For the getJSONfromrest i pass the url which value is coming from custom setting.this is the first callout.It will give list of URL's.From that i will take one URL and perform second callout.For each callout we have to write seperate mockclass?

Please help me out from this !!

Thanks in advance !!

  • You answered your own question. Yes, you write a separate mock. – crmprogdev Apr 1 '16 at 13:06
  • If i write a seperate mock class then it failed.For example in "getJSONfromrest " am performing a callout.So i have written mock class.That method is covered.then for next method "getVersionPriceURl" i have another callout.If i add another mock in test class then my covered lines in "getJSONfromrest " also uncovered.and got failed @crmprogdev – uma451 Apr 1 '16 at 13:10
  • You have to do the mock callout and get the mock response of the 1st one before you can test the 2nd one. – crmprogdev Apr 1 '16 at 13:15
  • I tried like this.but it gives error !! – uma451 Apr 1 '16 at 13:22
  • Above test clas i have put two mock like that only. – uma451 Apr 1 '16 at 13:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.