|
| 1 | +package spotify.api.impl; |
| 2 | + |
| 3 | +import okhttp3.MediaType; |
| 4 | +import okhttp3.Request; |
| 5 | +import okhttp3.ResponseBody; |
| 6 | +import org.junit.jupiter.api.Assertions; |
| 7 | +import org.junit.jupiter.api.BeforeEach; |
| 8 | +import org.junit.jupiter.api.Test; |
| 9 | +import org.mockito.Mock; |
| 10 | +import org.mockito.MockitoAnnotations; |
| 11 | +import retrofit2.Call; |
| 12 | +import retrofit2.Response; |
| 13 | +import spotify.exceptions.HttpRequestFailedException; |
| 14 | +import spotify.exceptions.SpotifyActionFailedException; |
| 15 | +import spotify.models.markets.MarketFull; |
| 16 | +import spotify.retrofit.services.MarketService; |
| 17 | + |
| 18 | +import java.io.IOException; |
| 19 | + |
| 20 | +import static org.mockito.ArgumentMatchers.anyString; |
| 21 | +import static org.mockito.Mockito.verify; |
| 22 | +import static org.mockito.Mockito.when; |
| 23 | + |
| 24 | + |
| 25 | +public class MarketApiRetrofitTest extends AbstractApiRetrofitTest { |
| 26 | + private MarketApiRetrofit sut; |
| 27 | + @Mock |
| 28 | + private MarketService mockedMarketService; |
| 29 | + @Mock |
| 30 | + private Call<MarketFull> mockedMarketFullCall; |
| 31 | + |
| 32 | + @BeforeEach |
| 33 | + void setUp() { |
| 34 | + MockitoAnnotations.openMocks(this); |
| 35 | + |
| 36 | + sut = new MarketApiRetrofit(fakeAccessToken, mockedMarketService); |
| 37 | + |
| 38 | + when(mockedMarketService.getMarkets(anyString())).thenReturn(mockedMarketFullCall); |
| 39 | + |
| 40 | + when(mockedMarketFullCall.request()).thenReturn(new Request.Builder().url(fakeUrl).build()); |
| 41 | + } |
| 42 | + |
| 43 | + @Test |
| 44 | + void getMarketsUsesCorrectValuesToCreatHttpCall() throws IOException { |
| 45 | + when(mockedMarketFullCall.execute()).thenReturn(Response.success(new MarketFull())); |
| 46 | + |
| 47 | + sut.getMarkets(); |
| 48 | + verify(mockedMarketService).getMarkets(fakeAccessTokenWithBearer); |
| 49 | + } |
| 50 | + |
| 51 | + @Test |
| 52 | + void getMarketsExecutesHttpCall() throws IOException { |
| 53 | + when(mockedMarketFullCall.execute()).thenReturn(Response.success(new MarketFull())); |
| 54 | + |
| 55 | + sut.getMarkets(); |
| 56 | + verify(mockedMarketFullCall).execute(); |
| 57 | + } |
| 58 | + |
| 59 | + @Test |
| 60 | + void getMarketsThrowsSpotifyActionFailedExceptionWhenError() throws IOException { |
| 61 | + when(mockedMarketFullCall.execute()) |
| 62 | + .thenReturn( |
| 63 | + Response.error( |
| 64 | + 400, |
| 65 | + ResponseBody.create(MediaType.get("application/json"), getJson("error.json")) |
| 66 | + ) |
| 67 | + ); |
| 68 | + |
| 69 | + Assertions.assertThrows(SpotifyActionFailedException.class, () -> sut.getMarkets()); |
| 70 | + } |
| 71 | + |
| 72 | + @Test |
| 73 | + void getMarketsThrowsHttpRequestFailedWhenHttpFails() throws IOException { |
| 74 | + when(mockedMarketFullCall.execute()).thenThrow(IOException.class); |
| 75 | + |
| 76 | + Assertions.assertThrows(HttpRequestFailedException.class, () -> sut.getMarkets()); |
| 77 | + } |
| 78 | + |
| 79 | + @Test |
| 80 | + void getMarketsReturnsMarketFullWhenSuccessful() throws IOException { |
| 81 | + when(mockedMarketFullCall.execute()).thenReturn(Response.success(new MarketFull())); |
| 82 | + |
| 83 | + Assertions.assertNotNull(sut.getMarkets()); |
| 84 | + } |
| 85 | +} |
0 commit comments