Java Code Examples for org.springframework.cloud.function.context.FunctionCatalog#lookup()

The following examples show how to use org.springframework.cloud.function.context.FunctionCatalog#lookup() . 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: BeanFactoryAwarePojoFunctionRegistryTests.java    From spring-cloud-function with Apache License 2.0 6 votes vote down vote up
@Test
	public void testWithPojoFunction() {
		FunctionCatalog catalog = this.configureCatalog();

//		MyFunctionLike f1 = catalog.lookup("myFunctionLike");
//		assertThat(f1.uppercase("foo")).isEqualTo("FOO");

		Function<String, String> f2 = catalog.lookup("myFunctionLike");
		assertThat(f2.apply("foo")).isEqualTo("FOO");

		Function<Integer, String> f2conversion = catalog.lookup("myFunctionLike");
		assertThat(f2conversion.apply(123)).isEqualTo("123");

		Function<Message<String>, String> f2message = catalog.lookup("myFunctionLike");
		assertThat(f2message.apply(MessageBuilder.withPayload("message").build())).isEqualTo("MESSAGE");

		Function<Message<String>, Message<byte[]>> f2messageReturned = catalog.lookup("myFunctionLike", "application/json");
		assertThat(new String(f2messageReturned.apply(MessageBuilder.withPayload("message").build()).getPayload())).isEqualTo("\"MESSAGE\"");

		Function<Flux<String>, Flux<String>> f3 = catalog.lookup("myFunctionLike");
		assertThat(f3.apply(Flux.just("foo")).blockFirst()).isEqualTo("FOO");
	}
 
Example 2
Source File: TaskConfiguration.java    From spring-cloud-function with Apache License 2.0 5 votes vote down vote up
@Bean
public CommandLineRunner commandLineRunner(FunctionCatalog registry) {
	final Supplier<Publisher<Object>> supplier = registry.lookup(Supplier.class, this.properties.getSupplier());
	final Function<Publisher<Object>, Publisher<Object>> function = registry.lookup(Function.class,
			this.properties.getFunction());
	final Consumer<Publisher<Object>> consumer = consumer(registry);
	CommandLineRunner runner = new CommandLineRunner() {

		@Override
		public void run(String... args) throws Exception {
			consumer.accept(function.apply(supplier.get()));
		}
	};
	return runner;
}
 
Example 3
Source File: BeanFactoryAwareFunctionRegistryMultiInOutTests.java    From spring-cloud-function with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unused")
	@Test
	@Disabled
	public void testMultiInputBiFunction() {
		FunctionCatalog catalog = this.configureCatalog();
		BiFunction<Flux<String>, Flux<Integer>, Flux<String>> multiInputFunction =
									catalog.lookup(BiFunction.class, "multiInputSingleOutputViaBiFunction");
		Flux<String> stringStream = Flux.just("one", "two", "three");
		Flux<Integer> intStream = Flux.just(1, 2, 3);

//		List<String> result = multiInputFunction.apply(Tuples.of(stringStream, intStream)).collectList().block();
//		System.out.println(result);
	}
 
Example 4
Source File: BeanFactoryAwareFunctionRegistryTests.java    From spring-cloud-function with Apache License 2.0 5 votes vote down vote up
@Test
public void testMultiInputWithComposition() {
	FunctionCatalog catalog = this.configureCatalog();
	Function<Tuple2<Flux<String>, Flux<String>>, Flux<String>> multiInputFunction =
								catalog.lookup("multiInputSingleOutputViaReactiveTuple|uppercase");
	Flux<String> stringStream = Flux.just("one", "two", "three");
	Flux<String> intStream = Flux.just("1", "2", "3");

	List<String> result = multiInputFunction.apply(Tuples.of(stringStream, intStream)).collectList().block();
	assertThat(result.size()).isEqualTo(3);
	assertThat(result.get(0)).isEqualTo("ONE-1");
	assertThat(result.get(1)).isEqualTo("TWO-2");
	assertThat(result.get(2)).isEqualTo("THREE-3");
}
 
