I'm trying to refactor the following code.I'm trying to figure out how to refactor this code OR if anyone have a good way to do code coverage for this.
My issue is that, in dev, the code coverage is only the if-else statement for '%DEV%' and the rest are not covered in dev because its only getting the URL base url for the respective environment. In QA, the code coverage is only the if-else statement for '%QA%' and the rest are not covered. So on and so forth...
The custom labels contain the following values.
System.Label.DevSB-->dev
System.Label.QASB-->qa
System.Label.StageSB-->stage
System.Label.IntSB-->stage
Our Salesforce Sandbox Instances and URLs are as follow.
mycompany--dev20.my.salesforce.com --> dev
mycompany--qa20.my.salesforce.com --> qa
mycompany--stage.my.salesforce.com --> stage
mycompany--int.my.salesforce.com --> stage environment but for integration purposes
mycompany.my.salesforce.com --> prod
Essentially we have this method and if this method is executed in our dev environment (mycompany--dev20.my.salesforce.com) it should return '%Dev%', and if its executed in qa environment (mycompany--qa20.my.salesforce.com), then it should return '%QA%' so on and so forth....
public static String getSandboxName(){
if(URL.getSalesforceBaseUrl().getHost().containsIgnoreCase(System.Label.DevSB)){
return '%Dev%';
} else if(URL.getSalesforceBaseUrl().getHost().containsIgnoreCase(System.Label.QASB)){
return '%QA%';
} else if(URL.getSalesforceBaseUrl().getHost().containsIgnoreCase(System.Label.StageSB) || URL.getSalesforceBaseUrl().getHost().containsIgnoreCase(System.Label.IntSB)){
return '%Stage%';
} else {
return '%PROD%';
}
}
This is my test classes but unfortunately, it is not working.
static testMethod void testGetSandboxNameDEV(){
String actualSandboxName;
PageReference pageRef;
pageRef = new PageReference('https://mycompany--dev20.my.salesforce.com');
Test.setCurrentPage(pageRef);
actualSandboxName = MyClass.getSandboxName();
System.Debug('baseUrl-->' + URL.getSalesforceBaseUrl().getHost());
System.assertEquals('%Dev%', actualSandboxName);
}
static testMethod void testGetSandboxNameQA(){
String actualSandboxName;
PageReference pageRef;
pageRef = new PageReference('https://mycompany--qa20.my.salesforce.com');
pageRef.setRedirect(true);
Test.setCurrentPageReference(pageRef);
actualSandboxName = MyClass.getSandboxName();
System.Debug('baseUrl-->' + URL.getSalesforceBaseUrl().getHost());
System.assertEquals('%QA%', actualSandboxName);
}
static testMethod void testGetSandboxNameSTAGE(){
String actualSandboxName;
PageReference pageRef;
pageRef = new PageReference('https://mycompany--stage.my.salesforce.com');
pageRef.setRedirect(true);
Test.setCurrentPageReference(pageRef);
actualSandboxName = MyClass.getSandboxName();
System.Debug('baseUrl-->' + URL.getSalesforceBaseUrl().getHost());
System.assertEquals('%Stage', actualSandboxName);
}