You need to sign in to do that
Don't have an account?

Test Class
Hi
please any one help me write a test class for this code:
public class CaseMergeWithParent {
public String s{ get; set; }
public String SearchText { get; set; }
public String operator1 { get; set; }
public String operator2 { get; set; }
public String operator3 { get; set; }
public String SearchSubject { get; set; }
public boolean isclosed {get;set;}
public String SearchDesc { get; set; }
public transient list<Case> CasesFound { get; set;}
public map<string,Case> CasesMap {get;set;}
public map<string,string> IdCaseNumberMap { get; set; }
public list<Case> CasesSelected { get; set; }
public case c {get;set;}
public case mc {get;set;}
public case contcase;
public boolean onload {get;set;}
public boolean includeAtt {get;set;}
// public boolean includeFeed {get;set;} // not being used because of the complexity for the time being
public boolean includeComments {get;set;}
public boolean includeHist {get;set;}
public boolean includeEmails {get;set;}
public boolean includeSolutionDetails {get;set;}
public boolean showtable {get;set;}
public string notes='';
public CaseMergeWithParent(ApexPages.StandardController controller){
showtable = false;
onload = true;
c = new case();
mc = new case();
contcase = new case();
CasesFound = new list<Case>();
CasesSelected = new list<Case>();
CasesMap = new map<string,Case>();
IdCaseNumberMap = new map<string,string>();
operator1 = operator2 = operator3 = 'AND';
contcase = (case)controller.getRecord();
//Force compatibiltiy mode to IE8 so page works in IE9 browsers. Official ie9 support expected Winter12 release
String browserType = Apexpages.currentPage().getHeaders().get('USER-AGENT');
if(browserType != null && browserType.contains('MSIE')){
Apexpages.currentPage().getHeaders().put('X-UA-Compatible', 'IE=8');
}
}
public PageReference runSearch() {
// SearchText = Apexpages.currentPage().getParameters().get('str');
onload = false;
string tmpSearchText = '\'%'+SearchText+'%\'';
string tmpSearchSubject = '\'%'+SearchSubject+'%\'';
CasesFound = new list<case>();
CasesSelected.clear();
String qryString = 'select Id, CaseNumber, Subject,IsClosed, Description, Additional_details__c, Detailed_Description__c,contactid,accountid,CreatedDate from case'+
' WHERE ('+
'(RecordTypeId = \'012G00000011jKv\')'+
+' AND ('+'isClosed = ' + isclosed+')';
IF(SearchText!=''){
qryString = qryString+' AND ('+'CaseNumber LIKE ' + tmpSearchText+')';
}
IF(SearchSubject!=''){
qryString = qryString+' '+operator1+' ('+'SUBJECT LIKE ' + tmpSearchSubject+' )' ;
}
string contactid = contcase.contactid;
system.debug('xxxxxxxxxxxx'+contactid);
IF(contactid!=null){
qryString = qryString+' '+operator2+' ('+'CONTACT.ID = \'' +contactid+'\')';
}
string accountid = contcase.accountid;
IF(accountid!=null){
qryString = qryString+' '+operator3+' ('+'ACCOUNT.ID = \'' +accountid+'\')';
}
qryString = qryString+') limit 1000';
system.debug('xxxxxxxxxxxx'+qryString);
CasesFound = Database.query(qryString);
return null;
}
public PageReference findSelected() {
CasesFound = new list<case>();
CasesSelected.clear();
onload = true;
string strselcaseIds = Apexpages.currentPage().getParameters().get('all');
list<string> lstselcaseIds = new list<string>();
if(strselcaseIds.contains(',')){
lstselcaseIds = strselcaseIds.split(',');
}
else{
lstselcaseIds.add(strselcaseIds);
}
CasesMap = new map<string,Case>([select Id, CaseNumber, Subject,IsClosed, Description, Additional_details__c, Detailed_Description__c,contactid,accountid,CreatedDate
from case where id in :lstselcaseIds]);
showtable = true;
return null;
}
public void do1() {
s = Apexpages.currentPage().getParameters().get('masterCaseId');
}
public pagereference casemerge(){
mc = CasesMap.get(s);
CasesMap.remove(s);
list<EmailMessage> emaillist = new list<EmailMessage>();
Attachment[] insertAttList = new Attachment[]{};
map<string, list<CaseComment>> caseCommMap;
map<string, list<CaseHistory>> caseHistMap;
map<string, list<EmailMessage>> caseEmailMap;
map<string, list<Attachment>> caseAttchMap;
map<string, list<solution_details__C>> caseSDs;
if(includeComments){
caseCommMap = new map<string,list<CaseComment>>();
for(CaseComment cc: [SELECT Id, CommentBody,CreatedDate,parentId, CreatedBy.name FROM CaseComment where parentId IN :CasesMap.keySet()]){
if(!caseCommMap.containsKey(cc.parentId)){
caseCommMap.put(cc.parentId, new list<CaseComment>());
}
caseCommMap.get(cc.parentId).add(cc);
}
}
if(includeHist){
caseHistMap = new map<string,list<CaseHistory>>();
for(CaseHistory ch: [SELECT Id, CreatedBy.name, CreatedDate, Field, OldValue, NewValue, CaseId FROM CaseHistory
where CaseId IN :CasesMap.keySet()]){
if(!caseHistMap.containsKey(ch.CaseId)){
caseHistMap.put(ch.CaseId, new list<CaseHistory>());
}
caseHistMap.get(ch.CaseId).add(ch);
}
}
if(includeEmails){
caseEmailMap = new map<string,list<EmailMessage>>();
for(EmailMessage e: [SELECT Id, ActivityId,
TextBody, HtmlBody, Headers, Subject, FromName, FromAddress, ToAddress, CcAddress, parentId,
BccAddress, Incoming, HasAttachment, Status, MessageDate, IsDeleted, ReplyToEmailMessageId FROM EmailMessage
where parentId IN :CasesMap.keySet()]) {
if(!caseEmailMap.containsKey(e.parentId)){
caseEmailMap.put(e.parentId, new list<EmailMessage>());
}
caseEmailMap.get(e.parentId).add(e);
}
}
if(includeAtt){
caseAttchMap = new map<string,list<Attachment>>();
for(Attachment a: [SELECT Id, IsDeleted, Name, IsPrivate, ContentType, BodyLength, Body, OwnerId,parentId,
Description, IsPartnerShared, ConnectionReceivedId, ConnectionSentId FROM Attachment
where parentId IN :CasesMap.keySet()]) {
if(!caseAttchMap.containsKey(a.parentId)){
caseAttchMap.put(a.parentId, new list<Attachment>());
}
caseAttchMap.get(a.parentId).add(a);
}
}
for(case cs : CasesMap.values()){
mc.description = nullify(mc.description) +'\r\n'+ nullify(cs.description);
mc.Additional_details__c = nullify(mc.Additional_details__c) +'\r\n'+ nullify(cs.Additional_details__c);
mc.Detailed_Description__c = nullify(mc.Detailed_Description__c) + '\r\n'+ nullify(cs.Detailed_Description__c);
notes='--------------------Details Of Merged Case Number # '+cs.casenumber+'--------------------';
notes=notes+'\r\n\r\nThis case has been merged by '+userinfo.getName()+' on '+system.now();
Attachment attach1= new Attachment();
attach1.ParentId = mc.id;
attach1.Name = 'Merged Case# '+cs.casenumber+' Data.txt';
if(includeComments && caseCommMap.containsKey(cs.Id)){
string comments ='\r\n\r\n------------- Case Comments -------------';
for(CaseComment cc: caseCommMap.get(cs.Id)) {
comments = comments+'\r\n'+'Comment Created By: '+cc.CreatedBy.name +' On '+cc.CreatedDate + cc.CommentBody;
}
notes=notes+comments;
}
if(includeHist && caseHistMap.containsKey(cs.Id)){
string history='\r\n\r\n------------- Case Field History -------------';
integer i=1;
for(CaseHistory cc: caseHistMap.get(cs.Id)) {
history = history+'\r\n'+i+' - '+cc.CreatedBy.name +' On '+cc.CreatedDate + ' changed '+cc.field+' from '+cc.oldvalue+' to '+cc.newvalue;
i++;
}
notes=notes+history;
}
attach1.Body = Blob.valueOf(notes);
insertAttList.add(attach1);
if(includeEmails && caseEmailMap.containsKey(cs.Id)){
system.debug('got in to includeEmail');
for(EmailMessage e: caseEmailMap.get(cs.Id)) {
EmailMessage eclone = e.clone();
eclone.parentid = mc.id;
emaillist.add(eclone);
}
}
if(includeAtt && caseAttchMap.containsKey(cs.Id)){
for(Attachment a: caseAttchMap.get(cs.Id)) {
Attachment aclone = a.clone();
aclone.parentid = mc.id;
insertAttList.add(aclone);
}
}
}
if(!emaillist.isEmpty())
{
insert emaillist;
}
if(!insertAttList.isEmpty()){
insert insertAttList;
}
update mc;
delete CasesMap.values();
return new pagereference('/'+mc.id);
}
public static string nullify(string s){
return ((s == null) ? ' ' : s);
}
}
Thanks
Satheesh
please any one help me write a test class for this code:
public class CaseMergeWithParent {
public String s{ get; set; }
public String SearchText { get; set; }
public String operator1 { get; set; }
public String operator2 { get; set; }
public String operator3 { get; set; }
public String SearchSubject { get; set; }
public boolean isclosed {get;set;}
public String SearchDesc { get; set; }
public transient list<Case> CasesFound { get; set;}
public map<string,Case> CasesMap {get;set;}
public map<string,string> IdCaseNumberMap { get; set; }
public list<Case> CasesSelected { get; set; }
public case c {get;set;}
public case mc {get;set;}
public case contcase;
public boolean onload {get;set;}
public boolean includeAtt {get;set;}
// public boolean includeFeed {get;set;} // not being used because of the complexity for the time being
public boolean includeComments {get;set;}
public boolean includeHist {get;set;}
public boolean includeEmails {get;set;}
public boolean includeSolutionDetails {get;set;}
public boolean showtable {get;set;}
public string notes='';
public CaseMergeWithParent(ApexPages.StandardController controller){
showtable = false;
onload = true;
c = new case();
mc = new case();
contcase = new case();
CasesFound = new list<Case>();
CasesSelected = new list<Case>();
CasesMap = new map<string,Case>();
IdCaseNumberMap = new map<string,string>();
operator1 = operator2 = operator3 = 'AND';
contcase = (case)controller.getRecord();
//Force compatibiltiy mode to IE8 so page works in IE9 browsers. Official ie9 support expected Winter12 release
String browserType = Apexpages.currentPage().getHeaders().get('USER-AGENT');
if(browserType != null && browserType.contains('MSIE')){
Apexpages.currentPage().getHeaders().put('X-UA-Compatible', 'IE=8');
}
}
public PageReference runSearch() {
// SearchText = Apexpages.currentPage().getParameters().get('str');
onload = false;
string tmpSearchText = '\'%'+SearchText+'%\'';
string tmpSearchSubject = '\'%'+SearchSubject+'%\'';
CasesFound = new list<case>();
CasesSelected.clear();
String qryString = 'select Id, CaseNumber, Subject,IsClosed, Description, Additional_details__c, Detailed_Description__c,contactid,accountid,CreatedDate from case'+
' WHERE ('+
'(RecordTypeId = \'012G00000011jKv\')'+
+' AND ('+'isClosed = ' + isclosed+')';
IF(SearchText!=''){
qryString = qryString+' AND ('+'CaseNumber LIKE ' + tmpSearchText+')';
}
IF(SearchSubject!=''){
qryString = qryString+' '+operator1+' ('+'SUBJECT LIKE ' + tmpSearchSubject+' )' ;
}
string contactid = contcase.contactid;
system.debug('xxxxxxxxxxxx'+contactid);
IF(contactid!=null){
qryString = qryString+' '+operator2+' ('+'CONTACT.ID = \'' +contactid+'\')';
}
string accountid = contcase.accountid;
IF(accountid!=null){
qryString = qryString+' '+operator3+' ('+'ACCOUNT.ID = \'' +accountid+'\')';
}
qryString = qryString+') limit 1000';
system.debug('xxxxxxxxxxxx'+qryString);
CasesFound = Database.query(qryString);
return null;
}
public PageReference findSelected() {
CasesFound = new list<case>();
CasesSelected.clear();
onload = true;
string strselcaseIds = Apexpages.currentPage().getParameters().get('all');
list<string> lstselcaseIds = new list<string>();
if(strselcaseIds.contains(',')){
lstselcaseIds = strselcaseIds.split(',');
}
else{
lstselcaseIds.add(strselcaseIds);
}
CasesMap = new map<string,Case>([select Id, CaseNumber, Subject,IsClosed, Description, Additional_details__c, Detailed_Description__c,contactid,accountid,CreatedDate
from case where id in :lstselcaseIds]);
showtable = true;
return null;
}
public void do1() {
s = Apexpages.currentPage().getParameters().get('masterCaseId');
}
public pagereference casemerge(){
mc = CasesMap.get(s);
CasesMap.remove(s);
list<EmailMessage> emaillist = new list<EmailMessage>();
Attachment[] insertAttList = new Attachment[]{};
map<string, list<CaseComment>> caseCommMap;
map<string, list<CaseHistory>> caseHistMap;
map<string, list<EmailMessage>> caseEmailMap;
map<string, list<Attachment>> caseAttchMap;
map<string, list<solution_details__C>> caseSDs;
if(includeComments){
caseCommMap = new map<string,list<CaseComment>>();
for(CaseComment cc: [SELECT Id, CommentBody,CreatedDate,parentId, CreatedBy.name FROM CaseComment where parentId IN :CasesMap.keySet()]){
if(!caseCommMap.containsKey(cc.parentId)){
caseCommMap.put(cc.parentId, new list<CaseComment>());
}
caseCommMap.get(cc.parentId).add(cc);
}
}
if(includeHist){
caseHistMap = new map<string,list<CaseHistory>>();
for(CaseHistory ch: [SELECT Id, CreatedBy.name, CreatedDate, Field, OldValue, NewValue, CaseId FROM CaseHistory
where CaseId IN :CasesMap.keySet()]){
if(!caseHistMap.containsKey(ch.CaseId)){
caseHistMap.put(ch.CaseId, new list<CaseHistory>());
}
caseHistMap.get(ch.CaseId).add(ch);
}
}
if(includeEmails){
caseEmailMap = new map<string,list<EmailMessage>>();
for(EmailMessage e: [SELECT Id, ActivityId,
TextBody, HtmlBody, Headers, Subject, FromName, FromAddress, ToAddress, CcAddress, parentId,
BccAddress, Incoming, HasAttachment, Status, MessageDate, IsDeleted, ReplyToEmailMessageId FROM EmailMessage
where parentId IN :CasesMap.keySet()]) {
if(!caseEmailMap.containsKey(e.parentId)){
caseEmailMap.put(e.parentId, new list<EmailMessage>());
}
caseEmailMap.get(e.parentId).add(e);
}
}
if(includeAtt){
caseAttchMap = new map<string,list<Attachment>>();
for(Attachment a: [SELECT Id, IsDeleted, Name, IsPrivate, ContentType, BodyLength, Body, OwnerId,parentId,
Description, IsPartnerShared, ConnectionReceivedId, ConnectionSentId FROM Attachment
where parentId IN :CasesMap.keySet()]) {
if(!caseAttchMap.containsKey(a.parentId)){
caseAttchMap.put(a.parentId, new list<Attachment>());
}
caseAttchMap.get(a.parentId).add(a);
}
}
for(case cs : CasesMap.values()){
mc.description = nullify(mc.description) +'\r\n'+ nullify(cs.description);
mc.Additional_details__c = nullify(mc.Additional_details__c) +'\r\n'+ nullify(cs.Additional_details__c);
mc.Detailed_Description__c = nullify(mc.Detailed_Description__c) + '\r\n'+ nullify(cs.Detailed_Description__c);
notes='--------------------Details Of Merged Case Number # '+cs.casenumber+'--------------------';
notes=notes+'\r\n\r\nThis case has been merged by '+userinfo.getName()+' on '+system.now();
Attachment attach1= new Attachment();
attach1.ParentId = mc.id;
attach1.Name = 'Merged Case# '+cs.casenumber+' Data.txt';
if(includeComments && caseCommMap.containsKey(cs.Id)){
string comments ='\r\n\r\n------------- Case Comments -------------';
for(CaseComment cc: caseCommMap.get(cs.Id)) {
comments = comments+'\r\n'+'Comment Created By: '+cc.CreatedBy.name +' On '+cc.CreatedDate + cc.CommentBody;
}
notes=notes+comments;
}
if(includeHist && caseHistMap.containsKey(cs.Id)){
string history='\r\n\r\n------------- Case Field History -------------';
integer i=1;
for(CaseHistory cc: caseHistMap.get(cs.Id)) {
history = history+'\r\n'+i+' - '+cc.CreatedBy.name +' On '+cc.CreatedDate + ' changed '+cc.field+' from '+cc.oldvalue+' to '+cc.newvalue;
i++;
}
notes=notes+history;
}
attach1.Body = Blob.valueOf(notes);
insertAttList.add(attach1);
if(includeEmails && caseEmailMap.containsKey(cs.Id)){
system.debug('got in to includeEmail');
for(EmailMessage e: caseEmailMap.get(cs.Id)) {
EmailMessage eclone = e.clone();
eclone.parentid = mc.id;
emaillist.add(eclone);
}
}
if(includeAtt && caseAttchMap.containsKey(cs.Id)){
for(Attachment a: caseAttchMap.get(cs.Id)) {
Attachment aclone = a.clone();
aclone.parentid = mc.id;
insertAttList.add(aclone);
}
}
}
if(!emaillist.isEmpty())
{
insert emaillist;
}
if(!insertAttList.isEmpty()){
insert insertAttList;
}
update mc;
delete CasesMap.values();
return new pagereference('/'+mc.id);
}
public static string nullify(string s){
return ((s == null) ? ' ' : s);
}
}
Thanks
Satheesh
Let me know if you get into any issues - http://forceschool.blogspot.in/2011/06/testing-apex-structure-of-test-class.html