org.springframework.cloud.function.context.FunctionType Java Examples

The following examples show how to use org.springframework.cloud.function.context.FunctionType. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: SimpleFunctionRegistryTests.java    From spring-cloud-function with Apache License 2.0 6 votes vote down vote up
@Test
public void testFunctionCompositionWithMessages() {
	FunctionRegistration<UpperCaseMessage> upperCaseRegistration = new FunctionRegistration<>(
			new UpperCaseMessage(), "uppercase")
					.type(FunctionType.of(UpperCaseMessage.class));
	FunctionRegistration<ReverseMessage> reverseRegistration = new FunctionRegistration<>(
			new ReverseMessage(), "reverse")
					.type(FunctionType.of(ReverseMessage.class));
	SimpleFunctionRegistry catalog = new SimpleFunctionRegistry(this.conversionService, this.messageConverter);
	catalog.register(upperCaseRegistration);
	catalog.register(reverseRegistration);

	Function<Flux<Message<String>>, Flux<Message<String>>> lookedUpFunction = catalog
			.lookup("uppercase|reverse");

	assertThat(lookedUpFunction).isNotNull();
	assertThat(lookedUpFunction
			.apply(Flux.just(MessageBuilder.withPayload("star").build())).blockFirst()
			.getPayload()).isEqualTo("RATS");
}
 
Example #2
Source File: SimpleFunctionRegistryTests.java    From spring-cloud-function with Apache License 2.0 6 votes vote down vote up
@Test
public void testFunctionCompositionMixedMessages() {
	FunctionRegistration<UpperCaseMessage> upperCaseRegistration = new FunctionRegistration<>(
			new UpperCaseMessage(), "uppercase")
					.type(FunctionType.of(UpperCaseMessage.class));
	FunctionRegistration<Reverse> reverseRegistration = new FunctionRegistration<>(
			new Reverse(), "reverse").type(FunctionType.of(Reverse.class));
	SimpleFunctionRegistry catalog = new SimpleFunctionRegistry(this.conversionService, this.messageConverter);
	catalog.register(upperCaseRegistration);
	catalog.register(reverseRegistration);

	Function<Message<String>, String> lookedUpFunction = catalog
			.lookup("uppercase|reverse");

	assertThat(lookedUpFunction).isNotNull();
	String result = lookedUpFunction.apply(MessageBuilder.withPayload("star").setHeader("foo", "bar").build());
	assertThat(result).isEqualTo("RATS");
}
 
Example #3
Source File: SimpleFunctionRegistryTests.java    From spring-cloud-function with Apache License 2.0 6 votes vote down vote up
@Test
@Disabled
public void testFunctionCompletelyImplicitComposition() {
	FunctionRegistration<Words> wordsRegistration = new FunctionRegistration<>(
			new Words(), "words").type(FunctionType.of(Words.class));
	FunctionRegistration<Reverse> reverseRegistration = new FunctionRegistration<>(
			new Reverse(), "reverse").type(FunctionType.of(Reverse.class));
	SimpleFunctionRegistry catalog = new SimpleFunctionRegistry(this.conversionService, this.messageConverter);
	catalog.register(wordsRegistration);
	catalog.register(reverseRegistration);

	// There's only one function, we should be able to leave that blank
	Supplier<Flux<String>> lookedUpFunction = catalog.lookup("|");

	assertThat(lookedUpFunction).isNotNull();
	assertThat(lookedUpFunction.get().blockFirst()).isEqualTo("olleh");
}
 
Example #4
Source File: SimpleFunctionRegistryTests.java    From spring-cloud-function with Apache License 2.0 6 votes vote down vote up
@Test
public void testFunctionCompositionImplicit() {
	FunctionRegistration<Words> wordsRegistration = new FunctionRegistration<>(
			new Words(), "words").type(FunctionType.of(Words.class));
	FunctionRegistration<Reverse> reverseRegistration = new FunctionRegistration<>(
			new Reverse(), "reverse").type(FunctionType.of(Reverse.class));
	FunctionRegistry catalog = new SimpleFunctionRegistry(this.conversionService, this.messageConverter);
	catalog.register(wordsRegistration);
	catalog.register(reverseRegistration);

	// There's only one function, we should be able to leave that blank
	Supplier<String> lookedUpFunction = catalog.lookup("words|");

	assertThat(lookedUpFunction).isNotNull();
	assertThat(lookedUpFunction.get()).isEqualTo("olleh");
}
 
