+ Start a Discussion
Hitesh KhannaHitesh Khanna 

how to write webservice callout apex test class for this code ?..can anybody help ?

@AuraEnabled(cacheable=true)
    public static EmbedTokenDetails getEmbedToken(String workspaceId, String reportId, String datasetId) {
       
        EmbedTokenDetails result = new EmbedTokenDetails();

        String access_token = getAccessToken();
        if(String.isNotBlank(access_token)){
            Http http = new Http();        
            HttpRequest reqGetEmbedToken = new HttpRequest();
            reqGetEmbedToken.setMethod('POST');
            String urlGetEmbedToken = 'https://api.powerbi.com/v1.0/myorg/groups/' + workspaceId + '/reports/' + reportId + '/GenerateToken';
            reqGetEmbedToken.setEndpoint(urlGetEmbedToken);
            reqGetEmbedToken.setHeader('Authorization', 'Bearer ' + access_token);    
            reqGetEmbedToken.setBody('{"accessLevel": "View", "datasetId": "' + datasetId + '"}');
    
            HttpResponse responseEmbedToken = http.send(reqGetEmbedToken);
            
            if(responseEmbedToken.getStatusCode()!=200){
                System.debug('Embed Token Response Status: '+responseEmbedToken.getStatus());
                System.debug('Embed Token Response Status Code: ' + responseEmbedToken.getStatusCode());
                System.debug('Embed Token Response Body: ' + responseEmbedToken.getBody());  
                // TREN:805 -> throw exception here that has the body in the exception message
                // concatenate status code with body
                return result;            
            }
            
            Map<String, Object> responseBody = (Map<String, Object>) JSON.deserializeUntyped(
                responseEmbedToken.getBody()
            );
            result.embedToken = (String) responseBody.get('token');
            result.embedTokenExpiration = (String) responseBody.get('expiration');

            system.debug('@@@result.embedToken:' + result.embedToken);
            system.debug('@@@result.embedTokenExpiration:' + result.embedTokenExpiration);
        }
        return result;
    }
 
Best Answer chosen by Hitesh Khanna
PriyaPriya (Salesforce Developers) 

Hi Hitesh,

Refer this link :-

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_callouts_wsdl2apex_testing.htm


Change your code as accordingly.

Regards.

Priya Ranjan