From 9dacf52bce0a64a61f292eab403fef3e91a0ed64 Mon Sep 17 00:00:00 2001 From: naneey Date: Wed, 24 Jan 2024 13:52:22 +0545 Subject: [PATCH] test: unit test for getters fixed --- .../cps/score/cpstreasury/CPSTreasury.java | 2 +- .../score/cpstreasury/CPSTreasuryTest.java | 156 ++++++++++++------ 2 files changed, 108 insertions(+), 50 deletions(-) diff --git a/CPSTreasury/src/main/java/community/icon/cps/score/cpstreasury/CPSTreasury.java b/CPSTreasury/src/main/java/community/icon/cps/score/cpstreasury/CPSTreasury.java index f939f8db..ed0324ef 100644 --- a/CPSTreasury/src/main/java/community/icon/cps/score/cpstreasury/CPSTreasury.java +++ b/CPSTreasury/src/main/java/community/icon/cps/score/cpstreasury/CPSTreasury.java @@ -255,7 +255,7 @@ public List 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; diff --git a/CPSTreasury/src/test/java/community/icon/cps/score/cpstreasury/CPSTreasuryTest.java b/CPSTreasury/src/test/java/community/icon/cps/score/cpstreasury/CPSTreasuryTest.java index 49523c1c..c72707d0 100644 --- a/CPSTreasury/src/test/java/community/icon/cps/score/cpstreasury/CPSTreasuryTest.java +++ b/CPSTreasury/src/test/java/community/icon/cps/score/cpstreasury/CPSTreasuryTest.java @@ -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; @@ -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(){ @@ -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(); @@ -214,12 +216,19 @@ void depositProposalFund() { Map 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 totalAmount = Map.of(consts.bnUSD,BigInteger.valueOf(45).multiply(ICX)); + assertEquals(proposalDataDetails.get(TOTAL_AMOUNT),totalAmount); + } private void depositProposalFundMethod() { @@ -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(); @@ -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 totalAmount = Map.of(consts.bnUSD,new BigInteger("63333333333333333333")); + assertEquals(proposalDataDetails.get(TOTAL_AMOUNT),totalAmount); } void updateProposalFundProposalException(){ @@ -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(); @@ -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 proposalDataDetails_before = (Map) 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> proposalDetailsData = (List>) 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 proposalDataDetails_after = (Map) tokenScore.call("getContributorProjectedFund", testing_account2.getAddress()); - assertEquals(proposalDataDetails_after.get("withdraw_amount_bnUSD"), BigInteger.valueOf(2575).multiply(MULTIPLIER).divide(BigInteger.valueOf(100))); proposalDetailsData = (List>) 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 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 proposalDataDetails_after = (Map) tokenScore.call("getContributorProjectedFund", testing_account2.getAddress()); - assertEquals(proposalDataDetails_after.get("withdraw_amount_bnUSD"), BigInteger.valueOf(505).multiply(MULTIPLIER).divide(BigInteger.valueOf(10))); List> proposalDetailsData = (List>) 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 totalAmount = Map.of(consts.bnUSD,BigInteger.valueOf(225).multiply(ICX).divide(BigInteger.valueOf(10))); + assertEquals(proposalDataDetails_after.get(TOTAL_AMOUNT),totalAmount); } @@ -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 proposalDataDetails_after = (Map) 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> proposalDetailsData = (List>) 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 totalAmount = Map.of(consts.bnUSD,BigInteger.ZERO); + assertEquals(proposalDataDetails_after.get(TOTAL_AMOUNT),totalAmount); } @@ -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 proposalDataDetails = (Map) 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 proposalDataDetails2 = (Map) 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> proposalDetailsData = (List>) 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)); + } @@ -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); @@ -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");