BAEL-2715: Modified tests and configurations
This commit is contained in:
parent
4d05dd1d9e
commit
e813a0e64d
|
@ -1,35 +1,24 @@
|
||||||
package com.baeldung.serverconfig.server;
|
package com.baeldung.serverconfig;
|
||||||
|
|
||||||
import io.netty.channel.EventLoopGroup;
|
import io.netty.channel.EventLoopGroup;
|
||||||
import io.netty.channel.nio.NioEventLoopGroup;
|
import io.netty.channel.nio.NioEventLoopGroup;
|
||||||
import io.netty.channel.socket.nio.NioServerSocketChannel;
|
import io.netty.channel.socket.nio.NioServerSocketChannel;
|
||||||
import org.springframework.boot.web.embedded.netty.NettyReactiveWebServerFactory;
|
import org.springframework.boot.web.embedded.netty.NettyReactiveWebServerFactory;
|
||||||
import org.springframework.boot.web.embedded.netty.NettyServerCustomizer;
|
import org.springframework.boot.web.embedded.netty.NettyServerCustomizer;
|
||||||
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.context.annotation.Profile;
|
||||||
import reactor.netty.http.server.HttpServer;
|
import reactor.netty.http.server.HttpServer;
|
||||||
|
|
||||||
@Component
|
@Configuration
|
||||||
public class NettyWebServerFactoryBootstrapCustomizer implements WebServerFactoryCustomizer<NettyReactiveWebServerFactory> {
|
@Profile("skipAutoConfig")
|
||||||
|
public class CustomNettyWebServerFactory {
|
||||||
|
|
||||||
@Override
|
@Bean
|
||||||
public void customize(NettyReactiveWebServerFactory serverFactory) {
|
public NettyReactiveWebServerFactory nettyReactiveWebServerFactory() {
|
||||||
serverFactory.addServerCustomizers(new PortCustomizer(8443));
|
NettyReactiveWebServerFactory webServerFactory = new NettyReactiveWebServerFactory();
|
||||||
serverFactory.addServerCustomizers(new EventLoopNettyCustomizer());
|
webServerFactory.addServerCustomizers(new EventLoopNettyCustomizer());
|
||||||
}
|
return webServerFactory;
|
||||||
|
|
||||||
private static class PortCustomizer implements NettyServerCustomizer {
|
|
||||||
|
|
||||||
private final int port;
|
|
||||||
|
|
||||||
private PortCustomizer(int port) {
|
|
||||||
this.port = port;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public HttpServer apply(HttpServer httpServer) {
|
|
||||||
return httpServer.port(port);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static class EventLoopNettyCustomizer implements NettyServerCustomizer {
|
private static class EventLoopNettyCustomizer implements NettyServerCustomizer {
|
|
@ -1,4 +1,4 @@
|
||||||
package com.baeldung.serverconfig.server;
|
package com.baeldung.serverconfig;
|
||||||
|
|
||||||
import org.springframework.web.bind.annotation.GetMapping;
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
import org.springframework.web.bind.annotation.PathVariable;
|
import org.springframework.web.bind.annotation.PathVariable;
|
|
@ -1,4 +1,4 @@
|
||||||
package com.baeldung.serverconfig.server;
|
package com.baeldung.serverconfig;
|
||||||
|
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import reactor.core.publisher.Mono;
|
import reactor.core.publisher.Mono;
|
|
@ -0,0 +1,30 @@
|
||||||
|
package com.baeldung.serverconfig;
|
||||||
|
|
||||||
|
import org.springframework.boot.web.embedded.netty.NettyReactiveWebServerFactory;
|
||||||
|
import org.springframework.boot.web.embedded.netty.NettyServerCustomizer;
|
||||||
|
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
import reactor.netty.http.server.HttpServer;
|
||||||
|
|
||||||
|
@Component
|
||||||
|
public class NettyWebServerFactoryPortCustomizer implements WebServerFactoryCustomizer<NettyReactiveWebServerFactory> {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void customize(NettyReactiveWebServerFactory serverFactory) {
|
||||||
|
serverFactory.addServerCustomizers(new PortCustomizer(8443));
|
||||||
|
}
|
||||||
|
|
||||||
|
private static class PortCustomizer implements NettyServerCustomizer {
|
||||||
|
|
||||||
|
private final int port;
|
||||||
|
|
||||||
|
private PortCustomizer(int port) {
|
||||||
|
this.port = port;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public HttpServer apply(HttpServer httpServer) {
|
||||||
|
return httpServer.port(port);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,4 +1,4 @@
|
||||||
package com.baeldung.serverconfig.server;
|
package com.baeldung.serverconfig;
|
||||||
|
|
||||||
import org.springframework.boot.web.embedded.netty.NettyReactiveWebServerFactory;
|
import org.springframework.boot.web.embedded.netty.NettyReactiveWebServerFactory;
|
||||||
import org.springframework.boot.web.embedded.netty.SslServerCustomizer;
|
import org.springframework.boot.web.embedded.netty.SslServerCustomizer;
|
|
@ -1,6 +1,5 @@
|
||||||
package com.baeldung.serverconfig;
|
package com.baeldung.serverconfig;
|
||||||
|
|
||||||
import com.baeldung.serverconfig.client.GreetingWebClient;
|
|
||||||
import org.springframework.boot.SpringApplication;
|
import org.springframework.boot.SpringApplication;
|
||||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
|
||||||
|
@ -9,8 +8,5 @@ public class ServerConfigApplication {
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
SpringApplication.run(ServerConfigApplication.class, args);
|
SpringApplication.run(ServerConfigApplication.class, args);
|
||||||
|
|
||||||
GreetingWebClient webClient = new GreetingWebClient();
|
|
||||||
webClient.getResult();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,37 +0,0 @@
|
||||||
package com.baeldung.serverconfig.client;
|
|
||||||
|
|
||||||
import io.netty.handler.ssl.SslContext;
|
|
||||||
import io.netty.handler.ssl.SslContextBuilder;
|
|
||||||
import io.netty.handler.ssl.util.InsecureTrustManagerFactory;
|
|
||||||
import lombok.SneakyThrows;
|
|
||||||
import org.springframework.http.client.reactive.ReactorClientHttpConnector;
|
|
||||||
import org.springframework.web.reactive.function.client.WebClient;
|
|
||||||
import reactor.core.publisher.Mono;
|
|
||||||
import reactor.netty.http.client.HttpClient;
|
|
||||||
|
|
||||||
public class GreetingWebClient {
|
|
||||||
|
|
||||||
private WebClient client = getWebClient();
|
|
||||||
|
|
||||||
public void getResult() {
|
|
||||||
System.out.println("Mono");
|
|
||||||
Mono<String> greetingMono = client.get()
|
|
||||||
.uri("/greet/{name}", "baeldung")
|
|
||||||
.retrieve()
|
|
||||||
.bodyToMono(String.class);
|
|
||||||
|
|
||||||
greetingMono.subscribe(System.out::println);
|
|
||||||
}
|
|
||||||
|
|
||||||
@SneakyThrows
|
|
||||||
private WebClient getWebClient() {
|
|
||||||
SslContext sslContext = SslContextBuilder
|
|
||||||
.forClient()
|
|
||||||
.trustManager(InsecureTrustManagerFactory.INSTANCE)
|
|
||||||
.build();
|
|
||||||
HttpClient httpClient = HttpClient.create().secure(t -> t.sslContext(sslContext));
|
|
||||||
return WebClient.builder()
|
|
||||||
.baseUrl("https://localhost:8443")
|
|
||||||
.clientConnector(new ReactorClientHttpConnector(httpClient)).build();
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,4 +1,4 @@
|
||||||
package com.baeldung.serverconfig.server;
|
package com.baeldung.serverconfig;
|
||||||
|
|
||||||
import static org.mockito.Mockito.when;
|
import static org.mockito.Mockito.when;
|
||||||
|
|
||||||
|
@ -22,11 +22,11 @@ public class GreetingControllerIntegrationTest {
|
||||||
@MockBean
|
@MockBean
|
||||||
private GreetingService greetingService;
|
private GreetingService greetingService;
|
||||||
|
|
||||||
private final String name = "baeldung";
|
private final String name = "Baeldung";
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void setUp() {
|
public void setUp() {
|
||||||
when(greetingService.greet(name)).thenReturn(Mono.just("Hello baeldung"));
|
when(greetingService.greet(name)).thenReturn(Mono.just("Greeting Baeldung"));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -36,6 +36,6 @@ public class GreetingControllerIntegrationTest {
|
||||||
.expectStatus()
|
.expectStatus()
|
||||||
.isOk()
|
.isOk()
|
||||||
.expectBody(String.class)
|
.expectBody(String.class)
|
||||||
.isEqualTo("Hello baeldung");
|
.isEqualTo("Greeting Baeldung");
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -0,0 +1,56 @@
|
||||||
|
package com.baeldung.serverconfig;
|
||||||
|
|
||||||
|
import io.netty.handler.ssl.SslContext;
|
||||||
|
import io.netty.handler.ssl.SslContextBuilder;
|
||||||
|
import io.netty.handler.ssl.util.InsecureTrustManagerFactory;
|
||||||
|
import javax.net.ssl.SSLException;
|
||||||
|
import org.junit.Before;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
|
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
|
||||||
|
import org.springframework.http.client.reactive.ReactorClientHttpConnector;
|
||||||
|
import org.springframework.test.context.ActiveProfiles;
|
||||||
|
import org.springframework.test.context.junit4.SpringRunner;
|
||||||
|
import org.springframework.test.web.reactive.server.WebTestClient;
|
||||||
|
import org.springframework.test.web.reactive.server.WebTestClient.ResponseSpec;
|
||||||
|
import reactor.netty.http.client.HttpClient;
|
||||||
|
|
||||||
|
@RunWith(SpringRunner.class)
|
||||||
|
@SpringBootTest(webEnvironment = WebEnvironment.DEFINED_PORT)
|
||||||
|
public class GreetingLiveTest {
|
||||||
|
|
||||||
|
private static final String BASE_URL = "https://localhost:8443";
|
||||||
|
|
||||||
|
private WebTestClient webTestClient;
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void setup() throws SSLException {
|
||||||
|
webTestClient = WebTestClient.bindToServer(getConnector())
|
||||||
|
.baseUrl(BASE_URL)
|
||||||
|
.build();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void shouldGreet() {
|
||||||
|
final String name = "Baeldung";
|
||||||
|
|
||||||
|
ResponseSpec response = webTestClient.get()
|
||||||
|
.uri("/greet/{name}", name)
|
||||||
|
.exchange();
|
||||||
|
|
||||||
|
response.expectStatus()
|
||||||
|
.isOk()
|
||||||
|
.expectBody(String.class)
|
||||||
|
.isEqualTo("Greeting Baeldung");
|
||||||
|
}
|
||||||
|
|
||||||
|
private ReactorClientHttpConnector getConnector() throws SSLException {
|
||||||
|
SslContext sslContext = SslContextBuilder
|
||||||
|
.forClient()
|
||||||
|
.trustManager(InsecureTrustManagerFactory.INSTANCE)
|
||||||
|
.build();
|
||||||
|
HttpClient httpClient = HttpClient.create().secure(t -> t.sslContext(sslContext));
|
||||||
|
return new ReactorClientHttpConnector(httpClient);
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,8 @@
|
||||||
|
package com.baeldung.serverconfig;
|
||||||
|
|
||||||
|
import org.springframework.test.context.ActiveProfiles;
|
||||||
|
|
||||||
|
@ActiveProfiles("skipAutoConfig")
|
||||||
|
public class GreetingSkipAutoConfigLiveTest extends GreetingLiveTest {
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue