+ Start a Discussion
deepakMdeepakM 

Need to cover the few code lines.

hi everyone

 

i need to cover the below lines but unable to do that.

 

The code is as follows:

Webservice static string Submit(string oppId)
{

 

string message = '';
boolean flag = true;

 

Flight_Request__c[] tf = [SELECT id,Customer_Approved_Itinerary__c,Opportunity__c FROM Flight_Request__c WHERE Opportunity__c = :oppId and Customer_Approved_Itinerary__c = true ];
if (tf.size() != 1)
{
message += 'Flight Request must contain only one record with Customer Approved Itinerary true \n';
flag = false;
}

 

}

 

 

My Test class method

 

static testMethod void myUnitTest() {

string message = '';
boolean flag = true;

 

Flight_Request__c fr = new Flight_Request__c();
fr.Budget__c = '23';
fr.Publish_Fare__c = '35';
fr.Flight_Itinerary_Name__c ='Test';
fr.Opportunity__c = opp.Id;
fr.From__c = opp.Origin_City__c;
fr.To__c = opp.Destination_City__c;
fr.Outbound_del__c = opp.Departure_Date__c;
fr.Pax_del__c = opp.Number_of_Passengers__c;
fr.Region_del__c = opp.Destination_Zone__c;
fr.Inbound_del__c = opp.Return_Date__c;
fr.Outbound_Flexibility__c = opp.Flexibility_From__c;
fr.Inbound_Flexibility__c = opp.Flexibility_To__c;
fr.Email_To__c ='anil@swiftsetup.com';
fr.Email_Content__c= 'Test';
fr.Subject__c ='subject test';
fr.Customer_Approved_Itinerary__c = true;
fr.Flight_Info__c = 'Test';
fr.Searcher_New__c ='Anil D';
fr.Email_Cc__c ='dm@we.com';
fr.Email_Bcc__c ='anil@we.com';
insert fr;

 

 

}

 

but it not coverd the above red lines.

 

please guide me.

 

Thanks

PrakashbPrakashb

Can you provide your complete test class??

 

Are you inseting a new Opportunity and providing those values in your test class??

deepakMdeepakM

My Test class is as follows:

 

