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

The following examples show how to use org.springframework.cloud.function.context.FunctionCatalog. 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: BeanFactoryAwareFunctionRegistryMultiInOutTests.java    From spring-cloud-function with Apache License 2.0 6 votes vote down vote up
@Test
public void testMultiOutputAsTuplePojoInInputByteArrayInputTypePojoMessage() {
	FunctionCatalog catalog = this.configureCatalog();
	Function<Flux<Message<byte[]>>, Tuple3<Flux<Person>, Flux<String>, Flux<Integer>>> multiOutputFunction =
								catalog.lookup("multiOutputAsTupleMessageIn");

	Message<byte[]> uncleSam = MessageBuilder.withPayload("{\"name\":\"Uncle Sam\",\"id\":1}".getBytes(StandardCharsets.UTF_8))
			.setHeader(MessageHeaders.CONTENT_TYPE, MimeTypeUtils.APPLICATION_JSON)
			.build();
	Message<byte[]> unclePierre = MessageBuilder.withPayload("{\"name\":\"Oncle Pierre\",\"id\":2}".getBytes(StandardCharsets.UTF_8))
			.setHeader(MessageHeaders.CONTENT_TYPE, MimeTypeUtils.APPLICATION_JSON)
			.build();
	Flux<Message<byte[]>> personStream = Flux.just(uncleSam, unclePierre);

	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 #2
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 #3
Source File: BeanFactoryAwarePojoFunctionRegistryTests.java    From spring-cloud-function with Apache License 2.0 6 votes vote down vote up
@Test
	public void testWithPojoFunctionImplementingFunction() {
		FunctionCatalog catalog = this.configureCatalog();

//		MyFunction f1 = catalog.lookup("myFunction");
//		assertThat(f1.uppercase("foo")).isEqualTo("FOO");

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

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

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

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

		Function<Flux<String>, Flux<String>> f3 = catalog.lookup("myFunction");
		assertThat(f3.apply(Flux.just("foo")).blockFirst()).isEqualTo("FOO");
	}
 
Example #4
Source File: ContextFunctionCatalogInitializerTests.java    From spring-cloud-function with Apache License 2.0 6 votes vote down vote up
private void create(ApplicationContextInitializer<GenericApplicationContext>[] types,
		String... props) {
	this.context = new GenericApplicationContext();
	Map<String, Object> map = new HashMap<>();
	for (String prop : props) {
		String[] array = StringUtils.delimitedListToStringArray(prop, "=");
		String key = array[0];
		String value = array.length > 1 ? array[1] : "";
		map.put(key, value);
	}
	if (!map.isEmpty()) {
		this.context.getEnvironment().getPropertySources()
				.addFirst(new MapPropertySource("testProperties", map));
	}
	for (ApplicationContextInitializer<GenericApplicationContext> type : types) {
		type.initialize(this.context);
	}
	new ContextFunctionCatalogInitializer.ContextFunctionCatalogBeanRegistrar(
			this.context).postProcessBeanDefinitionRegistry(this.context);
	this.context.refresh();
	this.catalog = this.context.getBean(FunctionCatalog.class);
	this.inspector = this.context.getBean(FunctionInspector.class);
}
 
Example #5
Source File: BeanFactoryAwareFunctionRegistryTests.java    From spring-cloud-function with Apache License 2.0 6 votes vote down vote up
@Test
public void testComposition() {
	FunctionCatalog catalog = this.configureCatalog();
	Function<Flux<String>, Flux<String>> fluxFunction = catalog.lookup("uppercase|reverseFlux");
	List<String> result = fluxFunction.apply(Flux.just("hello", "bye")).collectList().block();
	assertThat(result.get(0)).isEqualTo("OLLEH");
	assertThat(result.get(1)).isEqualTo("EYB");

	fluxFunction = catalog.lookup("uppercase|reverse|reverseFlux");
	result = fluxFunction.apply(Flux.just("hello", "bye")).collectList().block();
	assertThat(result.get(0)).isEqualTo("HELLO");
	assertThat(result.get(1)).isEqualTo("BYE");

	fluxFunction = catalog.lookup("uppercase|reverseFlux|reverse");
	result = fluxFunction.apply(Flux.just("hello", "bye")).collectList().block();
	assertThat(result.get(0)).isEqualTo("HELLO");
	assertThat(result.get(1)).isEqualTo("BYE");

	fluxFunction = catalog.lookup("uppercase|reverse");
	result = fluxFunction.apply(Flux.just("hello", "bye")).collectList().block();
	assertThat(result.get(0)).isEqualTo("OLLEH");
	assertThat(result.get(1)).isEqualTo("EYB");

	Function<String, String> function = catalog.lookup("uppercase|reverse");
	assertThat(function.apply("foo")).isEqualTo("OOF");
}
 
Example #6
Source File: ContextFunctionCatalogAutoConfigurationTests.java    From spring-cloud-function with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings({ "rawtypes", "unchecked" })
@Test
public void monoToMonoNonVoidFunction() {
	create(MonoToMonoNonVoidConfiguration.class);
	assertThat(this.context.getBean("function")).isInstanceOf(Function.class);
	assertThat(this.inspector
			.getInputType(this.catalog.lookup(Function.class, "function")))
					.isAssignableFrom(String.class);
	assertThat(this.inspector
			.getOutputType(this.catalog.lookup(Function.class, "function")))
					.isAssignableFrom(String.class);

	Function function = this.context.getBean(FunctionCatalog.class).lookup("function");
	Object result = ((Mono) function.apply(Mono.just("flux"))).block();
	System.out.println(result);
}
 
Example #7
Source File: BeanFactoryAwareFunctionRegistryTests.java    From spring-cloud-function with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
@Test
public void testDefaultLookup() throws Exception {
	FunctionCatalog catalog = this.configureCatalog();
	Object function = catalog.lookup("");
	assertThat(function).isNull();
	//==
	System.setProperty("spring.cloud.function.definition", "uppercase");
	catalog = this.configureCatalog();
	function = catalog.lookup("");
	assertThat(function).isNotNull();
	Field field = ReflectionUtils.findField(FunctionInvocationWrapper.class, "composed");
	field.setAccessible(true);
	assertThat(((boolean) field.get(function))).isFalse();
	//==
	System.setProperty("spring.cloud.function.definition", "uppercase|uppercaseFlux");
	catalog = this.configureCatalog();
	function = catalog.lookup("", "application/json");
	Function<Flux<String>, Flux<Message<String>>> typedFunction = (Function<Flux<String>, Flux<Message<String>>>) function;
	Object blockFirst = typedFunction.apply(Flux.just("hello")).blockFirst();
	System.out.println(blockFirst);
	assertThat(function).isNotNull();
	field = ReflectionUtils.findField(FunctionInvocationWrapper.class, "composed");
	field.setAccessible(true);
	assertThat(((boolean) field.get(function))).isTrue();
}
 
Example #8
Source File: BeanFactoryAwareFunctionRegistryMultiInOutTests.java    From spring-cloud-function with Apache License 2.0 6 votes vote down vote up
@Test
public void testMultiInputWithPojoConversion() {
	FunctionCatalog catalog = this.configureCatalog();
	Function<Tuple2<Flux<CartEvent>, Flux<CheckoutEvent>>, Flux<OrderEvent>> multiInputFunction =
								catalog.lookup("thomas", "application/json");
	CartEvent carEvent = new CartEvent();
	carEvent.setCarEvent("carEvent");
	Flux<CartEvent> carEventStream = Flux.just(carEvent);

	CheckoutEvent checkoutEvent = new CheckoutEvent();
	checkoutEvent.setCheckoutEvent("checkoutEvent");
	Flux<CheckoutEvent> checkoutEventStream = Flux.just(checkoutEvent);

	Tuple2<Flux<CartEvent>, Flux<CheckoutEvent>> streams = Tuples.of(carEventStream, checkoutEventStream);

	List<OrderEvent> result = multiInputFunction.apply(streams).collectList().block();
	System.out.println(result);
}
 
Example #9
Source File: BeanFactoryAwareFunctionRegistryTests.java    From spring-cloud-function with Apache License 2.0 6 votes vote down vote up
@Test
public void testImperativeFunction() {
	FunctionCatalog catalog = this.configureCatalog();

	Function<String, String> asIs = catalog.lookup("uppercase");
	assertThat(asIs.apply("uppercase")).isEqualTo("UPPERCASE");

	Function<Flux<String>, Flux<String>> asFlux = catalog.lookup("uppercase");
	List<String> result = asFlux.apply(Flux.just("uppercaseFlux", "uppercaseFlux2")).collectList().block();
	assertThat(result.get(0)).isEqualTo("UPPERCASEFLUX");
	assertThat(result.get(1)).isEqualTo("UPPERCASEFLUX2");

	Function<Flux<Message<byte[]>>, Flux<Message<byte[]>>> messageFlux = catalog.lookup("uppercase", "application/json");
	Message<byte[]> message1 = MessageBuilder.withPayload("\"uppercaseFlux\"".getBytes()).setHeader(MessageHeaders.CONTENT_TYPE, "application/json").build();
	Message<byte[]> message2 = MessageBuilder.withPayload("\"uppercaseFlux2\"".getBytes()).setHeader(MessageHeaders.CONTENT_TYPE, "application/json").build();
	List<Message<byte[]>> messageResult = messageFlux.apply(Flux.just(message1, message2)).collectList().block();
	assertThat(messageResult.get(0).getPayload()).isEqualTo("\"UPPERCASEFLUX\"".getBytes(StandardCharsets.UTF_8));
	assertThat(messageResult.get(1).getPayload()).isEqualTo("\"UPPERCASEFLUX2\"".getBytes(StandardCharsets.UTF_8));
}
 
Example #10
Source File: FunctionInvoker.java    From spring-cloud-function with Apache License 2.0 6 votes vote down vote up
private void start() {
	ConfigurableApplicationContext context = SpringApplication.run(FunctionClassUtils.getStartClass());
	Environment environment = context.getEnvironment();
	String functionName = environment.getProperty("spring.cloud.function.definition");
	FunctionCatalog functionCatalog = context.getBean(FunctionCatalog.class);
	this.mapper = context.getBean(ObjectMapper.class);
	this.configureObjectMapper();

	if (logger.isInfoEnabled()) {
		logger.info("Locating function: '" + functionName + "'");
	}

	this.function = functionCatalog.lookup(functionName, "application/json");
	Assert.notNull(this.function, "Failed to lookup function " + functionName);

	if (!StringUtils.hasText(functionName)) {
		FunctionInspector inspector = context.getBean(FunctionInspector.class);
		functionName = inspector.getRegistration(this.function).getNames().toString();
	}

	if (logger.isInfoEnabled()) {
		logger.info("Located function: '" + functionName + "'");
	}
}
 
Example #11
Source File: FunctionExporterInitializer.java    From spring-cloud-function with Apache License 2.0 6 votes vote down vote up
private void registerExport(GenericApplicationContext context) {
	context.registerBean(ExporterProperties.class, () -> new ExporterProperties());
	context.registerBean(FunctionExporterAutoConfiguration.class,
			() -> new FunctionExporterAutoConfiguration(context.getBean(ExporterProperties.class)));
	if (context.getBeanFactory().getBeanNamesForType(DestinationResolver.class, false, false).length == 0) {
		context.registerBean(DestinationResolver.class,
				() -> context.getBean(FunctionExporterAutoConfiguration.class).simpleDestinationResolver());
	}
	if (context.getBeanFactory().getBeanNamesForType(RequestBuilder.class, false, false).length == 0) {
		context.registerBean(RequestBuilder.class, () -> context.getBean(FunctionExporterAutoConfiguration.class)
				.simpleRequestBuilder(context.getEnvironment()));
	}
	if (context.getEnvironment().getProperty("spring.cloud.function.web.export.source.url") != null) {
		context.registerBean("origin", FunctionRegistration.class, () -> context
				.getBean(FunctionExporterAutoConfiguration.class).origin(context.getBean(WebClient.Builder.class)));
	}
	if (context.getEnvironment().getProperty("spring.cloud.function.web.export.sink.url") != null) {
		context.registerBean(SupplierExporter.class,
				() -> context.getBean(FunctionExporterAutoConfiguration.class).sourceForwarder(
						context.getBean(RequestBuilder.class), context.getBean(DestinationResolver.class),
						context.getBean(FunctionCatalog.class), context.getBean(WebClient.Builder.class)));
	}
}
 
Example #12
Source File: FunctionDeployerTests.java    From spring-cloud-function with Apache License 2.0 6 votes vote down vote up
@Test
public void testWithSimplestJar() throws Exception {
	String[] args = new String[] {
			"--spring.cloud.function.location=target/it/simplestjar/target/simplestjar-1.0.0.RELEASE.jar",
			"--spring.cloud.function.function-class=function.example.UpperCaseFunction" };

	ApplicationContext context = SpringApplication.run(DeployerApplication.class, args);
	FunctionCatalog catalog = context.getBean(FunctionCatalog.class);
	Function<String, String> function = catalog.lookup("upperCaseFunction");

	assertThat(function.apply("bob")).isEqualTo("BOB");
	assertThat(function.apply("stacy")).isEqualTo("STACY");

	Function<Flux<String>, Flux<String>> functionAsFlux = catalog.lookup("upperCaseFunction");

	List<String> results = functionAsFlux.apply(Flux.just("bob", "stacy")).collectList().block();
	assertThat(results.get(0)).isEqualTo("BOB");
	assertThat(results.get(1)).isEqualTo("STACY");
}
 
Example #13
Source File: FunctionDeployerTests.java    From spring-cloud-function with Apache License 2.0 6 votes vote down vote up
@Test
public void testWithSimplestJarExploaded() throws Exception {
	String[] args = new String[] {
			"--spring.cloud.function.location=target/it/simplestjar/target/classes",
			"--spring.cloud.function.function-class=function.example.UpperCaseFunction" };

	ApplicationContext context = SpringApplication.run(DeployerApplication.class, args);
	FunctionCatalog catalog = context.getBean(FunctionCatalog.class);
	Function<String, String> function = catalog.lookup("upperCaseFunction");

	assertThat(function.apply("bob")).isEqualTo("BOB");
	assertThat(function.apply("stacy")).isEqualTo("STACY");

	Function<Flux<String>, Flux<String>> functionAsFlux = catalog.lookup("upperCaseFunction");

	List<String> results = functionAsFlux.apply(Flux.just("bob", "stacy")).collectList().block();
	assertThat(results.get(0)).isEqualTo("BOB");
	assertThat(results.get(1)).isEqualTo("STACY");
}
 
Example #14
Source File: BeanFactoryAwareFunctionRegistryTests.java    From spring-cloud-function with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
@Test
public void testSerializationWithCompatibleWildcardSubtypeAcceptHeader() {
	FunctionCatalog catalog = this.configureCatalog(NegotiatingMessageConverterConfiguration.class);
	FunctionInvocationWrapper function = catalog.lookup("echo", "text/*");

	Message<Tuple2<String, String>> tupleResult = (Message<Tuple2<String, String>>) function.apply(MessageBuilder
			.withPayload(Tuples.of("bonjour", "monde"))
			.setHeader(MessageHeaders.CONTENT_TYPE, MimeType.valueOf("text/csv"))
			.build()
	);

	assertThat(tupleResult.getHeaders().get(MessageHeaders.CONTENT_TYPE)).isEqualTo(MimeType.valueOf("text/csv"));
	assertThat(tupleResult.getHeaders().get("accept")).isNull();

	Message<Date> dateResult = (Message<Date>) function.apply(MessageBuilder
			.withPayload(123)
			.setHeader(MessageHeaders.CONTENT_TYPE, MimeType.valueOf("text/integer"))
			.build()
	);

	assertThat(dateResult.getHeaders().get(MessageHeaders.CONTENT_TYPE)).isEqualTo(MimeType.valueOf("text/integer"));
	assertThat(dateResult.getHeaders().get("accept")).isNull();
}
 
Example #15
Source File: FunctionDeployerTests.java    From spring-cloud-function with Apache License 2.0 6 votes vote down vote up
@Test
public void testNoMainAndNoStartClassAndNoSpringConfigurationDiscoverClassFromManifest() throws Exception {
	String[] args = new String[] {
			"--spring.cloud.function.location=target/it/bootjarnostart/target/bootjarnostart-1.0.0.RELEASE-exec.jar" };

	ApplicationContext context = SpringApplication.run(DeployerApplication.class, args);
	FunctionCatalog catalog = context.getBean(FunctionCatalog.class);
	Function<String, String> function = catalog.lookup("upperCaseFunction");

	assertThat(function.apply("bob")).isEqualTo("BOB");
	assertThat(function.apply("stacy")).isEqualTo("STACY");

	Function<Flux<String>, Flux<String>> functionAsFlux = catalog.lookup("upperCaseFunction");

	List<String> results = functionAsFlux.apply(Flux.just("bob", "stacy")).collectList().block();
	assertThat(results.get(0)).isEqualTo("BOB");
	assertThat(results.get(1)).isEqualTo("STACY");
}
 
Example #16
Source File: BeanFactoryAwareFunctionRegistryMultiInOutTests.java    From spring-cloud-function with Apache License 2.0 6 votes vote down vote up
@Test
public void testMultiOutputAsTuplePojoInInputByteArray() {
	FunctionCatalog catalog = this.configureCatalog();
	Function<Flux<Message<byte[]>>, Tuple3<Flux<Person>, Flux<String>, Flux<Integer>>> multiOutputFunction =
								catalog.lookup("multiOutputAsTuplePojoIn");

	Message<byte[]> uncleSam = MessageBuilder.withPayload("{\"name\":\"Uncle Sam\",\"id\":1}".getBytes(StandardCharsets.UTF_8))
			.setHeader(MessageHeaders.CONTENT_TYPE, MimeTypeUtils.APPLICATION_JSON)
			.build();
	Message<byte[]> unclePierre = MessageBuilder.withPayload("{\"name\":\"Oncle Pierre\",\"id\":2}".getBytes(StandardCharsets.UTF_8))
			.setHeader(MessageHeaders.CONTENT_TYPE, MimeTypeUtils.APPLICATION_JSON)
			.build();
	Flux<Message<byte[]>> personStream = Flux.just(uncleSam, unclePierre);

	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 #17
Source File: FunctionDeployerTests.java    From spring-cloud-function with Apache License 2.0 6 votes vote down vote up
@Test
public void testBootAppWithMultipleInputOutput() {
	String[] args = new String[] {
			"--spring.cloud.function.location=target/it/bootapp-multi/target/bootapp-multi-1.0.0.RELEASE-exec.jar",
			"--spring.cloud.function.function-name=fn"
	};
	ApplicationContext context = SpringApplication.run(DeployerApplication.class, args);
	FunctionCatalog catalog = context.getBean(FunctionCatalog.class);

	Function<Tuple2<Flux<Message<byte[]>>, Flux<Message<byte[]>>>, Flux<Message<byte[]>>> multiInputFunction = catalog
			.lookup("fn", "application/json");

	Message<byte[]> carEventMessage = MessageBuilder.withPayload("{\"carEvent\":\"CAR IS BUILT\"}".getBytes()).build();
	Message<byte[]> checkoutEventMessage = MessageBuilder.withPayload("{\"checkoutEvent\":\"CAR IS CHECKED OUT\"}".getBytes()).build();
	Flux<Message<byte[]>> carEventStream = Flux.just(carEventMessage);
	Flux<Message<byte[]>> checkoutEventStream = Flux.just(checkoutEventMessage);

	Flux<Message<byte[]>> result = multiInputFunction.apply(Tuples.of(carEventStream, checkoutEventStream));

	byte[] resutBytes = result.blockFirst().getPayload();
	assertThat(resutBytes).isEqualTo("{\"orderEvent\":\"CartEvent: CAR IS BUILT- CheckoutEvent: CAR IS CHECKED OUT\"}".getBytes());
}
 
Example #18
Source File: BeanFactoryAwareFunctionRegistryTests.java    From spring-cloud-function with Apache License 2.0 6 votes vote down vote up
@Test
@Disabled
public void testImperativeVoidInputFunction() {
	FunctionCatalog catalog = this.configureCatalog();

	Function<String, String> anyInputSignature = catalog.lookup("voidInputFunction");
	assertThat(anyInputSignature.apply("uppercase")).isEqualTo("voidInputFunction");
	assertThat(anyInputSignature.apply("blah")).isEqualTo("voidInputFunction");
	assertThat(anyInputSignature.apply(null)).isEqualTo("voidInputFunction");

	Function<Void, String> asVoid = catalog.lookup("voidInputFunction");
	assertThat(asVoid.apply(null)).isEqualTo("voidInputFunction");

	Function<Mono<Void>, Mono<String>> asMonoVoidFlux = catalog.lookup("voidInputFunction");
	String result = asMonoVoidFlux.apply(Mono.empty()).block();
	assertThat(result).isEqualTo("voidInputFunction");

	Function<Flux<Void>, Flux<String>> asFluxVoidFlux = catalog.lookup("voidInputFunction");
	List<String> resultList = asFluxVoidFlux.apply(Flux.empty()).collectList().block();
	assertThat(resultList.get(0)).isEqualTo("voidInputFunction");
}
 
Example #19
Source File: BeanFactoryAwareFunctionRegistryMultiInOutTests.java    From spring-cloud-function with Apache License 2.0 5 votes vote down vote up
@Test
public void testMultiInputWithConversion() {
	FunctionCatalog catalog = this.configureCatalog();
	Function<Tuple2<Flux<Integer>, Flux<String>>, Flux<String>> multiInputFunction =
								catalog.lookup("multiInputSingleOutputViaReactiveTuple");
	Flux<Integer> stringStream = Flux.just(11, 22, 33);
	Flux<String> intStream = Flux.just("1", "2", "2");

	List<String> result = multiInputFunction.apply(Tuples.of(stringStream, intStream)).collectList().block();
	System.out.println(result);
}
 
Example #20
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 #21
Source File: FunctionDeployerTests.java    From spring-cloud-function with Apache License 2.0 5 votes vote down vote up
@Test
public void testWithMainAndStartClassNoSpringConfiguration() throws Exception {
	String[] args = new String[] {
			"--spring.cloud.function.location=target/it/bootjar/target/bootjar-1.0.0.RELEASE-exec.jar",
			"--spring.cloud.function.function-class=function.example.UpperCaseFunction;function.example.ReverseFunction" };

	ApplicationContext context = SpringApplication.run(DeployerApplication.class, args);
	FunctionCatalog catalog = context.getBean(FunctionCatalog.class);
	Function<String, String> function = catalog.lookup("upperCaseFunction");

	assertThat(function.apply("bob")).isEqualTo("BOB");
	assertThat(function.apply("stacy")).isEqualTo("STACY");

	function = catalog.lookup("reverseFunction");

	assertThat(function.apply("bob")).isEqualTo("bob");
	assertThat(function.apply("stacy")).isEqualTo("ycats");

	Function<Flux<String>, Flux<String>> functionAsFlux = catalog.lookup("upperCaseFunction");

	List<String> results = functionAsFlux.apply(Flux.just("bob", "stacy")).collectList().block();
	assertThat(results.get(0)).isEqualTo("BOB");
	assertThat(results.get(1)).isEqualTo("STACY");

	functionAsFlux = catalog.lookup("reverseFunction");

	results = functionAsFlux.apply(Flux.just("bob", "stacy")).collectList().block();

	assertThat(results.get(0)).isEqualTo("bob");
	assertThat(results.get(1)).isEqualTo("ycats");
}
 
Example #22
Source File: BeanFactoryAwareFunctionRegistryMultiInOutTests.java    From spring-cloud-function with Apache License 2.0 5 votes vote down vote up
@Test
public void testMultiOutputAsArray() {
	FunctionCatalog catalog = this.configureCatalog();
	Function<Tuple2<Flux<String>, Flux<Integer>>, Flux<?>[]> repeater =
								catalog.lookup("repeater");
	Flux<String> stringStream = Flux.just("one", "two", "three");
	Flux<Integer> intStream = Flux.just(3, 2, 1);

	Flux<?>[] result = repeater.apply(Tuples.of(stringStream, intStream));
	result[0].subscribe(System.out::println);
	result[1].subscribe(System.out::println);
}
 
Example #23
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 #24
Source File: BeanFactoryAwareFunctionRegistryTests.java    From spring-cloud-function with Apache License 2.0 5 votes vote down vote up
@Test
public void testMultipleValuesInOutputHandling() throws Exception {
	FunctionCatalog catalog = this.configureCatalog(CollectionOutConfiguration.class);
	FunctionInvocationWrapper function = catalog.lookup("parseToList", "application/json");
	assertThat(function).isNotNull();
	Object result = function.apply(MessageBuilder.withPayload("1, 2, 3".getBytes()).setHeader(MessageHeaders.CONTENT_TYPE, "text/plain").build());
	assertThat(result instanceof Message).isTrue();

	function = catalog.lookup("parseToListOfMessages", "application/json");
	assertThat(function).isNotNull();
	result = function.apply(MessageBuilder.withPayload("1, 2, 3".getBytes()).setHeader(MessageHeaders.CONTENT_TYPE, "text/plain").build());
	assertThat(result instanceof Message).isFalse();
}
 
Example #25
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 #26
Source File: BeanFactoryAwareFunctionRegistryMultiInOutTests.java    From spring-cloud-function with Apache License 2.0 5 votes vote down vote up
@Test
public void testMultiInput() {
	FunctionCatalog catalog = this.configureCatalog();
	Function<Tuple2<Flux<String>, Flux<Integer>>, Flux<String>> multiInputFunction =
								catalog.lookup("multiInputSingleOutputViaReactiveTuple");
	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 #27
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 #28
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 #29
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 #30
Source File: BeanFactoryAwareFunctionRegistryTests.java    From spring-cloud-function with Apache License 2.0 5 votes vote down vote up
@Test
public void pojoFunctionAsJson() {
	FunctionCatalog catalog = this.configureCatalog();
	Function<String, Person> uppercasePerson = catalog.lookup("uppercasePerson");

	Person person = uppercasePerson.apply("{\"name\":\"bill\",\"id\":2}");
	assertThat(person.getName()).isEqualTo("BILL");
}