function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
thecrmninjathecrmninja 

Testing all branches of a Custom Controller with Ternary logic

I am building a custom controller.  My end goal is to have a method that reacts to data from the user pressing a button on a VF page to determine if they can get Leads and react accordingly.

The controller functions properly in my dev environment.  However, my testing methodology is not hitting every branch of the method and I'm a bit confused as to the reason.

 

Controller:

 

global without sharing class TomController{

public String getResult(){
return res;
}
private String res=null;

//Here we set variables utilized later to define user-specific search parameters
public Double MAX= [select TruePerDiem__c from User where ID = :userinfo.getuserid()][0].TruePerDiem__c;
public Integer Diem = Max.intvalue();
public String Queue = [select LeadPullQueues__c from User where ID = :userinfo.getuserid()][0].LeadPullQueues__c;
public Double NewMAX= [select PerDiem__c from User where ID = :userinfo.getuserid()][0].PerDiem__c;
public Integer NewDiem = newMax.intvalue();
Public Date LastPulled = [select Last_Pulled__c from User where ID = :userinfo.getuserid()][0].Last_Pulled__c;
public Double DaysSince= [select PulledXDaysAgo__c from User where ID = :userinfo.getuserid()][0].PulledXDaysAgo__c;
public Integer LastPull = DaysSince.intvalue();


//Everything below is the method that reacts to a button being pushed on the VF Page

public PageReference doSearch() {

// Once the button is pushed, This method dictates a path of action to take based on data of the user pressing the button
// In the first branch, A user has not pulled Leads today, so we use their Regular Per Diem since they are free to pull their full allotment of Leads
// The trigger updating the user record will also follow this logic to reset the number of Leads a User has pulled on THIS day
// The trigger will ALWAYS update a User's Last Pulled Date
if(LastPull > 0){
// We update the Lead records only when the owner id = User's Lead Pull Queue Value
List<Lead> leads = [select Id,ownerid,PulledYes__c from lead where ownerid = :Queue limit :NewDiem];
List<User> Users = [select ID, PulledToday__c from User where ID = :userinfo.getuserid() Limit 1];
ID u = [select ID from User where ID = :userinfo.getuserid()][0].Id;

for(Lead l:leads){
l.ownerid=u;
l.PulledYes__c='yes';
l.this_was_pulled__c=Date.Today();
}

update leads;

res = 'success';
return new PageReference('/00Q/o');
}

// In this second branch, A user has pulled Leads today, so we use their True Per Diem to make ensure they only get the Leads they should
// The trigger updating the user record will also follow this logic to add to the number of Leads a User has pulled on THIS day

else if(MAX > 0 && LastPull <= 0){
// We update the Lead records only when the owner id = User's Lead Pull Queue Value
List<Lead> leads = [select Id,ownerid,PulledYes__c from lead where ownerid = :Queue limit :Diem];
List<User> Users = [select ID, PulledToday__c from User where ID = :userinfo.getuserid() Limit 1];
ID u = [select ID from User where ID = :userinfo.getuserid()][0].Id;

for(Lead l:leads){
l.ownerid=u;
l.this_was_pulled__c=Date.Today();

}

update leads;

res = 'success';
return new PageReference('/00Q/o');
}

// In this third branch, A user has pulled Leads today and has none left to pull, so we inform them with a simple message
// The trigger updating the user record will do nothing since no Leads will be pulled

else if(MAX <= 0 && LastPull < 1){
system.debug('User reached Daily Lead Limit, must generate an error message...');
ApexPages.Message myMsg = new ApexPages.Message(ApexPages.severity.INFO, 'You have reached your Daily Lead Limit');
ApexPages.addMessage(myMsg);
return null;
}

// In this final branch, none of the criteria has been met and the user will be notified to contact the System Administrator
// The trigger updating the user record will do nothing since no Leads will be pulled

Else {
system.debug('General Error on Lead Puller Controller, must generate an error message...');
ApexPages.Message myMsg = new ApexPages.Message(ApexPages.severity.ERROR, 'The Get Leads function did not assign you Leads, Please contact your System Administrator');
ApexPages.addMessage(myMsg);
return null;

}

}

}

 Testing Method:

 

