You need to sign in to do that
Don't have an account?
Rohan Telang
I have to write the Test class for the below Class. I don't understand how to write it.Please help
@RestResource(urlMapping='/ProductForWebTeam/*')
global class EGS_ProductForWebTeam {
public class ProductData {
public String sf_product_id;
public String name;
public String type;
public Decimal regular_price;
public String description;
public String short_description;
public List<Categories> categories;
public List<Attribute> attributes;
public List<Variation> variations;
}
public class Categories {
public Integer id;
public String name;
}
public class Attributes_Z {
public Integer id;
public String option;
}
public class Attribute {
public Integer id ;
public Boolean variation;
public Boolean visible;
public List<String> options;
}
/* public class Option{
public Integer id;
public String month;
}*/
public class Variation {
public Decimal regular_price;
public List<Attributes_Z> attributes;
}
@httpGet
global static void sendProductData() {
try {
List<ProductData> productWrapperList = new List<ProductData>();
Map<Id, Product2> productMap = new Map<Id, Product2>([SELECT Id, Name, Description, Product_Category__c, Product_Subcategory__c FROM Product2 WHERE Is_sent_to_web__c = false LIMIT 1]);
System.debug(productMap);
Pricebook2 priceBook = [SELECT Id FROM Pricebook2 WHERE Id = '01s0900000DSd7HAAT'];
List<PriceBookEntry> priceBookEntryList = [SELECT Product2Id, UnitPrice
FROM PriceBookEntry
WHERE pricebook2id = :priceBook.Id AND Product2Id =: productMap.keySet()];
Map<Id, PriceBookEntry> MapOfProductToPBE = new Map<Id, PriceBookEntry>();
for(PriceBookEntry pbe : priceBookEntryList)
{
MapOfProductToPBE.put(pbe.Product2Id, pbe);
}
System.debug(MapOfProductToPBE);
List<Product2> productListToBeUpdated = new List<Product2>();
for(Product2 product : productMap.values()) {
ProductData productData = new ProductData();
productData.sf_product_id = product.Id;
productData.name = product.Name;
productData.type = 'variable';
// if(MapOfProductToPBE.get(product.id).Product2id == product.id && MapOfProductToPBE.get(product.id).UnitPrice != null){
productData.regular_price = 100;
// }else{
// productData.regular_price = 0;
// }
productData.description = product.Description;
List<categories> categoryList = new List<categories>();
Categories c = new Categories();
if(product.Product_Category__c == 'Bundle') {
c.id = 60;
c.name = product.Product_Category__c;
} else if(product.Product_Category__c == 'Additional module') {
c.id = 61;
c.name = product.Product_Category__c;
} else if(product.Product_Category__c == 'Stand Alone') {
c.id = 62;
c.name = product.Product_Category__c;
}
categoryList.add(c);
productData.categories = categoryList;
//Code for attribute
List<Attribute> attributeList = new List<Attribute>();
Attribute varAttr = New Attribute();
varAttr.id = 2;
varAttr.variation = true;
varAttr.visible = true;
List<String> OptionList = new List<String>();
OptionList.add('3 Months');
OptionList.add('6 Months');
OptionList.add('1 Year');
varAttr.options = OptionList;
attributeList.add(varAttr);
productData.attributes = attributeList;
List<variation> VariationList = new List<variation>();
for(String i : OptionList)
{
variation v = new variation();
v.regular_price = 100;
List<Attributes_Z> AttrList = new List<Attributes_Z>();
Attributes_Z attrInstance = new Attributes_Z();
attrInstance.id = 2;
attrInstance.option = i;
AttrList.add(attrInstance);
v.attributes = AttrList;
VariationList.add(v);
}
productData.variations = VariationList;
product.Is_sent_to_web__c = true;
productListToBeUpdated.add(product);
productWrapperList.add(productData);
}
// update productListToBeUpdated;
System.debug(JSON.serialize(productWrapperList));
RestResponse res = RestContext.response;
res.statusCode = 200;
res.responseBody = Blob.valueOf(JSON.serialize(productWrapperList));
} catch(Exception e) {
RestResponse res = RestContext.response;
res.statusCode = 404;
res.responseBody = Blob.valueOf('Something went wrong! Error Message : '+e.getMessage()+' '+'line number '+e.getLineNumber()+ ' '+e.getCause());
}
}
}
global class EGS_ProductForWebTeam {
public class ProductData {
public String sf_product_id;
public String name;
public String type;
public Decimal regular_price;
public String description;
public String short_description;
public List<Categories> categories;
public List<Attribute> attributes;
public List<Variation> variations;
}
public class Categories {
public Integer id;
public String name;
}
public class Attributes_Z {
public Integer id;
public String option;
}
public class Attribute {
public Integer id ;
public Boolean variation;
public Boolean visible;
public List<String> options;
}
/* public class Option{
public Integer id;
public String month;
}*/
public class Variation {
public Decimal regular_price;
public List<Attributes_Z> attributes;
}
@httpGet
global static void sendProductData() {
try {
List<ProductData> productWrapperList = new List<ProductData>();
Map<Id, Product2> productMap = new Map<Id, Product2>([SELECT Id, Name, Description, Product_Category__c, Product_Subcategory__c FROM Product2 WHERE Is_sent_to_web__c = false LIMIT 1]);
System.debug(productMap);
Pricebook2 priceBook = [SELECT Id FROM Pricebook2 WHERE Id = '01s0900000DSd7HAAT'];
List<PriceBookEntry> priceBookEntryList = [SELECT Product2Id, UnitPrice
FROM PriceBookEntry
WHERE pricebook2id = :priceBook.Id AND Product2Id =: productMap.keySet()];
Map<Id, PriceBookEntry> MapOfProductToPBE = new Map<Id, PriceBookEntry>();
for(PriceBookEntry pbe : priceBookEntryList)
{
MapOfProductToPBE.put(pbe.Product2Id, pbe);
}
System.debug(MapOfProductToPBE);
List<Product2> productListToBeUpdated = new List<Product2>();
for(Product2 product : productMap.values()) {
ProductData productData = new ProductData();
productData.sf_product_id = product.Id;
productData.name = product.Name;
productData.type = 'variable';
// if(MapOfProductToPBE.get(product.id).Product2id == product.id && MapOfProductToPBE.get(product.id).UnitPrice != null){
productData.regular_price = 100;
// }else{
// productData.regular_price = 0;
// }
productData.description = product.Description;
List<categories> categoryList = new List<categories>();
Categories c = new Categories();
if(product.Product_Category__c == 'Bundle') {
c.id = 60;
c.name = product.Product_Category__c;
} else if(product.Product_Category__c == 'Additional module') {
c.id = 61;
c.name = product.Product_Category__c;
} else if(product.Product_Category__c == 'Stand Alone') {
c.id = 62;
c.name = product.Product_Category__c;
}
categoryList.add(c);
productData.categories = categoryList;
//Code for attribute
List<Attribute> attributeList = new List<Attribute>();
Attribute varAttr = New Attribute();
varAttr.id = 2;
varAttr.variation = true;
varAttr.visible = true;
List<String> OptionList = new List<String>();
OptionList.add('3 Months');
OptionList.add('6 Months');
OptionList.add('1 Year');
varAttr.options = OptionList;
attributeList.add(varAttr);
productData.attributes = attributeList;
List<variation> VariationList = new List<variation>();
for(String i : OptionList)
{
variation v = new variation();
v.regular_price = 100;
List<Attributes_Z> AttrList = new List<Attributes_Z>();
Attributes_Z attrInstance = new Attributes_Z();
attrInstance.id = 2;
attrInstance.option = i;
AttrList.add(attrInstance);
v.attributes = AttrList;
VariationList.add(v);
}
productData.variations = VariationList;
product.Is_sent_to_web__c = true;
productListToBeUpdated.add(product);
productWrapperList.add(productData);
}
// update productListToBeUpdated;
System.debug(JSON.serialize(productWrapperList));
RestResponse res = RestContext.response;
res.statusCode = 200;
res.responseBody = Blob.valueOf(JSON.serialize(productWrapperList));
} catch(Exception e) {
RestResponse res = RestContext.response;
res.statusCode = 404;
res.responseBody = Blob.valueOf('Something went wrong! Error Message : '+e.getMessage()+' '+'line number '+e.getLineNumber()+ ' '+e.getCause());
}
}
}
You can get understanding about writing Test for Apex Rest Class Class fro below link
https://trailhead.salesforce.com/en/content/learn/modules/apex_integration_services/apex_integration_webservices
If you face any specific challange while writing do let me know.
Thanks!