How can i get increase coverage for this code? I get only 55% so far.
Also, when I am doing an assert, it should not be equal to '0' but for some reason if i do that then it complains saying it should be 0.
Any help is greatly appreciated.
Thank you, Manohar
global class ISMTrackingBatchProcess implements Database.Batchable < sObject >
{
global Database.QueryLocator start(Database.BatchableContext ctx)
{
System.Debug('***** Here it starts *****');
String Query = 'Select Owner_Name__c, Contact_Name__c,CaseNumber, Owner_Email__c, Dummy_ISM_Datetime__c, TFS_ID__c, Id, ISM_Priority__c, ISM_Priority_Set_Critical_High_At__c From Case where Dummy_ISM_Datetime__c <> Null and ISM_Priority_Set_Critical_High_At__c <> Null and TFS_ID__c <> Null and ISM_Priority__c in (\'Critical\',\'High\')' ;
return Database.getQueryLocator(Query);
}
global void execute(Database.BatchableContext ctx, List<sObject> scope)
{
integer failedUpdates1;
integer failedUpdates2;
List<Case> cs= (List<Case>)scope;
List<ISM_Communication_Tracker__c> ismcts = new List<ISM_Communication_Tracker__c>();
List<ISM_Communication_Alert__c> ismcas = new List<ISM_Communication_Alert__c>();
boolean missflg ;
integer missflg10;
if (cs.size()>0)
{
for(Integer i = 0; i < cs.size(); i++){
if (cs[i].ISM_Priority__c == 'Critical' )
{
// Send Email ISM Alert Emails
integer dmincr = (Math.Floor(decimal.valueOf(Datetime.now().getTime() - cs[i].Dummy_ISM_Datetime__c.getTime() )) / (1000.0*60.0*60.0)).intValue();
for (integer j = 0; j < dmincr; j++){
//Test not covered from here onwards
***task[] t =[select id from task where whatId = :cs[i].id and createddate >= :cs[i].ISM_Priority_Set_Critical_High_At__c.addhours(j) and createddate <= :cs[i].ISM_Priority_Set_Critical_High_At__c.addhours(j).addMinutes(30) and Subject in ('Call','Email') limit 1];
if (t.size() == 0 )
{
ismcas.add(new ISM_Communication_Alert__c(Created_Date_and_Time__c = System.now() ,
Case_Owner_Email__c =cs[i].Owner_Email__c,
ISM_Priority__c =cs[i].ISM_Priority__c,
Unique_index__c =cs[i].CaseNumber +string.valueOf(cs[i].ISM_Priority_Set_Critical_High_At__c.addhours(j)) + string.valueOf(cs[i].ISM_Priority_Set_Critical_High_At__c.addhours(j).addMinutes(30)),
Case__c = cs[i].id,
Log_start_at__c = cs[i].ISM_Priority_Set_Critical_High_At__c.addhours(j),
Log_End_At__c = cs[i].ISM_Priority_Set_Critical_High_At__c.addhours(j).addMinutes(30),
of_Emails__c = j));
}
}***
////Test not covered until here
// Create ISM Communication Logs
integer hrincr = (Math.Floor(decimal.valueOf(Datetime.now().getTime() - cs[i].ISM_Priority_Set_Critical_High_At__c.getTime() )) / (1000.0*60.0*60.0)).intValue();
for (integer j = 0; j < hrincr; j++){
//Test not covered from here onwards
***task[] t =[select id from task where whatId = :cs[i].id and createddate >= :cs[i].ISM_Priority_Set_Critical_High_At__c.addhours(j) and createddate <= :cs[i].ISM_Priority_Set_Critical_High_At__c.addhours(j+1) and Subject in ('Call','Email') limit 1];
if (t.size() > 0 )
{
missflg = false;
missflg10 = 0;
}
else
{
missflg = true;
missflg10 = 1;
}
ismcts.add(new ISM_Communication_Tracker__c(Case__c = cs[i].id,
Missed_Communication__c = missflg,
Missed_Communication1_0__c = missflg10,
Log_Start_At__c = cs[i].ISM_Priority_Set_Critical_High_At__c.addhours(j),
PrimaryKey__c = string.valueOf(cs[i].id) + string.valueOf(cs[i].ISM_Priority_Set_Critical_High_At__c) + cs[i].ISM_Priority__c + string.valueof(j) ,
ISM_Priority__c = cs[i].ISM_Priority__c,
ISM_Priority_Set_Critical_High_At__c = cs[i].ISM_Priority_Set_Critical_High_At__c,
Logged_At__c = cs[i].ISM_Priority_Set_Critical_High_At__c.addhours(j+1),
of_hrs_since_ISM_Priority_Chnaged__c =Math.Floor(Decimal.valueOf(cs[i].ISM_Priority_Set_Critical_High_At__c.addhours(j+1).getTime() - cs[i].ISM_Priority_Set_Critical_High_At__c.getTime() )) / (1000.0*60.0*60.0)
));
}
}***
////Test not covered until here
Else if (cs[i].ISM_Priority__c == 'High' )
{
// Send Email ISM Alert Emails
integer dmincr = (Math.Floor(decimal.valueOf(Datetime.now().getTime() - cs[i].Dummy_ISM_Datetime__c.getTime() )) / (1000.0*60.0*60.0)).intValue();
for (integer j = 0; j < dmincr; j=j+4){
//Test not covered from here onwards
task[] t =[select id from task where whatId = :cs[i].id and createddate >= :cs[i].ISM_Priority_Set_Critical_High_At__c.addhours(j) and createddate <= :cs[i].ISM_Priority_Set_Critical_High_At__c.addhours(j).addMinutes(150) and Subject in ('Call','Email') limit 1];
if (t.size() == 0 )
{
ismcas.add(new ISM_Communication_Alert__c(Created_Date_and_Time__c = System.now() ,
Case_Owner_Email__c =cs[i].Owner_Email__c,
ISM_Priority__c =cs[i].ISM_Priority__c,
Unique_index__c =cs[i].CaseNumber +string.valueOf(cs[i].ISM_Priority_Set_Critical_High_At__c.addhours(j)) + string.valueOf(cs[i].ISM_Priority_Set_Critical_High_At__c.addhours(j).addMinutes(150)),
Case__c = cs[i].id,
Log_start_at__c = cs[i].ISM_Priority_Set_Critical_High_At__c.addhours(j),
Log_End_At__c = cs[i].ISM_Priority_Set_Critical_High_At__c.addhours(j).addMinutes(210),
of_Emails__c = j));
}
}
////Test not covered until here
//Create ISM Communication Tracking Log
integer hrincr = (Math.Floor(decimal.valueOf(Datetime.now().getTime() - cs[i].ISM_Priority_Set_Critical_High_At__c.getTime() )) / (1000.0*60.0*60.0)).intValue();
for (integer j = 3; j < hrincr; j=j+4){
//Test not covered from here onwards
task[] t =[select id from task where whatId = :cs[i].id and createddate >= :cs[i].ISM_Priority_Set_Critical_High_At__c.addhours(j-3) and createddate <= :cs[i].ISM_Priority_Set_Critical_High_At__c.addhours(j+1) and Subject in ('Call','Email') limit 1];
if (t.size() > 0 )
{
missflg = false;
missflg10 = 0;
}
else
{
missflg = true;
missflg10 = 1;
}
ismcts.add(new ISM_Communication_Tracker__c(Case__c = cs[i].id,
Missed_Communication__c = missflg,
Missed_Communication1_0__c = missflg10,
Log_Start_At__c = cs[i].ISM_Priority_Set_Critical_High_At__c.addhours(j-3),
PrimaryKey__c = string.valueOf(cs[i].id) + string.valueOf(cs[i].ISM_Priority_Set_Critical_High_At__c) + cs[i].ISM_Priority__c + + string.valueof(j-3) ,
ISM_Priority__c = cs[i].ISM_Priority__c,
ISM_Priority_Set_Critical_High_At__c = cs[i].ISM_Priority_Set_Critical_High_At__c,
Logged_At__c = cs[i].ISM_Priority_Set_Critical_High_At__c.addhours(j+1),
of_hrs_since_ISM_Priority_Chnaged__c =Math.Floor(Decimal.valueOf(cs[i].ISM_Priority_Set_Critical_High_At__c.addhours(j+1).getTime() - cs[i].ISM_Priority_Set_Critical_High_At__c.getTime() )) / (1000.0*60.0*60.0)
));
}
}
}
failedUpdates1 = 0;
List<Database.UpsertResult> dsrs1 = Database.upsert(ismcts, false);
for(Database.UpsertResult dsr1 : dsrs1){
if(!dsr1.isSuccess()){
failedUpdates1++;
////Test not covered until here
}
}
failedUpdates2 = 0;
List<Database.UpsertResult> dsrs2 = Database.upsert(ismcas, false);
for(Database.UpsertResult dsr2 : dsrs2){
if(!dsr2.isSuccess()){
failedUpdates2++;
}
}
}
}
global void finish(Database.BatchableContext ctx)
{
System.debug(LoggingLevel.WARN,'Batch Process Finished');
}
}
This is what I have so far...
@isTest
private class testscheduleCriticalISM{
public static testMethod void testscheduleISMTrackingBatchProcess()
{
Test.startTest();
scheduleISMTrackingBatchProcess s = new scheduleISMTrackingBatchProcess();
string sch = '0 0 * * 1-12 ? *';
system.schedule('Process Trans 1', sch, s);
Test.stopTest();
}
public static testMethod void testISMTrackingBatchProcess()
{
//get the initial details
Profile pf = [Select Id from Profile where Name = 'System Administrator'];
//creating RunAs User Record
User u = new User();
u.FirstName = 'Test';
u.LastName = 'user';
u.Email = '[email protected]';
u.CompanyName = 'test.com';
u.Title = 'Test user';
u.Username = '[email protected]';
u.Alias = 'testuser';
u.CommunityNickname = 'Test User';
u.TimeZoneSidKey = 'America/Chicago';
u.LocaleSidKey = 'en_US';
u.EmailEncodingKey = 'ISO-8859-1';
u.ProfileId = pf.Id;
u.LanguageLocaleKey = 'en_US';
insert u;
Datetime dToday = System.now().addHours(-5);
String ISM_Priority;
List <Case> kases = new List<Case>();
for(integer i = 0; i<200; i++){
if (i < 100)
{ ISM_Priority = 'Critical';}
else
{ ISM_Priority = 'High';}
Case c = new Case(Subject='testCase'+'i',
Ownerid = u.ID,
Status = 'Open',
Origin = 'Other',
TFS_ID__c = i,
ISM_Priority_Set_Critical_High_At__c = origDate,
Dummy_ISM_Datetime__c = origDate.addMinutes(-30),
ISM_Priority__c = ISM_Priority
);
kases.add(c);
}
insert kases;
task[] t =[select id from task where Subject in ('Call','Email') ];
integer dmincr = (Math.Floor(decimal.valueOf(System.now().getTime() - dToday.addMinutes(-30).getTime() )) / (1000.0*60.0*60.0)).intValue();
integer hrincr = (Math.Floor(decimal.valueOf(System.now().getTime() - dToday.getTime() )) / (1000.0*60.0*60.0)).intValue();
Test.startTest();
ISMTrackingBatchProcess b = new ISMTrackingBatchProcess();
ID myBatchJobID = database.executebatch(b);
Test.stopTest();
if (t.size() ==0 && hrincr > 4)
{
System.AssertEquals(database.countquery('SELECT COUNT() FROM ISM_Communication_Alert__c WHERE ISM_Priority__c =\'Critical\''), 0);
}
if (t.size() ==0 && dmincr > 4)
{
System.AssertEquals(database.countquery('SELECT COUNT() FROM ISM_Communication_Tracker__c WHERE ISM_Priority__c =\'High\''), 0);
}
}
}