Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @istest
- public class TestRetrieveNextUtils {
- public static Group createTestGroup()
- {
- Group g = new Group(Type='Queue',Name='testRetrieveNextCase');
- insert g;
- //Make this queue assignable to leads and cases
- List<QueueSobject> qs = new List<QueueSobject>();
- qs.add(new QueueSobject(QueueId=g.Id,SObjectType='Case'));
- qs.add(new QueueSobject(QueueId=g.Id,SObjectType='Lead'));
- insert qs;
- return g;
- }
- public static User createTestUser() {
- User user = new User();
- user.Username = 'test'+System.currentTimeMillis()+'@RetrieveNextUtils.com';
- user.LastName = 'LastTestName';
- user.Email = 'test@RetrieveNextUtils.com';
- user.alias = 'testAl';
- user.TimeZoneSidKey = 'America/New_York';
- user.LocaleSidKey = 'en_US';
- user.EmailEncodingKey = 'ISO-8859-1';
- user.ProfileId = [select id from Profile where Name='System Administrator'].Id;
- user.LanguageLocaleKey = 'en_US';
- insert user;
- //setUser(user);
- return user;
- }
- public static testMethod void testRetrieveNextLead()
- {
- User u = createTestUser();
- Group g = createTestGroup();
- GroupMember gm = new GroupMember(UserOrGroupId=u.Id,GroupId=g.Id);
- insert gm;
- Test.startTest();
- //We have to runAs so that we don't get a MIXED_DML_EXCEPTION
- System.runAs(u) {
- Lead l = new Lead(LastName='Test',OwnerId=g.Id,Company='Test');
- insert l;
- Id leadId = retrieveNextLead(u.Id);
- System.assertEquals(leadId,l.Id);
- Lead ownedLead = [select OwnerId from Lead where Id=:l.Id];
- System.assertEquals(ownedLead.OwnerId,u.Id);
- }
- }
- public static testMethod void testNegativeRetrieveNextLead()
- {
- User u = createTestUser();
- Group g = createTestGroup();
- Test.startTest();
- //We have to runAs so that we don't get a MIXED_DML_EXCEPTION
- System.runAs(u) {
- //Do not insert this user in the queue -- he should not get the case
- Lead l = new Lead(LastName='Test',OwnerId=g.Id,Company='Test');
- insert l;
- Id leadId = retrieveNextLead(u.Id);
- System.assertEquals(leadId,null);
- Lead ownedLead = [select OwnerId from Lead where Id=:l.Id];
- System.assertNotEquals(ownedLead.OwnerId,u.Id);
- }
- }
- public static testMethod void testRetrieveNextCase()
- {
- User u = createTestUser();
- Group g = createTestGroup();
- GroupMember gm = new GroupMember(UserOrGroupId=u.Id,GroupId=g.Id);
- insert gm;
- Test.startTest();
- //We have to runAs so that we don't get a MIXED_DML_EXCEPTION
- System.runAs(u) {
- Case c = new Case(Subject='Test',OwnerId=g.Id);
- insert c;
- Id caseId = retrieveNextCase(u.Id);
- System.assertEquals(caseId,c.Id);
- Case ownedCase = [select OwnerId from Case where Id=:c.Id];
- System.assertEquals(ownedCase.OwnerId,u.Id);
- }
- }
- public static testMethod void testNegativeRetrieveNextCase()
- {
- User u = createTestUser();
- Group g = createTestGroup();
- Test.startTest();
- //We have to runAs so that we don't get a MIXED_DML_EXCEPTION
- System.runAs(u) {
- //Do not insert this user in the queue -- he should not get the case
- Case c = new Case(Subject='Test',OwnerId=g.Id);
- insert c;
- Id caseId = retrieveNextCase(u.Id);
- System.assertEquals(caseId,null);
- Case ownedCase = [select OwnerId from Case where Id=:c.Id];
- System.assertNotEquals(ownedCase.OwnerId,u.Id);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement