Turned on checkstyle

This commit is contained in:
Marcin Grzejszczak
2019-02-01 15:48:32 +01:00
parent 94e9b8f2f8
commit e4b08a083c
268 changed files with 5114 additions and 3993 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2018 the original author or authors.
* Copyright 2012-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -32,7 +32,6 @@ import org.springframework.messaging.MessageHeaders;
import org.springframework.messaging.support.GenericMessage;
/**
*
* @author Dave Syer
* @author Oleg Zhurakousky
*
@@ -66,12 +65,12 @@ public class SpringBootApiGatewayRequestHandler extends
}
private boolean functionAcceptsMessage() {
return inspector.isMessage(function());
return this.inspector.isMessage(function());
}
private Object deserializeBody(String json) {
try {
return mapper.readValue(json, getInputType());
return this.mapper.readValue(json, getInputType());
}
catch (Exception e) {
throw new IllegalStateException("Cannot convert event", e);
@@ -91,13 +90,15 @@ public class SpringBootApiGatewayRequestHandler extends
protected APIGatewayProxyResponseEvent convertOutput(Object output) {
if (functionReturnsMessage(output)) {
Message<?> message = (Message<?>) output;
return new APIGatewayProxyResponseEvent().withStatusCode(
(Integer) message.getHeaders().getOrDefault("statuscode", HttpStatus.OK.value()))
return new APIGatewayProxyResponseEvent()
.withStatusCode((Integer) message.getHeaders()
.getOrDefault("statuscode", HttpStatus.OK.value()))
.withHeaders(toResponseHeaders(message.getHeaders()))
.withBody(serializeBody(message.getPayload()));
}
else {
return new APIGatewayProxyResponseEvent().withStatusCode(HttpStatus.OK.value())
return new APIGatewayProxyResponseEvent()
.withStatusCode(HttpStatus.OK.value())
.withBody(serializeBody(output));
}
@@ -116,7 +117,7 @@ public class SpringBootApiGatewayRequestHandler extends
private String serializeBody(Object body) {
try {
return mapper.writeValueAsString(body);
return this.mapper.writeValueAsString(body);
}
catch (JsonProcessingException e) {
throw new IllegalStateException("Cannot convert output", e);

View File

@@ -1,11 +1,11 @@
/*
* Copyright 2017 the original author or authors.
* Copyright 2012-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
@@ -16,8 +16,6 @@
package org.springframework.cloud.function.adapter.aws;
import static java.util.stream.Collectors.toList;
import java.util.List;
import java.util.stream.Collectors;
@@ -32,7 +30,11 @@ import org.springframework.cloud.function.context.catalog.FunctionInspector;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.GenericMessage;
import static java.util.stream.Collectors.toList;
/**
* @param <E> payload type
* @param <O> response type
* @author Mark Fisher
* @author Halvdan Hoem Grelland
*/
@@ -65,28 +67,26 @@ public class SpringBootKinesisEventHandler<E, O>
if (functionAcceptsMessage()) {
return wrapInMessages(payloads);
} else {
}
else {
return payloads;
}
}
private List<Message<E>> wrapInMessages(List<E> payloads) {
return payloads.stream()
.map(GenericMessage::new)
.collect(Collectors.toList());
return payloads.stream().map(GenericMessage::new).collect(Collectors.toList());
}
private List<E> deserializePayloads(List<KinesisEvent.KinesisEventRecord> records) {
return RecordDeaggregator.deaggregate(records).stream()
.map(this::deserializeUserRecord)
.collect(toList());
.map(this::deserializeUserRecord).collect(toList());
}
@SuppressWarnings("unchecked")
private E deserializeUserRecord(UserRecord userRecord) {
try {
byte[] jsonBytes = userRecord.getData().array();
return (E) mapper.readValue(jsonBytes, getInputType());
return (E) this.mapper.readValue(jsonBytes, getInputType());
}
catch (Exception e) {
throw new IllegalStateException("Cannot convert event", e);
@@ -94,6 +94,7 @@ public class SpringBootKinesisEventHandler<E, O>
}
private boolean functionAcceptsMessage() {
return inspector.isMessage(function());
return this.inspector.isMessage(function());
}
}

