|
| 1 | +package com.microsoft.graph.http; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 4 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 5 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 6 | +import static org.mockito.Mockito.doAnswer; |
| 7 | +import static org.mockito.Mockito.mock; |
| 8 | + |
| 9 | +import java.io.IOException; |
| 10 | +import java.util.concurrent.ExecutionException; |
| 11 | + |
| 12 | +import org.junit.jupiter.api.Test; |
| 13 | + |
| 14 | +import okhttp3.Call; |
| 15 | +import okhttp3.Response; |
| 16 | + |
| 17 | +public class CoreHttpCallbackFutureWrapperTests { |
| 18 | + |
| 19 | + @Test |
| 20 | + public void ThrowsIfCallIsNull() { |
| 21 | + assertThrows(NullPointerException.class, () -> new CoreHttpCallbackFutureWrapper(null)); |
| 22 | + } |
| 23 | + boolean isCanceled = false; |
| 24 | + |
| 25 | + @Test |
| 26 | + public void CancelsCall() { |
| 27 | + var call = mock(Call.class); |
| 28 | + doAnswer(i -> { |
| 29 | + isCanceled = true; |
| 30 | + return null; |
| 31 | + }).when(call).cancel(); |
| 32 | + var wrapper = new CoreHttpCallbackFutureWrapper(call); |
| 33 | + wrapper.future.cancel(true); |
| 34 | + assertTrue(isCanceled); |
| 35 | + } |
| 36 | + |
| 37 | + @Test |
| 38 | + public void ReturnsResponseWhenCompleted() throws IOException, InterruptedException, ExecutionException { |
| 39 | + var call = mock(Call.class); |
| 40 | + var response = mock(Response.class); |
| 41 | + var wrapper = new CoreHttpCallbackFutureWrapper(call); |
| 42 | + wrapper.onResponse(call, response); |
| 43 | + assertEquals(response, wrapper.future.get()); |
| 44 | + } |
| 45 | + |
| 46 | +} |
0 commit comments