From 5db78fa5cc53993972807180bbccc5823624b2c9 Mon Sep 17 00:00:00 2001 From: Peter Nied Date: Wed, 7 Aug 2024 20:36:25 +0000 Subject: [PATCH] Cleanup code style on PR Signed-off-by: Peter Nied --- .../java/com/rfs/common/InvalidResponse.java | 86 ++++++++---------- .../java/com/rfs/common/OpenSearchClient.java | 11 ++- .../version_os_2_11/IndexCreator_OS_2_11.java | 4 +- .../com/rfs/common/InvalidResponseTest.java | 91 ++++++++++++------- 4 files changed, 108 insertions(+), 84 deletions(-) diff --git a/RFS/src/main/java/com/rfs/common/InvalidResponse.java b/RFS/src/main/java/com/rfs/common/InvalidResponse.java index 11469203b..037f13be5 100644 --- a/RFS/src/main/java/com/rfs/common/InvalidResponse.java +++ b/RFS/src/main/java/com/rfs/common/InvalidResponse.java @@ -1,16 +1,16 @@ package com.rfs.common; -import java.util.Set; import java.util.ArrayList; import java.util.Map; -import java.util.Optional; -import java.util.stream.Collectors; import java.util.Map.Entry; +import java.util.Optional; +import java.util.Set; import java.util.regex.Pattern; +import java.util.stream.Collectors; import com.fasterxml.jackson.databind.JsonNode; -import com.rfs.common.http.HttpResponse; +import com.rfs.common.http.HttpResponse; import lombok.extern.slf4j.Slf4j; @Slf4j @@ -32,37 +32,32 @@ public Set getIllegalArguments() { var interimResults = new ArrayList>(); var bodyNode = OpenSearchClient.objectMapper.readTree(response.body); - var errorBody = Optional.ofNullable(bodyNode) - .map(node -> node.get("error")); + var errorBody = Optional.ofNullable(bodyNode).map(node -> node.get("error")); // Check high level cause - errorBody - .map(InvalidResponse::getUnknownSetting) - .ifPresent(interimResults::add); + errorBody.map(InvalidResponse::getUnknownSetting).ifPresent(interimResults::add); // Check root cause errors - errorBody - .map(node -> node.get("root_cause")) - .ifPresent(nodes -> { - nodes.forEach(node -> { - Optional.of(node) - .map(InvalidResponse::getUnknownSetting) - .ifPresent(interimResults::add); - }); - }); - + errorBody.map(node -> node.get("root_cause")).ifPresent(nodes -> { + nodes.forEach( + node -> { + Optional.of(node).map(InvalidResponse::getUnknownSetting).ifPresent(interimResults::add); + } + ); + }); + // Check all suppressed errors - errorBody - .map(node -> node.get("suppressed")) - .ifPresent(nodes -> { - nodes.forEach(node -> { - Optional.of(node) - .map(InvalidResponse::getUnknownSetting) - .ifPresent(interimResults::add); - }); - }); + errorBody.map(node -> node.get("suppressed")).ifPresent(nodes -> { + nodes.forEach( + node -> { + Optional.of(node).map(InvalidResponse::getUnknownSetting).ifPresent(interimResults::add); + } + ); + }); - var onlyExpectedErrors = interimResults.stream().map(Entry::getKey).allMatch("illegal_argument_exception"::equals); + var onlyExpectedErrors = interimResults.stream() + .map(Entry::getKey) + .allMatch("illegal_argument_exception"::equals); if (!onlyExpectedErrors) { log.warn("Expecting only invalid argument errors, found additional error types " + interimResults); return Set.of(); @@ -76,23 +71,20 @@ public Set getIllegalArguments() { } private static Map.Entry getUnknownSetting(JsonNode json) { - return Optional.ofNullable(json) - .map(node -> { - var typeNode = node.get("type"); - var reasonNode = node.get("reason"); - if (typeNode == null || reasonNode == null) { - return null; - } - return Map.entry(typeNode, reasonNode); - }) - .map(entry -> { - var matcher = unknownSetting.matcher(entry.getValue().asText()); - if (!matcher.matches()) { - return null; - } + return Optional.ofNullable(json).map(node -> { + var typeNode = node.get("type"); + var reasonNode = node.get("reason"); + if (typeNode == null || reasonNode == null) { + return null; + } + return Map.entry(typeNode, reasonNode); + }).map(entry -> { + var matcher = unknownSetting.matcher(entry.getValue().asText()); + if (!matcher.matches()) { + return null; + } - return Map.entry(entry.getKey().asText(), matcher.group(1)); - }) - .orElse(null); + return Map.entry(entry.getKey().asText(), matcher.group(1)); + }).orElse(null); } -} \ No newline at end of file +} diff --git a/RFS/src/main/java/com/rfs/common/OpenSearchClient.java b/RFS/src/main/java/com/rfs/common/OpenSearchClient.java index 63a12749f..5ba073dbc 100644 --- a/RFS/src/main/java/com/rfs/common/OpenSearchClient.java +++ b/RFS/src/main/java/com/rfs/common/OpenSearchClient.java @@ -114,12 +114,13 @@ private Optional createObjectIdempotent( + " should have been thrown."); boolean objectDoesNotExist = getResponse.statusCode == HttpURLConnection.HTTP_NOT_FOUND; if (objectDoesNotExist) { - logger.debug("Creating object " + objectPath + "\r\n" + settings.toPrettyString()); client.putAsync(objectPath, settings.toString(), context.createCheckRequestContext()).flatMap(resp -> { if (resp.statusCode == HttpURLConnection.HTTP_OK) { return Mono.just(resp); } else if (resp.statusCode == HttpURLConnection.HTTP_BAD_REQUEST) { - return Mono.error(new InvalidResponse("Create object failed for " + objectPath + "\r\n" + resp.body, resp)); + return Mono.error( + new InvalidResponse("Create object failed for " + objectPath + "\r\n" + resp.body, resp) + ); } else { String errorMessage = ("Could not create object: " + objectPath @@ -133,7 +134,11 @@ private Optional createObjectIdempotent( } }) .doOnError(e -> logger.error(e.getMessage())) - .retryWhen(Retry.backoff(3, Duration.ofSeconds(1)).maxBackoff(Duration.ofSeconds(10)).filter(OperationFailed.class::isInstance)) + .retryWhen( + Retry.backoff(3, Duration.ofSeconds(1)) + .maxBackoff(Duration.ofSeconds(10)) + .filter(OperationFailed.class::isInstance) + ) .block(); return Optional.of(settings); diff --git a/RFS/src/main/java/com/rfs/version_os_2_11/IndexCreator_OS_2_11.java b/RFS/src/main/java/com/rfs/version_os_2_11/IndexCreator_OS_2_11.java index b69aa20a0..0ee71dddb 100644 --- a/RFS/src/main/java/com/rfs/version_os_2_11/IndexCreator_OS_2_11.java +++ b/RFS/src/main/java/com/rfs/version_os_2_11/IndexCreator_OS_2_11.java @@ -2,7 +2,6 @@ import java.util.Optional; -import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.node.ObjectNode; @@ -11,7 +10,6 @@ import com.rfs.common.InvalidResponse; import com.rfs.common.OpenSearchClient; import com.rfs.models.IndexMetadata; - import lombok.extern.slf4j.Slf4j; @Slf4j @@ -61,7 +59,7 @@ public Optional create( log.warn("Expecting all retryable errors to start with 'index.', instead saw " + illegalArgument); return Optional.empty(); } - + var shortenedIllegalArgument = illegalArgument.replaceFirst("index.", ""); removeFieldsByPath(settings, shortenedIllegalArgument); } diff --git a/RFS/src/test/java/com/rfs/common/InvalidResponseTest.java b/RFS/src/test/java/com/rfs/common/InvalidResponseTest.java index b491de046..74d6bd4a2 100644 --- a/RFS/src/test/java/com/rfs/common/InvalidResponseTest.java +++ b/RFS/src/test/java/com/rfs/common/InvalidResponseTest.java @@ -1,12 +1,14 @@ package com.rfs.common; + +import java.util.Map; + import org.junit.jupiter.api.Test; +import com.rfs.common.http.HttpResponse; + +import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.containsInAnyOrder; import static org.hamcrest.Matchers.empty; -import static org.hamcrest.MatcherAssert.assertThat; - -import java.util.Map; -import com.rfs.common.http.HttpResponse; class InvalidResponseTest { @@ -29,9 +31,12 @@ void testGetIllegalArguments() { var errorBody = "{\r\n" + // " \"error\": {\r\n" + // " \"type\": \"illegal_argument_exception\",\r\n" + // - " \"reason\": \"unknown setting [index.creation_date] please check that any required plugins are installed, or check the breaking changes documentation for removed settings\"\r\n" + // - " },\r\n" + // - " \"status\": 400\r\n" + // + " \"reason\": \"unknown setting [index.creation_date] please check that any required plugins are installed, or check the breaking changes documentation for removed settings\"\r\n" + + // + " },\r\n" + + // + " \"status\": 400\r\n" + + // "}"; var response = new HttpResponse(200, "statusText", Map.of(), errorBody); var iar = new InvalidResponse("ignored", response); @@ -48,13 +53,20 @@ void testGetIllegalArguments_inRootCause() { " \"root_cause\": [\r\n" + // " {\r\n" + // " \"type\": \"illegal_argument_exception\",\r\n" + // - " \"reason\": \"unknown setting [index.creation_date] please check that any required plugins are installed, or check the breaking changes documentation for removed settings\"\r\n" + // - " }\r\n" + // - " ],\r\n" + // - " \"type\": \"illegal_argument_exception\",\r\n" + // - " \"reason\": \"unknown setting [index.creation_date] please check that any required plugins are installed, or check the breaking changes documentation for removed settings\"\r\n" + // - " },\r\n" + // - " \"status\": 400\r\n" + // + " \"reason\": \"unknown setting [index.creation_date] please check that any required plugins are installed, or check the breaking changes documentation for removed settings\"\r\n" + + // + " }\r\n" + + // + " ],\r\n" + + // + " \"type\": \"illegal_argument_exception\",\r\n" + + // + " \"reason\": \"unknown setting [index.creation_date] please check that any required plugins are installed, or check the breaking changes documentation for removed settings\"\r\n" + + // + " },\r\n" + + // + " \"status\": 400\r\n" + + // "}"; var response = new HttpResponse(200, "statusText", Map.of(), errorBody); var iar = new InvalidResponse("ignored", response); @@ -71,23 +83,40 @@ void testGetIllegalArguments_inRootCauseAndSuppressed() { " \"root_cause\": [\r\n" + // " {\r\n" + // " \"type\": \"illegal_argument_exception\",\r\n" + // - " \"reason\": \"unknown setting [index.creation_date] please check that any required plugins are installed, or check the breaking changes documentation for removed settings\"\r\n" + // - " }\r\n" + // - " ],\r\n" + // - " \"type\": \"illegal_argument_exception\",\r\n" + // - " \"reason\": \"unknown setting [index.creation_date] please check that any required plugins are installed, or check the breaking changes documentation for removed settings\",\r\n" + // - " \"suppressed\": [\r\n" + // - " {\r\n" + // - " \"type\": \"illegal_argument_exception\",\r\n" + // - " \"reason\": \"unknown setting [index.lifecycle.name] please check that any required plugins are installed, or check the breaking changes documentation for removed settings\"\r\n" + // - " },\r\n" + // - " {\r\n" + // - " \"type\": \"illegal_argument_exception\",\r\n" + // - " \"reason\": \"unknown setting [index.provided_name] please check that any required plugins are installed, or check the breaking changes documentation for removed settings\"\r\n" + // - " }\r\n" + // - " ]\r\n" + // - " },\r\n" + // - " \"status\": 400\r\n" + // + " \"reason\": \"unknown setting [index.creation_date] please check that any required plugins are installed, or check the breaking changes documentation for removed settings\"\r\n" + + // + " }\r\n" + + // + " ],\r\n" + + // + " \"type\": \"illegal_argument_exception\",\r\n" + + // + " \"reason\": \"unknown setting [index.creation_date] please check that any required plugins are installed, or check the breaking changes documentation for removed settings\",\r\n" + + // + " \"suppressed\": [\r\n" + + // + " {\r\n" + + // + " \"type\": \"illegal_argument_exception\",\r\n" + + // + " \"reason\": \"unknown setting [index.lifecycle.name] please check that any required plugins are installed, or check the breaking changes documentation for removed settings\"\r\n" + + // + " },\r\n" + + // + " {\r\n" + + // + " \"type\": \"illegal_argument_exception\",\r\n" + + // + " \"reason\": \"unknown setting [index.provided_name] please check that any required plugins are installed, or check the breaking changes documentation for removed settings\"\r\n" + + // + " }\r\n" + + // + " ]\r\n" + + // + " },\r\n" + + // + " \"status\": 400\r\n" + + // "}"; var response = new HttpResponse(200, "statusText", Map.of(), errorBody); var iar = new InvalidResponse("ignored", response);