### Question:
GrpcChannelsProvider { public static GrpcChannelsProvider.Builder builder() { return builder(null); } private  GrpcChannelsProvider(Map<String, GrpcChannelDescriptor> channelDescriptors); static GrpcChannelsProvider create(); static GrpcChannelsProvider create(Config config); static GrpcChannelsProvider.Builder builder(); static GrpcChannelsProvider.Builder builder(Config config); ManagedChannel channel(String name); static final String DEFAULT_CHANNEL_NAME; static final String DEFAULT_HOST; static final String CFG_KEY_CHANNELS; static final int DEFAULT_PORT; }### Answer:
@Test public void testDefaultChannelConfiguration() { GrpcChannelDescriptor cfg = GrpcChannelDescriptor.builder().build(); assertThat(cfg.host(), equalTo(DEFAULT_HOST)); assertThat(cfg.port(), equalTo(DEFAULT_PORT)); assertThat(cfg.tlsDescriptor().isPresent(), is(false)); assertThat(cfg.target().isPresent(), is(false)); assertThat(cfg.loadBalancerPolicy().isPresent(), is(false)); assertThat(cfg.nameResolverFactory().isPresent(), is(false)); }
@Test public void testChannelConfigurationWithHost() { GrpcChannelDescriptor cfg = GrpcChannelDescriptor.builder().host("abc.com").build(); assertThat(cfg.host(), equalTo("abc.com")); assertThat(cfg.port(), equalTo(DEFAULT_PORT)); assertThat(cfg.tlsDescriptor().isPresent(), is(false)); assertThat(cfg.target().isPresent(), is(false)); assertThat(cfg.loadBalancerPolicy().isPresent(), is(false)); assertThat(cfg.nameResolverFactory().isPresent(), is(false)); }
@Test public void testChannelConfigurationWithPort() { GrpcChannelDescriptor cfg = GrpcChannelDescriptor.builder().port(4096).build(); assertThat(cfg.host(), equalTo("localhost")); assertThat(cfg.port(), equalTo(4096)); assertThat(cfg.tlsDescriptor().isPresent(), is(false)); assertThat(cfg.target().isPresent(), is(false)); assertThat(cfg.loadBalancerPolicy().isPresent(), is(false)); assertThat(cfg.nameResolverFactory().isPresent(), is(false)); }
@Test public void testChannelConfigurationWithDefaultSsl() { GrpcChannelDescriptor cfg = GrpcChannelDescriptor.builder() .sslDescriptor(GrpcTlsDescriptor.builder().build()) .build(); assertThat(cfg.host(), equalTo("localhost")); assertThat(cfg.port(), equalTo(1408)); Optional<GrpcTlsDescriptor> descriptor = cfg.tlsDescriptor(); assertThat(descriptor.isPresent(), is(true)); assertThat(descriptor.get().isEnabled(), is(true)); assertThat(cfg.target().isPresent(), is(false)); assertThat(cfg.loadBalancerPolicy().isPresent(), is(false)); assertThat(cfg.nameResolverFactory().isPresent(), is(false)); }
@Test public void testChannelConfigurationWithSslConfig() { Resource certResource = mock(Resource.class); Resource keyResource = mock(Resource.class); Resource trustResource = mock(Resource.class); GrpcChannelDescriptor cfg = GrpcChannelDescriptor.builder() .sslDescriptor( GrpcTlsDescriptor.builder() .tlsCaCert(trustResource) .tlsCert(certResource) .tlsKey(keyResource) .build()) .build(); assertThat(cfg.host(), equalTo("localhost")); assertThat(cfg.port(), equalTo(1408)); Optional<GrpcTlsDescriptor> descriptor = cfg.tlsDescriptor(); assertThat(descriptor.isPresent(), is(true)); GrpcTlsDescriptor tlsDescriptor = descriptor.get(); assertThat(tlsDescriptor.isEnabled(), is(true)); assertThat(tlsDescriptor.tlsCaCert(), is(sameInstance(trustResource))); assertThat(tlsDescriptor.tlsCert(), is(sameInstance(certResource))); assertThat(tlsDescriptor.tlsKey(), is(sameInstance(keyResource))); }
@Test public void testChannelWithTarget() { String target = "dns: GrpcChannelDescriptor cfg = GrpcChannelDescriptor.builder() .target(target) .build(); assertThat(cfg.target().isPresent(), is(true)); assertThat(cfg.target().get(), is(target)); }
@Test public void testChannelWithLoadBalancer() { String policy = "round-robin"; GrpcChannelDescriptor cfg = GrpcChannelDescriptor.builder() .loadBalancerPolicy(policy) .build(); assertThat(cfg.loadBalancerPolicy().isPresent(), is(true)); assertThat(cfg.loadBalancerPolicy().get(), is(policy)); }