diff --git a/src/main/java/io/github/brenoepics/at4j/AzureApi.java b/src/main/java/io/github/brenoepics/at4j/AzureApi.java index f86f025..6309916 100644 --- a/src/main/java/io/github/brenoepics/at4j/AzureApi.java +++ b/src/main/java/io/github/brenoepics/at4j/AzureApi.java @@ -1,16 +1,17 @@ package io.github.brenoepics.at4j; +import com.fasterxml.jackson.databind.ObjectMapper; import io.github.brenoepics.at4j.azure.BaseURL; import io.github.brenoepics.at4j.azure.lang.Language; +import io.github.brenoepics.at4j.core.ratelimit.RateLimitManager; import io.github.brenoepics.at4j.core.thread.ThreadPool; -import io.github.brenoepics.at4j.data.DetectedLanguage; +import io.github.brenoepics.at4j.data.TranslationResult; import io.github.brenoepics.at4j.data.request.AvailableLanguagesParams; import io.github.brenoepics.at4j.data.request.DetectLanguageParams; import io.github.brenoepics.at4j.data.request.TranslateParams; import io.github.brenoepics.at4j.data.response.DetectResponse; import io.github.brenoepics.at4j.data.response.TranslationResponse; -import io.github.brenoepics.at4j.data.TranslationResult; - +import java.net.http.HttpClient; import java.util.Collection; import java.util.Optional; import java.util.concurrent.CompletableFuture; @@ -100,4 +101,25 @@ CompletableFuture>> getAvailableLanguages( * Detection */ CompletableFuture> detectLanguage(DetectLanguageParams params); + + /** + * Gets the ratelimit manager. + * + * @return RateLimitManager - The ratelimit manager. + */ + RateLimitManager getRatelimitManager(); + + /** + * Gets the HttpClient. + * + * @return HttpClient - The HttpClient. + */ + HttpClient getHttpClient(); + + /** + * Gets the ObjectMapper. + * + * @return ObjectMapper - The ObjectMapper. + */ + ObjectMapper getObjectMapper(); } diff --git a/src/main/java/io/github/brenoepics/at4j/AzureApiBuilder.java b/src/main/java/io/github/brenoepics/at4j/AzureApiBuilder.java index fc013d6..514e764 100644 --- a/src/main/java/io/github/brenoepics/at4j/AzureApiBuilder.java +++ b/src/main/java/io/github/brenoepics/at4j/AzureApiBuilder.java @@ -156,6 +156,6 @@ public AzureApi build() { httpClient.connectTimeout(connectTimeout); } - return new AzureApiImpl<>(httpClient.build(), baseURL, subscriptionKey, subscriptionRegion); + return new AzureApiImpl(httpClient.build(), baseURL, subscriptionKey, subscriptionRegion); } } diff --git a/src/main/java/io/github/brenoepics/at4j/core/AzureApiImpl.java b/src/main/java/io/github/brenoepics/at4j/core/AzureApiImpl.java index e44bfc8..863407b 100644 --- a/src/main/java/io/github/brenoepics/at4j/core/AzureApiImpl.java +++ b/src/main/java/io/github/brenoepics/at4j/core/AzureApiImpl.java @@ -1,15 +1,12 @@ package io.github.brenoepics.at4j.core; -import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; -import com.fasterxml.jackson.databind.node.ObjectNode; import io.github.brenoepics.at4j.AzureApi; import io.github.brenoepics.at4j.azure.BaseURL; import io.github.brenoepics.at4j.azure.lang.Language; import io.github.brenoepics.at4j.core.ratelimit.RateLimitManager; import io.github.brenoepics.at4j.core.thread.ThreadPool; import io.github.brenoepics.at4j.core.thread.ThreadPoolImpl; -import io.github.brenoepics.at4j.data.DetectedLanguage; import io.github.brenoepics.at4j.data.request.AvailableLanguagesParams; import io.github.brenoepics.at4j.data.request.DetectLanguageParams; import io.github.brenoepics.at4j.data.request.TranslateParams; @@ -18,9 +15,7 @@ import io.github.brenoepics.at4j.util.rest.RestEndpoint; import io.github.brenoepics.at4j.util.rest.RestMethod; import io.github.brenoepics.at4j.util.rest.RestRequest; - import java.net.http.HttpClient; -import java.util.ArrayList; import java.util.Collection; import java.util.Optional; import java.util.concurrent.CompletableFuture; @@ -29,7 +24,7 @@ * This class is an implementation of the AzureApi interface. It provides methods to interact with * Azure's translation API. */ -public class AzureApiImpl implements AzureApi { +public class AzureApiImpl implements AzureApi { /** The Http Client for this instance. */ private final HttpClient httpClient; @@ -47,7 +42,7 @@ public class AzureApiImpl implements AzureApi { private final ObjectMapper objectMapper = new ObjectMapper(); /** The ratelimit manager for this resource. */ - private final RateLimitManager ratelimitManager = new RateLimitManager<>(this); + private final RateLimitManager ratelimitManager = new RateLimitManager(this); /** The thread pool which is used internally. */ private final ThreadPoolImpl threadPool = new ThreadPoolImpl(); @@ -143,6 +138,7 @@ public void disconnect() { * * @return HttpClient - The used HttpClient. */ + @Override public HttpClient getHttpClient() { return this.httpClient; } @@ -152,6 +148,7 @@ public HttpClient getHttpClient() { * * @return ObjectMapper - The used ObjectMapper. */ + @Override public ObjectMapper getObjectMapper() { return objectMapper; } @@ -161,7 +158,8 @@ public ObjectMapper getObjectMapper() { * * @return RateLimitManager - The used RateLimitManager. */ - public RateLimitManager getRatelimitManager() { + @Override + public RateLimitManager getRatelimitManager() { return ratelimitManager; } } diff --git a/src/main/java/io/github/brenoepics/at4j/core/ratelimit/RateLimitBucket.java b/src/main/java/io/github/brenoepics/at4j/core/ratelimit/RateLimitBucket.java index 91d0436..054da39 100644 --- a/src/main/java/io/github/brenoepics/at4j/core/ratelimit/RateLimitBucket.java +++ b/src/main/java/io/github/brenoepics/at4j/core/ratelimit/RateLimitBucket.java @@ -8,7 +8,7 @@ * This class represents a rate limit bucket for Azure API requests. It manages the rate limit for * each endpoint and major URL parameter combination. */ -public class RateLimitBucket { +public class RateLimitBucket { private final ConcurrentLinkedQueue> requestQueue = new ConcurrentLinkedQueue<>(); @@ -103,7 +103,7 @@ public boolean endpointMatches(RestEndpoint endpoint) { @Override public boolean equals(Object obj) { if (obj instanceof RateLimitBucket) { - RateLimitBucket otherBucket = (RateLimitBucket) obj; + RateLimitBucket otherBucket = (RateLimitBucket) obj; return endpointMatches(otherBucket.endpoint); } return false; diff --git a/src/main/java/io/github/brenoepics/at4j/core/ratelimit/RateLimitManager.java b/src/main/java/io/github/brenoepics/at4j/core/ratelimit/RateLimitManager.java index 7803aeb..66f103f 100644 --- a/src/main/java/io/github/brenoepics/at4j/core/ratelimit/RateLimitManager.java +++ b/src/main/java/io/github/brenoepics/at4j/core/ratelimit/RateLimitManager.java @@ -1,13 +1,12 @@ package io.github.brenoepics.at4j.core.ratelimit; -import io.github.brenoepics.at4j.core.AzureApiImpl; +import io.github.brenoepics.at4j.AzureApi; import io.github.brenoepics.at4j.core.exceptions.AzureException; import io.github.brenoepics.at4j.util.logging.LoggerUtil; import io.github.brenoepics.at4j.util.rest.RestRequest; import io.github.brenoepics.at4j.util.rest.RestRequestHandler; import io.github.brenoepics.at4j.util.rest.RestRequestResponseInfoImpl; import io.github.brenoepics.at4j.util.rest.RestRequestResult; - import java.net.http.HttpHeaders; import java.net.http.HttpResponse; import java.util.HashSet; @@ -16,20 +15,19 @@ import java.util.Set; import java.util.concurrent.CompletableFuture; import java.util.function.Function; - import org.apache.logging.log4j.Logger; /** This class manages rate-limits and keeps track of them. */ -public class RateLimitManager { +public class RateLimitManager { /** The (logger) of this class. */ private static final Logger logger = LoggerUtil.getLogger(RateLimitManager.class); /** The Azure API instance for this rate-limit manager. */ - private final AzureApiImpl api; + private final AzureApi api; /** All buckets. */ - private final Set> buckets = new HashSet<>(); + private final Set> buckets = new HashSet<>(); /** The header for rate-limit remaining information. */ public static final String RATE_LIMITED_HEADER = "X-RateLimit-Remaining"; @@ -48,7 +46,7 @@ public class RateLimitManager { * * @param api The azure api instance for this rate-limit manager. */ - public RateLimitManager(AzureApiImpl api) { + public RateLimitManager(AzureApi api) { this.api = api; } @@ -59,7 +57,7 @@ public RateLimitManager(AzureApiImpl api) { * @param request The request to queue. */ public void queueRequest(RestRequest request) { - Optional> searchBucket = searchBucket(request); + Optional> searchBucket = searchBucket(request); if (searchBucket.isEmpty()) { return; @@ -73,7 +71,7 @@ public void queueRequest(RestRequest request) { * * @param bucket The bucket to submit the request to. */ - private void submitRequest(RateLimitBucket bucket) { + private void submitRequest(RateLimitBucket bucket) { RestRequest currentRequest = bucket.peekRequestFromQueue(); RestRequestResult result = null; @@ -101,7 +99,7 @@ private void submitRequest(RateLimitBucket bucket) { RestRequestHandler handleCurrentRequest( RestRequestResult result, RestRequest currentRequest, - RateLimitBucket bucket, + RateLimitBucket bucket, long responseTimestamp) { try { @@ -141,7 +139,7 @@ RestRequestHandler handleCurrentRequest( * * @param bucket The bucket to wait for. */ - void waitUntilSpaceGetsAvailable(RateLimitBucket bucket) { + void waitUntilSpaceGetsAvailable(RateLimitBucket bucket) { int sleepTime = bucket.getTimeTillSpaceGetsAvailable(); if (sleepTime > 0) { logger.debug( @@ -166,7 +164,7 @@ void waitUntilSpaceGetsAvailable(RateLimitBucket bucket) { * @param bucket The bucket to retry the request for. * @return The request that was retried. */ - RestRequest retryRequest(RateLimitBucket bucket) { + RestRequest retryRequest(RateLimitBucket bucket) { synchronized (buckets) { bucket.pollRequestFromQueue(); RestRequest request = bucket.peekRequestFromQueue(); @@ -199,9 +197,9 @@ private RestRequestResult mapAzureException(Throwable t) { * @param request The request. * @return The bucket that fits to the request. */ - Optional> searchBucket(RestRequest request) { + Optional> searchBucket(RestRequest request) { synchronized (buckets) { - RateLimitBucket bucket = getMatchingBucket(request); + RateLimitBucket bucket = getMatchingBucket(request); // Check if it is already in the queue, send not present if (bucket.peekRequestFromQueue() != null) { @@ -220,7 +218,7 @@ Optional> searchBucket(RestRequest request) { * @param request The request. * @return The bucket that matches the request. */ - RateLimitBucket getMatchingBucket(RestRequest request) { + RateLimitBucket getMatchingBucket(RestRequest request) { synchronized (buckets) { return buckets.stream() .filter(b -> b.endpointMatches(request.getEndpoint())) @@ -240,7 +238,7 @@ RateLimitBucket getMatchingBucket(RestRequest request) { void handleResponse( RestRequest request, RestRequestResult result, - RateLimitBucket bucket, + RateLimitBucket bucket, long responseTimestamp) { try { HttpResponse response = result.getResponse(); @@ -279,7 +277,7 @@ void handleResponse( * @param headers The headers of the response. * @param bucket The bucket the request belongs to. */ - private void handleCloudFlare(HttpHeaders headers, RateLimitBucket bucket) { + private void handleCloudFlare(HttpHeaders headers, RateLimitBucket bucket) { logger.warn( "Hit a CloudFlare API ban! {}", "You were sending a very large amount of invalid requests."); @@ -300,7 +298,7 @@ private void handleCloudFlare(HttpHeaders headers, RateLimitBucket bucket) { private void handleRateLimit( CompletableFuture> request, RestRequestResult result, - RateLimitBucket bucket, + RateLimitBucket bucket, HttpHeaders headers) { // Check if we didn't already complete it exceptionally. diff --git a/src/main/java/io/github/brenoepics/at4j/util/rest/RestRequest.java b/src/main/java/io/github/brenoepics/at4j/util/rest/RestRequest.java index 6eb3820..34292ec 100644 --- a/src/main/java/io/github/brenoepics/at4j/util/rest/RestRequest.java +++ b/src/main/java/io/github/brenoepics/at4j/util/rest/RestRequest.java @@ -3,7 +3,6 @@ import com.fasterxml.jackson.databind.JsonNode; import io.github.brenoepics.at4j.AT4J; import io.github.brenoepics.at4j.AzureApi; -import io.github.brenoepics.at4j.core.AzureApiImpl; import io.github.brenoepics.at4j.core.exceptions.AzureException; import io.github.brenoepics.at4j.util.logging.LoggerUtil; import java.io.IOException; @@ -24,7 +23,7 @@ public class RestRequest { /** The (logger) of this class. */ private static final Logger logger = LoggerUtil.getLogger(RestRequest.class); - private final AzureApiImpl api; + private final AzureApi api; private final RestMethod method; private final RestEndpoint endpoint; @@ -39,7 +38,6 @@ public class RestRequest { private final Exception origin; public static final String ERROR_FIELD = "error"; - public static final String REFERENCE_LINK = AT4J.DOCS_URL + "error-reference/#azure-"; /** * Creates a new instance of this class. @@ -49,7 +47,7 @@ public class RestRequest { * @param endpoint The endpoint to which the request should be sent. */ public RestRequest(AzureApi api, RestMethod method, RestEndpoint endpoint) { - this.api = (AzureApiImpl) api; + this.api = api; this.method = method; this.endpoint = endpoint; addQueryParameter("api-version", AT4J.AZURE_TRANSLATOR_API_VERSION); @@ -67,7 +65,7 @@ public RestRequest(AzureApi api, RestMethod method, RestEndpoint endpoint) { */ public RestRequest( AzureApi api, RestMethod method, RestEndpoint endpoint, boolean includeAuthorizationHeader) { - this.api = (AzureApiImpl) api; + this.api = api; this.method = method; this.endpoint = endpoint; addQueryParameter("api-version", AT4J.AZURE_TRANSLATOR_API_VERSION); @@ -81,7 +79,7 @@ public RestRequest( * * @return The api which is used for this request. */ - public AzureApiImpl getApi() { + public AzureApi getApi() { return api; } @@ -133,36 +131,30 @@ public Exception getOrigin() { /** * Adds a query parameter to the url. * - * @param key The key of the parameter. + * @param key The key of the parameter. * @param value The value of the parameter. - * @return The current instance to chain call methods. */ - public RestRequest addQueryParameter(String key, String value) { + public void addQueryParameter(String key, String value) { queryParameters.computeIfAbsent(key, k -> new ArrayList<>()).add(value); - return this; } /** * Adds multiple query parameters to the url. * * @param parameters The parameters to add. - * @return The current instance to chain call methods. */ - public RestRequest addQueryParameters(Map parameters) { + public void addQueryParameters(Map parameters) { parameters.forEach(this::addQueryParameter); - return this; } /** * Adds a header to the request. * - * @param name The name of the header. + * @param name The name of the header. * @param value The value of the header. - * @return The current instance to chain call methods. */ - public RestRequest addHeader(String name, String value) { + public void addHeader(String name, String value) { headers.put(name, value); - return this; } public Map getHeaders() { @@ -194,11 +186,9 @@ public RestRequest setBody(String body) { * Sets if an authorization header should be included in this request. * * @param includeAuthorizationHeader Whether the authorization header should be included or not. - * @return The current instance to chain call methods. */ - public RestRequest includeAuthorizationHeader(boolean includeAuthorizationHeader) { + public void includeAuthorizationHeader(boolean includeAuthorizationHeader) { this.includeAuthorizationHeader = includeAuthorizationHeader; - return this; } /** @@ -207,6 +197,7 @@ public RestRequest includeAuthorizationHeader(boolean includeAuthorizationHea * @param function A function which processes the rest response to the requested object. * @return A future which will contain the output of the function. */ + @SuppressWarnings("unchecked") public CompletableFuture execute(Function, T> function) { api.getRatelimitManager().queueRequest(this); CompletableFuture future = new CompletableFuture<>(); diff --git a/src/main/java/io/github/brenoepics/at4j/util/rest/RestRequestHandler.java b/src/main/java/io/github/brenoepics/at4j/util/rest/RestRequestHandler.java index 674725e..26df69a 100644 --- a/src/main/java/io/github/brenoepics/at4j/util/rest/RestRequestHandler.java +++ b/src/main/java/io/github/brenoepics/at4j/util/rest/RestRequestHandler.java @@ -4,7 +4,6 @@ * This class is responsible for handling REST requests. It stores the result of a REST request, the * current request being processed, and the timestamp of the response. * - * @param the type of the request */ public class RestRequestHandler { // The result of the REST request diff --git a/src/main/java/io/github/brenoepics/at4j/util/rest/RestRequestResultErrorCode.java b/src/main/java/io/github/brenoepics/at4j/util/rest/RestRequestResultErrorCode.java index 8037ed8..71dfa1c 100644 --- a/src/main/java/io/github/brenoepics/at4j/util/rest/RestRequestResultErrorCode.java +++ b/src/main/java/io/github/brenoepics/at4j/util/rest/RestRequestResultErrorCode.java @@ -213,7 +213,6 @@ public enum RestRequestResultErrorCode { + " from request header X-ClientTraceId.", ServiceUnavailableException::new, RestRequestHttpResponseCode.SERVICE_UNAVAILABLE); - ; /** A map for retrieving the enum instances by code. */ private static final Map instanceByCode; @@ -255,9 +254,9 @@ public enum RestRequestResultErrorCode { * * @param code The actual numeric close code. * @param meaning The textual meaning. - * @param azureExceptionInstantiation The azure exception instantiator that produces instances to + * @param azureExceptionInstantiation The azure exception instantiating that produces instances to * throw for this kind of result code. - * @param responseCode The response code for which the given instantiator should be used. + * @param responseCode The response code for which the given instantiating should be used. */ RestRequestResultErrorCode( int code, @@ -312,9 +311,9 @@ public String getReference() { } /** - * Gets the response code for which the given instantiator should be used. + * Gets the response code for which the given instantiating should be used. * - * @return The response code for which the given instantiator should be used. + * @return The response code for which the given instantiating should be used. */ public RestRequestHttpResponseCode getResponseCode() { return responseCode; diff --git a/src/test/java/io/github/brenoepics/at4j/core/AzureApiImplTest.java b/src/test/java/io/github/brenoepics/at4j/core/AzureApiImplTest.java index ce5f4c1..03d636f 100644 --- a/src/test/java/io/github/brenoepics/at4j/core/AzureApiImplTest.java +++ b/src/test/java/io/github/brenoepics/at4j/core/AzureApiImplTest.java @@ -1,5 +1,6 @@ package io.github.brenoepics.at4j.core; +import io.github.brenoepics.at4j.AzureApi; import io.github.brenoepics.at4j.AzureApiBuilder; import io.github.brenoepics.at4j.core.exceptions.AzureException; import io.github.brenoepics.at4j.data.request.TranslateParams; @@ -14,14 +15,14 @@ import static org.junit.jupiter.api.Assertions.*; -class AzureApiImplTest { +class AzureApiImplTest { - @Mock private AzureApiImpl azureApi; + @Mock private AzureApi azureApi; private TranslateParams translateParams; @BeforeEach public void setup() { - azureApi = (AzureApiImpl) new AzureApiBuilder().setKey("testKey").build(); + azureApi = new AzureApiBuilder().setKey("testKey").build(); translateParams = new TranslateParams("Hello", Collections.singleton("pt")).setSourceLanguage("en"); } diff --git a/src/test/java/io/github/brenoepics/at4j/core/exceptions/AzureExceptionTest.java b/src/test/java/io/github/brenoepics/at4j/core/exceptions/AzureExceptionTest.java index fe2ae73..ea27afb 100644 --- a/src/test/java/io/github/brenoepics/at4j/core/exceptions/AzureExceptionTest.java +++ b/src/test/java/io/github/brenoepics/at4j/core/exceptions/AzureExceptionTest.java @@ -1,7 +1,7 @@ package io.github.brenoepics.at4j.core.exceptions; +import io.github.brenoepics.at4j.AzureApi; import io.github.brenoepics.at4j.azure.BaseURL; -import io.github.brenoepics.at4j.core.AzureApiImpl; import io.github.brenoepics.at4j.util.rest.*; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -18,7 +18,7 @@ class AzureExceptionTest { - @Mock private AzureApiImpl api; + @Mock private AzureApi api; @Mock private RestRequestResult result; private RestRequestInfo request; diff --git a/src/test/java/io/github/brenoepics/at4j/core/ratelimit/RateLimitManagerTest.java b/src/test/java/io/github/brenoepics/at4j/core/ratelimit/RateLimitManagerTest.java index 0749787..9cd9965 100644 --- a/src/test/java/io/github/brenoepics/at4j/core/ratelimit/RateLimitManagerTest.java +++ b/src/test/java/io/github/brenoepics/at4j/core/ratelimit/RateLimitManagerTest.java @@ -3,6 +3,7 @@ import static org.junit.jupiter.api.Assertions.*; import static org.mockito.Mockito.*; +import io.github.brenoepics.at4j.AzureApi; import io.github.brenoepics.at4j.core.AzureApiImpl; import io.github.brenoepics.at4j.util.rest.RestEndpoint; import io.github.brenoepics.at4j.util.rest.RestRequest; @@ -16,10 +17,10 @@ import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -class RateLimitManagerTest { - private AzureApiImpl api; +class RateLimitManagerTest { + private AzureApi api; private RestRequest request; - private RateLimitManager rateLimitManager; + private RateLimitManager rateLimitManager; @BeforeEach public void setUp() { @@ -38,13 +39,13 @@ void testQueueRequest() { @Test void testSearchBucket() { when(request.getEndpoint()).thenReturn(RestEndpoint.LANGUAGES); - Optional> bucket = rateLimitManager.searchBucket(request); + Optional> bucket = rateLimitManager.searchBucket(request); assertTrue(bucket.isPresent()); } @Test void testWaitUntilSpaceGetsAvailable() { - RateLimitBucket bucket = new RateLimitBucket<>(RestEndpoint.LANGUAGES); + RateLimitBucket bucket = new RateLimitBucket<>(RestEndpoint.LANGUAGES); bucket.setRateLimitRemaining(1000); bucket.setRateLimitResetTimestamp(System.currentTimeMillis() + 1000); assertDoesNotThrow(() -> rateLimitManager.waitUntilSpaceGetsAvailable(bucket)); @@ -73,7 +74,7 @@ void handleResponseWhenStatusCodeIsNot429() { HttpHeaders headers = mock(HttpHeaders.class); when(headers.firstValue(RateLimitManager.RATE_LIMITED_HEADER)).thenReturn(Optional.of("1")); when(headers.firstValue(RateLimitManager.RATE_LIMIT_RESET_HEADER)).thenReturn(Optional.of("0")); - RateLimitBucket bucket = new RateLimitBucket<>(RestEndpoint.LANGUAGES); + RateLimitBucket bucket = new RateLimitBucket<>(RestEndpoint.LANGUAGES); RestRequestResult result = mock(RestRequestResult.class); when(result.getResponse()).thenReturn(mock(HttpResponse.class)); when(result.getResponse().statusCode()).thenReturn(200); @@ -95,7 +96,7 @@ void queueRequestShouldSubmitToExecutorServiceWhenBucketIsPresent() { @Test void searchBucketShouldReturnBucketWhenBucketMatchesRequest() { when(request.getEndpoint()).thenReturn(RestEndpoint.LANGUAGES); - Optional> bucket = rateLimitManager.searchBucket(request); + Optional> bucket = rateLimitManager.searchBucket(request); assertTrue(bucket.isPresent()); } @@ -104,7 +105,7 @@ void handleResponseShouldUpdateBucketWhenStatusCodeIsNot429() { HttpHeaders headers = mock(HttpHeaders.class); when(headers.firstValue(RateLimitManager.RATE_LIMITED_HEADER)).thenReturn(Optional.of("1")); when(headers.firstValue(RateLimitManager.RATE_LIMIT_RESET_HEADER)).thenReturn(Optional.of("0")); - RateLimitBucket bucket = new RateLimitBucket<>(RestEndpoint.LANGUAGES); + RateLimitBucket bucket = new RateLimitBucket<>(RestEndpoint.LANGUAGES); RestRequestResult result = mock(RestRequestResult.class); when(result.getResponse()).thenReturn(mock(HttpResponse.class)); when(result.getResponse().statusCode()).thenReturn(200); @@ -122,7 +123,7 @@ void handleResponseShouldHandleCloudFlareWhenStatusCodeIs429AndViaHeaderIsNotPre when(headers.firstValue("Via")).thenReturn(Optional.empty()); when(headers.firstValue(RateLimitManager.RATE_LIMITED_HEADER_CLOUDFLARE)) .thenReturn(Optional.of("10")); - RateLimitBucket bucket = new RateLimitBucket<>(RestEndpoint.LANGUAGES); + RateLimitBucket bucket = new RateLimitBucket<>(RestEndpoint.LANGUAGES); RestRequestResult result = mock(RestRequestResult.class); when(result.getResponse()).thenReturn(mock(HttpResponse.class)); when(result.getResponse().statusCode()).thenReturn(429); diff --git a/src/test/java/io/github/brenoepics/at4j/util/rest/RestRequestTest.java b/src/test/java/io/github/brenoepics/at4j/util/rest/RestRequestTest.java index e8615f7..3ae239e 100644 --- a/src/test/java/io/github/brenoepics/at4j/util/rest/RestRequestTest.java +++ b/src/test/java/io/github/brenoepics/at4j/util/rest/RestRequestTest.java @@ -1,6 +1,6 @@ package io.github.brenoepics.at4j.util.rest; -import io.github.brenoepics.at4j.core.AzureApiImpl; +import io.github.brenoepics.at4j.AzureApi; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayName; @@ -11,7 +11,7 @@ class RestRequestTest { - @Mock private AzureApiImpl api; + @Mock private AzureApi api; @Mock private RestMethod method; @Mock private RestEndpoint endpoint; @@ -29,6 +29,7 @@ void shouldAddQueryParameter() { restRequest.addQueryParameter("to", "pt"); restRequest.addQueryParameter("to", "es"); restRequest.addQueryParameter("to", "fr"); + restRequest.includeAuthorizationHeader(false); Assertions.assertTrue(restRequest.getQueryParameters().containsKey("to")); Assertions.assertTrue( restRequest.getQueryParameters().get("to").containsAll(Arrays.asList("pt", "es", "fr")));