View File

@@ -1,11 +1,11 @@
/*
* Copyright 2017 the original author or authors.
* Copyright 2012-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
@@ -22,12 +22,12 @@ import java.util.List;
import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.RequestHandler;
import org.reactivestreams.Publisher;
import reactor.core.publisher.Flux;
/**
* @param <E> event type
* @param <O> result types
* @author Mark Fisher
*/
public class SpringBootRequestHandler<E, O> extends SpringFunctionInitializer

View File

@@ -1,11 +1,11 @@
/*
* Copyright 2017-1018 the original author or authors.
* Copyright 2012-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
@@ -26,12 +26,11 @@ import java.util.List;
import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.RequestStreamHandler;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.reactivestreams.Publisher;
import org.springframework.beans.factory.annotation.Autowired;
import reactor.core.publisher.Flux;
import org.springframework.beans.factory.annotation.Autowired;
/**
* @author Dave Syer
* @author Oleg Zhurakousky
@@ -39,7 +38,7 @@ import reactor.core.publisher.Flux;
public class SpringBootStreamHandler extends SpringFunctionInitializer
implements RequestStreamHandler {
@Autowired(required=false)
@Autowired(required = false)
private ObjectMapper mapper;
public SpringBootStreamHandler() {
@@ -64,7 +63,7 @@ public class SpringBootStreamHandler extends SpringFunctionInitializer
initialize();
Object value = convertStream(input);
Publisher<?> flux = apply(extract(value));
mapper.writeValue(output, result(value, flux));
this.mapper.writeValue(output, result(value, flux));
}
private Object result(Object input, Publisher<?> flux) {
@@ -72,7 +71,7 @@ public class SpringBootStreamHandler extends SpringFunctionInitializer
for (Object value : Flux.from(flux).toIterable()) {
result.add(value);
}
if (isSingleValue(input) && result.size()==1) {
if (isSingleValue(input) && result.size() == 1) {
return result.get(0);
}
return result;
@@ -91,10 +90,11 @@ public class SpringBootStreamHandler extends SpringFunctionInitializer
private Object convertStream(InputStream input) {
try {
return mapper.readValue(input, getInputType());
return this.mapper.readValue(input, getInputType());
}
catch (Exception e) {
throw new IllegalStateException("Cannot convert event", e);
}
}
}

View File

@@ -1,11 +1,11 @@
/*
* Copyright 2017 the original author or authors.
* Copyright 2012-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
@@ -29,6 +29,7 @@ import java.util.jar.Manifest;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.reactivestreams.Publisher;
import reactor.core.publisher.Flux;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
@@ -38,8 +39,6 @@ import org.springframework.cloud.function.context.catalog.FunctionInspector;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.util.ClassUtils;
import reactor.core.publisher.Flux;
/**
* @author Dave Syer
*/
@@ -69,71 +68,6 @@ public class SpringFunctionInitializer implements Closeable {
this(getStartClass());
}
@Override
public void close() {
if (this.context != null) {
this.context.close();
}
}
@SuppressWarnings("unchecked")
protected void initialize() {
if (!this.initialized.compareAndSet(false, true)) {
return;
}
logger.info("Initializing: " + configurationClass);
SpringApplication builder = springApplication();
ConfigurableApplicationContext context = builder.run();
context.getAutowireCapableBeanFactory().autowireBean(this);
String name = context.getEnvironment().getProperty("function.name");
if (name == null) {
name = "function";
}
if (this.catalog == null) {
if (context.containsBean(name)) {
this.function = context.getBean(name, Function.class);
}
}
else {
Set<String> functionNames = this.catalog.getNames(Function.class);
if (functionNames.size() == 1) {
this.function = this.catalog.lookup(Function.class,
functionNames.iterator().next());
}
else {
this.function = this.catalog.lookup(Function.class, name);
}
}
this.context = context;
}
private SpringApplication springApplication() {
Class<?> sourceClass = configurationClass;
SpringApplication application = new org.springframework.cloud.function.context.FunctionalSpringApplication(
sourceClass);
application.setWebApplicationType(WebApplicationType.NONE);
return application;
}
protected Class<?> getInputType() {
if (inspector != null) {
return inspector.getInputType(function());
}
return Object.class;
}
protected Object function() {
return this.function;
}
protected Publisher<?> apply(Publisher<?> input) {
if (this.function != null) {
return Flux.from(function.apply(input));
}
throw new IllegalStateException("No function defined");
}
private static Class<?> getStartClass() {
ClassLoader classLoader = SpringFunctionInitializer.class.getClassLoader();
if (System.getenv("MAIN_CLASS") != null) {
@@ -180,4 +114,69 @@ public class SpringFunctionInitializer implements Closeable {
return null;
}
@Override
public void close() {
if (this.context != null) {
this.context.close();
}
}
@SuppressWarnings("unchecked")
protected void initialize() {
if (!this.initialized.compareAndSet(false, true)) {
return;
}
logger.info("Initializing: " + this.configurationClass);
SpringApplication builder = springApplication();
ConfigurableApplicationContext context = builder.run();
context.getAutowireCapableBeanFactory().autowireBean(this);
String name = context.getEnvironment().getProperty("function.name");
if (name == null) {
name = "function";
}
if (this.catalog == null) {
if (context.containsBean(name)) {
this.function = context.getBean(name, Function.class);
}
}
else {
Set<String> functionNames = this.catalog.getNames(Function.class);
if (functionNames.size() == 1) {
this.function = this.catalog.lookup(Function.class,
functionNames.iterator().next());
}
else {
this.function = this.catalog.lookup(Function.class, name);
}
}
this.context = context;
}
private SpringApplication springApplication() {
Class<?> sourceClass = this.configurationClass;
SpringApplication application = new org.springframework.cloud.function.context.FunctionalSpringApplication(
sourceClass);
application.setWebApplicationType(WebApplicationType.NONE);
return application;
}
protected Class<?> getInputType() {
if (this.inspector != null) {
return this.inspector.getInputType(function());
}
return Object.class;
}
protected Object function() {
return this.function;
}
protected Publisher<?> apply(Publisher<?> input) {
if (this.function != null) {
return Flux.from(this.function.apply(input));
}
throw new IllegalStateException("No function defined");
}
}

View File

@@ -1,8 +1,13 @@
package org.springframework.cloud.function.adapter.aws;
import java.util.Collections;
import java.util.Map;
import java.util.function.Function;
import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent;
import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyResponseEvent;
import org.junit.Test;
import org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration;
import org.springframework.cloud.function.context.config.ContextFunctionCatalogAutoConfiguration;
import org.springframework.context.annotation.Bean;
@@ -11,10 +16,6 @@ import org.springframework.context.annotation.Import;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.GenericMessage;
import java.util.Collections;
import java.util.Map;
import java.util.function.Function;
import static org.assertj.core.api.Assertions.assertThat;
public class SpringBootApiGatewayRequestHandlerTests {
@@ -23,71 +24,83 @@ public class SpringBootApiGatewayRequestHandlerTests {
@Test
public void functionBean() {
handler = new SpringBootApiGatewayRequestHandler(FunctionConfig.class);
handler.initialize();
this.handler = new SpringBootApiGatewayRequestHandler(FunctionConfig.class);
this.handler.initialize();
APIGatewayProxyRequestEvent request = new APIGatewayProxyRequestEvent();
request.setBody("{\"value\":\"foo\"}");
Object output = handler.handleRequest(request, null);
Object output = this.handler.handleRequest(request, null);
assertThat(output).isInstanceOf(APIGatewayProxyResponseEvent.class);
assertThat(((APIGatewayProxyResponseEvent) output).getStatusCode()).isEqualTo(200);
assertThat(((APIGatewayProxyResponseEvent) output).getBody()).isEqualTo("{\"value\":\"FOO\"}");
}
@Configuration
@Import({ContextFunctionCatalogAutoConfiguration.class,
JacksonAutoConfiguration.class})
protected static class FunctionConfig {
@Bean
public Function<Foo, Bar> function() {
return foo -> new Bar(foo.getValue().toUpperCase());
}
assertThat(((APIGatewayProxyResponseEvent) output).getStatusCode())
.isEqualTo(200);
assertThat(((APIGatewayProxyResponseEvent) output).getBody())
.isEqualTo("{\"value\":\"FOO\"}");
}
@Test
public void functionMessageBean() {
handler = new SpringBootApiGatewayRequestHandler(FunctionMessageConfig.class);
handler.initialize();
this.handler = new SpringBootApiGatewayRequestHandler(
FunctionMessageConfig.class);
this.handler.initialize();
APIGatewayProxyRequestEvent request = new APIGatewayProxyRequestEvent();
request.setBody("{\"value\":\"foo\"}");
Object output = handler.handleRequest(request, null);
Object output = this.handler.handleRequest(request, null);
assertThat(output).isInstanceOf(APIGatewayProxyResponseEvent.class);
assertThat(((APIGatewayProxyResponseEvent) output).getStatusCode()).isEqualTo(200);
assertThat(((APIGatewayProxyResponseEvent) output).getHeaders().get("spring")).isEqualTo("cloud");
assertThat(((APIGatewayProxyResponseEvent) output).getBody()).isEqualTo("{\"value\":\"FOO\"}");
assertThat(((APIGatewayProxyResponseEvent) output).getStatusCode())
.isEqualTo(200);
assertThat(((APIGatewayProxyResponseEvent) output).getHeaders().get("spring"))
.isEqualTo("cloud");
assertThat(((APIGatewayProxyResponseEvent) output).getBody())
.isEqualTo("{\"value\":\"FOO\"}");
}
@Configuration
@Import({ContextFunctionCatalogAutoConfiguration.class,
JacksonAutoConfiguration.class})
@Import({ ContextFunctionCatalogAutoConfiguration.class,
JacksonAutoConfiguration.class })
protected static class FunctionConfig {
@Bean
public Function<Foo, Bar> function() {
return foo -> new Bar(foo.getValue().toUpperCase());
}
}
@Configuration
@Import({ ContextFunctionCatalogAutoConfiguration.class,
JacksonAutoConfiguration.class })
protected static class FunctionMessageConfig {
@Bean
public Function<Message<Foo>, Message<Bar>> function() {
return (foo -> {
Map<String, Object> headers = Collections.singletonMap("spring", "cloud");
return new GenericMessage<>(
new Bar(foo.getPayload().getValue().toUpperCase()),
headers);
new Bar(foo.getPayload().getValue().toUpperCase()), headers);
});
}
}
protected static class Foo {
private String value;
public String getValue() {
return value;
return this.value;
}
public void setValue(String value) {
this.value = value;
}
}
protected static class Bar {
private String value;
public Bar() {
@@ -98,11 +111,13 @@ public class SpringBootApiGatewayRequestHandlerTests {
}
public String getValue() {
return value;
return this.value;
}
public void setValue(String value) {
this.value = value;
}
}
}

View File

@@ -1,11 +1,11 @@
/*
* Copyright 2017 the original author or authors.
* Copyright 2012-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
@@ -16,11 +16,6 @@
package org.springframework.cloud.function.adapter.aws;
import static java.util.Arrays.asList;
import static java.util.Collections.singletonList;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.fail;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.List;
@@ -41,6 +36,11 @@ import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.messaging.Message;
import static java.util.Arrays.asList;
import static java.util.Collections.singletonList;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.fail;
/**
* @author Halvdan Hoem Grelland
*/
@@ -50,122 +50,6 @@ public class SpringBootKinesisEventHandlerTests {
private SpringBootKinesisEventHandler<Foo, Bar> handler;
@Test
public void functionBeanHandlesKinesisEvent() throws Exception {
handler = new SpringBootKinesisEventHandler<>(FunctionConfig.class);
handler.initialize();
KinesisEvent event = asKinesisEvent(singletonList(new Foo("foo")));
List<Bar> output = handler.handleRequest(event, null);
assertThat(output).containsExactly(new Bar("FOO"));
}
@Test
public void functionBeanHandlesAggregatedKinesisEvent() throws Exception {
handler = new SpringBootKinesisEventHandler<>(FunctionConfig.class);
handler.initialize();
List<Foo> events = asList(new Foo("foo"), new Foo("bar"), new Foo("baz"));
KinesisEvent aggregatedEvent = asAggregatedKinesisEvent(events);
List<Bar> output = handler.handleRequest(aggregatedEvent, null);
assertThat(output)
.containsExactly(new Bar("FOO"), new Bar("BAR"), new Bar("BAZ"));
}
@Test
public void functionMessageBean() throws Exception {
handler = new SpringBootKinesisEventHandler<>(FunctionMessageConfig.class);
handler.initialize();
KinesisEvent event = asKinesisEvent(asList(new Foo("foo"), new Foo("bar")));
List<Bar> output = handler.handleRequest(event, null);
assertThat(output)
.containsExactly(new Bar("FOO"), new Bar("BAR"));
}
@Configuration
@Import({
ContextFunctionCatalogAutoConfiguration.class,
JacksonAutoConfiguration.class
})
protected static class FunctionConfig {
@Bean
public Function<Foo, Bar> function() {
return foo -> new Bar(foo.getValue().toUpperCase());
}
}
@Configuration
@Import({
ContextFunctionCatalogAutoConfiguration.class,
JacksonAutoConfiguration.class
})
protected static class FunctionMessageConfig {
@Bean
public Function<Message<Foo>, Bar> function() {
return foo -> new Bar(foo.getPayload().getValue().toUpperCase());
}
}
protected static class Foo {
private String value;
public Foo() {
}
public Foo(String value) {
this.value = value;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}
protected static class Bar {
private String value;
public Bar() {
}
public Bar(String value) {
this.value = value;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Bar bar = (Bar) o;
return Objects.equals(value, bar.value);
}
@Override
public int hashCode() {
return Objects.hash(value);
}
}
private static KinesisEvent asKinesisEvent(List<Object> payloads) {
KinesisEvent kinesisEvent = new KinesisEvent();
@@ -175,8 +59,7 @@ public class SpringBootKinesisEventHandlerTests {
KinesisEvent.Record record = new KinesisEvent.Record();
record.setData(asJsonByteBuffer(payload));
KinesisEvent.KinesisEventRecord kinesisEventRecord =
new KinesisEvent.KinesisEventRecord();
KinesisEvent.KinesisEventRecord kinesisEventRecord = new KinesisEvent.KinesisEventRecord();
kinesisEventRecord.setKinesis(record);
kinesisEventRecords.add(kinesisEventRecord);
@@ -190,12 +73,12 @@ public class SpringBootKinesisEventHandlerTests {
private static KinesisEvent asAggregatedKinesisEvent(List<?> payloads) {
RecordAggregator aggregator = new RecordAggregator();
payloads.stream()
.map(SpringBootKinesisEventHandlerTests::asJsonByteBuffer)
payloads.stream().map(SpringBootKinesisEventHandlerTests::asJsonByteBuffer)
.forEach(buffer -> {
try {
aggregator.addUserRecord("fakePartitionKey", buffer.array());
} catch (Exception e) {
}
catch (Exception e) {
fail("Creating aggregated record failed");
}
});
@@ -218,9 +101,132 @@ public class SpringBootKinesisEventHandlerTests {
private static ByteBuffer asJsonByteBuffer(Object object) {
try {
return ByteBuffer.wrap(mapper.writeValueAsBytes(object));
} catch (JsonProcessingException e) {
}
catch (JsonProcessingException e) {
fail("Setting up test data failed", e);
throw new RuntimeException(e);
}
}
@Test
public void functionBeanHandlesKinesisEvent() throws Exception {
this.handler = new SpringBootKinesisEventHandler<>(FunctionConfig.class);
this.handler.initialize();
KinesisEvent event = asKinesisEvent(singletonList(new Foo("foo")));
List<Bar> output = this.handler.handleRequest(event, null);
assertThat(output).containsExactly(new Bar("FOO"));
}
@Test
public void functionBeanHandlesAggregatedKinesisEvent() throws Exception {
this.handler = new SpringBootKinesisEventHandler<>(FunctionConfig.class);
this.handler.initialize();
List<Foo> events = asList(new Foo("foo"), new Foo("bar"), new Foo("baz"));
KinesisEvent aggregatedEvent = asAggregatedKinesisEvent(events);
List<Bar> output = this.handler.handleRequest(aggregatedEvent, null);
assertThat(output).containsExactly(new Bar("FOO"), new Bar("BAR"),
new Bar("BAZ"));
}
@Test
public void functionMessageBean() throws Exception {
this.handler = new SpringBootKinesisEventHandler<>(FunctionMessageConfig.class);
this.handler.initialize();
KinesisEvent event = asKinesisEvent(asList(new Foo("foo"), new Foo("bar")));
List<Bar> output = this.handler.handleRequest(event, null);
assertThat(output).containsExactly(new Bar("FOO"), new Bar("BAR"));
}
@Configuration
@Import({ ContextFunctionCatalogAutoConfiguration.class,
JacksonAutoConfiguration.class })
protected static class FunctionConfig {
@Bean
public Function<Foo, Bar> function() {
return foo -> new Bar(foo.getValue().toUpperCase());
}
}
@Configuration
@Import({ ContextFunctionCatalogAutoConfiguration.class,
JacksonAutoConfiguration.class })
protected static class FunctionMessageConfig {
@Bean
public Function<Message<Foo>, Bar> function() {
return foo -> new Bar(foo.getPayload().getValue().toUpperCase());
}
}
protected static class Foo {
private String value;
public Foo() {
}
public Foo(String value) {
this.value = value;
}
public String getValue() {
return this.value;
}
public void setValue(String value) {
this.value = value;
}
}
protected static class Bar {
private String value;
public Bar() {
}
public Bar(String value) {
this.value = value;
}
public String getValue() {
return this.value;
}
public void setValue(String value) {
this.value = value;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
Bar bar = (Bar) o;
return Objects.equals(this.value, bar.value);
}
@Override
public int hashCode() {
return Objects.hash(this.value);
}
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2016-2017 the original author or authors.
* Copyright 2012-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -38,9 +38,9 @@ public class SpringBootRequestHandlerTests {
@Test
public void functionBean() throws Exception {
handler = new SpringBootRequestHandler<Foo, Bar>(FunctionConfig.class);
handler.initialize();
Object output = handler.handleRequest(new Foo("foo"), null);
this.handler = new SpringBootRequestHandler<Foo, Bar>(FunctionConfig.class);
this.handler.initialize();
Object output = this.handler.handleRequest(new Foo("foo"), null);
assertThat(output).isInstanceOf(Bar.class);
}
@@ -48,13 +48,16 @@ public class SpringBootRequestHandlerTests {
@Import({ ContextFunctionCatalogAutoConfiguration.class,
JacksonAutoConfiguration.class })
protected static class FunctionConfig {
@Bean
public Function<Foo, Bar> function() {
return foo -> new Bar(foo.getValue().toUpperCase());
}
}
protected static class Foo {
private String value;
public Foo(String value) {
@@ -62,15 +65,17 @@ public class SpringBootRequestHandlerTests {
}
public String getValue() {
return value;
return this.value;
}
public void setValue(String value) {
this.value = value;
}
}
protected static class Bar {
private String value;
public Bar() {
@@ -81,11 +86,13 @@ public class SpringBootRequestHandlerTests {
}
public String getValue() {
return value;
return this.value;
}
public void setValue(String value) {
this.value = value;
}
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2016-2018 the original author or authors.
* Copyright 2012-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -40,21 +40,21 @@ public class SpringBootStreamHandlerTests {
@Test
public void functionBeanWithJacksonConfig() throws Exception {
handler = new SpringBootStreamHandler(FunctionConfigWithJackson.class);
handler.initialize();
this.handler = new SpringBootStreamHandler(FunctionConfigWithJackson.class);
this.handler.initialize();
ByteArrayOutputStream output = new ByteArrayOutputStream();
handler.handleRequest(new ByteArrayInputStream("{\"value\":\"foo\"}".getBytes()),
output, null);
this.handler.handleRequest(
new ByteArrayInputStream("{\"value\":\"foo\"}".getBytes()), output, null);
assertThat(output.toString()).isEqualTo("{\"value\":\"FOO\"}");
}
@Test
public void functionBeanWithoutJacksonConfig() throws Exception {
handler = new SpringBootStreamHandler(FunctionConfigWithoutJackson.class);
handler.initialize();
this.handler = new SpringBootStreamHandler(FunctionConfigWithoutJackson.class);
this.handler.initialize();
ByteArrayOutputStream output = new ByteArrayOutputStream();
handler.handleRequest(new ByteArrayInputStream("{\"value\":\"foo\"}".getBytes()),
output, null);
this.handler.handleRequest(
new ByteArrayInputStream("{\"value\":\"foo\"}".getBytes()), output, null);
assertThat(output.toString()).isEqualTo("{\"value\":\"FOO\"}");
}
@@ -62,34 +62,41 @@ public class SpringBootStreamHandlerTests {
@Import({ ContextFunctionCatalogAutoConfiguration.class,
JacksonAutoConfiguration.class })
protected static class FunctionConfigWithJackson {
@Bean
public Function<Foo, Bar> function() {
return foo -> new Bar(foo.getValue().toUpperCase());
}
}
@Configuration
@Import({ ContextFunctionCatalogAutoConfiguration.class})
@Import({ ContextFunctionCatalogAutoConfiguration.class })
protected static class FunctionConfigWithoutJackson {
@Bean
public Function<Foo, Bar> function() {
return foo -> new Bar(foo.getValue().toUpperCase());
}
}
protected static class Foo {
private String value;
public String getValue() {
return value;
return this.value;
}
public void setValue(String value) {
this.value = value;
}
}
protected static class Bar {
private String value;
public Bar() {
@@ -100,11 +107,13 @@ public class SpringBootStreamHandlerTests {
}
public String getValue() {
return value;
return this.value;
}
public void setValue(String value) {
this.value = value;
}
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2016-2017 the original author or authors.
* Copyright 2012-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -23,6 +23,7 @@ import java.util.stream.Collectors;
import org.junit.After;
import org.junit.Ignore;
import org.junit.Test;
import reactor.core.publisher.Flux;
import org.springframework.cloud.function.context.FunctionRegistration;
import org.springframework.cloud.function.context.FunctionType;
@@ -35,8 +36,6 @@ import org.springframework.context.support.GenericApplicationContext;
import static org.assertj.core.api.Assertions.assertThat;
import reactor.core.publisher.Flux;
/**
* @author Dave Syer
*
@@ -48,74 +47,78 @@ public class SpringFunctionInitializerTests {
@After
public void after() {
System.clearProperty("function.name");
if (initializer != null) {
initializer.close();
if (this.initializer != null) {
this.initializer.close();
}
}
@Test
public void functionBean() {
initializer = new SpringFunctionInitializer(FluxFunctionConfig.class);
initializer.initialize();
Flux<?> result = Flux.from(initializer.apply(Flux.just(new Foo())));
this.initializer = new SpringFunctionInitializer(FluxFunctionConfig.class);
this.initializer.initialize();
Flux<?> result = Flux.from(this.initializer.apply(Flux.just(new Foo())));
assertThat(result.blockFirst()).isInstanceOf(Bar.class);
}
@Test
public void functionApp() {
initializer = new SpringFunctionInitializer(FluxFunctionApp.class);
initializer.initialize();
Flux<?> result = Flux.from(initializer.apply(Flux.just(new Foo())));
this.initializer = new SpringFunctionInitializer(FluxFunctionApp.class);
this.initializer.initialize();
Flux<?> result = Flux.from(this.initializer.apply(Flux.just(new Foo())));
assertThat(result.blockFirst()).isInstanceOf(Bar.class);
}
@Test
public void functionCatalog() {
initializer = new SpringFunctionInitializer(FunctionConfig.class);
initializer.initialize();
Flux<?> result = Flux.from(initializer.apply(Flux.just(new Foo())));
this.initializer = new SpringFunctionInitializer(FunctionConfig.class);
this.initializer.initialize();
Flux<?> result = Flux.from(this.initializer.apply(Flux.just(new Foo())));
assertThat(result.blockFirst()).isInstanceOf(Bar.class);
}
@Test
@Ignore // related to boot 2.1 no bean override change
public void functionRegistrar() {
initializer = new SpringFunctionInitializer(FunctionRegistrar.class);
initializer.initialize();
Flux<?> result = Flux.from(initializer.apply(Flux.just(new Foo())));
this.initializer = new SpringFunctionInitializer(FunctionRegistrar.class);
this.initializer.initialize();
Flux<?> result = Flux.from(this.initializer.apply(Flux.just(new Foo())));
assertThat(result.blockFirst()).isInstanceOf(Bar.class);
}
@Test
public void namedFunctionCatalog() {
initializer = new SpringFunctionInitializer(NamedFunctionConfig.class);
this.initializer = new SpringFunctionInitializer(NamedFunctionConfig.class);
System.setProperty("function.name", "other");
initializer.initialize();
Flux<?> result = Flux.from(initializer.apply(Flux.just(new Foo())));
this.initializer.initialize();
Flux<?> result = Flux.from(this.initializer.apply(Flux.just(new Foo())));
assertThat(result.blockFirst()).isInstanceOf(Bar.class);
}
@Test
public void consumerCatalog() {
initializer = new SpringFunctionInitializer(ConsumerConfig.class);
initializer.initialize();
Flux<?> result = Flux.from(initializer.apply(Flux.just(new Foo())));
this.initializer = new SpringFunctionInitializer(ConsumerConfig.class);
this.initializer.initialize();
Flux<?> result = Flux.from(this.initializer.apply(Flux.just(new Foo())));
assertThat(result.toStream().collect(Collectors.toList())).isEmpty();
}
@Configuration
protected static class FluxFunctionConfig {
@Bean
public Function<Flux<Foo>, Flux<Bar>> function() {
return flux -> flux.map(foo -> new Bar());
}
}
protected static class FluxFunctionApp implements Function<Flux<Foo>, Flux<Bar>> {
@Override
public Flux<Bar> apply(Flux<Foo> flux) {
return flux.map(foo -> new Bar());
}
}
protected static class FunctionRegistrar
@@ -133,39 +136,49 @@ public class SpringFunctionInitializerTests {
.type(FunctionType.from(Foo.class).to(Bar.class)
.wrap(Flux.class).getType()));
}
}
@Configuration
@Import(ContextFunctionCatalogAutoConfiguration.class)
protected static class FunctionConfig {
@Bean
public Function<Foo, Bar> function() {
return foo -> new Bar();
}
}
@Configuration
@Import(ContextFunctionCatalogAutoConfiguration.class)
protected static class NamedFunctionConfig {
@Bean
public Function<Foo, Bar> other() {
return foo -> new Bar();
}
}
@Configuration
@Import(ContextFunctionCatalogAutoConfiguration.class)
protected static class ConsumerConfig {
@Bean
public Consumer<Foo> consumer() {
return foo -> {
};
}
}
protected static class Foo {
}
protected static class Bar {
}
}