@isTest
private class TestTomCtrlr3{

static testmethod void TestTomCtrlr2a(){

//Prepare user for the test data
date dueDate = date.newInstance(2008, 1, 30);

String Quv = [select Queue.id from QueueSobject where Queue.name='A Test US Leads'][0].Queue.id;

Profile p = [select id from profile where name='Standard User'];
User Luse1 = new User(username='testa1@test.com', lastname='test', PerDiem__c = 4, PulledToday__c = 5,
DailyQuota__c = 5, email='testa1@test.com', alias='test', communitynickname='test',
LanguageLocaleKey = 'en_US', Localesidkey = 'en_US', TimeZoneSidKey = 'America/Los_Angeles',
emailencodingkey = 'ISO-8859-1', profileid = p.id, Last_Pulled__c = DueDate, LeadPullQueues__c = Quv);
insert Luse1;

QueueSobject Qu = [select Queue.id from QueueSobject where Queue.name='A Test US Leads'];


// Seed the database with some leads, and make sure they can
// be bulk inserted successfully.

Lead lead1 = new Lead(LastName='Test1', Company='Test1 Inc.', Email='test1@duptest.com', PulledYes__c = '', OwnerId=Qu.Queue.ID);
Lead lead2 = new Lead(LastName='Test2', Company='Test2 Inc.', Email='test4@duptest.com', PulledYes__c = '', OwnerId=Qu.Queue.ID);
Lead lead3 = new Lead(LastName='Test3', Company='Test3 Inc.', Email='test5@duptest.com', PulledYes__c = '', OwnerId=Qu.Queue.ID);
Lead[] leads = new Lead[] {lead1, lead2, lead3};
insert leads;

PageReference pageRef = Page.leadgetter;
Test.setCurrentPageReference(pageRef);
ApexPages.currentPage().getParameters().put('id',Luse1.id);


TomController controller = new TomController();

controller.doSearch();
Integer t = [Select count() from Lead where ownerid = :Luse1.id];

system.AssertEquals(3, t);

}

static testmethod void TestTomCtrlr2b(){

//Prepare Queue for User
// tried this but did not work Queue Qd = New Queue(Name='TmpQueue', Type='Queue');

String Quv = [select Queue.id from QueueSobject where Queue.name='A Test US Leads'][0].Queue.id;

//Prepare user for the test data

Profile p = [select id from profile where name='Standard User'];
User Luse2 = new User(username='test2a@test.com', lastname='test', PerDiem__c = 4, PulledToday__c = 0,
DailyQuota__c = 5, email='tes2t@test.com', alias='test', communitynickname='test',
LanguageLocaleKey = 'en_US', Localesidkey = 'en_US', TimeZoneSidKey = 'America/Los_Angeles',
emailencodingkey = 'ISO-8859-1', profileid = p.id, Last_Pulled__c = Date.Today(), LeadPullQueues__c = Quv);
insert Luse2;
QueueSobject Qu = [select Queue.id from QueueSobject where Queue.name='A Test US Leads'];


// Seed the database with some leads, and make sure they can
// be bulk inserted successfully.

Lead lead1 = new Lead(LastName='Test1', Company='Test1 Inc.', Email='test1@duptest.com', PulledYes__c = '', OwnerId=Qu.Queue.ID);
Lead lead2 = new Lead(LastName='Test2', Company='Test2 Inc.', Email='test4@duptest.com', PulledYes__c = '', OwnerId=Qu.Queue.ID);
Lead lead3 = new Lead(LastName='Test3', Company='Test3 Inc.', Email='test5@duptest.com', PulledYes__c = '', OwnerId=Qu.Queue.ID);
Lead[] leads = new Lead[] {lead1, lead2, lead3};
insert leads;

PageReference pageRef = Page.leadgetter;
Test.setCurrentPageReference(pageRef);
ApexPages.currentPage().getParameters().put('id',Luse2.id);


TomController controller = new TomController();

controller.doSearch();
Integer t = [Select count() from Lead where id in :ownerid = :Luse2.id];


system.AssertEquals(3, t);

}

static testmethod void TestTomCtrlr2ErrorA(){

//Create Flag for Message Check

Boolean errorFound = false;

String Quv = [select Queue.id from QueueSobject where Queue.name='A Test US Leads'][0].Queue.id;

//Prepare user for the test data
//This user will trigger the third branch by having met the daily pull quota and having a "pull date" of today

Profile p = [select id from profile where name='Standard User'];
User Muse = new User(username='testamsg@test.com', lastname='test', PerDiem__c = 0, PulledToday__c = -1,
DailyQuota__c = 0, email='testamsg@test.com', alias='test', communitynickname='test',
LanguageLocaleKey = 'en_US', Localesidkey = 'en_US', TimeZoneSidKey = 'America/Los_Angeles',
emailencodingkey = 'ISO-8859-1', profileid = p.id, Last_Pulled__c = (Date.Today()-1), LeadPullQueues__c = Quv);
insert Muse;

//Trigger Controller by calling VF page, Button Method both in context of above user

PageReference pageRef = Page.leadgetter;
Test.setCurrentPageReference(pageRef);

ApexPages.currentPage().getParameters().put('id',Muse.id);

TomController controller = new TomController();

controller.doSearch();

//Controller's been instantiated, now check to make sure the message from third branch displays

/*
String error = controller.getError();
System.AssertEquals(error,'Link or ID must be at least 15 characters long.');
*/

//This is an approach that seems not to be working

ApexPages.Message[] mssgs = ApexPages.getMessages();
for(ApexPages.Message m : mssgs){
if(m.getDetail()=='You have reached your Daily Lead Limit')
errorFound=true;
}
System.assert(errorFound==false);

}

}

 I'm still playing with ideas here but it would be a big help if anybody with more experience could point out any glaring issues/ommissions.  

 

