From 027dcc32e3dc34013ea537e21c47724b79e6e150 Mon Sep 17 00:00:00 2001 From: BrenoEpic Date: Thu, 8 Feb 2024 13:06:11 -0300 Subject: [PATCH] fix!: Qodana suggestions --- .../io/github/brenoepics/at4j/AzureApi.java | 2 +- .../brenoepics/at4j/core/AzureApiImpl.java | 16 ++++----- .../at4j/core/ratelimit/RateLimitBucket.java | 6 ++-- .../at4j/core/ratelimit/RateLimitManager.java | 35 +++++++++---------- .../request/AvailableLanguagesParams.java | 3 +- .../data/request/DetectLanguageParams.java | 3 +- .../at4j/data/request/TranslateParams.java | 3 +- .../at4j/util/rest/RestRequest.java | 25 +++++++------ .../at4j/util/rest/RestRequestHandler.java | 12 +++---- .../rest/RestRequestResponseInfoImpl.java | 8 ++--- .../at4j/util/rest/RestRequestResult.java | 8 ++--- .../core/exceptions/AzureExceptionTest.java | 4 +-- .../core/ratelimit/RateLimitManagerTest.java | 16 ++++----- .../request/DetectLanguageParamsTest.java | 16 ++++----- .../at4j/util/rest/RestRequestTest.java | 6 ++-- 15 files changed, 79 insertions(+), 84 deletions(-) diff --git a/src/main/java/io/github/brenoepics/at4j/AzureApi.java b/src/main/java/io/github/brenoepics/at4j/AzureApi.java index 6309916..bf56496 100644 --- a/src/main/java/io/github/brenoepics/at4j/AzureApi.java +++ b/src/main/java/io/github/brenoepics/at4j/AzureApi.java @@ -107,7 +107,7 @@ CompletableFuture>> getAvailableLanguages( * * @return RateLimitManager - The ratelimit manager. */ - RateLimitManager getRatelimitManager(); + RateLimitManager getRatelimitManager(); /** * Gets the HttpClient. 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 863407b..839ca25 100644 --- a/src/main/java/io/github/brenoepics/at4j/core/AzureApiImpl.java +++ b/src/main/java/io/github/brenoepics/at4j/core/AzureApiImpl.java @@ -24,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; @@ -42,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(); @@ -89,8 +89,7 @@ public CompletableFuture> translate(TranslateParam return CompletableFuture.completedFuture(Optional.empty()); } - RestRequest> request = - new RestRequest<>(this, RestMethod.POST, RestEndpoint.TRANSLATE); + RestRequest request = new RestRequest(this, RestMethod.POST, RestEndpoint.TRANSLATE); request.setBody(params.getBody()); request.addQueryParameters(params.getQueryParameters()); @@ -107,8 +106,7 @@ public CompletableFuture> detectLanguage(DetectLanguage return CompletableFuture.completedFuture(Optional.empty()); } - RestRequest> request = - new RestRequest<>(this, RestMethod.POST, RestEndpoint.DETECT); + RestRequest request = new RestRequest(this, RestMethod.POST, RestEndpoint.DETECT); request.setBody(params.getBody()); return request.execute(params::handleResponse); @@ -117,8 +115,7 @@ public CompletableFuture> detectLanguage(DetectLanguage @Override public CompletableFuture>> getAvailableLanguages( AvailableLanguagesParams params) { - RestRequest>> request = - new RestRequest<>(this, RestMethod.GET, RestEndpoint.LANGUAGES, false); + RestRequest request = new RestRequest(this, RestMethod.GET, RestEndpoint.LANGUAGES, false); request.addQueryParameter("scope", params.getScope()); if (params.getSourceLanguage() != null) { @@ -158,8 +155,9 @@ public ObjectMapper getObjectMapper() { * * @return RateLimitManager - The used RateLimitManager. */ + @SuppressWarnings("unchecked") @Override - public RateLimitManager getRatelimitManager() { + 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 054da39..ccd195f 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 @@ -10,7 +10,7 @@ */ public class RateLimitBucket { - private final ConcurrentLinkedQueue> requestQueue = new ConcurrentLinkedQueue<>(); + private final ConcurrentLinkedQueue requestQueue = new ConcurrentLinkedQueue<>(); private final RestEndpoint endpoint; @@ -31,7 +31,7 @@ public RateLimitBucket(RestEndpoint endpoint) { * * @param request The request to add. */ - public void addRequestToQueue(RestRequest request) { + public void addRequestToQueue(RestRequest request) { requestQueue.add(request); } @@ -45,7 +45,7 @@ public void pollRequestFromQueue() { * * @return The peeked request. */ - public RestRequest peekRequestFromQueue() { + public RestRequest peekRequestFromQueue() { return requestQueue.peek(); } 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 66f103f..311b66d 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 @@ -56,7 +56,7 @@ public RateLimitManager(AzureApi api) { * * @param request The request to queue. */ - public void queueRequest(RestRequest request) { + public void queueRequest(RestRequest request) { Optional> searchBucket = searchBucket(request); if (searchBucket.isEmpty()) { @@ -72,13 +72,13 @@ public void queueRequest(RestRequest request) { * @param bucket The bucket to submit the request to. */ private void submitRequest(RateLimitBucket bucket) { - RestRequest currentRequest = bucket.peekRequestFromQueue(); - RestRequestResult result = null; + RestRequest currentRequest = bucket.peekRequestFromQueue(); + RestRequestResult result = null; long responseTimestamp = System.currentTimeMillis(); while (currentRequest != null) { - RestRequestHandler newResult = + RestRequestHandler newResult = handleCurrentRequest(result, currentRequest, bucket, responseTimestamp); result = newResult.getResult(); @@ -96,9 +96,9 @@ private void submitRequest(RateLimitBucket bucket) { * @param responseTimestamp The timestamp directly after the response finished. * @return The result of the current request. */ - RestRequestHandler handleCurrentRequest( - RestRequestResult result, - RestRequest currentRequest, + RestRequestHandler handleCurrentRequest( + RestRequestResult result, + RestRequest currentRequest, RateLimitBucket bucket, long responseTimestamp) { @@ -131,7 +131,7 @@ RestRequestHandler handleCurrentRequest( } } - return new RestRequestHandler<>(result, currentRequest, responseTimestamp); + return new RestRequestHandler(result, currentRequest, responseTimestamp); } /** @@ -164,10 +164,10 @@ 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(); + RestRequest request = bucket.peekRequestFromQueue(); if (request == null) { buckets.remove(bucket); } @@ -182,8 +182,7 @@ RestRequest retryRequest(RateLimitBucket bucket) { * @param t The exception to map. * @return The mapped exception. */ - @SuppressWarnings("unchecked") - private RestRequestResult mapAzureException(Throwable t) { + private RestRequestResult mapAzureException(Throwable t) { return ((AzureException) t) .getResponse() .map(RestRequestResponseInfoImpl.class::cast) @@ -197,7 +196,7 @@ 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); @@ -218,7 +217,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())) @@ -236,8 +235,8 @@ RateLimitBucket getMatchingBucket(RestRequest request) { * @param responseTimestamp The timestamp directly after the response finished. */ void handleResponse( - RestRequest request, - RestRequestResult result, + RestRequest request, + RestRequestResult result, RateLimitBucket bucket, long responseTimestamp) { try { @@ -296,8 +295,8 @@ private void handleCloudFlare(HttpHeaders headers, RateLimitBucket bu * @param headers The headers of the response. */ private void handleRateLimit( - CompletableFuture> request, - RestRequestResult result, + CompletableFuture request, + RestRequestResult result, RateLimitBucket bucket, HttpHeaders headers) { diff --git a/src/main/java/io/github/brenoepics/at4j/data/request/AvailableLanguagesParams.java b/src/main/java/io/github/brenoepics/at4j/data/request/AvailableLanguagesParams.java index 75497c7..4f95665 100644 --- a/src/main/java/io/github/brenoepics/at4j/data/request/AvailableLanguagesParams.java +++ b/src/main/java/io/github/brenoepics/at4j/data/request/AvailableLanguagesParams.java @@ -12,6 +12,7 @@ import java.util.Optional; /** This class represents the parameters for available languages. */ +@SuppressWarnings({"unused", "UnusedReturnValue"}) public class AvailableLanguagesParams { // List of scopes for the languages private List scope = List.of(LanguageScope.TRANSLATION); @@ -64,7 +65,7 @@ public AvailableLanguagesParams setSourceLanguage(String sourceLanguage) { * @param response The response to handle. * @return An optional containing the collection of languages if the response was successful. */ - public Optional> handleResponse(RestRequestResult>> response) { + public Optional> handleResponse(RestRequestResult response) { if (response.getJsonBody().isNull() || !response.getJsonBody().has("translation")) return Optional.empty(); diff --git a/src/main/java/io/github/brenoepics/at4j/data/request/DetectLanguageParams.java b/src/main/java/io/github/brenoepics/at4j/data/request/DetectLanguageParams.java index 6676c64..6d1b832 100644 --- a/src/main/java/io/github/brenoepics/at4j/data/request/DetectLanguageParams.java +++ b/src/main/java/io/github/brenoepics/at4j/data/request/DetectLanguageParams.java @@ -14,6 +14,7 @@ * This class represents the parameters for a language detection request. It contains a single * field, 'text', which is the text to be analyzed for language detection. */ +@SuppressWarnings({"unused", "UnusedReturnValue"}) public class DetectLanguageParams { // The text to be analyzed for language detection @@ -88,7 +89,7 @@ public ArrayNode getBody() { * 'language' */ public Optional handleResponse( - RestRequestResult> response) { + RestRequestResult response) { if (response.getJsonBody().isNull() || response.getJsonBody().isEmpty()) return Optional.empty(); JsonNode jsonBody = response.getJsonBody(); diff --git a/src/main/java/io/github/brenoepics/at4j/data/request/TranslateParams.java b/src/main/java/io/github/brenoepics/at4j/data/request/TranslateParams.java index ac1783c..9450b1f 100644 --- a/src/main/java/io/github/brenoepics/at4j/data/request/TranslateParams.java +++ b/src/main/java/io/github/brenoepics/at4j/data/request/TranslateParams.java @@ -22,6 +22,7 @@ * profanity, text type, alignment, sentence length, source language, target languages, and a * fallback language. */ +@SuppressWarnings({"unused", "UnusedReturnValue"}) public class TranslateParams { // The text to be translated private LinkedHashMap toTranslate; @@ -346,7 +347,7 @@ public JsonNode getBody() { * 'translations'. */ public Optional handleResponse( - RestRequestResult> response) { + RestRequestResult response) { if (response.getJsonBody().isNull() || response.getJsonBody().isEmpty()) return Optional.empty(); JsonNode jsonBody = response.getJsonBody(); 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 34292ec..c5a1a74 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 @@ -18,7 +18,7 @@ import org.apache.logging.log4j.Logger; /** This class is used to wrap a rest request. */ -public class RestRequest { +public class RestRequest { /** The (logger) of this class. */ private static final Logger logger = LoggerUtil.getLogger(RestRequest.class); @@ -32,7 +32,7 @@ public class RestRequest { private final Map headers = new HashMap<>(); private volatile String body = null; - private final CompletableFuture> result = new CompletableFuture<>(); + private final CompletableFuture result = new CompletableFuture<>(); /** The origin of the rest request. */ private final Exception origin; @@ -167,7 +167,7 @@ public Map getHeaders() { * @param body The body of the request. * @return The current instance to chain call methods. */ - public RestRequest setBody(JsonNode body) { + public RestRequest setBody(JsonNode body) { return setBody(body.toString()); } @@ -177,7 +177,7 @@ public RestRequest setBody(JsonNode body) { * @param body The body of the request. * @return The current instance to chain call methods. */ - public RestRequest setBody(String body) { + public RestRequest setBody(String body) { this.body = body; return this; } @@ -197,8 +197,7 @@ public void includeAuthorizationHeader(boolean includeAuthorizationHeader) { * @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) { + public CompletableFuture execute(Function function) { api.getRatelimitManager().queueRequest(this); CompletableFuture future = new CompletableFuture<>(); result.whenComplete( @@ -221,7 +220,7 @@ public CompletableFuture execute(Function, T> function) * * @return Gets the result of this request. */ - public CompletableFuture> getResult() { + public CompletableFuture getResult() { return result; } @@ -252,7 +251,7 @@ public RestRequestInfo asRestRequestInformation() { * @throws AzureException Thrown in case of an error while requesting azure. * @throws IOException Thrown if an error occurs while reading the response. */ - public RestRequestResult executeBlocking() + public RestRequestResult executeBlocking() throws AzureException, IOException, URISyntaxException { URI fullUrl = endpoint.getHttpUrl(api.getBaseURL(), queryParameters); HttpRequest.Builder requestBuilder = HttpRequest.newBuilder().uri(fullUrl); @@ -276,14 +275,14 @@ public RestRequestResult executeBlocking() getApi() .getHttpClient() .sendAsync(requestBuilder.build(), HttpResponse.BodyHandlers.ofString()); - RestRequestResult responseResult = handleResponse(fullUrl, response.join()); + RestRequestResult responseResult = handleResponse(fullUrl, response.join()); result.complete(responseResult); return responseResult; } - private RestRequestResult handleResponse(URI fullUrl, HttpResponse response) + private RestRequestResult handleResponse(URI fullUrl, HttpResponse response) throws IOException, AzureException { - RestRequestResult requestResult = new RestRequestResult<>(this, response); + RestRequestResult requestResult = new RestRequestResult(this, response); String bodyString = requestResult.getStringBody().orElse("empty"); logger.debug( "Sent {} request to {} and received status code {} with body {}", @@ -299,11 +298,11 @@ private RestRequestResult handleResponse(URI fullUrl, HttpResponse re return requestResult; } - private RestRequestResult handleError(int resultCode, RestRequestResult result) + private RestRequestResult handleError(int resultCode, RestRequestResult result) throws AzureException { RestRequestInfo requestInformation = asRestRequestInformation(); RestRequestResponseInfo responseInformation = - new RestRequestResponseInfoImpl<>(requestInformation, result); + new RestRequestResponseInfoImpl(requestInformation, result); Optional responseCodeOptional = RestRequestHttpResponseCode.fromCode(resultCode); 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 26df69a..13619fb 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 @@ -5,11 +5,11 @@ * current request being processed, and the timestamp of the response. * */ -public class RestRequestHandler { +public class RestRequestHandler { // The result of the REST request - private final RestRequestResult result; + private final RestRequestResult result; // The current request being processed - private final RestRequest currentRequest; + private final RestRequest currentRequest; // The timestamp of the response private final long responseTimestamp; @@ -22,7 +22,7 @@ public class RestRequestHandler { * @param responseTimestamp the timestamp of the response */ public RestRequestHandler( - RestRequestResult result, RestRequest currentRequest, long responseTimestamp) { + RestRequestResult result, RestRequest currentRequest, long responseTimestamp) { this.result = result; this.currentRequest = currentRequest; this.responseTimestamp = responseTimestamp; @@ -33,7 +33,7 @@ public RestRequestHandler( * * @return the result of the REST request */ - public RestRequestResult getResult() { + public RestRequestResult getResult() { return result; } @@ -42,7 +42,7 @@ public RestRequestResult getResult() { * * @return the current request being processed */ - public RestRequest getCurrentRequest() { + public RestRequest getCurrentRequest() { return currentRequest; } diff --git a/src/main/java/io/github/brenoepics/at4j/util/rest/RestRequestResponseInfoImpl.java b/src/main/java/io/github/brenoepics/at4j/util/rest/RestRequestResponseInfoImpl.java index ac87ec4..91351c5 100644 --- a/src/main/java/io/github/brenoepics/at4j/util/rest/RestRequestResponseInfoImpl.java +++ b/src/main/java/io/github/brenoepics/at4j/util/rest/RestRequestResponseInfoImpl.java @@ -4,10 +4,10 @@ import java.util.Optional; /** The implementation of {@link RestRequestResponseInfo}. */ -public class RestRequestResponseInfoImpl implements RestRequestResponseInfo { +public class RestRequestResponseInfoImpl implements RestRequestResponseInfo { private final RestRequestInfo request; - private final RestRequestResult restRequestResult; + private final RestRequestResult restRequestResult; /** * Creates a new rest request response information. @@ -16,7 +16,7 @@ public class RestRequestResponseInfoImpl implements RestRequestResponseInfo { * @param restRequestResult The result of the response. */ public RestRequestResponseInfoImpl( - RestRequestInfo request, RestRequestResult restRequestResult) { + RestRequestInfo request, RestRequestResult restRequestResult) { this.request = request; this.restRequestResult = restRequestResult; } @@ -26,7 +26,7 @@ public RestRequestResponseInfoImpl( * * @return The rest request result. */ - public RestRequestResult getRestRequestResult() { + public RestRequestResult getRestRequestResult() { return restRequestResult; } diff --git a/src/main/java/io/github/brenoepics/at4j/util/rest/RestRequestResult.java b/src/main/java/io/github/brenoepics/at4j/util/rest/RestRequestResult.java index 51f1b0d..7b9e247 100644 --- a/src/main/java/io/github/brenoepics/at4j/util/rest/RestRequestResult.java +++ b/src/main/java/io/github/brenoepics/at4j/util/rest/RestRequestResult.java @@ -11,12 +11,12 @@ import org.apache.logging.log4j.Logger; /** The result of a {@link RestRequest}. */ -public class RestRequestResult { +public class RestRequestResult { /** The (logger) of this class. */ private static final Logger logger = LoggerUtil.getLogger(RestRequestResult.class); - private final RestRequest request; + private final RestRequest request; private final HttpResponse response; private final String stringBody; private final JsonNode jsonBody; @@ -28,7 +28,7 @@ public class RestRequestResult { * @param response The response of the RestRequest. * @throws IOException Passed on from {@link HttpResponse#body()}. */ - public RestRequestResult(RestRequest request, HttpResponse response) + public RestRequestResult(RestRequest request, HttpResponse response) throws IOException { this.request = request; this.response = response; @@ -55,7 +55,7 @@ public RestRequestResult(RestRequest request, HttpResponse response) * * @return The Request which belongs to this result. */ - public RestRequest getRequest() { + public RestRequest getRequest() { return request; } 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 ea27afb..9169614 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 @@ -19,7 +19,7 @@ class AzureExceptionTest { @Mock private AzureApi api; - @Mock private RestRequestResult result; + @Mock private RestRequestResult result; private RestRequestInfo request; @BeforeEach @@ -50,7 +50,7 @@ void requestInformationIsRetained() { @Test void responseInformationIsRetained() { RestRequestResponseInfo response = - new RestRequestResponseInfoImpl<>(request, result); + new RestRequestResponseInfoImpl(request, result); AzureException exception = new AzureException(null, null, null, response); assertEquals(Optional.of(response), exception.getResponse()); 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 9cd9965..8e8c941 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 @@ -19,7 +19,7 @@ class RateLimitManagerTest { private AzureApi api; - private RestRequest request; + private RestRequest request; private RateLimitManager rateLimitManager; @BeforeEach @@ -55,7 +55,7 @@ void testWaitUntilSpaceGetsAvailable() { void testRetryRequest() { when(request.getEndpoint()).thenReturn(RestEndpoint.LANGUAGES); rateLimitManager.searchBucket(request); - RestRequest retriedRequest = + RestRequest retriedRequest = rateLimitManager.retryRequest(new RateLimitBucket<>(RestEndpoint.LANGUAGES)); assertNull(retriedRequest); } @@ -64,7 +64,7 @@ void testRetryRequest() { void retryRequestWhenBucketIsEmpty() { when(request.getEndpoint()).thenReturn(RestEndpoint.LANGUAGES); rateLimitManager.searchBucket(request); - RestRequest retriedRequest = + RestRequest retriedRequest = rateLimitManager.retryRequest(new RateLimitBucket<>(RestEndpoint.LANGUAGES)); assertNull(retriedRequest); } @@ -75,11 +75,11 @@ void handleResponseWhenStatusCodeIsNot429() { 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); - RestRequestResult result = mock(RestRequestResult.class); + RestRequestResult result = mock(RestRequestResult.class); when(result.getResponse()).thenReturn(mock(HttpResponse.class)); when(result.getResponse().statusCode()).thenReturn(200); when(result.getResponse().headers()).thenReturn(headers); - CompletableFuture> future = new CompletableFuture<>(); + CompletableFuture future = new CompletableFuture<>(); future.complete(result); when(request.getResult()).thenReturn(future); rateLimitManager.handleResponse(request, result, bucket, System.currentTimeMillis()); @@ -106,11 +106,11 @@ void handleResponseShouldUpdateBucketWhenStatusCodeIsNot429() { 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); - RestRequestResult result = mock(RestRequestResult.class); + RestRequestResult result = mock(RestRequestResult.class); when(result.getResponse()).thenReturn(mock(HttpResponse.class)); when(result.getResponse().statusCode()).thenReturn(200); when(result.getResponse().headers()).thenReturn(headers); - CompletableFuture> future = new CompletableFuture<>(); + CompletableFuture future = new CompletableFuture<>(); future.complete(result); when(request.getResult()).thenReturn(future); rateLimitManager.handleResponse(request, result, bucket, System.currentTimeMillis()); @@ -124,7 +124,7 @@ void handleResponseShouldHandleCloudFlareWhenStatusCodeIs429AndViaHeaderIsNotPre when(headers.firstValue(RateLimitManager.RATE_LIMITED_HEADER_CLOUDFLARE)) .thenReturn(Optional.of("10")); RateLimitBucket bucket = new RateLimitBucket<>(RestEndpoint.LANGUAGES); - RestRequestResult result = mock(RestRequestResult.class); + RestRequestResult result = mock(RestRequestResult.class); when(result.getResponse()).thenReturn(mock(HttpResponse.class)); when(result.getResponse().statusCode()).thenReturn(429); when(result.getResponse().headers()).thenReturn(headers); diff --git a/src/test/java/io/github/brenoepics/at4j/data/request/DetectLanguageParamsTest.java b/src/test/java/io/github/brenoepics/at4j/data/request/DetectLanguageParamsTest.java index 4377cfd..bb50a60 100644 --- a/src/test/java/io/github/brenoepics/at4j/data/request/DetectLanguageParamsTest.java +++ b/src/test/java/io/github/brenoepics/at4j/data/request/DetectLanguageParamsTest.java @@ -4,8 +4,7 @@ import static org.mockito.Mockito.*; import com.fasterxml.jackson.databind.ObjectMapper; -import com.fasterxml.jackson.databind.node.JsonNodeFactory; -import com.fasterxml.jackson.databind.node.ObjectNode; +import io.github.brenoepics.at4j.AzureApi; import io.github.brenoepics.at4j.core.AzureApiImpl; import io.github.brenoepics.at4j.data.response.DetectResponse; import io.github.brenoepics.at4j.util.rest.RestRequest; @@ -14,11 +13,8 @@ import java.net.http.HttpResponse; import java.util.ArrayList; import java.util.Arrays; -import java.util.List; import java.util.Optional; import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.ExtendWith; -import org.junit.jupiter.api.extension.Extension; class DetectLanguageParamsTest { @@ -38,9 +34,9 @@ void getBody_returnsNullForEmptyTexts() { @Test void handleResponse_returnsEmptyOptionalForNullJsonBody() throws IOException { DetectLanguageParams params = new DetectLanguageParams("test text"); - RestRequest> request = mock(RestRequest.class); + RestRequest request = mock(RestRequest.class); HttpResponse httpResponse = mock(HttpResponse.class); - RestRequestResult> response = new RestRequestResult<>(request, httpResponse); + RestRequestResult response = new RestRequestResult(request, httpResponse); Optional result = params.handleResponse(response); assertTrue(result.isEmpty()); } @@ -48,8 +44,8 @@ void handleResponse_returnsEmptyOptionalForNullJsonBody() throws IOException { @Test void handleResponse_returnsDetectResponseForValidJsonBody() throws IOException { DetectLanguageParams params = new DetectLanguageParams("test text"); - RestRequest> request = mock(RestRequest.class); - AzureApiImpl azureApi = mock(AzureApiImpl.class); + RestRequest request = mock(RestRequest.class); + AzureApi azureApi = mock(AzureApiImpl.class); ObjectMapper objectMapper = new ObjectMapper(); when(azureApi.getObjectMapper()).thenReturn(objectMapper); when(request.getApi()).thenReturn(azureApi); @@ -57,7 +53,7 @@ void handleResponse_returnsDetectResponseForValidJsonBody() throws IOException { when(httpResponse.body()) .thenReturn( "[{\"isTranslationSupported\":true,\"isTransliterationSupported\":false,\"language\":\"en\",\"score\":0.97}]"); - RestRequestResult> response = new RestRequestResult<>(request, httpResponse); + RestRequestResult response = new RestRequestResult(request, httpResponse); Optional result = params.handleResponse(response); assertTrue(result.isPresent()); assertEquals("en", result.get().getFirst().getLanguageCode()); 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 3ae239e..a6c6956 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 @@ -9,17 +9,17 @@ import java.util.Arrays; -class RestRequestTest { +class RestRequestTest { @Mock private AzureApi api; @Mock private RestMethod method; @Mock private RestEndpoint endpoint; - private RestRequest restRequest; + private RestRequest restRequest; @BeforeEach void setUp() { - restRequest = new RestRequest<>(api, method, endpoint); + restRequest = new RestRequest(api, method, endpoint); } @Test