You need to sign in to do that
Don't have an account?
ManojKumar Muthu
Test class for case attachment & Query
Hi there,
Can someone help with test class for below two Apex class??
@RestResource(urlMapping='/CaseAttachment/*')
global with sharing class CaseAttachment{
@HttpGet
global static List<Attachment> getCaseById() {
RestRequest req = RestContext.request;
RestResponse res = RestContext.response;
String Id = req.requestURI.substring(req.requestURI.lastIndexOf('/')+1);
List<Attachment> result = [Select id, Name, OwnerId, BodyLength, LastModifiedById, LastModifiedDate, ContentType, Body, Description, CreatedDate, CreatedById from Attachment where ParentId = :Id];
return result;
}
@HttpPost
global static void doget(String type, String body, String ContentType, String ParentID ){
RestRequest req = RestContext.request;
RestResponse res = RestContext.response;
String CaseName=req.requestURI.substring(req.requestURI.lastIndexOf('/')+1);
String doc=EncodingUtil.Base64Encode(req.requestBody);
Attachment a = new Attachment();
a.Name = type;
a.Body = Blob.valueOf(body);
a.ContentType = ContentType;
a.ParentId = 'Id';
insert a;
}
}
and-------------------------------------------------------------------------------------------------------
@RestResource(urlMapping='/QueryFilterStatus/*')
global with sharing class QueryFilterStatus{
@HttpPost
global static List<Case> dopostCase(String Owneremail, String Bugis, String Version, String accountid,String tracker,String priority,String contactEmail,String status,String CStatus,String subject, String Keyword) {
List<Case> caseList = new List<Case>();
String str='Select id, Case_Owner_Name__c, OwneEmail__c, Description, Subject, Case_Type__c, Code__c, Tracker__c, status, Version__c, type, priority, ContactEmail, ContactId, OwnerId, CaseNumber, CSM_ID__c, CUP_Phone__c, Call_Type__c, IsClosed from Case where accountid =\''+ accountid +'\'';
if(String.isNotBlank(Owneremail))
{
str+=' AND OwneEmail__c=\''+ Owneremail+'\'';
}
if(String.isNotBlank(Bugis))
{
str+=' AND BugIs__c=\''+ Bugis+'\'';
}
if(String.isNotBlank(Version))
{
str+=' AND Version__c=\''+ Version+'\'';
}
if(String.isNotBlank(tracker))
{
str+=' AND Tracker__c =\''+ tracker+'\'';
}
if(String.isNotBlank(priority))
{
str+=' AND Priority=\''+ priority+'\'';
}
if(String.isNotBlank(contactEmail))
{
str+=' AND ContactEmail=\''+ contactEmail+'\'';
}
if(String.isNotBlank(status))
{
str+=' AND Status =\''+ status+'\'';
}
if(String.isNotBlank(CStatus))
{
str+=' AND CStatus__c=\''+ CStatus+'\'';
}
//if(String.isNotBlank(subject))
//{
// str+=' AND Subject Like \''+ subject+'\'';
// }
//if(String.isNotBlank(description))
//{
// str+=' AND Description Like \''+ description+'\'';
//}
if(String.isNotBlank(Keyword))
{
str+=' AND (Subject Like\'%'+ Keyword+'%\'';
str+=' OR CaseNumber Like \'%'+ Keyword+'%\')';
}
System.debug(str);
caseList=Database.query(str);
return caseList;
}
}
Can someone help with test class for below two Apex class??
@RestResource(urlMapping='/CaseAttachment/*')
global with sharing class CaseAttachment{
@HttpGet
global static List<Attachment> getCaseById() {
RestRequest req = RestContext.request;
RestResponse res = RestContext.response;
String Id = req.requestURI.substring(req.requestURI.lastIndexOf('/')+1);
List<Attachment> result = [Select id, Name, OwnerId, BodyLength, LastModifiedById, LastModifiedDate, ContentType, Body, Description, CreatedDate, CreatedById from Attachment where ParentId = :Id];
return result;
}
@HttpPost
global static void doget(String type, String body, String ContentType, String ParentID ){
RestRequest req = RestContext.request;
RestResponse res = RestContext.response;
String CaseName=req.requestURI.substring(req.requestURI.lastIndexOf('/')+1);
String doc=EncodingUtil.Base64Encode(req.requestBody);
Attachment a = new Attachment();
a.Name = type;
a.Body = Blob.valueOf(body);
a.ContentType = ContentType;
a.ParentId = 'Id';
insert a;
}
}
and-------------------------------------------------------------------------------------------------------
@RestResource(urlMapping='/QueryFilterStatus/*')
global with sharing class QueryFilterStatus{
@HttpPost
global static List<Case> dopostCase(String Owneremail, String Bugis, String Version, String accountid,String tracker,String priority,String contactEmail,String status,String CStatus,String subject, String Keyword) {
List<Case> caseList = new List<Case>();
String str='Select id, Case_Owner_Name__c, OwneEmail__c, Description, Subject, Case_Type__c, Code__c, Tracker__c, status, Version__c, type, priority, ContactEmail, ContactId, OwnerId, CaseNumber, CSM_ID__c, CUP_Phone__c, Call_Type__c, IsClosed from Case where accountid =\''+ accountid +'\'';
if(String.isNotBlank(Owneremail))
{
str+=' AND OwneEmail__c=\''+ Owneremail+'\'';
}
if(String.isNotBlank(Bugis))
{
str+=' AND BugIs__c=\''+ Bugis+'\'';
}
if(String.isNotBlank(Version))
{
str+=' AND Version__c=\''+ Version+'\'';
}
if(String.isNotBlank(tracker))
{
str+=' AND Tracker__c =\''+ tracker+'\'';
}
if(String.isNotBlank(priority))
{
str+=' AND Priority=\''+ priority+'\'';
}
if(String.isNotBlank(contactEmail))
{
str+=' AND ContactEmail=\''+ contactEmail+'\'';
}
if(String.isNotBlank(status))
{
str+=' AND Status =\''+ status+'\'';
}
if(String.isNotBlank(CStatus))
{
str+=' AND CStatus__c=\''+ CStatus+'\'';
}
//if(String.isNotBlank(subject))
//{
// str+=' AND Subject Like \''+ subject+'\'';
// }
//if(String.isNotBlank(description))
//{
// str+=' AND Description Like \''+ description+'\'';
//}
if(String.isNotBlank(Keyword))
{
str+=' AND (Subject Like\'%'+ Keyword+'%\'';
str+=' OR CaseNumber Like \'%'+ Keyword+'%\')';
}
System.debug(str);
caseList=Database.query(str);
return caseList;
}
}
All Answers
when I run a test below the error i am getting for the first test classs,
Class.System.EncodingUtil.base64Encode: line 10, column 1
Class.CaseAttachment.doget: line 18, column 1
Class.CaseAttachment1Test.testCaseAttachment1One: line 55, column 1
@RestResource(urlMapping='/CaseAttachment/*')
global with sharing class CaseAttachment{
@HttpGet
global static List<Attachment> getCaseById() {
RestRequest req = RestContext.request;
RestResponse res = RestContext.response;
String Id = req.requestURI.substring(req.requestURI.lastIndexOf('/')+1);
List<Attachment> result = [Select id, Name, OwnerId, BodyLength, LastModifiedById, LastModifiedDate, ContentType, Body, Description, CreatedDate, CreatedById from Attachment where ParentId = :Id];
return result;
}
@HttpPost
global static void doget(String type, String body, String ContentType, String ParentID ){
RestRequest req = RestContext.request;
RestResponse res = RestContext.response;
String CaseName=req.requestURI.substring(req.requestURI.lastIndexOf('/')+1);
String doc=EncodingUtil.Base64Encode(req.requestBody);
Attachment a = new Attachment();
a.Name = type;
a.Body = Blob.valueOf(body);
a.ContentType = ContentType;
a.ParentId = 'Id';
insert a;
}
________________________________________________________________________________________________________________
@isTest
private class CaseAttachment1Test {
@isTest static void testCaseAttachment1One() {
Test.startTest();
//create account
Account acc = new Account();
//enter details
acc.Name = 'Test Account';
insert acc;
//create case
Case c = new Case();
//enter details
c.AccountId = acc.Id;
c.Type = 'My Type';
c.Origin = 'My Origin';
c.Status = 'My Status';
insert c;
Attachment attach=new Attachment();
attach.Name='Test';
Blob bodyBlob=Blob.valueOf('Testing Body of Attachment');
attach.body=bodyBlob;
attach.parentId=c.id;
insert attach;
RestRequest req = new RestRequest();
RestResponse res = new RestResponse();
req.requestURI = '/services/apexrest/CaseAttachment/'+c.accountId; //Request URL
req.requestBody = Blob.valueof('JsonMsg');
RestContext.request = req;
RestContext.response= res;
CaseAttachment.getCaseById();
RestRequest req1 = new RestRequest();
RestResponse res1 = new RestResponse();
//String JsonMsg=JSON.serialize([Select Id from Case]);
req1.requestURI = '/services/apexrest/CaseAttachment/'; //Request URL
//req.requestBody = Blob.valueof(JsonMsg);
req1.httpMethod = 'POST';
RestContext.request = req1;
RestContext.response= res1;
CaseAttachment.doget('pdf','aaaaaaaaaaaaaaaaa','pdf',c.Id);
Test.stopTest();
Test.stopTest();
}
}
this part is alone is not covering in apex class
Attachment a = new Attachment();
a.Name = name;
a.Body = Blob.valueOf(body);
a.ContentType = ContentType;
a.ParentId = 'Id';
insert a;