Skip to content

Commit d252a50

Browse files
author
Tina Kung
committed
Added test coverage
1 parent 84b497a commit d252a50

File tree

5 files changed

+378
-0
lines changed

5 files changed

+378
-0
lines changed
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
@isTest
2+
private class CustomProductSelectorControllerTest {
3+
4+
@isTest
5+
private static void negativeTest() {
6+
CustomProductSelectorController con;
7+
con = new CustomProductSelectorController( new ApexPages.StandardController( new zqu__Quote__c() ) );
8+
System.assert( Apexpages.hasMessages() );
9+
}
10+
11+
@isTest
12+
private static void selectProduct() {
13+
14+
zqu__ZProduct__c product = TestDataSetup.prepareProductCatalog();
15+
Id productId = product.Id;
16+
17+
Account acct = new Account( Name = 'TestAccount' );
18+
insert acct;
19+
Opportunity oppty = new Opportunity( Name = 'TestOppty', AccountId = acct.Id, StageName = 'Prospect', CloseDate = Date.parse( '12/8/2013' ) );
20+
insert oppty;
21+
zqu__Quote__c quote = new zqu__Quote__c( Name = 'TestQuote', zqu__Opportunity__c = oppty.Id, zqu__Currency__c = 'USD', zqu__StartDate__c = Date.today() );
22+
insert quote;
23+
24+
CustomProductSelectorController con = new CustomProductSelectorController( new ApexPages.StandardController( quote ) );
25+
26+
System.assert( con.selectedProductShare != null && con.selectedProductShare.dataObject == null );
27+
System.assert( con.selectedRatePlanShare != null && con.selectedRatePlanShare.dataObject == null );
28+
29+
zqu.ZComponentDataShare productShare = new zqu.zComponentDataShare();
30+
zqu.ZComponentDataShare ratePlanShare = new zqu.zComponentDataShare();
31+
32+
List<zqu__ProductRatePlan__c> ratePlans = [ SELECT id FROM zqu__ProductRatePlan__c WHERE zqu__zProduct__c = :productId LIMIT 1 ];
33+
zqu__ProductRatePlan__c ratePlan = ratePlans.get(0);
34+
35+
productShare.dataObject = String.valueOf( productId );
36+
ratePlanShare.dataObject = String.valueOf( ratePlan.Id );
37+
38+
con.selectedProductShare = productShare;
39+
con.selectedRatePlanShare = ratePlanShare;
40+
41+
productShare = con.selectedProductShare;
42+
ratePlanShare = con.selectedRatePlanShare;
43+
44+
System.debug( 'productShare = ' + productShare );
45+
System.debug( 'ratePlanShare = ' + ratePlanShare );
46+
47+
System.assertEquals( true, con.getIsRenderRatePlan() );
48+
System.assertEquals( false, con.getIsRenderChargeGroup() );
49+
System.assertEquals( false, con.getNoMatchProduct() );
50+
System.assertEquals( '/' + quote.Id, con.navigateBack().getUrl() );
51+
52+
con.addNewChargeGroupToQuote();
53+
54+
con.currentChargeGroupId = con.chargeGroup.groupId;
55+
56+
con.editChargeGroup();
57+
System.assert( true == con.editMode );
58+
59+
con.saveChargeGroup();
60+
61+
con.deleteChargeGroup();
62+
63+
con.cancelEditing();
64+
System.assert( false == con.editMode );
65+
66+
con.toSelectNewProductsView();
67+
System.assert( con.selectedProductShare.dataObject == null );
68+
System.assert( con.selectedRatePlanShare.dataObject == null );
69+
70+
}
71+
72+
73+
74+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<ApexClass xmlns="http://soap.sforce.com/2006/04/metadata">
3+
<apiVersion>23.0</apiVersion>
4+
<packageVersions>
5+
<majorNumber>5</majorNumber>
6+
<minorNumber>3</minorNumber>
7+
<namespace>zqu</namespace>
8+
</packageVersions>
9+
<status>Active</status>
10+
</ApexClass>
Lines changed: 282 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,282 @@
1+
@isTest
2+
public with sharing class TestDataSetup {
3+
4+
public static final Date DEFAULT_EFFECTIVE_START_DATE = Date.today();
5+
public static final Date DEFAULT_EFFECTIVE_END_DATE = Date.today().addYears(1);
6+
public static final String DEFAULT_CURRENCY = 'USD';
7+
8+
9+
public static final String MODEL_F = 'Flat Fee Pricing';
10+
public static final String MODEL_P = 'Per Unit Pricing';
11+
public static final String MODEL_V = 'Volume Pricing';
12+
public static final String MODEL_T = 'Tiered Pricing';
13+
public static final String MODEL_O = 'Overage Pricing';
14+
public static final String MODEL_TO = 'Tiered with Overage Pricing';
15+
public static final String MODEL_FA = 'Discount-Fixed Amount';
16+
public static final String MODEL_FP = 'Discount-Percentage';
17+
18+
private static ID preparedProductID {
19+
get;
20+
set;
21+
}
22+
23+
private static List < String > rateplannames = new List < String > {
24+
'Plan for Flat Fee charges', 'Plan for Per Unit charges', 'Plan for Volume charges', 'Plan for Overage charges', 'Plan for Tiered charges', 'Plan for Discount-Fixed Amount charges'
25+
};
26+
27+
private static Map < String, List < List < String >>> PLAN_CHARGE_MAPPING = new Map < String, List < List < String >>> {
28+
'Plan for Flat Fee charges' => new List < List < String >> {
29+
new List < String > {
30+
'Flat Fee Pricing', 'One-Time'
31+
}, new List < String > {
32+
'Flat Fee Pricing', 'Recurring'
33+
}, new List < String > {
34+
'Flat Fee Pricing', 'Usage'
35+
}
36+
}, 'Plan for Per Unit charges' => new List < List < String >> {
37+
new List < String > {
38+
'Per Unit Pricing', 'One-Time'
39+
}, new List < String > {
40+
'Per Unit Pricing', 'Recurring'
41+
}, new List < String > {
42+
'Per Unit Pricing', 'Usage'
43+
}
44+
}, 'Plan for Volume charges' => new List < List < String >> {
45+
new List < String > {
46+
'Volume Pricing', 'One-Time'
47+
}, new List < String > {
48+
'Volume Pricing', 'Recurring'
49+
}, new List < String > {
50+
'Volume Pricing', 'Usage'
51+
}
52+
}, 'Plan for Overage charges' => new List < List < String >> {
53+
new List < String > {
54+
'Overage Pricing', 'Usage'
55+
}, new List < String > {
56+
'Tiered with Overage Pricing', 'Usage'
57+
}
58+
}, 'Plan for Tiered charges' => new List < List < String >> {
59+
new List < String > {
60+
'Tiered Pricing', 'One-Time'
61+
}, new List < String > {
62+
'Tiered Pricing', 'Recurring'
63+
}, new List < String > {
64+
'Tiered Pricing', 'Usage'
65+
}
66+
}, 'Plan for Discount-Fixed Amount charges' => new List < List < String >> {
67+
new List < String > {
68+
'Discount-Fixed Amount', 'Recurring'
69+
}, new List < String > {
70+
'Per Unit Pricing', 'One-Time'
71+
}, new List < String > {
72+
'Per Unit Pricing', 'Recurring'
73+
}
74+
}, 'Plan for Discount-Percentage charges' => new List < List < String >> {
75+
new List < String > {
76+
'Discount-Percentage', 'Recurring'
77+
}, new List < String > {
78+
'Per Unit Pricing', 'One-Time'
79+
}, new List < String > {
80+
'Per Unit Pricing', 'Recurring'
81+
}
82+
}
83+
};
84+
85+
private static Map < String, List < Decimal >> TIER_MAPPING = new Map < String, List < Decimal >> {
86+
'One-Time_Flat Fee Pricing' => new List < Decimal > {
87+
1000
88+
}, 'One-Time_Per Unit Pricing' => new List < Decimal > {
89+
1000
90+
}, 'One-Time_Volume Pricing' => new List < Decimal > {
91+
0, 0, 500, 0, 800, 1, 1000, 0
92+
}, 'One-Time_Tiered Pricing' => new List < Decimal > {
93+
500, 0, 400, 1, 200, 0
94+
}, 'Recurring_Flat Fee Pricing' => new List < Decimal > {
95+
1500
96+
}, 'Recurring_Per Unit Pricing' => new List < Decimal > {
97+
1000
98+
}, 'Recurring_Volume Pricing' => new List < Decimal > {
99+
0, 1, 800, 1, 1200, 0, 1500, 1
100+
}, 'Recurring_Tiered Pricing' => new List < Decimal > {
101+
800, 0, 400, 0, 300, 1
102+
}, 'Usage_Flat Fee Pricing' => new List < Decimal > {
103+
0
104+
}, 'Usage_Per Unit Pricing' => new List < Decimal > {
105+
0
106+
}, 'Usage_Volume Pricing' => new List < Decimal > {}, 'Usage_Tiered Pricing' => new List < Decimal > {}, 'Usage_Overage Pricing' => new List < Decimal > {
107+
0
108+
}, 'Usage_Tiered with Overage Pricing' => new List < Decimal > {
109+
8, 100, 200
110+
}, 'Recurring_Discount-Fixed Amount' => new List < Decimal > {
111+
598, 400, 500
112+
}, 'Recurring_Discount-Percentage' => new List < Decimal > {
113+
7
114+
}, '_Flat Fee Pricing' => new List < Decimal > {
115+
0
116+
}, '_Per Unit Pricing' => new List < Decimal > {
117+
0
118+
}, '_Volume Pricing' => new List < Decimal > {
119+
0, 0, 0, 0, 10, 0, 0, 1, 0, 1, 10, 1, 0, 0, 0, 0, 10, 0
120+
}, '_Tiered Pricing' => new List < Decimal > {
121+
0, 1, 0, 0, 10, 0, 0, 1, 0, 1, 10, 1, 0, 0, 0, 0, 10, 0
122+
}
123+
};
124+
125+
126+
@isTest
127+
public static zqu__ZProduct__c prepareProductCatalog() {
128+
List < zqu__ZProduct__c > listOFzprod = new List < zqu__ZProduct__c > ();
129+
zqu__ZProduct__c zprod = null;
130+
try {
131+
// Following cases will return list size as 0 and thus new product catalog will be created.
132+
// 1. null == preparedProductID
133+
// 2. preparedProductID is expired
134+
listOFzprod = [SELECT name, zqu__effectivestartdate__c, zqu__effectiveenddate__c, zqu__sku__c, zqu__zuoraid__c, zqu__deleted__c FROM zqu__ZProduct__c WHERE id = : preparedProductID];
135+
} catch (Exception e) {
136+
System.assert(false, 'Exception occured while querying ZProduct__c. Exception Details: ' + e.getMessage());
137+
}
138+
if (listOFzprod.size() == 0) {
139+
//generate ProductCatalog in Salesforce
140+
zprod = new zqu__ZProduct__c();
141+
zprod.Name = 'CC Testing';
142+
zprod.zqu__EffectiveStartDate__c = DEFAULT_EFFECTIVE_START_DATE;
143+
zprod.zqu__EffectiveEndDate__c = DEFAULT_EFFECTIVE_END_DATE;
144+
zprod.zqu__SKU__c = 'testingsku0001';
145+
zprod.zqu__ZuoraId__c = 'zuoraid0000000001';
146+
zprod.zqu__Deleted__c = false;
147+
zprod.Current_Product__c = 'Yes';
148+
try {
149+
insert zprod;
150+
} catch (Exception e) {
151+
System.assert(false, 'ZQTestDataSetup.prepareProductCatalog: Exception occured while inserting ZProduct__c. Exception Details: ' + e.getMessage());
152+
}
153+
System.assert(null != zprod.Id, 'Product record preparation failed when prepare for testing product catalog.');
154+
155+
preparedProductID = zprod.Id;
156+
157+
List < zqu__ProductRatePlan__c > p_rateplans = new List < zqu__ProductRatePlan__c > ();
158+
Integer rpzuoraid = 1;
159+
for (String rpname: rateplannames) {
160+
zqu__ProductRatePlan__c p_rateplan = new zqu__ProductRatePlan__c();
161+
p_rateplan.Name = rpname;
162+
p_rateplan.zqu__EffectiveStartDate__c = DEFAULT_EFFECTIVE_START_DATE;
163+
p_rateplan.zqu__EffectiveEndDate__c = DEFAULT_EFFECTIVE_END_DATE;
164+
p_rateplan.zqu__ZProduct__r = zprod;
165+
p_rateplan.zqu__ZProduct__c = zprod.Id;
166+
p_rateplan.zqu__ZuoraId__c = String.valueOf(rpzuoraid);
167+
p_rateplan.zqu__Deleted__c = false;
168+
p_rateplan.Region__c = 'US East';
169+
p_rateplans.add(p_rateplan);
170+
rpzuoraid++;
171+
}
172+
try {
173+
insert p_rateplans;
174+
} catch (Exception e) {
175+
System.assert(false, 'ZQTestDataSetup.prepareProductCatalog: Exception occured while inserting ProductRatePlan__c records. Exception Details: ' + e.getMessage());
176+
}
177+
System.assert(p_rateplans.size() > 0, 'Product Rateplan records prepare failed when prepare for testing product catalog.');
178+
List < zqu__ProductRatePlanCharge__c > p_all_charges = new List < zqu__ProductRatePlanCharge__c > ();
179+
for (zqu__ProductRatePlan__c p_rateplan: p_rateplans) {
180+
List < zqu__ProductRatePlanCharge__c > rp_charges = getProductRatePlanCharges(p_rateplan);
181+
p_all_charges.addAll(rp_charges);
182+
}
183+
try {
184+
insert p_all_charges;
185+
} catch (Exception e) {
186+
System.assert(false, 'ZQTestDataSetup.prepareProductCatalog: Exception occured while inserting ProductRatePlanCharge__c records. Exception Details: ' + e.getMessage());
187+
}
188+
System.assert(p_all_charges.size() > 0, 'Product Rateplan charge records prepare failed when prepare for testing product catalog.');
189+
List < zqu__ProductRatePlanChargeTier__c > p_all_tiers = new List < zqu__ProductRatePlanChargeTier__c > ();
190+
for (zqu__ProductRatePlanCharge__c charge: p_all_charges) {
191+
List < zqu__ProductRatePlanChargeTier__c > p_chargetiers = getProductRatePlanChargeTiers(charge);
192+
p_all_tiers.addAll(p_chargetiers);
193+
}
194+
try {
195+
insert p_all_tiers;
196+
} catch (Exception e) {
197+
System.assert(false, 'ZQTestDataSetup.prepareProductCatalog: Exception occured while inserting ProductRatePlanChargeTier__c records. Exception Details: ' + e.getMessage());
198+
}
199+
System.assert(p_all_tiers.size() > 0, 'Product Rateplan charge tier records prepare failed when prepare for testing product catalog.');
200+
}
201+
return zprod;
202+
}
203+
204+
private static List < zqu__ProductRatePlanCharge__c > getProductRatePlanCharges(zqu__ProductRatePlan__c rp) {
205+
List < List < String >> chargedefines = PLAN_CHARGE_MAPPING.get(rp.Name);
206+
List < zqu__ProductRatePlanCharge__c > charges = new List < zqu__ProductRatePlanCharge__c > ();
207+
Integer zuoraid = Integer.valueOf(rp.zqu__ZuoraId__c) * 1000;
208+
for (List < String > chargedefine: chargedefines) {
209+
zqu__ProductRatePlanCharge__c p_charge = new zqu__ProductRatePlanCharge__c();
210+
p_charge.Name = chargedefine.get(1) + ' ' + chargedefine.get(0);
211+
p_charge.zqu__Model__c = chargedefine.get(0);
212+
p_charge.zqu__Type__c = chargedefine.get(1);
213+
p_charge.zqu__UOM__c = 'UOM tesing';
214+
p_charge.zqu__DefaultQuantity__c = 1;
215+
p_charge.zqu__MinQuantity__c = 0;
216+
p_charge.zqu__MaxQuantity__c = 500;
217+
p_charge.zqu__RecurringPeriod__c = 'Monthly';
218+
p_charge.zqu__ZuoraId__c = String.valueOf(zuoraid);
219+
p_charge.zqu__ProductRatePlan__c = rp.Id;
220+
p_charge.zqu__ProductRatePlan__r = rp;
221+
p_charge.zqu__Deleted__c = false;
222+
if (p_charge.zqu__Model__c == 'Discount-Fixed Amount' || p_charge.zqu__Model__c == 'Discount-Percentage') {
223+
p_charge.zqu__Discount_Apply_Type__c = 3;
224+
p_charge.zqu__Upto_How_Many_Periods__c = 5;
225+
p_charge.zqu__Discount_Level__c = 'RatePlan';
226+
}
227+
zuoraid++;
228+
charges.add(p_charge);
229+
}
230+
return charges;
231+
}
232+
233+
private static List < zqu__ProductRatePlanChargeTier__c > getProductRatePlanChargeTiers(zqu__ProductRatePlanCharge__c charge) {
234+
List < Decimal > tierprices = TIER_MAPPING.get(charge.zqu__Type__c + '_' + charge.zqu__Model__c);
235+
List < zqu__ProductRatePlanChargeTier__c > p_tiers = new List < zqu__ProductRatePlanChargeTier__c > ();
236+
Integer tiernumber = 0;
237+
if (MODEL_V.equals(charge.zqu__Model__c) || MODEL_T.equals(charge.zqu__Model__c)) {
238+
for (Integer i = 0; i < tierprices.size(); i++) {
239+
zqu__ProductRatePlanChargeTier__c p_tier = new zqu__ProductRatePlanChargeTier__c();
240+
p_tier.zqu__Price__c = tierprices[i];
241+
if (0 == tierprices[i + 1]) {
242+
p_tier.zqu__PriceFormat__c = 'Flat Fee';
243+
} else {
244+
p_tier.zqu__PriceFormat__c = 'Per Unit';
245+
}
246+
p_tier.zqu__Tier__c = tiernumber + 1;
247+
p_tier.zqu__Currency__c = DEFAULT_CURRENCY;
248+
if (0 == tiernumber) {
249+
p_tier.zqu__StartingUnit__c = 0;
250+
} else {
251+
p_tier.zqu__StartingUnit__c = 100 * tiernumber + 1;
252+
}
253+
if (tiernumber <= tierprices.size() - 1) {
254+
p_tier.zqu__EndingUnit__c = 100 * (tiernumber + 1);
255+
}
256+
p_tier.zqu__ProductRatePlanCharge__r = charge;
257+
p_tier.zqu__ProductRatePlanCharge__c = charge.Id;
258+
p_tier.zqu__Deleted__c = false;
259+
p_tiers.add(p_tier);
260+
i++;
261+
tiernumber++;
262+
}
263+
} else {
264+
for (Decimal tierprice: tierprices) {
265+
zqu__ProductRatePlanChargeTier__c p_tier = new zqu__ProductRatePlanChargeTier__c();
266+
p_tier.zqu__Price__c = tierprice;
267+
p_tier.zqu__Tier__c = tiernumber + 1;
268+
p_tier.zqu__Currency__c = DEFAULT_CURRENCY;
269+
p_tier.zqu__StartingUnit__c = 100 * tiernumber + 1;
270+
if (tiernumber <= tierprices.size() - 1) {
271+
p_tier.zqu__EndingUnit__c = 100 * (tiernumber + 1);
272+
}
273+
p_tier.zqu__ProductRatePlanCharge__r = charge;
274+
p_tier.zqu__ProductRatePlanCharge__c = charge.Id;
275+
p_tier.zqu__Deleted__c = false;
276+
p_tiers.add(p_tier);
277+
}
278+
}
279+
return p_tiers;
280+
}
281+
282+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<ApexClass xmlns="http://soap.sforce.com/2006/04/metadata">
3+
<apiVersion>23.0</apiVersion>
4+
<packageVersions>
5+
<majorNumber>5</majorNumber>
6+
<minorNumber>3</minorNumber>
7+
<namespace>zqu</namespace>
8+
</packageVersions>
9+
<status>Active</status>
10+
</ApexClass>

CustomProductSelector/src/package.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
<description>A Z-Force Quotes sample code package that includes a reference implementation of a custom Product Selector with filtering capabilities using Global Methods and Components.</description>
55
<types>
66
<members>CustomProductSelectorController</members>
7+
<members>CustomProductSelectorControllerTest</members>
8+
<members>TestDataSetup</members>
79
<name>ApexClass</name>
810
</types>
911
<types>

0 commit comments

Comments
 (0)