You need to sign in to do that
Don't have an account?
IntegrationGuy
Need help in writing a test class for a URL rewriter class
global class PublicEmailPreferencesURLRewriter implements Site.UrlRewriter {
public static final String EMAIL_PROFILE_FRIENDLY = '/profile/';
public static final String EMAIL_PROFILE_VF_PAGE_BASE = '/PublicEmailPreferences';
public static final String EMAIL_PROFILE_VF_PAGE = EMAIL_PROFILE_VF_PAGE_BASE + '?' + PublicEmailPreferencesController.URL_PARM_ID + '=';
global PageReference[] generateUrlFor(PageReference[] urls) {
System.debug('generateUrlFor has been invoked for ' + urls);
PageReference[] pageRefs = new List<PageReference>();
Set<Id> cIds = new Set<Id>();
for (PageReference pRef : urls) {
String urlStr = pRef.getUrl();
if (urlStr.startsWith(EMAIL_PROFILE_VF_PAGE_BASE)) {
cIds.add(urlStr.substring(EMAIL_PROFILE_VF_PAGE.length()));
}
}
Map<Id, Contact> contactsById = new Map<Id, Contact>([SELECT Id, HashId__c FROM Contact WHERE Id IN :cIds]);
for (PageReference pRef : urls) {
String urlStr = pRef.getUrl();
pageRefs.add(
urlStr.startsWith(EMAIL_PROFILE_VF_PAGE_BASE) ?
new PageReference(EMAIL_PROFILE_FRIENDLY + contactsById.get(urlStr.substring(EMAIL_PROFILE_VF_PAGE.length())).HashId__c) :
pRef
);
}
return pageRefs;
}
global PageReference mapRequestUrl(PageReference url) {
PageReference pageRef = null;
String urlStr = url.getUrl();
System.debug('mapping url=' + urlStr);
if (urlStr.startsWith(EMAIL_PROFILE_FRIENDLY)) {
String hashId = urlStr.substring(EMAIL_PROFILE_FRIENDLY.length());
if (hashId != '') {
List<Contact> contacts = [SELECT Id FROM Contact WHERE HashId__c = :hashId];
if (!contacts.isEmpty()) {
pageRef = new PageReference(EMAIL_PROFILE_VF_PAGE + contacts[0].Id);
} else {
pageRef = new PageReference(EMAIL_PROFILE_VF_PAGE);
}
}
}
return pageRef;
}
}
public static final String EMAIL_PROFILE_FRIENDLY = '/profile/';
public static final String EMAIL_PROFILE_VF_PAGE_BASE = '/PublicEmailPreferences';
public static final String EMAIL_PROFILE_VF_PAGE = EMAIL_PROFILE_VF_PAGE_BASE + '?' + PublicEmailPreferencesController.URL_PARM_ID + '=';
global PageReference[] generateUrlFor(PageReference[] urls) {
System.debug('generateUrlFor has been invoked for ' + urls);
PageReference[] pageRefs = new List<PageReference>();
Set<Id> cIds = new Set<Id>();
for (PageReference pRef : urls) {
String urlStr = pRef.getUrl();
if (urlStr.startsWith(EMAIL_PROFILE_VF_PAGE_BASE)) {
cIds.add(urlStr.substring(EMAIL_PROFILE_VF_PAGE.length()));
}
}
Map<Id, Contact> contactsById = new Map<Id, Contact>([SELECT Id, HashId__c FROM Contact WHERE Id IN :cIds]);
for (PageReference pRef : urls) {
String urlStr = pRef.getUrl();
pageRefs.add(
urlStr.startsWith(EMAIL_PROFILE_VF_PAGE_BASE) ?
new PageReference(EMAIL_PROFILE_FRIENDLY + contactsById.get(urlStr.substring(EMAIL_PROFILE_VF_PAGE.length())).HashId__c) :
pRef
);
}
return pageRefs;
}
global PageReference mapRequestUrl(PageReference url) {
PageReference pageRef = null;
String urlStr = url.getUrl();
System.debug('mapping url=' + urlStr);
if (urlStr.startsWith(EMAIL_PROFILE_FRIENDLY)) {
String hashId = urlStr.substring(EMAIL_PROFILE_FRIENDLY.length());
if (hashId != '') {
List<Contact> contacts = [SELECT Id FROM Contact WHERE HashId__c = :hashId];
if (!contacts.isEmpty()) {
pageRef = new PageReference(EMAIL_PROFILE_VF_PAGE + contacts[0].Id);
} else {
pageRef = new PageReference(EMAIL_PROFILE_VF_PAGE);
}
}
}
return pageRef;
}
}
This test class worked like a charm :)
<pre>
@IsTest
public class PublicEmailPreferencesURLRewriter_T1{
@testSetup
static void setupTestData(){
test.startTest();
Contact contact_Obj = new Contact(LastName = 'LastName194', HasOptedOutOfEmail = false);
Insert contact_Obj;
test.stopTest();
}
static testMethod void test_generateUrlFor_UseCase1(){
List<Contact> contact_Obj = [SELECT LastName,HasOptedOutOfEmail from Contact];
System.assertEquals(true,contact_Obj.size()>0);
PublicEmailPreferencesURLRewriter obj01 = new PublicEmailPreferencesURLRewriter();
obj01.generateUrlFor(new List<PageReference>());
}
static testMethod void test_generateUrlFor_UseCase3(){
List<Contact> contact_Obj = [SELECT LastName,HasOptedOutOfEmail from Contact];
System.assertEquals(true,contact_Obj.size()>0);
PublicEmailPreferencesURLRewriter obj01 = new PublicEmailPreferencesURLRewriter();
contact_Obj[0].LastName='test';
contact_Obj[0].HasOptedOutOfEmail=false;
Update contact_Obj[0];
obj01.generateUrlFor(new List<PageReference>());
}
static testMethod void testURLrewriter()
{ // I try to create the internal saleforce pageReference<br><br>
pageReference myPage = new pageReference('/apex/myPage');
Test.setCurrentPage(myPage);
myPage.setRedirect(true);
List<PageReference> res = new List<PageReference>();
res.add(myPage);
res.add(new PageReference('/apex/myPage2' ));
// now call the methods<br><br>
PublicEmailPreferencesURLRewriter urlClass = new PublicEmailPreferencesURLRewriter ();
urlClass.mapRequestUrl(myPage);
urlClass.generateUrlFor(res);
}
static testMethod void test_mapRequestUrl_UseCase1(){
PageReference pageRef = Page.PublicEmailPreferences;
Test.setCurrentPage(pageRef);
List<Contact> contact_Obj = [SELECT LastName,HasOptedOutOfEmail, HashId__c from Contact];
System.assertEquals(true,contact_Obj.size()>0);
ApexPages.StandardSetController sc = new ApexPages.StandardSetController(contact_Obj);
PublicEmailPreferencesURLRewriter obj01 = new PublicEmailPreferencesURLRewriter();
contact_Obj[0].LastName='test';
contact_Obj[0].HasOptedOutOfEmail=false;
Update contact_Obj[0];
obj01.mapRequestUrl(new PageReference('/profile/generatesome random hash code'));
}
static testMethod void test_mapRequestUrl_UseCase2(){
List<Contact> contact_Obj = [SELECT LastName,HasOptedOutOfEmail from Contact];
System.assertEquals(true,contact_Obj.size()>0);
PublicEmailPreferencesURLRewriter obj01 = new PublicEmailPreferencesURLRewriter();
obj01.mapRequestUrl(new PageReference('https://fakesite.com'));
}
}
</pre>
All Answers
Not able to cover this part anyhow,
Map<Id, Contact> contactsById = new Map<Id, Contact>([SELECT Id, HashId__c FROM Contact WHERE Id IN :cIds]);
for (PageReference pRef : urls) {
String urlStr = pRef.getUrl();
pageRefs.add(
urlStr.startsWith(EMAIL_PROFILE_VF_PAGE_BASE) ?
new PageReference(EMAIL_PROFILE_FRIENDLY + contactsById.get(urlStr.substring(EMAIL_PROFILE_VF_PAGE.length())).HashId__c) :
pRef
);
}
return pageRefs;
@isTest
private class PublicEmailPreferencesURLRewriter_Test{
public static final String EMAIL_PROFILE_FRIENDLY = '/profile/';
public static final String EMAIL_PROFILE_VF_PAGE_BASE = '/PublicEmailPreferences';
public static final String EMAIL_PROFILE_VF_PAGE = EMAIL_PROFILE_VF_PAGE_BASE + '?' + PublicEmailPreferencesController.URL_PARM_ID + '=';
static string s;
@isTest
static void testGenerateUrlFor() {
PublicEmailPreferencesURLRewriter rewriter = new PublicEmailPreferencesURLRewriter();
System.assert(rewriter.generateUrlFor(null) == null);
}
@isTest
static void testMapUrl() {
Account objAccount = new Account();
objAccount.Name = 'Americas';
insert objAccount;
Contact testLead = new Contact();
testLead.FirstName = 'Test Contact First Name';
testLead.LastName = 'Last Name';
testLead.AccountId = objAccount.Id;
Id testId = Database.insert(testLead).id;
PublicEmailPreferencesURLRewriter rewriter = new PublicEmailPreferencesURLRewriter();
PageReference pageRef = Page.PublicEmailPreferences;
Test.setCurrentPage(pageRef);
pageRef.getParameters().put('id', testLead.id);
//String actualResult = rewriter.mapRequestUrl(new PageReference(EMAIL_PROFILE_FRIENDLY + contactsById.get(urlStr.substring(EMAIL_PROFILE_VF_PAGE.length())).HashId__c)) .getUrl();
String expectedResult = '/VIP?id=' + testId;
//System.debug(actualResult);
//System.assert(actualResult == expectedResult);
}
@isTest
static void testMapNonVipUrl() {
PublicEmailPreferencesURLRewriter rewriter = new PublicEmailPreferencesURLRewriter();
PageReference actualResult = rewriter.mapRequestUrl(new PageReference('/testUrl'));
String expectedResult = '/testUrl';
System.assert(actualResult == null);
}
}
</pre>
Site Name: EmailPref
VF: PublicEmailPreferences
This test class worked like a charm :)
<pre>
@IsTest
public class PublicEmailPreferencesURLRewriter_T1{
@testSetup
static void setupTestData(){
test.startTest();
Contact contact_Obj = new Contact(LastName = 'LastName194', HasOptedOutOfEmail = false);
Insert contact_Obj;
test.stopTest();
}
static testMethod void test_generateUrlFor_UseCase1(){
List<Contact> contact_Obj = [SELECT LastName,HasOptedOutOfEmail from Contact];
System.assertEquals(true,contact_Obj.size()>0);
PublicEmailPreferencesURLRewriter obj01 = new PublicEmailPreferencesURLRewriter();
obj01.generateUrlFor(new List<PageReference>());
}
static testMethod void test_generateUrlFor_UseCase3(){
List<Contact> contact_Obj = [SELECT LastName,HasOptedOutOfEmail from Contact];
System.assertEquals(true,contact_Obj.size()>0);
PublicEmailPreferencesURLRewriter obj01 = new PublicEmailPreferencesURLRewriter();
contact_Obj[0].LastName='test';
contact_Obj[0].HasOptedOutOfEmail=false;
Update contact_Obj[0];
obj01.generateUrlFor(new List<PageReference>());
}
static testMethod void testURLrewriter()
{ // I try to create the internal saleforce pageReference<br><br>
pageReference myPage = new pageReference('/apex/myPage');
Test.setCurrentPage(myPage);
myPage.setRedirect(true);
List<PageReference> res = new List<PageReference>();
res.add(myPage);
res.add(new PageReference('/apex/myPage2' ));
// now call the methods<br><br>
PublicEmailPreferencesURLRewriter urlClass = new PublicEmailPreferencesURLRewriter ();
urlClass.mapRequestUrl(myPage);
urlClass.generateUrlFor(res);
}
static testMethod void test_mapRequestUrl_UseCase1(){
PageReference pageRef = Page.PublicEmailPreferences;
Test.setCurrentPage(pageRef);
List<Contact> contact_Obj = [SELECT LastName,HasOptedOutOfEmail, HashId__c from Contact];
System.assertEquals(true,contact_Obj.size()>0);
ApexPages.StandardSetController sc = new ApexPages.StandardSetController(contact_Obj);
PublicEmailPreferencesURLRewriter obj01 = new PublicEmailPreferencesURLRewriter();
contact_Obj[0].LastName='test';
contact_Obj[0].HasOptedOutOfEmail=false;
Update contact_Obj[0];
obj01.mapRequestUrl(new PageReference('/profile/generatesome random hash code'));
}
static testMethod void test_mapRequestUrl_UseCase2(){
List<Contact> contact_Obj = [SELECT LastName,HasOptedOutOfEmail from Contact];
System.assertEquals(true,contact_Obj.size()>0);
PublicEmailPreferencesURLRewriter obj01 = new PublicEmailPreferencesURLRewriter();
obj01.mapRequestUrl(new PageReference('https://fakesite.com'));
}
}
</pre>