Basically, I'm able to cover the first DML transaction and the  setting the variables.  For some reason, calling the second and third branches is not running the "Do Search" method and moving the Leads or generating the message.

 

 

 

Best Answer chosen by Admin (Salesforce Developers) 
thecrmninjathecrmninja

I resolved this issue.  The key was the inclusion of the following call at each branch

 

 

System.RunAs(UserName)

 

 Including this call within each test method allowed me to callout against my custom controller and instantiate each branch of my Controller's logic. 

Here is my revised test class for reference

 

 

@isTest private class TestTomCtrlrB{ static testmethod void TestTomCtrlr2a(){ //Prepare user for the test data date dueDate = date.newInstance(2008, 1, 30); Profile p = [select id from profile where name='Standard User']; User Luse2 = new User(username='testa1@test.com', lastname='test', PerDiem__c = 4, PulledToday__c = 5, DailyQuota__c = 5, email='testa1@test.com', alias='test', communitynickname='test', LanguageLocaleKey = 'en_US', Localesidkey = 'en_US', TimeZoneSidKey = 'America/Los_Angeles', emailencodingkey = 'ISO-8859-1', profileid = p.id, Last_Pulled__c = dueDate, Open_Leads_Owned__c = 3); insert Luse2; QueueSobject Qu = [select Queue.id from QueueSobject where Queue.name='A Test US Leads']; // Seed the database with some leads, and make sure they can // be bulk inserted successfully. Lead lead4 = new Lead(LastName='Test1', Company='Test1 Inc.', Email='test1@duptest.com', PulledYes__c = '', OwnerId=Qu.Queue.ID); Lead lead5 = new Lead(LastName='Test2', Company='Test2 Inc.', Email='test4@duptest.com', PulledYes__c = '', OwnerId=Qu.Queue.ID); Lead lead6 = new Lead(LastName='Test3', Company='Test3 Inc.', Email='test5@duptest.com', PulledYes__c = '', OwnerId=Qu.Queue.ID); Lead[] leadsMore = new Lead[] {lead4, lead5, lead6}; insert leadsMore; System.RunAs(Luse2){ PageReference pageRef = Page.leadgetter; Test.setCurrentPageReference(pageRef); ApexPages.currentPage().getParameters().put('id',Luse2.id); TomController controller = new TomController(); controller.doSearch(); Integer s = [Select count() from Lead where ownerid = :Luse2.id]; system.AssertEquals(0, s); } } static testmethod void TestTomCtrlr2b(){ String Quv = [select Queue.id from QueueSobject where Queue.name='A Test US Leads'][0].Queue.id; //Prepare user for the test data Profile p = [select id from profile where name='Standard User']; User Luse2 = new User(username='test2a@test.com', lastname='test', PerDiem__c = 4, PulledToday__c = 0, DailyQuota__c = 5, email='tes2t@test.com', alias='test', communitynickname='test2', LanguageLocaleKey = 'en_US', Localesidkey = 'en_US', TimeZoneSidKey = 'America/Los_Angeles', emailencodingkey = 'ISO-8859-1', profileid = p.id, Open_Leads_Owned__c = 0, LeadPullQueues__c = Quv); insert Luse2; QueueSobject Qu = [select Queue.id from QueueSobject where Queue.name='A Test US Leads']; // Seed the database with some leads, and make sure they can // be bulk inserted successfully. Lead lead11 = new Lead(LastName='Test1', Company='Test1 Inc.', Email='test1@duptest.com', PulledYes__c = '', OwnerId=Qu.Queue.ID); Lead lead22 = new Lead(LastName='Test2', Company='Test2 Inc.', Email='test4@duptest.com', PulledYes__c = '', OwnerId=Qu.Queue.ID); Lead lead33 = new Lead(LastName='Test3', Company='Test3 Inc.', Email='test5@duptest.com', PulledYes__c = '', OwnerId=Qu.Queue.ID); Lead[] leads = new Lead[] {lead11, lead22, lead33}; insert leads; System.RunAs(Luse2){ Test.startTest(); PageReference pageRef = Page.leadgetter; Test.setCurrentPageReference(pageRef); ApexPages.currentPage().getParameters().put('id',Luse2.id); TomController controller = new TomController(); controller.doSearch(); Integer t = [Select count() from Lead where ownerid = :luse2.id]; //Integer t = [Select count() from Lead where PulledYes__c = 'yes']; system.AssertEquals(3, t); Test.stopTest(); } } static testmethod void TestTomCtrlr2ErrorA(){ //Create Flag for Message Check Boolean errorFound = false; String Quv = [select Queue.id from QueueSobject where Queue.name='A Test US Leads'][0].Queue.id; //Prepare user for the test data //This user will trigger the third branch by having met the daily pull quota and having a "pull date" of today Profile p = [select id from profile where name='Standard User']; User Muse = new User(username='testamsg@test.com', lastname='test', PerDiem__c = 0, PulledToday__c = -1, DailyQuota__c = 0, email='testamsg@test.com', alias='test', communitynickname='test2', LanguageLocaleKey = 'en_US', Localesidkey = 'en_US', TimeZoneSidKey = 'America/Los_Angeles', emailencodingkey = 'ISO-8859-1', profileid = p.id, Open_Leads_Owned__c = 0, LeadPullQueues__c = Quv); insert Muse; //Trigger Controller by calling VF page, Button Method both in context of above user System.RunAs(Muse){ PageReference pageRef = Page.leadgetter; Test.setCurrentPageReference(pageRef); ApexPages.currentPage().getParameters().put('id',Muse.id); TomController controller = new TomController(); controller.doSearch(); //Controller's been instantiated, now check to make sure the message from third branch displays ApexPages.Message[] mssgs = ApexPages.getMessages(); for(ApexPages.Message m : mssgs){ if(m.getDetail()=='You have reached your Daily Lead Limit') errorFound=true; } System.assert(errorFound==true); } } }

 

I did change the Custom Controller slightly so that it respect Open Leads Owned which is just a number field.  I will confirm that I can do this against a formula field and come back and post my results.  Just wanted to share this resolution separately for others with similar problems.

 

 

 

All Answers

JonPJonP

Your main question side, you should consolidate your multiple queries against the same User record into a single query returning all desired fields.  The code you have posted is highly inefficient and makes it likely you'll encounter governor limits as you add more functionality.

 

Also lines like this do nothing whatsoever:

 

ID u = [select ID from User where ID = :userinfo.getuserid()][0].Id;

 

Instead of declaring the variable u, just put :UserInfo.getUserId() inline into your code, eliminating an unneeded query and making your code more readable because you don't have to look up what u means.

 

Message Edited by JonP on 05-06-2009 01:17 PM
JonPJonP

As to your original question...  Can you explain the User.PulledXDaysAgo__c field?  How is it populated/calculated?  What are the possible values?

 

Your logic seems to care about the numeric value of LastPull, but your text description sounds like all you care about is whether or not the user has pulled leads today.  Perhaps replacing the integer class member with a boolean would draw this distinction more clearly in your code, and potentially reveal logical error(s) that are causing your tests to fail?

 

In other words, maybe your tests are correct but your code is wrong.  If you first focus on making sure the tests are right, you'll know you're done when all your tests pass.

thecrmninjathecrmninja

Thank you for your input on the code cleanliness.  I haven't been tidying up as I have been going along and am guilty as charged.

 

The LastPull value is derived from a formula field.  The field compares the last time the User pulled leads via the controller (this is updated via a trigger) against todays date.  If the user did not last pull Leads today, this formula pulls a value great than zero which should allow them to pull their full per diem of Leads.  If it is today, it finds their "True" per diem, which is derived by a formula field that calculates the lowest number from their Daily Per Diem or their Daily Total Allotment minus the Leads they have pulled.

I suppose I could just compare their last pull date versus today's date and generate a boolean.  I had something similar earlier and I was still encountering the same issue (the first Else If statement not being hit in my test).

JonPJonP

It sounds like your formula logic is pretty complex.  Maybe you should split it out into multiple fields, or even write test methods that confirm its output is correct for various test scenarios.  (Yes--test methods for a formula field!  Why not?)

 

Another way to look at the problem is, what information does your doSearch() method need?  Can you just provide the "max # of leads this user can pull right now"--if it's 0 then return an error, otherwise query for leads using that value as the limit.  This would allow you to consolidate your three cases into two.

 

As for "code cleanliness," everybody has a different style of coding.  Some people like to get the logic working, then refactor into a "cleaner" version, e.g. extract common logic into methods, etc.  In this situation, I would strongly recommend doing that refactoring now, to make your doSearch() method as lean as possible and to clearly indicate through well-named methods and parameters what each variable means.  By making your code more readable, with shorter methods, it will make it easier to understand and test each method in isolation, and therefore to find and fix bugs.

 

 

thecrmninjathecrmninja

I resolved this issue.  The key was the inclusion of the following call at each branch

 

 

System.RunAs(UserName)

 