@isTest
private class SendPaymentTestCase {

static testMethod void myUnitTest() {
string message= '';
boolean flag= true;
integer i =1;
string profileid ='00ed0000000vQrb';
string userID ='005d00000014S25' ;
string Isamex = 'true';
Opportunity opp = new Opportunity();
opp.Name = 'Test Opp';
opp.StageName = 'Ticketing';
opp.p1_First_Name__c =null;
opp.p1_Last_Name__c =null;
opp.p1_Gender__c = null;
opp.p1_First_Name__c =null;
opp.p1_Last_Name__c =null;
opp.p1_Gender__c = null;
opp.Passport_Exp_Date__c = System.now().date();
opp.DOB_Main_Passenger__c = System.now().date();

opp.p2_First_Name__c =null;
opp.p2_Last_Name__c =null;
opp.p2_Gender__c = null;
opp.P2_Passport_Exp_Date__c = System.now().date();
opp.P2_DOB__c = System.now().date();

opp.p3_First_Name__c =null;
opp.p3_Last_Name__c =null;
opp.p3_Gender__c = null;
opp.P3_Passport_Exp_Date__c = System.now().date();
opp.P3_DOB__c = System.now().date();

opp.p4_First_Name__c =null;
opp.p4_Last_Name__c =null;
opp.p4_Gender__c = null;
opp.P4_Passport_Exp_Date__c = System.now().date();
opp.P4_DOB__c = System.now().date();

opp.p5_First_Name__c =null;
opp.p5_Last_Name__c =null;
opp.p5_Gender__c = null;
opp.P5_Passport_Exp_Date__c = System.now().date();
opp.P5_DOB__c = System.now().date();

opp.CloseDate = System.now().date();
opp.Number_of_Passengers__c = 5;
opp.Notification_Email_Address__c ='';
flag = false;
message = 'Notification Email Address is required \n';

opp.One_Way__c = null;
flag = false;
message = 'One Way is required \n';
opp.Return_Date__c = Date.today();
flag = false;
opp.Meal_Preference__c = null;
opp.Override_Itinerary_Builder__c = false;
//opp.Ticketing_Agent__c = 'Anil';
insert opp;

flag = false;
message = 'Notification Email Address is required \n';

//flag= false;
opp.Date_Sold__c = System.now().date();
opp.Departure_Date__c = null;
opp.Destination_Zone__c = null;
opp.Email__c = '';
opp.One_Way__c = 'No';
opp.Return_Date__c = null;
flag = false;
message = 'Return Date is required \n';
opp.Meal_Preference__c =null;
opp.Seat_Preference__c =null;
opp.p1_First_Name__c =null;
opp.p1_Last_Name__c = null;
opp.p1_Gender__c = null;
opp.Passport_Exp_Date__c = System.now().date();
opp.DOB_Main_Passenger__c = null;
opp.Complete_Itinerary_Information__c = null;
opp.p1_First_Name__c =null;
opp.p1_Last_Name__c =null;
opp.p1_Gender__c = null;
opp.p1_First_Name__c =null;
opp.p1_Last_Name__c =null;
opp.p1_Gender__c = null;
opp.p2_First_Name__c ='test1';
opp.p2_Last_Name__c =null;
opp.p2_Gender__c = null;
opp.P2_Passport_Exp_Date__c = System.now().date();
opp.P2_DOB__c = null;
opp.p3_First_Name__c ='test2';
opp.p3_Last_Name__c =null;
opp.p3_Gender__c = null;
opp.P3_Passport_Exp_Date__c = System.now().date();
opp.P3_DOB__c = null;
opp.p4_First_Name__c ='test4';
opp.p4_Last_Name__c =null;
opp.p4_Gender__c = null;
opp.P4_Passport_Exp_Date__c = System.now().date();
opp.P4_DOB__c = null;
opp.p5_First_Name__c =null;
opp.p5_Last_Name__c =null;
opp.p5_Gender__c = null;
opp.P5_Passport_Exp_Date__c = System.now().date();
opp.P5_DOB__c = System.now().date();
opp.Override_Itinerary_Builder__c = true;
opp.ISUpdatedFromSalesforce__c= false;
opp.Origin_City__c ='test';
opp.Destination_City__c ='test1';
opp.StageName = 'Awaiting Customer Payment';

//Awaiting Customer Payment
update opp;

GP_Calc__c gp = new GP_Calc__c();
//gp.Program__c = 'SQ';
//gp.Opportunity__c = opp.Id;
//gp.Mileage__c =22.0;
// gp.Tax__c = 12.0;
// gp.From__c ='ert';
// gp.To__c ='ert';
// insert gp;
message = 'No Mileage & Tax $ has been added \n';
flag = false;
Flight_Request__c fr12 = new Flight_Request__c();
message = 'Flight Request must contain only one record with Customer Approved Itinerary true \n';
flag = false;
Flight_Request__c fr = new Flight_Request__c();
fr.Budget__c = '23';
fr.Publish_Fare__c = '35';
fr.Flight_Itinerary_Name__c ='Test';
fr.Opportunity__c = opp.Id;
fr.From__c = opp.Origin_City__c;
fr.To__c = opp.Destination_City__c;
fr.Outbound_del__c = opp.Departure_Date__c;
fr.Pax_del__c = opp.Number_of_Passengers__c;
fr.Region_del__c = opp.Destination_Zone__c;
fr.Inbound_del__c = opp.Return_Date__c;
fr.Outbound_Flexibility__c = opp.Flexibility_From__c;
fr.Inbound_Flexibility__c = opp.Flexibility_To__c;
fr.Email_To__c ='anil@swiftsetup.com';
fr.Email_Content__c= 'Test';
fr.Subject__c ='subject test';
fr.Customer_Approved_Itinerary__c = true;
fr.Flight_Info__c = 'Test';
fr.Searcher_New__c ='Anil D';
fr.Email_Cc__c ='deepakm@abc.com';
fr.Email_Bcc__c ='anil@abc.com';
insert fr;

Flight_Request__c fr1 = new Flight_Request__c();
fr1.Budget__c = '23';
fr1.Publish_Fare__c = '35';
fr1.Flight_Itinerary_Name__c ='Test';
fr1.Opportunity__c = opp.Id;
fr1.From__c = opp.Origin_City__c;
fr1.To__c = opp.Destination_City__c;
fr1.Outbound_del__c = opp.Departure_Date__c;
fr1.Pax_del__c = opp.Number_of_Passengers__c;
fr1.Region_del__c = opp.Destination_Zone__c;
fr1.Inbound_del__c = opp.Return_Date__c;
fr1.Outbound_Flexibility__c = opp.Flexibility_From__c;
fr1.Inbound_Flexibility__c = opp.Flexibility_To__c;
fr1.Email_To__c ='anil@swiftsetup.com';
fr1.Email_Content__c= 'Test';
fr1.Subject__c ='subject test';
fr1.Customer_Approved_Itinerary__c = true;
fr1.Flight_Info__c = 'Test';
fr1.Searcher_New__c ='Anil D';
fr1.Email_Cc__c ='deepakm@swiftsetup.com';
fr1.Email_Bcc__c ='anil@swiftsetup.com';
insert fr1;


try
{
ApexPages.StandardController sc = new ApexPages.StandardController(opp);
SendPaymentAuthForm SPA = new SendPaymentAuthForm(sc);
SendPaymentAuthForm.Submit(opp.Id, opp.p1_Email_Address__c,Isamex);
List<Opportunity> listOpp = new List<Opportunity>();
// HttpRequest req = new HttpRequest();
// HttpResponse res = new HttpResponse();
// Http http = new Http();
// req.setEndpoint('https://alphaflightguru.com/sendcustomeremail?oppid='+ opp.Id +'&emailId='+opp.Notification_Email_Address__c);
//req.setMethod('GET');

// res = http.send(req);
// string result = res.getBody();
// String expectedValue = 'success';
// System.assertEquals(result, expectedValue);
// System.assertEquals(200,res.getStatusCode());
Opportunity opp2 = new Opportunity();
opp2.Name ='Testopp';
opp2.CloseDate = Date.today();
opp2.StageName ='Awaiting Customer Payment';
opp2.Complete_Itinerary_Information__c ='Testtstdtdd';

listOpp.add(opp2);
Database.update(listOpp);

message =' Email Sent Successfully';
}
catch(System.CalloutException e)
{
message = e.getMessage();
}
message =' Email Sent Not Successfully';
}
static testMethod void myUnitTest02()
{
string profileid ='00ed0000000vQrA';
string userID ='005d00000014S25' ;
boolean flag= true;
Opportunity opp1 = new Opportunity();
ApexPages.StandardController sc = new ApexPages.StandardController(opp1);
SendPaymentAuthForm SPA = new SendPaymentAuthForm(sc);
string message = 'You are not authorized for this.';
Flight_Request__c fr1 = new Flight_Request__c();
message =' Email Sent Not Successfully';



}


}

 

 

 

and my complete class

 

 

global class SendPaymentAuthForm {
public SendPaymentAuthForm(ApexPages.StandardController controller)
{
}
Webservice static string Submit(string oppId,string emailId,string Isamex)
{
string message = '';
string result = '';
boolean flag = true;

System.debug('******Isamex********'+Isamex);

try
{

List<Opportunity> listOpp = new List<Opportunity>();
Opportunity opp = [SELECT id,Name,OwnerId,Override_Itinerary_Builder__c, StageName,Email__c, Departure_Date__c, Destination_Zone__c, Sold__c, Total_Cost__c, GP__c, Total_Miles__c, Date_and_Time_Expires__c, One_Way__c,
Return_Date__c,Lead_ID__c, Meal_Preference__c, Seat_Preference__c, Billing_Address_1__c, Billing_Address_2__c, Billing_City__c, Billing_State__c, Billing_Zip__c,
Pax_Phone__c, Pax_Email_separate_by_comma__c, p1_First_Name__c, p_Middle_Initial__c, p1_Last_Name__c, p1_Gender__c, DOB_Main_Passenger__c, p1_Email_Address__c,
p1_Passport_Number__c, Passport_Exp_Date__c, Number_of_Passengers__c,Complete_Itinerary_Information__c,Date_Sold__c,Order_Submitted__c,
p2_First_Name__c,p2_Last_Name__c,p2_Gender__c,p2_DOB__c,P2_Passport_Exp_Date__c,Notification_Email_Address__c,
p3_First_Name__c,p3_Last_Name__c,p3_Gender__c,P3_DOB__c,P3_Passport_Exp_Date__c,
p4_First_Name__c,p4_Last_Name__c,p4_Gender__c,P4_DOB__c,P4_Passport_Exp_Date__c,ISUpdatedFromSalesforce__c,
p5_First_Name__c,p5_Last_Name__c,p5_Gender__c,P5_DOB__c,P5_Passport_Exp_Date__c,Total_Sold_Amount__c,
Searcher__c FROM Opportunity WHERE Id = :oppId];

GP_Calc__c[] gp_cal = [SELECT id FROM GP_Calc__c WHERE Opportunity__c = :oppId];

GP_Calc__c[] gplist = [SELECT Id FROM GP_Calc__c WHERE Program__c = 'SQ'];

Integer i = [SELECT COUNT() FROM GP_Calc__c WHERE Program__c = 'SQ' and Opportunity__c = :oppId];

if(gp_cal.size() == 0)
{
message += 'No Mileage & Tax $ has been added \n';
flag = false;
}



if(opp.StageName != 'To Be Searched' && opp.StageName != 'Presentation' && opp.StageName != 'Search')
{
flag = false;
message += 'Stage should be \'To Be Searched\' or \'Presentation\' or \'Search\' \n';
}
if(opp.Total_Sold_Amount__c == null)
{
flag = false;
message += 'Total Sold Amount is required \n';
}
if(opp.Departure_Date__c == null)
{
flag = false;
message += 'Departure Date is required \n';
}

if(opp.Destination_Zone__c == null)
{
flag = false;
message += 'Region is required \n';
}
if(opp.Email__c == null)
{
flag = false;
message += 'Email Address is required \n';
}

if(opp.Total_Cost__c <= 0)
{
flag = false;
message += 'Total Cost must be a positive number \n';
}
if(opp.Total_Miles__c <= 0)
{
flag = false;
message += 'Total Miles must be a positive number \n';
}
if(opp.One_Way__c == null)
{
flag = false;
message += 'One Way is required \n';
}
if(opp.One_Way__c == 'No' && opp.Return_Date__c == null)
{
flag = false;
message += 'Return Date is required \n';
}
if(opp.Meal_Preference__c == null)
{
flag = false;
message += 'Meal Preference is required \n';
}
if(opp.Seat_Preference__c == null)
{
flag = false;
message += 'Seat Preference is required \n';
}
if(opp.Notification_Email_Address__c == null)
{
flag = false;
message += 'Notification Email Address is required \n';
}

if(opp.Searcher__c == null)
{
flag = false;
message += 'Searcher is required \n';
}



if(opp.Override_Itinerary_Builder__c == true)
{

if(opp.Complete_Itinerary_Information__c == null || opp.Complete_Itinerary_Information__c == '' || opp.Complete_Itinerary_Information__c == '<br>')
{

flag = false;
message += 'Complete Itinerary Information is required \n';
}

}
else
{

Flight_Request__c[] tf = [SELECT id,Customer_Approved_Itinerary__c,Opportunity__c FROM Flight_Request__c WHERE Opportunity__c = :oppId and Customer_Approved_Itinerary__c = true ];
if (tf.size() != 1)
{
message += 'Flight Request must contain only one record with Customer Approved Itinerary true \n';
flag = false;
}
}


if(flag)
{

//WebServiceSendPayment.HttpResult(oppId, emailId);
HttpRequest req = new HttpRequest();
HttpResponse res = new HttpResponse();
Http http = new Http();
req.setEndpoint('https://abc.com?oppid='+oppId +'&emailId='+emailId+'&Isamex='+Isamex);
req.setMethod('GET');

res = http.send(req);
result = res.getBody();
System.debug('Test result'+res.toString());
System.debug('Result:'+result);


if(result.contains('success'))
{
//WebServiceSubmitOrder.HttpResult(oppId);
//List<Opportunity> listOpp = new List<Opportunity>();
Opportunity oppNew = [SELECT id, StageName,CloseDate,Complete_Itinerary_Information__c FROM Opportunity WHERE Id = :oppId];
oppNew.StageName = 'Awaiting Customer Payment';
listOpp.add(oppNew);

if (listOpp != null && !listOpp .isEmpty())
{
Database.update(listOpp);
}
message =' Email Sent Successfully';
System.debug('STATUS:'+res.getStatus());
System.debug('STATUS_CODE:'+res.getStatusCode());

}
else
{
message =' Email Sent Not Successfully';

}

}



}


catch(System.CalloutException e)
{
System.debug('Callout error: '+ e);

}

return message;
}

}

 

and red lines are not covered.

 

Thanks

PrakashbPrakashb

You have made the Override_Itinerary_Builder__c has true and so its going to your if part and your else part is not getting covered.

TrinayTrinay

Hi Deepak,

 

   After you insert Flight_Request__c record in your test class. You also need to retrive it. 

For example:

so you also need to follow the below lines into your test class. 

 

Flight_Request__c[] tf = [SELECT id,Customer_Approved_Itinerary__c,Opportunity__c FROM Flight_Request__c WHERE Opportunity__c = [inserted record id] and Customer_Approved_Itinerary__c = true ];
if (tf.size() != 1)
{
message += 'Flight Request must contain only one record with Customer Approved Itinerary true \n';
flag = false;
}