Turned on checkstyle
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>spring-cloud-function-adapter-parent</artifactId>
|
||||
@@ -18,6 +19,6 @@
|
||||
<module>spring-cloud-function-adapter-aws</module>
|
||||
<module>spring-cloud-function-adapter-openwhisk</module>
|
||||
<module>spring-cloud-function-adapter-azure</module>
|
||||
</modules>
|
||||
</modules>
|
||||
|
||||
</project>
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>spring-cloud-function-adapter-aws</artifactId>
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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 {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>spring-cloud-function-adapter-azure</artifactId>
|
||||
@@ -20,7 +20,8 @@
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
|
||||
<java.version>1.8</java.version>
|
||||
<azure.functions.java.core.version>1.0.0-beta-5</azure.functions.java.core.version>
|
||||
<azure.functions.java.core.version>1.0.0-beta-5
|
||||
</azure.functions.java.core.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
|
||||
@@ -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,
|
||||
@@ -24,12 +24,12 @@ import java.util.function.Function;
|
||||
|
||||
import com.microsoft.azure.functions.ExecutionContext;
|
||||
import com.microsoft.azure.functions.OutputBinding;
|
||||
|
||||
import org.reactivestreams.Publisher;
|
||||
|
||||
import reactor.core.publisher.Flux;
|
||||
|
||||
/**
|
||||
* @param <I> input type
|
||||
* @param <O> result type
|
||||
* @author Soby Chacko
|
||||
*/
|
||||
public class AzureSpringBootRequestHandler<I, O> extends AzureSpringFunctionInitializer {
|
||||
@@ -118,4 +118,5 @@ public class AzureSpringBootRequestHandler<I, O> extends AzureSpringFunctionInit
|
||||
protected O convertOutput(Object output) {
|
||||
return (O) output;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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,7 +29,6 @@ import java.util.function.Function;
|
||||
import java.util.jar.Manifest;
|
||||
|
||||
import com.microsoft.azure.functions.ExecutionContext;
|
||||
|
||||
import org.reactivestreams.Publisher;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
@@ -46,20 +45,20 @@ import org.springframework.util.ClassUtils;
|
||||
*/
|
||||
public class AzureSpringFunctionInitializer implements Closeable {
|
||||
|
||||
private volatile static ConfigurableApplicationContext context;
|
||||
|
||||
private final Class<?> configurationClass;
|
||||
|
||||
private volatile Function<Publisher<?>, Publisher<?>> function;
|
||||
|
||||
private AtomicBoolean initialized = new AtomicBoolean();
|
||||
|
||||
private final Class<?> configurationClass;
|
||||
|
||||
@Autowired(required = false)
|
||||
private volatile FunctionCatalog catalog;
|
||||
|
||||
@Autowired(required = false)
|
||||
private volatile FunctionInspector inspector;
|
||||
|
||||
private volatile static ConfigurableApplicationContext context;
|
||||
|
||||
public AzureSpringFunctionInitializer(Class<?> configurationClass) {
|
||||
this.configurationClass = configurationClass == null ? getClass()
|
||||
: configurationClass;
|
||||
@@ -69,6 +68,55 @@ public class AzureSpringFunctionInitializer implements Closeable {
|
||||
this(getStartClass());
|
||||
}
|
||||
|
||||
private static Class<?> getStartClass() {
|
||||
ClassLoader classLoader = org.springframework.cloud.function.adapter.azure.AzureSpringFunctionInitializer.class
|
||||
.getClassLoader();
|
||||
if (System.getenv("MAIN_CLASS") != null) {
|
||||
return ClassUtils.resolveClassName(System.getenv("MAIN_CLASS"), classLoader);
|
||||
}
|
||||
try {
|
||||
Class<?> result = getStartClass(
|
||||
Collections.list(classLoader.getResources("META-INF/MANIFEST.MF")));
|
||||
if (result == null) {
|
||||
result = getStartClass(Collections
|
||||
.list(classLoader.getResources("meta-inf/manifest.mf")));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
catch (Exception ex) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private static Class<?> getStartClass(List<URL> list) {
|
||||
for (URL url : list) {
|
||||
try {
|
||||
InputStream inputStream = url.openStream();
|
||||
try {
|
||||
Manifest manifest = new Manifest(inputStream);
|
||||
String startClass = manifest.getMainAttributes()
|
||||
.getValue("Main-Class");
|
||||
if (startClass != null) {
|
||||
Class<?> aClass = ClassUtils.forName(startClass,
|
||||
org.springframework.cloud.function.adapter.azure.AzureSpringFunctionInitializer.class
|
||||
.getClassLoader());
|
||||
SpringBootApplication declaredAnnotation = aClass
|
||||
.getDeclaredAnnotation(SpringBootApplication.class);
|
||||
if (declaredAnnotation != null) {
|
||||
return aClass;
|
||||
}
|
||||
}
|
||||
}
|
||||
finally {
|
||||
inputStream.close();
|
||||
}
|
||||
}
|
||||
catch (Exception ex) {
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() throws IOException {
|
||||
if (AzureSpringFunctionInitializer.context != null) {
|
||||
@@ -132,7 +180,7 @@ public class AzureSpringFunctionInitializer implements Closeable {
|
||||
}
|
||||
|
||||
private SpringApplication springApplication() {
|
||||
Class<?> sourceClass = configurationClass;
|
||||
Class<?> sourceClass = this.configurationClass;
|
||||
SpringApplication application = new org.springframework.cloud.function.context.FunctionalSpringApplication(
|
||||
sourceClass);
|
||||
application.setWebApplicationType(WebApplicationType.NONE);
|
||||
@@ -144,7 +192,8 @@ public class AzureSpringFunctionInitializer implements Closeable {
|
||||
return true;
|
||||
}
|
||||
if (this.inspector != null) {
|
||||
return Collection.class.isAssignableFrom(inspector.getInputType(function));
|
||||
return Collection.class
|
||||
.isAssignableFrom(this.inspector.getInputType(function));
|
||||
}
|
||||
return ((Collection<?>) input).size() <= 1;
|
||||
}
|
||||
@@ -154,7 +203,8 @@ public class AzureSpringFunctionInitializer implements Closeable {
|
||||
return true;
|
||||
}
|
||||
if (this.inspector != null) {
|
||||
return Collection.class.isAssignableFrom(inspector.getOutputType(function));
|
||||
return Collection.class
|
||||
.isAssignableFrom(this.inspector.getOutputType(function));
|
||||
}
|
||||
return ((Collection<?>) output).size() <= 1;
|
||||
}
|
||||
@@ -174,64 +224,16 @@ public class AzureSpringFunctionInitializer implements Closeable {
|
||||
throw new IllegalStateException("No function defined with name=" + name);
|
||||
}
|
||||
|
||||
private static Class<?> getStartClass() {
|
||||
ClassLoader classLoader = org.springframework.cloud.function.adapter.azure.AzureSpringFunctionInitializer.class
|
||||
.getClassLoader();
|
||||
if (System.getenv("MAIN_CLASS") != null) {
|
||||
return ClassUtils.resolveClassName(System.getenv("MAIN_CLASS"), classLoader);
|
||||
}
|
||||
try {
|
||||
Class<?> result = getStartClass(
|
||||
Collections.list(classLoader.getResources("META-INF/MANIFEST.MF")));
|
||||
if (result == null) {
|
||||
result = getStartClass(Collections
|
||||
.list(classLoader.getResources("meta-inf/manifest.mf")));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
catch (Exception ex) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private static Class<?> getStartClass(List<URL> list) {
|
||||
for (URL url : list) {
|
||||
try {
|
||||
InputStream inputStream = url.openStream();
|
||||
try {
|
||||
Manifest manifest = new Manifest(inputStream);
|
||||
String startClass = manifest.getMainAttributes()
|
||||
.getValue("Main-Class");
|
||||
if (startClass != null) {
|
||||
Class<?> aClass = ClassUtils.forName(startClass,
|
||||
org.springframework.cloud.function.adapter.azure.AzureSpringFunctionInitializer.class
|
||||
.getClassLoader());
|
||||
SpringBootApplication declaredAnnotation = aClass
|
||||
.getDeclaredAnnotation(SpringBootApplication.class);
|
||||
if (declaredAnnotation != null) {
|
||||
return aClass;
|
||||
}
|
||||
}
|
||||
}
|
||||
finally {
|
||||
inputStream.close();
|
||||
}
|
||||
}
|
||||
catch (Exception ex) {
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public Function<Publisher<?>, Publisher<?>> getFunction() {
|
||||
return function;
|
||||
return this.function;
|
||||
}
|
||||
|
||||
public FunctionCatalog getCatalog() {
|
||||
return catalog;
|
||||
return this.catalog;
|
||||
}
|
||||
|
||||
public FunctionInspector getInspector() {
|
||||
return inspector;
|
||||
return this.inspector;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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.
|
||||
@@ -24,6 +24,7 @@ import java.util.stream.Collectors;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Test;
|
||||
import reactor.core.publisher.Flux;
|
||||
|
||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
@@ -31,8 +32,6 @@ import org.springframework.context.annotation.Configuration;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
import reactor.core.publisher.Flux;
|
||||
|
||||
/**
|
||||
* @author Dave Syer
|
||||
*
|
||||
@@ -114,50 +113,60 @@ public class AzureSpringBootRequestHandlerTests {
|
||||
|
||||
@After
|
||||
public void close() throws IOException {
|
||||
if (handler != null)
|
||||
handler.close();
|
||||
if (this.handler != null) {
|
||||
this.handler.close();
|
||||
}
|
||||
}
|
||||
|
||||
@Configuration
|
||||
protected static class BareConfig {
|
||||
|
||||
@Bean
|
||||
public Function<Flux<Foo>, Flux<Bar>> function() {
|
||||
return foos -> foos.map(foo -> new Bar(foo.getValue().toUpperCase()));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@EnableAutoConfiguration
|
||||
protected static class AutoConfig {
|
||||
|
||||
@Bean
|
||||
public Function<Foo, Bar> uppercase() {
|
||||
return foo -> new Bar(foo.getValue().toUpperCase());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@EnableAutoConfiguration
|
||||
protected static class ListConfig {
|
||||
|
||||
@Bean
|
||||
public Function<List<Foo>, List<Bar>> uppercase() {
|
||||
return foos -> foos.stream().map(foo -> new Bar(foo.getValue().toUpperCase()))
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@EnableAutoConfiguration
|
||||
protected static class CollectConfig {
|
||||
|
||||
@Bean
|
||||
public Function<List<Foo>, Bar> uppercase() {
|
||||
return foos -> new Bar(foos.stream().map(foo -> foo.getValue().toUpperCase())
|
||||
.collect(Collectors.joining(",")));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@EnableAutoConfiguration
|
||||
protected static class MultiConfig {
|
||||
|
||||
@Bean
|
||||
public Function<Foo, Bar> uppercase() {
|
||||
return foo -> new Bar(foo.getValue().toUpperCase());
|
||||
@@ -167,6 +176,7 @@ public class AzureSpringBootRequestHandlerTests {
|
||||
public Function<Bar, Foo> lowercase() {
|
||||
return bar -> new Foo(bar.getValue().toLowerCase());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -178,25 +188,26 @@ class Foo {
|
||||
Foo() {
|
||||
}
|
||||
|
||||
public String lowercase() {
|
||||
return value.toLowerCase();
|
||||
}
|
||||
|
||||
public Foo(String value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
public String lowercase() {
|
||||
return this.value.toLowerCase();
|
||||
}
|
||||
|
||||
public String uppercase() {
|
||||
return value.toUpperCase();
|
||||
return this.value.toUpperCase();
|
||||
}
|
||||
|
||||
public String getValue() {
|
||||
return value;
|
||||
return this.value;
|
||||
}
|
||||
|
||||
public void setValue(String value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
class Bar {
|
||||
@@ -211,11 +222,11 @@ class Bar {
|
||||
}
|
||||
|
||||
public String getValue() {
|
||||
return value;
|
||||
return this.value;
|
||||
}
|
||||
|
||||
public void setValue(String value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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.
|
||||
@@ -20,6 +20,7 @@ import java.util.function.Function;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Test;
|
||||
import reactor.core.publisher.Flux;
|
||||
|
||||
import org.springframework.boot.SpringBootConfiguration;
|
||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||
@@ -31,8 +32,6 @@ import org.springframework.context.support.GenericApplicationContext;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
import reactor.core.publisher.Flux;
|
||||
|
||||
/**
|
||||
* @author Dave Syer
|
||||
*
|
||||
@@ -50,8 +49,9 @@ public class AzureSpringFunctionInitializerTests {
|
||||
|
||||
@After
|
||||
public void close() throws IOException {
|
||||
if (handler != null)
|
||||
handler.close();
|
||||
if (this.handler != null) {
|
||||
this.handler.close();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -84,18 +84,22 @@ public class AzureSpringFunctionInitializerTests {
|
||||
@SpringBootConfiguration
|
||||
@EnableAutoConfiguration
|
||||
protected static class BareConfig {
|
||||
|
||||
@Bean
|
||||
public Function<Foo, Bar> function() {
|
||||
return foo -> new Bar(foo.getValue().toUpperCase());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@SpringBootConfiguration
|
||||
protected static class FunctionConfig implements Function<Foo, Bar> {
|
||||
|
||||
@Override
|
||||
public Bar apply(Foo foo) {
|
||||
return new Bar(foo.getValue().toUpperCase());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@SpringBootConfiguration
|
||||
@@ -113,5 +117,7 @@ public class AzureSpringFunctionInitializerTests {
|
||||
"uppercase")
|
||||
.type(FunctionType.from(Foo.class).to(Bar.class)));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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.
|
||||
@@ -22,6 +22,7 @@ import java.util.logging.Logger;
|
||||
import com.microsoft.azure.functions.ExecutionContext;
|
||||
|
||||
public class TestExecutionContext implements ExecutionContext {
|
||||
|
||||
private String name;
|
||||
|
||||
public TestExecutionContext(String name) {
|
||||
@@ -40,6 +41,7 @@ public class TestExecutionContext implements ExecutionContext {
|
||||
|
||||
@Override
|
||||
public String getFunctionName() {
|
||||
return name;
|
||||
return this.name;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>spring-cloud-function-adapter-openwhisk</artifactId>
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* 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.
|
||||
@@ -29,7 +29,7 @@ public class FunctionProperties {
|
||||
private String type = "function";
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
return this.name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
@@ -37,10 +37,11 @@ public class FunctionProperties {
|
||||
}
|
||||
|
||||
public String getType() {
|
||||
return type;
|
||||
return this.type;
|
||||
}
|
||||
|
||||
public void setType(String type) {
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* 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.
|
||||
@@ -23,6 +23,7 @@ import org.springframework.boot.context.properties.EnableConfigurationProperties
|
||||
/**
|
||||
* @author Mark Fisher
|
||||
*/
|
||||
// @checkstyle:off
|
||||
@SpringBootApplication
|
||||
@EnableConfigurationProperties(FunctionProperties.class)
|
||||
public class OpenWhiskActionApplication {
|
||||
@@ -30,4 +31,6 @@ public class OpenWhiskActionApplication {
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(OpenWhiskActionApplication.class, args);
|
||||
}
|
||||
|
||||
}
|
||||
// @checkstyle:on
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* 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.
|
||||
@@ -23,18 +23,16 @@ import java.util.Map;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
|
||||
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.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import reactor.core.publisher.Flux;
|
||||
|
||||
/**
|
||||
* @author Mark Fisher
|
||||
* @author Kamesh Sampath
|
||||
@@ -43,6 +41,7 @@ import reactor.core.publisher.Flux;
|
||||
public class OpenWhiskActionHandler extends OpenWhiskFunctionInitializer {
|
||||
|
||||
private static final String NO_INPUT_PROVIDED = "No input provided";
|
||||
|
||||
private static Log logger = LogFactory.getLog(OpenWhiskFunctionInitializer.class);
|
||||
|
||||
@Autowired
|
||||
@@ -61,7 +60,7 @@ public class OpenWhiskActionHandler extends OpenWhiskFunctionInitializer {
|
||||
public Object run(@RequestBody OpenWhiskActionRequest request) {
|
||||
Object input = convertEvent(request.getValue());
|
||||
Object result = NO_INPUT_PROVIDED;
|
||||
if(input !=null ) {
|
||||
if (input != null) {
|
||||
Publisher<?> output = apply(extract(input));
|
||||
result = result(input, output);
|
||||
}
|
||||
@@ -104,17 +103,20 @@ public class OpenWhiskActionHandler extends OpenWhiskFunctionInitializer {
|
||||
|
||||
private Object convertToFunctionParamType(Object payload) {
|
||||
try {
|
||||
return mapper.convertValue(payload, getInputType());
|
||||
} catch (Exception e) {
|
||||
return this.mapper.convertValue(payload, getInputType());
|
||||
}
|
||||
catch (Exception e) {
|
||||
throw new IllegalStateException("Cannot convert event payload", e);
|
||||
}
|
||||
}
|
||||
|
||||
private String serializeBody(Object body) {
|
||||
try {
|
||||
return "{\"result\":" + mapper.writeValueAsString(body) + "}";
|
||||
} catch (JsonProcessingException e) {
|
||||
return "{\"result\":" + this.mapper.writeValueAsString(body) + "}";
|
||||
}
|
||||
catch (JsonProcessingException e) {
|
||||
throw new IllegalStateException("Cannot convert output", e);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* 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.
|
||||
@@ -41,7 +41,7 @@ public class OpenWhiskActionRequest {
|
||||
private Map<String, Object> value;
|
||||
|
||||
public String getActionName() {
|
||||
return actionName;
|
||||
return this.actionName;
|
||||
}
|
||||
|
||||
public void setActionName(String actionName) {
|
||||
@@ -49,7 +49,7 @@ public class OpenWhiskActionRequest {
|
||||
}
|
||||
|
||||
public String getActivationId() {
|
||||
return activationId;
|
||||
return this.activationId;
|
||||
}
|
||||
|
||||
public void setActivationId(String activationId) {
|
||||
@@ -57,7 +57,7 @@ public class OpenWhiskActionRequest {
|
||||
}
|
||||
|
||||
public String getApiKey() {
|
||||
return apiKey;
|
||||
return this.apiKey;
|
||||
}
|
||||
|
||||
public void setApiKey(String apiKey) {
|
||||
@@ -65,7 +65,7 @@ public class OpenWhiskActionRequest {
|
||||
}
|
||||
|
||||
public String getDeadline() {
|
||||
return deadline;
|
||||
return this.deadline;
|
||||
}
|
||||
|
||||
public void setDeadline(String deadline) {
|
||||
@@ -73,7 +73,7 @@ public class OpenWhiskActionRequest {
|
||||
}
|
||||
|
||||
public String getNamespace() {
|
||||
return namespace;
|
||||
return this.namespace;
|
||||
}
|
||||
|
||||
public void setNamespace(String namespace) {
|
||||
@@ -81,10 +81,11 @@ public class OpenWhiskActionRequest {
|
||||
}
|
||||
|
||||
public Map<String, Object> getValue() {
|
||||
return value;
|
||||
return this.value;
|
||||
}
|
||||
|
||||
public void setValue(Map<String, Object> value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* 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.
|
||||
@@ -24,13 +24,12 @@ import java.util.function.Supplier;
|
||||
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.cloud.function.context.FunctionCatalog;
|
||||
import org.springframework.cloud.function.context.catalog.FunctionInspector;
|
||||
|
||||
import reactor.core.publisher.Flux;
|
||||
|
||||
/**
|
||||
* @author Dave Syer
|
||||
* @author Mark Fisher
|
||||
@@ -76,8 +75,8 @@ public class OpenWhiskFunctionInitializer {
|
||||
}
|
||||
|
||||
protected Class<?> getInputType() {
|
||||
if (inspector != null) {
|
||||
return inspector.getInputType(function());
|
||||
if (this.inspector != null) {
|
||||
return this.inspector.getInputType(function());
|
||||
}
|
||||
return Object.class;
|
||||
}
|
||||
@@ -89,7 +88,7 @@ public class OpenWhiskFunctionInitializer {
|
||||
|
||||
protected Publisher<?> apply(Publisher<?> input) {
|
||||
if (this.function != null) {
|
||||
return function.apply(input);
|
||||
return this.function.apply(input);
|
||||
}
|
||||
if (this.consumer != null) {
|
||||
this.consumer.accept(input);
|
||||
@@ -100,4 +99,5 @@ public class OpenWhiskFunctionInitializer {
|
||||
}
|
||||
throw new IllegalStateException("No function defined");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* 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.
|
||||
@@ -28,7 +28,7 @@ public class OpenWhiskInitRequest {
|
||||
private String main;
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
return this.name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
@@ -36,7 +36,7 @@ public class OpenWhiskInitRequest {
|
||||
}
|
||||
|
||||
public boolean isBinary() {
|
||||
return binary;
|
||||
return this.binary;
|
||||
}
|
||||
|
||||
public void setBinary(boolean binary) {
|
||||
@@ -44,10 +44,11 @@ public class OpenWhiskInitRequest {
|
||||
}
|
||||
|
||||
public String getMain() {
|
||||
return main;
|
||||
return this.main;
|
||||
}
|
||||
|
||||
public void setMain(String main) {
|
||||
this.main = main;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* 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.
|
||||
@@ -21,7 +21,6 @@ import java.util.Map;
|
||||
import java.util.function.Function;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
@@ -61,11 +60,11 @@ public class OpenWhiskActionHandlerTest {
|
||||
testData.put("name", "Spring");
|
||||
Map<String, Object> eventData = new HashMap<>();
|
||||
eventData.put("payload", testData);
|
||||
actionHandler.init(new OpenWhiskInitRequest());
|
||||
this.actionHandler.init(new OpenWhiskInitRequest());
|
||||
OpenWhiskActionRequest actionRequest = new OpenWhiskActionRequest();
|
||||
actionRequest.setActionName("test_action");
|
||||
actionRequest.setValue(eventData);
|
||||
Object result = actionHandler.run(actionRequest);
|
||||
Object result = this.actionHandler.run(actionRequest);
|
||||
assertNotNull(result);
|
||||
assertEquals("{\"result\":{\"name\":\"Spring\",\"message\":\"Hello, Spring\"}}",
|
||||
result);
|
||||
@@ -75,10 +74,10 @@ public class OpenWhiskActionHandlerTest {
|
||||
public void testHandlerWithoutPayload() {
|
||||
Map<String, String> testData = new HashMap<>();
|
||||
testData.put("name", "Spring");
|
||||
actionHandler.init(new OpenWhiskInitRequest());
|
||||
this.actionHandler.init(new OpenWhiskInitRequest());
|
||||
OpenWhiskActionRequest actionRequest = new OpenWhiskActionRequest();
|
||||
actionRequest.setActionName("test_action");
|
||||
Object result = actionHandler.run(actionRequest);
|
||||
Object result = this.actionHandler.run(actionRequest);
|
||||
assertNotNull(result);
|
||||
assertEquals("{\"result\":\"No input provided\"}", result);
|
||||
}
|
||||
@@ -111,6 +110,7 @@ public class OpenWhiskActionHandlerTest {
|
||||
private final String GREETINGS_FORMAT = "Hello, %s";
|
||||
|
||||
private String name;
|
||||
|
||||
private String message;
|
||||
|
||||
public Greetings() {
|
||||
@@ -122,21 +122,22 @@ public class OpenWhiskActionHandlerTest {
|
||||
}
|
||||
|
||||
public String getMessage() {
|
||||
return message;
|
||||
return this.message;
|
||||
}
|
||||
|
||||
public void setMessage(String message) {
|
||||
this.message = String.format(GREETINGS_FORMAT,
|
||||
this.name != null ? name : "nobody");
|
||||
this.message = String.format(this.GREETINGS_FORMAT,
|
||||
this.name != null ? this.name : "nobody");
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
return this.name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,2 +1,2 @@
|
||||
function.name: greeter
|
||||
function.type: function
|
||||
function.name:greeter
|
||||
function.type:function
|
||||
|
||||
Reference in New Issue
Block a user