You need to sign in to do that
Don't have an account?
aamir arshad
help me to test class code coverage please
public with sharing class ProductController {
@AuraEnabled(cacheable=true)
public static List<Product2Wrapper> getAllProduct2(){
Map<String, Object> r = new Map<String, Object> ();
List<Product2Wrapper> proWrapperList = new List<Product2Wrapper>();
String qry = 'SELECT Id, Name, Product2Id, Product2.Name ,Product2.Description, Product2.nodhom__Name_with_Code_Query__c, Product2.ProductCode, UnitPrice FROM PricebookEntry where IsActive = TRUE';
qry += ' order by Product2.ProductCode ';
for(PricebookEntry pro : DataBase.query(qry)){
Product2Wrapper proWrapper = new Product2Wrapper();
proWrapper.productId = pro.Product2Id;
proWrapper.Name = pro.Product2.Name;
proWrapper.NameWithCodeQuery = pro.Product2.nodhom__Name_with_Code_Query__c;
proWrapper.ProductCode = pro.Product2.ProductCode;
proWrapper.Description = pro.Product2.Description;
proWrapper.unit = pro.UnitPrice;
proWrapperList.add(proWrapper);
}
return proWrapperList;
}
@AuraEnabled(cacheable=true)
public static List<Product2Wrapper> getAllProduct(Integer pageSize,Integer pageNumber){
Map<String, Object> r = new Map<String, Object> ();
Integer offset = (pageNumber - 1) * pageSize;
Integer totalRecords = [SELECT COUNT() FROM Product2];
Integer recordEnd = pageSize * pageNumber;
List<Product2Wrapper> proWrapperList = new List<Product2Wrapper>();
String qry = 'SELECT Id, Name, ProductCode, Net_Price__c, Standard_Margin__c, nodhom__Sales_Price__c, Description, nodhom__Name_with_Code_Query__c,Category_1__c, Category_2__c FROM Product2 where IsActive = TRUE LIMIT :pageSize OFFSET :offset';
for(Product2 pro : DataBase.query(qry)){
Product2Wrapper proWrapper = new Product2Wrapper();
proWrapper.productId = pro.Id;
proWrapper.Name = pro.Name;
proWrapper.NameWithCodeQuery = pro.nodhom__Name_with_Code_Query__c;
proWrapper.ProductCode = pro.ProductCode;
proWrapper.Description = pro.Description;
Decimal netPrice;
if(pro.Net_Price__c != null){
proWrapper.netPrice = pro.Net_Price__c;
netPrice = pro.Net_Price__c;
}else{
proWrapper.netPrice = 0;
netPrice = 0;
}
Decimal margin;
if(pro.Standard_Margin__c != null){
proWrapper.margin = pro.Standard_Margin__c;
margin = pro.Standard_Margin__c;
}else{
proWrapper.margin = 0;
margin = 0;
}
proWrapper.unitPrice = netPrice+(netPrice*margin/100);
proWrapper.totalAmount = 0;
proWrapper.isChecked = false;
proWrapper.productCategory1 = String.isBlank(pro.Category_1__c)?'':pro.Category_1__c;
proWrapper.productCategory2 = String.isBlank(pro.Category_2__c)?'':pro.Category_2__c;
proWrapper.pageNumber = pageNumber;
proWrapper.pageSize = pageSize;
proWrapper.recordStart = offset + 1;
proWrapper.recordEnd = totalRecords >= recordEnd ? recordEnd : totalRecords;
proWrapper.totalRecords = totalRecords;
proWrapperList.add(proWrapper);
}
return proWrapperList;
}
@AuraEnabled
public static List<Map<String, String>> getPickListValues(String objApiName, String fieldName) {
List < Map < String, String >> options = new List < Map < String, String >> ();
Map < String, Schema.SObjectType > objGlobalMap = Schema.getGlobalDescribe();
Schema.SObjectType objType = Schema.getGlobalDescribe().get(objApiName);
if (objType == null) {
return options;
}
Schema.DescribeSObjectResult sobjectDescribe = objType.getDescribe();
Map < String, Schema.SObjectField > fieldMap = sobjectDescribe.fields.getMap();
if (fieldMap.get(fieldName) == null) {
return options;
}
List < Schema.PicklistEntry > pickListValues = fieldMap.get(fieldName).getDescribe().getPickListValues();
for (Schema.PicklistEntry f: pickListValues) {
Map < String, String > values = new Map < String, String > {
'label' => f.getLabel(),
'value' => f.getValue()
};
options.add(values);
}
return options;
}
@AuraEnabled
public static void saveAllProducts(String recordId,String selectedProducts){
List<Product2Wrapper> selectedProductsList = (List<Product2Wrapper>) JSON.deserialize(
selectedProducts,
List<Product2Wrapper>.class
);
List<Deal_Line_Item__c> dealLineList = new List<Deal_Line_Item__c>();
for(Product2Wrapper pro : selectedProductsList){
Deal_Line_Item__c dlines = new Deal_Line_Item__c();
dlines.Deal_No__c = recordId;
dlines.Margin__c = pro.margin;
dlines.Quantity__c = String.valueOf(pro.quantity);
dlines.Net_Price__c = pro.netPrice;
dlines.Item__c = pro.productId;
dealLineList.add(dlines);
}
try{
if(!dealLineList.isEmpty())
insert dealLineList;
}catch(Exception e){
system.debug(e);
}
}
public class Product2Wrapper {
@AuraEnabled
public Id productId{get;set;}
@AuraEnabled
public String Name{get;set;}
@AuraEnabled
public String NameWithCodeQuery{get;set;}
@AuraEnabled
public String ProductCode{get;set;}
@AuraEnabled
public String Description{get;set;}
@AuraEnabled
public Decimal unit{get;set;}
@AuraEnabled
public Decimal netPrice{get;set;}
@AuraEnabled
public Decimal margin{get;set;}
@AuraEnabled
public Decimal unitPrice{get;set;}
@AuraEnabled
public Decimal totalAmount{get;set;}
@AuraEnabled
public Boolean isChecked{get;set;}
@AuraEnabled
public String productCategory1{get;set;}
@AuraEnabled
public String productCategory2{get;set;}
@AuraEnabled
public Decimal quantity{get;set;}
@AuraEnabled
public Decimal recordStart{get;set;}
@AuraEnabled
public Decimal pageNumber{get;set;}
@AuraEnabled
public Decimal totalRecords{get;set;}
@AuraEnabled
public Decimal recordEnd{get;set;}
@AuraEnabled
public Decimal pageSize{get;set;}
}
}
@AuraEnabled(cacheable=true)
public static List<Product2Wrapper> getAllProduct2(){
Map<String, Object> r = new Map<String, Object> ();
List<Product2Wrapper> proWrapperList = new List<Product2Wrapper>();
String qry = 'SELECT Id, Name, Product2Id, Product2.Name ,Product2.Description, Product2.nodhom__Name_with_Code_Query__c, Product2.ProductCode, UnitPrice FROM PricebookEntry where IsActive = TRUE';
qry += ' order by Product2.ProductCode ';
for(PricebookEntry pro : DataBase.query(qry)){
Product2Wrapper proWrapper = new Product2Wrapper();
proWrapper.productId = pro.Product2Id;
proWrapper.Name = pro.Product2.Name;
proWrapper.NameWithCodeQuery = pro.Product2.nodhom__Name_with_Code_Query__c;
proWrapper.ProductCode = pro.Product2.ProductCode;
proWrapper.Description = pro.Product2.Description;
proWrapper.unit = pro.UnitPrice;
proWrapperList.add(proWrapper);
}
return proWrapperList;
}
@AuraEnabled(cacheable=true)
public static List<Product2Wrapper> getAllProduct(Integer pageSize,Integer pageNumber){
Map<String, Object> r = new Map<String, Object> ();
Integer offset = (pageNumber - 1) * pageSize;
Integer totalRecords = [SELECT COUNT() FROM Product2];
Integer recordEnd = pageSize * pageNumber;
List<Product2Wrapper> proWrapperList = new List<Product2Wrapper>();
String qry = 'SELECT Id, Name, ProductCode, Net_Price__c, Standard_Margin__c, nodhom__Sales_Price__c, Description, nodhom__Name_with_Code_Query__c,Category_1__c, Category_2__c FROM Product2 where IsActive = TRUE LIMIT :pageSize OFFSET :offset';
for(Product2 pro : DataBase.query(qry)){
Product2Wrapper proWrapper = new Product2Wrapper();
proWrapper.productId = pro.Id;
proWrapper.Name = pro.Name;
proWrapper.NameWithCodeQuery = pro.nodhom__Name_with_Code_Query__c;
proWrapper.ProductCode = pro.ProductCode;
proWrapper.Description = pro.Description;
Decimal netPrice;
if(pro.Net_Price__c != null){
proWrapper.netPrice = pro.Net_Price__c;
netPrice = pro.Net_Price__c;
}else{
proWrapper.netPrice = 0;
netPrice = 0;
}
Decimal margin;
if(pro.Standard_Margin__c != null){
proWrapper.margin = pro.Standard_Margin__c;
margin = pro.Standard_Margin__c;
}else{
proWrapper.margin = 0;
margin = 0;
}
proWrapper.unitPrice = netPrice+(netPrice*margin/100);
proWrapper.totalAmount = 0;
proWrapper.isChecked = false;
proWrapper.productCategory1 = String.isBlank(pro.Category_1__c)?'':pro.Category_1__c;
proWrapper.productCategory2 = String.isBlank(pro.Category_2__c)?'':pro.Category_2__c;
proWrapper.pageNumber = pageNumber;
proWrapper.pageSize = pageSize;
proWrapper.recordStart = offset + 1;
proWrapper.recordEnd = totalRecords >= recordEnd ? recordEnd : totalRecords;
proWrapper.totalRecords = totalRecords;
proWrapperList.add(proWrapper);
}
return proWrapperList;
}
@AuraEnabled
public static List<Map<String, String>> getPickListValues(String objApiName, String fieldName) {
List < Map < String, String >> options = new List < Map < String, String >> ();
Map < String, Schema.SObjectType > objGlobalMap = Schema.getGlobalDescribe();
Schema.SObjectType objType = Schema.getGlobalDescribe().get(objApiName);
if (objType == null) {
return options;
}
Schema.DescribeSObjectResult sobjectDescribe = objType.getDescribe();
Map < String, Schema.SObjectField > fieldMap = sobjectDescribe.fields.getMap();
if (fieldMap.get(fieldName) == null) {
return options;
}
List < Schema.PicklistEntry > pickListValues = fieldMap.get(fieldName).getDescribe().getPickListValues();
for (Schema.PicklistEntry f: pickListValues) {
Map < String, String > values = new Map < String, String > {
'label' => f.getLabel(),
'value' => f.getValue()
};
options.add(values);
}
return options;
}
@AuraEnabled
public static void saveAllProducts(String recordId,String selectedProducts){
List<Product2Wrapper> selectedProductsList = (List<Product2Wrapper>) JSON.deserialize(
selectedProducts,
List<Product2Wrapper>.class
);
List<Deal_Line_Item__c> dealLineList = new List<Deal_Line_Item__c>();
for(Product2Wrapper pro : selectedProductsList){
Deal_Line_Item__c dlines = new Deal_Line_Item__c();
dlines.Deal_No__c = recordId;
dlines.Margin__c = pro.margin;
dlines.Quantity__c = String.valueOf(pro.quantity);
dlines.Net_Price__c = pro.netPrice;
dlines.Item__c = pro.productId;
dealLineList.add(dlines);
}
try{
if(!dealLineList.isEmpty())
insert dealLineList;
}catch(Exception e){
system.debug(e);
}
}
public class Product2Wrapper {
@AuraEnabled
public Id productId{get;set;}
@AuraEnabled
public String Name{get;set;}
@AuraEnabled
public String NameWithCodeQuery{get;set;}
@AuraEnabled
public String ProductCode{get;set;}
@AuraEnabled
public String Description{get;set;}
@AuraEnabled
public Decimal unit{get;set;}
@AuraEnabled
public Decimal netPrice{get;set;}
@AuraEnabled
public Decimal margin{get;set;}
@AuraEnabled
public Decimal unitPrice{get;set;}
@AuraEnabled
public Decimal totalAmount{get;set;}
@AuraEnabled
public Boolean isChecked{get;set;}
@AuraEnabled
public String productCategory1{get;set;}
@AuraEnabled
public String productCategory2{get;set;}
@AuraEnabled
public Decimal quantity{get;set;}
@AuraEnabled
public Decimal recordStart{get;set;}
@AuraEnabled
public Decimal pageNumber{get;set;}
@AuraEnabled
public Decimal totalRecords{get;set;}
@AuraEnabled
public Decimal recordEnd{get;set;}
@AuraEnabled
public Decimal pageSize{get;set;}
}
}
To test the "ProductController" Apex class, you can write an Apex test class that covers various scenarios and ensures that the methods are working correctly. Below is an example of how you can create a test class for "ProductController" to achieve code coverage: In the test methods, you should create appropriate test data and call the methods from the "ProductController" class with that data. After calling the methods, use assertions to verify that the methods return the expected results or perform the intended actions.
Additionally, in your test class, you may need to use the Test.startTest() and Test.stopTest() methods to ensure proper handling of asynchronous and cacheable actions, like the @AuraEnabled(cacheable=true) methods in the "ProductController" class.
Hope this helps !
Thank you.