@HttpPost
global static List<HHSurvey> getHHSurveyList() {
RestRequest req = RestContext.request;
RequestData mRequestData = (RequestData)JSON.deserialize(req.requestBody.toString(),RequestData.class);
List<HH_Survey__c> hhSList = new List<HH_Survey__c>();
Map<String,List<HH_Housing__c>>hhHList=new Map<String,List<HH_Housing__c>>();
List<HHSurvey> hhSurveyList = new List<HHSurvey>();
Set<String> hhSurveyIds=new Set<String>();
DateTime FromDate=null;
DateTime ToDate=null;
if(mRequestData!=null&& mRequestData.AgentMobileNo!=null&&mRequestData.AgentMobileNo!=''
&&mRequestData.FromDate!=null&&mRequestData.FromDate!='0'&&mRequestData.ToDate!=null&&mRequestData.ToDate!='0')
{
FromDate=datetime.newinstance(long.valueOf(mRequestData.FromDate));
ToDate=datetime.newinstance(long.valueOf(mRequestData.ToDate));
if(mRequestData.FromDate.equals(mRequestData.ToDate))
{
**hhSList** = [SELECT Id,Family_s_monthly_income__c,Do_you_have_your_own_house__c,Saral_Jeevan_channel_is_accessible__c,Need_Remedy_Help1__c,Irrigated_land_In_Acre__c,AgentMobileNo__c,Main_occupation_of_the_family__c,Household_Serial_Number__c,Consent_Status__c,Hamlet_Segment_Camp__c from HH_Survey__c where AgentMobileNo__c=:mRequestData.AgentMobileNo AND AgentMobileNo__c<>null And CreatedDateWithNoTime__c<> null
AND CreatedDateWithNoTime__c=: FromDate.Date() order by CreatedDateWithNoTime__c Desc ];
}
else
{
hhSList = [SELECT Id,Family_s_monthly_income__c,Do_you_have_your_own_house__c,Saral_Jeevan_channel_is_accessible__c,Dry_land_In_Acre__c,Mobile1__c,Loan_Amount__c,Respondent_Name__c,Landmark__c from HH_Survey__c where AgentMobileNo__c=:mRequestData.AgentMobileNo AND AgentMobileNo__c<>null And CreatedDateWithNoTime__c<> null
AND CreatedDateWithNoTime__c>=: FromDate.Date() AND CreatedDateWithNoTime__c<=:ToDate.Date() order by CreatedDateWithNoTime__c Desc ];
}
if(hhSList !=null&&hhSList.size()>0 )
{
for(HH_Survey__c hhSurvey :hhSList ){
hhSurveyIds.add(hhSurvey.Id);
}
hhHList=getHousingList(hhSurveyIds);
for(HH_Survey__c hhs : hhSList ){
HHSurvey hs = new HHSurvey();
hs.Id = hhs.Id;
hs.FamilyMonthlyIncome= hhs.Family_s_monthly_income__c;
hs.Doyouhaveyourownhouse= hhs.Do_you_have_your_own_house__c;
hs.SaralJeevanChannelAccess=
hhs.Saral_Jeevan_channel_is_accessible__c;
hs.Remarks= hhs.Remarks__c;
hs.TV= hhs.Do_you_have_TV_in_your_house__c ;
hs.SurveyNo= hhs.HH_Survey_Id__c;
List<HHHouse> setHH=null;
if(hhHList!=null &&hhHList.size()>0 &&hhHList.containsKey(hs.Id)){
setHH=setHousingList(hhHList.get(hs.Id));
hs.HouseList.addAll(setHH);
}
hhSurveyList.add(hs);
}
}
return hhSurveyList ;
}
global class HHHouse{
String Id;
String HHSurveyId;
Decimal ApproxSize;
Decimal HouseType;
Decimal HouseAge;
}
global class RequestData{
String AgentMobileNo;
String FromDate;
String ToDate;
}
global class HHSurvey{
String Id;
Decimal FamilyMonthlyIncome;
String Doyouhaveyourownhouse;
String SaralJeevanChannelAccess;
String Remarks;
String Village;
Decimal DryLandInAcre;
String MobileOne;
Decimal Houshold;
Decimal BalanceLoanAmountOutstanding;
Decimal TotalLandHoldingAcre;
Decimal SolutionDate;
Decimal DOBOfFamilyHead;
String NameofHouseholdHead;
Decimal SurveyDate;
String HSC;
String ConsentStatus;
List<HHHouse> HouseList=new List<HHHouse>();
}
Test class
static testMethod void SurveylistTest()
{
Test.startTest();
JSONGenerator gen = JSON.createGenerator(true);
gen.writeStartObject();
gen.writeStringField('AgentMobileNo', '7022834238');
gen.writeStringField('FromDate', '1527964200000');
gen.writeStringField('ToDate', '1527964200000');
gen.writeEndObject();
String json =gen.getAsString();
RestRequest req = new RestRequest();
RestResponse res = new RestResponse();
req.requestURI = 'https://cs5.salesforce.com/services/apexrest/getHHSurveyList/';
req.httpMethod = 'POST';
req.requestBody= blob.valueOf(json);
RestContext.request = req;
RestContext.response = res;
List<WS_GetHHSurveyList.HHSurvey> surveylist = WS_GetHHSurveyList.getHHSurveyList();
System.assert(surveylist.size() ==0);
Test.StopTest();
}
asking a question documentation
. Instead of requesting a test class for custom code, give it a try and comeback with any specific question; with that you'd learn on how to solve the same problem again. This answer has some trailhead links which are quite good and help you with the start. – Raul Feb 11 at 8:55getHHSurveyList
method from your test, and pass in the JSON string by making a call toJSON.serialize
and setting the result in therequestBody
. Google for examples. But if you haven't already, first work through these Apex Testing Trailhead units. – Keith C Feb 11 at 9:18