Skip to content

Commit

Permalink
Properly support multi-world vault implementations
Browse files Browse the repository at this point in the history
  • Loading branch information
ericgrandt committed Sep 14, 2024
1 parent f9ffaa2 commit 050ede1
Show file tree
Hide file tree
Showing 2 changed files with 231 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import net.kyori.adventure.text.serializer.plain.PlainTextComponentSerializer;
import net.milkbowl.vault.economy.Economy;
import net.milkbowl.vault.economy.EconomyResponse;
import org.apache.commons.lang.NotImplementedException;
import org.bukkit.OfflinePlayer;

public class EconomyImpl implements Economy {
Expand Down Expand Up @@ -70,7 +69,7 @@ public boolean hasAccount(OfflinePlayer player) {

@Override
public boolean hasAccount(OfflinePlayer player, String worldName) {
throw new NotImplementedException("World specific accounts are not yet supported");
return hasAccount(player);
}

@Override
Expand All @@ -80,7 +79,7 @@ public boolean createPlayerAccount(OfflinePlayer player) {

@Override
public boolean createPlayerAccount(OfflinePlayer player, String worldName) {
throw new NotImplementedException("World specific accounts are not yet supported");
return createPlayerAccount(player);
}

@Override
Expand All @@ -90,7 +89,7 @@ public double getBalance(OfflinePlayer player) {

@Override
public double getBalance(OfflinePlayer player, String world) {
throw new NotImplementedException("World specific accounts are not yet supported");
return getBalance(player);
}

@Override
Expand All @@ -100,7 +99,7 @@ public boolean has(OfflinePlayer player, double amount) {

@Override
public boolean has(OfflinePlayer player, String worldName, double amount) {
throw new NotImplementedException("World specific accounts are not yet supported");
return has(player, amount);
}

@Override
Expand All @@ -125,7 +124,7 @@ public EconomyResponse withdrawPlayer(OfflinePlayer player, double amount) {

@Override
public EconomyResponse withdrawPlayer(OfflinePlayer player, String worldName, double amount) {
throw new NotImplementedException("World specific accounts are not yet supported");
return withdrawPlayer(player, amount);
}

@Override
Expand All @@ -150,7 +149,7 @@ public EconomyResponse depositPlayer(OfflinePlayer player, double amount) {

@Override
public EconomyResponse depositPlayer(OfflinePlayer player, String worldName, double amount) {
throw new NotImplementedException("World specific accounts are not yet supported");
return depositPlayer(player, amount);
}

@Override
Expand Down Expand Up @@ -272,8 +271,4 @@ public boolean createPlayerAccount(String playerName) {
public boolean createPlayerAccount(String playerName, String worldName) {
throw new UnsupportedOperationException();
}

public CurrencyDto getDefaultCurrency() {
return defaultCurrency;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,24 @@ public void hasAccount_WithPlayerHavingAnAccount_ShouldReturnTrue() {
assertTrue(actual);
}

@Test
@Tag("Unit")
public void hasAccount_WorldName_WithPlayerHavingAnAccount_ShouldReturnTrue() {
// Arrange
UUID playerUUID = UUID.randomUUID();
OfflinePlayer playerMock = mock(OfflinePlayer.class);
when(playerMock.getUniqueId()).thenReturn(playerUUID);
when(economyMock.hasAccount(playerUUID)).thenReturn(true);

EconomyImpl sut = new EconomyImpl(true, defaultCurrency, economyMock);

// Act
boolean actual = sut.hasAccount(playerMock, "randomWorld");

// Assert
assertTrue(actual);
}

@Test
@Tag("Unit")
public void createPlayerAccount_WithSuccessfulCallToCreateAccount_ShouldReturnTrue() throws SQLException {
Expand All @@ -184,6 +202,24 @@ public void createPlayerAccount_WithSuccessfulCallToCreateAccount_ShouldReturnTr
assertTrue(actual);
}

@Test
@Tag("Unit")
public void createPlayerAccount_World_WithSuccessfulCallToCreateAccount_ShouldReturnTrue() throws SQLException {
// Arrange
UUID playerUUID = UUID.randomUUID();
OfflinePlayer playerMock = mock(OfflinePlayer.class);
when(playerMock.getUniqueId()).thenReturn(playerUUID);
when(economyMock.createAccount(playerUUID)).thenReturn(true);

EconomyImpl sut = new EconomyImpl(true, defaultCurrency, economyMock);

// Act
boolean actual = sut.createPlayerAccount(playerMock, "randomWorld");

// Assert
assertTrue(actual);
}

@Test
@Tag("Unit")
public void getBalance_WithBalanceFound_ShouldReturnBalance() throws SQLException {
Expand All @@ -203,6 +239,25 @@ public void getBalance_WithBalanceFound_ShouldReturnBalance() throws SQLExceptio
assertEquals(expected, actual);
}

@Test
@Tag("Unit")
public void getBalance_World_WithBalanceFound_ShouldReturnBalance() throws SQLException {
// Arrange
UUID playerUUID = UUID.randomUUID();
OfflinePlayer playerMock = mock(OfflinePlayer.class);
when(playerMock.getUniqueId()).thenReturn(playerUUID);
when(economyMock.getBalance(playerUUID, 1)).thenReturn(BigDecimal.TEN);

EconomyImpl sut = new EconomyImpl(true, defaultCurrency, economyMock);

// Act
double actual = sut.getBalance(playerMock, "randomWorld");
double expected = 10;

// Assert
assertEquals(expected, actual);
}

@Test
@Tag("Unit")
public void has_WithAmountLessThanBalance_ShouldReturnTrue() {
Expand Down Expand Up @@ -257,6 +312,60 @@ public void has_WithAmountLessThanBalance_ShouldReturnFalse() {
assertFalse(actual);
}

@Test
@Tag("Unit")
public void has_World_WithAmountLessThanBalance_ShouldReturnTrue() {
// Arrange
UUID playerUUID = UUID.randomUUID();
OfflinePlayer playerMock = mock(OfflinePlayer.class);
when(playerMock.getUniqueId()).thenReturn(playerUUID);
when(economyMock.getBalance(playerUUID, 1)).thenReturn(BigDecimal.valueOf(100));

EconomyImpl sut = new EconomyImpl(true, defaultCurrency, economyMock);

// Act
boolean actual = sut.has(playerMock, "randomWorld", 10);

// Assert
assertTrue(actual);
}

@Test
@Tag("Unit")
public void has_World_WithAmountEqualToBalance_ShouldReturnTrue() {
// Arrange
UUID playerUUID = UUID.randomUUID();
OfflinePlayer playerMock = mock(OfflinePlayer.class);
when(playerMock.getUniqueId()).thenReturn(playerUUID);
when(economyMock.getBalance(playerUUID, 1)).thenReturn(BigDecimal.TEN);

EconomyImpl sut = new EconomyImpl(true, defaultCurrency, economyMock);

// Act
boolean actual = sut.has(playerMock, "randomWorld", 10);

// Assert
assertTrue(actual);
}

@Test
@Tag("Unit")
public void has_World_WithAmountLessThanBalance_ShouldReturnFalse() {
// Arrange
UUID playerUUID = UUID.randomUUID();
OfflinePlayer playerMock = mock(OfflinePlayer.class);
when(playerMock.getUniqueId()).thenReturn(playerUUID);
when(economyMock.getBalance(playerUUID, 1)).thenReturn(BigDecimal.TEN);

EconomyImpl sut = new EconomyImpl(true, defaultCurrency, economyMock);

// Act
boolean actual = sut.has(playerMock, "randomWorld", 11);

// Assert
assertFalse(actual);
}

@Test
@Tag("Unit")
public void withdrawPlayer_WithSuccessfulWithdraw_ShouldReturnCorrectEconomyResponse() {
Expand Down Expand Up @@ -315,6 +424,64 @@ public void withdrawPlayer_WithFailedWithdraw_ShouldReturnCorrectEconomyResponse
assertEquals(expected.errorMessage, actual.errorMessage);
}

@Test
@Tag("Unit")
public void withdrawPlayer_World_WithSuccessfulWithdraw_ShouldReturnCorrectEconomyResponse() {
// Arrange
UUID playerUUID = UUID.randomUUID();
OfflinePlayer playerMock = mock(OfflinePlayer.class);
when(playerMock.getUniqueId()).thenReturn(playerUUID);
when(economyMock.withdraw(playerUUID, 1, BigDecimal.valueOf(10D), true)).thenReturn(
new TransactionResult(TransactionResult.ResultType.SUCCESS, "")
);

EconomyImpl sut = new EconomyImpl(true, defaultCurrency, economyMock);

// Act
EconomyResponse actual = sut.withdrawPlayer(playerMock, "randomWorld", 10);
EconomyResponse expected = new EconomyResponse(
10,
0,
EconomyResponse.ResponseType.SUCCESS,
""
);

// Assert
assertEquals(expected.amount, actual.amount);
assertEquals(expected.balance, actual.balance);
assertEquals(expected.type, actual.type);
assertEquals(expected.errorMessage, actual.errorMessage);
}

@Test
@Tag("Unit")
public void withdrawPlayer_World_WithFailedWithdraw_ShouldReturnCorrectEconomyResponse() {
// Arrange
UUID playerUUID = UUID.randomUUID();
OfflinePlayer playerMock = mock(OfflinePlayer.class);
when(playerMock.getUniqueId()).thenReturn(playerUUID);
when(economyMock.withdraw(playerUUID, 1, BigDecimal.valueOf(10D), true)).thenReturn(
new TransactionResult(TransactionResult.ResultType.FAILURE, "Failed")
);

EconomyImpl sut = new EconomyImpl(true, defaultCurrency, economyMock);

// Act
EconomyResponse actual = sut.withdrawPlayer(playerMock, "randomWorld", 10);
EconomyResponse expected = new EconomyResponse(
10,
0,
EconomyResponse.ResponseType.FAILURE,
"Failed"
);

// Assert
assertEquals(expected.amount, actual.amount);
assertEquals(expected.balance, actual.balance);
assertEquals(expected.type, actual.type);
assertEquals(expected.errorMessage, actual.errorMessage);
}

@Test
@Tag("Unit")
public void depositPlayer_WithSuccessfulDeposit_ShouldReturnCorrectEconomyResponse() {
Expand Down Expand Up @@ -372,4 +539,62 @@ public void depositPlayer_WithFailedDeposit_ShouldReturnCorrectEconomyResponse()
assertEquals(expected.type, actual.type);
assertEquals(expected.errorMessage, actual.errorMessage);
}

@Test
@Tag("Unit")
public void depositPlayer_World_WithSuccessfulDeposit_ShouldReturnCorrectEconomyResponse() {
// Arrange
UUID playerUUID = UUID.randomUUID();
OfflinePlayer playerMock = mock(OfflinePlayer.class);
when(playerMock.getUniqueId()).thenReturn(playerUUID);
when(economyMock.deposit(playerUUID, 1, BigDecimal.valueOf(10D), true)).thenReturn(
new TransactionResult(TransactionResult.ResultType.SUCCESS, "")
);

EconomyImpl sut = new EconomyImpl(true, defaultCurrency, economyMock);

// Act
EconomyResponse actual = sut.depositPlayer(playerMock, "randomWorld", 10);
EconomyResponse expected = new EconomyResponse(
10,
0,
EconomyResponse.ResponseType.SUCCESS,
""
);

// Assert
assertEquals(expected.amount, actual.amount);
assertEquals(expected.balance, actual.balance);
assertEquals(expected.type, actual.type);
assertEquals(expected.errorMessage, actual.errorMessage);
}

@Test
@Tag("Unit")
public void depositPlayer_World_WithFailedDeposit_ShouldReturnCorrectEconomyResponse() {
// Arrange
UUID playerUUID = UUID.randomUUID();
OfflinePlayer playerMock = mock(OfflinePlayer.class);
when(playerMock.getUniqueId()).thenReturn(playerUUID);
when(economyMock.deposit(playerUUID, 1, BigDecimal.valueOf(10D), true)).thenReturn(
new TransactionResult(TransactionResult.ResultType.FAILURE, "Failed")
);

EconomyImpl sut = new EconomyImpl(true, defaultCurrency, economyMock);

// Act
EconomyResponse actual = sut.depositPlayer(playerMock, "randomWorld", 10);
EconomyResponse expected = new EconomyResponse(
10,
0,
EconomyResponse.ResponseType.FAILURE,
"Failed"
);

// Assert
assertEquals(expected.amount, actual.amount);
assertEquals(expected.balance, actual.balance);
assertEquals(expected.type, actual.type);
assertEquals(expected.errorMessage, actual.errorMessage);
}
}

0 comments on commit 050ede1

Please sign in to comment.