2016-07-29 13:16:11 +02:00
|
|
|
package org.appledash.saneeconomy.test;
|
|
|
|
|
2017-07-15 22:36:32 +02:00
|
|
|
import com.google.common.collect.ImmutableList;
|
2016-07-29 13:16:11 +02:00
|
|
|
import org.appledash.saneeconomy.economy.Currency;
|
|
|
|
import org.appledash.saneeconomy.economy.EconomyManager;
|
|
|
|
import org.appledash.saneeconomy.economy.economable.Economable;
|
2016-09-21 10:16:36 +02:00
|
|
|
import org.appledash.saneeconomy.economy.transaction.Transaction;
|
|
|
|
import org.appledash.saneeconomy.economy.transaction.TransactionReason;
|
|
|
|
import org.appledash.saneeconomy.economy.transaction.TransactionResult;
|
2016-07-29 13:16:11 +02:00
|
|
|
import org.appledash.saneeconomy.test.mock.MockEconomyStorageBackend;
|
|
|
|
import org.appledash.saneeconomy.test.mock.MockOfflinePlayer;
|
2016-09-18 10:37:44 +02:00
|
|
|
import org.appledash.saneeconomy.test.mock.MockSaneEconomy;
|
2016-07-29 13:16:11 +02:00
|
|
|
import org.junit.Assert;
|
2016-09-18 10:37:44 +02:00
|
|
|
import org.junit.Before;
|
2016-07-29 13:16:11 +02:00
|
|
|
import org.junit.Test;
|
|
|
|
|
|
|
|
import java.text.DecimalFormat;
|
2017-07-15 22:36:32 +02:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Random;
|
|
|
|
import java.util.stream.Collectors;
|
2016-07-29 13:16:11 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Created by AppleDash on 7/29/2016.
|
|
|
|
* Blackjack is still best pony.
|
|
|
|
*/
|
|
|
|
public class EconomyManagerTest {
|
2016-09-18 10:37:44 +02:00
|
|
|
private EconomyManager economyManager;
|
|
|
|
|
|
|
|
@Before
|
|
|
|
public void setupEconomyManager() {
|
|
|
|
economyManager = new EconomyManager(new MockSaneEconomy(),
|
|
|
|
new Currency("test dollar", "test dollars", new DecimalFormat("0.00")),
|
2017-02-20 22:17:25 +01:00
|
|
|
new MockEconomyStorageBackend(), null);
|
2016-09-18 10:37:44 +02:00
|
|
|
}
|
|
|
|
|
2016-07-29 13:16:11 +02:00
|
|
|
@Test
|
|
|
|
public void testEconomyManager() {
|
|
|
|
Economable playerOne = Economable.wrap(new MockOfflinePlayer("One"));
|
|
|
|
Economable playerTwo = Economable.wrap(new MockOfflinePlayer("Two"));
|
|
|
|
|
|
|
|
// Accounts should not exist
|
|
|
|
Assert.assertFalse(economyManager.accountExists(playerOne));
|
|
|
|
Assert.assertFalse(economyManager.accountExists(playerTwo));
|
2016-10-02 17:33:30 +02:00
|
|
|
Assert.assertEquals(0.0D, economyManager.getBalance(playerOne), 0.0);
|
|
|
|
Assert.assertEquals(0.0D, economyManager.getBalance(playerTwo), 0.0);
|
2016-07-29 13:16:11 +02:00
|
|
|
|
2016-09-21 10:16:36 +02:00
|
|
|
economyManager.setBalance(playerOne, 100.0D);
|
2016-07-29 13:16:11 +02:00
|
|
|
|
|
|
|
// Now one should have an account, but two should not
|
|
|
|
Assert.assertTrue(economyManager.accountExists(playerOne));
|
|
|
|
Assert.assertFalse(economyManager.accountExists(playerTwo));
|
|
|
|
|
|
|
|
// One should have balance, two should not
|
2016-10-02 17:33:30 +02:00
|
|
|
Assert.assertEquals(100.0, economyManager.getBalance(playerOne), 0.0);
|
|
|
|
Assert.assertEquals(0.0, economyManager.getBalance(playerTwo), 0.0);
|
2016-07-29 13:16:11 +02:00
|
|
|
|
|
|
|
// One should be able to transfer to two
|
2018-05-25 08:27:02 +02:00
|
|
|
Assert.assertTrue(economyManager.transact(new Transaction(economyManager.getCurrency(), playerOne, playerTwo, 50.0, TransactionReason.PLAYER_PAY)).getStatus() == TransactionResult.Status.SUCCESS);
|
2016-07-29 13:16:11 +02:00
|
|
|
|
|
|
|
// One should now have only 50 left, two should have 50 now
|
2018-05-25 08:27:02 +02:00
|
|
|
Assert.assertEquals("Player one should have 50 dollars", 50.0, economyManager.getBalance(playerOne), 0.0);
|
|
|
|
Assert.assertEquals("Player two should have 50 dollars", 50.0, economyManager.getBalance(playerTwo), 0.0);
|
2016-07-29 13:16:11 +02:00
|
|
|
|
|
|
|
// Ensure that balance addition and subtraction works...
|
2016-10-02 17:33:30 +02:00
|
|
|
Assert.assertEquals(25.0, economyManager.transact(
|
2018-05-25 08:27:02 +02:00
|
|
|
new Transaction(economyManager.getCurrency(), playerOne, Economable.CONSOLE, 25.0, TransactionReason.TEST_TAKE)
|
2016-10-02 17:33:30 +02:00
|
|
|
).getFromBalance(), 0.0);
|
2016-09-21 10:16:36 +02:00
|
|
|
|
2016-10-02 17:33:30 +02:00
|
|
|
Assert.assertEquals(50.0, economyManager.transact(
|
2018-05-25 08:27:02 +02:00
|
|
|
new Transaction(economyManager.getCurrency(), Economable.CONSOLE, playerOne, 25.0, TransactionReason.TEST_GIVE)
|
2016-10-02 17:33:30 +02:00
|
|
|
).getToBalance(), 0.0);
|
2016-09-21 10:16:36 +02:00
|
|
|
|
2016-10-02 17:33:30 +02:00
|
|
|
Assert.assertEquals(TransactionResult.Status.ERR_NOT_ENOUGH_FUNDS, economyManager.transact(
|
2018-05-25 08:27:02 +02:00
|
|
|
new Transaction(economyManager.getCurrency(), playerTwo, Economable.CONSOLE, Double.MAX_VALUE, TransactionReason.TEST_TAKE)
|
2016-10-02 17:33:30 +02:00
|
|
|
).getStatus());
|
2016-07-29 13:16:11 +02:00
|
|
|
|
|
|
|
// Ensure that hasBalance works
|
|
|
|
Assert.assertTrue(economyManager.hasBalance(playerOne, 50.0));
|
|
|
|
Assert.assertFalse(economyManager.hasBalance(playerOne, 51.0));
|
2016-07-29 13:25:39 +02:00
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-07-15 22:36:32 +02:00
|
|
|
@Test
|
|
|
|
public void testTopBalances() {
|
|
|
|
Random random = new Random();
|
|
|
|
List<Economable> economables = new ArrayList<>(10);
|
|
|
|
|
|
|
|
for (int i = 0; i < 10; i++) {
|
|
|
|
Economable economable = Economable.wrap(new MockOfflinePlayer("Dude" + i));
|
|
|
|
economables.add(economable);
|
|
|
|
this.economyManager.setBalance(economable, random.nextInt(1000));
|
|
|
|
}
|
|
|
|
|
|
|
|
this.economyManager.getBackend().reloadTopPlayerBalances();
|
|
|
|
|
|
|
|
List<Double> javaSortedBalances = economables.stream().map(this.economyManager::getBalance).sorted((left, right) -> -left.compareTo(right)).collect(Collectors.toList());
|
2018-10-05 10:38:35 +02:00
|
|
|
List<Double> ecoManTopBalances = ImmutableList.copyOf(this.economyManager.getTopBalances(10, 0).values());
|
2017-07-15 22:36:32 +02:00
|
|
|
|
|
|
|
Assert.assertTrue("List is not correctly sorted!", areListsEqual(javaSortedBalances, ecoManTopBalances));
|
2018-10-05 10:38:35 +02:00
|
|
|
Assert.assertEquals("Wrong number of top balances!", 5, this.economyManager.getTopBalances(5, 0).size());
|
2017-07-15 22:36:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private <T> boolean areListsEqual(List<T> first, List<T> second) {
|
|
|
|
if (first.size() != second.size()) {
|
|
|
|
throw new IllegalArgumentException("Lists must be same length (first=" + first.size() + ", second=" + second.size() + ")");
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < first.size(); i++) {
|
|
|
|
if (!first.get(i).equals(second.get(i))) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2016-07-29 13:16:11 +02:00
|
|
|
}
|