reactive work
This commit is contained in:
parent
443dc05f53
commit
df8b6bea54
|
@ -22,10 +22,6 @@
|
||||||
<groupId>org.springframework.boot</groupId>
|
<groupId>org.springframework.boot</groupId>
|
||||||
<artifactId>spring-boot-starter-data-jpa</artifactId>
|
<artifactId>spring-boot-starter-data-jpa</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
|
||||||
<groupId>org.springframework.boot</groupId>
|
|
||||||
<artifactId>spring-boot-starter-security</artifactId>
|
|
||||||
</dependency>
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.springframework.boot</groupId>
|
<groupId>org.springframework.boot</groupId>
|
||||||
<artifactId>spring-boot-starter-validation</artifactId>
|
<artifactId>spring-boot-starter-validation</artifactId>
|
||||||
|
@ -46,8 +42,8 @@
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>javax.json.bind</groupId>
|
<groupId>javax.json.bind</groupId>
|
||||||
<artifactId>javax.json.bind-api</artifactId>
|
<artifactId>javax.json.bind-api</artifactId>
|
||||||
<version>${jsonb-api.version}</version>
|
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
<!-- Dependencies for Yasson -->
|
<!-- Dependencies for Yasson -->
|
||||||
<!-- <dependency> -->
|
<!-- <dependency> -->
|
||||||
<!-- <groupId>org.eclipse</groupId> -->
|
<!-- <groupId>org.eclipse</groupId> -->
|
||||||
|
@ -60,6 +56,7 @@
|
||||||
<!-- <version>1.1.2</version> -->
|
<!-- <version>1.1.2</version> -->
|
||||||
<!-- </dependency> -->
|
<!-- </dependency> -->
|
||||||
<!-- Dependencies for Johnzon -->
|
<!-- Dependencies for Johnzon -->
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.apache.geronimo.specs</groupId>
|
<groupId>org.apache.geronimo.specs</groupId>
|
||||||
<artifactId>geronimo-json_1.1_spec</artifactId>
|
<artifactId>geronimo-json_1.1_spec</artifactId>
|
||||||
|
@ -68,7 +65,6 @@
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.apache.johnzon</groupId>
|
<groupId>org.apache.johnzon</groupId>
|
||||||
<artifactId>johnzon-jsonb</artifactId>
|
<artifactId>johnzon-jsonb</artifactId>
|
||||||
<version>${johnzon.version}</version>
|
|
||||||
</dependency>
|
</dependency>
|
||||||
<!-- utils -->
|
<!-- utils -->
|
||||||
<dependency>
|
<dependency>
|
||||||
|
@ -98,11 +94,6 @@
|
||||||
<artifactId>spring-boot-starter-test</artifactId>
|
<artifactId>spring-boot-starter-test</artifactId>
|
||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
|
||||||
<groupId>org.springframework.security</groupId>
|
|
||||||
<artifactId>spring-security-test</artifactId>
|
|
||||||
<scope>test</scope>
|
|
||||||
</dependency>
|
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.apache.commons</groupId>
|
<groupId>org.apache.commons</groupId>
|
||||||
|
@ -114,12 +105,10 @@
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.junit.jupiter</groupId>
|
<groupId>org.junit.jupiter</groupId>
|
||||||
<artifactId>junit-jupiter-api</artifactId>
|
<artifactId>junit-jupiter-api</artifactId>
|
||||||
<version>${junit.jupiter.version}</version>
|
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.junit.jupiter</groupId>
|
<groupId>org.junit.jupiter</groupId>
|
||||||
<artifactId>junit-jupiter-engine</artifactId>
|
<artifactId>junit-jupiter-engine</artifactId>
|
||||||
<version>${junit.jupiter.version}</version>
|
|
||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
|
@ -135,6 +124,15 @@
|
||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.projectlombok</groupId>
|
||||||
|
<artifactId>lombok</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.apache.commons</groupId>
|
||||||
|
<artifactId>commons-lang3</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
|
|
|
@ -1,15 +0,0 @@
|
||||||
package com.baeldung;
|
|
||||||
|
|
||||||
import org.springframework.boot.SpringApplication;
|
|
||||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
|
||||||
import org.springframework.context.annotation.ComponentScan;
|
|
||||||
|
|
||||||
@SpringBootApplication
|
|
||||||
@ComponentScan(basePackages = { "com.baeldung.web" })
|
|
||||||
public class Spring5Application {
|
|
||||||
|
|
||||||
public static void main(String[] args) {
|
|
||||||
SpringApplication.run(Spring5Application.class, args);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
|
@ -1,34 +0,0 @@
|
||||||
package com.baeldung;
|
|
||||||
|
|
||||||
import org.springframework.context.ApplicationContext;
|
|
||||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
|
||||||
import org.springframework.context.annotation.Bean;
|
|
||||||
import org.springframework.context.annotation.ComponentScan;
|
|
||||||
import org.springframework.http.server.reactive.HttpHandler;
|
|
||||||
import org.springframework.http.server.reactive.ReactorHttpHandlerAdapter;
|
|
||||||
import org.springframework.web.reactive.config.EnableWebFlux;
|
|
||||||
import org.springframework.web.server.adapter.WebHttpHandlerBuilder;
|
|
||||||
import reactor.ipc.netty.NettyContext;
|
|
||||||
import reactor.ipc.netty.http.server.HttpServer;
|
|
||||||
|
|
||||||
@ComponentScan(basePackages = {"com.baeldung.security"})
|
|
||||||
@EnableWebFlux
|
|
||||||
public class SpringSecurity5Application {
|
|
||||||
|
|
||||||
public static void main(String[] args) {
|
|
||||||
try (AnnotationConfigApplicationContext context =
|
|
||||||
new AnnotationConfigApplicationContext(SpringSecurity5Application.class)) {
|
|
||||||
context.getBean(NettyContext.class).onClose().block();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Bean
|
|
||||||
public NettyContext nettyContext(ApplicationContext context) {
|
|
||||||
HttpHandler handler = WebHttpHandlerBuilder.applicationContext(context)
|
|
||||||
.build();
|
|
||||||
ReactorHttpHandlerAdapter adapter = new ReactorHttpHandlerAdapter(handler);
|
|
||||||
HttpServer httpServer = HttpServer.create("localhost", 8080);
|
|
||||||
return httpServer.newHandler(adapter).block();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
|
@ -1,23 +0,0 @@
|
||||||
package com.baeldung.functional;
|
|
||||||
|
|
||||||
class Actor {
|
|
||||||
private String firstname;
|
|
||||||
private String lastname;
|
|
||||||
|
|
||||||
public Actor() {
|
|
||||||
}
|
|
||||||
|
|
||||||
public Actor(String firstname, String lastname) {
|
|
||||||
this.firstname = firstname;
|
|
||||||
this.lastname = lastname;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getFirstname() {
|
|
||||||
return firstname;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getLastname() {
|
|
||||||
return lastname;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
|
@ -1,61 +0,0 @@
|
||||||
package com.baeldung.functional;
|
|
||||||
|
|
||||||
import static org.springframework.web.reactive.function.BodyInserters.fromObject;
|
|
||||||
import static org.springframework.web.reactive.function.server.RequestPredicates.GET;
|
|
||||||
import static org.springframework.web.reactive.function.server.RouterFunctions.route;
|
|
||||||
import static org.springframework.web.reactive.function.server.RouterFunctions.toHttpHandler;
|
|
||||||
import static org.springframework.web.reactive.function.server.ServerResponse.ok;
|
|
||||||
|
|
||||||
import org.apache.catalina.Context;
|
|
||||||
import org.apache.catalina.startup.Tomcat;
|
|
||||||
import org.springframework.boot.web.embedded.tomcat.TomcatWebServer;
|
|
||||||
import org.springframework.boot.web.server.WebServer;
|
|
||||||
import org.springframework.core.io.ClassPathResource;
|
|
||||||
import org.springframework.http.server.reactive.HttpHandler;
|
|
||||||
import org.springframework.http.server.reactive.ServletHttpHandlerAdapter;
|
|
||||||
import org.springframework.web.reactive.function.server.RouterFunction;
|
|
||||||
import org.springframework.web.reactive.function.server.RouterFunctions;
|
|
||||||
import org.springframework.web.reactive.function.server.ServerResponse;
|
|
||||||
import org.springframework.web.server.WebHandler;
|
|
||||||
import org.springframework.web.server.adapter.WebHttpHandlerBuilder;
|
|
||||||
|
|
||||||
public class ExploreSpring5URLPatternUsingRouterFunctions {
|
|
||||||
|
|
||||||
private RouterFunction<ServerResponse> routingFunction() {
|
|
||||||
|
|
||||||
return route(GET("/p?ths"), serverRequest -> ok().body(fromObject("/p?ths"))).andRoute(GET("/test/{*id}"), serverRequest -> ok().body(fromObject(serverRequest.pathVariable("id"))))
|
|
||||||
.andRoute(GET("/*card"), serverRequest -> ok().body(fromObject("/*card path was accessed")))
|
|
||||||
.andRoute(GET("/{var1}_{var2}"), serverRequest -> ok().body(fromObject(serverRequest.pathVariable("var1") + " , " + serverRequest.pathVariable("var2"))))
|
|
||||||
.andRoute(GET("/{baeldung:[a-z]+}"), serverRequest -> ok().body(fromObject("/{baeldung:[a-z]+} was accessed and baeldung=" + serverRequest.pathVariable("baeldung"))))
|
|
||||||
.and(RouterFunctions.resources("/files/{*filepaths}", new ClassPathResource("files/")));
|
|
||||||
}
|
|
||||||
|
|
||||||
WebServer start() throws Exception {
|
|
||||||
WebHandler webHandler = (WebHandler) toHttpHandler(routingFunction());
|
|
||||||
HttpHandler httpHandler = WebHttpHandlerBuilder.webHandler(webHandler)
|
|
||||||
.filter(new IndexRewriteFilter())
|
|
||||||
.build();
|
|
||||||
|
|
||||||
Tomcat tomcat = new Tomcat();
|
|
||||||
tomcat.setHostname("localhost");
|
|
||||||
tomcat.setPort(9090);
|
|
||||||
Context rootContext = tomcat.addContext("", System.getProperty("java.io.tmpdir"));
|
|
||||||
ServletHttpHandlerAdapter servlet = new ServletHttpHandlerAdapter(httpHandler);
|
|
||||||
Tomcat.addServlet(rootContext, "httpHandlerServlet", servlet);
|
|
||||||
rootContext.addServletMappingDecoded("/", "httpHandlerServlet");
|
|
||||||
|
|
||||||
TomcatWebServer server = new TomcatWebServer(tomcat);
|
|
||||||
server.start();
|
|
||||||
return server;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void main(String[] args) {
|
|
||||||
try {
|
|
||||||
new FunctionalWebApplication().start();
|
|
||||||
} catch (Exception e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
|
@ -1,41 +0,0 @@
|
||||||
package com.baeldung.functional;
|
|
||||||
|
|
||||||
import org.springframework.core.io.buffer.DataBuffer;
|
|
||||||
import org.springframework.util.MultiValueMap;
|
|
||||||
import org.springframework.web.reactive.function.server.ServerRequest;
|
|
||||||
import org.springframework.web.reactive.function.server.ServerResponse;
|
|
||||||
import reactor.core.publisher.Mono;
|
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.concurrent.atomic.AtomicLong;
|
|
||||||
|
|
||||||
import static org.springframework.web.reactive.function.BodyExtractors.toDataBuffers;
|
|
||||||
import static org.springframework.web.reactive.function.BodyExtractors.toFormData;
|
|
||||||
import static org.springframework.web.reactive.function.BodyInserters.fromObject;
|
|
||||||
import static org.springframework.web.reactive.function.server.ServerResponse.ok;
|
|
||||||
|
|
||||||
public class FormHandler {
|
|
||||||
|
|
||||||
Mono<ServerResponse> handleLogin(ServerRequest request) {
|
|
||||||
return request.body(toFormData())
|
|
||||||
.map(MultiValueMap::toSingleValueMap)
|
|
||||||
.filter(formData -> "baeldung".equals(formData.get("user")))
|
|
||||||
.filter(formData -> "you_know_what_to_do".equals(formData.get("token")))
|
|
||||||
.flatMap(formData -> ok().body(Mono.just("welcome back!"), String.class))
|
|
||||||
.switchIfEmpty(ServerResponse.badRequest()
|
|
||||||
.build());
|
|
||||||
}
|
|
||||||
|
|
||||||
Mono<ServerResponse> handleUpload(ServerRequest request) {
|
|
||||||
return request.body(toDataBuffers())
|
|
||||||
.collectList()
|
|
||||||
.flatMap(dataBuffers -> ok().body(fromObject(extractData(dataBuffers).toString())));
|
|
||||||
}
|
|
||||||
|
|
||||||
private AtomicLong extractData(List<DataBuffer> dataBuffers) {
|
|
||||||
AtomicLong atomicLong = new AtomicLong(0);
|
|
||||||
dataBuffers.forEach(d -> atomicLong.addAndGet(d.asByteBuffer()
|
|
||||||
.array().length));
|
|
||||||
return atomicLong;
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,87 +0,0 @@
|
||||||
package com.baeldung.functional;
|
|
||||||
|
|
||||||
import static org.springframework.web.reactive.function.BodyInserters.fromObject;
|
|
||||||
import static org.springframework.web.reactive.function.server.RequestPredicates.GET;
|
|
||||||
import static org.springframework.web.reactive.function.server.RequestPredicates.POST;
|
|
||||||
import static org.springframework.web.reactive.function.server.RequestPredicates.path;
|
|
||||||
import static org.springframework.web.reactive.function.server.RouterFunctions.route;
|
|
||||||
import static org.springframework.web.reactive.function.server.RouterFunctions.toHttpHandler;
|
|
||||||
import static org.springframework.web.reactive.function.server.ServerResponse.ok;
|
|
||||||
|
|
||||||
import java.util.Arrays;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.concurrent.CopyOnWriteArrayList;
|
|
||||||
|
|
||||||
import org.springframework.boot.SpringApplication;
|
|
||||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
|
||||||
import org.springframework.boot.web.servlet.ServletRegistrationBean;
|
|
||||||
import org.springframework.context.annotation.Bean;
|
|
||||||
import org.springframework.context.annotation.ComponentScan;
|
|
||||||
import org.springframework.context.annotation.Configuration;
|
|
||||||
import org.springframework.context.annotation.Profile;
|
|
||||||
import org.springframework.core.io.ClassPathResource;
|
|
||||||
import org.springframework.http.server.reactive.HttpHandler;
|
|
||||||
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
|
|
||||||
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
|
|
||||||
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
|
|
||||||
import org.springframework.web.reactive.function.server.RouterFunction;
|
|
||||||
import org.springframework.web.reactive.function.server.RouterFunctions;
|
|
||||||
import org.springframework.web.reactive.function.server.ServerResponse;
|
|
||||||
import org.springframework.web.server.WebHandler;
|
|
||||||
import org.springframework.web.server.adapter.WebHttpHandlerBuilder;
|
|
||||||
|
|
||||||
import reactor.core.publisher.Flux;
|
|
||||||
|
|
||||||
@SpringBootApplication
|
|
||||||
@ComponentScan(basePackages = { "com.baeldung.functional" })
|
|
||||||
public class FunctionalSpringBootApplication {
|
|
||||||
|
|
||||||
private static final Actor BRAD_PITT = new Actor("Brad", "Pitt");
|
|
||||||
private static final Actor TOM_HANKS = new Actor("Tom", "Hanks");
|
|
||||||
private static final List<Actor> actors = new CopyOnWriteArrayList<>(Arrays.asList(BRAD_PITT, TOM_HANKS));
|
|
||||||
|
|
||||||
private RouterFunction<ServerResponse> routingFunction() {
|
|
||||||
FormHandler formHandler = new FormHandler();
|
|
||||||
|
|
||||||
RouterFunction<ServerResponse> restfulRouter = route(GET("/"), serverRequest -> ok().body(Flux.fromIterable(actors), Actor.class)).andRoute(POST("/"), serverRequest -> serverRequest.bodyToMono(Actor.class)
|
|
||||||
.doOnNext(actors::add)
|
|
||||||
.then(ok().build()));
|
|
||||||
|
|
||||||
return route(GET("/test"), serverRequest -> ok().body(fromObject("helloworld"))).andRoute(POST("/login"), formHandler::handleLogin)
|
|
||||||
.andRoute(POST("/upload"), formHandler::handleUpload)
|
|
||||||
.and(RouterFunctions.resources("/files/**", new ClassPathResource("files/")))
|
|
||||||
.andNest(path("/actor"), restfulRouter)
|
|
||||||
.filter((request, next) -> {
|
|
||||||
System.out.println("Before handler invocation: " + request.path());
|
|
||||||
return next.handle(request);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
@Bean
|
|
||||||
public ServletRegistrationBean servletRegistrationBean() throws Exception {
|
|
||||||
HttpHandler httpHandler = WebHttpHandlerBuilder.webHandler((WebHandler) toHttpHandler(routingFunction()))
|
|
||||||
.filter(new IndexRewriteFilter())
|
|
||||||
.build();
|
|
||||||
ServletRegistrationBean registrationBean = new ServletRegistrationBean<>(new RootServlet(httpHandler), "/");
|
|
||||||
registrationBean.setLoadOnStartup(1);
|
|
||||||
registrationBean.setAsyncSupported(true);
|
|
||||||
return registrationBean;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Configuration
|
|
||||||
@EnableWebSecurity
|
|
||||||
@Profile("!https")
|
|
||||||
static class SecurityConfig extends WebSecurityConfigurerAdapter {
|
|
||||||
@Override
|
|
||||||
protected void configure(final HttpSecurity http) throws Exception {
|
|
||||||
http.authorizeRequests()
|
|
||||||
.anyRequest()
|
|
||||||
.permitAll();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void main(String[] args) {
|
|
||||||
SpringApplication.run(FunctionalSpringBootApplication.class, args);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
|
@ -1,80 +0,0 @@
|
||||||
package com.baeldung.functional;
|
|
||||||
|
|
||||||
import static org.springframework.web.reactive.function.BodyInserters.fromObject;
|
|
||||||
import static org.springframework.web.reactive.function.server.RequestPredicates.GET;
|
|
||||||
import static org.springframework.web.reactive.function.server.RequestPredicates.POST;
|
|
||||||
import static org.springframework.web.reactive.function.server.RequestPredicates.path;
|
|
||||||
import static org.springframework.web.reactive.function.server.RouterFunctions.route;
|
|
||||||
import static org.springframework.web.reactive.function.server.RouterFunctions.toHttpHandler;
|
|
||||||
import static org.springframework.web.reactive.function.server.ServerResponse.ok;
|
|
||||||
|
|
||||||
import java.util.Arrays;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.concurrent.CopyOnWriteArrayList;
|
|
||||||
|
|
||||||
import org.apache.catalina.Context;
|
|
||||||
import org.apache.catalina.startup.Tomcat;
|
|
||||||
import org.springframework.boot.web.embedded.tomcat.TomcatWebServer;
|
|
||||||
import org.springframework.boot.web.server.WebServer;
|
|
||||||
import org.springframework.core.io.ClassPathResource;
|
|
||||||
import org.springframework.http.server.reactive.HttpHandler;
|
|
||||||
import org.springframework.http.server.reactive.ServletHttpHandlerAdapter;
|
|
||||||
import org.springframework.web.reactive.function.server.RouterFunction;
|
|
||||||
import org.springframework.web.reactive.function.server.RouterFunctions;
|
|
||||||
import org.springframework.web.reactive.function.server.ServerResponse;
|
|
||||||
import org.springframework.web.server.WebHandler;
|
|
||||||
import org.springframework.web.server.adapter.WebHttpHandlerBuilder;
|
|
||||||
|
|
||||||
import reactor.core.publisher.Flux;
|
|
||||||
|
|
||||||
public class FunctionalWebApplication {
|
|
||||||
|
|
||||||
private static final Actor BRAD_PITT = new Actor("Brad", "Pitt");
|
|
||||||
private static final Actor TOM_HANKS = new Actor("Tom", "Hanks");
|
|
||||||
private static final List<Actor> actors = new CopyOnWriteArrayList<>(Arrays.asList(BRAD_PITT, TOM_HANKS));
|
|
||||||
|
|
||||||
private RouterFunction<ServerResponse> routingFunction() {
|
|
||||||
FormHandler formHandler = new FormHandler();
|
|
||||||
|
|
||||||
RouterFunction<ServerResponse> restfulRouter = route(GET("/"), serverRequest -> ok().body(Flux.fromIterable(actors), Actor.class)).andRoute(POST("/"), serverRequest -> serverRequest.bodyToMono(Actor.class)
|
|
||||||
.doOnNext(actors::add)
|
|
||||||
.then(ok().build()));
|
|
||||||
|
|
||||||
return route(GET("/test"), serverRequest -> ok().body(fromObject("helloworld"))).andRoute(POST("/login"), formHandler::handleLogin)
|
|
||||||
.andRoute(POST("/upload"), formHandler::handleUpload)
|
|
||||||
.and(RouterFunctions.resources("/files/**", new ClassPathResource("files/")))
|
|
||||||
.andNest(path("/actor"), restfulRouter)
|
|
||||||
.filter((request, next) -> {
|
|
||||||
System.out.println("Before handler invocation: " + request.path());
|
|
||||||
return next.handle(request);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
WebServer start() throws Exception {
|
|
||||||
WebHandler webHandler = (WebHandler) toHttpHandler(routingFunction());
|
|
||||||
HttpHandler httpHandler = WebHttpHandlerBuilder.webHandler(webHandler)
|
|
||||||
.filter(new IndexRewriteFilter())
|
|
||||||
.build();
|
|
||||||
|
|
||||||
Tomcat tomcat = new Tomcat();
|
|
||||||
tomcat.setHostname("localhost");
|
|
||||||
tomcat.setPort(9090);
|
|
||||||
Context rootContext = tomcat.addContext("", System.getProperty("java.io.tmpdir"));
|
|
||||||
ServletHttpHandlerAdapter servlet = new ServletHttpHandlerAdapter(httpHandler);
|
|
||||||
Tomcat.addServlet(rootContext, "httpHandlerServlet", servlet);
|
|
||||||
rootContext.addServletMappingDecoded("/", "httpHandlerServlet");
|
|
||||||
|
|
||||||
TomcatWebServer server = new TomcatWebServer(tomcat);
|
|
||||||
server.start();
|
|
||||||
return server;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void main(String[] args) {
|
|
||||||
try {
|
|
||||||
new FunctionalWebApplication().start();
|
|
||||||
} catch (Exception e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,27 +0,0 @@
|
||||||
package com.baeldung.functional;
|
|
||||||
|
|
||||||
import org.springframework.http.server.reactive.ServerHttpRequest;
|
|
||||||
import org.springframework.web.server.ServerWebExchange;
|
|
||||||
import org.springframework.web.server.WebFilter;
|
|
||||||
import org.springframework.web.server.WebFilterChain;
|
|
||||||
import reactor.core.publisher.Mono;
|
|
||||||
|
|
||||||
class IndexRewriteFilter implements WebFilter {
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Mono<Void> filter(ServerWebExchange serverWebExchange, WebFilterChain webFilterChain) {
|
|
||||||
ServerHttpRequest request = serverWebExchange.getRequest();
|
|
||||||
if (request.getURI()
|
|
||||||
.getPath()
|
|
||||||
.equals("/")) {
|
|
||||||
return webFilterChain.filter(serverWebExchange.mutate()
|
|
||||||
.request(builder -> builder.method(request.getMethod())
|
|
||||||
.contextPath(request.getPath()
|
|
||||||
.toString())
|
|
||||||
.path("/test"))
|
|
||||||
.build());
|
|
||||||
}
|
|
||||||
return webFilterChain.filter(serverWebExchange);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
|
@ -1,11 +0,0 @@
|
||||||
package com.baeldung.functional;
|
|
||||||
|
|
||||||
import java.util.Random;
|
|
||||||
|
|
||||||
public class MyService {
|
|
||||||
|
|
||||||
public int getRandomNumber() {
|
|
||||||
return (new Random().nextInt(10));
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
|
@ -1,82 +0,0 @@
|
||||||
package com.baeldung.functional;
|
|
||||||
|
|
||||||
import static org.springframework.web.reactive.function.BodyExtractors.toDataBuffers;
|
|
||||||
import static org.springframework.web.reactive.function.BodyExtractors.toFormData;
|
|
||||||
import static org.springframework.web.reactive.function.BodyInserters.fromObject;
|
|
||||||
import static org.springframework.web.reactive.function.server.RequestPredicates.GET;
|
|
||||||
import static org.springframework.web.reactive.function.server.RequestPredicates.POST;
|
|
||||||
import static org.springframework.web.reactive.function.server.RequestPredicates.path;
|
|
||||||
import static org.springframework.web.reactive.function.server.RouterFunctions.route;
|
|
||||||
import static org.springframework.web.reactive.function.server.RouterFunctions.toHttpHandler;
|
|
||||||
import static org.springframework.web.reactive.function.server.ServerResponse.ok;
|
|
||||||
|
|
||||||
import java.util.Arrays;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.concurrent.CopyOnWriteArrayList;
|
|
||||||
import java.util.concurrent.atomic.AtomicLong;
|
|
||||||
|
|
||||||
import org.springframework.core.io.ClassPathResource;
|
|
||||||
import org.springframework.http.server.reactive.HttpHandler;
|
|
||||||
import org.springframework.http.server.reactive.ServletHttpHandlerAdapter;
|
|
||||||
import org.springframework.util.MultiValueMap;
|
|
||||||
import org.springframework.web.reactive.function.server.RouterFunction;
|
|
||||||
import org.springframework.web.reactive.function.server.RouterFunctions;
|
|
||||||
import org.springframework.web.reactive.function.server.ServerResponse;
|
|
||||||
import org.springframework.web.server.WebHandler;
|
|
||||||
import org.springframework.web.server.adapter.WebHttpHandlerBuilder;
|
|
||||||
|
|
||||||
import reactor.core.publisher.Flux;
|
|
||||||
import reactor.core.publisher.Mono;
|
|
||||||
|
|
||||||
public class RootServlet extends ServletHttpHandlerAdapter {
|
|
||||||
|
|
||||||
public RootServlet() {
|
|
||||||
this(WebHttpHandlerBuilder.webHandler((WebHandler) toHttpHandler(routingFunction()))
|
|
||||||
.filter(new IndexRewriteFilter())
|
|
||||||
.build());
|
|
||||||
}
|
|
||||||
|
|
||||||
RootServlet(HttpHandler httpHandler) {
|
|
||||||
super(httpHandler);
|
|
||||||
}
|
|
||||||
|
|
||||||
private static final Actor BRAD_PITT = new Actor("Brad", "Pitt");
|
|
||||||
private static final Actor TOM_HANKS = new Actor("Tom", "Hanks");
|
|
||||||
private static final List<Actor> actors = new CopyOnWriteArrayList<>(Arrays.asList(BRAD_PITT, TOM_HANKS));
|
|
||||||
|
|
||||||
private static RouterFunction<?> routingFunction() {
|
|
||||||
|
|
||||||
return route(GET("/test"), serverRequest -> ok().body(fromObject("helloworld"))).andRoute(POST("/login"), serverRequest -> serverRequest.body(toFormData())
|
|
||||||
.map(MultiValueMap::toSingleValueMap)
|
|
||||||
.map(formData -> {
|
|
||||||
System.out.println("form data: " + formData.toString());
|
|
||||||
if ("baeldung".equals(formData.get("user")) && "you_know_what_to_do".equals(formData.get("token"))) {
|
|
||||||
return ok().body(Mono.just("welcome back!"), String.class)
|
|
||||||
.block();
|
|
||||||
}
|
|
||||||
return ServerResponse.badRequest()
|
|
||||||
.build()
|
|
||||||
.block();
|
|
||||||
}))
|
|
||||||
.andRoute(POST("/upload"), serverRequest -> serverRequest.body(toDataBuffers())
|
|
||||||
.collectList()
|
|
||||||
.map(dataBuffers -> {
|
|
||||||
AtomicLong atomicLong = new AtomicLong(0);
|
|
||||||
dataBuffers.forEach(d -> atomicLong.addAndGet(d.asByteBuffer()
|
|
||||||
.array().length));
|
|
||||||
System.out.println("data length:" + atomicLong.get());
|
|
||||||
return ok().body(fromObject(atomicLong.toString()))
|
|
||||||
.block();
|
|
||||||
}))
|
|
||||||
.and(RouterFunctions.resources("/files/**", new ClassPathResource("files/")))
|
|
||||||
.andNest(path("/actor"), route(GET("/"), serverRequest -> ok().body(Flux.fromIterable(actors), Actor.class)).andRoute(POST("/"), serverRequest -> serverRequest.bodyToMono(Actor.class)
|
|
||||||
.doOnNext(actors::add)
|
|
||||||
.then(ok().build())))
|
|
||||||
.filter((request, next) -> {
|
|
||||||
System.out.println("Before handler invocation: " + request.path());
|
|
||||||
return next.handle(request);
|
|
||||||
});
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
|
@ -1,127 +0,0 @@
|
||||||
package com.baeldung.jsonb;
|
|
||||||
|
|
||||||
import java.math.BigDecimal;
|
|
||||||
import java.time.LocalDate;
|
|
||||||
|
|
||||||
import javax.json.bind.annotation.JsonbDateFormat;
|
|
||||||
import javax.json.bind.annotation.JsonbNumberFormat;
|
|
||||||
import javax.json.bind.annotation.JsonbProperty;
|
|
||||||
import javax.json.bind.annotation.JsonbTransient;
|
|
||||||
|
|
||||||
public class Person {
|
|
||||||
|
|
||||||
private int id;
|
|
||||||
@JsonbProperty("person-name")
|
|
||||||
private String name;
|
|
||||||
@JsonbProperty(nillable = true)
|
|
||||||
private String email;
|
|
||||||
@JsonbTransient
|
|
||||||
private int age;
|
|
||||||
@JsonbDateFormat("dd-MM-yyyy")
|
|
||||||
private LocalDate registeredDate;
|
|
||||||
private BigDecimal salary;
|
|
||||||
|
|
||||||
public Person() {
|
|
||||||
}
|
|
||||||
|
|
||||||
public Person(int id, String name, String email, int age, LocalDate registeredDate, BigDecimal salary) {
|
|
||||||
super();
|
|
||||||
this.id = id;
|
|
||||||
this.name = name;
|
|
||||||
this.email = email;
|
|
||||||
this.age = age;
|
|
||||||
this.registeredDate = registeredDate;
|
|
||||||
this.salary = salary;
|
|
||||||
}
|
|
||||||
|
|
||||||
public int getId() {
|
|
||||||
return id;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setId(int id) {
|
|
||||||
this.id = id;
|
|
||||||
}
|
|
||||||
|
|
||||||
public int getAge() {
|
|
||||||
return age;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setAge(int age) {
|
|
||||||
this.age = age;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getEmail() {
|
|
||||||
return email;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setEmail(String email) {
|
|
||||||
this.email = email;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getName() {
|
|
||||||
return name;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setName(String name) {
|
|
||||||
this.name = name;
|
|
||||||
}
|
|
||||||
|
|
||||||
@JsonbNumberFormat(locale = "en_US", value = "#0.0")
|
|
||||||
public BigDecimal getSalary() {
|
|
||||||
return salary;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setSalary(BigDecimal salary) {
|
|
||||||
this.salary = salary;
|
|
||||||
}
|
|
||||||
|
|
||||||
public LocalDate getRegisteredDate() {
|
|
||||||
return registeredDate;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setRegisteredDate(LocalDate registeredDate) {
|
|
||||||
this.registeredDate = registeredDate;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String toString() {
|
|
||||||
StringBuilder builder = new StringBuilder();
|
|
||||||
builder.append("Person [id=");
|
|
||||||
builder.append(id);
|
|
||||||
builder.append(", name=");
|
|
||||||
builder.append(name);
|
|
||||||
builder.append(", email=");
|
|
||||||
builder.append(email);
|
|
||||||
builder.append(", age=");
|
|
||||||
builder.append(age);
|
|
||||||
builder.append(", registeredDate=");
|
|
||||||
builder.append(registeredDate);
|
|
||||||
builder.append(", salary=");
|
|
||||||
builder.append(salary);
|
|
||||||
builder.append("]");
|
|
||||||
return builder.toString();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public int hashCode() {
|
|
||||||
final int prime = 31;
|
|
||||||
int result = 1;
|
|
||||||
result = prime * result + id;
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean equals(Object obj) {
|
|
||||||
if (this == obj)
|
|
||||||
return true;
|
|
||||||
if (obj == null)
|
|
||||||
return false;
|
|
||||||
if (getClass() != obj.getClass())
|
|
||||||
return false;
|
|
||||||
Person other = (Person) obj;
|
|
||||||
if (id != other.id)
|
|
||||||
return false;
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
|
@ -1,58 +0,0 @@
|
||||||
package com.baeldung.jsonb;
|
|
||||||
|
|
||||||
import java.math.BigDecimal;
|
|
||||||
import java.time.LocalDate;
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.Arrays;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
import javax.annotation.PostConstruct;
|
|
||||||
|
|
||||||
import org.springframework.http.HttpStatus;
|
|
||||||
import org.springframework.web.bind.annotation.GetMapping;
|
|
||||||
import org.springframework.web.bind.annotation.PathVariable;
|
|
||||||
import org.springframework.web.bind.annotation.PostMapping;
|
|
||||||
import org.springframework.web.bind.annotation.RequestBody;
|
|
||||||
import org.springframework.web.bind.annotation.ResponseBody;
|
|
||||||
import org.springframework.web.bind.annotation.ResponseStatus;
|
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
|
||||||
|
|
||||||
@RestController("/person")
|
|
||||||
public class PersonController {
|
|
||||||
|
|
||||||
List<Person> personRepository;
|
|
||||||
|
|
||||||
@PostConstruct
|
|
||||||
public void init() {
|
|
||||||
// @formatter:off
|
|
||||||
personRepository = new ArrayList<>(Arrays.asList(
|
|
||||||
new Person(1, "Jhon", "jhon@test.com", 20, LocalDate.of(2019, 9, 9), BigDecimal.valueOf(1000)),
|
|
||||||
new Person(2, "Jhon", "jhon1@test.com", 20, LocalDate.of(2019, 9, 9), BigDecimal.valueOf(1500)),
|
|
||||||
new Person(3, "Jhon", null, 20, LocalDate.of(2019, 9, 9), BigDecimal.valueOf(1000)),
|
|
||||||
new Person(4, "Tom", "tom@test.com", 21, LocalDate.of(2019, 9, 9), BigDecimal.valueOf(1500)),
|
|
||||||
new Person(5, "Mark", "mark@test.com", 21, LocalDate.of(2019, 9, 9), BigDecimal.valueOf(1200)),
|
|
||||||
new Person(6, "Julia", "jhon@test.com", 20, LocalDate.of(2019, 9, 9), BigDecimal.valueOf(1000))));
|
|
||||||
// @formatter:on
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
@GetMapping("/person/{id}")
|
|
||||||
@ResponseBody
|
|
||||||
public Person findById(@PathVariable final int id) {
|
|
||||||
return personRepository.get(id);
|
|
||||||
}
|
|
||||||
|
|
||||||
@PostMapping("/person")
|
|
||||||
@ResponseStatus(HttpStatus.OK)
|
|
||||||
@ResponseBody
|
|
||||||
public boolean insertPerson(@RequestBody final Person person) {
|
|
||||||
return personRepository.add(person);
|
|
||||||
}
|
|
||||||
|
|
||||||
@GetMapping("/person")
|
|
||||||
@ResponseBody
|
|
||||||
public List<Person> findAll() {
|
|
||||||
return personRepository;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
|
@ -1,30 +0,0 @@
|
||||||
package com.baeldung.jsonb;
|
|
||||||
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.Collection;
|
|
||||||
|
|
||||||
import org.springframework.boot.SpringApplication;
|
|
||||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
|
||||||
import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
|
|
||||||
import org.springframework.context.annotation.Bean;
|
|
||||||
import org.springframework.context.annotation.ComponentScan;
|
|
||||||
import org.springframework.http.converter.HttpMessageConverter;
|
|
||||||
import org.springframework.http.converter.json.JsonbHttpMessageConverter;
|
|
||||||
|
|
||||||
@SpringBootApplication
|
|
||||||
@ComponentScan(basePackages = { "com.baeldung.jsonb" })
|
|
||||||
public class Spring5Application {
|
|
||||||
|
|
||||||
public static void main(String[] args) {
|
|
||||||
SpringApplication.run(Spring5Application.class, args);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Bean
|
|
||||||
public HttpMessageConverters customConverters() {
|
|
||||||
Collection<HttpMessageConverter<?>> messageConverters = new ArrayList<>();
|
|
||||||
JsonbHttpMessageConverter jsonbHttpMessageConverter = new JsonbHttpMessageConverter();
|
|
||||||
messageConverters.add(jsonbHttpMessageConverter);
|
|
||||||
return new HttpMessageConverters(true, messageConverters);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
|
@ -1,46 +0,0 @@
|
||||||
package com.baeldung.jupiter;
|
|
||||||
|
|
||||||
import org.springframework.core.MethodParameter;
|
|
||||||
import org.springframework.core.annotation.SynthesizingMethodParameter;
|
|
||||||
import org.springframework.util.Assert;
|
|
||||||
|
|
||||||
import java.lang.reflect.Constructor;
|
|
||||||
import java.lang.reflect.Executable;
|
|
||||||
import java.lang.reflect.Method;
|
|
||||||
import java.lang.reflect.Parameter;
|
|
||||||
|
|
||||||
abstract class MethodParameterFactory {
|
|
||||||
|
|
||||||
private MethodParameterFactory() {
|
|
||||||
}
|
|
||||||
|
|
||||||
public static MethodParameter createMethodParameter(Parameter parameter) {
|
|
||||||
Assert.notNull(parameter, "Parameter must not be null");
|
|
||||||
Executable executable = parameter.getDeclaringExecutable();
|
|
||||||
if (executable instanceof Method) {
|
|
||||||
return new MethodParameter((Method) executable, getIndex(parameter));
|
|
||||||
}
|
|
||||||
return new MethodParameter((Constructor<?>) executable, getIndex(parameter));
|
|
||||||
}
|
|
||||||
|
|
||||||
public static SynthesizingMethodParameter createSynthesizingMethodParameter(Parameter parameter) {
|
|
||||||
Assert.notNull(parameter, "Parameter must not be null");
|
|
||||||
Executable executable = parameter.getDeclaringExecutable();
|
|
||||||
if (executable instanceof Method) {
|
|
||||||
return new SynthesizingMethodParameter((Method) executable, getIndex(parameter));
|
|
||||||
}
|
|
||||||
throw new UnsupportedOperationException("Cannot create a SynthesizingMethodParameter for a constructor parameter: " + parameter);
|
|
||||||
}
|
|
||||||
|
|
||||||
private static int getIndex(Parameter parameter) {
|
|
||||||
Assert.notNull(parameter, "Parameter must not be null");
|
|
||||||
Executable executable = parameter.getDeclaringExecutable();
|
|
||||||
Parameter[] parameters = executable.getParameters();
|
|
||||||
for (int i = 0; i < parameters.length; i++) {
|
|
||||||
if (parameters[i] == parameter) {
|
|
||||||
return i;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
throw new IllegalStateException(String.format("Failed to resolve index of parameter [%s] in executable [%s]", parameter, executable.toGenericString()));
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,44 +0,0 @@
|
||||||
package com.baeldung.jupiter;
|
|
||||||
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
|
||||||
import org.springframework.beans.factory.annotation.Qualifier;
|
|
||||||
import org.springframework.beans.factory.annotation.Value;
|
|
||||||
import org.springframework.beans.factory.config.DependencyDescriptor;
|
|
||||||
import org.springframework.context.ApplicationContext;
|
|
||||||
import org.springframework.core.MethodParameter;
|
|
||||||
import org.springframework.core.annotation.AnnotatedElementUtils;
|
|
||||||
|
|
||||||
import java.lang.annotation.Annotation;
|
|
||||||
import java.lang.reflect.AnnotatedElement;
|
|
||||||
import java.lang.reflect.Method;
|
|
||||||
import java.lang.reflect.Parameter;
|
|
||||||
import java.util.Optional;
|
|
||||||
|
|
||||||
import static org.springframework.core.annotation.AnnotatedElementUtils.hasAnnotation;
|
|
||||||
|
|
||||||
abstract class ParameterAutowireUtils {
|
|
||||||
|
|
||||||
private ParameterAutowireUtils() {
|
|
||||||
}
|
|
||||||
|
|
||||||
public static boolean isAutowirable(Parameter parameter) {
|
|
||||||
return ApplicationContext.class.isAssignableFrom(parameter.getType()) || hasAnnotation(parameter, Autowired.class) || hasAnnotation(parameter, Qualifier.class) || hasAnnotation(parameter, Value.class);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static Object resolveDependency(Parameter parameter, Class<?> containingClass, ApplicationContext applicationContext) {
|
|
||||||
|
|
||||||
boolean required = findMergedAnnotation(parameter, Autowired.class).map(Autowired::required)
|
|
||||||
.orElse(true);
|
|
||||||
MethodParameter methodParameter = (parameter.getDeclaringExecutable() instanceof Method ? MethodParameterFactory.createSynthesizingMethodParameter(parameter) : MethodParameterFactory.createMethodParameter(parameter));
|
|
||||||
DependencyDescriptor descriptor = new DependencyDescriptor(methodParameter, required);
|
|
||||||
descriptor.setContainingClass(containingClass);
|
|
||||||
|
|
||||||
return applicationContext.getAutowireCapableBeanFactory()
|
|
||||||
.resolveDependency(descriptor, null);
|
|
||||||
}
|
|
||||||
|
|
||||||
private static <A extends Annotation> Optional<A> findMergedAnnotation(AnnotatedElement element, Class<A> annotationType) {
|
|
||||||
|
|
||||||
return Optional.ofNullable(AnnotatedElementUtils.findMergedAnnotation(element, annotationType));
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,94 +0,0 @@
|
||||||
package com.baeldung.jupiter;
|
|
||||||
|
|
||||||
import java.lang.reflect.Constructor;
|
|
||||||
import java.lang.reflect.Executable;
|
|
||||||
import java.lang.reflect.Method;
|
|
||||||
import java.lang.reflect.Parameter;
|
|
||||||
|
|
||||||
import org.junit.jupiter.api.extension.AfterAllCallback;
|
|
||||||
import org.junit.jupiter.api.extension.AfterEachCallback;
|
|
||||||
import org.junit.jupiter.api.extension.BeforeAllCallback;
|
|
||||||
import org.junit.jupiter.api.extension.BeforeEachCallback;
|
|
||||||
import org.junit.jupiter.api.extension.ExtensionContext;
|
|
||||||
import org.junit.jupiter.api.extension.ParameterContext;
|
|
||||||
import org.junit.jupiter.api.extension.ParameterResolutionException;
|
|
||||||
import org.junit.jupiter.api.extension.ParameterResolver;
|
|
||||||
import org.junit.jupiter.api.extension.TestInstancePostProcessor;
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
|
||||||
import org.springframework.context.ApplicationContext;
|
|
||||||
import org.springframework.core.annotation.AnnotatedElementUtils;
|
|
||||||
import org.springframework.test.context.TestContextManager;
|
|
||||||
import org.springframework.util.Assert;
|
|
||||||
|
|
||||||
public class SpringExtension implements BeforeAllCallback, AfterAllCallback, TestInstancePostProcessor, BeforeEachCallback, AfterEachCallback, ParameterResolver {
|
|
||||||
|
|
||||||
private static final ExtensionContext.Namespace namespace = ExtensionContext.Namespace.create(SpringExtension.class);
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void beforeAll(ExtensionContext context) throws Exception {
|
|
||||||
getTestContextManager(context).beforeTestClass();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void afterAll(ExtensionContext context) throws Exception {
|
|
||||||
try {
|
|
||||||
getTestContextManager(context).afterTestClass();
|
|
||||||
} finally {
|
|
||||||
context.getStore(namespace)
|
|
||||||
.remove(context.getTestClass()
|
|
||||||
.get());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void postProcessTestInstance(Object testInstance, ExtensionContext context) throws Exception {
|
|
||||||
getTestContextManager(context).prepareTestInstance(testInstance);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void beforeEach(ExtensionContext context) throws Exception {
|
|
||||||
Object testInstance = context.getTestInstance();
|
|
||||||
Method testMethod = context.getTestMethod()
|
|
||||||
.get();
|
|
||||||
getTestContextManager(context).beforeTestMethod(testInstance, testMethod);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void afterEach(ExtensionContext context) throws Exception {
|
|
||||||
Object testInstance = context.getTestInstance();
|
|
||||||
Method testMethod = context.getTestMethod()
|
|
||||||
.get();
|
|
||||||
Throwable testException = context.getExecutionException()
|
|
||||||
.orElse(null);
|
|
||||||
getTestContextManager(context).afterTestMethod(testInstance, testMethod, testException);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean supportsParameter(ParameterContext parameterContext, ExtensionContext extensionContext) throws ParameterResolutionException {
|
|
||||||
Parameter parameter = parameterContext.getParameter();
|
|
||||||
Executable executable = parameter.getDeclaringExecutable();
|
|
||||||
return ((executable instanceof Constructor) && AnnotatedElementUtils.hasAnnotation(executable, Autowired.class)) || ParameterAutowireUtils.isAutowirable(parameter);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Object resolveParameter(ParameterContext parameterContext, ExtensionContext extensionContext) throws ParameterResolutionException {
|
|
||||||
Parameter parameter = parameterContext.getParameter();
|
|
||||||
Class<?> testClass = extensionContext.getTestClass()
|
|
||||||
.get();
|
|
||||||
ApplicationContext applicationContext = getApplicationContext(extensionContext);
|
|
||||||
return ParameterAutowireUtils.resolveDependency(parameter, testClass, applicationContext);
|
|
||||||
}
|
|
||||||
|
|
||||||
private ApplicationContext getApplicationContext(ExtensionContext context) {
|
|
||||||
return getTestContextManager(context).getTestContext()
|
|
||||||
.getApplicationContext();
|
|
||||||
}
|
|
||||||
|
|
||||||
private TestContextManager getTestContextManager(ExtensionContext context) {
|
|
||||||
Assert.notNull(context, "ExtensionContext must not be null");
|
|
||||||
Class<?> testClass = context.getTestClass()
|
|
||||||
.get();
|
|
||||||
ExtensionContext.Store store = context.getStore(namespace);
|
|
||||||
return store.getOrComputeIfAbsent(testClass, TestContextManager::new, TestContextManager.class);
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,39 +0,0 @@
|
||||||
package com.baeldung.jupiter;
|
|
||||||
|
|
||||||
import org.junit.jupiter.api.extension.ExtendWith;
|
|
||||||
import org.springframework.context.ApplicationContextInitializer;
|
|
||||||
import org.springframework.context.ConfigurableApplicationContext;
|
|
||||||
import org.springframework.core.annotation.AliasFor;
|
|
||||||
import org.springframework.test.context.ContextConfiguration;
|
|
||||||
|
|
||||||
import java.lang.annotation.*;
|
|
||||||
|
|
||||||
@ExtendWith(SpringExtension.class)
|
|
||||||
@ContextConfiguration
|
|
||||||
@Documented
|
|
||||||
@Inherited
|
|
||||||
@Retention(RetentionPolicy.RUNTIME)
|
|
||||||
@Target(ElementType.TYPE)
|
|
||||||
public @interface SpringJUnit5Config {
|
|
||||||
|
|
||||||
@AliasFor(annotation = ContextConfiguration.class, attribute = "classes")
|
|
||||||
Class<?>[] value() default {};
|
|
||||||
|
|
||||||
@AliasFor(annotation = ContextConfiguration.class)
|
|
||||||
Class<?>[] classes() default {};
|
|
||||||
|
|
||||||
@AliasFor(annotation = ContextConfiguration.class)
|
|
||||||
String[] locations() default {};
|
|
||||||
|
|
||||||
@AliasFor(annotation = ContextConfiguration.class)
|
|
||||||
Class<? extends ApplicationContextInitializer<? extends ConfigurableApplicationContext>>[] initializers() default {};
|
|
||||||
|
|
||||||
@AliasFor(annotation = ContextConfiguration.class)
|
|
||||||
boolean inheritLocations() default true;
|
|
||||||
|
|
||||||
@AliasFor(annotation = ContextConfiguration.class)
|
|
||||||
boolean inheritInitializers() default true;
|
|
||||||
|
|
||||||
@AliasFor(annotation = ContextConfiguration.class)
|
|
||||||
String name() default "";
|
|
||||||
}
|
|
|
@ -1,20 +0,0 @@
|
||||||
package com.baeldung.jupiter;
|
|
||||||
|
|
||||||
import com.baeldung.web.reactive.Task;
|
|
||||||
import org.springframework.context.annotation.Bean;
|
|
||||||
import org.springframework.context.annotation.Configuration;
|
|
||||||
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
|
|
||||||
|
|
||||||
@Configuration
|
|
||||||
public class TestConfig {
|
|
||||||
|
|
||||||
@Bean
|
|
||||||
static PropertySourcesPlaceholderConfigurer placeholderConfigurer() {
|
|
||||||
return new PropertySourcesPlaceholderConfigurer();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Bean
|
|
||||||
Task taskName() {
|
|
||||||
return new Task("taskName", 1);
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,27 +0,0 @@
|
||||||
package com.baeldung.persistence;
|
|
||||||
|
|
||||||
import static org.apache.commons.lang3.RandomStringUtils.randomAlphabetic;
|
|
||||||
|
|
||||||
import java.util.stream.IntStream;
|
|
||||||
|
|
||||||
import org.springframework.beans.factory.InitializingBean;
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
|
||||||
import org.springframework.stereotype.Component;
|
|
||||||
|
|
||||||
import com.baeldung.web.Foo;
|
|
||||||
|
|
||||||
@Component
|
|
||||||
public class DataSetupBean implements InitializingBean {
|
|
||||||
|
|
||||||
@Autowired
|
|
||||||
private FooRepository repo;
|
|
||||||
|
|
||||||
//
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void afterPropertiesSet() throws Exception {
|
|
||||||
IntStream.range(1, 20)
|
|
||||||
.forEach(i -> repo.save(new Foo(randomAlphabetic(8))));
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
|
@ -1,10 +0,0 @@
|
||||||
package com.baeldung.persistence;
|
|
||||||
|
|
||||||
import org.springframework.data.jpa.repository.JpaRepository;
|
|
||||||
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
|
|
||||||
|
|
||||||
import com.baeldung.web.Foo;
|
|
||||||
|
|
||||||
public interface FooRepository extends JpaRepository<Foo, Long>, JpaSpecificationExecutor<Foo> {
|
|
||||||
|
|
||||||
}
|
|
|
@ -0,0 +1,13 @@
|
||||||
|
package com.baeldung.reactive;
|
||||||
|
|
||||||
|
import org.springframework.boot.SpringApplication;
|
||||||
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
|
||||||
|
@SpringBootApplication
|
||||||
|
public class Spring5ReactiveApplication {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
SpringApplication.run(Spring5ReactiveApplication.class, args);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,13 @@
|
||||||
|
package com.baeldung.reactive.controller;
|
||||||
|
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
@AllArgsConstructor
|
||||||
|
@Data
|
||||||
|
public class Foo {
|
||||||
|
|
||||||
|
private long id;
|
||||||
|
private String name;
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,34 @@
|
||||||
|
package com.baeldung.reactive.controller;
|
||||||
|
|
||||||
|
import static org.apache.commons.lang3.RandomStringUtils.randomAlphabetic;
|
||||||
|
|
||||||
|
import java.time.Duration;
|
||||||
|
|
||||||
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
|
import org.springframework.web.bind.annotation.PathVariable;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
|
import reactor.core.publisher.ConnectableFlux;
|
||||||
|
import reactor.core.publisher.Flux;
|
||||||
|
import reactor.core.publisher.Mono;
|
||||||
|
|
||||||
|
@RestController("/foos")
|
||||||
|
public class FooReactiveController {
|
||||||
|
|
||||||
|
@GetMapping("/{id}")
|
||||||
|
public Mono<Foo> getFoo(@PathVariable("id") long id) {
|
||||||
|
return Mono.just(new Foo(id, randomAlphabetic(6)));
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping("/")
|
||||||
|
public Flux<Foo> getAllFoos() {
|
||||||
|
final ConnectableFlux<Foo> flux = Flux.<Foo> create(fluxSink -> {
|
||||||
|
while (true) {
|
||||||
|
fluxSink.next(new Foo(System.currentTimeMillis(), randomAlphabetic(6)));
|
||||||
|
}
|
||||||
|
}).sample(Duration.ofSeconds(1)).publish();
|
||||||
|
|
||||||
|
return flux;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -1,37 +0,0 @@
|
||||||
package com.baeldung.security;
|
|
||||||
|
|
||||||
import org.springframework.web.bind.annotation.GetMapping;
|
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
|
||||||
import reactor.core.publisher.Mono;
|
|
||||||
|
|
||||||
import java.security.Principal;
|
|
||||||
|
|
||||||
@RestController
|
|
||||||
public class GreetController {
|
|
||||||
|
|
||||||
private GreetService greetService;
|
|
||||||
|
|
||||||
public GreetController(GreetService greetService) {
|
|
||||||
this.greetService = greetService;
|
|
||||||
}
|
|
||||||
|
|
||||||
@GetMapping("/")
|
|
||||||
public Mono<String> greet(Mono<Principal> principal) {
|
|
||||||
return principal
|
|
||||||
.map(Principal::getName)
|
|
||||||
.map(name -> String.format("Hello, %s", name));
|
|
||||||
}
|
|
||||||
|
|
||||||
@GetMapping("/admin")
|
|
||||||
public Mono<String> greetAdmin(Mono<Principal> principal) {
|
|
||||||
return principal
|
|
||||||
.map(Principal::getName)
|
|
||||||
.map(name -> String.format("Admin access: %s", name));
|
|
||||||
}
|
|
||||||
|
|
||||||
@GetMapping("/greetService")
|
|
||||||
public Mono<String> greetService() {
|
|
||||||
return greetService.greet();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
|
@ -1,15 +0,0 @@
|
||||||
package com.baeldung.security;
|
|
||||||
|
|
||||||
import org.springframework.security.access.prepost.PreAuthorize;
|
|
||||||
import org.springframework.stereotype.Service;
|
|
||||||
import reactor.core.publisher.Mono;
|
|
||||||
|
|
||||||
@Service
|
|
||||||
public class GreetService {
|
|
||||||
|
|
||||||
@PreAuthorize("hasRole('ADMIN')")
|
|
||||||
public Mono<String> greet() {
|
|
||||||
return Mono.just("Hello from service!");
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
|
@ -1,42 +0,0 @@
|
||||||
package com.baeldung.security;
|
|
||||||
|
|
||||||
import org.springframework.context.annotation.Bean;
|
|
||||||
import org.springframework.security.config.annotation.method.configuration.EnableReactiveMethodSecurity;
|
|
||||||
import org.springframework.security.config.annotation.web.reactive.EnableWebFluxSecurity;
|
|
||||||
import org.springframework.security.config.web.server.ServerHttpSecurity;
|
|
||||||
import org.springframework.security.core.userdetails.MapReactiveUserDetailsService;
|
|
||||||
import org.springframework.security.core.userdetails.User;
|
|
||||||
import org.springframework.security.core.userdetails.UserDetails;
|
|
||||||
import org.springframework.security.web.server.SecurityWebFilterChain;
|
|
||||||
|
|
||||||
@EnableWebFluxSecurity
|
|
||||||
@EnableReactiveMethodSecurity
|
|
||||||
public class SecurityConfig {
|
|
||||||
|
|
||||||
@Bean
|
|
||||||
public SecurityWebFilterChain securitygWebFilterChain(ServerHttpSecurity http) {
|
|
||||||
return http.authorizeExchange()
|
|
||||||
.pathMatchers("/admin").hasAuthority("ROLE_ADMIN")
|
|
||||||
.anyExchange().authenticated()
|
|
||||||
.and().formLogin()
|
|
||||||
.and().build();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Bean
|
|
||||||
public MapReactiveUserDetailsService userDetailsService() {
|
|
||||||
UserDetails user = User.withDefaultPasswordEncoder()
|
|
||||||
.username("user")
|
|
||||||
.password("password")
|
|
||||||
.roles("USER")
|
|
||||||
.build();
|
|
||||||
|
|
||||||
UserDetails admin = User.withDefaultPasswordEncoder()
|
|
||||||
.username("admin")
|
|
||||||
.password("password")
|
|
||||||
.roles("ADMIN")
|
|
||||||
.build();
|
|
||||||
|
|
||||||
return new MapReactiveUserDetailsService(user, admin);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
|
@ -1,84 +0,0 @@
|
||||||
package com.baeldung.web;
|
|
||||||
|
|
||||||
import javax.persistence.Entity;
|
|
||||||
import javax.persistence.GeneratedValue;
|
|
||||||
import javax.persistence.GenerationType;
|
|
||||||
import javax.persistence.Id;
|
|
||||||
|
|
||||||
@Entity
|
|
||||||
public class Foo {
|
|
||||||
|
|
||||||
@Id
|
|
||||||
@GeneratedValue(strategy = GenerationType.AUTO)
|
|
||||||
private long id;
|
|
||||||
|
|
||||||
private String name;
|
|
||||||
|
|
||||||
public Foo() {
|
|
||||||
super();
|
|
||||||
}
|
|
||||||
|
|
||||||
public Foo(final String name) {
|
|
||||||
super();
|
|
||||||
|
|
||||||
this.name = name;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Foo(final long id, final String name) {
|
|
||||||
super();
|
|
||||||
|
|
||||||
this.id = id;
|
|
||||||
this.name = name;
|
|
||||||
}
|
|
||||||
|
|
||||||
// API
|
|
||||||
|
|
||||||
public long getId() {
|
|
||||||
return id;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setId(final long id) {
|
|
||||||
this.id = id;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getName() {
|
|
||||||
return name;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setName(final String name) {
|
|
||||||
this.name = name;
|
|
||||||
}
|
|
||||||
|
|
||||||
//
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public int hashCode() {
|
|
||||||
final int prime = 31;
|
|
||||||
int result = 1;
|
|
||||||
result = prime * result + ((name == null) ? 0 : name.hashCode());
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean equals(Object obj) {
|
|
||||||
if (this == obj)
|
|
||||||
return true;
|
|
||||||
if (obj == null)
|
|
||||||
return false;
|
|
||||||
if (getClass() != obj.getClass())
|
|
||||||
return false;
|
|
||||||
Foo other = (Foo) obj;
|
|
||||||
if (name == null) {
|
|
||||||
if (other.name != null)
|
|
||||||
return false;
|
|
||||||
} else if (!name.equals(other.name))
|
|
||||||
return false;
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String toString() {
|
|
||||||
return "Foo [name=" + name + "]";
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
|
@ -1,53 +0,0 @@
|
||||||
package com.baeldung.web;
|
|
||||||
|
|
||||||
import com.baeldung.persistence.FooRepository;
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
|
||||||
import org.springframework.data.domain.PageRequest;
|
|
||||||
import org.springframework.http.HttpStatus;
|
|
||||||
import org.springframework.validation.annotation.Validated;
|
|
||||||
import org.springframework.web.bind.annotation.*;
|
|
||||||
|
|
||||||
import javax.validation.constraints.Max;
|
|
||||||
import javax.validation.constraints.Min;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
@RestController("/foos")
|
|
||||||
public class FooController {
|
|
||||||
|
|
||||||
@Autowired
|
|
||||||
private FooRepository repo;
|
|
||||||
|
|
||||||
// API - read
|
|
||||||
|
|
||||||
@GetMapping("/foos/{id}")
|
|
||||||
@ResponseBody
|
|
||||||
@Validated
|
|
||||||
public Foo findById(@PathVariable @Min(0) final long id) {
|
|
||||||
return repo.findById(id)
|
|
||||||
.orElse(null);
|
|
||||||
}
|
|
||||||
|
|
||||||
@GetMapping
|
|
||||||
@ResponseBody
|
|
||||||
public List<Foo> findAll() {
|
|
||||||
return repo.findAll();
|
|
||||||
}
|
|
||||||
|
|
||||||
@GetMapping(params = { "page", "size" })
|
|
||||||
@ResponseBody
|
|
||||||
@Validated
|
|
||||||
public List<Foo> findPaginated(@RequestParam("page") @Min(0) final int page, @Max(100) @RequestParam("size") final int size) {
|
|
||||||
return repo.findAll(PageRequest.of(page, size))
|
|
||||||
.getContent();
|
|
||||||
}
|
|
||||||
|
|
||||||
// API - write
|
|
||||||
|
|
||||||
@PutMapping("/foos/{id}")
|
|
||||||
@ResponseStatus(HttpStatus.OK)
|
|
||||||
@ResponseBody
|
|
||||||
public Foo update(@PathVariable("id") final String id, @RequestBody final Foo foo) {
|
|
||||||
return foo;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
|
@ -1,39 +0,0 @@
|
||||||
package com.baeldung.web;
|
|
||||||
|
|
||||||
import org.springframework.web.bind.annotation.GetMapping;
|
|
||||||
import org.springframework.web.bind.annotation.PathVariable;
|
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
|
||||||
|
|
||||||
@RestController
|
|
||||||
public class PathPatternController {
|
|
||||||
|
|
||||||
@GetMapping("/spring5/{*id}")
|
|
||||||
public String URIVariableHandler(@PathVariable String id) {
|
|
||||||
return id;
|
|
||||||
}
|
|
||||||
|
|
||||||
@GetMapping("/s?ring5")
|
|
||||||
public String wildcardTakingExactlyOneChar() {
|
|
||||||
return "/s?ring5";
|
|
||||||
}
|
|
||||||
|
|
||||||
@GetMapping("/spring5/*id")
|
|
||||||
public String wildcardTakingZeroOrMoreChar() {
|
|
||||||
return "/spring5/*id";
|
|
||||||
}
|
|
||||||
|
|
||||||
@GetMapping("/resources/**")
|
|
||||||
public String wildcardTakingZeroOrMorePathSegments() {
|
|
||||||
return "/resources/**";
|
|
||||||
}
|
|
||||||
|
|
||||||
@GetMapping("/{baeldung:[a-z]+}")
|
|
||||||
public String regexInPathVariable(@PathVariable String baeldung) {
|
|
||||||
return baeldung;
|
|
||||||
}
|
|
||||||
|
|
||||||
@GetMapping("/{var1}_{var2}")
|
|
||||||
public String multiplePathVariablesInSameSegment(@PathVariable String var1, @PathVariable String var2) {
|
|
||||||
return "Two variables are var1=" + var1 + " and var2=" + var2;
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,28 +0,0 @@
|
||||||
package com.baeldung.web.reactive;
|
|
||||||
|
|
||||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
|
||||||
|
|
||||||
public class Task {
|
|
||||||
|
|
||||||
private final String name;
|
|
||||||
|
|
||||||
private final int id;
|
|
||||||
|
|
||||||
public Task(@JsonProperty("name") String name, @JsonProperty("id") int id) {
|
|
||||||
this.name = name;
|
|
||||||
this.id = id;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getName() {
|
|
||||||
return this.name;
|
|
||||||
}
|
|
||||||
|
|
||||||
public int getId() {
|
|
||||||
return this.id;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String toString() {
|
|
||||||
return "Task{" + "name='" + name + '\'' + ", id=" + id + '}';
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,85 +0,0 @@
|
||||||
package com.baeldung.web.reactive.client;
|
|
||||||
|
|
||||||
import org.reactivestreams.Publisher;
|
|
||||||
import org.reactivestreams.Subscriber;
|
|
||||||
import org.springframework.http.*;
|
|
||||||
import org.springframework.http.client.reactive.ClientHttpRequest;
|
|
||||||
import org.springframework.util.LinkedMultiValueMap;
|
|
||||||
import org.springframework.util.MultiValueMap;
|
|
||||||
import org.springframework.web.bind.annotation.GetMapping;
|
|
||||||
import org.springframework.web.bind.annotation.ResponseStatus;
|
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
|
||||||
import org.springframework.web.reactive.function.BodyInserter;
|
|
||||||
import org.springframework.web.reactive.function.BodyInserters;
|
|
||||||
import org.springframework.web.reactive.function.client.WebClient;
|
|
||||||
import reactor.core.publisher.Mono;
|
|
||||||
|
|
||||||
import java.net.URI;
|
|
||||||
import java.nio.charset.Charset;
|
|
||||||
import java.time.ZonedDateTime;
|
|
||||||
import java.util.Collections;
|
|
||||||
|
|
||||||
@RestController
|
|
||||||
public class WebClientController {
|
|
||||||
|
|
||||||
@ResponseStatus(HttpStatus.OK)
|
|
||||||
@GetMapping("/resource")
|
|
||||||
public void getResource() {
|
|
||||||
}
|
|
||||||
|
|
||||||
public void demonstrateWebClient() {
|
|
||||||
// request
|
|
||||||
WebClient.UriSpec<WebClient.RequestBodySpec> request1 = createWebClientWithServerURLAndDefaultValues().method(HttpMethod.POST);
|
|
||||||
WebClient.UriSpec<WebClient.RequestBodySpec> request2 = createWebClientWithServerURLAndDefaultValues().post();
|
|
||||||
|
|
||||||
// request body specifications
|
|
||||||
WebClient.RequestBodySpec uri1 = createWebClientWithServerURLAndDefaultValues().method(HttpMethod.POST)
|
|
||||||
.uri("/resource");
|
|
||||||
WebClient.RequestBodySpec uri2 = createWebClientWithServerURLAndDefaultValues().post()
|
|
||||||
.uri(URI.create("/resource"));
|
|
||||||
|
|
||||||
// request header specification
|
|
||||||
WebClient.RequestHeadersSpec<?> requestSpec1 = uri1.body(BodyInserters.fromPublisher(Mono.just("data"), String.class));
|
|
||||||
WebClient.RequestHeadersSpec<?> requestSpec2 = uri2.body(BodyInserters.fromObject("data"));
|
|
||||||
|
|
||||||
// inserters
|
|
||||||
BodyInserter<Publisher<String>, ReactiveHttpOutputMessage> inserter1 = BodyInserters
|
|
||||||
.fromPublisher(Subscriber::onComplete, String.class);
|
|
||||||
|
|
||||||
LinkedMultiValueMap<String, String> map = new LinkedMultiValueMap<>();
|
|
||||||
map.add("key1", "value1");
|
|
||||||
map.add("key2", "value2");
|
|
||||||
|
|
||||||
BodyInserter<MultiValueMap<String, ?>, ClientHttpRequest> inserter2 = BodyInserters.fromMultipartData(map);
|
|
||||||
BodyInserter<String, ReactiveHttpOutputMessage> inserter3 = BodyInserters.fromObject("body");
|
|
||||||
|
|
||||||
// responses
|
|
||||||
WebClient.ResponseSpec response1 = uri1.body(inserter3)
|
|
||||||
.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
|
|
||||||
.accept(MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML)
|
|
||||||
.acceptCharset(Charset.forName("UTF-8"))
|
|
||||||
.ifNoneMatch("*")
|
|
||||||
.ifModifiedSince(ZonedDateTime.now())
|
|
||||||
.retrieve();
|
|
||||||
WebClient.ResponseSpec response2 = requestSpec2.retrieve();
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
private WebClient createWebClient() {
|
|
||||||
return WebClient.create();
|
|
||||||
}
|
|
||||||
|
|
||||||
private WebClient createWebClientWithServerURL() {
|
|
||||||
return WebClient.create("http://localhost:8081");
|
|
||||||
}
|
|
||||||
|
|
||||||
private WebClient createWebClientWithServerURLAndDefaultValues() {
|
|
||||||
return WebClient.builder()
|
|
||||||
.baseUrl("http://localhost:8081")
|
|
||||||
.defaultCookie("cookieKey", "cookieValue")
|
|
||||||
.defaultHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
|
|
||||||
.defaultUriVariables(Collections.singletonMap("url", "http://localhost:8080"))
|
|
||||||
.build();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
|
@ -1,3 +1 @@
|
||||||
server.port=8081
|
|
||||||
|
|
||||||
logging.level.root=INFO
|
logging.level.root=INFO
|
|
@ -1,29 +0,0 @@
|
||||||
package com.baeldung;
|
|
||||||
|
|
||||||
import org.junit.Test;
|
|
||||||
import org.junit.runner.RunWith;
|
|
||||||
import org.springframework.boot.test.context.SpringBootTest;
|
|
||||||
import org.springframework.test.context.junit4.SpringRunner;
|
|
||||||
|
|
||||||
@RunWith(SpringRunner.class)
|
|
||||||
@SpringBootTest
|
|
||||||
public class Example1IntegrationTest {
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void test1a() {
|
|
||||||
block(3000);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void test1b() {
|
|
||||||
block(3000);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void block(long ms) {
|
|
||||||
try {
|
|
||||||
Thread.sleep(ms);
|
|
||||||
} catch (InterruptedException e) {
|
|
||||||
System.out.println("Thread interrupted");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,29 +0,0 @@
|
||||||
package com.baeldung;
|
|
||||||
|
|
||||||
import org.junit.Test;
|
|
||||||
import org.junit.runner.RunWith;
|
|
||||||
import org.springframework.boot.test.context.SpringBootTest;
|
|
||||||
import org.springframework.test.context.junit4.SpringRunner;
|
|
||||||
|
|
||||||
@RunWith(SpringRunner.class)
|
|
||||||
@SpringBootTest
|
|
||||||
public class Example2IntegrationTest {
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void test1a() {
|
|
||||||
block(3000);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void test1b() {
|
|
||||||
block(3000);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void block(long ms) {
|
|
||||||
try {
|
|
||||||
Thread.sleep(ms);
|
|
||||||
} catch (InterruptedException e) {
|
|
||||||
System.out.println("Thread Interrupted");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,24 +0,0 @@
|
||||||
package com.baeldung;
|
|
||||||
|
|
||||||
import org.junit.Test;
|
|
||||||
import org.junit.experimental.ParallelComputer;
|
|
||||||
import org.junit.runner.Computer;
|
|
||||||
import org.junit.runner.JUnitCore;
|
|
||||||
|
|
||||||
public class ParallelIntegrationTest {
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void runTests() {
|
|
||||||
final Class<?>[] classes = { Example1IntegrationTest.class, Example2IntegrationTest.class };
|
|
||||||
|
|
||||||
JUnitCore.runClasses(new Computer(), classes);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void runTestsInParallel() {
|
|
||||||
final Class<?>[] classes = { Example1IntegrationTest.class, Example2IntegrationTest.class };
|
|
||||||
|
|
||||||
JUnitCore.runClasses(new ParallelComputer(true, true), classes);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
|
@ -1,16 +0,0 @@
|
||||||
package com.baeldung;
|
|
||||||
|
|
||||||
import org.junit.Test;
|
|
||||||
import org.junit.runner.RunWith;
|
|
||||||
import org.springframework.boot.test.context.SpringBootTest;
|
|
||||||
import org.springframework.test.context.junit4.SpringRunner;
|
|
||||||
|
|
||||||
@RunWith(SpringRunner.class)
|
|
||||||
@SpringBootTest
|
|
||||||
public class Spring5ApplicationIntegrationTest {
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void contextLoads() {
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
|
@ -1,52 +0,0 @@
|
||||||
package com.baeldung;
|
|
||||||
|
|
||||||
import org.junit.Test;
|
|
||||||
import org.junit.runner.RunWith;
|
|
||||||
import org.springframework.beans.BeansException;
|
|
||||||
import org.springframework.beans.factory.InitializingBean;
|
|
||||||
import org.springframework.context.ApplicationContext;
|
|
||||||
import org.springframework.context.ApplicationContextAware;
|
|
||||||
import org.springframework.context.annotation.Configuration;
|
|
||||||
import org.springframework.test.context.ContextConfiguration;
|
|
||||||
import org.springframework.test.context.junit4.SpringRunner;
|
|
||||||
|
|
||||||
import java.util.concurrent.TimeUnit;
|
|
||||||
|
|
||||||
import static org.junit.Assert.assertNotNull;
|
|
||||||
import static org.junit.Assert.assertTrue;
|
|
||||||
|
|
||||||
@RunWith(SpringRunner.class)
|
|
||||||
@ContextConfiguration(classes = Spring5JUnit4ConcurrentIntegrationTest.SimpleConfiguration.class)
|
|
||||||
public class Spring5JUnit4ConcurrentIntegrationTest implements ApplicationContextAware, InitializingBean {
|
|
||||||
|
|
||||||
@Configuration
|
|
||||||
public static class SimpleConfiguration {
|
|
||||||
}
|
|
||||||
|
|
||||||
private ApplicationContext applicationContext;
|
|
||||||
|
|
||||||
private boolean beanInitialized = false;
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public final void afterPropertiesSet() throws Exception {
|
|
||||||
this.beanInitialized = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public final void setApplicationContext(final ApplicationContext applicationContext) throws BeansException {
|
|
||||||
this.applicationContext = applicationContext;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public final void verifyApplicationContextSet() throws InterruptedException {
|
|
||||||
TimeUnit.SECONDS.sleep(2);
|
|
||||||
assertNotNull("The application context should have been set due to ApplicationContextAware semantics.", this.applicationContext);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public final void verifyBeanInitialized() throws InterruptedException {
|
|
||||||
TimeUnit.SECONDS.sleep(2);
|
|
||||||
assertTrue("This test bean should have been initialized due to InitializingBean semantics.", this.beanInitialized);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
|
@ -1,42 +0,0 @@
|
||||||
package com.baeldung.functional;
|
|
||||||
|
|
||||||
import static org.junit.Assert.*;
|
|
||||||
|
|
||||||
import org.junit.Test;
|
|
||||||
import org.junit.runner.RunWith;
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
|
||||||
import org.springframework.boot.test.context.SpringBootTest;
|
|
||||||
import org.springframework.test.context.junit4.SpringRunner;
|
|
||||||
import org.springframework.web.context.support.GenericWebApplicationContext;
|
|
||||||
|
|
||||||
import com.baeldung.Spring5Application;
|
|
||||||
|
|
||||||
@RunWith(SpringRunner.class)
|
|
||||||
@SpringBootTest(classes = Spring5Application.class)
|
|
||||||
public class BeanRegistrationTest {
|
|
||||||
|
|
||||||
@Autowired
|
|
||||||
private GenericWebApplicationContext context;
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void whenRegisterBean_thenOk() {
|
|
||||||
context.registerBean(MyService.class, () -> new MyService());
|
|
||||||
MyService myService = (MyService) context.getBean("com.baeldung.functional.MyService");
|
|
||||||
assertTrue(myService.getRandomNumber() < 10);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void whenRegisterBeanWithName_thenOk() {
|
|
||||||
context.registerBean("mySecondService", MyService.class, () -> new MyService());
|
|
||||||
MyService mySecondService = (MyService) context.getBean("mySecondService");
|
|
||||||
assertTrue(mySecondService.getRandomNumber() < 10);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void whenRegisterBeanWithCallback_thenOk() {
|
|
||||||
context.registerBean("myCallbackService", MyService.class, () -> new MyService(), bd -> bd.setAutowireCandidate(false));
|
|
||||||
MyService myCallbackService = (MyService) context.getBean("myCallbackService");
|
|
||||||
assertTrue(myCallbackService.getRandomNumber() < 10);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
|
@ -1,110 +0,0 @@
|
||||||
package com.baeldung.functional;
|
|
||||||
|
|
||||||
import org.junit.AfterClass;
|
|
||||||
import org.junit.BeforeClass;
|
|
||||||
import org.junit.Test;
|
|
||||||
import org.springframework.boot.web.server.WebServer;
|
|
||||||
import org.springframework.test.web.reactive.server.WebTestClient;
|
|
||||||
|
|
||||||
public class ExploreSpring5URLPatternUsingRouterFunctionsTest {
|
|
||||||
|
|
||||||
private static WebTestClient client;
|
|
||||||
private static WebServer server;
|
|
||||||
|
|
||||||
@BeforeClass
|
|
||||||
public static void setup() throws Exception {
|
|
||||||
server = new ExploreSpring5URLPatternUsingRouterFunctions().start();
|
|
||||||
client = WebTestClient.bindToServer()
|
|
||||||
.baseUrl("http://localhost:" + server.getPort())
|
|
||||||
.build();
|
|
||||||
}
|
|
||||||
|
|
||||||
@AfterClass
|
|
||||||
public static void destroy() {
|
|
||||||
server.stop();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void givenRouter_whenGetPathWithSingleCharWildcard_thenGotPathPattern() throws Exception {
|
|
||||||
client.get()
|
|
||||||
.uri("/paths")
|
|
||||||
.exchange()
|
|
||||||
.expectStatus()
|
|
||||||
.isOk()
|
|
||||||
.expectBody(String.class)
|
|
||||||
.isEqualTo("/p?ths");
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void givenRouter_whenMultipleURIVariablePattern_thenGotPathVariable() throws Exception {
|
|
||||||
client.get()
|
|
||||||
.uri("/test/ab/cd")
|
|
||||||
.exchange()
|
|
||||||
.expectStatus()
|
|
||||||
.isOk()
|
|
||||||
.expectBody(String.class)
|
|
||||||
.isEqualTo("/ab/cd");
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void givenRouter_whenGetMultipleCharWildcard_thenGotPathPattern() throws Exception {
|
|
||||||
|
|
||||||
client.get()
|
|
||||||
.uri("/wildcard")
|
|
||||||
.exchange()
|
|
||||||
.expectStatus()
|
|
||||||
.isOk()
|
|
||||||
.expectBody(String.class)
|
|
||||||
.isEqualTo("/*card path was accessed");
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void givenRouter_whenGetMultiplePathVaribleInSameSegment_thenGotPathVariables() throws Exception {
|
|
||||||
|
|
||||||
client.get()
|
|
||||||
.uri("/baeldung_tutorial")
|
|
||||||
.exchange()
|
|
||||||
.expectStatus()
|
|
||||||
.isOk()
|
|
||||||
.expectBody(String.class)
|
|
||||||
.isEqualTo("baeldung , tutorial");
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void givenRouter_whenGetRegexInPathVarible_thenGotPathVariable() throws Exception {
|
|
||||||
|
|
||||||
client.get()
|
|
||||||
.uri("/abcd")
|
|
||||||
.exchange()
|
|
||||||
.expectStatus()
|
|
||||||
.isOk()
|
|
||||||
.expectBody(String.class)
|
|
||||||
.isEqualTo("/{baeldung:[a-z]+} was accessed and baeldung=abcd");
|
|
||||||
|
|
||||||
client.get()
|
|
||||||
.uri("/1234")
|
|
||||||
.exchange()
|
|
||||||
.expectStatus()
|
|
||||||
.is4xxClientError();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void givenResources_whenAccess_thenGot() throws Exception {
|
|
||||||
client.get()
|
|
||||||
.uri("/files/test/test.txt")
|
|
||||||
.exchange()
|
|
||||||
.expectStatus()
|
|
||||||
.isOk()
|
|
||||||
.expectBody(String.class)
|
|
||||||
.isEqualTo("test");
|
|
||||||
|
|
||||||
client.get()
|
|
||||||
.uri("/files/hello.txt")
|
|
||||||
.exchange()
|
|
||||||
.expectStatus()
|
|
||||||
.isOk()
|
|
||||||
.expectBody(String.class)
|
|
||||||
.isEqualTo("hello");
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
|
@ -1,141 +0,0 @@
|
||||||
package com.baeldung.functional;
|
|
||||||
|
|
||||||
import org.junit.AfterClass;
|
|
||||||
import org.junit.BeforeClass;
|
|
||||||
import org.junit.Test;
|
|
||||||
import org.springframework.boot.web.server.WebServer;
|
|
||||||
import org.springframework.core.io.ClassPathResource;
|
|
||||||
import org.springframework.core.io.Resource;
|
|
||||||
import org.springframework.http.MediaType;
|
|
||||||
import org.springframework.test.web.reactive.server.WebTestClient;
|
|
||||||
import org.springframework.util.LinkedMultiValueMap;
|
|
||||||
import org.springframework.util.MultiValueMap;
|
|
||||||
import org.springframework.web.reactive.function.BodyInserters;
|
|
||||||
|
|
||||||
import static org.springframework.web.reactive.function.BodyInserters.fromObject;
|
|
||||||
import static org.springframework.web.reactive.function.BodyInserters.fromResource;
|
|
||||||
|
|
||||||
public class FunctionalWebApplicationIntegrationTest {
|
|
||||||
|
|
||||||
private static WebTestClient client;
|
|
||||||
private static WebServer server;
|
|
||||||
|
|
||||||
@BeforeClass
|
|
||||||
public static void setup() throws Exception {
|
|
||||||
server = new FunctionalWebApplication().start();
|
|
||||||
client = WebTestClient.bindToServer()
|
|
||||||
.baseUrl("http://localhost:" + server.getPort())
|
|
||||||
.build();
|
|
||||||
}
|
|
||||||
|
|
||||||
@AfterClass
|
|
||||||
public static void destroy() {
|
|
||||||
server.stop();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void givenRouter_whenGetTest_thenGotHelloWorld() throws Exception {
|
|
||||||
client.get()
|
|
||||||
.uri("/test")
|
|
||||||
.exchange()
|
|
||||||
.expectStatus()
|
|
||||||
.isOk()
|
|
||||||
.expectBody(String.class)
|
|
||||||
.isEqualTo("helloworld");
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void givenIndexFilter_whenRequestRoot_thenRewrittenToTest() throws Exception {
|
|
||||||
client.get()
|
|
||||||
.uri("/")
|
|
||||||
.exchange()
|
|
||||||
.expectStatus()
|
|
||||||
.isOk()
|
|
||||||
.expectBody(String.class)
|
|
||||||
.isEqualTo("helloworld");
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void givenLoginForm_whenPostValidToken_thenSuccess() throws Exception {
|
|
||||||
MultiValueMap<String, String> formData = new LinkedMultiValueMap<>(1);
|
|
||||||
formData.add("user", "baeldung");
|
|
||||||
formData.add("token", "you_know_what_to_do");
|
|
||||||
|
|
||||||
client.post()
|
|
||||||
.uri("/login")
|
|
||||||
.contentType(MediaType.APPLICATION_FORM_URLENCODED)
|
|
||||||
.body(BodyInserters.fromFormData(formData))
|
|
||||||
.exchange()
|
|
||||||
.expectStatus()
|
|
||||||
.isOk()
|
|
||||||
.expectBody(String.class)
|
|
||||||
.isEqualTo("welcome back!");
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void givenLoginForm_whenRequestWithInvalidToken_thenFail() throws Exception {
|
|
||||||
MultiValueMap<String, String> formData = new LinkedMultiValueMap<>(2);
|
|
||||||
formData.add("user", "baeldung");
|
|
||||||
formData.add("token", "try_again");
|
|
||||||
|
|
||||||
client.post()
|
|
||||||
.uri("/login")
|
|
||||||
.contentType(MediaType.APPLICATION_FORM_URLENCODED)
|
|
||||||
.body(BodyInserters.fromFormData(formData))
|
|
||||||
.exchange()
|
|
||||||
.expectStatus()
|
|
||||||
.isBadRequest();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void givenUploadForm_whenRequestWithMultipartData_thenSuccess() throws Exception {
|
|
||||||
Resource resource = new ClassPathResource("/baeldung-weekly.png");
|
|
||||||
client.post()
|
|
||||||
.uri("/upload")
|
|
||||||
.contentType(MediaType.MULTIPART_FORM_DATA)
|
|
||||||
.body(fromResource(resource))
|
|
||||||
.exchange()
|
|
||||||
.expectStatus()
|
|
||||||
.isOk()
|
|
||||||
.expectBody(String.class)
|
|
||||||
.isEqualTo(String.valueOf(resource.contentLength()));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void givenActors_whenAddActor_thenAdded() throws Exception {
|
|
||||||
client.get()
|
|
||||||
.uri("/actor")
|
|
||||||
.exchange()
|
|
||||||
.expectStatus()
|
|
||||||
.isOk()
|
|
||||||
.expectBodyList(Actor.class)
|
|
||||||
.hasSize(2);
|
|
||||||
|
|
||||||
client.post()
|
|
||||||
.uri("/actor")
|
|
||||||
.body(fromObject(new Actor("Clint", "Eastwood")))
|
|
||||||
.exchange()
|
|
||||||
.expectStatus()
|
|
||||||
.isOk();
|
|
||||||
|
|
||||||
client.get()
|
|
||||||
.uri("/actor")
|
|
||||||
.exchange()
|
|
||||||
.expectStatus()
|
|
||||||
.isOk()
|
|
||||||
.expectBodyList(Actor.class)
|
|
||||||
.hasSize(3);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void givenResources_whenAccess_thenGot() throws Exception {
|
|
||||||
client.get()
|
|
||||||
.uri("/files/hello.txt")
|
|
||||||
.exchange()
|
|
||||||
.expectStatus()
|
|
||||||
.isOk()
|
|
||||||
.expectBody(String.class)
|
|
||||||
.isEqualTo("hello");
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
|
@ -1,43 +0,0 @@
|
||||||
package com.baeldung.jsonb;
|
|
||||||
|
|
||||||
import static org.junit.Assert.assertTrue;
|
|
||||||
|
|
||||||
import java.math.BigDecimal;
|
|
||||||
import java.time.LocalDate;
|
|
||||||
|
|
||||||
import org.junit.Test;
|
|
||||||
import org.junit.runner.RunWith;
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
|
||||||
import org.springframework.beans.factory.annotation.Value;
|
|
||||||
import org.springframework.boot.test.context.SpringBootTest;
|
|
||||||
import org.springframework.boot.test.web.client.TestRestTemplate;
|
|
||||||
import org.springframework.http.ResponseEntity;
|
|
||||||
import org.springframework.test.context.junit4.SpringRunner;
|
|
||||||
|
|
||||||
@RunWith(SpringRunner.class)
|
|
||||||
@SpringBootTest(classes = Spring5Application.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
|
|
||||||
public class JsonbIntegrationTest {
|
|
||||||
@Value("${security.user.name}")
|
|
||||||
private String username;
|
|
||||||
@Value("${security.user.password}")
|
|
||||||
private String password;
|
|
||||||
|
|
||||||
@Autowired
|
|
||||||
private TestRestTemplate template;
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void givenId_whenUriIsPerson_thenGetPerson() {
|
|
||||||
ResponseEntity<Person> response = template.withBasicAuth(username, password)
|
|
||||||
.getForEntity("/person/1", Person.class);
|
|
||||||
Person person = response.getBody();
|
|
||||||
assertTrue(person.equals(new Person(2, "Jhon", "jhon1@test.com", 0, LocalDate.of(2019, 9, 9), BigDecimal.valueOf(1500.0))));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void whenSendPostAPerson_thenGetOkStatus() {
|
|
||||||
ResponseEntity<Boolean> response = template.withBasicAuth(username, password)
|
|
||||||
.postForEntity("/person", "{\"birthDate\":\"07-09-2017\",\"email\":\"jhon1@test.com\",\"person-name\":\"Jhon\",\"id\":10}", Boolean.class);
|
|
||||||
assertTrue(response.getBody());
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
|
@ -1,18 +0,0 @@
|
||||||
package com.baeldung.jupiter;
|
|
||||||
|
|
||||||
import java.lang.annotation.ElementType;
|
|
||||||
import java.lang.annotation.Retention;
|
|
||||||
import java.lang.annotation.RetentionPolicy;
|
|
||||||
import java.lang.annotation.Target;
|
|
||||||
|
|
||||||
import org.springframework.test.context.junit.jupiter.EnabledIf;
|
|
||||||
|
|
||||||
@Target({ ElementType.TYPE, ElementType.METHOD })
|
|
||||||
@Retention(RetentionPolicy.RUNTIME)
|
|
||||||
@EnabledIf(
|
|
||||||
expression = "#{systemProperties['java.version'].startsWith('1.8')}",
|
|
||||||
reason = "Enabled on Java 8"
|
|
||||||
)
|
|
||||||
public @interface EnabledOnJava8 {
|
|
||||||
|
|
||||||
}
|
|
|
@ -1,50 +0,0 @@
|
||||||
package com.baeldung.jupiter;
|
|
||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
|
||||||
|
|
||||||
import org.junit.jupiter.api.Test;
|
|
||||||
import org.springframework.context.annotation.Configuration;
|
|
||||||
import org.springframework.test.context.TestPropertySource;
|
|
||||||
import org.springframework.test.context.junit.jupiter.DisabledIf;
|
|
||||||
import org.springframework.test.context.junit.jupiter.EnabledIf;
|
|
||||||
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
|
|
||||||
|
|
||||||
@SpringJUnitConfig(Spring5EnabledAnnotationTest.Config.class)
|
|
||||||
@TestPropertySource(properties = { "tests.enabled=true" })
|
|
||||||
public class Spring5EnabledAnnotationTest {
|
|
||||||
|
|
||||||
@Configuration
|
|
||||||
static class Config {
|
|
||||||
}
|
|
||||||
|
|
||||||
@EnabledIf("true")
|
|
||||||
@Test
|
|
||||||
void givenEnabledIfLiteral_WhenTrue_ThenTestExecuted() {
|
|
||||||
assertTrue(true);
|
|
||||||
}
|
|
||||||
|
|
||||||
@EnabledIf(expression = "${tests.enabled}", loadContext = true)
|
|
||||||
@Test
|
|
||||||
void givenEnabledIfExpression_WhenTrue_ThenTestExecuted() {
|
|
||||||
assertTrue(true);
|
|
||||||
}
|
|
||||||
|
|
||||||
@EnabledIf("#{systemProperties['java.version'].startsWith('1.8')}")
|
|
||||||
@Test
|
|
||||||
void givenEnabledIfSpel_WhenTrue_ThenTestExecuted() {
|
|
||||||
assertTrue(true);
|
|
||||||
}
|
|
||||||
|
|
||||||
@EnabledOnJava8
|
|
||||||
@Test
|
|
||||||
void givenEnabledOnJava8_WhenTrue_ThenTestExecuted() {
|
|
||||||
assertTrue(true);
|
|
||||||
}
|
|
||||||
|
|
||||||
@DisabledIf("#{systemProperties['java.version'].startsWith('1.7')}")
|
|
||||||
@Test
|
|
||||||
void givenDisabledIf_WhenTrue_ThenTestNotExecuted() {
|
|
||||||
assertTrue(true);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
|
@ -1,38 +0,0 @@
|
||||||
package com.baeldung.jupiter;
|
|
||||||
|
|
||||||
import com.baeldung.web.reactive.Task;
|
|
||||||
import org.junit.jupiter.api.DisplayName;
|
|
||||||
import org.junit.jupiter.api.Test;
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
|
||||||
import org.springframework.context.ApplicationContext;
|
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
|
||||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
|
||||||
|
|
||||||
@SpringJUnit5Config(TestConfig.class)
|
|
||||||
@DisplayName("@SpringJUnit5Config Tests")
|
|
||||||
class Spring5JUnit5ComposedAnnotationIntegrationTest {
|
|
||||||
|
|
||||||
@Autowired
|
|
||||||
private Task task;
|
|
||||||
|
|
||||||
@Autowired
|
|
||||||
private List<Task> tasks;
|
|
||||||
|
|
||||||
@Test
|
|
||||||
@DisplayName("ApplicationContext injected into method")
|
|
||||||
void givenAMethodName_whenInjecting_thenApplicationContextInjectedIntoMethod(ApplicationContext applicationContext) {
|
|
||||||
assertNotNull(applicationContext, "ApplicationContext should have been injected into method by Spring");
|
|
||||||
assertEquals(this.task, applicationContext.getBean("taskName", Task.class));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
@DisplayName("Spring @Beans injected into fields")
|
|
||||||
void givenAnObject_whenInjecting_thenSpringBeansInjected() {
|
|
||||||
assertNotNull(task, "Task should have been @Autowired by Spring");
|
|
||||||
assertEquals("taskName", task.getName(), "Task's name");
|
|
||||||
assertEquals(1, tasks.size(), "Number of Tasks in context");
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,25 +0,0 @@
|
||||||
package com.baeldung.jupiter;
|
|
||||||
|
|
||||||
import com.baeldung.web.reactive.Task;
|
|
||||||
import org.junit.jupiter.api.Test;
|
|
||||||
import org.junit.jupiter.api.extension.ExtendWith;
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
|
||||||
import org.springframework.context.ApplicationContext;
|
|
||||||
import org.springframework.test.context.ContextConfiguration;
|
|
||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
|
||||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
|
||||||
|
|
||||||
@ExtendWith(SpringExtension.class)
|
|
||||||
@ContextConfiguration(classes = TestConfig.class)
|
|
||||||
class Spring5JUnit5IntegrationTest {
|
|
||||||
|
|
||||||
@Autowired
|
|
||||||
private Task task;
|
|
||||||
|
|
||||||
@Test
|
|
||||||
void givenAMethodName_whenInjecting_thenApplicationContextInjectedIntoMetho(ApplicationContext applicationContext) {
|
|
||||||
assertNotNull(applicationContext, "ApplicationContext should have been injected by Spring");
|
|
||||||
assertEquals(this.task, applicationContext.getBean("taskName", Task.class));
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,25 +0,0 @@
|
||||||
package com.baeldung.jupiter;
|
|
||||||
|
|
||||||
import com.baeldung.Example1IntegrationTest;
|
|
||||||
import com.baeldung.Example2IntegrationTest;
|
|
||||||
import org.junit.experimental.ParallelComputer;
|
|
||||||
import org.junit.jupiter.api.Test;
|
|
||||||
import org.junit.runner.Computer;
|
|
||||||
import org.junit.runner.JUnitCore;
|
|
||||||
|
|
||||||
class Spring5JUnit5ParallelIntegrationTest {
|
|
||||||
|
|
||||||
@Test
|
|
||||||
void givenTwoTestClasses_whenJUnitRunParallel_thenTheTestsExecutingParallel() {
|
|
||||||
final Class<?>[] classes = { Example1IntegrationTest.class, Example2IntegrationTest.class };
|
|
||||||
|
|
||||||
JUnitCore.runClasses(new ParallelComputer(true, true), classes);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
void givenTwoTestClasses_whenJUnitRunParallel_thenTheTestsExecutingLinear() {
|
|
||||||
final Class<?>[] classes = { Example1IntegrationTest.class, Example2IntegrationTest.class };
|
|
||||||
|
|
||||||
JUnitCore.runClasses(new Computer(), classes);
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,31 +0,0 @@
|
||||||
package com.baeldung.jupiter;
|
|
||||||
|
|
||||||
import org.junit.jupiter.api.Test;
|
|
||||||
|
|
||||||
import java.util.function.Supplier;
|
|
||||||
import java.util.regex.Pattern;
|
|
||||||
import java.util.stream.Collectors;
|
|
||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
|
||||||
|
|
||||||
class Spring5Java8NewFeaturesIntegrationTest {
|
|
||||||
|
|
||||||
@FunctionalInterface
|
|
||||||
public interface FunctionalInterfaceExample<Input, Result> {
|
|
||||||
Result reverseString(Input input);
|
|
||||||
}
|
|
||||||
|
|
||||||
public class StringUtils {
|
|
||||||
FunctionalInterfaceExample<String, String> functionLambdaString = s -> Pattern.compile(" +")
|
|
||||||
.splitAsStream(s)
|
|
||||||
.map(word -> new StringBuilder(word).reverse())
|
|
||||||
.collect(Collectors.joining(" "));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
void givenStringUtil_whenSupplierCall_thenFunctionalInterfaceReverseString() throws Exception {
|
|
||||||
Supplier<StringUtils> stringUtilsSupplier = StringUtils::new;
|
|
||||||
|
|
||||||
assertEquals(stringUtilsSupplier.get().functionLambdaString.reverseString("hello"), "olleh");
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,94 +0,0 @@
|
||||||
package com.baeldung.jupiter;
|
|
||||||
|
|
||||||
import com.baeldung.web.reactive.Task;
|
|
||||||
import org.junit.jupiter.api.AfterAll;
|
|
||||||
import org.junit.jupiter.api.BeforeAll;
|
|
||||||
import org.springframework.http.server.reactive.HttpHandler;
|
|
||||||
import org.springframework.http.server.reactive.ReactorHttpHandlerAdapter;
|
|
||||||
import org.springframework.web.reactive.function.server.RouterFunction;
|
|
||||||
import org.springframework.web.reactive.function.server.RouterFunctions;
|
|
||||||
import org.springframework.web.reactive.function.server.ServerResponse;
|
|
||||||
import reactor.core.publisher.Flux;
|
|
||||||
import reactor.core.publisher.Mono;
|
|
||||||
import reactor.ipc.netty.NettyContext;
|
|
||||||
import reactor.ipc.netty.http.server.HttpServer;
|
|
||||||
|
|
||||||
import java.time.Duration;
|
|
||||||
|
|
||||||
import static org.springframework.web.reactive.function.server.RequestPredicates.GET;
|
|
||||||
import static org.springframework.web.reactive.function.server.RequestPredicates.POST;
|
|
||||||
|
|
||||||
public class Spring5ReactiveServerClientIntegrationTest {
|
|
||||||
|
|
||||||
private static NettyContext nettyContext;
|
|
||||||
|
|
||||||
@BeforeAll
|
|
||||||
public static void setUp() throws Exception {
|
|
||||||
HttpServer server = HttpServer.create("localhost", 8080);
|
|
||||||
RouterFunction<?> route = RouterFunctions.route(POST("/task/process"), request -> ServerResponse.ok()
|
|
||||||
.body(request.bodyToFlux(Task.class)
|
|
||||||
.map(ll -> new Task("TaskName", 1)), Task.class))
|
|
||||||
.and(RouterFunctions.route(GET("/task"), request -> ServerResponse.ok()
|
|
||||||
.body(Mono.just("server is alive"), String.class)));
|
|
||||||
HttpHandler httpHandler = RouterFunctions.toHttpHandler(route);
|
|
||||||
ReactorHttpHandlerAdapter adapter = new ReactorHttpHandlerAdapter(httpHandler);
|
|
||||||
nettyContext = server.newHandler(adapter)
|
|
||||||
.block();
|
|
||||||
}
|
|
||||||
|
|
||||||
@AfterAll
|
|
||||||
public static void shutDown() {
|
|
||||||
nettyContext.dispose();
|
|
||||||
}
|
|
||||||
|
|
||||||
// @Test
|
|
||||||
// public void givenCheckTask_whenServerHandle_thenServerResponseALiveString() throws Exception {
|
|
||||||
// WebClient client = WebClient.create("http://localhost:8080");
|
|
||||||
// Mono<String> result = client
|
|
||||||
// .get()
|
|
||||||
// .uri("/task")
|
|
||||||
// .exchange()
|
|
||||||
// .then(response -> response.bodyToMono(String.class));
|
|
||||||
//
|
|
||||||
// assertThat(result.block()).isInstanceOf(String.class);
|
|
||||||
// }
|
|
||||||
|
|
||||||
// @Test
|
|
||||||
// public void givenThreeTasks_whenServerHandleTheTasks_thenServerResponseATask() throws Exception {
|
|
||||||
// URI uri = URI.create("http://localhost:8080/task/process");
|
|
||||||
// ExchangeFunction exchange = ExchangeFunctions.create(new ReactorClientHttpConnector());
|
|
||||||
// ClientRequest request = ClientRequest
|
|
||||||
// .method(HttpMethod.POST, uri)
|
|
||||||
// .body(BodyInserters.fromPublisher(getLatLngs(), Task.class))
|
|
||||||
// .build();
|
|
||||||
//
|
|
||||||
// Flux<Task> taskResponse = exchange
|
|
||||||
// .exchange(request)
|
|
||||||
// .flatMap(response -> response.bodyToFlux(Task.class));
|
|
||||||
//
|
|
||||||
// assertThat(taskResponse.blockFirst()).isInstanceOf(Task.class);
|
|
||||||
// }
|
|
||||||
|
|
||||||
// @Test
|
|
||||||
// public void givenCheckTask_whenServerHandle_thenOragicServerResponseALiveString() throws Exception {
|
|
||||||
// URI uri = URI.create("http://localhost:8080/task");
|
|
||||||
// ExchangeFunction exchange = ExchangeFunctions.create(new ReactorClientHttpConnector());
|
|
||||||
// ClientRequest request = ClientRequest
|
|
||||||
// .method(HttpMethod.GET, uri)
|
|
||||||
// .body(BodyInserters.fromPublisher(getLatLngs(), Task.class))
|
|
||||||
// .build();
|
|
||||||
//
|
|
||||||
// Flux<String> taskResponse = exchange
|
|
||||||
// .exchange(request)
|
|
||||||
// .flatMap(response -> response.bodyToFlux(String.class));
|
|
||||||
//
|
|
||||||
// assertThat(taskResponse.blockFirst()).isInstanceOf(String.class);
|
|
||||||
// }
|
|
||||||
|
|
||||||
private static Flux<Task> getLatLngs() {
|
|
||||||
return Flux.range(0, 3)
|
|
||||||
.zipWith(Flux.interval(Duration.ofSeconds(1)))
|
|
||||||
.map(x -> new Task("taskname", 1))
|
|
||||||
.doOnNext(ll -> System.out.println("Produced: {}" + ll));
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,33 +0,0 @@
|
||||||
package com.baeldung.jupiter;
|
|
||||||
|
|
||||||
import static org.junit.Assert.assertNotNull;
|
|
||||||
|
|
||||||
import org.junit.jupiter.api.Test;
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
|
||||||
import org.springframework.context.ApplicationContext;
|
|
||||||
import org.springframework.context.annotation.Configuration;
|
|
||||||
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @SpringJUnitConfig(SpringJUnitConfigTest.Config.class) is equivalent to:
|
|
||||||
*
|
|
||||||
* @ExtendWith(SpringExtension.class)
|
|
||||||
* @ContextConfiguration(classes = SpringJUnitConfigTest.Config.class )
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
@SpringJUnitConfig(SpringJUnitConfigTest.Config.class)
|
|
||||||
public class SpringJUnitConfigTest {
|
|
||||||
|
|
||||||
@Configuration
|
|
||||||
static class Config {
|
|
||||||
}
|
|
||||||
|
|
||||||
@Autowired
|
|
||||||
private ApplicationContext applicationContext;
|
|
||||||
|
|
||||||
@Test
|
|
||||||
void givenAppContext_WhenInjected_ThenItShouldNotBeNull() {
|
|
||||||
assertNotNull(applicationContext);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
|
@ -1,34 +0,0 @@
|
||||||
package com.baeldung.jupiter;
|
|
||||||
|
|
||||||
import static org.junit.Assert.assertNotNull;
|
|
||||||
|
|
||||||
import org.junit.jupiter.api.Test;
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
|
||||||
import org.springframework.context.annotation.Configuration;
|
|
||||||
import org.springframework.test.context.junit.jupiter.web.SpringJUnitWebConfig;
|
|
||||||
import org.springframework.web.context.WebApplicationContext;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @SpringJUnitWebConfig(SpringJUnitWebConfigTest.Config.class) is equivalent to:
|
|
||||||
*
|
|
||||||
* @ExtendWith(SpringExtension.class)
|
|
||||||
* @WebAppConfiguration
|
|
||||||
* @ContextConfiguration(classes = SpringJUnitWebConfigTest.Config.class )
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
@SpringJUnitWebConfig(SpringJUnitWebConfigTest.Config.class)
|
|
||||||
public class SpringJUnitWebConfigTest {
|
|
||||||
|
|
||||||
@Configuration
|
|
||||||
static class Config {
|
|
||||||
}
|
|
||||||
|
|
||||||
@Autowired
|
|
||||||
private WebApplicationContext webAppContext;
|
|
||||||
|
|
||||||
@Test
|
|
||||||
void givenWebAppContext_WhenInjected_ThenItShouldNotBeNull() {
|
|
||||||
assertNotNull(webAppContext);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
|
@ -1,48 +0,0 @@
|
||||||
package com.baeldung.security;
|
|
||||||
|
|
||||||
import com.baeldung.SpringSecurity5Application;
|
|
||||||
import org.junit.Before;
|
|
||||||
import org.junit.Test;
|
|
||||||
import org.junit.runner.RunWith;
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
|
||||||
import org.springframework.context.ApplicationContext;
|
|
||||||
import org.springframework.security.test.context.support.WithMockUser;
|
|
||||||
import org.springframework.test.context.ContextConfiguration;
|
|
||||||
import org.springframework.test.context.junit4.SpringRunner;
|
|
||||||
import org.springframework.test.web.reactive.server.WebTestClient;
|
|
||||||
|
|
||||||
@RunWith(SpringRunner.class)
|
|
||||||
@ContextConfiguration(classes = SpringSecurity5Application.class)
|
|
||||||
public class SecurityTest {
|
|
||||||
|
|
||||||
@Autowired
|
|
||||||
ApplicationContext context;
|
|
||||||
|
|
||||||
private WebTestClient rest;
|
|
||||||
|
|
||||||
@Before
|
|
||||||
public void setup() {
|
|
||||||
this.rest = WebTestClient
|
|
||||||
.bindToApplicationContext(this.context)
|
|
||||||
.configureClient()
|
|
||||||
.build();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void whenNoCredentials_thenRedirectToLogin() {
|
|
||||||
this.rest.get()
|
|
||||||
.uri("/")
|
|
||||||
.exchange()
|
|
||||||
.expectStatus().is3xxRedirection();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
@WithMockUser
|
|
||||||
public void whenHasCredentials_thenSeesGreeting() {
|
|
||||||
this.rest.get()
|
|
||||||
.uri("/")
|
|
||||||
.exchange()
|
|
||||||
.expectStatus().isOk()
|
|
||||||
.expectBody(String.class).isEqualTo("Hello, user");
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,101 +0,0 @@
|
||||||
package com.baeldung.web;
|
|
||||||
|
|
||||||
import com.baeldung.Spring5Application;
|
|
||||||
import org.junit.BeforeClass;
|
|
||||||
import org.junit.Test;
|
|
||||||
import org.junit.runner.RunWith;
|
|
||||||
import org.springframework.boot.test.context.SpringBootTest;
|
|
||||||
import org.springframework.test.context.junit4.SpringRunner;
|
|
||||||
import org.springframework.test.web.reactive.server.WebTestClient;
|
|
||||||
|
|
||||||
@RunWith(SpringRunner.class)
|
|
||||||
@SpringBootTest(classes = Spring5Application.class)
|
|
||||||
public class PathPatternsUsingHandlerMethodIntegrationTest {
|
|
||||||
|
|
||||||
private static WebTestClient client;
|
|
||||||
|
|
||||||
@BeforeClass
|
|
||||||
public static void setUp() {
|
|
||||||
client = WebTestClient.bindToController(new PathPatternController())
|
|
||||||
.build();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void givenHandlerMethod_whenMultipleURIVariablePattern_then200() {
|
|
||||||
|
|
||||||
client.get()
|
|
||||||
.uri("/spring5/ab/cd")
|
|
||||||
.exchange()
|
|
||||||
.expectStatus()
|
|
||||||
.is2xxSuccessful()
|
|
||||||
.expectBody()
|
|
||||||
.equals("/ab/cd");
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void givenHandlerMethod_whenURLWithWildcardTakingZeroOrMoreChar_then200() {
|
|
||||||
|
|
||||||
client.get()
|
|
||||||
.uri("/spring5/userid")
|
|
||||||
.exchange()
|
|
||||||
.expectStatus()
|
|
||||||
.is2xxSuccessful()
|
|
||||||
.expectBody()
|
|
||||||
.equals("/spring5/*id");
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void givenHandlerMethod_whenURLWithWildcardTakingExactlyOneChar_then200() {
|
|
||||||
|
|
||||||
client.get()
|
|
||||||
.uri("/string5")
|
|
||||||
.exchange()
|
|
||||||
.expectStatus()
|
|
||||||
.is2xxSuccessful()
|
|
||||||
.expectBody()
|
|
||||||
.equals("/s?ring5");
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void givenHandlerMethod_whenURLWithWildcardTakingZeroOrMorePathSegments_then200() {
|
|
||||||
|
|
||||||
client.get()
|
|
||||||
.uri("/resources/baeldung")
|
|
||||||
.exchange()
|
|
||||||
.expectStatus()
|
|
||||||
.is2xxSuccessful()
|
|
||||||
.expectBody()
|
|
||||||
.equals("/resources/**");
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void givenHandlerMethod_whenURLWithRegexInPathVariable_thenExpectedOutput() {
|
|
||||||
|
|
||||||
client.get()
|
|
||||||
.uri("/abc")
|
|
||||||
.exchange()
|
|
||||||
.expectStatus()
|
|
||||||
.is2xxSuccessful()
|
|
||||||
.expectBody()
|
|
||||||
.equals("abc");
|
|
||||||
|
|
||||||
client.get()
|
|
||||||
.uri("/123")
|
|
||||||
.exchange()
|
|
||||||
.expectStatus()
|
|
||||||
.is4xxClientError();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void givenHandlerMethod_whenURLWithMultiplePathVariablesInSameSegment_then200() {
|
|
||||||
|
|
||||||
client.get()
|
|
||||||
.uri("/baeldung_tutorial")
|
|
||||||
.exchange()
|
|
||||||
.expectStatus()
|
|
||||||
.is2xxSuccessful()
|
|
||||||
.expectBody()
|
|
||||||
.equals("Two variables are var1=baeldung and var2=tutorial");
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
|
@ -1,60 +0,0 @@
|
||||||
package com.baeldung.web.client;
|
|
||||||
|
|
||||||
import com.baeldung.Spring5Application;
|
|
||||||
import org.junit.Test;
|
|
||||||
import org.junit.runner.RunWith;
|
|
||||||
import org.springframework.boot.test.context.SpringBootTest;
|
|
||||||
import org.springframework.boot.web.server.LocalServerPort;
|
|
||||||
import org.springframework.test.context.junit4.SpringRunner;
|
|
||||||
import org.springframework.test.web.reactive.server.WebTestClient;
|
|
||||||
import org.springframework.web.reactive.function.server.RequestPredicates;
|
|
||||||
import org.springframework.web.reactive.function.server.RouterFunction;
|
|
||||||
import org.springframework.web.reactive.function.server.RouterFunctions;
|
|
||||||
import org.springframework.web.reactive.function.server.ServerResponse;
|
|
||||||
import org.springframework.web.server.WebHandler;
|
|
||||||
import reactor.core.publisher.Mono;
|
|
||||||
|
|
||||||
@RunWith(SpringRunner.class)
|
|
||||||
@SpringBootTest(classes = Spring5Application.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
|
|
||||||
public class WebTestClientTest {
|
|
||||||
|
|
||||||
@LocalServerPort
|
|
||||||
private int port;
|
|
||||||
|
|
||||||
private final RouterFunction ROUTER_FUNCTION = RouterFunctions.route(RequestPredicates.GET("/resource"), request -> ServerResponse.ok()
|
|
||||||
.build());
|
|
||||||
private final WebHandler WEB_HANDLER = exchange -> Mono.empty();
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testWebTestClientWithServerWebHandler() {
|
|
||||||
WebTestClient.bindToWebHandler(WEB_HANDLER)
|
|
||||||
.build();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testWebTestClientWithRouterFunction() {
|
|
||||||
WebTestClient.bindToRouterFunction(ROUTER_FUNCTION)
|
|
||||||
.build()
|
|
||||||
.get()
|
|
||||||
.uri("/resource")
|
|
||||||
.exchange()
|
|
||||||
.expectStatus()
|
|
||||||
.isOk()
|
|
||||||
.expectBody()
|
|
||||||
.isEmpty();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testWebTestClientWithServerURL() {
|
|
||||||
WebTestClient.bindToServer()
|
|
||||||
.baseUrl("http://localhost:" + port)
|
|
||||||
.build()
|
|
||||||
.get()
|
|
||||||
.uri("/resource")
|
|
||||||
.exchange()
|
|
||||||
.expectStatus()
|
|
||||||
.is3xxRedirection()
|
|
||||||
.expectBody();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
Binary file not shown.
Before Width: | Height: | Size: 22 KiB |
Loading…
Reference in New Issue