Add support for detecting FunctionRegistration or Function
User can now provide a Function or an ApplicationInitializer. Also the initializer can create a FunctionRegistration with the handler name instead of a bean with the handler name. Better control of input and output types that way. Fixes gh-231
This commit is contained in:
@@ -21,7 +21,6 @@ import java.util.function.Supplier;
|
||||
|
||||
import org.junit.Assume;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
@@ -97,13 +96,45 @@ public abstract class FunctionCreatorConfigurationTests {
|
||||
@EnableAutoConfiguration
|
||||
@TestPropertySource(properties = {
|
||||
"function.location=app:classpath,file:target/test-classes,file:target/test-classes/app",
|
||||
"function.bean=myDoubler",
|
||||
"function.bean=doubler",
|
||||
"function.main=org.springframework.cloud.function.test.FunctionRegistrar" })
|
||||
public static class SingleFunctionWithRegistrarTests
|
||||
extends FunctionCreatorConfigurationTests {
|
||||
|
||||
@Test
|
||||
@Ignore // related to boot 2.1 no bean override change
|
||||
public void testDouble() {
|
||||
Function<Flux<Integer>, Flux<Integer>> function = catalog
|
||||
.lookup(Function.class, "function0");
|
||||
assertThat(function.apply(Flux.just(2)).blockFirst()).isEqualTo(4);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@EnableAutoConfiguration
|
||||
@TestPropertySource(properties = {
|
||||
"function.location=app:classpath,file:target/test-classes,file:target/test-classes/app",
|
||||
"function.bean=frenchizer",
|
||||
"function.main=org.springframework.cloud.function.test.FunctionRegistrar" })
|
||||
public static class SingleFunctionWithRegistrarAndRegistrationTests
|
||||
extends FunctionCreatorConfigurationTests {
|
||||
|
||||
@Test
|
||||
public void testFrenchize() {
|
||||
Function<Flux<Integer>, Flux<String>> function = catalog
|
||||
.lookup(Function.class, "function0");
|
||||
assertThat(function.apply(Flux.just(2)).blockFirst()).isEqualTo("deux");
|
||||
}
|
||||
}
|
||||
|
||||
@EnableAutoConfiguration
|
||||
@TestPropertySource(properties = {
|
||||
"function.location=app:classpath,file:target/test-classes,file:target/test-classes/app",
|
||||
"function.bean=myDoubler",
|
||||
"function.main=org.springframework.cloud.function.test.FunctionInitializer" })
|
||||
public static class SingleFunctionWithInitializerTests
|
||||
extends FunctionCreatorConfigurationTests {
|
||||
|
||||
@Test
|
||||
public void testDouble() {
|
||||
Function<Flux<Integer>, Flux<Integer>> function = catalog
|
||||
.lookup(Function.class, "function0");
|
||||
|
||||
@@ -0,0 +1,53 @@
|
||||
/*
|
||||
* Copyright 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 org.springframework.boot.SpringApplication;
|
||||
import org.springframework.cloud.function.context.FunctionalSpringApplication;
|
||||
import org.springframework.context.ApplicationContextInitializer;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.support.GenericApplicationContext;
|
||||
|
||||
/**
|
||||
* @author Dave Syer
|
||||
*/
|
||||
public class FunctionInitializer
|
||||
implements ApplicationContextInitializer<GenericApplicationContext> {
|
||||
|
||||
@Bean
|
||||
public Doubler myDoubler() {
|
||||
return new Doubler();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public Frenchizer myFrenchizer() {
|
||||
return new Frenchizer();
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
SpringApplication application = new FunctionalSpringApplication(
|
||||
FunctionInitializer.class);
|
||||
application.run(args);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void initialize(GenericApplicationContext context) {
|
||||
// TODO: support for FunctionRegistration
|
||||
context.registerBean("myDoubler", Doubler.class, () -> myDoubler());
|
||||
context.registerBean("myFrenchizer", Frenchizer.class, () -> myFrenchizer());
|
||||
}
|
||||
}
|
||||
@@ -16,10 +16,10 @@
|
||||
|
||||
package org.springframework.cloud.function.test;
|
||||
|
||||
import java.util.Collections;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.cloud.function.context.FunctionRegistration;
|
||||
import org.springframework.cloud.function.context.FunctionType;
|
||||
import org.springframework.cloud.function.context.FunctionalSpringApplication;
|
||||
import org.springframework.context.ApplicationContextInitializer;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.support.GenericApplicationContext;
|
||||
@@ -41,21 +41,21 @@ public class FunctionRegistrar
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
SpringApplication application = new SpringApplication(Object.class) {
|
||||
@Override
|
||||
protected void load(ApplicationContext context, Object[] sources) {
|
||||
}
|
||||
};
|
||||
application.addInitializers(new FunctionRegistrar());
|
||||
application.setDefaultProperties(
|
||||
Collections.singletonMap("spring.functional.enabled", "true"));
|
||||
SpringApplication application = new FunctionalSpringApplication(
|
||||
FunctionRegistrar.class);
|
||||
application.run(args);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void initialize(GenericApplicationContext context) {
|
||||
// TODO: support for FunctionRegistration
|
||||
context.registerBean("myDoubler", Doubler.class, () -> myDoubler());
|
||||
context.registerBean("myFrenchizer", Frenchizer.class, () -> myFrenchizer());
|
||||
context.registerBean("theDoubler", FunctionRegistration.class,
|
||||
() -> new FunctionRegistration<>(myDoubler(), "doubler")
|
||||
.type(FunctionType.of((Doubler.class))));
|
||||
context.registerBean("frenchizer", FunctionRegistration.class, () -> {
|
||||
Frenchizer function = myFrenchizer();
|
||||
function.init();
|
||||
return new FunctionRegistration<>(function, "theFrenchizer")
|
||||
.type(FunctionType.of((Frenchizer.class)));
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user