Example #5
Source File: SimpleFunctionRegistryTests.java    From spring-cloud-function with Apache License 2.0 6 votes vote down vote up
@Test
public void testFunctionComposition() {
	FunctionRegistration<UpperCase> upperCaseRegistration = new FunctionRegistration<>(
			new UpperCase(), "uppercase").type(FunctionType.of(UpperCase.class));
	FunctionRegistration<Reverse> reverseRegistration = new FunctionRegistration<>(
			new Reverse(), "reverse").type(FunctionType.of(Reverse.class));
	SimpleFunctionRegistry catalog = new SimpleFunctionRegistry(this.conversionService, this.messageConverter);
	catalog.register(upperCaseRegistration);
	catalog.register(reverseRegistration);

	Function<Flux<String>, Flux<String>> lookedUpFunction = catalog
			.lookup("uppercase|reverse");
	assertThat(lookedUpFunction).isNotNull();
	assertThat(lookedUpFunction.apply(Flux.just("star")).blockFirst())
			.isEqualTo("RATS");
}
 
Example #6
Source File: SimpleFunctionRegistryTests.java    From spring-cloud-function with Apache License 2.0 6 votes vote down vote up
@Test
public void testFunctionLookup() {

	TestFunction function = new TestFunction();
	FunctionRegistration<TestFunction> registration = new FunctionRegistration<>(
			function, "foo").type(FunctionType.of(TestFunction.class));
	SimpleFunctionRegistry catalog = new SimpleFunctionRegistry(this.conversionService, this.messageConverter);
	catalog.register(registration);

	FunctionInvocationWrapper lookedUpFunction = catalog.lookup("hello");
	assertThat(lookedUpFunction).isNotNull(); // because we only have one and can look it up with any name
	FunctionRegistration<TestFunction> registration2 = new FunctionRegistration<>(
			function, "foo2").type(FunctionType.of(TestFunction.class));
	catalog.register(registration2);
	lookedUpFunction = catalog.lookup("hello");
	assertThat(lookedUpFunction).isNull();
}
 
Example #7
Source File: SimpleFunctionRegistryTests.java    From spring-cloud-function with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings({ "rawtypes", "unchecked" })
@Test
public void testReactiveFunctionMessages() {
	FunctionRegistration<ReactiveFunction> registration = new FunctionRegistration<>(new ReactiveFunction(), "reactive")
		.type(FunctionType.of(ReactiveFunction.class));

	SimpleFunctionRegistry catalog = new SimpleFunctionRegistry(this.conversionService, this.messageConverter);
	catalog.register(registration);

	Function lookedUpFunction = catalog.lookup("reactive");

	assertThat(lookedUpFunction).isNotNull();
	Flux<List<String>> result = (Flux<List<String>>) lookedUpFunction
		.apply(Flux.just(MessageBuilder
			.withPayload("[{\"name\":\"item1\"},{\"name\":\"item2\"}]")
			.setHeader(MessageHeaders.CONTENT_TYPE, "application/json")
			.build()
		));
	Assertions.assertIterableEquals(result.blockFirst(), Arrays.asList("item1", "item2"));
}
 
Example #8
Source File: FunctionTypeUtilsTests.java    From spring-cloud-function with Apache License 2.0 6 votes vote down vote up
@Test
public void testFunctionTypeByClassDiscovery() {
	FunctionType type = FunctionType.of(FunctionTypeUtils.discoverFunctionTypeFromClass(Function.class));
	assertThat(type.getInputType()).isAssignableFrom(Object.class);

	type = FunctionType.of(FunctionTypeUtils.discoverFunctionTypeFromClass(MessageFunction.class));
	assertThat(type.getInputType()).isAssignableFrom(String.class);
	assertThat(type.getOutputType()).isAssignableFrom(String.class);

	type = FunctionType.of(FunctionTypeUtils.discoverFunctionTypeFromClass(MyMessageFunction.class));
	assertThat(type.getInputType()).isAssignableFrom(String.class);
	assertThat(type.getOutputType()).isAssignableFrom(String.class);

	type = FunctionType.of(FunctionTypeUtils.discoverFunctionTypeFromClass(MessageConsumer.class));
	assertThat(type.getInputType()).isAssignableFrom(String.class);

	type = FunctionType.of(FunctionTypeUtils.discoverFunctionTypeFromClass(MyMessageConsumer.class));
	assertThat(type.getInputType()).isAssignableFrom(String.class);
}
 
