You need to sign in to do that
Don't have an account?
Poorna Priya 16
Need test class with 75% code coverage
Trigger:
trigger GenerateAffiliate on Account (before insert)
{
for(Account account: Trigger.new)
{
if(Trigger.isInsert)
{
String jsonString,paymentAccessKey;
if (account != null)
{
String[] allowedAccountType = new String[] { 'Craftsman' };
String selectedRecType = account.RecordType.Name;
if (allowedAccountType.contains(selectedRecType) == true)
{
List<Novalnet_Global_Configuration__c> novalnetConfigList = [Select Id,Product_Activation_Key__c,Payment_Access_Key__c from Novalnet_Global_Configuration__c where Account_Record_Type__c =: selectedRecType];
if (novalnetConfigList.isEmpty() == false)
{
Novalnet_Global_Configuration__c novalnetConfig = novalnetConfigList[0];
paymentAccessKey = novalnetConfig.Payment_Access_Key__c;
Map<String, Object> merchant = new Map<String, Object>();
merchant.put('signature', novalnetConfig.Product_Activation_Key__c);
Map<String, Object> address = new Map<String, Object>();
address.put('street', account.BillingStreet);
address.put('city', account.BillingCity);
address.put('zip', account.BillingPostalCode);
address.put('country_code', account.BillingCountry);
Map<String, Object> affiliate = new Map<String, Object>();
affiliate.put('first_name', account.Name);
affiliate.put('last_name', account.Name);
affiliate.put('email', account.Email__c);
affiliate.put('customer_no', account.Website);
affiliate.put('tel', account.Phone);
affiliate.put('account_holder', account.Name);
affiliate.put('iban', account.IBAN__c);
affiliate.put('address', address);
String language = UserInfo.getLanguage();
String[] languageAry = language.split('_');
Map<String, Object> custom = new Map<String, Object>();
custom.put('lang', languageAry[0].toUppercase());
Map<String, Object> requestData = new Map<String, Object>();
requestData.put('merchant', merchant);
requestData.put('affiliate', affiliate);
requestData.put('custom', custom);
List <String> result = getValidatedRequest(requestData);
jsonString = result[0];
System.debug('Result List 0:: '+result[0]);
System.debug('Result List 1::' +result[1]);
if(result[1] == '')
{
if(Trigger.isInsert) //Testclass
{
// ServerCallouts.getCalloutData(jsonString, paymentAccessKey);
}
//Servercallouts.getCalloutData(jsonString, paymentAccessKey);
System.debug('Server Callout');
}
else
{
account.addError('ErrorMessage ::: '+result[1]);
}
}
}
}
}
}
public static List<String> getValidatedRequest(Map<String, Object> requestData)
{
String[] mandatoryParams = new String []{'signature', 'first_name', 'last_name', 'email', 'street', 'city', 'zip', 'country_code'};
String errorMsg = '';
List<String> result = new List<String>();
//return result;
for(String category : requestData.KeySet())
{
Map<String, Object> requestDataObj = (Map<String, Object>)requestData.get(String.valueOf(category));
for(Object categoryKey : requestDataObj.KeySet())
{
if(categoryKey == 'address')
{
Map<String, Object> subCategory = (Map<String, Object>) requestDataObj.get(String.valueOf(categoryKey));
for(Object subCategoryKey : subCategory.KeySet()) {
Map<String, Object> subCategoryParam = (Map<String, Object>) requestDataObj.get(String.valueOf(categoryKey));
string paramValue = String.valueOf(subCategoryParam.get(String.valueOf(subCategoryKey)));
if(paramValue == '' || paramValue == null)
{
if(mandatoryParams.contains(String.valueOf(subCategoryKey)))
{
errorMsg = 'Required parameter - ' + subCategoryKey + ' in the category ' + categoryKey + ' not received. ' +'\n';
result.add(JSON.Serialize(requestData,true));
result.add(errorMsg);
return result;
}
subCategory.remove(String.valueOf(subCategoryKey));
}
requestDataObj.put(String.valueOf(categoryKey), subCategory);
}
}
else
{
string paramValue = String.valueOf(requestDataObj.get(String.valueOf(categoryKey)));
if(paramValue == '' || paramValue == null)
{
if(mandatoryParams.contains(String.valueOf(categoryKey)))
{
errorMsg = 'Required parameter - ' + categoryKey + ' in the category ' + category + ' not received. ' +'\n';
result.add(JSON.Serialize(requestData,true));
result.add(errorMsg);
return result;
}
requestDataObj.remove(String.valueOf(categoryKey));
}
}
}
requestData.put(category, requestDataObj);
}
result.add(JSON.Serialize(requestData,true));
result.add(errorMsg);
return result;
}
}
Test Class:
@isTest
private class AccountTriggerTest
{
@isTest
static void AccountTrigger()
{
Account acc = new Account(Name = 'Max', RecordTypeId = '0125g000000AMDLAA4',BillingStreet = 'Musterstr', BillingCity = 'Jaipur', BillingState = 'Raj', BillingPostalCode = '12345', BillingCountry = 'DE', Phone = '+49 089 123456', Fax = '+49 089 123456', Website = 'info@nova.com', Email__c = 'test@nova.com', IBAN__c = 'DE24300225090024114523523761956');
Insert acc;
acc = [select Id, Name,BillingStreet,BillingCity,Email__c,IBAN__c from Account where Id = :acc.Id];
String requestData = '{"merchant":{"signature":"7ibc7ob5|tu3454363345345674JEH3gNbeWJfIHah||567856|doU3HJVoyfsggm7MQ44qf7cpn7pc"},"customer":{"lang":"EN"},"affiliate":{"vat_id":"DE123453456","tel":"98744564563210","reference":"affiliateXXX","password":"Nova123123","mobile":"9874563210","login":"Nova","last_name":"Sample Acc","iban":null,"gender":"Female","first_name":"Sample Acc","fax":"+41054879","email":"test@nova.com","company_email":"info@nova.com","address":{"zip":"DE","street":"Musterstr","house_no":"2","country_code":"DE","company":"ABC GmbH","city":"Musterhausen"},"account_holder":"Sample Acc"}}';
String PaymentAccesskey = 'YTg3ZmY2NzlhMmYzZTcxZDkxODFhfgsdg4retget34trtrfdgdNjdiNzU0MjEyMmM=';
Account account = new Account( Name = 'Max' );
ServerCallouts.getCalloutData(requestData, PaymentAccesskey);
System.assertEquals(account.Name, 'Max');
}
}
trigger GenerateAffiliate on Account (before insert)
{
for(Account account: Trigger.new)
{
if(Trigger.isInsert)
{
String jsonString,paymentAccessKey;
if (account != null)
{
String[] allowedAccountType = new String[] { 'Craftsman' };
String selectedRecType = account.RecordType.Name;
if (allowedAccountType.contains(selectedRecType) == true)
{
List<Novalnet_Global_Configuration__c> novalnetConfigList = [Select Id,Product_Activation_Key__c,Payment_Access_Key__c from Novalnet_Global_Configuration__c where Account_Record_Type__c =: selectedRecType];
if (novalnetConfigList.isEmpty() == false)
{
Novalnet_Global_Configuration__c novalnetConfig = novalnetConfigList[0];
paymentAccessKey = novalnetConfig.Payment_Access_Key__c;
Map<String, Object> merchant = new Map<String, Object>();
merchant.put('signature', novalnetConfig.Product_Activation_Key__c);
Map<String, Object> address = new Map<String, Object>();
address.put('street', account.BillingStreet);
address.put('city', account.BillingCity);
address.put('zip', account.BillingPostalCode);
address.put('country_code', account.BillingCountry);
Map<String, Object> affiliate = new Map<String, Object>();
affiliate.put('first_name', account.Name);
affiliate.put('last_name', account.Name);
affiliate.put('email', account.Email__c);
affiliate.put('customer_no', account.Website);
affiliate.put('tel', account.Phone);
affiliate.put('account_holder', account.Name);
affiliate.put('iban', account.IBAN__c);
affiliate.put('address', address);
String language = UserInfo.getLanguage();
String[] languageAry = language.split('_');
Map<String, Object> custom = new Map<String, Object>();
custom.put('lang', languageAry[0].toUppercase());
Map<String, Object> requestData = new Map<String, Object>();
requestData.put('merchant', merchant);
requestData.put('affiliate', affiliate);
requestData.put('custom', custom);
List <String> result = getValidatedRequest(requestData);
jsonString = result[0];
System.debug('Result List 0:: '+result[0]);
System.debug('Result List 1::' +result[1]);
if(result[1] == '')
{
if(Trigger.isInsert) //Testclass
{
// ServerCallouts.getCalloutData(jsonString, paymentAccessKey);
}
//Servercallouts.getCalloutData(jsonString, paymentAccessKey);
System.debug('Server Callout');
}
else
{
account.addError('ErrorMessage ::: '+result[1]);
}
}
}
}
}
}
public static List<String> getValidatedRequest(Map<String, Object> requestData)
{
String[] mandatoryParams = new String []{'signature', 'first_name', 'last_name', 'email', 'street', 'city', 'zip', 'country_code'};
String errorMsg = '';
List<String> result = new List<String>();
//return result;
for(String category : requestData.KeySet())
{
Map<String, Object> requestDataObj = (Map<String, Object>)requestData.get(String.valueOf(category));
for(Object categoryKey : requestDataObj.KeySet())
{
if(categoryKey == 'address')
{
Map<String, Object> subCategory = (Map<String, Object>) requestDataObj.get(String.valueOf(categoryKey));
for(Object subCategoryKey : subCategory.KeySet()) {
Map<String, Object> subCategoryParam = (Map<String, Object>) requestDataObj.get(String.valueOf(categoryKey));
string paramValue = String.valueOf(subCategoryParam.get(String.valueOf(subCategoryKey)));
if(paramValue == '' || paramValue == null)
{
if(mandatoryParams.contains(String.valueOf(subCategoryKey)))
{
errorMsg = 'Required parameter - ' + subCategoryKey + ' in the category ' + categoryKey + ' not received. ' +'\n';
result.add(JSON.Serialize(requestData,true));
result.add(errorMsg);
return result;
}
subCategory.remove(String.valueOf(subCategoryKey));
}
requestDataObj.put(String.valueOf(categoryKey), subCategory);
}
}
else
{
string paramValue = String.valueOf(requestDataObj.get(String.valueOf(categoryKey)));
if(paramValue == '' || paramValue == null)
{
if(mandatoryParams.contains(String.valueOf(categoryKey)))
{
errorMsg = 'Required parameter - ' + categoryKey + ' in the category ' + category + ' not received. ' +'\n';
result.add(JSON.Serialize(requestData,true));
result.add(errorMsg);
return result;
}
requestDataObj.remove(String.valueOf(categoryKey));
}
}
}
requestData.put(category, requestDataObj);
}
result.add(JSON.Serialize(requestData,true));
result.add(errorMsg);
return result;
}
}
Test Class:
@isTest
private class AccountTriggerTest
{
@isTest
static void AccountTrigger()
{
Account acc = new Account(Name = 'Max', RecordTypeId = '0125g000000AMDLAA4',BillingStreet = 'Musterstr', BillingCity = 'Jaipur', BillingState = 'Raj', BillingPostalCode = '12345', BillingCountry = 'DE', Phone = '+49 089 123456', Fax = '+49 089 123456', Website = 'info@nova.com', Email__c = 'test@nova.com', IBAN__c = 'DE24300225090024114523523761956');
Insert acc;
acc = [select Id, Name,BillingStreet,BillingCity,Email__c,IBAN__c from Account where Id = :acc.Id];
String requestData = '{"merchant":{"signature":"7ibc7ob5|tu3454363345345674JEH3gNbeWJfIHah||567856|doU3HJVoyfsggm7MQ44qf7cpn7pc"},"customer":{"lang":"EN"},"affiliate":{"vat_id":"DE123453456","tel":"98744564563210","reference":"affiliateXXX","password":"Nova123123","mobile":"9874563210","login":"Nova","last_name":"Sample Acc","iban":null,"gender":"Female","first_name":"Sample Acc","fax":"+41054879","email":"test@nova.com","company_email":"info@nova.com","address":{"zip":"DE","street":"Musterstr","house_no":"2","country_code":"DE","company":"ABC GmbH","city":"Musterhausen"},"account_holder":"Sample Acc"}}';
String PaymentAccesskey = 'YTg3ZmY2NzlhMmYzZTcxZDkxODFhfgsdg4retget34trtrfdgdNjdiNzU0MjEyMmM=';
Account account = new Account( Name = 'Max' );
ServerCallouts.getCalloutData(requestData, PaymentAccesskey);
System.assertEquals(account.Name, 'Max');
}
}
The code provided in question does not highlight the uncovered lines. Since this code is huge and requires an understanding of your implementation, it might not be possible to provide exact edit suggestions. However, the below articles give a good insight into how coverage can be improved
https://salesforce.stackexchange.com/questions/244794/how-do-i-increase-my-code-coverage-or-why-cant-i-cover-these-lines
Code Coverage Best Practices
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_code_coverage_best_pract.htm
Checking Code Coverage
https://help.salesforce.com/articleView?id=code_dev_console_tests_coverage.htm&type=5
Deployment related code coverage issues
https://help.salesforce.com/articleView?id=000335222&type=1&mode=1
The developer community recommends providing any attempts/code you've started, any errors you're getting, or where exactly you're struggling in achieving this while posting a question.
Hope this helps you. Please mark this answer as best if this information is useful. Thank you
The above test class provide less code coverage.Please, find screenshot.