function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Atul Singh 87Atul 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 ; 
    }
Prathyu bPrathyu b
Hi Atul,

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.
 
Raj VakatiRaj Vakati
Try this code
 
@isTest(seeAllData=false)
public class KnowledgeTopicUpdate_UT {
//need a knowledge article and a team and a topic assignment and need to create a topic assignment for the knowledge article based off of the team.
  static testMethod void MakeArticle() {
      knowledge__kav newArticle = new knowledge__kav(Title='test',summary='xyz',urlname='xyz',Page_Index__c='Testing');
      insert newArticle;
	  
      knowledge__kav obj1 = [SELECT Id,Title,KnowledgeArticleId FROM knowledge__kav WHERE id =: newArticle.Id];


KbManagement.PublishingService.publishArticle(obj1.KnowledgeArticleId, true);
     

	 test.startTest();
     
	 YOURCLASSNAME.getCanonicalLink(obj1.Id)
	 
      test.stopTest();
  }
}

 
Ajay K DubediAjay K Dubedi
Hi Aniroodha,
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