Example #9
Source File: ContextFunctionCatalogInitializerTests.java    From spring-cloud-function with Apache License 2.0 5 votes vote down vote up
@Test
public void simpleFunction() {
	create(SimpleConfiguration.class);
	Object bean = this.context.getBean("function");
	assertThat(bean).isInstanceOf(FunctionRegistration.class);
	Function<Flux<String>, Flux<Person>> function
		= this.catalog.lookup(Function.class, "function");
	assertThat(function.apply(Flux.just("{\"name\":\"foo\"}")).blockFirst().getName()).isEqualTo("FOO");
	assertThat(bean).isNotSameAs(function);
	assertThat(this.inspector.getRegistration(function)).isNotNull();
	assertThat(this.inspector.getRegistration(function).getType())
			.isEqualTo(FunctionType.from(Person.class).to(Person.class));
}
 
Example #10
Source File: SimpleFunctionRegistryTests.java    From spring-cloud-function with Apache License 2.0 5 votes vote down vote up
@Test
public void testFunctionCompositionExplicit() {
	FunctionRegistration<Words> wordsRegistration = new FunctionRegistration<>(
			new Words(), "words").type(FunctionType.of(Words.class));
	FunctionRegistration<Reverse> reverseRegistration = new FunctionRegistration<>(
			new Reverse(), "reverse").type(FunctionType.of(Reverse.class));
	SimpleFunctionRegistry catalog = new SimpleFunctionRegistry(this.conversionService, this.messageConverter);
	catalog.register(wordsRegistration);
	catalog.register(reverseRegistration);

	Supplier<String> lookedUpFunction = catalog.lookup("words|reverse");

	assertThat(lookedUpFunction).isNotNull();
	assertThat(lookedUpFunction.get()).isEqualTo("olleh");
}
 
Example #11
Source File: ContextFunctionCatalogInitializerTests.java    From spring-cloud-function with Apache License 2.0 5 votes vote down vote up
@Test
public void scanFunction() {
	create(EmptyConfiguration.class,
			"spring.cloud.function.scan.packages=org.springframework.cloud.function.context.scan");
	Object bean = this.context.getBean(TestFunction.class.getName());
	assertThat(bean).isInstanceOf(Function.class);
	Function<Flux<String>, Flux<String>> function = this.catalog
			.lookup(Function.class, TestFunction.class.getName());
	assertThat(function.apply(Flux.just("foo")).blockFirst()).isEqualTo("FOO");
	assertThat(bean).isNotSameAs(function);
	assertThat(this.inspector.getRegistration(function)).isNotNull();
	assertThat(this.inspector.getRegistration(function).getType())
			.isEqualTo(FunctionType.from(String.class).to(String.class));
}
 
Example #12
Source File: ContextFunctionCatalogInitializerTests.java    From spring-cloud-function with Apache License 2.0 5 votes vote down vote up
@Override
public void initialize(GenericApplicationContext context) {
	context.registerBean("function", FunctionRegistration.class,
			() -> new FunctionRegistration<>(function()).type(
					FunctionType.from(Person.class).to(Person.class).getType()));
	context.registerBean("supplier", FunctionRegistration.class,
			() -> new FunctionRegistration<>(supplier())
					.type(FunctionType.supplier(String.class).getType()));
	context.registerBean("consumer", FunctionRegistration.class,
			() -> new FunctionRegistration<>(consumer())
					.type(FunctionType.consumer(String.class).getType()));
	context.registerBean(SimpleConfiguration.class, () -> this);
}
 
Example #13
Source File: ContextFunctionCatalogInitializerTests.java    From spring-cloud-function with Apache License 2.0 5 votes vote down vote up
@Override
public void initialize(GenericApplicationContext context) {
	context.registerBean("function", FunctionRegistration.class,
			() -> new FunctionRegistration<>(function()).type(
					FunctionType.from(String.class).to(String.class).getType()));
	context.registerBean(PropertiesConfiguration.class, () -> this);
}
 
Example #14
Source File: ContextFunctionCatalogInitializerTests.java    From spring-cloud-function with Apache License 2.0 5 votes vote down vote up
@Override
public void initialize(GenericApplicationContext context) {
	context.registerBean("function", FunctionRegistration.class,
			() -> new FunctionRegistration<>(function()).type(
					FunctionType.from(String.class).to(String.class).getType()));
	context.registerBean(ValueConfiguration.class, () -> this);
}
 
Example #15
Source File: ContextFunctionCatalogInitializerTests.java    From spring-cloud-function with Apache License 2.0 5 votes vote down vote up
@Override
public void initialize(GenericApplicationContext context) {
	context.registerBean(String.class, () -> value());
	context.registerBean("foos", FunctionRegistration.class,
			() -> new FunctionRegistration<>(foos(context.getBean(String.class)))
					.type(FunctionType.from(String.class).to(Foo.class)
							.getType()));
}
 
