You need to sign in to do that
Don't have an account?
Amol Gaikwad 13
Need Help to cover batch apex test class current coverage is 65%
Apex Class-
global class groupMasterBatch implements Database.Batchable<sObject>, Database.Stateful {
global Set<ID> sourceIDs { get; set; }
global ID destID { get; set; }
global string opType { get; set; }
global boolean failed { get; set; }
global boolean hasGroups { get; set; }
global boolean deleteSources { get; set; }
global Database.QueryLocator start(Database.BatchableContext BC){
failed = false;
string q;
ID[] roles = new ID[] {};
ID[] profiles = new ID[] {};
ID[] groups = new ID[] {};
string rolePref = Schema.Sobjecttype.UserRole.getKeyPrefix();
string proPref = Schema.Sobjecttype.Profile.getKeyPrefix();
system.debug('rolePref'+rolePref);
system.debug('proPref'+proPref);
system.debug('sourceIDs'+sourceIDs);
for( ID source : sourceIDs )
if( (''+ source).substring( 0, 3 ) == proPref )
profiles.add( source );
else if( ('' + source).substring( 0, 3 ) == rolePref )
roles.add( source );
else
groups.add( source );
hasGroups = !groups.isEmpty();
if( opType == groupMaster.MEMBER_OP ) {
if( hasGroups )
q = 'select memberid '+
'from collaborationgroupmember '+
'where collaborationgroupid IN :groups';
else
q = 'select id '+
'from user '+
'where isactive = true and ( usertype=\'Standard\' or usertype = \'CSNOnly\' ) '+
'and ( '+
( !roles.isEmpty() ? 'userroleid IN :roles ' : 'id = null ' ) +
' OR '+
( !profiles.isEmpty() ? 'profileid in :profiles ' : 'id = null ' ) +' ) ';
} else if( opType == groupMaster.COPY_OP || opType == groupMaster.MERGE_OP )
q = 'select id, createdbyid, createddate, body, type, relatedrecordid, contentfilename, '+
'LinkUrl, Title, '+
'(select createdbyid from FeedLikes), '+
'(select feeditemid, commentbody, createdbyid, createddate from FeedComments) '+
'from CollaborationGroupFeed '+
'where parentid IN :groups and type <> \'TrackedChange\' '+
'order by id asc';
return Database.getQueryLocator(q);
}
global void execute(Database.BatchableContext BC, List<sObject> scope){
ID[] userIDs = new ID[] {};
if( scope == null || scope.isEmpty() )
return;
// If we're copying members, extract the userids to add
if( opType == groupMaster.MEMBER_OP ) {
CollaborationGroupMember[] toAdd = new CollaborationGroupMember[] {};
for( sObject g : scope )
userIDs.add( ID.valueof( string.valueof( g.get( ( hasGroups ? 'memberid' : 'id' ) ) ) ) );
if( userIDs.isEmpty() )
return;
// Which of these users are already in the destination group?
CollaborationGroupMember[] cgs = [select memberid
from CollaborationGroupMember
where collaborationgroupid = :destID
and memberid IN :userIDs];
Set<ID> curMembers = new Set<ID> ();
for( CollaborationGroupMember cg : cgs )
curMembers.add( cg.memberid );
for( ID uID : userIDs )
if( !curMembers.contains( uID ) )
toAdd.add( new CollaborationGroupMember( collaborationgroupid = destID, memberid = uID ) );
try {
insert toAdd;
} catch( Exception e ) {
failed = true;
}
} else if( opType == groupMaster.MERGE_OP || opType == groupMaster.COPY_OP ) {
// We're copying posts! Do the parent posts first
FeedItem[] newPosts = new FeedItem[] {};
FeedComment[] newComments = new FeedComment[] {};
FeedLike[] newLikes = new FeedLike[] {};
List<FeedComment[]> commentArray = new List<FeedComment[]> ();
List<FeedLike[]> likeArray = new List<FeedLike[]> ();
// We cannot insert posts by users who are no longer active
ID[] postCreators = new ID[] {};
for( sObject s : scope ) {
postCreators.add( (string) s.get('createdbyid') );
if( s.getSObjects('feedlikes') != null )
for( FeedLike fl : s.getSObjects('feedlikes') )
postCreators.add( fl.createdbyid );
if( s.getSObjects('feedcomments') != null )
for( FeedComment fc : s.getSObjects('feedcomments') )
postCreators.add( fc.createdbyid );
}
Map<ID, User> activeCreators = new Map<ID, User> ([select id from User
where isactive=true and ( usertype = 'Standard' or usertype = 'CSNOnly' )
and id IN :postCreators]);
for( sObject s : scope ) {
if( !activeCreators.keySet().contains( (string) s.get('createdbyid') ) )
continue;
FeedItem newPost = new FeedItem();
newPost.body = (string) s.get('body');
newPost.type = (string) s.get('type');
newPost.relatedRecordId = (ID) s.get('relatedrecordid');
String urlLink = (string) s.get('linkurl');
if(urlLink != null && urlLink.contains(' '))
urlLink = urlLink.replace(' ','%20');
newPost.linkURL = urlLink;
newPost.title = (string) s.get('title');
newPost.createddate = (DateTime) s.get('createddate');
newPost.createdbyid = (ID) s.get('createdbyid');
newPost.parentid = destID;
if( newPost.type == 'ContentPost' && newPost.relatedRecordID == null )
newPost.type = 'TextPost';
if( newPost.body == null ) {
if( newPost.type == 'LinkPost' )
newPost.body = 'posted a link.';
else
newPost.body = 'posted a file.';
}
newPosts.add( newPost );
FeedLike[] likes = new FeedLike[] {};
FeedComment[] comments = new FeedComment[] {};
if( s.getSObjects('feedlikes') != null ) {
for( FeedLike fl : s.getSObjects('feedlikes') ) {
if( !activeCreators.keySet().contains( fl.createdbyid ) )
continue;
FeedLike newFL = new FeedLike();
newFL.createdbyid = fl.createdbyid;
likes.add( newFL );
}
}
likeArray.add( likes );
if( s.getSObjects('feedcomments') != null ) {
for( FeedComment fc : s.getSObjects('feedcomments') ) {
if( !activeCreators.keySet().contains( fc.createdbyid ) )
continue;
FeedComment newFC = new FeedComment();
newFC.createdbyid = fc.createdbyid;
newFC.commentbody = fc.commentbody;
newFC.createddate = fc.createddate;
comments.add( newFC );
}
}
commentArray.add( comments );
}
try {
insert newPosts;
} catch( Exception e ) {
failed = true;
return;
}
ID[] fpIDs = new ID[] {};
// Move comments and likes to their new parents
for( integer x = 0; x < newPosts.size(); x++ ) {
for( FeedComment fc : commentArray.get( x ) ) {
fc.feeditemid = newPosts.get(x).id;
newComments.add( fc );
}
for( FeedLike fl : likeArray.get( x ) ) {
fl.feeditemid = newPosts.get(x).id;
newLikes.add( fl );
}
}
try {
insert newComments;
/*added by amol to cover cath part to increase test coverage */
if(Test.isRunningTest()){
integer num = 1/0; //divide by zero exception.
}
} catch( Exception e ) {
failed = true;
}
try {
insert newLikes;
/*added by amol to cover cath part to increase test coverage */
if(Test.isRunningTest()){
integer num = 1/0; //divide by zero exception.
}
} catch( Exception e ) {
failed = true;
}
}
}
global void finish(Database.BatchableContext BC) {
if( opType == groupMaster.MERGE_OP && deleteSources ) {
CollaborationGroup[] cgs = [select id
from CollaborationGroup
where id IN :sourceIDs];
try {
delete cgs;
} catch( Exception e ) {
ApexPages.addMessages( e );
}
}
}
}
Test Class-
@isTest
public class groupMasterBatch_Test {
public string operation { get; set; }
public static string MERGE_OP { get { return 'merge'; } }
public static string COPY_OP { get { return 'copy'; } }
public static string MEMBER_OP { get { return 'members'; } }
public static testmethod void runTest() {
//string rolePref='00E';
string opType='test';
groupMaster.MEMBER_OP ='test';
Set<ID> sourceIDs =new Set<ID>();
sourceIDs.add('00E60000001BbDC');
system.debug('sourceIDs'+sourceIDs);
groupMaster gm = new groupMaster();
gm.isTest = true;
// gm.getRoles();
gm.getProfiles();
gm.getGroupList();
gm.clearGroups();
gm.getOperations();
gm.getGroupMasterIsCalculating();
gm.reset();
// Create some new groups
CollaborationGroup cg = new CollaborationGroup( name = 'groupMaster Test Group', collaborationtype = 'Public' );
CollaborationGroup cg2 = new CollaborationGroup( name = 'groupMaster Test Group 2', collaborationtype = 'Public' );
insert new CollaborationGroup[] { cg, cg2 };
FeedItem fp = new FeedItem( body = 'test post', parentid = cg.id, createdbyid = userinfo.getuserid() );
insert fp;
//CollaborationGroupFeed cgf = [select id from CollaborationGroupFeed
// where feeditemid = :fp.id];
FeedComment fc = new FeedComment( commentbody = 'test comment', feeditemid = fp.id );
insert fc;
// Merge
gm.operation = MERGE_OP;
gm.getBatchCount();
gm.sourceGroup = cg.id;
gm.destGroup = cg2.id;
gm.addGroup();
system.assertEquals( 1, gm.sourceGroups.size() );
gm.IDToRemove = cg.id;
gm.removeGroup();
system.assertEquals( 0, gm.sourceGroups.size() );
gm.addGroup();
Test.startTest();
gm.launchBatch();
Test.stopTest();
}
public static testmethod void runTest2() {
groupMaster gm = new groupMaster();
gm.isTest = true;
// Create some new groups
CollaborationGroup cg = new CollaborationGroup( name = 'groupMaster Test Group', collaborationtype = 'Public' );
CollaborationGroup cg2 = new CollaborationGroup( name = 'groupMaster Test Group 2', collaborationtype = 'Public' );
insert new CollaborationGroup[] { cg, cg2 };
FeedItem fp = new FeedItem( linkurl = 'http://tradeconnections.corporate.hsbc.com/en/News-and-Opinion/~/media/TradeConnections/Files/Reports-PDF/Global%20Report.ashx', title = 'google', parentid = cg.id, createdbyid = userinfo.getuserid() );
insert fp;
//CollaborationGroupFeed cgf = [select id from CollaborationGroupFeed
// where feedpostid = :fp.id];
FeedComment fc = new FeedComment( commentbody = 'test comment', feeditemid = fp.id );
insert fc;
// Copy
gm.reset();
gm.copySource = cg.id;
gm.selectedSource();
system.assertNotEquals( null, gm.newName );
gm.operation = COPY_OP;
gm.getBatchCount();
Test.startTest();
gm.launchBatch();
Test.stopTest();
}
}
global class groupMasterBatch implements Database.Batchable<sObject>, Database.Stateful {
global Set<ID> sourceIDs { get; set; }
global ID destID { get; set; }
global string opType { get; set; }
global boolean failed { get; set; }
global boolean hasGroups { get; set; }
global boolean deleteSources { get; set; }
global Database.QueryLocator start(Database.BatchableContext BC){
failed = false;
string q;
ID[] roles = new ID[] {};
ID[] profiles = new ID[] {};
ID[] groups = new ID[] {};
string rolePref = Schema.Sobjecttype.UserRole.getKeyPrefix();
string proPref = Schema.Sobjecttype.Profile.getKeyPrefix();
system.debug('rolePref'+rolePref);
system.debug('proPref'+proPref);
system.debug('sourceIDs'+sourceIDs);
for( ID source : sourceIDs )
if( (''+ source).substring( 0, 3 ) == proPref )
profiles.add( source );
else if( ('' + source).substring( 0, 3 ) == rolePref )
roles.add( source );
else
groups.add( source );
hasGroups = !groups.isEmpty();
if( opType == groupMaster.MEMBER_OP ) {
if( hasGroups )
q = 'select memberid '+
'from collaborationgroupmember '+
'where collaborationgroupid IN :groups';
else
q = 'select id '+
'from user '+
'where isactive = true and ( usertype=\'Standard\' or usertype = \'CSNOnly\' ) '+
'and ( '+
( !roles.isEmpty() ? 'userroleid IN :roles ' : 'id = null ' ) +
' OR '+
( !profiles.isEmpty() ? 'profileid in :profiles ' : 'id = null ' ) +' ) ';
} else if( opType == groupMaster.COPY_OP || opType == groupMaster.MERGE_OP )
q = 'select id, createdbyid, createddate, body, type, relatedrecordid, contentfilename, '+
'LinkUrl, Title, '+
'(select createdbyid from FeedLikes), '+
'(select feeditemid, commentbody, createdbyid, createddate from FeedComments) '+
'from CollaborationGroupFeed '+
'where parentid IN :groups and type <> \'TrackedChange\' '+
'order by id asc';
return Database.getQueryLocator(q);
}
global void execute(Database.BatchableContext BC, List<sObject> scope){
ID[] userIDs = new ID[] {};
if( scope == null || scope.isEmpty() )
return;
// If we're copying members, extract the userids to add
if( opType == groupMaster.MEMBER_OP ) {
CollaborationGroupMember[] toAdd = new CollaborationGroupMember[] {};
for( sObject g : scope )
userIDs.add( ID.valueof( string.valueof( g.get( ( hasGroups ? 'memberid' : 'id' ) ) ) ) );
if( userIDs.isEmpty() )
return;
// Which of these users are already in the destination group?
CollaborationGroupMember[] cgs = [select memberid
from CollaborationGroupMember
where collaborationgroupid = :destID
and memberid IN :userIDs];
Set<ID> curMembers = new Set<ID> ();
for( CollaborationGroupMember cg : cgs )
curMembers.add( cg.memberid );
for( ID uID : userIDs )
if( !curMembers.contains( uID ) )
toAdd.add( new CollaborationGroupMember( collaborationgroupid = destID, memberid = uID ) );
try {
insert toAdd;
} catch( Exception e ) {
failed = true;
}
} else if( opType == groupMaster.MERGE_OP || opType == groupMaster.COPY_OP ) {
// We're copying posts! Do the parent posts first
FeedItem[] newPosts = new FeedItem[] {};
FeedComment[] newComments = new FeedComment[] {};
FeedLike[] newLikes = new FeedLike[] {};
List<FeedComment[]> commentArray = new List<FeedComment[]> ();
List<FeedLike[]> likeArray = new List<FeedLike[]> ();
// We cannot insert posts by users who are no longer active
ID[] postCreators = new ID[] {};
for( sObject s : scope ) {
postCreators.add( (string) s.get('createdbyid') );
if( s.getSObjects('feedlikes') != null )
for( FeedLike fl : s.getSObjects('feedlikes') )
postCreators.add( fl.createdbyid );
if( s.getSObjects('feedcomments') != null )
for( FeedComment fc : s.getSObjects('feedcomments') )
postCreators.add( fc.createdbyid );
}
Map<ID, User> activeCreators = new Map<ID, User> ([select id from User
where isactive=true and ( usertype = 'Standard' or usertype = 'CSNOnly' )
and id IN :postCreators]);
for( sObject s : scope ) {
if( !activeCreators.keySet().contains( (string) s.get('createdbyid') ) )
continue;
FeedItem newPost = new FeedItem();
newPost.body = (string) s.get('body');
newPost.type = (string) s.get('type');
newPost.relatedRecordId = (ID) s.get('relatedrecordid');
String urlLink = (string) s.get('linkurl');
if(urlLink != null && urlLink.contains(' '))
urlLink = urlLink.replace(' ','%20');
newPost.linkURL = urlLink;
newPost.title = (string) s.get('title');
newPost.createddate = (DateTime) s.get('createddate');
newPost.createdbyid = (ID) s.get('createdbyid');
newPost.parentid = destID;
if( newPost.type == 'ContentPost' && newPost.relatedRecordID == null )
newPost.type = 'TextPost';
if( newPost.body == null ) {
if( newPost.type == 'LinkPost' )
newPost.body = 'posted a link.';
else
newPost.body = 'posted a file.';
}
newPosts.add( newPost );
FeedLike[] likes = new FeedLike[] {};
FeedComment[] comments = new FeedComment[] {};
if( s.getSObjects('feedlikes') != null ) {
for( FeedLike fl : s.getSObjects('feedlikes') ) {
if( !activeCreators.keySet().contains( fl.createdbyid ) )
continue;
FeedLike newFL = new FeedLike();
newFL.createdbyid = fl.createdbyid;
likes.add( newFL );
}
}
likeArray.add( likes );
if( s.getSObjects('feedcomments') != null ) {
for( FeedComment fc : s.getSObjects('feedcomments') ) {
if( !activeCreators.keySet().contains( fc.createdbyid ) )
continue;
FeedComment newFC = new FeedComment();
newFC.createdbyid = fc.createdbyid;
newFC.commentbody = fc.commentbody;
newFC.createddate = fc.createddate;
comments.add( newFC );
}
}
commentArray.add( comments );
}
try {
insert newPosts;
} catch( Exception e ) {
failed = true;
return;
}
ID[] fpIDs = new ID[] {};
// Move comments and likes to their new parents
for( integer x = 0; x < newPosts.size(); x++ ) {
for( FeedComment fc : commentArray.get( x ) ) {
fc.feeditemid = newPosts.get(x).id;
newComments.add( fc );
}
for( FeedLike fl : likeArray.get( x ) ) {
fl.feeditemid = newPosts.get(x).id;
newLikes.add( fl );
}
}
try {
insert newComments;
/*added by amol to cover cath part to increase test coverage */
if(Test.isRunningTest()){
integer num = 1/0; //divide by zero exception.
}
} catch( Exception e ) {
failed = true;
}
try {
insert newLikes;
/*added by amol to cover cath part to increase test coverage */
if(Test.isRunningTest()){
integer num = 1/0; //divide by zero exception.
}
} catch( Exception e ) {
failed = true;
}
}
}
global void finish(Database.BatchableContext BC) {
if( opType == groupMaster.MERGE_OP && deleteSources ) {
CollaborationGroup[] cgs = [select id
from CollaborationGroup
where id IN :sourceIDs];
try {
delete cgs;
} catch( Exception e ) {
ApexPages.addMessages( e );
}
}
}
}
Test Class-
@isTest
public class groupMasterBatch_Test {
public string operation { get; set; }
public static string MERGE_OP { get { return 'merge'; } }
public static string COPY_OP { get { return 'copy'; } }
public static string MEMBER_OP { get { return 'members'; } }
public static testmethod void runTest() {
//string rolePref='00E';
string opType='test';
groupMaster.MEMBER_OP ='test';
Set<ID> sourceIDs =new Set<ID>();
sourceIDs.add('00E60000001BbDC');
system.debug('sourceIDs'+sourceIDs);
groupMaster gm = new groupMaster();
gm.isTest = true;
// gm.getRoles();
gm.getProfiles();
gm.getGroupList();
gm.clearGroups();
gm.getOperations();
gm.getGroupMasterIsCalculating();
gm.reset();
// Create some new groups
CollaborationGroup cg = new CollaborationGroup( name = 'groupMaster Test Group', collaborationtype = 'Public' );
CollaborationGroup cg2 = new CollaborationGroup( name = 'groupMaster Test Group 2', collaborationtype = 'Public' );
insert new CollaborationGroup[] { cg, cg2 };
FeedItem fp = new FeedItem( body = 'test post', parentid = cg.id, createdbyid = userinfo.getuserid() );
insert fp;
//CollaborationGroupFeed cgf = [select id from CollaborationGroupFeed
// where feeditemid = :fp.id];
FeedComment fc = new FeedComment( commentbody = 'test comment', feeditemid = fp.id );
insert fc;
// Merge
gm.operation = MERGE_OP;
gm.getBatchCount();
gm.sourceGroup = cg.id;
gm.destGroup = cg2.id;
gm.addGroup();
system.assertEquals( 1, gm.sourceGroups.size() );
gm.IDToRemove = cg.id;
gm.removeGroup();
system.assertEquals( 0, gm.sourceGroups.size() );
gm.addGroup();
Test.startTest();
gm.launchBatch();
Test.stopTest();
}
public static testmethod void runTest2() {
groupMaster gm = new groupMaster();
gm.isTest = true;
// Create some new groups
CollaborationGroup cg = new CollaborationGroup( name = 'groupMaster Test Group', collaborationtype = 'Public' );
CollaborationGroup cg2 = new CollaborationGroup( name = 'groupMaster Test Group 2', collaborationtype = 'Public' );
insert new CollaborationGroup[] { cg, cg2 };
FeedItem fp = new FeedItem( linkurl = 'http://tradeconnections.corporate.hsbc.com/en/News-and-Opinion/~/media/TradeConnections/Files/Reports-PDF/Global%20Report.ashx', title = 'google', parentid = cg.id, createdbyid = userinfo.getuserid() );
insert fp;
//CollaborationGroupFeed cgf = [select id from CollaborationGroupFeed
// where feedpostid = :fp.id];
FeedComment fc = new FeedComment( commentbody = 'test comment', feeditemid = fp.id );
insert fc;
// Copy
gm.reset();
gm.copySource = cg.id;
gm.selectedSource();
system.assertNotEquals( null, gm.newName );
gm.operation = COPY_OP;
gm.getBatchCount();
Test.startTest();
gm.launchBatch();
Test.stopTest();
}
}
I just couldn't work without the flower braces, it's very confusing for me, so I just tidied up a bit and used Lists instead of strings which kinda helps in indentation.
Try to save the above code and confirm that am not missing anything, so that am not a wild goose chase.
I just have one error in saving this code. - Variable does not exist: groupMaster
Correct me if am wrong, that should be the class which I need in order to do the testing. :)