You need to sign in to do that
Don't have an account?
kasa venkat 14
hi,i have a class can any one please send me test class for this class.i getting 50%.but remaing iam unable to get percentage .very urgent.thanks in advance.
this is the class.
public class SLS_Notification_Engine {
// Dummy contact for use of template
Contact cnt;
/*
* @Method Name : SLS_Notification_Engine
* @Description : Constructor
*/
public SLS_Notification_Engine() {
cnt = [select id, Email from Contact where lastname != null limit 1];
Map<String, RecordType> mapCaseRTbyDeveloperName = new Map<String, RecordType>();
for(RecordType rt:StaticConstants.getRecordTypeIDsByObject('Case')){
mapCaseRTbyDeveloperName.put(rt.DeveloperName, rt);
}
notifyCreator(mapCaseRTbyDeveloperName);
notifyRequester(mapCaseRTbyDeveloperName);
}
public void notifyCreator( Map<String,RecordType> mapRecordTypes) {
list<case> finalLstCases = new list<case>();
list<messaging.singleEmailMessage> lstMessages = new list<messaging.singleEmailMessage>();
List<Messaging.SingleEmailMessage> msgListToBeSend = new List<Messaging.SingleEmailMessage>();
list<case> lstCases = new List<case>();
ID orgWideEmailID = [select id,DisplayName from OrgWideEmailAddress where DisplayName=:SLS_StaticConstants.SLS_PSD_ORG_WIDE_ADDR_DNAME].id;
ID reqTemplateID = [select id from EmailTemplate where DeveloperName=:SLS_StaticConstants.SLS_REQ_NOTIFY_Temp_NAME].id;
lstCases = [select recordTypeId,status,notify_creator__c,caseNumber,notifiedCreator__c,ownerChangeDate__c,ownerId,owner.Name,owner.Email,lastModifiedDate from case
where recordTypeId = :mapRecordTypes.get(SLS_StaticConstants.SLS_Case_RecordType).Id
and status=:'Open'
and notify_creator__c != null
and notify_creator__c!= 'No Notification'
and notifiedCreator__c =: false
and ownerChangeDate__c != null
];
system.debug('------lstCases in notifyCreator method ***--'+lstCases.size() );
system.debug('------lstCases in notifyCreator method ***--'+lstCases );
if( lstCases != null && !lstCases.isEmpty() ) {
for(case creatorCAS : lstCases) {
Date modifedDate = date.newInstance(creatorCAS.lastModifiedDate.year(),creatorCAS.lastModifiedDate.month(),creatorCAS.lastModifiedDate.day());
if(modifedDate == creatorCAS.ownerChangeDate__c && creatorCAS.notify_creator__c!='No Notification' && system.today() > (creatorCAS.ownerChangeDate__c + integer.ValueOf(creatorCAS.notify_creator__c)) ) { //change the criteria to greater than after testing , + integer.ValueOf(creatorCAS.notify_creator__c)
creatorCAS.notifiedCreator__c = true;
finalLstCases.add(creatorCAS);
}
}
}
system.debug('------finalLstCases in notifyCreator method ***--'+finalLstCases.size() );
if( finalLstCases!= null && !finalLstCases.isEmpty() ) {
lstMessages.clear();
for(case creatorCase : finalLstCases) {
Messaging.SingleEmailMessage creatorMail = new Messaging.SingleEmailMessage();
creatorMail.setTemplateId(reqTemplateID);
creatorMail.setWhatId(creatorCase.id);
creatorMail.setTargetObjectId(cnt.id);
creatorMail.setToAddresses(new string[]{creatorCase.owner.Email});
lstMessages.add(creatorMail);
Savepoint sp = Database.setSavepoint();
Messaging.sendEmail(lstMessages);// Dummy email send
Database.rollback(sp);// Email will not send as it is rolled Back
// Send actual notification email
for (Messaging.SingleEmailMessage email : lstMessages) {
Messaging.SingleEmailMessage emailToSend = new Messaging.SingleEmailMessage();
emailToSend.setOrgWideEmailAddressId(orgWideEmailID);
emailToSend.setToAddresses(email.getToAddresses());
emailToSend.setPlainTextBody(email.getPlainTextBody());
emailToSend.setHTMLBody(email.getHTMLBody());
emailToSend.setSubject(email.getSubject());
msgListToBeSend.add(emailToSend);
}
}
}
system.debug('------------lstMessages in creator method ---'+lstMessages.size() );
try{
if( msgListToBeSend!=null && !msgListToBeSend.isEmpty() )
{
Messaging.sendEmail(msgListToBeSend);
}
if(finalLstCases!= null && !finalLstCases.isEmpty() ) {
StaticFunctions.runOnce();
StaticFunctions.runOnceAfterTrigger();
update finalLstCases;
}
}
catch(exception e) {
system.debug('Exception occured while updating notified creator'+e);
}
}
public void notifyRequester( Map<String,RecordType> mapRecordTypes) {
list<case> finalLstCases = new list<case>();
list<messaging.singleEmailMessage> lstMessages = new list<messaging.singleEmailMessage>();
List<Messaging.SingleEmailMessage> msgListToBeSend = new List<Messaging.SingleEmailMessage>();
ID orgWideEmailID = [select id,DisplayName from OrgWideEmailAddress where DisplayName=:SLS_StaticConstants.SLS_PSD_ORG_WIDE_ADDR_DNAME].id;
ID reqTemplateID = [select id from EmailTemplate where DeveloperName=:SLS_StaticConstants.SLS_CRTR_NOTIFY_Temp_NAME].id;
list<case> lstCases = new list<case>();
lstCases = [ select recordTypeId,caseNumber,status,Requester_Email__c,notify_requestor__c,notifiedRequester__c,Requester__c,Requester__r.Name,External_Requester__c,ownerChangeDate__c,lastModifiedDate from case where
recordTypeId = :mapRecordTypes.get(SLS_StaticConstants.SLS_Case_RecordType).Id
and status =:'Open'
and (Requester_Email__c != null or Requester_Email__c != '' )
and notify_requestor__c != null
and notify_requestor__c!= 'No Notification'
and notifiedRequester__c =: false
and ownerChangeDate__c != null ];
system.debug('------ lstCases size in requester method **----'+lstCases.size() );
system.debug('------ lstCases size in requester method **----'+lstCases);
if(lstCases!=null && !lstCases.isEmpty() ) {
for(case requesterCAS : lstCases) {
Date mofiedDate = date.newInstance(requesterCAS.lastModifiedDate.year(),requesterCAS.lastModifiedDate.month(),requesterCAS.lastModifiedDate.day());
if( requesterCAS.ownerChangeDate__c == mofiedDate && requesterCAS.notify_requestor__c!= 'No Notification' && system.today() > (requesterCAS.ownerChangeDate__c + integer.ValueOf(requesterCAS.notify_requestor__c) ) ) { //change the criteria to greater than after testing,
requesterCAS.notifiedRequester__c = true;
finalLstCases.add(requesterCAS);
}
}
}
system.debug('-----finalLstCases in requester**---'+finalLstCases.size() );
if(finalLstCases!=null && !finalLstCases.isEmpty() ) {
lstMessages.clear();
for(case requesterCase : finalLstCases) {
if( string.isNotBlank(requestercase.Requester_Email__c) ) {
Messaging.SingleEmailMessage requesterMail = new Messaging.SingleEmailMessage();
requesterMail.setTemplateId(reqTemplateID);
requesterMail.setWhatId(requesterCase.id);
requesterMail.setTargetObjectId(cnt.id);
requesterMail.setToAddresses(new string[]{requestercase.Requester_Email__c});
lstMessages.add(requesterMail);
Savepoint sp = Database.setSavepoint();
Messaging.sendEmail(lstMessages); // Dummy email send
Database.rollback(sp); // Email will not send as it is rolled Back
}
for (Messaging.SingleEmailMessage email : lstMessages) {
Messaging.SingleEmailMessage emailToSend = new Messaging.SingleEmailMessage();
emailToSend.setOrgWideEmailAddressId(orgWideEmailID);
emailToSend.setToAddresses(email.getToAddresses());
emailToSend.setPlainTextBody(email.getPlainTextBody());
emailToSend.setHTMLBody(email.getHTMLBody());
emailToSend.setSubject(email.getSubject());
msgListToBeSend .add(emailToSend);
}
}
}
system.debug('----------------------lstMessages in requester ***---------'+lstMessages.size() );
try {
if(!msgListToBeSend.isEmpty() ) {
Messaging.sendEmail(msgListToBeSend);
}
if(!finalLstCases.isEmpty() ) {
StaticFunctions.runOnce();
StaticFunctions.runOnceAfterTrigger();
update finalLstCases;
}
}
catch(Exception e) {
system.debug('Exception occured while updating notified requester'+e);
}
}
}
@isTest()
public class test_schedule_SLS_Notification_Engine {
static testMethod void testScheduleSLSCases() {
list<contact> lstContacts = TestData.createContacts();
insert lstContacts;
Employee__c empObj = SLS_TestData.createEmployees();
insert empObj;
list<case> lstCases = SLS_TestData.createSLSCases(empObj);
system.debug('-----lstCases in test class **---'+lstCases);
lstCases[0].notify_creator__c = '2';
lstCases[0].notify_requestor__c = '2';
update lstCases[0];
Test.startTest();
String CRON_EXP = '0 0 * * * ? *';
String jobId = System.schedule('ScheduleSLSCasesClassTest',CRON_EXP,new schedule_SLS_Notification_Engine() );
Test.stopTest();
}
}
public class SLS_Notification_Engine {
// Dummy contact for use of template
Contact cnt;
/*
* @Method Name : SLS_Notification_Engine
* @Description : Constructor
*/
public SLS_Notification_Engine() {
cnt = [select id, Email from Contact where lastname != null limit 1];
Map<String, RecordType> mapCaseRTbyDeveloperName = new Map<String, RecordType>();
for(RecordType rt:StaticConstants.getRecordTypeIDsByObject('Case')){
mapCaseRTbyDeveloperName.put(rt.DeveloperName, rt);
}
notifyCreator(mapCaseRTbyDeveloperName);
notifyRequester(mapCaseRTbyDeveloperName);
}
public void notifyCreator( Map<String,RecordType> mapRecordTypes) {
list<case> finalLstCases = new list<case>();
list<messaging.singleEmailMessage> lstMessages = new list<messaging.singleEmailMessage>();
List<Messaging.SingleEmailMessage> msgListToBeSend = new List<Messaging.SingleEmailMessage>();
list<case> lstCases = new List<case>();
ID orgWideEmailID = [select id,DisplayName from OrgWideEmailAddress where DisplayName=:SLS_StaticConstants.SLS_PSD_ORG_WIDE_ADDR_DNAME].id;
ID reqTemplateID = [select id from EmailTemplate where DeveloperName=:SLS_StaticConstants.SLS_REQ_NOTIFY_Temp_NAME].id;
lstCases = [select recordTypeId,status,notify_creator__c,caseNumber,notifiedCreator__c,ownerChangeDate__c,ownerId,owner.Name,owner.Email,lastModifiedDate from case
where recordTypeId = :mapRecordTypes.get(SLS_StaticConstants.SLS_Case_RecordType).Id
and status=:'Open'
and notify_creator__c != null
and notify_creator__c!= 'No Notification'
and notifiedCreator__c =: false
and ownerChangeDate__c != null
];
system.debug('------lstCases in notifyCreator method ***--'+lstCases.size() );
system.debug('------lstCases in notifyCreator method ***--'+lstCases );
if( lstCases != null && !lstCases.isEmpty() ) {
for(case creatorCAS : lstCases) {
Date modifedDate = date.newInstance(creatorCAS.lastModifiedDate.year(),creatorCAS.lastModifiedDate.month(),creatorCAS.lastModifiedDate.day());
if(modifedDate == creatorCAS.ownerChangeDate__c && creatorCAS.notify_creator__c!='No Notification' && system.today() > (creatorCAS.ownerChangeDate__c + integer.ValueOf(creatorCAS.notify_creator__c)) ) { //change the criteria to greater than after testing , + integer.ValueOf(creatorCAS.notify_creator__c)
creatorCAS.notifiedCreator__c = true;
finalLstCases.add(creatorCAS);
}
}
}
system.debug('------finalLstCases in notifyCreator method ***--'+finalLstCases.size() );
if( finalLstCases!= null && !finalLstCases.isEmpty() ) {
lstMessages.clear();
for(case creatorCase : finalLstCases) {
Messaging.SingleEmailMessage creatorMail = new Messaging.SingleEmailMessage();
creatorMail.setTemplateId(reqTemplateID);
creatorMail.setWhatId(creatorCase.id);
creatorMail.setTargetObjectId(cnt.id);
creatorMail.setToAddresses(new string[]{creatorCase.owner.Email});
lstMessages.add(creatorMail);
Savepoint sp = Database.setSavepoint();
Messaging.sendEmail(lstMessages);// Dummy email send
Database.rollback(sp);// Email will not send as it is rolled Back
// Send actual notification email
for (Messaging.SingleEmailMessage email : lstMessages) {
Messaging.SingleEmailMessage emailToSend = new Messaging.SingleEmailMessage();
emailToSend.setOrgWideEmailAddressId(orgWideEmailID);
emailToSend.setToAddresses(email.getToAddresses());
emailToSend.setPlainTextBody(email.getPlainTextBody());
emailToSend.setHTMLBody(email.getHTMLBody());
emailToSend.setSubject(email.getSubject());
msgListToBeSend.add(emailToSend);
}
}
}
system.debug('------------lstMessages in creator method ---'+lstMessages.size() );
try{
if( msgListToBeSend!=null && !msgListToBeSend.isEmpty() )
{
Messaging.sendEmail(msgListToBeSend);
}
if(finalLstCases!= null && !finalLstCases.isEmpty() ) {
StaticFunctions.runOnce();
StaticFunctions.runOnceAfterTrigger();
update finalLstCases;
}
}
catch(exception e) {
system.debug('Exception occured while updating notified creator'+e);
}
}
public void notifyRequester( Map<String,RecordType> mapRecordTypes) {
list<case> finalLstCases = new list<case>();
list<messaging.singleEmailMessage> lstMessages = new list<messaging.singleEmailMessage>();
List<Messaging.SingleEmailMessage> msgListToBeSend = new List<Messaging.SingleEmailMessage>();
ID orgWideEmailID = [select id,DisplayName from OrgWideEmailAddress where DisplayName=:SLS_StaticConstants.SLS_PSD_ORG_WIDE_ADDR_DNAME].id;
ID reqTemplateID = [select id from EmailTemplate where DeveloperName=:SLS_StaticConstants.SLS_CRTR_NOTIFY_Temp_NAME].id;
list<case> lstCases = new list<case>();
lstCases = [ select recordTypeId,caseNumber,status,Requester_Email__c,notify_requestor__c,notifiedRequester__c,Requester__c,Requester__r.Name,External_Requester__c,ownerChangeDate__c,lastModifiedDate from case where
recordTypeId = :mapRecordTypes.get(SLS_StaticConstants.SLS_Case_RecordType).Id
and status =:'Open'
and (Requester_Email__c != null or Requester_Email__c != '' )
and notify_requestor__c != null
and notify_requestor__c!= 'No Notification'
and notifiedRequester__c =: false
and ownerChangeDate__c != null ];
system.debug('------ lstCases size in requester method **----'+lstCases.size() );
system.debug('------ lstCases size in requester method **----'+lstCases);
if(lstCases!=null && !lstCases.isEmpty() ) {
for(case requesterCAS : lstCases) {
Date mofiedDate = date.newInstance(requesterCAS.lastModifiedDate.year(),requesterCAS.lastModifiedDate.month(),requesterCAS.lastModifiedDate.day());
if( requesterCAS.ownerChangeDate__c == mofiedDate && requesterCAS.notify_requestor__c!= 'No Notification' && system.today() > (requesterCAS.ownerChangeDate__c + integer.ValueOf(requesterCAS.notify_requestor__c) ) ) { //change the criteria to greater than after testing,
requesterCAS.notifiedRequester__c = true;
finalLstCases.add(requesterCAS);
}
}
}
system.debug('-----finalLstCases in requester**---'+finalLstCases.size() );
if(finalLstCases!=null && !finalLstCases.isEmpty() ) {
lstMessages.clear();
for(case requesterCase : finalLstCases) {
if( string.isNotBlank(requestercase.Requester_Email__c) ) {
Messaging.SingleEmailMessage requesterMail = new Messaging.SingleEmailMessage();
requesterMail.setTemplateId(reqTemplateID);
requesterMail.setWhatId(requesterCase.id);
requesterMail.setTargetObjectId(cnt.id);
requesterMail.setToAddresses(new string[]{requestercase.Requester_Email__c});
lstMessages.add(requesterMail);
Savepoint sp = Database.setSavepoint();
Messaging.sendEmail(lstMessages); // Dummy email send
Database.rollback(sp); // Email will not send as it is rolled Back
}
for (Messaging.SingleEmailMessage email : lstMessages) {
Messaging.SingleEmailMessage emailToSend = new Messaging.SingleEmailMessage();
emailToSend.setOrgWideEmailAddressId(orgWideEmailID);
emailToSend.setToAddresses(email.getToAddresses());
emailToSend.setPlainTextBody(email.getPlainTextBody());
emailToSend.setHTMLBody(email.getHTMLBody());
emailToSend.setSubject(email.getSubject());
msgListToBeSend .add(emailToSend);
}
}
}
system.debug('----------------------lstMessages in requester ***---------'+lstMessages.size() );
try {
if(!msgListToBeSend.isEmpty() ) {
Messaging.sendEmail(msgListToBeSend);
}
if(!finalLstCases.isEmpty() ) {
StaticFunctions.runOnce();
StaticFunctions.runOnceAfterTrigger();
update finalLstCases;
}
}
catch(Exception e) {
system.debug('Exception occured while updating notified requester'+e);
}
}
}
@isTest()
public class test_schedule_SLS_Notification_Engine {
static testMethod void testScheduleSLSCases() {
list<contact> lstContacts = TestData.createContacts();
insert lstContacts;
Employee__c empObj = SLS_TestData.createEmployees();
insert empObj;
list<case> lstCases = SLS_TestData.createSLSCases(empObj);
system.debug('-----lstCases in test class **---'+lstCases);
lstCases[0].notify_creator__c = '2';
lstCases[0].notify_requestor__c = '2';
update lstCases[0];
Test.startTest();
String CRON_EXP = '0 0 * * * ? *';
String jobId = System.schedule('ScheduleSLSCasesClassTest',CRON_EXP,new schedule_SLS_Notification_Engine() );
Test.stopTest();
}
}