Example #16
Source File: StreamBridge.java    From spring-cloud-stream with Apache License 2.0 5 votes vote down vote up
@Override
public void afterSingletonsInstantiated() {
	if (this.initialized) {
		return;
	}
	FunctionRegistration<Function<Object, Object>> fr = new FunctionRegistration<>(v -> v, STREAM_BRIDGE_FUNC_NAME);
	this.functionRegistry.register(fr.type(FunctionType.from(Object.class).to(Object.class).message()));
	Map<String, DirectWithAttributesChannel> channels = applicationContext.getBeansOfType(DirectWithAttributesChannel.class);
	for (Entry<String, DirectWithAttributesChannel> channelEntry : channels.entrySet()) {
		if (channelEntry.getValue().getAttribute("type").equals("output")) {
			this.channelCache.put(channelEntry.getKey(), channelEntry.getValue());
		}
	}
	this.initialized = true;
}
 
Example #17
Source File: ImplicitFunctionBindingTests.java    From spring-cloud-stream with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings({ "unchecked", "rawtypes" })
@Test
public void dynamicBindingTestWithFunctionRegistrationAndExplicitDestination() {
	try (ConfigurableApplicationContext context = new SpringApplicationBuilder(
			TestChannelBinderConfiguration.getCompleteConfiguration(EmptyConfiguration.class))
					.web(WebApplicationType.NONE)
					.run("--spring.jmx.enabled=false", "--spring.cloud.stream.bindings.function-in-0.destination=input")) {

		InputDestination input = context.getBean(InputDestination.class);
		try {
			input.send(new GenericMessage<byte[]>("hello".getBytes()));
			fail(); // it should since there are no functions and no bindings
		}
		catch (Exception e) {
			// good, we expected it
		}

		Function<byte[], byte[]> function = v -> v;
		FunctionRegistration functionRegistration = new FunctionRegistration(function, "function");
		functionRegistration = functionRegistration.type(FunctionType.from(byte[].class).to(byte[].class));
		FunctionBindingTestUtils.bind(context, functionRegistration);

		input.send(new GenericMessage<byte[]>("hello".getBytes()), "input");

		OutputDestination output = context.getBean(OutputDestination.class);
		assertThat(output.receive(1000, "function-out-0").getPayload()).isEqualTo("hello".getBytes());
	}
}
 
Example #18
Source File: BeanFactoryAwareFunctionRegistry.java    From spring-cloud-function with Apache License 2.0 5 votes vote down vote up
@Override
Type discoverFunctionType(Object function, String... names) {
	if (function instanceof RoutingFunction) {
		return FunctionType.of(FunctionContextUtils.findType(applicationContext.getBeanFactory(), names)).getType();
	}
	else if (function instanceof FunctionRegistration) {
		return ((FunctionRegistration) function).getType().getType();
	}
	boolean beanDefinitionExists = false;
	for (int i = 0; i < names.length && !beanDefinitionExists; i++) {
		beanDefinitionExists = this.applicationContext.getBeanFactory().containsBeanDefinition(names[i]);
		if (this.applicationContext.containsBean("&" + names[i])) {
			Class<?> objectType = this.applicationContext.getBean("&" + names[i], FactoryBean.class)
				.getObjectType();
			return FunctionTypeUtils.discoverFunctionTypeFromClass(objectType);
		}
	}
	if (!beanDefinitionExists) {
		logger.info("BeanDefinition for function name(s) '" + Arrays.asList(names) +
			"' can not be located. FunctionType will be based on " + function.getClass());
	}

	Type type = FunctionTypeUtils.discoverFunctionTypeFromClass(function.getClass());
	if (beanDefinitionExists) {
		Type t = FunctionTypeUtils.getImmediateGenericType(type, 0);
		if (t == null || t == Object.class) {
			type = FunctionType.of(FunctionContextUtils.findType(this.applicationContext.getBeanFactory(), names)).getType();
		}
	}
	return type;
}
 
Example #19
Source File: AbstractComposableFunctionRegistry.java    From spring-cloud-function with Apache License 2.0 5 votes vote down vote up
@Override
public FunctionRegistration<?> getRegistration(Object function) {
	String functionName = function == null ? null
			: this.lookupFunctionName(function);
	if (StringUtils.hasText(functionName)) {
		FunctionRegistration<?> registration = new FunctionRegistration<Object>(
				function, functionName);
		FunctionType functionType = this.findType(registration, functionName);
		return registration.type(functionType.getType());
	}
	return null;
}
 
