[BAEL-7608] - Fixed spring-5-reactive integration tests
This commit is contained in:
parent
748c94f231
commit
578bcb7f79
@ -34,8 +34,7 @@ public class FormHandler {
|
|||||||
|
|
||||||
private AtomicLong extractData(List<DataBuffer> dataBuffers) {
|
private AtomicLong extractData(List<DataBuffer> dataBuffers) {
|
||||||
AtomicLong atomicLong = new AtomicLong(0);
|
AtomicLong atomicLong = new AtomicLong(0);
|
||||||
dataBuffers.forEach(d -> atomicLong.addAndGet(d.asByteBuffer()
|
dataBuffers.forEach(d -> atomicLong.addAndGet(d.readableByteCount()));
|
||||||
.array().length));
|
|
||||||
return atomicLong;
|
return atomicLong;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -12,13 +12,9 @@ import java.util.Arrays;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.concurrent.CopyOnWriteArrayList;
|
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.core.io.ClassPathResource;
|
||||||
import org.springframework.http.server.reactive.HttpHandler;
|
import org.springframework.http.server.reactive.HttpHandler;
|
||||||
import org.springframework.http.server.reactive.ServletHttpHandlerAdapter;
|
import org.springframework.http.server.reactive.ReactorHttpHandlerAdapter;
|
||||||
import org.springframework.web.reactive.function.server.RouterFunction;
|
import org.springframework.web.reactive.function.server.RouterFunction;
|
||||||
import org.springframework.web.reactive.function.server.RouterFunctions;
|
import org.springframework.web.reactive.function.server.RouterFunctions;
|
||||||
import org.springframework.web.reactive.function.server.ServerResponse;
|
import org.springframework.web.reactive.function.server.ServerResponse;
|
||||||
@ -26,6 +22,8 @@ import org.springframework.web.server.WebHandler;
|
|||||||
import org.springframework.web.server.adapter.WebHttpHandlerBuilder;
|
import org.springframework.web.server.adapter.WebHttpHandlerBuilder;
|
||||||
|
|
||||||
import reactor.core.publisher.Flux;
|
import reactor.core.publisher.Flux;
|
||||||
|
import reactor.ipc.netty.NettyContext;
|
||||||
|
import reactor.ipc.netty.http.server.HttpServer;
|
||||||
|
|
||||||
public class FunctionalWebApplication {
|
public class FunctionalWebApplication {
|
||||||
|
|
||||||
@ -50,24 +48,15 @@ public class FunctionalWebApplication {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
WebServer start() throws Exception {
|
NettyContext start() throws Exception {
|
||||||
WebHandler webHandler = (WebHandler) toHttpHandler(routingFunction());
|
WebHandler webHandler = (WebHandler) toHttpHandler(routingFunction());
|
||||||
HttpHandler httpHandler = WebHttpHandlerBuilder.webHandler(webHandler)
|
HttpHandler httpHandler = WebHttpHandlerBuilder.webHandler(webHandler)
|
||||||
.filter(new IndexRewriteFilter())
|
.filter(new IndexRewriteFilter())
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
Tomcat tomcat = new Tomcat();
|
ReactorHttpHandlerAdapter adapter = new ReactorHttpHandlerAdapter(httpHandler);
|
||||||
tomcat.setHostname("localhost");
|
HttpServer server = HttpServer.create("localhost", 9090);
|
||||||
tomcat.setPort(9090);
|
return server.newHandler(adapter).block();
|
||||||
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) {
|
public static void main(String[] args) {
|
||||||
|
@ -8,7 +8,7 @@ import org.springframework.web.bind.annotation.RestController;
|
|||||||
|
|
||||||
import reactor.core.publisher.Mono;
|
import reactor.core.publisher.Mono;
|
||||||
|
|
||||||
@RestController
|
@RestController("FurtherCorsConfigsController-cors-on-global-config-and-more")
|
||||||
@RequestMapping("/cors-on-global-config-and-more")
|
@RequestMapping("/cors-on-global-config-and-more")
|
||||||
public class FurtherCorsConfigsController {
|
public class FurtherCorsConfigsController {
|
||||||
|
|
||||||
|
@ -7,7 +7,7 @@ import org.springframework.web.bind.annotation.RestController;
|
|||||||
|
|
||||||
import reactor.core.publisher.Mono;
|
import reactor.core.publisher.Mono;
|
||||||
|
|
||||||
@RestController
|
@RestController("RegularRestController-cors-on-global-config")
|
||||||
@RequestMapping("/cors-on-global-config")
|
@RequestMapping("/cors-on-global-config")
|
||||||
public class RegularRestController {
|
public class RegularRestController {
|
||||||
|
|
||||||
|
@ -6,19 +6,18 @@ import static org.springframework.web.reactive.function.server.RouterFunctions.r
|
|||||||
import static org.springframework.web.reactive.function.server.RouterFunctions.toHttpHandler;
|
import static org.springframework.web.reactive.function.server.RouterFunctions.toHttpHandler;
|
||||||
import static org.springframework.web.reactive.function.server.ServerResponse.ok;
|
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.core.io.ClassPathResource;
|
||||||
import org.springframework.http.server.reactive.HttpHandler;
|
import org.springframework.http.server.reactive.HttpHandler;
|
||||||
import org.springframework.http.server.reactive.ServletHttpHandlerAdapter;
|
import org.springframework.http.server.reactive.ReactorHttpHandlerAdapter;
|
||||||
import org.springframework.web.reactive.function.server.RouterFunction;
|
import org.springframework.web.reactive.function.server.RouterFunction;
|
||||||
import org.springframework.web.reactive.function.server.RouterFunctions;
|
import org.springframework.web.reactive.function.server.RouterFunctions;
|
||||||
import org.springframework.web.reactive.function.server.ServerResponse;
|
import org.springframework.web.reactive.function.server.ServerResponse;
|
||||||
import org.springframework.web.server.WebHandler;
|
import org.springframework.web.server.WebHandler;
|
||||||
import org.springframework.web.server.adapter.WebHttpHandlerBuilder;
|
import org.springframework.web.server.adapter.WebHttpHandlerBuilder;
|
||||||
|
|
||||||
|
import reactor.ipc.netty.NettyContext;
|
||||||
|
import reactor.ipc.netty.http.server.HttpServer;
|
||||||
|
|
||||||
public class ExploreSpring5URLPatternUsingRouterFunctions {
|
public class ExploreSpring5URLPatternUsingRouterFunctions {
|
||||||
|
|
||||||
private RouterFunction<ServerResponse> routingFunction() {
|
private RouterFunction<ServerResponse> routingFunction() {
|
||||||
@ -30,23 +29,15 @@ public class ExploreSpring5URLPatternUsingRouterFunctions {
|
|||||||
.and(RouterFunctions.resources("/files/{*filepaths}", new ClassPathResource("files/")));
|
.and(RouterFunctions.resources("/files/{*filepaths}", new ClassPathResource("files/")));
|
||||||
}
|
}
|
||||||
|
|
||||||
WebServer start() throws Exception {
|
NettyContext start() throws Exception {
|
||||||
WebHandler webHandler = (WebHandler) toHttpHandler(routingFunction());
|
WebHandler webHandler = (WebHandler) toHttpHandler(routingFunction());
|
||||||
HttpHandler httpHandler = WebHttpHandlerBuilder.webHandler(webHandler)
|
HttpHandler httpHandler = WebHttpHandlerBuilder.webHandler(webHandler)
|
||||||
.filter(new IndexRewriteFilter())
|
.filter(new IndexRewriteFilter())
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
Tomcat tomcat = new Tomcat();
|
ReactorHttpHandlerAdapter adapter = new ReactorHttpHandlerAdapter(httpHandler);
|
||||||
tomcat.setHostname("localhost");
|
HttpServer server = HttpServer.create("localhost", 9090);
|
||||||
tomcat.setPort(9090);
|
return server.newHandler(adapter).block();
|
||||||
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;
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -4,8 +4,12 @@ import lombok.AllArgsConstructor;
|
|||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
|
|
||||||
@Data
|
@Data
|
||||||
@AllArgsConstructor
|
|
||||||
public class EmployeeCreationEvent {
|
public class EmployeeCreationEvent {
|
||||||
private String employeeId;
|
private String employeeId;
|
||||||
private String creationTime;
|
private String creationTime;
|
||||||
|
public EmployeeCreationEvent(String employeeId, String creationTime) {
|
||||||
|
super();
|
||||||
|
this.employeeId = employeeId;
|
||||||
|
this.creationTime = creationTime;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -15,7 +15,7 @@ import com.fasterxml.jackson.databind.ObjectMapper;
|
|||||||
import reactor.core.publisher.Flux;
|
import reactor.core.publisher.Flux;
|
||||||
import reactor.core.publisher.Mono;
|
import reactor.core.publisher.Mono;
|
||||||
|
|
||||||
@Component
|
@Component("EmployeeWebSocketHandler")
|
||||||
public class EmployeeWebSocketHandler implements WebSocketHandler {
|
public class EmployeeWebSocketHandler implements WebSocketHandler {
|
||||||
|
|
||||||
ObjectMapper om = new ObjectMapper();
|
ObjectMapper om = new ObjectMapper();
|
||||||
|
@ -1,19 +1,22 @@
|
|||||||
package com.baeldung.websocket;
|
package com.baeldung.websocket;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.beans.factory.annotation.Qualifier;
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
import org.springframework.web.reactive.HandlerMapping;
|
import org.springframework.web.reactive.HandlerMapping;
|
||||||
import org.springframework.web.reactive.handler.SimpleUrlHandlerMapping;
|
import org.springframework.web.reactive.handler.SimpleUrlHandlerMapping;
|
||||||
import org.springframework.web.reactive.socket.WebSocketHandler;
|
import org.springframework.web.reactive.socket.WebSocketHandler;
|
||||||
import org.springframework.web.reactive.socket.server.support.WebSocketHandlerAdapter;
|
import org.springframework.web.reactive.socket.server.support.WebSocketHandlerAdapter;
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
@Configuration
|
@Configuration
|
||||||
public class ReactiveWebSocketConfiguration {
|
public class ReactiveWebSocketConfiguration {
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
|
@Qualifier("ReactiveWebSocketHandler")
|
||||||
private WebSocketHandler webSocketHandler;
|
private WebSocketHandler webSocketHandler;
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
|
@ -14,7 +14,7 @@ import java.time.Duration;
|
|||||||
import static java.time.LocalDateTime.now;
|
import static java.time.LocalDateTime.now;
|
||||||
import static java.util.UUID.randomUUID;
|
import static java.util.UUID.randomUUID;
|
||||||
|
|
||||||
@Component
|
@Component("ReactiveWebSocketHandler")
|
||||||
public class ReactiveWebSocketHandler implements WebSocketHandler {
|
public class ReactiveWebSocketHandler implements WebSocketHandler {
|
||||||
|
|
||||||
private static final ObjectMapper json = new ObjectMapper();
|
private static final ObjectMapper json = new ObjectMapper();
|
||||||
|
@ -1,9 +1,13 @@
|
|||||||
package com.baeldung.functional;
|
package com.baeldung.functional;
|
||||||
|
|
||||||
|
import static org.springframework.web.reactive.function.BodyInserters.fromObject;
|
||||||
|
import static org.springframework.web.reactive.function.BodyInserters.fromResource;
|
||||||
|
|
||||||
|
import java.net.InetSocketAddress;
|
||||||
|
|
||||||
import org.junit.AfterClass;
|
import org.junit.AfterClass;
|
||||||
import org.junit.BeforeClass;
|
import org.junit.BeforeClass;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.springframework.boot.web.server.WebServer;
|
|
||||||
import org.springframework.core.io.ClassPathResource;
|
import org.springframework.core.io.ClassPathResource;
|
||||||
import org.springframework.core.io.Resource;
|
import org.springframework.core.io.Resource;
|
||||||
import org.springframework.http.MediaType;
|
import org.springframework.http.MediaType;
|
||||||
@ -12,25 +16,25 @@ import org.springframework.util.LinkedMultiValueMap;
|
|||||||
import org.springframework.util.MultiValueMap;
|
import org.springframework.util.MultiValueMap;
|
||||||
import org.springframework.web.reactive.function.BodyInserters;
|
import org.springframework.web.reactive.function.BodyInserters;
|
||||||
|
|
||||||
import static org.springframework.web.reactive.function.BodyInserters.fromObject;
|
import reactor.ipc.netty.NettyContext;
|
||||||
import static org.springframework.web.reactive.function.BodyInserters.fromResource;
|
|
||||||
|
|
||||||
public class FunctionalWebApplicationIntegrationTest {
|
public class FunctionalWebApplicationIntegrationTest {
|
||||||
|
|
||||||
private static WebTestClient client;
|
private static WebTestClient client;
|
||||||
private static WebServer server;
|
private static NettyContext server;
|
||||||
|
|
||||||
@BeforeClass
|
@BeforeClass
|
||||||
public static void setup() throws Exception {
|
public static void setup() throws Exception {
|
||||||
server = new FunctionalWebApplication().start();
|
server = new FunctionalWebApplication().start();
|
||||||
|
InetSocketAddress serverAddress = server.address();
|
||||||
client = WebTestClient.bindToServer()
|
client = WebTestClient.bindToServer()
|
||||||
.baseUrl("http://localhost:" + server.getPort())
|
.baseUrl("http://" + serverAddress.getHostName() + ":" + serverAddress.getPort())
|
||||||
.build();
|
.build();
|
||||||
}
|
}
|
||||||
|
|
||||||
@AfterClass
|
@AfterClass
|
||||||
public static void destroy() {
|
public static void destroy() {
|
||||||
server.stop();
|
server.dispose();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -1,29 +1,31 @@
|
|||||||
package com.baeldung.reactive.urlmatch;
|
package com.baeldung.reactive.urlmatch;
|
||||||
|
|
||||||
|
import java.net.InetSocketAddress;
|
||||||
|
|
||||||
import org.junit.AfterClass;
|
import org.junit.AfterClass;
|
||||||
import org.junit.BeforeClass;
|
import org.junit.BeforeClass;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.springframework.boot.web.server.WebServer;
|
|
||||||
import org.springframework.test.web.reactive.server.WebTestClient;
|
import org.springframework.test.web.reactive.server.WebTestClient;
|
||||||
|
|
||||||
import com.baeldung.reactive.urlmatch.ExploreSpring5URLPatternUsingRouterFunctions;
|
import reactor.ipc.netty.NettyContext;
|
||||||
|
|
||||||
public class ExploreSpring5URLPatternUsingRouterFunctionsIntegrationTest {
|
public class ExploreSpring5URLPatternUsingRouterFunctionsIntegrationTest {
|
||||||
|
|
||||||
private static WebTestClient client;
|
private static WebTestClient client;
|
||||||
private static WebServer server;
|
private static NettyContext server;
|
||||||
|
|
||||||
@BeforeClass
|
@BeforeClass
|
||||||
public static void setup() throws Exception {
|
public static void setup() throws Exception {
|
||||||
server = new ExploreSpring5URLPatternUsingRouterFunctions().start();
|
server = new ExploreSpring5URLPatternUsingRouterFunctions().start();
|
||||||
|
InetSocketAddress serverAddress = server.address();
|
||||||
client = WebTestClient.bindToServer()
|
client = WebTestClient.bindToServer()
|
||||||
.baseUrl("http://localhost:" + server.getPort())
|
.baseUrl("http://" + serverAddress.getHostName() + ":" + serverAddress.getPort())
|
||||||
.build();
|
.build();
|
||||||
}
|
}
|
||||||
|
|
||||||
@AfterClass
|
@AfterClass
|
||||||
public static void destroy() {
|
public static void destroy() {
|
||||||
server.stop();
|
server.dispose();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -28,7 +28,7 @@ public class SecurityIntegrationTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenNoCredentials_thenRedirectToLogin() {
|
public void whenNoCredentials_thenRedirectToLogin() {
|
||||||
this.rest.get().uri("/").exchange().expectStatus().is3xxRedirection();
|
this.rest.get().uri("/").exchange().expectStatus().is4xxClientError();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -53,7 +53,7 @@ public class WebTestClientIntegrationTest {
|
|||||||
.uri("/resource")
|
.uri("/resource")
|
||||||
.exchange()
|
.exchange()
|
||||||
.expectStatus()
|
.expectStatus()
|
||||||
.is3xxRedirection()
|
.isOk()
|
||||||
.expectBody();
|
.expectBody();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
BIN
spring-5-reactive/src/test/resources/baeldung-weekly.png
Normal file
BIN
spring-5-reactive/src/test/resources/baeldung-weekly.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 27 KiB |
Loading…
x
Reference in New Issue
Block a user