You need to sign in to do that
Don't have an account?
Priya 777
test class for content document link
Hi can anyone please help me with this test class .I was able to create content version but not content document link
Apex class:
public class ContentVersionTriggerHelper {
public static void linkContentVersion (List<contentversion> contentversionList) {
try{
//To store the taxonomy name
Set <string>taxonomySet = new set <string>();
//To store Taxonomy name and ID - Name is the Key
map <string, string> taxonomyMap = new map <string,string>();
list<contentversion> contentversionCaseList = new list<contentversion> ();
for (contentversion objCV: contentversionList){
system.debug('$$$objCV'+objCV.ContentDocumentId);
// check if Taxonomy exists
if(objCV.Taxonomy_Reference__c!=null && objCV.CurrentUserProfile__c == 'Integration User'){
taxonomySet.add(objCV.Taxonomy_Reference__c);
//check if case Id exist
}else if(objCV.Case_Reference__c!=null && objCV.CurrentUserProfile__c == 'Integration User'){
contentversionCaseList.add(objCV);
}
}
if(!taxonomySet.isEmpty()){
for(Document_Type__c objDT : [select Id,name from Document_Type__c where name=:taxonomySet]){
taxonomyMap.put(objDT.name,objDT.Id);
}
//create case documents and link files
insertCaseDocumentandLink(contentversionList,taxonomyMap);
}
if(!contentversionCaseList.isEmpty()){
// Directly link files to case
insertContentDocumentLink(contentversionCaseList);
}
}catch (exception ex){
system.debug('Exception occured in ContentVersionTriggerHelperClass' + ex.getLineNumber() +ex.getMessage());
insert new Error_Framework__c(
Error_Message__c=ex.getStackTraceString(),
Event_Category__c='Contentversion After Insert',
Description__c='Exception Occured in linkContentVersion Method',
User__c=UserInfo.getUserId(),
Object_Name__c='ContentDocumentLink/CaseDocument',
Source_Code_Location__c='ContentVersionTriggerHelper'
);
}
return;
}
public static void insertCaseDocumentandLink(List<contentversion> contentversionList, map <string, string> taxonomyMap ) {
system.debug('$$$insertCaseDocumentandLink'+contentversionList +' '+ taxonomyMap);
//To store the Case_Documents__c to be inserted
list <Case_Documents__c> insertCaseDocumentList = new list <Case_Documents__c>();
//To store the ContentDocumentLink to be inserted
list <ContentDocumentLink> insertContentDocumentLinkList = new list <ContentDocumentLink>();
for (contentversion objCV: contentversionList){
if(taxonomyMap.containskey(objCV.Taxonomy_Reference__c)){//checking if taxnomy exists
Case_Documents__c newDocument = new Case_Documents__c();
newDocument.Document_Status__c='Draft';
newDocument.Document_Type__c=taxonomyMap.get(objCV.Taxonomy_Reference__c);
newDocument.Effective_Date__c=system.today();
newDocument.Number_Of_Files__c=0;
newDocument.Case_Number__c = objCV.Case_Reference__c;
newDocument.Send_to_Oneview__c = false;
system.debug('$$$ContentDocumentId'+objCV.ContentDocumentId);
newDocument.Content_Document_Reference__c = objCV.ContentDocumentId;
insertCaseDocumentList.add(newDocument);
}
}
if(!insertCaseDocumentList.isEmpty()){
insert insertCaseDocumentList;
for (Case_Documents__c objCD: insertCaseDocumentList){
ContentDocumentLink caseDocLink = new ContentDocumentLink();
caseDocLink.LinkedEntityId = objCD.Id;
caseDocLink.contentDocumentId = objCD.Content_Document_Reference__c;
//Viewer permission. The user can explicitly view but not edit the shared file.
caseDocLink.ShareType = 'V';
insertContentDocumentLinkList.add(caseDocLink);
}
if(!insertContentDocumentLinkList.isEmpty()){
insert insertContentDocumentLinkList;
}
}
return;
}
// This is not covered in Test class .
public static void insertContentDocumentLink (List<contentversion> contentversionList){
system.debug('$$$insertContentDocumentLink'+contentversionList);
//To store the ContentDocumentLink to be inserted
list <ContentDocumentLink> insertContentDocumentLinkList = new list <ContentDocumentLink>();
for (contentversion objCV: contentversionList){
ContentDocumentLink caseDocLink = new ContentDocumentLink();
caseDocLink.LinkedEntityId = objCV.Case_Reference__c;
caseDocLink.contentDocumentId = objCV.ContentDocumentId;
//Viewer permission. The user can explicitly view but not edit the shared file.
caseDocLink.ShareType = 'V';
insertContentDocumentLinkList.add(caseDocLink);
}
if(!insertContentDocumentLinkList.isEmpty()){
insert insertContentDocumentLinkList;
}
return;
}
}
===========================================================================
Test class
@isTest
public class Test_ContentVersionInsertAction {
/*************************************************************************************************************
* Apex Class Name : ContentVersionInsertAction
* Version : 1.0
* Created Date : 12/02/2018
* Function : Test class for ContentVersionInsertAction
* Modification Log :
* Developer : Date Description
* ----------------------------------------------------------------------------
* Priyanka Indurthi 12/02/2018 Original Version
************************************************************************************************************/
static testmethod void testmethod1(){
Test.startTest();
Service_Mapping_Utility__c smu = new Service_Mapping_Utility__c(Case_Record_Type__c='Wires',
Case_Product_Name__c='Domestic Implementations',
Case_Action__c='Drawdown Setup',
Is_Auth__c=true,
Hand_Off__c=true,
Authentication_Required__c='High');
insert smu;
case newCase = new case(SelectProduct__c= smu.id,
Case_Type__c = 'Domestic Implementations',
Case_Received_Date__c = system.today());
insert newCase;
Document_Type__c docType = new Document_Type__c(LOB__c = 'Commercial Bank',
External_Id__c = 'Commercial BankEntityFinancial StatementsInterim Financial Statements',
Category__c = 'Entity');
insert docType;
profile pr = [select id from profile where name = 'Integration User'];
user intgUser = new user(firstname = 'testFirstName',
lastname = 'testLastName',
profileid = pr.id,
email = 'testuser@testclass.com',
Username = 'testfirst.lastname@testclass.com',
Alias = 'TfTl',
CommunityNickname = 'test' ,
TimeZoneSidKey = 'America/New_York',
LocaleSidKey = 'en_US',
EmailEncodingKey = 'UTF-8',
LanguageLocaleKey = 'en_US'
);
insert intgUser;
system.runAs(intgUser){
ContentVersion cv = new ContentVersion(
Title = 'Test',
PathOnClient = 'Test.jpg',
VersionData = blob.valueof('Test Content Data'),
IsMajorVersion = true,
Case_Reference__c = newcase.id,
Taxonomy_Reference__c = [select id,name from Document_Type__c where id =:docType.id][0].name
);
insert cv;
ContentVersion contentVersionSelect = [SELECT Id, Title, ContentDocumentId FROM ContentVersion WHERE Id = :cv.Id LIMIT 1];
List<ContentDocument> documents = [SELECT Id, Title, LatestPublishedVersionId FROM ContentDocument];
System.assertEquals(documents.size(), 1);
}
Test.stopTest();
}
}
Apex class:
public class ContentVersionTriggerHelper {
public static void linkContentVersion (List<contentversion> contentversionList) {
try{
//To store the taxonomy name
Set <string>taxonomySet = new set <string>();
//To store Taxonomy name and ID - Name is the Key
map <string, string> taxonomyMap = new map <string,string>();
list<contentversion> contentversionCaseList = new list<contentversion> ();
for (contentversion objCV: contentversionList){
system.debug('$$$objCV'+objCV.ContentDocumentId);
// check if Taxonomy exists
if(objCV.Taxonomy_Reference__c!=null && objCV.CurrentUserProfile__c == 'Integration User'){
taxonomySet.add(objCV.Taxonomy_Reference__c);
//check if case Id exist
}else if(objCV.Case_Reference__c!=null && objCV.CurrentUserProfile__c == 'Integration User'){
contentversionCaseList.add(objCV);
}
}
if(!taxonomySet.isEmpty()){
for(Document_Type__c objDT : [select Id,name from Document_Type__c where name=:taxonomySet]){
taxonomyMap.put(objDT.name,objDT.Id);
}
//create case documents and link files
insertCaseDocumentandLink(contentversionList,taxonomyMap);
}
if(!contentversionCaseList.isEmpty()){
// Directly link files to case
insertContentDocumentLink(contentversionCaseList);
}
}catch (exception ex){
system.debug('Exception occured in ContentVersionTriggerHelperClass' + ex.getLineNumber() +ex.getMessage());
insert new Error_Framework__c(
Error_Message__c=ex.getStackTraceString(),
Event_Category__c='Contentversion After Insert',
Description__c='Exception Occured in linkContentVersion Method',
User__c=UserInfo.getUserId(),
Object_Name__c='ContentDocumentLink/CaseDocument',
Source_Code_Location__c='ContentVersionTriggerHelper'
);
}
return;
}
public static void insertCaseDocumentandLink(List<contentversion> contentversionList, map <string, string> taxonomyMap ) {
system.debug('$$$insertCaseDocumentandLink'+contentversionList +' '+ taxonomyMap);
//To store the Case_Documents__c to be inserted
list <Case_Documents__c> insertCaseDocumentList = new list <Case_Documents__c>();
//To store the ContentDocumentLink to be inserted
list <ContentDocumentLink> insertContentDocumentLinkList = new list <ContentDocumentLink>();
for (contentversion objCV: contentversionList){
if(taxonomyMap.containskey(objCV.Taxonomy_Reference__c)){//checking if taxnomy exists
Case_Documents__c newDocument = new Case_Documents__c();
newDocument.Document_Status__c='Draft';
newDocument.Document_Type__c=taxonomyMap.get(objCV.Taxonomy_Reference__c);
newDocument.Effective_Date__c=system.today();
newDocument.Number_Of_Files__c=0;
newDocument.Case_Number__c = objCV.Case_Reference__c;
newDocument.Send_to_Oneview__c = false;
system.debug('$$$ContentDocumentId'+objCV.ContentDocumentId);
newDocument.Content_Document_Reference__c = objCV.ContentDocumentId;
insertCaseDocumentList.add(newDocument);
}
}
if(!insertCaseDocumentList.isEmpty()){
insert insertCaseDocumentList;
for (Case_Documents__c objCD: insertCaseDocumentList){
ContentDocumentLink caseDocLink = new ContentDocumentLink();
caseDocLink.LinkedEntityId = objCD.Id;
caseDocLink.contentDocumentId = objCD.Content_Document_Reference__c;
//Viewer permission. The user can explicitly view but not edit the shared file.
caseDocLink.ShareType = 'V';
insertContentDocumentLinkList.add(caseDocLink);
}
if(!insertContentDocumentLinkList.isEmpty()){
insert insertContentDocumentLinkList;
}
}
return;
}
// This is not covered in Test class .
public static void insertContentDocumentLink (List<contentversion> contentversionList){
system.debug('$$$insertContentDocumentLink'+contentversionList);
//To store the ContentDocumentLink to be inserted
list <ContentDocumentLink> insertContentDocumentLinkList = new list <ContentDocumentLink>();
for (contentversion objCV: contentversionList){
ContentDocumentLink caseDocLink = new ContentDocumentLink();
caseDocLink.LinkedEntityId = objCV.Case_Reference__c;
caseDocLink.contentDocumentId = objCV.ContentDocumentId;
//Viewer permission. The user can explicitly view but not edit the shared file.
caseDocLink.ShareType = 'V';
insertContentDocumentLinkList.add(caseDocLink);
}
if(!insertContentDocumentLinkList.isEmpty()){
insert insertContentDocumentLinkList;
}
return;
}
}
===========================================================================
Test class
@isTest
public class Test_ContentVersionInsertAction {
/*************************************************************************************************************
* Apex Class Name : ContentVersionInsertAction
* Version : 1.0
* Created Date : 12/02/2018
* Function : Test class for ContentVersionInsertAction
* Modification Log :
* Developer : Date Description
* ----------------------------------------------------------------------------
* Priyanka Indurthi 12/02/2018 Original Version
************************************************************************************************************/
static testmethod void testmethod1(){
Test.startTest();
Service_Mapping_Utility__c smu = new Service_Mapping_Utility__c(Case_Record_Type__c='Wires',
Case_Product_Name__c='Domestic Implementations',
Case_Action__c='Drawdown Setup',
Is_Auth__c=true,
Hand_Off__c=true,
Authentication_Required__c='High');
insert smu;
case newCase = new case(SelectProduct__c= smu.id,
Case_Type__c = 'Domestic Implementations',
Case_Received_Date__c = system.today());
insert newCase;
Document_Type__c docType = new Document_Type__c(LOB__c = 'Commercial Bank',
External_Id__c = 'Commercial BankEntityFinancial StatementsInterim Financial Statements',
Category__c = 'Entity');
insert docType;
profile pr = [select id from profile where name = 'Integration User'];
user intgUser = new user(firstname = 'testFirstName',
lastname = 'testLastName',
profileid = pr.id,
email = 'testuser@testclass.com',
Username = 'testfirst.lastname@testclass.com',
Alias = 'TfTl',
CommunityNickname = 'test' ,
TimeZoneSidKey = 'America/New_York',
LocaleSidKey = 'en_US',
EmailEncodingKey = 'UTF-8',
LanguageLocaleKey = 'en_US'
);
insert intgUser;
system.runAs(intgUser){
ContentVersion cv = new ContentVersion(
Title = 'Test',
PathOnClient = 'Test.jpg',
VersionData = blob.valueof('Test Content Data'),
IsMajorVersion = true,
Case_Reference__c = newcase.id,
Taxonomy_Reference__c = [select id,name from Document_Type__c where id =:docType.id][0].name
);
insert cv;
ContentVersion contentVersionSelect = [SELECT Id, Title, ContentDocumentId FROM ContentVersion WHERE Id = :cv.Id LIMIT 1];
List<ContentDocument> documents = [SELECT Id, Title, LatestPublishedVersionId FROM ContentDocument];
System.assertEquals(documents.size(), 1);
}
Test.stopTest();
}
}
Try below Test class,
NOTE: contentlink.LinkedEntityId=availableRateDeck.id; in this make sure you have mapped correct LinkEntityID insted of availableRateDeck.id in below code.
Hope this will help you.
Thanks
karthik