 Including this call within each test method allowed me to callout against my custom controller and instantiate each branch of my Controller's logic. 

Here is my revised test class for reference

 

 

@isTest private class TestTomCtrlrB{ static testmethod void TestTomCtrlr2a(){ //Prepare user for the test data date dueDate = date.newInstance(2008, 1, 30); Profile p = [select id from profile where name='Standard User']; User Luse2 = new User(username='testa1@test.com', lastname='test', PerDiem__c = 4, PulledToday__c = 5, DailyQuota__c = 5, email='testa1@test.com', alias='test', communitynickname='test', LanguageLocaleKey = 'en_US', Localesidkey = 'en_US', TimeZoneSidKey = 'America/Los_Angeles', emailencodingkey = 'ISO-8859-1', profileid = p.id, Last_Pulled__c = dueDate, Open_Leads_Owned__c = 3); insert Luse2; QueueSobject Qu = [select Queue.id from QueueSobject where Queue.name='A Test US Leads']; // Seed the database with some leads, and make sure they can // be bulk inserted successfully. Lead lead4 = new Lead(LastName='Test1', Company='Test1 Inc.', Email='test1@duptest.com', PulledYes__c = '', OwnerId=Qu.Queue.ID); Lead lead5 = new Lead(LastName='Test2', Company='Test2 Inc.', Email='test4@duptest.com', PulledYes__c = '', OwnerId=Qu.Queue.ID); Lead lead6 = new Lead(LastName='Test3', Company='Test3 Inc.', Email='test5@duptest.com', PulledYes__c = '', OwnerId=Qu.Queue.ID); Lead[] leadsMore = new Lead[] {lead4, lead5, lead6}; insert leadsMore; System.RunAs(Luse2){ PageReference pageRef = Page.leadgetter; Test.setCurrentPageReference(pageRef); ApexPages.currentPage().getParameters().put('id',Luse2.id); TomController controller = new TomController(); controller.doSearch(); Integer s = [Select count() from Lead where ownerid = :Luse2.id]; system.AssertEquals(0, s); } } static testmethod void TestTomCtrlr2b(){ String Quv = [select Queue.id from QueueSobject where Queue.name='A Test US Leads'][0].Queue.id; //Prepare user for the test data Profile p = [select id from profile where name='Standard User']; User Luse2 = new User(username='test2a@test.com', lastname='test', PerDiem__c = 4, PulledToday__c = 0, DailyQuota__c = 5, email='tes2t@test.com', alias='test', communitynickname='test2', LanguageLocaleKey = 'en_US', Localesidkey = 'en_US', TimeZoneSidKey = 'America/Los_Angeles', emailencodingkey = 'ISO-8859-1', profileid = p.id, Open_Leads_Owned__c = 0, LeadPullQueues__c = Quv); insert Luse2; QueueSobject Qu = [select Queue.id from QueueSobject where Queue.name='A Test US Leads']; // Seed the database with some leads, and make sure they can // be bulk inserted successfully. Lead lead11 = new Lead(LastName='Test1', Company='Test1 Inc.', Email='test1@duptest.com', PulledYes__c = '', OwnerId=Qu.Queue.ID); Lead lead22 = new Lead(LastName='Test2', Company='Test2 Inc.', Email='test4@duptest.com', PulledYes__c = '', OwnerId=Qu.Queue.ID); Lead lead33 = new Lead(LastName='Test3', Company='Test3 Inc.', Email='test5@duptest.com', PulledYes__c = '', OwnerId=Qu.Queue.ID); Lead[] leads = new Lead[] {lead11, lead22, lead33}; insert leads; System.RunAs(Luse2){ Test.startTest(); PageReference pageRef = Page.leadgetter; Test.setCurrentPageReference(pageRef); ApexPages.currentPage().getParameters().put('id',Luse2.id); TomController controller = new TomController(); controller.doSearch(); Integer t = [Select count() from Lead where ownerid = :luse2.id]; //Integer t = [Select count() from Lead where PulledYes__c = 'yes']; system.AssertEquals(3, t); Test.stopTest(); } } static testmethod void TestTomCtrlr2ErrorA(){ //Create Flag for Message Check Boolean errorFound = false; String Quv = [select Queue.id from QueueSobject where Queue.name='A Test US Leads'][0].Queue.id; //Prepare user for the test data //This user will trigger the third branch by having met the daily pull quota and having a "pull date" of today Profile p = [select id from profile where name='Standard User']; User Muse = new User(username='testamsg@test.com', lastname='test', PerDiem__c = 0, PulledToday__c = -1, DailyQuota__c = 0, email='testamsg@test.com', alias='test', communitynickname='test2', LanguageLocaleKey = 'en_US', Localesidkey = 'en_US', TimeZoneSidKey = 'America/Los_Angeles', emailencodingkey = 'ISO-8859-1', profileid = p.id, Open_Leads_Owned__c = 0, LeadPullQueues__c = Quv); insert Muse; //Trigger Controller by calling VF page, Button Method both in context of above user System.RunAs(Muse){ PageReference pageRef = Page.leadgetter; Test.setCurrentPageReference(pageRef); ApexPages.currentPage().getParameters().put('id',Muse.id); TomController controller = new TomController(); controller.doSearch(); //Controller's been instantiated, now check to make sure the message from third branch displays ApexPages.Message[] mssgs = ApexPages.getMessages(); for(ApexPages.Message m : mssgs){ if(m.getDetail()=='You have reached your Daily Lead Limit') errorFound=true; } System.assert(errorFound==true); } } }

 

I did change the Custom Controller slightly so that it respect Open Leads Owned which is just a number field.  I will confirm that I can do this against a formula field and come back and post my results.  Just wanted to share this resolution separately for others with similar problems.

 

 

 

This was selected as the best answer
thecrmninjathecrmninja

JonP, I did have some quick questions about your points here.  They are as follows:

 

Do I need to be concerned with Governor Limits in Custom Controllers like I do in Triggers?  Or, are you referring to another limit issue? 

When it comes to this controller, I'm never really going to be going north of 20 leads being pulled, a value set by the "diem" variables.

 

A couple of the variable queries at the beginning of the controller are that way because I need to convert Double values into Integer values in order to limit the queries by them.  Would the "UserInfo" approach workaround that if placed within a query?  If not, are there more efficient methods that you are aware of that I should adopt?

 

Thanks again for your feedback.  I have only ever been a light coder in the past and I'm teaching myself when it comes to Apex, so I'll take any kind advice that I can get my hands on.  

JonPJonP

Governor limits do apply to controllers--all Apex code runs under governor management.

 

Regarding the initialization of your instance variables, it's far more efficient to define a class constructor and do this work there, using a single query against User that returns all the columns you need and then performs the calculations/conversions locally.  It would be a good exercise for you to look at all your queries in your controller to see which could be eliminated or merged.  (For instance, within doSearch() you could delete all of the queries against User.) 

 

By the way, I like your use of a merge variable in the LIMIT expression for your Lead queries.