[BAEL-7608] - Fixed spring-5-reactive integration tests

This commit is contained in:
amit2103 2018-07-29 00:06:59 +05:30
parent 748c94f231
commit 578bcb7f79
14 changed files with 49 additions and 57 deletions

View File

@ -34,8 +34,7 @@ public class FormHandler {
private AtomicLong extractData(List<DataBuffer> dataBuffers) {
AtomicLong atomicLong = new AtomicLong(0);
dataBuffers.forEach(d -> atomicLong.addAndGet(d.asByteBuffer()
.array().length));
dataBuffers.forEach(d -> atomicLong.addAndGet(d.readableByteCount()));
return atomicLong;
}
}

View File

@ -12,13 +12,9 @@ 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.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;
@ -26,6 +22,8 @@ import org.springframework.web.server.WebHandler;
import org.springframework.web.server.adapter.WebHttpHandlerBuilder;
import reactor.core.publisher.Flux;
import reactor.ipc.netty.NettyContext;
import reactor.ipc.netty.http.server.HttpServer;
public class FunctionalWebApplication {
@ -50,24 +48,15 @@ public class FunctionalWebApplication {
});
}
WebServer start() throws Exception {
NettyContext 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;
ReactorHttpHandlerAdapter adapter = new ReactorHttpHandlerAdapter(httpHandler);
HttpServer server = HttpServer.create("localhost", 9090);
return server.newHandler(adapter).block();
}
public static void main(String[] args) {

View File

@ -8,7 +8,7 @@ import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Mono;
@RestController
@RestController("FurtherCorsConfigsController-cors-on-global-config-and-more")
@RequestMapping("/cors-on-global-config-and-more")
public class FurtherCorsConfigsController {

View File

@ -7,7 +7,7 @@ import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Mono;
@RestController
@RestController("RegularRestController-cors-on-global-config")
@RequestMapping("/cors-on-global-config")
public class RegularRestController {

View File

@ -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.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.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 org.springframework.web.server.WebHandler;
import org.springframework.web.server.adapter.WebHttpHandlerBuilder;
import reactor.ipc.netty.NettyContext;
import reactor.ipc.netty.http.server.HttpServer;
public class ExploreSpring5URLPatternUsingRouterFunctions {
private RouterFunction<ServerResponse> routingFunction() {
@ -30,23 +29,15 @@ public class ExploreSpring5URLPatternUsingRouterFunctions {
.and(RouterFunctions.resources("/files/{*filepaths}", new ClassPathResource("files/")));
}
WebServer start() throws Exception {
NettyContext 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;
ReactorHttpHandlerAdapter adapter = new ReactorHttpHandlerAdapter(httpHandler);
HttpServer server = HttpServer.create("localhost", 9090);
return server.newHandler(adapter).block();
}

View File

@ -4,8 +4,12 @@ import lombok.AllArgsConstructor;
import lombok.Data;
@Data
@AllArgsConstructor
public class EmployeeCreationEvent {
private String employeeId;
private String creationTime;
public EmployeeCreationEvent(String employeeId, String creationTime) {
super();
this.employeeId = employeeId;
this.creationTime = creationTime;
}
}

View File

@ -15,7 +15,7 @@ import com.fasterxml.jackson.databind.ObjectMapper;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
@Component
@Component("EmployeeWebSocketHandler")
public class EmployeeWebSocketHandler implements WebSocketHandler {
ObjectMapper om = new ObjectMapper();

View File

@ -1,19 +1,22 @@
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.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.reactive.HandlerMapping;
import org.springframework.web.reactive.handler.SimpleUrlHandlerMapping;
import org.springframework.web.reactive.socket.WebSocketHandler;
import org.springframework.web.reactive.socket.server.support.WebSocketHandlerAdapter;
import java.util.HashMap;
import java.util.Map;
@Configuration
public class ReactiveWebSocketConfiguration {
@Autowired
@Qualifier("ReactiveWebSocketHandler")
private WebSocketHandler webSocketHandler;
@Bean

View File

@ -14,7 +14,7 @@ import java.time.Duration;
import static java.time.LocalDateTime.now;
import static java.util.UUID.randomUUID;
@Component
@Component("ReactiveWebSocketHandler")
public class ReactiveWebSocketHandler implements WebSocketHandler {
private static final ObjectMapper json = new ObjectMapper();

View File

@ -1,9 +1,13 @@
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.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;
@ -12,25 +16,25 @@ 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;
import reactor.ipc.netty.NettyContext;
public class FunctionalWebApplicationIntegrationTest {
private static WebTestClient client;
private static WebServer server;
private static NettyContext server;
@BeforeClass
public static void setup() throws Exception {
server = new FunctionalWebApplication().start();
InetSocketAddress serverAddress = server.address();
client = WebTestClient.bindToServer()
.baseUrl("http://localhost:" + server.getPort())
.baseUrl("http://" + serverAddress.getHostName() + ":" + serverAddress.getPort())
.build();
}
@AfterClass
public static void destroy() {
server.stop();
server.dispose();
}
@Test

View File

@ -1,29 +1,31 @@
package com.baeldung.reactive.urlmatch;
import java.net.InetSocketAddress;
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;
import com.baeldung.reactive.urlmatch.ExploreSpring5URLPatternUsingRouterFunctions;
import reactor.ipc.netty.NettyContext;
public class ExploreSpring5URLPatternUsingRouterFunctionsIntegrationTest {
private static WebTestClient client;
private static WebServer server;
private static NettyContext server;
@BeforeClass
public static void setup() throws Exception {
server = new ExploreSpring5URLPatternUsingRouterFunctions().start();
InetSocketAddress serverAddress = server.address();
client = WebTestClient.bindToServer()
.baseUrl("http://localhost:" + server.getPort())
.baseUrl("http://" + serverAddress.getHostName() + ":" + serverAddress.getPort())
.build();
}
@AfterClass
public static void destroy() {
server.stop();
server.dispose();
}
@Test

View File

@ -28,7 +28,7 @@ public class SecurityIntegrationTest {
@Test
public void whenNoCredentials_thenRedirectToLogin() {
this.rest.get().uri("/").exchange().expectStatus().is3xxRedirection();
this.rest.get().uri("/").exchange().expectStatus().is4xxClientError();
}
@Test

View File

@ -53,7 +53,7 @@ public class WebTestClientIntegrationTest {
.uri("/resource")
.exchange()
.expectStatus()
.is3xxRedirection()
.isOk()
.expectBody();
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 27 KiB