Add Supplier Exporter sample

This commit is contained in:
Oleg Zhurakousky
2020-05-07 10:28:25 +02:00
parent 07e4de71d2
commit 5aeb77a073
12 changed files with 460 additions and 0 deletions

View File

@@ -0,0 +1,55 @@
package com.example.demo;
import com.example.test.TestServer;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.cloud.function.context.test.FunctionalSpringBootTest;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.util.SocketUtils;
import org.springframework.web.reactive.function.client.WebClient;
import static org.assertj.core.api.Assertions.assertThat;
@FunctionalSpringBootTest({"spring.cloud.function.web.export.source.url=http://localhost:${export.port}/home",
"spring.cloud.function.web.export.sink.url=http://localhost:${export.port}/echo",
"logging.level.reactor=OFF",
"logging.level.io.netty=OFF"})
public class DemoApplicationTests {
static ConfigurableApplicationContext context;
@Value("${export.port}")
private int port;
@Autowired
private WebClient.Builder builder;
@Test
public void contextLoads() throws Exception {
WebClient client = builder.baseUrl("http://localhost:" + port).build();
client.post().uri("/add").bodyValue("{\"name\":\"Fred\"}").exchange().block();
Thread.sleep(1000L);
String response = client.get().uri("/take").exchange().block().bodyToMono(String.class).block();
assertThat(response).isEqualTo("{\"name\":\"hi Fred!\"}");
}
@AfterAll
static void after() {
if (context != null) {
context.close();
}
}
@BeforeAll
static void before() {
int port = SocketUtils.findAvailableTcpPort();
System.setProperty("export.port", "" + port);
context = SpringApplication.run(TestServer.class, "--server.port="+port, "--spring.cloud.function.web.export.enabled=false", "--spring.main.web-application-type=reactive");
}
}

View File

@@ -0,0 +1,85 @@
package com.example.test;
import java.util.Arrays;
import java.util.LinkedHashSet;
import java.util.Set;
import java.util.function.Function;
import java.util.function.Supplier;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.function.json.JsonMapper;
import org.springframework.context.annotation.Bean;
import reactor.core.publisher.Mono;
import reactor.core.publisher.MonoProcessor;
@SpringBootApplication(proxyBeanMethods = false)
public class TestServer {
private MonoProcessor<String> output = MonoProcessor.<String>create();
private String response = "";
public static void main(String[] args) {
Set<String> list = new LinkedHashSet<>(Arrays.asList(args));
list.addAll(Arrays.asList("--server.port=8000", "--spring.cloud.function.web.export.enabled=false", "--spring.main.web-application-type=reactive"));
SpringApplication.run(TestServer.class, list.toArray(new String[0]));
}
@Bean
public Supplier<Mono<String>> home() {
return () -> output;
}
// @Bean
// public Function<String, String> echo(JsonMapper mapper) {
// return input -> {
// response = input;
// return "Echo: " + response;
// };
// }
@Bean
public Function<Foo, String> echo(JsonMapper mapper) {
return input -> {
System.out.println("===> POJO " + input);
response = new String(mapper.toJson(input));
return "Echo: " + response;
};
}
@Bean
public Function<String, String> add() {
return input -> {
System.err.println("Add: " + input);
output.onNext(input);
output = MonoProcessor.<String>create();
return "Added: " + input;
};
}
@Bean
public Supplier<String> take() {
return () -> response;
}
}
class Foo {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Foo(String name) {
this.name = name;
}
Foo() {}
}