Add "wrapper" type methods to FunctionInspector

These can be used to more reliably discover whether the user
has declared a function with flux types or "bare" POJOs. They
then pave the way to supporting single valued types in a special
way.

Also consolidate and simplify the logic in FunctionInspector
This commit is contained in:
Dave Syer
2017-05-24 09:08:30 +01:00
parent 719237e9c7
commit 0d2418a47b
11 changed files with 283 additions and 84 deletions

View File

@@ -30,9 +30,12 @@ import org.junit.Test;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.cloud.function.test.GenericFunction;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import static org.assertj.core.api.Assertions.assertThat;
@@ -68,6 +71,34 @@ public class ContextFunctionCatalogAutoConfigurationTests {
assertThat(context.getBean("function")).isInstanceOf(Function.class);
assertThat(catalog.lookupFunction("function")).isInstanceOf(Function.class);
assertThat(inspector.getInputType("function")).isAssignableFrom(Map.class);
assertThat(inspector.getInputWrapper("function")).isAssignableFrom(Map.class);
}
@Test
public void genericFluxFunction() {
create(GenericFluxConfiguration.class);
assertThat(context.getBean("function")).isInstanceOf(Function.class);
assertThat(catalog.lookupFunction("function")).isInstanceOf(Function.class);
assertThat(inspector.getInputType("function")).isAssignableFrom(Map.class);
assertThat(inspector.getInputWrapper("function")).isAssignableFrom(Flux.class);
}
@Test
public void externalFunction() {
create(ExternalConfiguration.class);
assertThat(context.getBean("function")).isInstanceOf(Function.class);
assertThat(catalog.lookupFunction("function")).isInstanceOf(Function.class);
assertThat(inspector.getInputType("function")).isAssignableFrom(Map.class);
assertThat(inspector.getInputWrapper("function")).isAssignableFrom(Map.class);
}
@Test
public void componentScanFunction() {
create(ComponentScanConfiguration.class);
assertThat(context.getBean("function")).isInstanceOf(Function.class);
assertThat(catalog.lookupFunction("function")).isInstanceOf(Function.class);
assertThat(inspector.getInputType("function")).isAssignableFrom(Map.class);
assertThat(inspector.getInputWrapper("function")).isAssignableFrom(Map.class);
}
@Test
@@ -149,6 +180,28 @@ public class ContextFunctionCatalogAutoConfigurationTests {
}
}
@EnableAutoConfiguration
@Configuration
@Import(GenericFunction.class)
protected static class ExternalConfiguration {
}
@EnableAutoConfiguration
@Configuration
@ComponentScan(basePackageClasses=GenericFunction.class)
protected static class ComponentScanConfiguration {
}
@EnableAutoConfiguration
@Configuration
protected static class GenericFluxConfiguration {
@Bean
public Function<Flux<Map<String, String>>, Flux<Map<String, String>>> function() {
return flux -> flux.map(m -> m.entrySet().stream().collect(Collectors
.toMap(e -> e.getKey(), e -> e.getValue().toString().toUpperCase())));
}
}
@EnableAutoConfiguration
@Configuration
protected static class QualifiedConfiguration {
@@ -184,3 +237,4 @@ public class ContextFunctionCatalogAutoConfigurationTests {
}
}

View File

@@ -0,0 +1,38 @@
/*
* Copyright 2016-2017 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.test;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* @author Dave Syer
*
*/
@Configuration
public class GenericFunction {
@Bean
public Function<Map<String, String>, Map<String, String>> function() {
return m -> m.entrySet().stream().collect(Collectors.toMap(e -> e.getKey(),
e -> e.getValue().toString().toUpperCase()));
}
}