Example 5
Source File: RoutingFunctionTests.java    From spring-cloud-function with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings({ "unchecked", "rawtypes" })
@Test
public void testInvocationWithMessageAndHeader() {
	FunctionCatalog functionCatalog = this.configureCatalog();
	Function function = functionCatalog.lookup(RoutingFunction.FUNCTION_NAME);
	assertThat(function).isNotNull();
	Message<String> message = MessageBuilder.withPayload("hello")
			.setHeader(FunctionProperties.PREFIX + ".definition", "reverse").build();
	assertThat(function.apply(message)).isEqualTo("olleh");
}
 
Example 6
Source File: RoutingFunctionTests.java    From spring-cloud-function with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings({ "unchecked", "rawtypes" })
@Test
public void testRoutingSimpleInputWithReactiveFunctionWithMessageHeader() {
	FunctionCatalog functionCatalog = this.configureCatalog();
	Function function = functionCatalog.lookup(RoutingFunction.FUNCTION_NAME);
	assertThat(function).isNotNull();
	Message<String> message = MessageBuilder.withPayload("hello")
			.setHeader(FunctionProperties.PREFIX + ".definition", "echoFlux").build();
	assertThat(((Flux) function.apply(message)).blockFirst()).isEqualTo("hello");
}
 
Example 7
Source File: BeanFactoryAwareFunctionRegistryTests.java    From spring-cloud-function with Apache License 2.0 5 votes vote down vote up
/**
 * The following two tests test the fallback mechanism when an accept header has several values.
 * The function produces Integer, which cannot be serialized by the default converter supporting text/plain
 * (StringMessageConverter) but can by the one supporting application/json, which comes second.
 */
@Test
public void testMultipleOrderedAcceptValues() throws Exception {
	FunctionCatalog catalog = this.configureCatalog(MultipleOrderedAcceptValuesConfiguration.class);
	Function<String, Message<byte[]>> function = catalog.lookup("beanFactoryAwareFunctionRegistryTests.MultipleOrderedAcceptValuesConfiguration", "text/plain,application/json");
	assertThat(function).isNotNull();
	Message<byte[]> result = function.apply("hello");
	assertThat(result.getPayload()).isEqualTo("5".getBytes("UTF-8"));
}
 
Example 8
Source File: BeanFactoryAwareFunctionRegistryTests.java    From spring-cloud-function with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unused")
@Test
public void testReactiveFunctionWithImperativeInputAndOutputFail() {
	FunctionCatalog catalog = this.configureCatalog();
	Function<String, String> reverse = catalog.lookup("reverseFlux");
	Assertions.assertThrows(ClassCastException.class, () -> {
		String result = reverse.apply("reverseFlux");
	});
}
 
Example 9
Source File: BeanFactoryAwareFunctionRegistryTests.java    From spring-cloud-function with Apache License 2.0 5 votes vote down vote up
@Test
public void testCompositionSupplierAndFunction() {
	FunctionCatalog catalog = this.configureCatalog();

	Supplier<Flux<String>> numberSupplierFlux = catalog.lookup("numberword|uppercaseFlux");
	String result = numberSupplierFlux.get().blockFirst();
	assertThat(result).isEqualTo("ONE");
}
 
Example 10
Source File: BeanFactoryAwareFunctionRegistryTests.java    From spring-cloud-function with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings({ "unchecked", "rawtypes" })
@Test
public void testValueWrappedInMessageIfNecessary() {
	FunctionCatalog catalog = this.configureCatalog(PojoToMessageFunctionCompositionConfiguration.class);
	Function f = catalog.lookup("uppercase|echo");
	assertThat(f.apply("hello")).isEqualTo("HELLO");
	f = catalog.lookup("toJson|uppercasePerson");
	assertThat(f.apply("Bubbles")).isEqualTo("BUBBLES");
}
 
