You need to sign in to do that
Don't have an account?

Need Help in writing Test class for controller !!!!!!
Test class !!!!!
public class UploadController {
public class MainWrapper {
@AuraEnabled public List<ColumnWrapper> ColumnWrap{get;set;}
@AuraEnabled public List<DocWrap> lstContentDocument{get;set;}
public MainWrapper(List<ColumnWrapper> lstColumnWrapper, List<DocWrap> lstContentDocument){
this.lstColumnWrapper = lstColumnWrapper;
this.lstContentDocument = lstContentDocument;
}
}
public class DocWrapper {
@AuraEnabled public String strBase64{get;set;}
@AuraEnabled public String strContentType{get;set;}
public DocWrapper(String strBase64, String strContentType){
this.strBase64 = strBase64;
this.strContentType = strContentType;
}
}
public class DocWrap {
@AuraEnabled public String Id{get;set;}
@AuraEnabled public String Title{get;set;}
@AuraEnabled public String CreatedDate{get;set;}
@AuraEnabled public String FileType{get;set;}
@AuraEnabled public String FileExtension{get;set;}
@AuraEnabled public String ContentSize{get;set;}
@AuraEnabled public Boolean isInSFDC{get;set;}
public DocWrap(String Id, String Title, String CreatedDate, String FileType, String FileExtension, String ContentSize, Boolean isInSFDC){
this.Id = Id;
this.Title = Title;
this.Id = Id;
this.CreatedDate = CreatedDate;
this.FileType = FileType;
this.FileExtension = FileExtension;
this.ContentSize = ContentSize;
this.isInSFDC = isInSFDC;
}
}
public class ColumnWrapper {
@AuraEnabled public String label {get;set;}
@AuraEnabled public String fieldName {get;set;}
@AuraEnabled public String type {get;set;}
@AuraEnabled public Boolean sortable {get;set;}
@AuraEnabled public SubColumnWrapper typeAttributes {get;set;}
public ColumnWrapper(string label, String fieldName,string type, Boolean sortable, SubColumnWrapper typeAttributes){
this.label = label;
this.fieldName = fieldName;
this.type = type;
this.sortable = sortable;
this.typeAttributes = typeAttributes;
}
public ColumnWrapper(string label, String fieldName,string type, Boolean sortable){
this.label = label;
this.fieldName = fieldName;
this.type = type;
this.sortable = sortable;
}
}
//typeAttributes: { label: 'View Details', name: 'view_details', title: 'Click to View Details'}
public class SubColumnWrapper {
@AuraEnabled public String label {get;set;}
@AuraEnabled public String name {get;set;}
@AuraEnabled public string title {get;set;}
public SubColumnWrapper(){
}
public SubColumnWrapper(string label, String name,string title){
this.label = label;
this.name = name;
this.title = title;
}
}
@AuraEnabled
public static string getContent(String strMime){
List<Document_Mime_Types__mdt> lst = new List<Document_Mime_Types__mdt>();
lst = [SELECT Id, Content_Type__c, Mime_Type__c FROM Document_Mime_Types__mdt WHERE Mime_Type__c =: strMime];
if(lst.size() > 0)
return lst[0].Content_Type__c;
else
return null;
}
@AuraEnabled
public static DocWrapper ValidateDoc(String strContentDocumentId, Boolean onSalesforce) {
//System.debug('ContentDocumentId'+strContentDocumentId);
ContentVersion obj = new ContentVersion();
if(onSalesforce)
{
obj = [SELECT FileType, FileExtension ,CreatedDate,VersionData FROM ContentVersion WHERE ContentDocumentId =: strContentDocumentId];
//System.debug('obj.VersionData : '+obj.VersionData);
}
else{
final string Folder = '/'+ strContentDocumentId;
System.HttpResponse res = makeCallout(Folder, 'DR_Documents');
if(res.getStatusCode() == 200 || res.getStatusCode() == 201){
// Parsing Code
System.debug('res.getBody())'+res.getBody());
String str = res.getBody().substringAfter('{"documents":[');
//System.debug('res.g '+str);
str = str.substringBefore(']}');
Map<String, Object> m1 = (Map<String, Object>)JSON.deserializeUntyped(str);
//System.debug('Map'+m1);
//System.debug('String.valueOf(m1.get())'+String.valueOf(m1.get('mimeType')));
String strmime = getContent(String.valueOf(m1.get('mimeType')));
//System.debug('Sstr1 mimeType'+ strmime);
if(String.isblank(strmime))
{
return new DocWrapper( String.valueOf(m1.get('content')), String.valueOf(m1.get('mimeType')));
}
else
{
return new DocWrapper( String.valueOf(m1.get('content')), strmime);
}
}
}
String str112 = getContent(obj.FileExtension);
if(String.isBlank(str112)){
return new DocWrapper(EncodingUtil.base64Encode(obj.VersionData), obj.FileExtension);
}
else {
return new DocWrapper(EncodingUtil.base64Encode(obj.VersionData), str112);
}
}
@AuraEnabled
public static MainWrapper getRecordLocal(String strCaseId) {
List<ContentDocumentLink> lstContentDocumentLink = new List<ContentDocumentLink>();
List<ColumnWrapper> lstColumnWrapper = new List<ColumnWrapper>();
List<DocWrap> lstDocWrap = new List<DocWrap>();
Set<ID> setId = new Set<ID>();
Set<ID> setEmailMessageId = new Set<ID>();
for(EmailMessage objEmailMessage : [SELECT Id FROM EmailMessage WHERE ParentId =: strCaseId])
setEmailMessageId.add(objEmailMessage.Id);
setEmailMessageId.add(strCaseId);
lstContentDocumentLink = [SELECT ContentDocumentId FROM ContentDocumentLink
WHERE LinkedEntityId =: setEmailMessageId ];
for(ContentDocumentLink objContentDocumentLink : lstContentDocumentLink)
setId.add(objContentDocumentLink.ContentDocumentId);
for(ContentDocument objContentDocument :[SELECT Id, Title, CreatedDate, FileType, FileExtension, ContentSize FROM ContentDocument WHERE Id =: setId Order by CreatedDate DESC]){
String size = '';
if(objContentDocument.ContentSize/1024 < 1024)
size = String.valueOf(objContentDocument.ContentSize / 1024) + ' KB';
else
size = String.valueOf(objContentDocument.ContentSize / 1048576 ) + ' MB';
lstDocWrap.add(new DocWrap(String.valueOf(objContentDocument.Id), objContentDocument.Title, String.valueOf(objContentDocument.CreatedDate), objContentDocument.FileType, objContentDocument.FileExtension, size, true ));
}
lstColumnWrapper.add(new ColumnWrapper('Title', 'Title', 'text' , true ));
lstColumnWrapper.add(new ColumnWrapper('Created Date', 'CreatedDate', 'datetime' , true));
lstColumnWrapper.add(new ColumnWrapper('Type', 'FileType', 'text' , true ));
lstColumnWrapper.add(new ColumnWrapper('View Document', 'Id', 'button' , false , new SubColumnWrapper('View', 'View Document','View Document')));
if(lstDocWrap.isEmpty())
{
return new MainWrapper(lstColumnWrapper, lstDocWrap); }
else{
return new MainWrapper(lstColumnWrapper, lstDocWrap);}
}
@AuraEnabled
public static List<DocWrap> getRecord(String strCaseId) {
//system.debug('strCaseId'+strCaseId);
List<DocWrap> lstDocWrap = new List<DocWrap>();
try{
String metaDeveloperName = '';
string Folder = '';
Case objCase = [SELECT CaseNumber,C_Case_Id__c, Document_Id__c,document_number__c FROM Case WHERE ID =: strCaseId LIMIT 1];
if(objCase.document_number__c !=''){
metaDeveloperName = 'DCN_Documents';
Folder = '?metadataValues=' + objCase.document_number__c +'&metadataNames=DCN%20Id&category=ESAWS%20-%20Model&subCategory=Intake';
}
else{
metaDeveloperName = 'DR_Documents';
Folder = '?category=ESAWS%20-%20Model&subCategory=Documents&metadataNames=Case%20Id&metadataValues='+ objCase.CaseNumber;
}
System.HttpResponse res = makeCallout(Folder,metaDeveloperName);
// System.debug('response'+res);
String ResponseBody;
Map<String, Object> m = new Map<String, Object>();
System.debug('res.getStatusCode() Before'+res.getStatusCode() );
if(res.getStatusCode() == 200 || res.getStatusCode() == 201){
// Parsing Code
ResponseBody = res.getBody();
for(MS_parseJSON.cls_documents objcls_documents : MS_parseJSON.parse(res.getBody()).documents ){
lstDocWrap.add(new DocWrap(objcls_documents.id, objcls_documents.name, objcls_documents.createdOn, objcls_documents.mimeType , objcls_documents.mimeType ,' ' ,false ));
}
}
if(objCase.C_Case_Id__c != null ){
final string str_C_Folder = '?category=ESAWS%20-%20Model&subCategory=Documents&metadataNames=Case%20Id&metadataValues='+ objCase.C_Case_Id__c;
System.HttpResponse resCA = makeCallout(str_C_Folder,metaDeveloperName);
String ResponseBodyCA;
Map<String, Object> m1 = new Map<String, Object>();
if(resCA.getStatusCode() == 200 || resCA.getStatusCode() == 201){
// Parsing Code
ResponseBodyCA = resCA.getBody();
for(MS_parseJSON.cls_documents objcls_documents : MS_parseJSON.parse(resCA.getBody()).documents ){
lstDocWrap.add(new DocWrap(objcls_documents.id, objcls_documents.name, objcls_documents.createdOn, objcls_documents.mimeType , objcls_documents.mimeType ,' ' ,false ));
}
}
}
if(objCase.Document_Id__c != null){
List<String> lstDocIds = new List<String>();
lstDocIds = objCase.Document_Id__c.split(',');
for(String strId : lstDocIds){
final string Folder1 = '/'+ strId;
System.HttpResponse res1 = makeCallout(Folder1,metaDeveloperName );
system.debug('res1'+res1);
if(res1.getStatusCode() == 200 || res1.getStatusCode() == 201){
// Parsing Code
for(MS_parseJSON.cls_documents objcls_documents : MS_parseJSON.parse(res1.getBody()).documents )
lstDocWrap.add(new DocWrap(objcls_documents.id, objcls_documents.name, objcls_documents.createdOn, objcls_documents.mimeType , objcls_documents.mimeType ,' ' ,false ));
}
}
}
}
catch(Exception vEx){
system.debug('vEx'+vEx);
EHSerExceptionLogUtilityClass.CreateException(vEx);
}
if(!lstDocWrap.isEmpty())
return lstDocWrap;
return null;
}
public static System.HttpResponse makeCallout(String Folder,String metaDeveloperName){
system.Debug('Folder'+Folder);
system.Debug('metaDeveloperName'+metaDeveloperName);
// Http instance
HttpRequest ObjRequest = new HttpRequest();
Http ObjHTTp = new Http();
List<String> lstRequestBody = new List<String>();
//Final data
final string Content_Type = 'application/json';
final string On_CRED = 'DMS Credentials';
final string strMethod = 'GET';
ObjRequest = EUploadController.MakeRequest(metaDeveloperName, Folder, strMethod, On_CRED );
System.HttpResponse res;
try{
res = ObjHTTp.send(ObjRequest);
}
catch(Exception vEx){
EHSerExceptionLogUtilityClass.CaptureAPIResponse(res.getStatusCode() + res.getBody() + 'UploadController');
}
system.debug('res1'+res.getBody());
System.debug('Status code'+res.getStatusCode() );
if(res.getStatusCode() == 200 || res.getStatusCode() == 201)
return res;
else
EHSerExceptionLogUtilityClass.CaptureAPIResponse(res.getStatusCode() + res.getBody() + 'UploadController');
return res;
}
}
As you are making callouts in the class you have to make HttpMockClass for fake response.
Please see the link Testing HTTP Callouts by Implementing the HttpCalloutMock Interface (https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_restful_http_testing_httpcalloutmock.htm).
Please insert "Document_Mime_Types__mdt" and for EmailMessage
I would try two things:
1. Wrap your testmethod Messaging.sendEmail method in a Test.startTest() and Test.stopTest() method. When the stoptest() is invoked, SFDC executes all async operations (e.g. sends the email). Then do your asserts
2. If #1 doesn't work, write your testmethod to simulate the act of sending the email by doing an insert on EmailMessage as test data, then do database SOQL on the result of the trigger to see if the right stuff happened.