Skip to content

Commit

Permalink
test: unit test for getters fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
naneey committed Jan 24, 2024
1 parent 29d7fc0 commit 9dacf52
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ public List<String> getSponsorProjects(Address address) {
String flag = (String) proposal_details.get(consts.TOKEN);
BigInteger totalBudget = (BigInteger) proposal_details.get(consts.SPONSOR_REWARD);
BigInteger totalPaidAmount = (BigInteger) proposal_details.get(consts.SPONSOR_WITHDRAW_AMOUNT);
BigInteger depositedSponsorBond = ((BigInteger) proposal_details.get(consts.TOTAL_BUDGET)).divide(bondPercentage);
BigInteger depositedSponsorBond = ((BigInteger) proposal_details.get(consts.TOTAL_BUDGET)).multiply(bondPercentage).divide(BigInteger.valueOf(100));
BigInteger remainingAmount = totalBudget.subtract(totalPaidAmount);
int remainingCount = totalInstallment - totalPaidCount;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
import java.util.List;
import java.util.Map;

import static community.icon.cps.score.cpstreasury.CPSTreasury.*;
import static community.icon.cps.score.cpstreasury.utils.consts.*;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
Expand Down Expand Up @@ -125,10 +127,10 @@ void setBnUSDScore() {
@Test
void setOnsetPayment(){
setCPFTreasuryScoreMethod();
doReturn(BigInteger.valueOf(10)).when(scoreSpy).callScore(eq(BigInteger.class),any(),eq("getSponsorBondPercentage"));
BigInteger per = BigInteger.ONE;
doReturn(BigInteger.valueOf(15)).when(scoreSpy).callScore(eq(BigInteger.class),any(),eq("getSponsorBondPercentage"));
BigInteger onSetPayment = BigInteger.valueOf(10);
contextMock.when(caller()).thenReturn(cpfTreasury);
tokenScore.invoke(owner,"setOnsetPayment",per);
tokenScore.invoke(owner,"setOnsetPayment",onSetPayment);
}

public MockedStatic.Verification caller(){
Expand Down Expand Up @@ -201,9 +203,9 @@ void setBnUSDScoreNotContract() {

@Test
void depositProposalFund() {
/* totalBudget = 100, onsetPayment = 1% installmentCount = 2
* remainingBudgetAfter onsetPayment = 99
* installMentAmount = 99/2 =49.5
/* totalBudget = 100, onsetPayment = 10% installmentCount = 2
* remainingBudgetAfter onsetPayment = 90
* installMentAmount = 90/2 =45
*/
setOnsetPayment();
depositProposalFundMethod();
Expand All @@ -214,12 +216,19 @@ void depositProposalFund() {
Map<String, ?> expectedData = Map.of(
consts.IPFS_HASH, "Proposal 1",
consts.TOKEN, "bnUSD",
consts.TOTAL_BUDGET, BigInteger.valueOf(100).multiply(MULTIPLIER),
consts.TOTAL_INSTALLMENT_PAID, BigInteger.ONE.multiply(MULTIPLIER),
consts.TOTAL_INSTALLMENT_COUNT, 2,
consts.TOTAL_TIMES_INSTALLMENT_PAID, 0,
consts.INSTALLMENT_AMOUNT, BigInteger.valueOf(495).multiply(MULTIPLIER).divide(BigInteger.TEN));
consts.TOTAL_BUDGET, BigInteger.valueOf(100).multiply(ICX),
TOTAL_INSTALLMENT_PAID, BigInteger.TEN.multiply(ICX),
TOTAL_INSTALLMENT_COUNT, 2,
TOTAL_TIMES_INSTALLMENT_PAID, 0,
consts.INSTALLMENT_AMOUNT, BigInteger.valueOf(45).multiply(ICX));
assertEquals(proposalDetails.get(0), expectedData);

assertEquals(proposalDataDetails.get(PROJECT_COUNT),1);
assertEquals(proposalDataDetails.get(WITHDRAWN_BNUSD),BigInteger.TEN.multiply(ICX));

Map<String,BigInteger> totalAmount = Map.of(consts.bnUSD,BigInteger.valueOf(45).multiply(ICX));
assertEquals(proposalDataDetails.get(TOTAL_AMOUNT),totalAmount);

}

private void depositProposalFundMethod() {
Expand Down Expand Up @@ -257,11 +266,11 @@ void depositProposalFundProposalAlreadyExists(){
@Test
void updateProposalFund() {
/* before budget adjustment:
totalBudget = 100 installmentCount = 2 onsetPayment = 1
installmentAmount = (100-1)/2 = 49.5
totalBudget = 100 installmentCount = 2 onsetPayment = 10
installmentAmount = (100-10)/2 = 45
after budget adjustment :
totalBudget = 200 installCount = 3 onsetPayment = 1
installmentAmount = (200-1)/3 = 66.33
totalBudget = 200 installCount = 3 onsetPayment = 10
installmentAmount = (200-10)/3 = 63.333
* */
setOnsetPayment();
depositProposalFundMethod();
Expand All @@ -284,11 +293,17 @@ void updateProposalFund() {
consts.IPFS_HASH, "Proposal 1",
consts.TOKEN, "bnUSD",
consts.TOTAL_BUDGET, BigInteger.valueOf(200).multiply(MULTIPLIER),
consts.TOTAL_INSTALLMENT_PAID, BigInteger.ONE.multiply(ICX),
consts.TOTAL_INSTALLMENT_COUNT, 3,
consts.TOTAL_TIMES_INSTALLMENT_PAID, 0,
consts.INSTALLMENT_AMOUNT, new BigInteger("66333333333333333333"));
TOTAL_INSTALLMENT_PAID, BigInteger.TEN.multiply(ICX),
TOTAL_INSTALLMENT_COUNT, 3,
TOTAL_TIMES_INSTALLMENT_PAID, 0,
consts.INSTALLMENT_AMOUNT, new BigInteger("63333333333333333333"));
assertEquals(proposalDetails.get(0), expectedData);

assertEquals(proposalDataDetails.get(PROJECT_COUNT),1);
assertEquals(proposalDataDetails.get(WITHDRAWN_BNUSD),BigInteger.TEN.multiply(ICX));

Map<String,BigInteger> totalAmount = Map.of(consts.bnUSD,new BigInteger("63333333333333333333"));
assertEquals(proposalDataDetails.get(TOTAL_AMOUNT),totalAmount);
}

void updateProposalFundProposalException(){
Expand Down Expand Up @@ -316,18 +331,18 @@ void updateProposalFundProposalDoesnotExist(){
expectErrorMessage(updateProposalFundProposalDoesnotExist, "Reverted(0): " + "CPS_TREASURY: Invalid IPFS hash.");
}

private void depositProposalFund_MilestoneCheck() {
/* Sponsor bond = 10 % intital payement = 1%
* duration = 2 months milestone = 3 */
private void depositProposalFund_MilestoneCheck() {
/* Sponsor reward = 2% intital payement = 10%
* milestone count = 4 */
JsonObject depositProposal = new JsonObject();
depositProposal.add("method", "depositProposalFund");
JsonObject params = new JsonObject();
params.add("ipfs_hash", "Proposal 1");
params.add("project_duration", 4);
params.add("project_duration", 4); // milestone count
params.add("sponsor_address", testing_account.getAddress().toString());
params.add("contributor_address", testing_account2.getAddress().toString());
params.add("total_budget", BigInteger.valueOf(100).multiply(MULTIPLIER).toString(16));
params.add("sponsor_reward", BigInteger.valueOf(10).multiply(MULTIPLIER).toString(16));
params.add("total_budget", BigInteger.valueOf(100).multiply(ICX).toString(16));
params.add("sponsor_reward", BigInteger.valueOf(2).multiply(ICX).toString(16));
params.add("token", "bnUSD");
depositProposal.add("params", params);
setCPFTreasuryScoreMethod();
Expand All @@ -336,46 +351,66 @@ private void depositProposalFund_MilestoneCheck() {

@Test
void sendInstallmentToContributor() { // send first installment
/* total budget = 100 intital payement = 10
* each milestone payment =(100-10)/4 = 22.5 */

setOnsetPayment();
depositProposalFund_MilestoneCheck();
setCpsScoreMethod();

// proposal details after submission
Map<String, ?> proposalDataDetails_before = (Map<String, ?>) tokenScore.call("getContributorProjectedFund", testing_account2.getAddress());
assertEquals(proposalDataDetails_before.get("withdraw_amount_bnUSD"), BigInteger.valueOf(1).multiply(MULTIPLIER));
assertEquals(proposalDataDetails_before.get("withdraw_amount_bnUSD"), BigInteger.valueOf(10).multiply(ICX));

List<Map<String, ?>> proposalDetailsData = (List<Map<String, ?>>) proposalDataDetails_before.get("data");
assertEquals(proposalDetailsData.get(0).get("total_installment_paid"), BigInteger.valueOf(1).multiply(MULTIPLIER));
assertEquals(proposalDetailsData.get(0).get("total_installment_paid"), BigInteger.valueOf(10).multiply(ICX));
assertEquals(proposalDetailsData.get(0).get("total_installment_count"), 4);
assertEquals(proposalDetailsData.get(0).get("installment_amount"), BigInteger.valueOf(2475).multiply(MULTIPLIER).divide(BigInteger.valueOf(100)));
assertEquals(proposalDetailsData.get(0).get("installment_amount"), BigInteger.valueOf(225).multiply(ICX).divide(BigInteger.valueOf(10)));
assertEquals(proposalDetailsData.get(0).get("total_times_installment_paid"), 0);

contextMock.when(caller()).thenReturn(score_address);
tokenScore.invoke(owner, "sendInstallmentToContributor", "Proposal 1", 1);
tokenScore.invoke(owner, "sendInstallmentToContributor", "Proposal 1", BigInteger.valueOf(225).multiply(ICX).divide(BigInteger.valueOf(10)));
tokenScore.invoke(owner, "sendRewardToSponsor", "Proposal 1", 1);
@SuppressWarnings("unchecked")
Map<String, ?> proposalDataDetails_after = (Map<String, ?>) tokenScore.call("getContributorProjectedFund", testing_account2.getAddress());
assertEquals(proposalDataDetails_after.get("withdraw_amount_bnUSD"), BigInteger.valueOf(2575).multiply(MULTIPLIER).divide(BigInteger.valueOf(100)));

proposalDetailsData = (List<Map<String, ?>>) proposalDataDetails_after.get("data");
assertEquals(proposalDetailsData.get(0).get("total_installment_paid"), BigInteger.valueOf(2575).multiply(MULTIPLIER).divide(BigInteger.valueOf(100)));
assertEquals(proposalDetailsData.get(0).get("total_installment_paid"), BigInteger.valueOf(325).multiply(ICX).divide(BigInteger.valueOf(10)));
assertEquals(proposalDetailsData.get(0).get("total_installment_count"), 4);
assertEquals(proposalDetailsData.get(0).get("installment_amount"), BigInteger.valueOf(2475).multiply(MULTIPLIER).divide(BigInteger.valueOf(100)));
assertEquals(proposalDetailsData.get(0).get("installment_amount"), BigInteger.valueOf(225).multiply(ICX).divide(BigInteger.valueOf(10)));
assertEquals(proposalDetailsData.get(0).get("total_times_installment_paid"), 1);


assertEquals(proposalDataDetails_after.get(PROJECT_COUNT),1);
assertEquals(proposalDataDetails_after.get(WITHDRAWN_BNUSD),BigInteger.valueOf(325).multiply(ICX).divide(BigInteger.valueOf(10)));

Map<String,BigInteger> totalAmount = Map.of(consts.bnUSD,BigInteger.valueOf(225).multiply(ICX).divide(BigInteger.valueOf(10)));
assertEquals(proposalDataDetails_after.get(TOTAL_AMOUNT),totalAmount);

}


@Test
void sendInstallmentToContributor_SecondInstallment() { // send first installment
void sendInstallmentToContributor_SecondInstallment() { // send second installment
sendInstallmentToContributor();

tokenScore.invoke(owner, "sendInstallmentToContributor", "Proposal 1", 1);
tokenScore.invoke(owner, "sendInstallmentToContributor", "Proposal 1", BigInteger.valueOf(225).multiply(ICX).divide(BigInteger.valueOf(10)));
tokenScore.invoke(owner, "sendRewardToSponsor", "Proposal 1", 1);
@SuppressWarnings("unchecked")
Map<String, ?> proposalDataDetails_after = (Map<String, ?>) tokenScore.call("getContributorProjectedFund", testing_account2.getAddress());
assertEquals(proposalDataDetails_after.get("withdraw_amount_bnUSD"), BigInteger.valueOf(505).multiply(MULTIPLIER).divide(BigInteger.valueOf(10)));

List<Map<String, ?>> proposalDetailsData = (List<Map<String, ?>>) proposalDataDetails_after.get("data");
assertEquals(proposalDetailsData.get(0).get("total_installment_paid"), BigInteger.valueOf(505).multiply(MULTIPLIER).divide(BigInteger.valueOf(10)));
assertEquals(proposalDetailsData.get(0).get("total_installment_paid"), BigInteger.valueOf(55).multiply(ICX));
assertEquals(proposalDetailsData.get(0).get("total_installment_count"), 4);
assertEquals(proposalDetailsData.get(0).get("installment_amount"), BigInteger.valueOf(2475).multiply(MULTIPLIER).divide(BigInteger.valueOf(100)));
assertEquals(proposalDetailsData.get(0).get("installment_amount"), BigInteger.valueOf(225).multiply(ICX).divide(BigInteger.valueOf(10)));
assertEquals(proposalDetailsData.get(0).get("total_times_installment_paid"), 2);


assertEquals(proposalDataDetails_after.get(PROJECT_COUNT),1);
assertEquals(proposalDataDetails_after.get(WITHDRAWN_BNUSD),BigInteger.valueOf(55).multiply(ICX));

Map<String,BigInteger> totalAmount = Map.of(consts.bnUSD,BigInteger.valueOf(225).multiply(ICX).divide(BigInteger.valueOf(10)));
assertEquals(proposalDataDetails_after.get(TOTAL_AMOUNT),totalAmount);

}

Expand All @@ -384,10 +419,21 @@ void sendInstallmentToContributor_Completed(){
// total Milestone = 4, remaining milestone = 2
sendInstallmentToContributor_SecondInstallment();

tokenScore.invoke(owner, "sendInstallmentToContributor", "Proposal 1", 2);
tokenScore.invoke(owner, "sendInstallmentToContributor", "Proposal 1", BigInteger.valueOf(45).multiply(ICX));
tokenScore.invoke(owner, "sendRewardToSponsor", "Proposal 1", 2);
@SuppressWarnings("unchecked")
Map<String, ?> proposalDataDetails_after = (Map<String, ?>) tokenScore.call("getContributorProjectedFund", testing_account2.getAddress());
assertEquals(proposalDataDetails_after.get("withdraw_amount_bnUSD"), BigInteger.valueOf(100).multiply(MULTIPLIER));
assertEquals(proposalDataDetails_after.get("withdraw_amount_bnUSD"), BigInteger.valueOf(100).multiply(ICX));


List<Map<String, ?>> proposalDetailsData = (List<Map<String, ?>>) proposalDataDetails_after.get("data");
assertEquals(proposalDetailsData.size(),0);

assertEquals(proposalDataDetails_after.get(PROJECT_COUNT),0);
assertEquals(proposalDataDetails_after.get(WITHDRAWN_BNUSD),BigInteger.valueOf(100).multiply(ICX));

Map<String,BigInteger> totalAmount = Map.of(consts.bnUSD,BigInteger.ZERO);
assertEquals(proposalDataDetails_after.get(TOTAL_AMOUNT),totalAmount);

}

Expand All @@ -396,19 +442,27 @@ void sendRewardToSponsor() {
setOnsetPayment();
depositProposalFund_MilestoneCheck();
setCpsScoreMethod();
doReturn(BigInteger.valueOf(15)).when(scoreSpy).callScore(BigInteger.class, score_address, "getSponsorBondPercentage");
@SuppressWarnings("unchecked")
Map<String, ?> proposalDataDetails = (Map<String, ?>) tokenScore.call("getSponsorProjectedFund", testing_account.getAddress());
assertEquals(proposalDataDetails.get("withdraw_amount_bnUSD"), BigInteger.valueOf(1).multiply(MULTIPLIER).divide(BigInteger.TEN));
assertEquals(proposalDataDetails.get(WITHDRAWN_BNUSD), BigInteger.valueOf(2).multiply(ICX).divide(BigInteger.TEN));
assertEquals(proposalDataDetails.get(PROJECT_COUNT), 1);
assertEquals(proposalDataDetails.get(TOTAL_SPONSOR_BOND), Map.of(consts.bnUSD,BigInteger.valueOf(15).multiply(ICX)));
assertEquals(proposalDataDetails.get(TOTAL_AMOUNT), Map.of(consts.bnUSD,BigInteger.valueOf(45).multiply(ICX).divide(BigInteger.valueOf(100))));

sendRewardToSponsorMethod();
Map<String, ?> proposalDataDetails2 = (Map<String, ?>) tokenScore.call("getSponsorProjectedFund", testing_account.getAddress());
assertEquals(proposalDataDetails2.get("withdraw_amount_bnUSD"), BigInteger.valueOf(2575).multiply(MULTIPLIER).divide(BigInteger.valueOf(1000)));
assertEquals(proposalDataDetails2.get(WITHDRAWN_BNUSD), BigInteger.valueOf(65).multiply(ICX).divide(BigInteger.valueOf(100)));


List<Map<String, ?>> proposalDetailsData = (List<Map<String, ?>>) proposalDataDetails2.get("data");
assertEquals(proposalDetailsData.get(0).get("total_installment_paid"), BigInteger.valueOf(2575).multiply(MULTIPLIER).divide(BigInteger.valueOf(1000)));
assertEquals(proposalDetailsData.get(0).get("total_installment_count"), 4);
assertEquals(proposalDetailsData.get(0).get("installment_amount"), BigInteger.valueOf(2475).multiply(MULTIPLIER).divide(BigInteger.valueOf(1000)));
assertEquals(proposalDetailsData.get(0).get(TOTAL_BUDGET), BigInteger.valueOf(2).multiply(ICX));
assertEquals(proposalDetailsData.get(0).get(TOTAL_INSTALLMENT_PAID), BigInteger.valueOf(65).multiply(ICX).divide(BigInteger.valueOf(100)));
assertEquals(proposalDetailsData.get(0).get(TOTAL_INSTALLMENT_COUNT), 4);
assertEquals(proposalDetailsData.get(0).get(TOTAL_TIMES_INSTALLMENT_PAID), 1);
assertEquals(proposalDetailsData.get(0).get(INSTALLMENT_AMOUNT), BigInteger.valueOf(45).multiply(ICX).divide(BigInteger.valueOf(100)));
assertEquals(proposalDetailsData.get(0).get(SPONSOR_BOND_AMOUNT), BigInteger.valueOf(15).multiply(ICX));


}

Expand All @@ -426,10 +480,10 @@ void disqualifyProject(){
setBnUSDScoreMethod();

/* total budget = 100 sponsor reward = 2
* on proposal submission : contributor_reward = 1 and sponsor_reward = 0.02
* remainning budget = 102 -1-0.02 = 100.98
* on proposal submission : contributor_reward = 10 and sponsor_reward = 0.2
* remainning budget = 102 -10-0.2 = 91.8
* */
BigInteger remainingBudget = BigInteger.valueOf(10098).multiply(MULTIPLIER).divide(BigInteger.valueOf(100));
BigInteger remainingBudget = BigInteger.valueOf(918).multiply(ICX).divide(BigInteger.valueOf(10));
doNothing().when(scoreSpy).callScore(eq(bnUSDScore), eq("transfer"), eq(cpfTreasury),
eq(remainingBudget),any());
contextMock.when(caller()).thenReturn(score_address);
Expand All @@ -444,13 +498,17 @@ void disqualifyProject(){

@Test
void claimReward(){
/* total budget = 100 sponsor reward = 2
* initial payment = 10%
* 10 % of 2 = 0.2 */
setScoresMethod();
depositProposalFundMethod();
// sendRewardToSponsorMethod();
setBnUSDScoreMethod();
BigInteger reward = BigInteger.valueOf(2).multiply(MULTIPLIER).divide(BigInteger.valueOf(100));
BigInteger reward = BigInteger.valueOf(2).multiply(MULTIPLIER).divide(BigInteger.valueOf(10));
doNothing().when(scoreSpy).callScore(eq(bnUSDScore),eq("transfer"),eq(testing_account.getAddress()),eq(reward));
contextMock.when(caller()).thenReturn(testing_account.getAddress());

// claimed by sponsor
tokenScore.invoke(testing_account, "claimReward");


Expand Down

0 comments on commit 9dacf52

Please sign in to comment.