From e460c4451a60432c76ef8d4f91085164ec3b1b62 Mon Sep 17 00:00:00 2001 From: Oleg Zhurakousky Date: Wed, 16 Apr 2025 14:38:31 +0200 Subject: [PATCH] GH-1259 Fix formatting of previous commit --- .../function/adapter/gcp/FunctionInvoker.java | 48 +++++++++---------- .../gcp/FunctionInvokerBackgroundTests.java | 6 +-- .../adapter/gcp/FunctionInvokerHttpTests.java | 6 +-- 3 files changed, 28 insertions(+), 32 deletions(-) diff --git a/spring-cloud-function-adapters/spring-cloud-function-adapter-gcp/src/main/java/org/springframework/cloud/function/adapter/gcp/FunctionInvoker.java b/spring-cloud-function-adapters/spring-cloud-function-adapter-gcp/src/main/java/org/springframework/cloud/function/adapter/gcp/FunctionInvoker.java index 72b819557..a10aa2ac4 100644 --- a/spring-cloud-function-adapters/spring-cloud-function-adapter-gcp/src/main/java/org/springframework/cloud/function/adapter/gcp/FunctionInvoker.java +++ b/spring-cloud-function-adapters/spring-cloud-function-adapter-gcp/src/main/java/org/springframework/cloud/function/adapter/gcp/FunctionInvoker.java @@ -33,12 +33,11 @@ import com.google.cloud.functions.HttpFunction; import com.google.cloud.functions.HttpRequest; import com.google.cloud.functions.HttpResponse; import com.google.cloud.functions.RawBackgroundFunction; - -import reactor.core.publisher.Flux; - import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.reactivestreams.Publisher; +import reactor.core.publisher.Flux; + import org.springframework.boot.SpringApplication; import org.springframework.boot.WebApplicationType; import org.springframework.cloud.function.context.FunctionCatalog; @@ -132,12 +131,7 @@ public class FunctionInvoker implements HttpFunction, RawBackgroundFunction { Object resultObject = function.apply(message); if (resultObject != null) { - Message result = null; - if (resultObject instanceof Publisher) { - result = getResultFromPublisher(resultObject); - } else { - result = (Message) resultObject; - } + Message result = resultObject instanceof Publisher ? getResultFromPublisher(resultObject) : (Message) resultObject; buildHttpResponse(httpRequest, httpResponse, result); } @@ -163,7 +157,8 @@ public class FunctionInvoker implements HttpFunction, RawBackgroundFunction { Message result = null; if (resultObject instanceof Publisher) { result = getResultFromPublisher(resultObject); - } else { + } + else { result = (Message) resultObject; } @@ -172,19 +167,19 @@ public class FunctionInvoker implements HttpFunction, RawBackgroundFunction { } } - /** - * This method build the http response from service - * - * @throws IOException + /* + * This method build the http response from service. */ private void buildHttpResponse(HttpRequest httpRequest, HttpResponse httpResponse, Message result) throws IOException { MessageHeaders headers = result.getHeaders(); if (result.getHeaders().containsKey(MessageHeaders.CONTENT_TYPE)) { httpResponse.setContentType(result.getHeaders().get(MessageHeaders.CONTENT_TYPE).toString()); - } else if (result.getHeaders().containsKey("Content-Type")) { + } + else if (result.getHeaders().containsKey("Content-Type")) { httpResponse.setContentType(result.getHeaders().get("Content-Type").toString()); - } else { + } + else { httpRequest.getContentType().ifPresent(contentType -> httpResponse.setContentType(contentType)); } String content = result.getPayload() instanceof String strPayload ? strPayload @@ -196,7 +191,8 @@ public class FunctionInvoker implements HttpFunction, RawBackgroundFunction { String headerValue = ((Collection) values).stream().map(item -> item.toString()) .collect(Collectors.joining(",")); httpResponse.appendHeader(header.getKey(), headerValue); - } else { + } + else { httpResponse.appendHeader(header.getKey(), header.getValue().toString()); } } @@ -204,15 +200,16 @@ public class FunctionInvoker implements HttpFunction, RawBackgroundFunction { if (headers.containsKey(HTTP_STATUS_CODE)) { if (headers.get(HTTP_STATUS_CODE) instanceof Integer) { httpResponse.setStatusCode((int) headers.get(HTTP_STATUS_CODE)); - } else { + } + else { log.warn("The statusCode should be an Integer value"); } } } - /** - * This methd get the result from reactor's publisher - * + /* + * This methd get the result from reactor's publisher. + * * For reference: https://github.com/spring-cloud/spring-cloud-function/blob/main/spring-cloud-function-adapters/spring-cloud-function-adapter-aws/src/main/java/org/springframework/cloud/function/adapter/aws/AWSLambdaUtils.java */ private Message getResultFromPublisher(Object resultObject) { @@ -223,7 +220,8 @@ public class FunctionInvoker implements HttpFunction, RawBackgroundFunction { if (item instanceof Message messageItem) { results.add(convertFromJsonIfNecessary(messageItem.getPayload())); lastMessage = messageItem; - } else { + } + else { results.add(convertFromJsonIfNecessary(item)); } } @@ -231,9 +229,11 @@ public class FunctionInvoker implements HttpFunction, RawBackgroundFunction { byte[] resultsPayload; if (results.size() == 1) { resultsPayload = jsonMapper.toJson(results.get(0)); - } else if (results.size() > 1) { + } + else if (results.size() > 1) { resultsPayload = jsonMapper.toJson(results); - } else { + } + else { resultsPayload = null; } diff --git a/spring-cloud-function-adapters/spring-cloud-function-adapter-gcp/src/test/java/org/springframework/cloud/function/adapter/gcp/FunctionInvokerBackgroundTests.java b/spring-cloud-function-adapters/spring-cloud-function-adapter-gcp/src/test/java/org/springframework/cloud/function/adapter/gcp/FunctionInvokerBackgroundTests.java index 901a21c88..0c4bc4698 100644 --- a/spring-cloud-function-adapters/spring-cloud-function-adapter-gcp/src/test/java/org/springframework/cloud/function/adapter/gcp/FunctionInvokerBackgroundTests.java +++ b/spring-cloud-function-adapters/spring-cloud-function-adapter-gcp/src/test/java/org/springframework/cloud/function/adapter/gcp/FunctionInvokerBackgroundTests.java @@ -22,12 +22,10 @@ import java.util.function.Supplier; import com.github.blindpirate.extensions.CaptureSystemOutput; import com.google.gson.Gson; - -import reactor.core.publisher.Flux; -import reactor.core.publisher.Mono; - import org.hamcrest.Matchers; import org.junit.jupiter.api.Test; +import reactor.core.publisher.Flux; +import reactor.core.publisher.Mono; import org.springframework.cloud.function.context.config.ContextFunctionCatalogAutoConfiguration; import org.springframework.cloud.function.json.JsonMapper; diff --git a/spring-cloud-function-adapters/spring-cloud-function-adapter-gcp/src/test/java/org/springframework/cloud/function/adapter/gcp/FunctionInvokerHttpTests.java b/spring-cloud-function-adapters/spring-cloud-function-adapter-gcp/src/test/java/org/springframework/cloud/function/adapter/gcp/FunctionInvokerHttpTests.java index a1b6dc41b..51f1324e7 100644 --- a/spring-cloud-function-adapters/spring-cloud-function-adapter-gcp/src/test/java/org/springframework/cloud/function/adapter/gcp/FunctionInvokerHttpTests.java +++ b/spring-cloud-function-adapters/spring-cloud-function-adapter-gcp/src/test/java/org/springframework/cloud/function/adapter/gcp/FunctionInvokerHttpTests.java @@ -30,14 +30,12 @@ import java.util.function.Supplier; import com.google.cloud.functions.HttpRequest; import com.google.cloud.functions.HttpResponse; import com.google.gson.Gson; - -import reactor.core.publisher.Flux; -import reactor.core.publisher.Mono; - import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.Mockito; +import reactor.core.publisher.Flux; +import reactor.core.publisher.Mono; import org.springframework.boot.test.system.CapturedOutput; import org.springframework.boot.test.system.OutputCaptureExtension;