BAEL-2715: Modified tests and configurations

This commit is contained in:
isaolmez 2019-03-19 22:29:12 +03:00
parent 4d05dd1d9e
commit e813a0e64d
10 changed files with 113 additions and 71 deletions

View File

@ -1,35 +1,24 @@
package com.baeldung.serverconfig.server;
package com.baeldung.serverconfig;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
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 org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import reactor.netty.http.server.HttpServer;
@Component
public class NettyWebServerFactoryBootstrapCustomizer implements WebServerFactoryCustomizer<NettyReactiveWebServerFactory> {
@Configuration
@Profile("skipAutoConfig")
public class CustomNettyWebServerFactory {
@Override
public void customize(NettyReactiveWebServerFactory serverFactory) {
serverFactory.addServerCustomizers(new PortCustomizer(8443));
serverFactory.addServerCustomizers(new EventLoopNettyCustomizer());
}
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);
}
@Bean
public NettyReactiveWebServerFactory nettyReactiveWebServerFactory() {
NettyReactiveWebServerFactory webServerFactory = new NettyReactiveWebServerFactory();
webServerFactory.addServerCustomizers(new EventLoopNettyCustomizer());
return webServerFactory;
}
private static class EventLoopNettyCustomizer implements NettyServerCustomizer {

View File

@ -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.PathVariable;

View File

@ -1,4 +1,4 @@
package com.baeldung.serverconfig.server;
package com.baeldung.serverconfig;
import org.springframework.stereotype.Service;
import reactor.core.publisher.Mono;

View File

@ -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);
}
}
}

View File

@ -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.SslServerCustomizer;

View File

@ -1,6 +1,5 @@
package com.baeldung.serverconfig;
import com.baeldung.serverconfig.client.GreetingWebClient;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@ -9,8 +8,5 @@ public class ServerConfigApplication {
public static void main(String[] args) {
SpringApplication.run(ServerConfigApplication.class, args);
GreetingWebClient webClient = new GreetingWebClient();
webClient.getResult();
}
}

View File

@ -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();
}
}

View File

@ -1,4 +1,4 @@
package com.baeldung.serverconfig.server;
package com.baeldung.serverconfig;
import static org.mockito.Mockito.when;
@ -22,11 +22,11 @@ public class GreetingControllerIntegrationTest {
@MockBean
private GreetingService greetingService;
private final String name = "baeldung";
private final String name = "Baeldung";
@Before
public void setUp() {
when(greetingService.greet(name)).thenReturn(Mono.just("Hello baeldung"));
when(greetingService.greet(name)).thenReturn(Mono.just("Greeting Baeldung"));
}
@Test
@ -36,6 +36,6 @@ public class GreetingControllerIntegrationTest {
.expectStatus()
.isOk()
.expectBody(String.class)
.isEqualTo("Hello baeldung");
.isEqualTo("Greeting Baeldung");
}
}

View File

@ -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);
}
}

View File

@ -0,0 +1,8 @@
package com.baeldung.serverconfig;
import org.springframework.test.context.ActiveProfiles;
@ActiveProfiles("skipAutoConfig")
public class GreetingSkipAutoConfigLiveTest extends GreetingLiveTest {
}