-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
The wallet service is 100% covered by tests
- Loading branch information
Showing
10 changed files
with
228 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 10 additions & 10 deletions
20
...sitory/specified/user/UserRepository.java → ...deHub/repository/user/UserRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,10 @@ | ||
package com.example.TradeHub.repository.specified.user; | ||
|
||
import com.example.TradeHub.domain.user.User; | ||
import org.springframework.data.repository.CrudRepository; | ||
import org.springframework.stereotype.Repository; | ||
|
||
@Repository | ||
public interface UserRepository extends CrudRepository<User, Long> { | ||
|
||
} | ||
package com.example.TradeHub.repository.user; | ||
|
||
import com.example.TradeHub.domain.user.User; | ||
import org.springframework.data.repository.CrudRepository; | ||
import org.springframework.stereotype.Repository; | ||
|
||
@Repository | ||
public interface UserRepository extends CrudRepository<User, Long> { | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
185 changes: 185 additions & 0 deletions
185
src/test/java/com/example/TradeHub/WalletsServiceTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,185 @@ | ||
package com.example.TradeHub; | ||
|
||
import com.example.TradeHub.domain.user.User; | ||
import com.example.TradeHub.domain.wallet.CryptoWallet; | ||
import com.example.TradeHub.repository.user.UserRepository; | ||
import com.example.TradeHub.repository.wallet.CryptoWalletRepository; | ||
import com.example.TradeHub.service.WalletsService; | ||
import org.junit.jupiter.api.Test; | ||
import org.mockito.InjectMocks; | ||
import org.mockito.Mock; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
|
||
import java.math.BigDecimal; | ||
import java.util.NoSuchElementException; | ||
import java.util.Optional; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.Mockito.*; | ||
|
||
|
||
@SpringBootTest | ||
public class WalletsServiceTest { | ||
|
||
@Mock | ||
private CryptoWalletRepository cryptoWalletRepository; | ||
|
||
@Mock | ||
private UserRepository userRepository; | ||
|
||
@InjectMocks | ||
private WalletsService walletsService; | ||
|
||
|
||
@Test | ||
public void testIncreaseUserBalance_NewWallet() { | ||
// Arrange | ||
Long userId = 1L; | ||
String asset = "BTC"; | ||
BigDecimal amount = new BigDecimal("0.1"); | ||
|
||
when(cryptoWalletRepository.findByUserAndCryptocurrency(userId, asset)) | ||
.thenReturn(Optional.empty()); | ||
|
||
when(userRepository.findById(userId)) | ||
.thenReturn(Optional.of(new User())); | ||
|
||
// Act | ||
walletsService.increaseUserBalance(userId, asset, amount); | ||
|
||
// Assert | ||
verify(cryptoWalletRepository).save(any()); | ||
} | ||
|
||
@Test | ||
public void testIncreaseUserBalance_ExistingWallet() { | ||
// Arrange | ||
Long userId = 1L; | ||
String asset = "BTC"; | ||
BigDecimal amount = new BigDecimal("0.1"); | ||
|
||
CryptoWallet existingWallet = CryptoWallet.builder() | ||
.userId(userId) | ||
.baseAsset(asset) | ||
.balance(new BigDecimal("1.0")) | ||
.build(); | ||
|
||
when(userRepository.findById(userId)) | ||
.thenReturn(Optional.of(new User())); | ||
|
||
when(cryptoWalletRepository.findByUserAndCryptocurrency(userId, asset)) | ||
.thenReturn(Optional.of(existingWallet)); | ||
|
||
// Act | ||
walletsService.increaseUserBalance(userId, asset, amount); | ||
|
||
|
||
// Assert | ||
verify(cryptoWalletRepository).save(existingWallet); | ||
} | ||
|
||
@Test | ||
public void testIncreaseUserBalance_UserNotExist() { | ||
// Arrange | ||
Long userId = 1L; | ||
String asset = "BTC"; | ||
BigDecimal amount = new BigDecimal("0.1"); | ||
|
||
when(userRepository.findById(userId)) | ||
.thenReturn(Optional.empty()); | ||
|
||
// Act & Assert | ||
assertThrows(NoSuchElementException.class, () -> { | ||
walletsService.increaseUserBalance(userId, asset, amount); | ||
}); | ||
} | ||
|
||
@Test | ||
public void testDecreaseUserBalance_WalletNotExist() { | ||
// Arrange | ||
Long userId = 1L; | ||
String asset = "BTC"; | ||
BigDecimal amount = new BigDecimal("0.1"); | ||
|
||
when(cryptoWalletRepository.findByUserAndCryptocurrency(userId, asset)) | ||
.thenReturn(Optional.empty()); | ||
|
||
when(userRepository.findById(userId)) | ||
.thenReturn(Optional.of(new User())); | ||
|
||
// Act && Assert | ||
assertThrows(NoSuchElementException.class, () -> { | ||
walletsService.decreaseUserBalance(userId, asset, amount); | ||
}); | ||
} | ||
|
||
@Test | ||
public void testDecreaseUserBalance_ExistingWalletAndEnoughFunds() { | ||
// Arrange | ||
Long userId = 1L; | ||
String asset = "BTC"; | ||
BigDecimal amount = new BigDecimal("0.1"); | ||
|
||
CryptoWallet existingWallet = CryptoWallet.builder() | ||
.userId(userId) | ||
.baseAsset(asset) | ||
.balance(new BigDecimal("1.0")) | ||
.build(); | ||
|
||
when(userRepository.findById(userId)) | ||
.thenReturn(Optional.of(new User())); | ||
|
||
when(cryptoWalletRepository.findByUserAndCryptocurrency(userId, asset)) | ||
.thenReturn(Optional.of(existingWallet)); | ||
|
||
// Act | ||
walletsService.decreaseUserBalance(userId, asset, amount); | ||
|
||
|
||
// Assert | ||
verify(cryptoWalletRepository).save(existingWallet); | ||
} | ||
|
||
@Test | ||
public void testDecreaseUserBalance_ExistingWalletAndNotEnoughFunds() { | ||
// Arrange | ||
Long userId = 1L; | ||
String asset = "BTC"; | ||
|
||
CryptoWallet existingWallet = CryptoWallet.builder() | ||
.userId(userId) | ||
.baseAsset(asset) | ||
.balance(new BigDecimal("1.0")) | ||
.build(); | ||
|
||
BigDecimal amount = existingWallet.getBalance().add(new BigDecimal(1L)); | ||
|
||
when(userRepository.findById(userId)) | ||
.thenReturn(Optional.of(new User())); | ||
|
||
when(cryptoWalletRepository.findByUserAndCryptocurrency(userId, asset)) | ||
.thenReturn(Optional.of(existingWallet)); | ||
|
||
// Act & Assert | ||
assertThrows(IllegalStateException.class, () -> { | ||
walletsService.decreaseUserBalance(userId, asset, amount); | ||
}); | ||
} | ||
|
||
@Test | ||
public void testDecreaseUserBalance_UserNotExist() { | ||
// Arrange | ||
Long userId = 1L; | ||
String asset = "BTC"; | ||
BigDecimal amount = new BigDecimal("0.1"); | ||
|
||
when(userRepository.findById(userId)) | ||
.thenReturn(Optional.empty()); | ||
|
||
// Act & Assert | ||
assertThrows(NoSuchElementException.class, () -> { | ||
walletsService.decreaseUserBalance(userId, asset, amount); | ||
}); | ||
} | ||
} |