Example 11
Source File: BeanFactoryAwareFunctionRegistryMultiInOutTests.java    From spring-cloud-function with Apache License 2.0 5 votes vote down vote up
@Test
public void testMultiOutputAsTuplePojoInInputTypeMatch() {
	FunctionCatalog catalog = this.configureCatalog();
	Function<Flux<Person>, Tuple3<Flux<Person>, Flux<String>, Flux<Integer>>> multiOutputFunction =
								catalog.lookup("multiOutputAsTuplePojoIn");
	Flux<Person> personStream = Flux.just(new Person("Uncle Sam", 1), new Person("Oncle Pierre", 2));

	Tuple3<Flux<Person>, Flux<String>, Flux<Integer>> result = multiOutputFunction.apply(personStream);
	result.getT1().subscribe(v -> System.out.println("=> 1: " + v));
	result.getT2().subscribe(v -> System.out.println("=> 2: " + v));
	result.getT3().subscribe(v -> System.out.println("=> 3: " + v));
}
 
Example 12
Source File: FunctionWebUtils.java    From spring-cloud-function with Apache License 2.0 5 votes vote down vote up
private static Object doFindFunction(HttpMethod method, FunctionCatalog functionCatalog,
										Map<String, Object> attributes, String path) {
	path = path.startsWith("/") ? path.substring(1) : path;
	if (method.equals(HttpMethod.GET)) {
		Supplier<Publisher<?>> supplier = functionCatalog.lookup(Supplier.class, path);
		if (supplier != null) {
			attributes.put(WebRequestConstants.SUPPLIER, supplier);
			return supplier;
		}
	}

	StringBuilder builder = new StringBuilder();
	String name = path;
	String value = null;
	for (String element : path.split("/")) {
		if (builder.length() > 0) {
			builder.append("/");
		}
		builder.append(element);
		name = builder.toString();
		value = path.length() > name.length() ? path.substring(name.length() + 1)
				: null;
		Function<Object, Object> function = functionCatalog.lookup(Function.class,
				name);
		if (function != null) {
			attributes.put(WebRequestConstants.FUNCTION, function);
			if (value != null) {
				attributes.put(WebRequestConstants.ARGUMENT, value);
			}
			return function;
		}
	}
	return null;
}
 
Example 13
Source File: RoutingFunctionTests.java    From spring-cloud-function with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings({ "unchecked", "rawtypes" })
@Test
public void testInvocationWithMessageAndRoutingExpression() {
	System.setProperty(FunctionProperties.PREFIX + ".routing-expression", "headers.function_name");
	FunctionCatalog functionCatalog = this.configureCatalog();
	Function function = functionCatalog.lookup(RoutingFunction.FUNCTION_NAME);
	assertThat(function).isNotNull();
	Message<String> message = MessageBuilder.withPayload("hello").setHeader("function_name", "reverse").build();
	assertThat(function.apply(message)).isEqualTo("olleh");
}
 
Example 14
Source File: BeanFactoryAwareFunctionRegistryTests.java    From spring-cloud-function with Apache License 2.0 5 votes vote down vote up
@Test
public void testMessageToPojoConversion() {
	FunctionCatalog catalog = this.configureCatalog();
	Function<Message<String>, Person> uppercasePerson = catalog.lookup("uppercasePerson");
	Person person =  uppercasePerson.apply(MessageBuilder.withPayload("{\"name\":\"bill\",\"id\":2}").build());
	assertThat(person.getName()).isEqualTo("BILL");
}
 
Example 15
Source File: BeanFactoryAwareFunctionRegistryTests.java    From spring-cloud-function with Apache License 2.0 5 votes vote down vote up
@Test
public void testConsumerFunction() { // function that returns Void, effectively a Consumer
	FunctionCatalog catalog = this.configureCatalog();

	Function<String, Void> consumerFunction = catalog.lookup("consumerFunction");
	assertThat(consumerFunction.apply("hello")).isNull();

	Function<Message<byte[]>, Void> consumerFunctionAsMessageA = catalog.lookup("consumerFunction");
	assertThat(consumerFunctionAsMessageA.apply(new GenericMessage<byte[]>("\"hello\"".getBytes()))).isNull();

	Function<Message<byte[]>, Void> consumerFunctionAsMessageB = catalog.lookup("consumerFunction", "application/json");
	assertThat(consumerFunctionAsMessageB.apply(new GenericMessage<byte[]>("\"hello\"".getBytes()))).isNull();
}
 
