You need to sign in to do that
Don't have an account?
Surender
Urgent Test Class needed..
Urgently needed test class for the below class.
Apex:
public class ContactRelatedListController {
Account account;
ApexPages.StandardSetController controller;
Public Integer size{get;set;}
Public Integer noOfRecords{get; set;}
Public Integer overallListSize = 0;
Public String isAllowedToCreateContact{get;set;}
public string accountId {get;set;}
//public string selectedContactId {get;set;}
public String selectedContactId;
private User userAndCompanyInfo{get;set;}
public string userId {get;set;}
public ContactRelatedListController(ApexPages.StandardController controller) {
userAndCompanyInfo = SupportCommunityUtility.getCompanyInfo();
accountId = userAndCompanyInfo.AccountId;
// account = (Account)controller.getRecord();
string currentProfileId = userinfo.getProfileId();
System.debug('$$$ current user profile : '+currentProfileId);
if(currentProfileId.startsWith('00e70000001K2HI') || currentProfileId.startsWith('00e70000001JsCp') || currentProfileId.startsWith('00e70000000ox3X'))
{
isAllowedToCreateContact = 'block';
}
else
{
isAllowedToCreateContact = 'none';
}
}
/*public pageReference ShowAll() {
controller.setPageSize(controller.getResultSize());
return null;
} */
public List<CustomerContactListUtility> getActiveContactsList()
{
List<CustomerContactListUtility> contactsList = new List<CustomerContactListUtility>();
Integer atRatePosition;
string nickName;
string alias;
string enableUrl;
Map<Id,User> contactUserMap = new Map<Id,User>();
Set<id> contactIds = new Set<id>();
for(Contact contact : (list<Contact>)setCon.getRecords()){
contactIds.add(contact.Id);
}
for(User user : [select id,ContactId from user where ContactId =: contactIds]){
contactUserMap.put(user.ContactId,user);
}
for(Contact contact : (List<Contact>)setCon.getRecords()) {
CustomerContactListUtility contactList = new CustomerContactListUtility();
contactList.EditContactUrl = '/'+contact.Id;
contactList.Id = contact.Id;
contactlist.UserProfile = contact.User_Profile__c;
contactList.Region = contact.Region__c;
contactList.Email = contact.Email;
if(contactUserMap.get(contact.Id) != null){
contactList.ContactUser = 'Change Access';
// User currentUser = contactUserMap.get(contact.Id);
// contactList.ChangeAccessUrl = '/_ui/core/portal/PortalUserManagePage/e?id='+currentUser.Id+'&retURL=%2F'+accountId;
User currentUser = contactUserMap.get(contact.Id);
contactList.ChangeAccessUrl = '/_ui/core/portal/PortalUserManagePage/e?id='+currentUser.Id+'&retURL=/apex/ShowContactsPage?id='+accountId+'-'+currentUser.Id;
string userProfile = ApexPages.currentPage().getParameters().get('id');
system.debug('userProfile: '+userProfile);
if(userProfile.contains('-')){
string[] userId = userProfile.split('-');
if(userId.size() > 1){
User existingUser = contactUserMap.get(contact.Id);
system.debug('existingUser.Id '+existingUser.Id);
system.debug('userId[1] '+userId[1]);
if(userId[1] == existingUser.Id){
User user = [select id,profile.name from user where id =: userId[1]];
Contact cont = [select Name,Email,Phone,Region__c from contact where Id =: contact.Id];
contactList.UserProfile = user.Profile.Name;
contactList.Phone = cont.Phone;
contactList.Region = cont.Region__c;
contactList.Email = cont.Email;
contactList.Name = cont.Name;
system.debug('fetched user: '+cont.name+' '+cont.Phone+' '+cont.Region__c+' '+cont.Email);
}
else{
contactList.UserProfile = contact.User_Profile__c;
}
}
else{
contactList.UserProfile = contact.User_Profile__c;
}
system.debug('url id: '+userId);
}
if(contact.Phone != null){
EnableUrl += '&Phone='+contact.Phone;
contactList.Phone = string.valueOf(contact.Phone);
}
string sessionId = UserInfo.getSessionId();
contactList.RemoveUrl = '/_ui/core/portal/DisableCSPUserPage/e?save=1&id='+currentUser.Id+'&id='+contact.Id+'&cntc='+contact.Id+'&_CONFIRMATIONTOKEN='+sessionId;
// contactList.ChangeAccessUrl = 'openChangeAccess();';
}
else{
contactList.ContactUser = 'Enable';
if(contact.Email != null){
atRatePosition = contact.Email.indexOf('@');
nickName = contact.Email != null ? string.valueOf(contact.Email).substring(0,atRatePosition) : null;
}
else nickName = null;
EnableUrl = '/_ui/core/portal/PortalUserManagePage/e?ut=c&retURL=%2F'+accountId+'&retURL=%2F'+contact.Id+'&cntc='+contact.Id;
if(contact.FirstName != null){
alias = string.valueOf(contact.FirstName).substring(0,1) + string.valueOf(contact.Lastname).substring(0,3);
}
else{
alias = string.valueOf(contact.LastName).subString(0,4);
}
EnableUrl += '&Alias='+Alias+'&CommunityNickname='+nickName;
//alias = contact.FirstName != '' ? string.valueOf(contact.FirstName).substring(0,1) + string.valueOf(contact.Lastname).substring(0,3) : contact.LastName;
if(contact.Email != null){
EnableUrl += '&Email='+contact.Email+'&Username='+contact.Email;
contactList.Email = string.valueOf(contact.Email);
}
if(contact.Title != null){
EnableUrl += '&Title='+contact.Title;
contactList.Title = string.valueOf(contact.Title);
}
if(contact.Phone != null){
EnableUrl += '&Phone='+contact.Phone;
contactList.Phone = string.valueOf(contact.Phone);
}
if(contact.Fax != null){
EnableUrl += '&Fax='+contact.Fax;
contactList.Fax = string.valueOf(contact.Fax);
}
if(contact.MobilePhone != null){
EnableUrl += '&MobilePhone='+contact.MobilePhone;
contactList.MobilePhone = contact.MobilePhone;
}
if(contact.LastName != null){
EnableUrl += '&name_lastName='+contact.LastName;
contactList.MobilePhone = contact.LastName;
}
if(contact.MailingStreet != null){
EnableUrl += '&Addressstreet='+contact.MailingStreet;
contactList.MailingStreet = contact.MailingStreet;
}
if(contact.MailingCity != null){
EnableUrl += '&Addresscity='+contact.MailingCity;
contactList.MailingCity = contact.MailingCity;
}
if(contact.MailingState != null){
EnableUrl += '&Addressstate='+contact.MailingState;
contactList.MailingState = contact.MailingState;
}
if(contact.MailingPostalCode != null){
EnableUrl += '&Addresszip='+contact.MailingPostalCode;
contactList.MailingPostalCode = contact.MailingPostalCode;
}
if(contact.MailingCountry != null){
EnableUrl += '&Addresscountry='+contact.MailingCountry;
contactList.MailingCountry = contact.MailingCountry;
}
}
contactList.Name = contact.Name;
contactList.EditUrl = '/'+contact.Id+'/e?retURL=%2F'+accountId;
contactList.EnableUrl = EnableUrl;
contactsList.add(contactList);
}
system.debug('contactsList: '+contactsList);
overallListSize = contactsList.size();
return contactsList;
}
public ApexPages.StandardSetController setCon {
get{
if(setCon == null){
size = 5;
System.debug('@@@ accountId = '+accountId);
if(accountId == null)
{
return null;
}
string queryString = 'select Id,FirstName,LastName,Name,User_Profile__c,Support_Role__c,Region__c,Phone,Email,Preferred_Language__c, Fax, HomePhone, MailingAddress, MailingCity, MailingCountry, MailingPostalCode,MailingState, MailingStreet, MobilePhone, Title from Contact where accountId = :accountId and Inactive__c = false order by Name';
setCon = new ApexPages.StandardSetController(Database.getQueryLocator(queryString));
setCon.setPageSize(size);
noOfRecords = setCon.getResultSize();
}
return setCon;
}set;
}
public pageReference refresh() {
setCon = null;
getActiveContactsList();
setCon.setPageNumber(1);
return null;
}
public void setNextPageSize()
{
Integer currentPageSize = setCon.getPageSize();
setCon.setPageSize(currentPageSize + currentPageSize);
}
public void disableContactUser(){
setCon = null;
getActiveContactsList();
setCon.setPageNumber(1);
}
public String getselectedContactId(){
return selectedContactId;
}
public void setselectedContactId(String selectedContactId){
System.debug('ppsr'+selectedContactId);
this.selectedContactId = selectedContactId;
}
}
Test Class:
public class ContactRelatedListControllerTest{
static testMethod void tesCongtactsDisplay() {
Account TestAccount = new Account(Name = 'Initech', Type = Utility.SITE_ACCOUNT_TYPE, BillingCountry = 'USA');
insert TestAccount;
// Create two contacts with same account
Contact contact1 = new Contact(FirstName = 'TestFirstName1', LastName = 'TestLastName2', email = 'test1@jda.com.test', accountId = TestAccount.Id, MailingCountry = 'USA');
Contact contact2 = new Contact(FirstName = 'TestFirstName1', LastName = 'TestLastName2', email = 'test2@jda.com.test', accountId = TestAccount.Id, MailingCountry = 'USA');
List<Contact> contactList = new List<Contact>{contact1, contact2};
insert contactList;
//ApexPages.StandardSetController ob=new ApexPages.StandardSetController();
//ob.SetCon(TestAccount);
ApexPages.StandardSetController con = new ApexPages.StandardSetController(Database.getQueryLocator([select Id,Name from Account WHERE Id =: TestAccount.Id]));
ApexPages.StandardController ContactsList= new ApexPages.standardController(TestAccount);
ContactRelatedListController obj= new ContactRelatedListController(ContactsList);
obj.getActiveContactsList();
}
}
I wrote the above test class but it is failing with NullPointerException for SetCon property.
Can someone help me in writing test class in achieving 100% code coverage.
Feedback is appreciated.
Apex:
public class ContactRelatedListController {
Account account;
ApexPages.StandardSetController controller;
Public Integer size{get;set;}
Public Integer noOfRecords{get; set;}
Public Integer overallListSize = 0;
Public String isAllowedToCreateContact{get;set;}
public string accountId {get;set;}
//public string selectedContactId {get;set;}
public String selectedContactId;
private User userAndCompanyInfo{get;set;}
public string userId {get;set;}
public ContactRelatedListController(ApexPages.StandardController controller) {
userAndCompanyInfo = SupportCommunityUtility.getCompanyInfo();
accountId = userAndCompanyInfo.AccountId;
// account = (Account)controller.getRecord();
string currentProfileId = userinfo.getProfileId();
System.debug('$$$ current user profile : '+currentProfileId);
if(currentProfileId.startsWith('00e70000001K2HI') || currentProfileId.startsWith('00e70000001JsCp') || currentProfileId.startsWith('00e70000000ox3X'))
{
isAllowedToCreateContact = 'block';
}
else
{
isAllowedToCreateContact = 'none';
}
}
/*public pageReference ShowAll() {
controller.setPageSize(controller.getResultSize());
return null;
} */
public List<CustomerContactListUtility> getActiveContactsList()
{
List<CustomerContactListUtility> contactsList = new List<CustomerContactListUtility>();
Integer atRatePosition;
string nickName;
string alias;
string enableUrl;
Map<Id,User> contactUserMap = new Map<Id,User>();
Set<id> contactIds = new Set<id>();
for(Contact contact : (list<Contact>)setCon.getRecords()){
contactIds.add(contact.Id);
}
for(User user : [select id,ContactId from user where ContactId =: contactIds]){
contactUserMap.put(user.ContactId,user);
}
for(Contact contact : (List<Contact>)setCon.getRecords()) {
CustomerContactListUtility contactList = new CustomerContactListUtility();
contactList.EditContactUrl = '/'+contact.Id;
contactList.Id = contact.Id;
contactlist.UserProfile = contact.User_Profile__c;
contactList.Region = contact.Region__c;
contactList.Email = contact.Email;
if(contactUserMap.get(contact.Id) != null){
contactList.ContactUser = 'Change Access';
// User currentUser = contactUserMap.get(contact.Id);
// contactList.ChangeAccessUrl = '/_ui/core/portal/PortalUserManagePage/e?id='+currentUser.Id+'&retURL=%2F'+accountId;
User currentUser = contactUserMap.get(contact.Id);
contactList.ChangeAccessUrl = '/_ui/core/portal/PortalUserManagePage/e?id='+currentUser.Id+'&retURL=/apex/ShowContactsPage?id='+accountId+'-'+currentUser.Id;
string userProfile = ApexPages.currentPage().getParameters().get('id');
system.debug('userProfile: '+userProfile);
if(userProfile.contains('-')){
string[] userId = userProfile.split('-');
if(userId.size() > 1){
User existingUser = contactUserMap.get(contact.Id);
system.debug('existingUser.Id '+existingUser.Id);
system.debug('userId[1] '+userId[1]);
if(userId[1] == existingUser.Id){
User user = [select id,profile.name from user where id =: userId[1]];
Contact cont = [select Name,Email,Phone,Region__c from contact where Id =: contact.Id];
contactList.UserProfile = user.Profile.Name;
contactList.Phone = cont.Phone;
contactList.Region = cont.Region__c;
contactList.Email = cont.Email;
contactList.Name = cont.Name;
system.debug('fetched user: '+cont.name+' '+cont.Phone+' '+cont.Region__c+' '+cont.Email);
}
else{
contactList.UserProfile = contact.User_Profile__c;
}
}
else{
contactList.UserProfile = contact.User_Profile__c;
}
system.debug('url id: '+userId);
}
if(contact.Phone != null){
EnableUrl += '&Phone='+contact.Phone;
contactList.Phone = string.valueOf(contact.Phone);
}
string sessionId = UserInfo.getSessionId();
contactList.RemoveUrl = '/_ui/core/portal/DisableCSPUserPage/e?save=1&id='+currentUser.Id+'&id='+contact.Id+'&cntc='+contact.Id+'&_CONFIRMATIONTOKEN='+sessionId;
// contactList.ChangeAccessUrl = 'openChangeAccess();';
}
else{
contactList.ContactUser = 'Enable';
if(contact.Email != null){
atRatePosition = contact.Email.indexOf('@');
nickName = contact.Email != null ? string.valueOf(contact.Email).substring(0,atRatePosition) : null;
}
else nickName = null;
EnableUrl = '/_ui/core/portal/PortalUserManagePage/e?ut=c&retURL=%2F'+accountId+'&retURL=%2F'+contact.Id+'&cntc='+contact.Id;
if(contact.FirstName != null){
alias = string.valueOf(contact.FirstName).substring(0,1) + string.valueOf(contact.Lastname).substring(0,3);
}
else{
alias = string.valueOf(contact.LastName).subString(0,4);
}
EnableUrl += '&Alias='+Alias+'&CommunityNickname='+nickName;
//alias = contact.FirstName != '' ? string.valueOf(contact.FirstName).substring(0,1) + string.valueOf(contact.Lastname).substring(0,3) : contact.LastName;
if(contact.Email != null){
EnableUrl += '&Email='+contact.Email+'&Username='+contact.Email;
contactList.Email = string.valueOf(contact.Email);
}
if(contact.Title != null){
EnableUrl += '&Title='+contact.Title;
contactList.Title = string.valueOf(contact.Title);
}
if(contact.Phone != null){
EnableUrl += '&Phone='+contact.Phone;
contactList.Phone = string.valueOf(contact.Phone);
}
if(contact.Fax != null){
EnableUrl += '&Fax='+contact.Fax;
contactList.Fax = string.valueOf(contact.Fax);
}
if(contact.MobilePhone != null){
EnableUrl += '&MobilePhone='+contact.MobilePhone;
contactList.MobilePhone = contact.MobilePhone;
}
if(contact.LastName != null){
EnableUrl += '&name_lastName='+contact.LastName;
contactList.MobilePhone = contact.LastName;
}
if(contact.MailingStreet != null){
EnableUrl += '&Addressstreet='+contact.MailingStreet;
contactList.MailingStreet = contact.MailingStreet;
}
if(contact.MailingCity != null){
EnableUrl += '&Addresscity='+contact.MailingCity;
contactList.MailingCity = contact.MailingCity;
}
if(contact.MailingState != null){
EnableUrl += '&Addressstate='+contact.MailingState;
contactList.MailingState = contact.MailingState;
}
if(contact.MailingPostalCode != null){
EnableUrl += '&Addresszip='+contact.MailingPostalCode;
contactList.MailingPostalCode = contact.MailingPostalCode;
}
if(contact.MailingCountry != null){
EnableUrl += '&Addresscountry='+contact.MailingCountry;
contactList.MailingCountry = contact.MailingCountry;
}
}
contactList.Name = contact.Name;
contactList.EditUrl = '/'+contact.Id+'/e?retURL=%2F'+accountId;
contactList.EnableUrl = EnableUrl;
contactsList.add(contactList);
}
system.debug('contactsList: '+contactsList);
overallListSize = contactsList.size();
return contactsList;
}
public ApexPages.StandardSetController setCon {
get{
if(setCon == null){
size = 5;
System.debug('@@@ accountId = '+accountId);
if(accountId == null)
{
return null;
}
string queryString = 'select Id,FirstName,LastName,Name,User_Profile__c,Support_Role__c,Region__c,Phone,Email,Preferred_Language__c, Fax, HomePhone, MailingAddress, MailingCity, MailingCountry, MailingPostalCode,MailingState, MailingStreet, MobilePhone, Title from Contact where accountId = :accountId and Inactive__c = false order by Name';
setCon = new ApexPages.StandardSetController(Database.getQueryLocator(queryString));
setCon.setPageSize(size);
noOfRecords = setCon.getResultSize();
}
return setCon;
}set;
}
public pageReference refresh() {
setCon = null;
getActiveContactsList();
setCon.setPageNumber(1);
return null;
}
public void setNextPageSize()
{
Integer currentPageSize = setCon.getPageSize();
setCon.setPageSize(currentPageSize + currentPageSize);
}
public void disableContactUser(){
setCon = null;
getActiveContactsList();
setCon.setPageNumber(1);
}
public String getselectedContactId(){
return selectedContactId;
}
public void setselectedContactId(String selectedContactId){
System.debug('ppsr'+selectedContactId);
this.selectedContactId = selectedContactId;
}
}
Test Class:
public class ContactRelatedListControllerTest{
static testMethod void tesCongtactsDisplay() {
Account TestAccount = new Account(Name = 'Initech', Type = Utility.SITE_ACCOUNT_TYPE, BillingCountry = 'USA');
insert TestAccount;
// Create two contacts with same account
Contact contact1 = new Contact(FirstName = 'TestFirstName1', LastName = 'TestLastName2', email = 'test1@jda.com.test', accountId = TestAccount.Id, MailingCountry = 'USA');
Contact contact2 = new Contact(FirstName = 'TestFirstName1', LastName = 'TestLastName2', email = 'test2@jda.com.test', accountId = TestAccount.Id, MailingCountry = 'USA');
List<Contact> contactList = new List<Contact>{contact1, contact2};
insert contactList;
//ApexPages.StandardSetController ob=new ApexPages.StandardSetController();
//ob.SetCon(TestAccount);
ApexPages.StandardSetController con = new ApexPages.StandardSetController(Database.getQueryLocator([select Id,Name from Account WHERE Id =: TestAccount.Id]));
ApexPages.StandardController ContactsList= new ApexPages.standardController(TestAccount);
ContactRelatedListController obj= new ContactRelatedListController(ContactsList);
obj.getActiveContactsList();
}
}
I wrote the above test class but it is failing with NullPointerException for SetCon property.
Can someone help me in writing test class in achieving 100% code coverage.
Feedback is appreciated.
Please find the guidelines:
https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_testing_best_practices.htm
Thanks,
Pratik