You need to sign in to do that
Don't have an account?
Atul Singh 87
how to increase code coverage for the below code?
@AuraEnabled
public static String getCanonicalLink(String articleId) {
String urlName = null ;
try{
if(String.isNotBlank(articleId)) {
List<Knowledge__kav> lstCurrentArticle = [SELECT id , Custom_Canonical_URL__c ,UrlName , Page_Index__c,
Custom_Canonical_URL__r.UrlName
FROM Knowledge__kav WHERE Id =: articleId
LIMIT 1];
String domainURL = Site.getBaseCustomUrl()+Site.getPathPrefix()+ARTICLE_PATH;
System.debug('Value of page index '+lstCurrentArticle[0].Page_Index__c);
//if record found and record says its page is index (TRUE denotes NO INDEX here)
if(!lstCurrentArticle.isEmpty() && ! lstCurrentArticle[0].Page_Index__c) {
if(String.isNotBlank(lstCurrentArticle[0].Custom_Canonical_URL__c)) {
List<Knowledge__kav> lstParentArticle = [SELECT id , UrlName
FROM Knowledge__kav WHERE Id =: lstCurrentArticle[0].Custom_Canonical_URL__c
LIMIT 1];
if(!lstParentArticle.isEmpty()){
System.debug('Parent Url Name == '+lstCurrentArticle[0].Custom_Canonical_URL__r.UrlName);
urlName = domainURL+lstParentArticle[0].UrlName ;
}
}
else {
urlName = domainURL+lstCurrentArticle[0].UrlName ;
}
}
}
}
catch(Exception objExp) {
ApplicationLogger.addLog('requestName', objExp);
}
return urlName ;
}
public static String getCanonicalLink(String articleId) {
String urlName = null ;
try{
if(String.isNotBlank(articleId)) {
List<Knowledge__kav> lstCurrentArticle = [SELECT id , Custom_Canonical_URL__c ,UrlName , Page_Index__c,
Custom_Canonical_URL__r.UrlName
FROM Knowledge__kav WHERE Id =: articleId
LIMIT 1];
String domainURL = Site.getBaseCustomUrl()+Site.getPathPrefix()+ARTICLE_PATH;
System.debug('Value of page index '+lstCurrentArticle[0].Page_Index__c);
//if record found and record says its page is index (TRUE denotes NO INDEX here)
if(!lstCurrentArticle.isEmpty() && ! lstCurrentArticle[0].Page_Index__c) {
if(String.isNotBlank(lstCurrentArticle[0].Custom_Canonical_URL__c)) {
List<Knowledge__kav> lstParentArticle = [SELECT id , UrlName
FROM Knowledge__kav WHERE Id =: lstCurrentArticle[0].Custom_Canonical_URL__c
LIMIT 1];
if(!lstParentArticle.isEmpty()){
System.debug('Parent Url Name == '+lstCurrentArticle[0].Custom_Canonical_URL__r.UrlName);
urlName = domainURL+lstParentArticle[0].UrlName ;
}
}
else {
urlName = domainURL+lstCurrentArticle[0].UrlName ;
}
}
}
}
catch(Exception objExp) {
ApplicationLogger.addLog('requestName', objExp);
}
return urlName ;
}
Can you tell me the sample values for Custom_Canonical_URL__c & ARTICLE_PATH in your code.
Custom_Canonical_URL__c value is id of any knoweldge article? If yes, can you try below to get code coverage.
1. Cretae Knowledge__kav record with all the required values.
2. Create one more Knowledge__kav record with ll the required values and Custom_Canonical_URL__c value = 1st record id.
Then save the code and run for test.
Please let me know if you find any difficulties.
I have gone through your code and since you haven't provided any info about your current code coverage, I'll recommend you to do the following:
1. Since you have use try-catch blocks, therefore you should cover both the blocks to increse your current code coverage. I hope your code is correct and your try block is executing well. For catch block to get executed try passing articleId such that it doesn't matches any record in org. THerefore it will generate an exception wirh no rows.
2. Next you have used one if-else block too, hoping your if(!lstCurrentArticle.isEmpty() && ! lstCurrentArticle[0].Page_Index__c) is getting executed for else make this condition false.
Hence to conclude you will be requiring 3 methods in your test class for to test above 2 plus one correct condtion.
Hope this helps. If it does, please mark as Best Answer to help others too. Further if you require any other assistance regarding this, feel free to contact.
Thanks,
Ajay Dubedi