Example 16
Source File: FunctionDeployerTests.java    From spring-cloud-function with Apache License 2.0 5 votes vote down vote up
@Test
public void testWithMainAndStartClassAndSpringConfigurationAndTypeConversion() throws Exception {
	String[] args = new String[] {
			"--spring.cloud.function.location=target/it/bootapp/target/bootapp-1.0.0.RELEASE-exec.jar",
			"--spring.cloud.function.function-name=uppercasePerson" };

	ApplicationContext context = SpringApplication.run(DeployerApplication.class, args);
	FunctionCatalog catalog = context.getBean(FunctionCatalog.class);
	Function<Message<byte[]>, Message<byte[]>> function = catalog.lookup("uppercasePerson", "application/json");

	Message<byte[]> result = function.apply(
			MessageBuilder.withPayload("{\"name\":\"bob\",\"id\":1}".getBytes(StandardCharsets.UTF_8)).build());
	assertThat(new String(result.getPayload(), StandardCharsets.UTF_8)).isEqualTo("{\"name\":\"BOB\",\"id\":1}");
}
 
Example 17
Source File: RoutingFunctionTests.java    From spring-cloud-function with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings({ "unchecked", "rawtypes" })
@Test
public void testRoutingReactiveInputWithReactiveFunctionAndExpressionMessageHeader() {
	FunctionCatalog functionCatalog = this.configureCatalog();
	Function function = functionCatalog.lookup(RoutingFunction.FUNCTION_NAME);
	assertThat(function).isNotNull();
	Message<String> message = MessageBuilder.withPayload("hello")
			.setHeader(FunctionProperties.PREFIX + ".routing-expression", "'echoFlux'").build();
	Flux resultFlux = (Flux) function.apply(Flux.just(message));
	Assertions.assertThrows(Exception.class, resultFlux::subscribe);
}
 
Example 18
Source File: BeanFactoryAwareFunctionRegistryTests.java    From spring-cloud-function with Apache License 2.0 5 votes vote down vote up
@Test
public void testMultipleOrderedAcceptValuesMessageOutput() throws Exception {
	FunctionCatalog catalog = this.configureCatalog(MultipleOrderedAcceptValuesAsMessageOutputConfiguration.class);
	Function<String, Message<byte[]>> function = catalog.lookup(
			"beanFactoryAwareFunctionRegistryTests.MultipleOrderedAcceptValuesAsMessageOutputConfiguration",
			"text/plain,application/json");
	assertThat(function).isNotNull();
	Message<byte[]> result = function.apply("hello");
	assertThat(result.getPayload()).isEqualTo("5".getBytes("UTF-8"));
}
 
Example 19
Source File: BeanFactoryAwareFunctionRegistryMultiInOutTests.java    From spring-cloud-function with Apache License 2.0 5 votes vote down vote up
@Test
public void testMultiInputWithComposition() {
	FunctionCatalog catalog = this.configureCatalog();
	Function<Tuple2<Flux<String>, Flux<String>>, Flux<String>> multiInputFunction =
								catalog.lookup("multiInputSingleOutputViaReactiveTuple|uppercase");
	Flux<String> stringStream = Flux.just("one", "two", "three");
	Flux<String> intStream = Flux.just("1", "2", "3");

	List<String> result = multiInputFunction.apply(Tuples.of(stringStream, intStream)).collectList().block();
	System.out.println(result);
}
 
Example 20
Source File: FunctionDeployerTests.java    From spring-cloud-function with Apache License 2.0 5 votes vote down vote up
@Test
public void testWithMainAndStartClassAndSpringConfiguration() throws Exception {
	String[] args = new String[] {
			"--spring.cloud.function.location=target/it/bootapp/target/bootapp-1.0.0.RELEASE-exec.jar",
			"--spring.cloud.function.definition=uppercase" };
	ApplicationContext context = SpringApplication.run(DeployerApplication.class, args);
	FunctionCatalog catalog = context.getBean(FunctionCatalog.class);
	Function<Message<byte[]>, Message<byte[]>> function = catalog.lookup("uppercase", "application/json");

	Message<byte[]> result = function
			.apply(MessageBuilder.withPayload("\"bob\"".getBytes(StandardCharsets.UTF_8)).build());
	assertThat(new String(result.getPayload(), StandardCharsets.UTF_8)).isEqualTo("\"BOB\"");
}