From c685866f097996b5cc1a93549819158a6ce7afc0 Mon Sep 17 00:00:00 2001 From: Dave Syer Date: Thu, 25 May 2017 07:51:26 +0100 Subject: [PATCH] Fix compiled scripts demo from README Compiled functions always show up with no metadata, and you can only guess what the types are from the bean definition. Probably we should add more information to the bean definition if we have it when we compile the function. There is still a problem if user defines functions that are not of Flux (but that has always been the case). --- README.adoc | 6 +- scripts/registerConsumer.sh | 2 +- scripts/registerFunction.sh | 2 +- scripts/registerSupplier.sh | 2 +- ...ntextFunctionCatalogAutoConfiguration.java | 6 +- .../cloud/function/web/SingletonTests.java | 97 +++++++++++++++++++ 6 files changed, 108 insertions(+), 7 deletions(-) create mode 100644 spring-cloud-function-web/src/test/java/org/springframework/cloud/function/web/SingletonTests.java diff --git a/README.adoc b/README.adoc index b91a42be9..dc40153a3 100644 --- a/README.adoc +++ b/README.adoc @@ -137,7 +137,7 @@ Also, start a RabbitMQ server locally (e.g. execute `rabbitmq-server`). ---- ./web.sh -f uppercase -p 9000 -curl -H "Content-Type: text/plain" -H "Accept: text/plain" :9000/uppercase -d foo +curl -H "Content-Type: text/plain" -H "Accept: text/plain" localhost:9000/uppercase -d foo ---- === Register a Supplier: @@ -150,7 +150,7 @@ curl -H "Content-Type: text/plain" -H "Accept: text/plain" :9000/uppercase -d fo ---- ./web.sh -s words -p 9001 -curl -H "Accept: application/json" :9001/words +curl -H "Accept: application/json" localhost:9001/words ---- === Register a Consumer: @@ -163,7 +163,7 @@ curl -H "Accept: application/json" :9001/words ---- ./web.sh -c print -p 9002 -curl -X POST -H "Content-Type: text/plain" -d foo :9002/print +curl -X POST -H "Content-Type: text/plain" -d foo localhost:9002/print ---- === Run Stream Processing Microservices: diff --git a/scripts/registerConsumer.sh b/scripts/registerConsumer.sh index 37fa380a8..7836c40de 100755 --- a/scripts/registerConsumer.sh +++ b/scripts/registerConsumer.sh @@ -11,4 +11,4 @@ while getopts ":n:f:" opt; do esac done -curl -X POST -H "Content-Type: text/plain" -d $FUNC :8080/consumer/$NAME +curl -X POST -H "Content-Type: text/plain" -d $FUNC localhost:8080/consumer/$NAME diff --git a/scripts/registerFunction.sh b/scripts/registerFunction.sh index 34ce2f921..5638f4db8 100755 --- a/scripts/registerFunction.sh +++ b/scripts/registerFunction.sh @@ -17,5 +17,5 @@ while getopts ":n:f:i:o:" opt; do esac done -curl -X POST -H "Content-Type: text/plain" -d $FUNC ":8080/function/$NAME?inputType=$INTYPE&outputType=$OUTTYPE" +curl -X POST -H "Content-Type: text/plain" -d $FUNC "localhost:8080/function/$NAME?inputType=$INTYPE&outputType=$OUTTYPE" diff --git a/scripts/registerSupplier.sh b/scripts/registerSupplier.sh index 107368990..df853198f 100755 --- a/scripts/registerSupplier.sh +++ b/scripts/registerSupplier.sh @@ -14,4 +14,4 @@ while getopts ":n:f:t:" opt; do esac done -curl -X POST -H "Content-Type: text/plain" -d $FUNC :8080/supplier/$NAME?type=$TYPE +curl -X POST -H "Content-Type: text/plain" -d $FUNC localhost:8080/supplier/$NAME?type=$TYPE diff --git a/spring-cloud-function-context/src/main/java/org/springframework/cloud/function/context/ContextFunctionCatalogAutoConfiguration.java b/spring-cloud-function-context/src/main/java/org/springframework/cloud/function/context/ContextFunctionCatalogAutoConfiguration.java index 1fd346301..fbaca8443 100644 --- a/spring-cloud-function-context/src/main/java/org/springframework/cloud/function/context/ContextFunctionCatalogAutoConfiguration.java +++ b/spring-cloud-function-context/src/main/java/org/springframework/cloud/function/context/ContextFunctionCatalogAutoConfiguration.java @@ -35,6 +35,7 @@ import java.util.function.Function; import java.util.function.Supplier; import org.springframework.beans.BeansException; +import org.springframework.beans.factory.ListableBeanFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.beans.factory.config.BeanDefinition; @@ -422,7 +423,10 @@ public class ContextFunctionCatalogAutoConfiguration { param = resolvable.getGeneric(index).getGeneric(0).getType(); } else { - // TODO: compiled functions only work as String -> String + // TODO: compiled functions (only work as String -> String) + if (paramType.isWrapper()) { + return Flux.class; + } return String.class; } } diff --git a/spring-cloud-function-web/src/test/java/org/springframework/cloud/function/web/SingletonTests.java b/spring-cloud-function-web/src/test/java/org/springframework/cloud/function/web/SingletonTests.java new file mode 100644 index 000000000..3f6d113ab --- /dev/null +++ b/spring-cloud-function-web/src/test/java/org/springframework/cloud/function/web/SingletonTests.java @@ -0,0 +1,97 @@ +/* + * Copyright 2012-2015 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 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.cloud.function.web; + +import java.net.URI; +import java.util.function.Supplier; + +import org.junit.Test; +import org.junit.runner.RunWith; + +import org.springframework.beans.BeansException; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; +import org.springframework.beans.factory.support.BeanDefinitionRegistry; +import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor; +import org.springframework.beans.factory.support.RootBeanDefinition; +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.boot.context.embedded.LocalServerPort; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; +import org.springframework.boot.test.web.client.TestRestTemplate; +import org.springframework.context.annotation.Bean; +import org.springframework.http.HttpStatus; +import org.springframework.http.RequestEntity; +import org.springframework.http.ResponseEntity; +import org.springframework.test.context.junit4.SpringRunner; + +import static org.assertj.core.api.Assertions.assertThat; + +import reactor.core.publisher.Flux; + +/** + * @author Dave Syer + * + */ +@RunWith(SpringRunner.class) +@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT) +public class SingletonTests { + + @LocalServerPort + private int port; + @Autowired + private TestRestTemplate rest; + + @Test + public void words() throws Exception { + ResponseEntity result = rest.exchange( + RequestEntity.get(new URI("/words")).build(), String.class); + assertThat(result.getStatusCode()).isEqualTo(HttpStatus.OK); + assertThat(result.getBody()).isEqualTo("[\"foo\",\"bar\"]"); + } + + @EnableAutoConfiguration + @org.springframework.boot.test.context.TestConfiguration + protected static class TestConfiguration { + @Bean + public static BeanDefinitionRegistryPostProcessor processor() { + return new BeanDefinitionRegistryPostProcessor() { + + @Override + public void postProcessBeanFactory( + ConfigurableListableBeanFactory beanFactory) + throws BeansException { + } + + @Override + public void postProcessBeanDefinitionRegistry( + BeanDefinitionRegistry registry) throws BeansException { + // Simulates what happens whem you add a compiled function + RootBeanDefinition beanDefinition = new RootBeanDefinition(MySupplier.class); + registry.registerBeanDefinition("words", beanDefinition); + } + }; + } + } + + static class MySupplier implements Supplier> { + @Override + public Flux get() { + return Flux.just("foo", "bar"); + } + } +}