You need to sign in to do that
Don't have an account?
sumit d
Test class for triggerHelper covering error message
Hi All,
I have a trigger helper class for trigger in which i am trying to do a Test use case in test class given below:-
Test case:-
1. Create an Account.
2. Create an Opportunity for the above Account.
3. Add one Hardware Products on Opportunity
4. Add one service product which does not have a relationship with this hardware product in SA Junction data
5. Verify that the validation message is shown.
My helper class is given below:-
public without sharing class OpportunityLineItemTriggerHelper {
public static List<OpportunityLineItem> newOpportunityLineItem = new List<OpportunityLineItem>();
public static List<OpportunityLineItem> oldOpportunityLineItem = new List<OpportunityLineItem>();
public static Map<Id, OpportunityLineItem> newMapOpportunityLineItem = new Map<Id, OpportunityLineItem>();
public static Map<Id, OpportunityLineItem> oldMapOpportunityLineItem = new Map<Id, OpportunityLineItem>();
public static void validateServiceAgreementProduct(){
Map<Id, Product2> mapProductIdToProduct = new Map<Id, Product2>(
[ SELECT Id, Name, Family
FROM Product2
WHERE Family LIKE 'SA - %'
OR Family LIKE 'Service - %'
]
);
Set<Id> parentOppIds = new Set<Id>();
Set<Id> serviceProductIds = new Set<Id>();
List<OpportunityLineItem> serviceProductLines = new List<OpportunityLineItem>();
for( OpportunityLineItem oppLine : newOpportunityLineItem ){
if( mapProductIdToProduct.containsKey( oppLine.Product2Id ) ){
parentOppIds.add( oppLine.opportunityId );
serviceProductLines.add( oppLine );
serviceProductIds.add( oppLine.Product2Id );
}
}
Map<Id, Opportunity> oppMap = new Map<Id, Opportunity>( [ SELECT Id, Name,
( SELECT Id, Name, Product2Id
FROM OpportunityLineItems
Where Product2Id not in :serviceProductIds
)
FROM Opportunity
WHERE Id IN: parentOppIds ]
);
Map<Id, Set<Id>> mapOppIdToHardwareProductIds = new Map<Id, Set<Id>>();
for( Opportunity opp : oppMap.values() ) {
Set<Id> setHarwareProductIds = new Set<Id>();
for( OpportunityLineItem opli : opp.OpportunityLineItems ) {
setHarwareProductIds.add( opli.Product2Id );
}
mapOppIdToHardwareProductIds.put( opp.Id, setHarwareProductIds );
}
Map<Id, Set<Id>> mapServicesProductIDToHardwareProductIds = new Map<Id, Set<Id>>();
for( SA_Junction__c sJunction : [ SELECT Id, Hardware_Product__c,
Services_Product__c
FROM SA_Junction__c
Where Services_Product__c in :serviceProductIds
]
){
Set<Id> hardwareProductIds = mapServicesProductIDToHardwareProductIds.get( sJunction.Services_Product__c );
hardwareProductIds = hardwareProductIds == null ? new Set<Id>() : hardwareProductIds;
hardwareProductIds.add( sJunction.Hardware_Product__c );
mapServicesProductIDToHardwareProductIds.put( sJunction.Services_Product__c, hardwareProductIds );
}
for( OpportunityLineItem oppLine : serviceProductLines ){
Set<Id> actualHardwareProductIdsOnOpp = mapOppIdToHardwareProductIds.get( oppLine.OpportunityId );
Set<Id> expectedHardwareProductIds = mapServicesProductIDToHardwareProductIds.get( oppLine.Product2Id );
if( expectedHardwareProductIds != null && expectedHardwareProductIds.size() > 0
&& actualHardwareProductIdsOnOpp != null && actualHardwareProductIdsOnOpp.size() > 0 ) {
Boolean hasExpectedHardware = false;
for( Id hardwareProdId : actualHardwareProductIdsOnOpp ) {
if( expectedHardwareProductIds.contains( hardwareProdId )) {
hasExpectedHardware = true;
break;
}
}
if( !hasExpectedHardware ) {
oppLine.addError('You need to select the correct agreement based on hardware products.');
}
}
}
}
}
I could not able to cover the bold part.Can anyone help me how to write test class for it?
I have a trigger helper class for trigger in which i am trying to do a Test use case in test class given below:-
Test case:-
1. Create an Account.
2. Create an Opportunity for the above Account.
3. Add one Hardware Products on Opportunity
4. Add one service product which does not have a relationship with this hardware product in SA Junction data
5. Verify that the validation message is shown.
My helper class is given below:-
public without sharing class OpportunityLineItemTriggerHelper {
public static List<OpportunityLineItem> newOpportunityLineItem = new List<OpportunityLineItem>();
public static List<OpportunityLineItem> oldOpportunityLineItem = new List<OpportunityLineItem>();
public static Map<Id, OpportunityLineItem> newMapOpportunityLineItem = new Map<Id, OpportunityLineItem>();
public static Map<Id, OpportunityLineItem> oldMapOpportunityLineItem = new Map<Id, OpportunityLineItem>();
public static void validateServiceAgreementProduct(){
Map<Id, Product2> mapProductIdToProduct = new Map<Id, Product2>(
[ SELECT Id, Name, Family
FROM Product2
WHERE Family LIKE 'SA - %'
OR Family LIKE 'Service - %'
]
);
Set<Id> parentOppIds = new Set<Id>();
Set<Id> serviceProductIds = new Set<Id>();
List<OpportunityLineItem> serviceProductLines = new List<OpportunityLineItem>();
for( OpportunityLineItem oppLine : newOpportunityLineItem ){
if( mapProductIdToProduct.containsKey( oppLine.Product2Id ) ){
parentOppIds.add( oppLine.opportunityId );
serviceProductLines.add( oppLine );
serviceProductIds.add( oppLine.Product2Id );
}
}
Map<Id, Opportunity> oppMap = new Map<Id, Opportunity>( [ SELECT Id, Name,
( SELECT Id, Name, Product2Id
FROM OpportunityLineItems
Where Product2Id not in :serviceProductIds
)
FROM Opportunity
WHERE Id IN: parentOppIds ]
);
Map<Id, Set<Id>> mapOppIdToHardwareProductIds = new Map<Id, Set<Id>>();
for( Opportunity opp : oppMap.values() ) {
Set<Id> setHarwareProductIds = new Set<Id>();
for( OpportunityLineItem opli : opp.OpportunityLineItems ) {
setHarwareProductIds.add( opli.Product2Id );
}
mapOppIdToHardwareProductIds.put( opp.Id, setHarwareProductIds );
}
Map<Id, Set<Id>> mapServicesProductIDToHardwareProductIds = new Map<Id, Set<Id>>();
for( SA_Junction__c sJunction : [ SELECT Id, Hardware_Product__c,
Services_Product__c
FROM SA_Junction__c
Where Services_Product__c in :serviceProductIds
]
){
Set<Id> hardwareProductIds = mapServicesProductIDToHardwareProductIds.get( sJunction.Services_Product__c );
hardwareProductIds = hardwareProductIds == null ? new Set<Id>() : hardwareProductIds;
hardwareProductIds.add( sJunction.Hardware_Product__c );
mapServicesProductIDToHardwareProductIds.put( sJunction.Services_Product__c, hardwareProductIds );
}
for( OpportunityLineItem oppLine : serviceProductLines ){
Set<Id> actualHardwareProductIdsOnOpp = mapOppIdToHardwareProductIds.get( oppLine.OpportunityId );
Set<Id> expectedHardwareProductIds = mapServicesProductIDToHardwareProductIds.get( oppLine.Product2Id );
if( expectedHardwareProductIds != null && expectedHardwareProductIds.size() > 0
&& actualHardwareProductIdsOnOpp != null && actualHardwareProductIdsOnOpp.size() > 0 ) {
Boolean hasExpectedHardware = false;
for( Id hardwareProdId : actualHardwareProductIdsOnOpp ) {
if( expectedHardwareProductIds.contains( hardwareProdId )) {
hasExpectedHardware = true;
break;
}
}
if( !hasExpectedHardware ) {
oppLine.addError('You need to select the correct agreement based on hardware products.');
}
}
}
}
}
I could not able to cover the bold part.Can anyone help me how to write test class for it?