Example #20
Source File: FunctionEndpointInitializerTests.java    From spring-cloud-function with Apache License 2.0 5 votes vote down vote up
@Override
public void initialize(GenericApplicationContext applicationContext) {
	applicationContext.registerBean("uppercase", FunctionRegistration.class,
			() -> new FunctionRegistration<>(uppercase())
				.type(FunctionType.from(String.class).to(String.class)));
	applicationContext.registerBean("reverse", FunctionRegistration.class,
			() -> new FunctionRegistration<>(reverse())
				.type(FunctionType.from(String.class).to(String.class)));
	applicationContext.registerBean("lowercase", FunctionRegistration.class,
			() -> new FunctionRegistration<>(lowercase())
				.type(FunctionType.from(String.class).to(String.class)));
	applicationContext.registerBean("supplier", FunctionRegistration.class,
			() -> new FunctionRegistration<>(supplier())
				.type(FunctionType.supplier(String.class)));
}
 
Example #21
Source File: FunctionExporterAutoConfiguration.java    From spring-cloud-function with Apache License 2.0 5 votes vote down vote up
@Bean
@ConditionalOnProperty(prefix = "spring.cloud.function.web.export.source", name = "url")
public FunctionRegistration<Supplier<Flux<?>>> origin(WebClient.Builder builder) {
	HttpSupplier supplier = new HttpSupplier(builder.build(), this.props);
	FunctionRegistration<Supplier<Flux<?>>> registration = new FunctionRegistration<>(supplier);
	FunctionType type = FunctionType.supplier(this.props.getSource().getType()).wrap(Flux.class);
	if (this.props.getSource().isIncludeHeaders()) {
		type = type.message();
	}
	registration = registration.type(type);
	return registration;
}
 
Example #22
Source File: DemoApplication.java    From spring-graalvm-native with Apache License 2.0 4 votes vote down vote up
@Override
public void initialize(GenericApplicationContext context) {
	context.registerBean("foobar", FunctionRegistration.class,
			() -> new FunctionRegistration<>(new Foobar()).type(
					FunctionType.from(String.class).to(String.class)));
}
 
Example #23
Source File: AbstractComposableFunctionRegistry.java    From spring-cloud-function with Apache License 2.0 4 votes vote down vote up
protected void addType(String name, FunctionType functionType) {
	this.types.computeIfAbsent(name, str -> functionType);
}
 
Example #24
Source File: FunctionTypeUtilsTests.java    From spring-cloud-function with Apache License 2.0 4 votes vote down vote up
@Test
public void testWithComplexHierarchy() {
	FunctionType type = FunctionType.of(FunctionTypeUtils.discoverFunctionTypeFromClass(ReactiveFunctionImpl.class));
	assertThat(String.class).isAssignableFrom(type.getInputType());
	assertThat(Integer.class).isAssignableFrom(type.getOutputType());
}
 
Example #25
Source File: AbstractComposableFunctionRegistry.java    From spring-cloud-function with Apache License 2.0 4 votes vote down vote up
protected FunctionType findType(FunctionRegistration<?> functionRegistration, String name) {
	return functionRegistration.getType() != null
			?  functionRegistration.getType()
					:  this.getFunctionType(name);
}
 
Example #26
Source File: AbstractComposableFunctionRegistry.java    From spring-cloud-function with Apache License 2.0 4 votes vote down vote up
public FunctionType getFunctionType(String name) {
	return this.types.get(name);
}
 
Example #27
Source File: FunctionalExporterTests.java    From spring-cloud-function with Apache License 2.0 4 votes vote down vote up
@Override
public void initialize(GenericApplicationContext context) {
	context.registerBean("uppercase", FunctionRegistration.class,
			() -> new FunctionRegistration<>(uppercase()).type(
					FunctionType.from(Person.class).to(String.class).message()));
}
 
Example #28
Source File: DemoApplication.java    From spring-cloud-function with Apache License 2.0 4 votes vote down vote up
@Override
public void initialize(GenericApplicationContext context) {
	context.registerBean("foobar", FunctionRegistration.class,
			() -> new FunctionRegistration<>(new Foobar())
					.type(FunctionType.from(Foo.class).to(Foo.class)));
}
 
Example #29
Source File: LambdaApplication.java    From spring-cloud-function with Apache License 2.0 4 votes vote down vote up
@Override
public void initialize(GenericApplicationContext context) {
	context.registerBean("uppercase", FunctionRegistration.class,
			() -> new FunctionRegistration<>(uppercase()).type(
					FunctionType.from(String.class).to(String.class)));
}
 
Example #30
Source File: FunctionApplication.java    From spring-init with Apache License 2.0 4 votes vote down vote up
@Bean
public FunctionRegistration<Function<String, String>> uppercase() {
	return new FunctionRegistration<Function<String, String>>(value -> value.toUpperCase())
			.type(FunctionType.from(String.class).to(String.class));
}