/*
 * Copyright 2016, gRPC Authors All rights reserved.
 *
 * 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 io.grpc.netty;

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import io.netty.handler.ssl.SslContext;
import java.util.concurrent.TimeUnit;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;


/**
 * Unit tests for {@link NettyServerBuilder}.
 */
@RunWith(JUnit4.class)
public class NettyServerBuilderTest {

  @Rule public final ExpectedException thrown = ExpectedException.none();

  @Test
  public void sslContextCanBeNull() {
    NettyServerBuilder builder = NettyServerBuilder.forPort(8080);
    builder.sslContext(null);
  }

  @Test
  public void failIfSslContextIsNotServer() {
    SslContext sslContext = mock(SslContext.class);
    when(sslContext.isClient()).thenReturn(true);

    NettyServerBuilder builder = NettyServerBuilder.forPort(8080);

    thrown.expect(IllegalArgumentException.class);
    thrown.expectMessage("Client SSL context can not be used for server");
    builder.sslContext(sslContext);
  }

  @Test
  public void failIfKeepAliveTimeNegative() {
    thrown.expect(IllegalArgumentException.class);
    thrown.expectMessage("keepalive time must be positive");

    NettyServerBuilder.forPort(8080).keepAliveTime(-10L, TimeUnit.HOURS);
  }

  @Test
  public void failIfKeepAliveTimeoutNegative() {
    thrown.expect(IllegalArgumentException.class);
    thrown.expectMessage("keepalive timeout must be positive");

    NettyServerBuilder.forPort(8080).keepAliveTimeout(-10L, TimeUnit.HOURS);
  }
}

@@ -1148,55 +1148,15 @@
 %0A%0A  
-@Test%0A  public void sslContextCanBeNull() %7B%0A   
+private
  Net
@@ -1214,16 +1214,63 @@
 t(8080);
+%0A%0A  @Test%0A  public void sslContextCanBeNull() %7B
 %0A    bui
@@ -1458,76 +1458,8 @@
 );%0A%0A
-    NettyServerBuilder builder = NettyServerBuilder.forPort(8080);%0A%0A
     
@@ -1796,40 +1796,15 @@
     
-NettyServerBuilder.forPort(8080)
+builder
 .kee
@@ -2023,62 +2023,1424 @@
     
-NettyServerBuilder.forPort(8080).keepAliveTimeout(-10L
+builder.keepAliveTimeout(-10L, TimeUnit.HOURS);%0A  %7D%0A%0A  @Test%0A  public void failIfMaxConcurrentCallsPerConnectionNegative() %7B%0A    thrown.expect(IllegalArgumentException.class);%0A    thrown.expectMessage(%22max must be positive%22);%0A%0A    builder.maxConcurrentCallsPerConnection(0);%0A  %7D%0A%0A  @Test%0A  public void failIfMaxHeaderListSizeNegative() %7B%0A    thrown.expect(IllegalArgumentException.class);%0A    thrown.expectMessage(%22maxHeaderListSize must be %3E 0%22);%0A%0A    builder.maxHeaderListSize(0);%0A  %7D%0A%0A  @Test%0A  public void failIfMaxConnectionIdleNegative() %7B%0A    thrown.expect(IllegalArgumentException.class);%0A    thrown.expectMessage(%22max connection idle must be positive%22);%0A%0A    builder.maxConnectionIdle(-1, TimeUnit.HOURS);%0A  %7D%0A%0A  @Test%0A  public void failIfMaxConnectionAgeNegative() %7B%0A    thrown.expect(IllegalArgumentException.class);%0A    thrown.expectMessage(%22max connection age must be positive%22);%0A%0A    builder.maxConnectionAge(-1, TimeUnit.HOURS);%0A  %7D%0A%0A  @Test%0A  public void failIfMaxConnectionAgeGraceNegative() %7B%0A    thrown.expect(IllegalArgumentException.class);%0A    thrown.expectMessage(%22max connection age grace must be non-negative%22);%0A%0A    builder.maxConnectionAgeGrace(-1, TimeUnit.HOURS);%0A  %7D%0A%0A  @Test%0A  public void failIfPermitKeepAliveTimeNegative() %7B%0A    thrown.expect(IllegalArgumentException.class);%0A    thrown.expectMessage(%22permit keepalive time must be non-negative%22);%0A%0A    builder.permitKeepAliveTime(-1
 , Ti

add test for NettyServerBuilder