|
| 1 | +package com.demo.tuto.crypto.test.service.client; |
| 2 | + |
| 3 | +import com.demo.tuto.crypto.dto.SimpleCoinPriceResponse; |
| 4 | +import com.demo.tuto.crypto.exception.CryptoException; |
| 5 | +import com.demo.tuto.crypto.mapper.CoinPriceMapper; |
| 6 | +import com.demo.tuto.crypto.pojo.SimpleCoinPrice; |
| 7 | +import com.demo.tuto.crypto.service.CoinService; |
| 8 | +import com.demo.tuto.crypto.service.CryptoService; |
| 9 | +import org.junit.jupiter.api.Assertions; |
| 10 | +import org.junit.jupiter.api.Test; |
| 11 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 12 | +import org.mockito.InjectMocks; |
| 13 | +import org.mockito.Mock; |
| 14 | +import org.mockito.Mockito; |
| 15 | +import org.mockito.junit.jupiter.MockitoExtension; |
| 16 | + |
| 17 | +import java.math.BigDecimal; |
| 18 | +import java.time.Clock; |
| 19 | +import java.time.LocalDateTime; |
| 20 | +import java.util.List; |
| 21 | + |
| 22 | +@ExtendWith(MockitoExtension.class) |
| 23 | +public class CryptoServiceTest { |
| 24 | + |
| 25 | + @Mock |
| 26 | + private CoinPriceMapper coinPriceMapper; |
| 27 | + |
| 28 | + @Mock |
| 29 | + private CoinService coinService; |
| 30 | + |
| 31 | + @InjectMocks |
| 32 | + private CryptoService cryptoService; |
| 33 | + |
| 34 | + @Test |
| 35 | + public void givenBitcoin_whenGetCurrentPrice_thenReturnSimpleCoinPrice() throws CryptoException { |
| 36 | + |
| 37 | + // Given |
| 38 | + final String coinId = "bitcoin"; |
| 39 | + final LocalDateTime localDateTime = LocalDateTime.now(Clock.systemUTC()); |
| 40 | + final SimpleCoinPrice simpleCoinPrice = new SimpleCoinPrice("bitcoin", "usd", BigDecimal.valueOf(80300), localDateTime); |
| 41 | + final SimpleCoinPriceResponse simpleCoinPriceResponse = new SimpleCoinPriceResponse("bitcoin", "usd", BigDecimal.valueOf(80300), localDateTime); |
| 42 | + Mockito.when(coinService.fetchCoinPrices(List.of(coinId))).thenReturn(List.of(simpleCoinPrice)); |
| 43 | + Mockito.when(coinPriceMapper.toSimpleCoinPriceResponses(List.of(simpleCoinPrice))).thenReturn(List.of(simpleCoinPriceResponse)); |
| 44 | + |
| 45 | + // Act |
| 46 | + final List<SimpleCoinPriceResponse> response = cryptoService.getCurrentPrice(coinId); |
| 47 | + |
| 48 | + // Then |
| 49 | + Assertions.assertNotNull(response); |
| 50 | + Assertions.assertEquals(1, response.size()); |
| 51 | + Assertions.assertEquals(response, List.of(simpleCoinPriceResponse)); |
| 52 | + } |
| 53 | + |
| 54 | + @Test |
| 55 | + public void givenBlankCoinId_whenGetCurrentPrice_thenThrowIllegalArgumentException() throws CryptoException { |
| 56 | + |
| 57 | + // Given |
| 58 | + final String coinId = ""; |
| 59 | + |
| 60 | + // Act |
| 61 | + final IllegalArgumentException exception = Assertions.assertThrows(IllegalArgumentException.class, () -> cryptoService.getCurrentPrice(coinId)); |
| 62 | + |
| 63 | + // Then |
| 64 | + Assertions.assertInstanceOf(IllegalArgumentException.class, exception); |
| 65 | + Assertions.assertEquals("Invalid input parameters", exception.getMessage()); |
| 66 | + } |
| 67 | + |
| 68 | + @Test |
| 69 | + public void givenBitcoin_whenGetCurrentPriceAndCoinServiceThrowCryptoException_thenThrowCryptoException() throws CryptoException { |
| 70 | + |
| 71 | + // Given |
| 72 | + final String coinId = "bitcoin"; |
| 73 | + final String exceptionErrorMessage = "Error requesting Gecko API"; |
| 74 | + |
| 75 | + Mockito.when(coinService.fetchCoinPrices(List.of(coinId))).thenThrow(new CryptoException(exceptionErrorMessage)); |
| 76 | + |
| 77 | + // Act |
| 78 | + final CryptoException exception = Assertions.assertThrows(CryptoException.class, () -> cryptoService.getCurrentPrice(coinId)); |
| 79 | + |
| 80 | + // Then |
| 81 | + Assertions.assertInstanceOf(CryptoException.class, exception); |
| 82 | + Assertions.assertEquals(exceptionErrorMessage, exception.getMessage()); |
| 83 | + } |
| 84 | + |
| 85 | +} |
0 commit comments