ClientConfigurations { public static ClientConfiguration of(ServiceConfiguration config) { return ClientConfiguration.builder() .sslSocketFactory(SslSocketFactories.createSslSocketFactory(config.security())) .trustManager(SslSocketFactories.createX509TrustManager(config.security())) .uris(config.uris()) .connectTimeout(config.connectTimeout().orElse(DEFAULT_CONNECT_TIMEOUT)) .readTimeout(config.readTimeout().orElse(DEFAULT_READ_TIMEOUT)) .writeTimeout(config.writeTimeout().orElse(DEFAULT_WRITE_TIMEOUT)) .enableGcmCipherSuites(config.enableGcmCipherSuites().orElse(DEFAULT_ENABLE_GCM_CIPHERS)) .fallbackToCommonNameVerification( config.fallbackToCommonNameVerification().orElse(DEFAULT_FALLBACK_TO_COMMON_NAME_VERIFICATION)) .proxy(config.proxy() .map(ClientConfigurations::createProxySelector) .orElseGet(ProxySelector::getDefault)) .proxyCredentials(config.proxy().flatMap(ProxyConfiguration::credentials)) .meshProxy(meshProxy(config.proxy())) .maxNumRetries(config.maxNumRetries().orElse(DEFAULT_MAX_NUM_RETRIES)) .nodeSelectionStrategy(DEFAULT_NODE_SELECTION_STRATEGY) .failedUrlCooldown(DEFAULT_FAILED_URL_COOLDOWN) .backoffSlotSize(config.backoffSlotSize().orElse(DEFAULT_BACKOFF_SLOT_SIZE)) .clientQoS(CLIENT_QOS_DEFAULT) .serverQoS(PROPAGATE_QOS_DEFAULT) .retryOnTimeout(RETRY_ON_TIMEOUT_DEFAULT) .retryOnSocketException(RETRY_ON_SOCKET_EXCEPTION_DEFAULT) .taggedMetricRegistry(SharedTaggedMetricRegistries.getSingleton()) .build(); } private  ClientConfigurations(); static ClientConfiguration of(ServiceConfiguration config); static ClientConfiguration of(
            List<String> uris, SSLSocketFactory sslSocketFactory, X509TrustManager trustManager); static ClientConfiguration of(
            List<String> uris, SSLSocketFactory sslSocketFactory, X509TrustManager trustManager, UserAgent userAgent); static ProxySelector createProxySelector(ProxyConfiguration proxyConfig);  }
@Test public void testFromServiceConfig_fillsInDefaults() { ServiceConfiguration serviceConfig = ServiceConfiguration.builder() .uris(uris) .security(SslConfiguration.of(Paths.get("src/test/resources/trustStore.jks"))) .build(); ClientConfiguration actual = ClientConfigurations.of(serviceConfig); assertThat(actual.sslSocketFactory()).isNotNull(); assertThat(actual.trustManager()).isNotNull(); assertThat(actual.uris()).isEqualTo(uris); assertThat(actual.connectTimeout()).isEqualTo(Duration.ofSeconds(10)); assertThat(actual.readTimeout()).isEqualTo(Duration.ofMinutes(5)); assertThat(actual.writeTimeout()).isEqualTo(Duration.ofMinutes(5)); assertThat(actual.enableGcmCipherSuites()) .as("GCM ciphers should be used by default with JRE9+") .isNotEqualTo("1.8".equals(System.getProperty("java.specification.version"))); assertThat(actual.enableHttp2()).isEmpty(); assertThat(actual.fallbackToCommonNameVerification()).isFalse(); assertThat(actual.proxy().select(URI.create("https: assertThat(actual.taggedMetricRegistry()).isSameAs(DefaultTaggedMetricRegistry.getDefault()); }
@Test public void testFromParameters_fillsInDefaults() { SSLSocketFactory sslFactory = mock(SSLSocketFactory.class); X509TrustManager trustManager = mock(X509TrustManager.class); ClientConfiguration actual = ClientConfigurations.of(uris, sslFactory, trustManager); assertThat(actual.sslSocketFactory()).isEqualTo(sslFactory); assertThat(actual.trustManager()).isEqualTo(trustManager); assertThat(actual.uris()).isEqualTo(uris); assertThat(actual.connectTimeout()).isEqualTo(Duration.ofSeconds(10)); assertThat(actual.readTimeout()).isEqualTo(Duration.ofMinutes(5)); assertThat(actual.writeTimeout()).isEqualTo(Duration.ofMinutes(5)); assertThat(actual.enableGcmCipherSuites()) .as("GCM ciphers should be used by default with JRE9+") .isNotEqualTo("1.8".equals(System.getProperty("java.specification.version"))); assertThat(actual.enableHttp2()).isEmpty(); assertThat(actual.fallbackToCommonNameVerification()).isFalse(); assertThat(actual.proxy().select(URI.create("https: }
@Test public void testTimeoutMustBeMilliseconds() { ServiceConfiguration serviceConfig = ServiceConfiguration.builder() .uris(uris) .security(SslConfiguration.of(Paths.get("src/test/resources/trustStore.jks"))) .connectTimeout(Duration.ofNanos(5)) .build(); Assertions.assertThatLoggableExceptionThrownBy(() -> ClientConfigurations.of(serviceConfig)) .hasLogMessage("Timeouts with sub-millisecond precision are not supported"); }
@Test public void meshProxy_exactlyOneUri() throws Exception { assertThatThrownBy(() -> ClientConfigurations.of(meshProxyServiceConfig(ImmutableList.of("uri1", "uri2"), 0))) .isInstanceOf(IllegalArgumentException.class) .hasMessage("If meshProxy is configured then uris must contain exactly 1 URI"); }
@Test public void overriding_tagged_metric_registry_is_convenient() { ServiceConfiguration serviceConfig = ServiceConfiguration.builder() .uris(uris) .security(SslConfiguration.of(Paths.get("src/test/resources/trustStore.jks"))) .build(); ClientConfiguration overridden = ClientConfiguration.builder() .from(ClientConfigurations.of(serviceConfig)) .taggedMetricRegistry(new DefaultTaggedMetricRegistry()) .build(); assertThat(overridden.taggedMetricRegistry()).isNotSameAs(DefaultTaggedMetricRegistry.getDefault()); }
@Test public void sensible_equality_and_hashcode() { SslConfiguration sslConfiguration = SslConfiguration.of(Paths.get("src/test/resources/trustStore.jks")); SSLSocketFactory socketFactory = SslSocketFactories.createSslSocketFactory(sslConfiguration); X509TrustManager x509TrustManager = SslSocketFactories.createX509TrustManager(sslConfiguration); ClientConfiguration instance1 = ClientConfigurations.of(uris, socketFactory, x509TrustManager); ClientConfiguration instance2 = ClientConfigurations.of(uris, socketFactory, x509TrustManager); assertThat(instance1).isEqualTo(instance2); assertThat(instance1).hasSameHashCodeAs(instance2); }