+ Start a Discussion
Padmini S 26Padmini S 26 

System.StringException: Unrecognized base64 character: * in test class

Hi All,

I am getting the error in test class as System.StringException: Unrecognized base64 character: *. I am not getting how to resolve the issue. Could anyone help on this issue.
 
@isTest
private class TestUpdateCourseCompletionStatusService 
{
    public static testmethod void testupdate() {
     LMS__c lmsc = new LMS__c ();
    lmsc.Prod_CryptoKey__c = 'prod crypto';
    lmsc.Test_CryptoKey__c = 'test crypto';
    lmsc.Prod_IV__c = 'prod iv';
    lmsc.Test_IV__c = 'test iv';
    insert lmsc;
    
    Registration__c coursereg = new Registration__c();
    coursereg.Name__c = 'testcourse';
    coursereg.ID_Number__c = '1234';
    coursereg.LMS_Status__c ='Completed';
    insert coursereg;
    
     System.RestContext.request = new RestRequest();
     RestContext.request.requestURI = '/UpdateCourseCompletionStatus/*';
     RestContext.request.addHeader('decodedB64 ', '12345');
     
    UpdateCourseCompletionStatusService.updateCourseRegistration();
  
}
}

Thanks in Advance.


 
Pankaj_GanwaniPankaj_Ganwani
Hi Padmini,

It seems you will have to convert the string ie '12345' to Blob before setting it to the header. Use Blob.valueOf() method to convert the string to blob and then try:
 
@isTest
private class TestUpdateCourseCompletionStatusService 
{
    public static testmethod void testupdate() {
     LMS__c lmsc = new LMS__c ();
    lmsc.Prod_CryptoKey__c = 'prod crypto';
    lmsc.Test_CryptoKey__c = 'test crypto';
    lmsc.Prod_IV__c = 'prod iv';
    lmsc.Test_IV__c = 'test iv';
    insert lmsc;
    
    Registration__c coursereg = new Registration__c();
    coursereg.Name__c = 'testcourse';
    coursereg.ID_Number__c = '1234';
    coursereg.LMS_Status__c ='Completed';
    insert coursereg;
    
     System.RestContext.request = new RestRequest();
     RestContext.request.requestURI = '/UpdateCourseCompletionStatus/*';
     RestContext.request.addHeader('decodedB64 ', Blob.valueOf('12345'));
     
    UpdateCourseCompletionStatusService.updateCourseRegistration();
  
}
}

 
Padmini S 26Padmini S 26
Hi Pankaj,

Thanks for your reply.  I have tried with your code. I am getting the error as "Method does not exist or incorrect signature: [RestRequest].addHeader(String, Blob) "

 
Pankaj_GanwaniPankaj_Ganwani
Okay, then try using EncodingUtil.base64Encode(Blob.valueOf('12345')) at line no 20.
Padmini S 26Padmini S 26
Tried with EncodingUtil.base64Encode(Blob.valueOf('12345')). Test class fails with error "System.StringException: Unrecognized base64 character: *"
Pankaj_GanwaniPankaj_Ganwani
Can you debug the above statement in test class and see what comes? It seems your base64 string contains some characters which are not valid ones.Base64 encoded strings only contain letters, numbers and the characters + and \ (and = for padding). 
Padmini S 26Padmini S 26
Hi Pankaj, I have added the debug log system.debug ('*****' +EncodingUtil.base64Encode(Blob.valueOf('1234')));. But i didn't get any value, showing error as Unrecognized base64 character: * in debug logs.
Pankaj_GanwaniPankaj_Ganwani
Can you please paste your controller code here, you are writing the test class for?
Padmini S 26Padmini S 26
@RestResource(urlMapping='/UpdateCourseCompletionStatus/*')
global with sharing class UpdateCourseCompletionStatusService
{
   @HttpPost
    global static String updateCourseRegistration(){
    
    String cryptoKey;
    String IV;
    LMS__c lms=new LMS__c();
        lms = LMSCredentials__c.getinstance();   // Custom Setting
        if(UserInfo.getOrganizationId() == 'ProductionID') { // prod instance
            cryptoKey=lms.Prod_CryptoKey__c;
            IV=lms.Prod_IV__c;    
        } 
        else {   //sandbox
            cryptoKey=lms.Test_CryptoKey__c;
            IV=lms.Test_IV__c; 
        } 
    
        RestRequest req = RestContext.request;
        RestResponse res = Restcontext.response;
        String Endpoint=req.requestURI;
        string enrCourseComDate=endpoint.substring(endpoint.lastIndexOf('/')+1);
        String enrCourseComDateUTF= EncodingUtil.urlDecode(enrCourseComDate,'UTF-8');
        enrCourseComDateUTF= EncodingUtil.urlDecode(enrCourseComDateUTF,'UTF-8');
        Blob decodedB64 = EncodingUtil.base64Decode(enrCourseComDateUTF);
        system.debug(enrCourseComDateUTF);
        Blob decryptedBlob = Crypto.decrypt('AES256', blob.valueof(cryptoKey),blob.valueof(IV), decodedB64);
        string CourseComDate=decryptedBlob.toString();
        
        if(courseComDate.length()==8){
            endpoint=endpoint.removeEnd('/'+enrCourseComDate);
            String encEnCourseId= endpoint.substring(endpoint.lastIndexOf('/')+1);
            encEnCourseId= EncodingUtil.urlDecode(encEnCourseId,'UTF-8');
            encEnCourseId= EncodingUtil.urlDecode(encEnCourseId,'UTF-8');
            Blob decodedB641 = EncodingUtil.base64Decode(encEnCourseId);
            Blob decryptedBlob1 = Crypto.decrypt('AES256', blob.valueof(cryptoKey),blob.valueof(IV), decodedB641);
            string enCourseId=decryptedBlob1.toString();
            if(enCourseId.length()==5){
                List<Registration__c> cr=[select id,name,Course_Instance_LMS_Mode__c,LMS1_Enrcourse_ID__c,Registration_Date__c,Course_completion_Date__c from Registration__c where
                                                    LMS1_Enrcourse_ID__c=:enCourseId];
                if(cr.size()>0){                                    
                    Integer dateint=integer.valueof(courseComDate.substring(0,2));
                    Integer monthint=integer.valueof(courseComDate.substring(2,4));
                    Integer yint=integer.valueof(courseComDate.substring(4,8));
                    Date d = Date.newInstance(yint, monthint, dateint);
                    if(cr[0].Course_Instance_LMS_Mode__c!='Blended')
                        cr[0].Course_completion_Date__c=d;
                    //cr[0].LMS_Platform__c='WizLearn';
                    if(cr[0].Course_Instance_LMS_Mode__c!='Blended')
                        cr[0].LMS_Status__c='Completed';
                    else
                        cr[0].LMS_Status__c='Blended course completed';
                    update cr;
                  return '2000-Successful';
                }
                else
                    return '1003-EnrCourseId not found in SFDC';
            }
            else
                return '1002-Invalid EnrCourseID';
        }
        else 
            return '1001-Invalid Date';
   }
}