@isTest
public class PPAndLoanApprovalProcessTest_NEW {
/**
* @name setup
* @parm
* @desc test set up method
* @version 1.0
* @author : Someone
**/
@testsetup
private static void setUp() {
Test.startTest();
LogSeverityLevelValue__c logLevelSettings = TestDataFactorySFN.createLoggingLevel();
Database.upsert(logLevelSettings);
//Switch on the triggers
List<Trigger_Bypass__c> tSwitch = TestDataFactorySFN.createTriggerSwitch();
Database.insert(tSwitch);
List<Account> listAccBus = new List<Account>();
List<Account> listAccInd = new List<Account>();
Profile p= [SELECT Id From Profile WHERE Name='nTest' OR Name='nTest2' LIMIT 1];
User smevsde = [SELECT Id FROM user WHERE profileid = : p.Id and IsActive = true LIMIT 1];
system.debug('setup smevsde-->' + smevsde.Id);
//insert Product structure matrix
Product_StructureMatrix__c prdstm = new Product_StructureMatrix__c();
prdstm.Name='987654321';
prdstm.Product__c='Commercial (VR)';
prdstm.Product_Type__c='SME - Term loan';
prdstm.Product_Type_ID__c='123456789';
prdstm.Segment__c='SME';
insert prdstm;
Product_StructureMatrix__c prdstm2 = new Product_StructureMatrix__c();
prdstm2.Name='987654321';
prdstm2.Product__c='Commercial';
prdstm2.Product_Type__c='SME - Line of credit';
prdstm2.Product_Type_ID__c='123456789';
prdstm2.Segment__c='SME';
insert prdstm2;
system.runAs(smevsde){
//Create Business Accounts
listAccBus = SystemTestDataFactory.createTestAccounts(1);
//Create Opportunity from Business Account
/**Method Definition : SystemTestDataFactory.oppcreate(Account Id,Name,Stage,Loss Reason,Product Line, Product Type,
Product, Amount, Source, Type)
**/
Integer NoOfOpps = 0;
List<Opportunity> oppList = new List<Opportunity>();
oppList.add(SystemTestDataFactory.oppcreate(listAccBus[0].Id, 'TestOpportunity', 'Proposed',
'NBC – Risk','SME','SME - Term loan','Commercial (VR)',500.00,'Prospecting','Increase'));
oppList.add(SystemTestDataFactory.oppcreate(listAccBus[0].Id, 'TestOpportunity', 'Proposed',
'NBC – Risk','SME','SME - Line of credit','Commercial',500.00,'Prospecting','Increase'));
NoOfOpps=oppList.size();
if(!oppList.isEmpty()){
Database.insert(oppList);
}
//Create Disbusrement Forecast
List<Disbursement_Forecast__c> disbForecast = TestDataFactorySFN.createDisbursementForecast(1,oppList.get(0).Id);
List<Disbursement_Forecast__c> disbForecast2 = TestDataFactorySFN.createDisbursementForecast(1,oppList.get(1).Id);
//Create Product Package
List<LLC_BI__Product_Package__c> prodpackaglist = SystemTestDataFactory.createProdPkg(1);
Database.insert(prodpackaglist);
//Create Loan linked and link it to PP
StaticTriggerValues.isLoanUpdateFromUI = false;
List<LLC_BI__Loan__c> loanList = new List<LLC_BI__Loan__c>();
loanList = SystemTestDataFactory.createLoans(noOfOpps,prodpackaglist.get(0).Id,oppList,listAccBus.get(0).Id);
//Update the opportunities with created loans
for(integer i=0;i<noOfOpps;i++){
oppList.get(i).LLC_BI__Loan__c = loanList.get(i).Id;
}
if(!oppList.isEmpty()){
Database.update(oppList);
}
}
}
/**
*Method Name: testManualApprovalProcessManuallyApproved
*Description: This method will test approval process manually approved scenario.
**/
@isTest
private static void testManualApprovalProcessManuallyApproved(){
//Given
Profile p= [SELECT Id From Profile WHERE Name='nTest' OR Name='nTest2' LIMIT 1];
Profile p1= [SELECT Id From Profile WHERE Name='nTest3'];
User smevsde = [SELECT Id FROM user WHERE profileid = : p.Id and IsActive = true LIMIT 1];
system.debug('method smevsde-->' + smevsde.Id);
User credoff = [SELECT Id FROM user WHERE profileid = : p1.Id and IsActive = true LIMIT 1];
Id crdOffQId = [SELECT Id from Group where Name =: System.Label.SFN_Credit_Team_Queue and Type
=: System.Label.SFN_Queue limit 1][0].Id;
LLC_BI__Product_Package__c pplist = [SELECT Id,LLC_BI__Status__c, OwnerId, Assigned_to_Me__c FROM
LLC_BI__Product_Package__c LIMIT 1];
pplist.LLC_BI__Status__c = 'Open';
pplist.LLC_BI__Reviewable_Loan_Facilities_Count__c = 1;
ProductPackages.isFirstTime = true;
StaticTriggerValues.ppAfterUpdate = true;
Database.update(pplist);
List<LLC_BI__Loan__c> loansList = [SELECT Id, LLC_BI__Stage__c, LLC_BI__Status__c,LLC_BI__Is_Review_Ready__c, OwnerId, LLC_BI__Product_Package__c
FROM LLC_BI__Loan__c WHERE LLC_BI__Product_Package__c =: pplist.Id ];
for(Integer i=0;i<loansList.size();i++){
loansList.get(i).LLC_BI__Stage__c = 'Underwriting';
}
//Loans.isCalledFirstTime = true;
Database.update(loansList);
Test.startTest();
//Create an approval request
//When
System.runAs(smevsde){
Approval.ProcessSubmitRequest req1 = new Approval.ProcessSubmitRequest();
req1.setComments('Submitting request for approval.');
req1.setObjectId(pplist.Id);
StaticTriggerValues.ppAfterUpdate = true;
Approval.ProcessResult result = Approval.process(req1);
System.assert(result.isSuccess());
List<Id> newWorkItemIds = result.getNewWorkItemIds();
List<Id> newWorkItemIds2 = new List<Id>();
List<Id> newWorkItemIds3 = new List<Id>();
Approval.ProcessSubmitRequest req2 = new Approval.ProcessSubmitRequest();
Approval.ProcessSubmitRequest req21 = new Approval.ProcessSubmitRequest();
for(Integer i=0;i<1;i++){
req2.setComments('Submitting request for approval.');
req2.setObjectId(loansList[i].Id);
Loans.isCalledFirstTime = true;
Approval.ProcessResult result2 = Approval.process(req2);
System.assert(result2.isSuccess());
newWorkItemIds2 = result2.getNewWorkitemIds();
req21.setComments('Submitting request for approval.');
req21.setObjectId(loansList[i+1].Id);
Loans.isCalledFirstTime = true;
Approval.ProcessResult result21 = Approval.process(req21);
System.assert(result21.isSuccess());
newWorkItemIds3 = result21.getNewWorkitemIds();
}
system.debug('newWorkItemIds2-->' + newWorkItemIds2);
system.debug('newWorkItemIds3-->' + newWorkItemIds3);
System.debug('Loans stage after approval submit-->' + loansList.get(0).LLC_BI__Stage__c);
//System.debug('Loans stage after approval submit-->' + loansList.get(1).LLC_BI__Stage__c);
ProductPackages.isFirstTime = true;
system.debug('newWorkItemIds-->' + newWorkItemIds);
System.runAs(credoff){
system.debug('credoff user Id' + credoff.Id);
pplist.Assigned_to_Me__c = credoff.Id;
ppList.OwnerId = credoff.Id;
Database.update(pplist);
system.debug('Assigned owner PP--' + pplist.OwnerId);
system.debug('Assigned owner Loan--' + loansList.get(0).OwnerId);
system.debug('Assigned owner Loan--' + loansList.get(1).OwnerId);
Approval.ProcessWorkItemRequest req3 = new Approval.ProcessWorkItemRequest();
req3.setComments('Approving request.');
req3.setAction('Approve');
req3.setWorkItemId(newWorkItemIds.get(0));
system.debug('req3.setWorkItemId-->' + newWorkItemIds.get(0));
StaticTriggerValues.ppAfterUpdate = true;
Approval.ProcessResult result3 = Approval.process(req3);
System.assert(result3.isSuccess(), 'Result Status 3:'+result3.isSuccess());
system.debug('Result Status 3: ' + result3);
system.debug('newWorkItemIds2 size--' + newWorkItemIds2.size());
for(Integer i=0;i<newWorkItemIds2.size();i++){
Approval.ProcessWorkItemRequest req4 = new Approval.ProcessWorkItemRequest();
req4.setComments('Approving request.');
req4.setAction('Approve');
req4.setWorkItemId(newWorkItemIds2.get(i));
system.debug('req4.setWorkItemId-->' + newWorkItemIds2.get(i));
//Loans.isCalledFirstTime = true;
Approval.ProcessResult result4 = Approval.process(req4);
system.debug('result4-->' + result4);
System.assert(result4.isSuccess(), 'Result Status 4:'+result4.isSuccess());
}
for(Integer i=0;i<newWorkItemIds3.size();i++){
Approval.ProcessWorkItemRequest req5 = new Approval.ProcessWorkItemRequest();
req5.setComments('Approving request.');
req5.setAction('Approve');
req5.setWorkItemId(newWorkItemIds3.get(i));
system.debug('req5.setWorkItemId-->' + newWorkItemIds3.get(i));
//Loans.isCalledFirstTime = true;
Approval.ProcessResult result5 = Approval.process(req5);
system.debug('result4-->' + result5);
System.assert(result5.isSuccess(), 'Result Status 5:'+result5.isSuccess());
}
}
}
//Then
test.stopTest();
LLC_BI__Product_Package__c ppNewList = [SELECT Id, LLc_BI__Status__c, OwnerId FROM LLC_BI__Product_Package__c
LIMIT 1];
system.assertEquals('Manually Approved', ppNewList.LLC_BI__Status__c);
system.assertEquals(smevsde.Id, ppNewList.OwnerId);
List<LLC_BI__Loan__c> loanListNew = [SELECT Id, LLC_BI__Stage__c, OwnerId, LLC_BI__Product_Package__c
FROM LLC_BI__Loan__c WHERE LLC_BI__Product_Package__c =: ppNewList.Id LIMIT 100];
for(Integer i=0;i<loanListNew.size();i++){
system.assertEquals('Pre-Closing', loanListNew.get(i).LLC_BI__Stage__c);
system.assertEquals(smevsde.Id, loanListNew.get(i).OwnerId);
}
}