diff --git a/src/test/java/com/github/brenoepics/at4j/core/AzureApiImplTest.java b/src/test/java/com/github/brenoepics/at4j/core/AzureApiImplTest.java index c1aa74a..bd9354c 100644 --- a/src/test/java/com/github/brenoepics/at4j/core/AzureApiImplTest.java +++ b/src/test/java/com/github/brenoepics/at4j/core/AzureApiImplTest.java @@ -17,8 +17,7 @@ class AzureApiImplTest { - @Mock - private AzureApiImpl azureApi; + @Mock private AzureApiImpl azureApi; private TranslateParams translateParams; @BeforeEach diff --git a/src/test/java/com/github/brenoepics/at4j/core/ratelimit/RateLimitManagerTest.java b/src/test/java/com/github/brenoepics/at4j/core/ratelimit/RateLimitManagerTest.java index 64002a6..05886d0 100644 --- a/src/test/java/com/github/brenoepics/at4j/core/ratelimit/RateLimitManagerTest.java +++ b/src/test/java/com/github/brenoepics/at4j/core/ratelimit/RateLimitManagerTest.java @@ -23,135 +23,134 @@ class RateLimitManagerTest { - @Mock - private AzureApiImpl api; - @Mock - private RestRequest request; - @Mock - private RestRequestResult result; - @Mock - private Response response; - @Mock - private RateLimitBucket bucket; - - private RateLimitManager rateLimitManager; - - @Mock - private ThreadPool threadPool; - @Mock - private ExecutorService executorService; - - @BeforeEach - public void setUp() { - MockitoAnnotations.openMocks(this); - rateLimitManager = new RateLimitManager<>(api); - - when(api.getThreadPool()).thenReturn(threadPool); - when(threadPool.getExecutorService()).thenReturn(executorService); - - when(response.header("X-RateLimit-Remaining", "1")).thenReturn("10"); - when(response.header("X-RateLimit-Reset", "0")).thenReturn("1000"); - } - - @Test - void queuesRequestWhenBucketIsPresent() { - when(request.getMajorUrlParameter()).thenReturn(Optional.empty()); - when(bucket.peekRequestFromQueue()).thenReturn(null); - - rateLimitManager.queueRequest(request); - - verify(executorService, times(1)).submit(any(Runnable.class)); - } - - @Test - void retriesRequestWhenBucketIsNotEmpty() { - when(bucket.peekRequestFromQueue()).thenReturn(request); - - RestRequest retriedRequest = rateLimitManager.retryRequest(bucket); - - assertEquals(request, retriedRequest); - } - - @Test - void doesNotRetryRequestWhenBucketIsEmpty() { - when(bucket.peekRequestFromQueue()).thenReturn(null); - - RestRequest retriedRequest = rateLimitManager.retryRequest(bucket); - - assertNull(retriedRequest); - } - - @Test - void doesNotHandleResponseWhenResultIsNull() { - assertDoesNotThrow(() -> rateLimitManager.handleResponse(request, null, bucket, System.currentTimeMillis())); - } - - @Test - void queuesRequestWhenBucketIsNotPresent() { - when(request.getMajorUrlParameter()).thenReturn(Optional.empty()); - when(bucket.peekRequestFromQueue()).thenReturn(request); - - - rateLimitManager.queueRequest(request); - assertDoesNotThrow(() -> rateLimitManager.handleResponse(request, result, bucket, System.currentTimeMillis())); - } - - @Test - void handleResponseUpdatesBucketInformationWhenResponseCodeIsNot429() { - when(response.header("X-RateLimit-Remaining", "1")).thenReturn("10"); - when(response.header("X-RateLimit-Reset", "0")).thenReturn("1000"); - when(result.getResponse()).thenReturn(response); - when(response.code()).thenReturn(200); - when(request.getResult()).thenReturn(CompletableFuture.completedFuture(result)); - - rateLimitManager.handleResponse(request, result, bucket, System.currentTimeMillis()); - - verify(bucket, times(1)).setRateLimitRemaining(10); - verify(bucket, times(1)).setRateLimitResetTimestamp(anyLong()); - } - - @Test - void handleResponseDoesNotUpdateBucketInformationWhenResponseCodeIs429AndViaHeaderIsNull() { - when(result.getResponse()).thenReturn(response); - when(response.code()).thenReturn(429); - when(response.header("Via")).thenReturn(null); - when(response.header("Retry-after")).thenReturn("1000"); - when(response.header("X-RateLimit-Remaining", "1")).thenReturn("10"); - when(response.header("X-RateLimit-Reset", "0")).thenReturn("1000"); - - assertDoesNotThrow(() -> rateLimitManager.handleResponse(request, result, bucket, System.currentTimeMillis())); - } - - @Test - void handleCurrentRequestThrowsException() throws AzureException, IOException { - // Arrange - RuntimeException expectedException = new RuntimeException(); - when(request.executeBlocking()).thenThrow(expectedException); - - // Act - Executable executable = () -> rateLimitManager.handleCurrentRequest(result, request, bucket, System.currentTimeMillis()); - - // Assert - assertThrows(RuntimeException.class, executable); - } - - - @Test - void handleResponseDoesNotUpdateBucketInformationWhenResponseCodeIsNot429AndRequestResultIsDone() { - when(result.getResponse()).thenReturn(response); - when(response.code()).thenReturn(200); - when(request.getResult()).thenReturn(CompletableFuture.completedFuture(result)); - - assertDoesNotThrow(() -> rateLimitManager.handleResponse(request, result, bucket, System.currentTimeMillis())); - } - - @Test - void searchBucketReturnsBucketWhenBucketIsPresentAndRequestQueueIsEmpty() { - when(request.getMajorUrlParameter()).thenReturn(Optional.empty()); - when(bucket.peekRequestFromQueue()).thenReturn(null); - - Optional> searchBucket = rateLimitManager.searchBucket(request); - - assertTrue(searchBucket.isPresent()); - } -} \ No newline at end of file + @Mock private AzureApiImpl api; + @Mock private RestRequest request; + @Mock private RestRequestResult result; + @Mock private Response response; + @Mock private RateLimitBucket bucket; + + private RateLimitManager rateLimitManager; + + @Mock private ThreadPool threadPool; + @Mock private ExecutorService executorService; + + @BeforeEach + public void setUp() { + MockitoAnnotations.openMocks(this); + rateLimitManager = new RateLimitManager<>(api); + + when(api.getThreadPool()).thenReturn(threadPool); + when(threadPool.getExecutorService()).thenReturn(executorService); + + when(response.header("X-RateLimit-Remaining", "1")).thenReturn("10"); + when(response.header("X-RateLimit-Reset", "0")).thenReturn("1000"); + } + + @Test + void queuesRequestWhenBucketIsPresent() { + when(request.getMajorUrlParameter()).thenReturn(Optional.empty()); + when(bucket.peekRequestFromQueue()).thenReturn(null); + + rateLimitManager.queueRequest(request); + + verify(executorService, times(1)).submit(any(Runnable.class)); + } + + @Test + void retriesRequestWhenBucketIsNotEmpty() { + when(bucket.peekRequestFromQueue()).thenReturn(request); + + RestRequest retriedRequest = rateLimitManager.retryRequest(bucket); + + assertEquals(request, retriedRequest); + } + + @Test + void doesNotRetryRequestWhenBucketIsEmpty() { + when(bucket.peekRequestFromQueue()).thenReturn(null); + + RestRequest retriedRequest = rateLimitManager.retryRequest(bucket); + + assertNull(retriedRequest); + } + + @Test + void doesNotHandleResponseWhenResultIsNull() { + assertDoesNotThrow( + () -> rateLimitManager.handleResponse(request, null, bucket, System.currentTimeMillis())); + } + + @Test + void queuesRequestWhenBucketIsNotPresent() { + when(request.getMajorUrlParameter()).thenReturn(Optional.empty()); + when(bucket.peekRequestFromQueue()).thenReturn(request); + + rateLimitManager.queueRequest(request); + assertDoesNotThrow( + () -> rateLimitManager.handleResponse(request, result, bucket, System.currentTimeMillis())); + } + + @Test + void handleResponseUpdatesBucketInformationWhenResponseCodeIsNot429() { + when(response.header("X-RateLimit-Remaining", "1")).thenReturn("10"); + when(response.header("X-RateLimit-Reset", "0")).thenReturn("1000"); + when(result.getResponse()).thenReturn(response); + when(response.code()).thenReturn(200); + when(request.getResult()).thenReturn(CompletableFuture.completedFuture(result)); + + rateLimitManager.handleResponse(request, result, bucket, System.currentTimeMillis()); + + verify(bucket, times(1)).setRateLimitRemaining(10); + verify(bucket, times(1)).setRateLimitResetTimestamp(anyLong()); + } + + @Test + void handleResponseDoesNotUpdateBucketInformationWhenResponseCodeIs429AndViaHeaderIsNull() { + when(result.getResponse()).thenReturn(response); + when(response.code()).thenReturn(429); + when(response.header("Via")).thenReturn(null); + when(response.header("Retry-after")).thenReturn("1000"); + when(response.header("X-RateLimit-Remaining", "1")).thenReturn("10"); + when(response.header("X-RateLimit-Reset", "0")).thenReturn("1000"); + + assertDoesNotThrow( + () -> rateLimitManager.handleResponse(request, result, bucket, System.currentTimeMillis())); + } + + @Test + void handleCurrentRequestThrowsException() throws AzureException, IOException { + // Arrange + RuntimeException expectedException = new RuntimeException(); + when(request.executeBlocking()).thenThrow(expectedException); + + // Act + Executable executable = + () -> + rateLimitManager.handleCurrentRequest( + result, request, bucket, System.currentTimeMillis()); + + // Assert + assertThrows(RuntimeException.class, executable); + } + + @Test + void + handleResponseDoesNotUpdateBucketInformationWhenResponseCodeIsNot429AndRequestResultIsDone() { + when(result.getResponse()).thenReturn(response); + when(response.code()).thenReturn(200); + when(request.getResult()).thenReturn(CompletableFuture.completedFuture(result)); + + assertDoesNotThrow( + () -> rateLimitManager.handleResponse(request, result, bucket, System.currentTimeMillis())); + } + + @Test + void searchBucketReturnsBucketWhenBucketIsPresentAndRequestQueueIsEmpty() { + when(request.getMajorUrlParameter()).thenReturn(Optional.empty()); + when(bucket.peekRequestFromQueue()).thenReturn(null); + + Optional> searchBucket = rateLimitManager.searchBucket(request); + + assertTrue(searchBucket.isPresent()); + } +}