So, I have a class that is called by a URL and based on the information provided in the parameters, it runs it through a query and returns results as XML in a VisualForce page.
Here's the Class:
public without sharing class Five9Query {
public string cid { get;set; }
public string phone { get;set; }
public boolean accAccess { get;set; }
public boolean access { get;set; }
public string priority { get;set; }
public string id { get;set; }
public string pin { get;set; }
public string tempCid { get;set; }
public Integer errorCode { get;set; }
private string secPin = '11111111';
public Five9Query() {
string pin = ApexPages.currentPage().getParameters().get('pin');
if (pin == secPin){
system.debug('Pin: ' + pin);
string tempCid = ApexPages.currentPage().getParameters().get('cid');
system.debug('tempCID: ' +tempCid);
Integer cidLength = tempCid.length();
if (cidLength == 10){
cid = '(' + tempCid.substring(0,3) + ') ' + tempCid.substring(3,6) + '-' + tempCid.substring(6,10);
system.debug('cid: ' +cid);
}
else if (cidLength > 10){
cid = tempCid;
}
List<Contact> conMobile = [SELECT id, MobilePhone, Dealer_Portal_Account_Created__c, Account.PortalLoginCreatedEmailed__c , account.discount_tier__c FROM Contact WHERE MobilePhone = :cid limit 1];
List<Contact> conPhone = [SELECT id, Phone,Dealer_Portal_Account_Created__c, account.portalLoginCreatedEmailed__c, account.discount_tier__c FROM Contact WHERE Phone = :cid limit 1];
List<Account> accPhone = [SELECT id, Phone, portalLoginCreatedEmailed__c, discount_tier__c FROM Account WHERE Phone = :cid limit 1];
List<Account> accFax = [SELECT id, Fax, portalLoginCreatedEmailed__c, discount_tier__c FROM Account WHERE Fax = :cid limit 1];
List<Contact> conFax = [SELECT id, Fax,Dealer_Portal_Account_Created__c, account.portalLoginCreatedEmailed__c, account.discount_tier__c FROM Contact WHERE Fax = :cid limit 1];
List<Account> accBillingPhone = [SELECT id, Billing_Contact_Phone__c, portalLoginCreatedEmailed__c, discount_tier__c FROM Account WHERE Billing_Contact_Phone__c = :cid limit 1];
//Checking Contact Mobile Phone
if (!conMobile.isEmpty()){
if(conMobile[0].account.portalLoginCreatedEmailed__c == true){
errorCode = 0;
id = conMobile[0].Id;
phone = conMobile[0].MobilePhone;
access = conMobile[0].Dealer_Portal_Account_Created__c;
priority = conMobile[0].account.discount_tier__c;
}
else{
errorCode = 3;
id = conMobile[0].Id;
phone = conMobile[0].MobilePhone;
access = conMobile[0].account.portalLoginCreatedEmailed__c ;
priority = conMobile[0].account.discount_tier__c;
}
}
//Checking Contact Phone
else if (!conPhone.isEmpty()){
if(conPhone[0].account.portalLoginCreatedEmailed__c == true){
errorCode = 0;
id = conPhone[0].Id;
phone = conPhone[0].Phone;
access = conPhone[0].Dealer_Portal_Account_Created__c;
priority = conPhone[0].account.discount_tier__c;
}
else{
errorCode = 3;
id = conPhone[0].Id;
phone = conPhone[0].Phone;
access = conPhone[0].account.portalLoginCreatedEmailed__c;
priority = conPhone[0].account.discount_tier__c;
}
}
//Checking Account Phone
else if (!accPhone.isEmpty()){
errorCode = 0;
id = accPhone[0].Id;
phone = accPhone[0].Phone;
access = accPhone[0].portalLoginCreatedEmailed__c;
priority = accPhone[0].discount_tier__c;
}
//Checking Account Billing Phone
else if (!accBillingPhone.isEmpty()){
errorCode = 0;
id = accBillingPhone[0].Id;
phone = accBillingPhone[0].Billing_Contact_Phone__c;
access = accBillingPhone[0].portalLoginCreatedEmailed__c;
priority = accBillingPhone[0].discount_tier__c;
}
//Checking Account Fax
else if (!accFax.isEmpty()){
errorCode = 0;
id = accFax[0].Id;
phone = accFax[0].Fax;
access = accFax[0].portalLoginCreatedEmailed__c;
priority = accFax[0].discount_tier__c;
}
//Checking Contact Fax
else if (!conFax.isEmpty()){
if(conFax[0].account.portalLoginCreatedEmailed__c == true){
errorCode = 0;
id = conFax[0].Id;
phone = conFax[0].Fax;
access = conFax[0].Dealer_Portal_Account_Created__c;
priority = conFax[0].account.discount_tier__c;
}
else{
errorCode = 3;
id = conFax[0].Id;
phone = conFax[0].Fax;
access = conFax[0].account.portalLoginCreatedEmailed__c;
priority = conFax[0].account.discount_tier__c;
}
}
else{
System.debug('No Match');
errorCode = 1;
}
}
else {
system.debug('invalid pin');
errorCode = 2;
}
}
}
Here's the VF page:
<apex:page Controller="Five9Query" contentType="text/xml" showHeader="false" sidebar="false" cache="false">
<?xml version="1.0" encoding="UTF-8" ?>
<response>
<id>{!id}</id>
<access>{!access}</access>
<priority>{!priority}</priority>
<errorcode>{!errorCode}</errorcode>
</response>
</apex:page>
What I need to do is write a test class that: 1. Creates an account - done - verifies creation - done 2. Creates a contact - done - verifies creation - done 3. Calls a URL with a variable from the account and/or contact 4. Parses the XML results to ensure results match (or don't)
1 and 2 are done, 3 and 4 are where I am stuck. What do I need to do to get 3 and 4 working?
Attribution to: MoreThanWYSIWYG
Possible Suggestion/Solution #1
First you need to create a PageReference
in your test class, then use getParemeters().put(...)
to add the parameters you need for your test. Once you've done that you can then use Test.setCurrentPage(...)
to set the page for your test.
After all of these steps are complete you should be able to create an instance of your class and get the results you're after.
The PageReference
class is covered in the documentation here. It's also worth looking at the 'Testing Custom Controllers and Controller Extensions' part of the documentation here
Attribution to: Alex Tennant
Possible Suggestion/Solution #2
Check the URL page documentation in the Apex documentation. You can parse all sorts of stuff from the URL. http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_methods_system_url.htm#apex_methods_system_url You may need the getQuery() method, and then to parse that - I'm not 100% sure of what, exactly, you're asking for once you get the URL. So the procedure would be PageReference.getURL(), then create a URL class from that URL string, and then work with that.
Attribution to: DavidSchach
Possible Suggestion/Solution #3
In addition to what Alex said, you'll probably want to get the 'page' so call getContents() on the pageReference. This will return a blob then use toString() on the blob. This way you can parse the page generated and compare it to what you want.
You may not need to call Test.setCurrentPage(...) if you use the code below, but I haven't tested the test code...
ie.
PageReference vfPage = new PageReference('/apex/' + visualforcePageName);
vfPage.getParameters().put('id',client.Id);
string generatedXML = vfPage.getContent().getBlob().toString();
system.assert(generatedXML.equals(expectedXML));
Attribution to: Stuart McVicar
This content is remixed from stackoverflow or stackexchange. Please visit https://salesforce.stackexchange.com/questions/31289