/*
 * Copyright 2012-2018 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.springframework.boot.web.embedded.netty;

import java.time.Duration;
import java.util.Arrays;

import org.junit.Test;
import org.mockito.InOrder;
import reactor.ipc.netty.http.server.HttpServerOptions;

import org.springframework.boot.web.reactive.server.AbstractReactiveWebServerFactory;
import org.springframework.boot.web.reactive.server.AbstractReactiveWebServerFactoryTests;
import org.springframework.boot.web.server.PortInUseException;
import org.springframework.test.util.ReflectionTestUtils;

import static org.assertj.core.api.Assertions.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.inOrder;
import static org.mockito.Mockito.mock;

/**
 * Tests for {@link NettyReactiveWebServerFactory}.
 *
 * @author Brian Clozel
 */
public class NettyReactiveWebServerFactoryTests
		extends AbstractReactiveWebServerFactoryTests {

	@Override
	protected NettyReactiveWebServerFactory getFactory() {
		return new NettyReactiveWebServerFactory(0);
	}

	@Test
	public void portInUseExceptionIsThrownWhenPortIsAlreadyInUse() {
		AbstractReactiveWebServerFactory factory = getFactory();
		factory.setPort(0);
		this.webServer = factory.getWebServer(new EchoHandler());
		this.webServer.start();
		factory.setPort(this.webServer.getPort());
		this.thrown.expect(PortInUseException.class);
		this.thrown.expectMessage(
				equalTo("Port " + this.webServer.getPort() + " is already in use"));
		factory.getWebServer(new EchoHandler()).start();
	}

	@Test
	public void nettyCustomizers() {
		NettyReactiveWebServerFactory factory = getFactory();
		NettyServerCustomizer[] customizers = new NettyServerCustomizer[2];
		for (int i = 0; i < customizers.length; i++) {
			customizers[i] = mock(NettyServerCustomizer.class);
		}
		factory.setServerCustomizers(Arrays.asList(customizers[0], customizers[1]));
		this.webServer = factory.getWebServer(new EchoHandler());
		InOrder ordered = inOrder((Object[]) customizers);
		for (NettyServerCustomizer customizer : customizers) {
			ordered.verify(customizer).customize(any(HttpServerOptions.Builder.class));
		}
	}

	@Test
	public void customStartupTimeout() {
		Duration timeout = Duration.ofDays(365);
		NettyReactiveWebServerFactory factory = getFactory();
		factory.setLifecycleTimeout(timeout);
		this.webServer = factory.getWebServer(new EchoHandler());
		this.webServer.start();
		Object context = ReflectionTestUtils.getField(this.webServer, "nettyContext");
		Object actualTimeout = ReflectionTestUtils.getField(context, "lifecycleTimeout");
		assertThat(actualTimeout).isEqualTo(timeout);
	}

}

@@ -1050,25 +1050,25 @@
 .server.
-PortInUse
+WebServer
 Exceptio
@@ -1191,53 +1191,8 @@
 at;%0A
-import static org.hamcrest.Matchers.equalTo;%0A
 impo
@@ -1647,18 +1647,9 @@
 oid 
-portInUseE
+e
 xcep
@@ -1924,17 +1924,17 @@
 ect(
-PortInUse
+WebServer
 Exce
@@ -1951,110 +1951,8 @@
 s);%0A
-%09%09this.thrown.expectMessage(%0A%09%09%09%09equalTo(%22Port %22 + this.webServer.getPort() + %22 is already in use%22));%0A
 %09%09fa

Fix failing NettyReactiveWebServerFactoryTests