ElasticSearchConfiguration { public static ElasticSearchConfiguration fromProperties(Configuration configuration) throws ConfigurationException { return builder() .addHosts(getHosts(configuration)) .hostScheme(getHostScheme(configuration)) .credential(getCredential(configuration)) .sslTrustConfiguration(sslTrustConfiguration(configuration)) .nbShards(configuration.getInteger(ELASTICSEARCH_NB_SHARDS, DEFAULT_NB_SHARDS)) .nbReplica(configuration.getInteger(ELASTICSEARCH_NB_REPLICA, DEFAULT_NB_REPLICA)) .waitForActiveShards(configuration.getInteger(WAIT_FOR_ACTIVE_SHARDS, DEFAULT_WAIT_FOR_ACTIVE_SHARDS)) .minDelay(Optional.ofNullable(configuration.getInteger(ELASTICSEARCH_RETRY_CONNECTION_MIN_DELAY, null))) .maxRetries(Optional.ofNullable(configuration.getInteger(ELASTICSEARCH_RETRY_CONNECTION_MAX_RETRIES, null))) .build(); } private  ElasticSearchConfiguration(ImmutableList<Host> hosts, int nbShards, int nbReplica, int waitForActiveShards, int minDelay, int maxRetries, Duration requestTimeout,
                                       HostScheme hostScheme, Optional<Credential> credential, SSLConfiguration sslConfiguration); static Builder builder(); static ElasticSearchConfiguration fromProperties(Configuration configuration); ImmutableList<Host> getHosts(); int getNbShards(); int getNbReplica(); int getWaitForActiveShards(); int getMinDelay(); int getMaxRetries(); Duration getRequestTimeout(); HostScheme getHostScheme(); Optional<Credential> getCredential(); SSLConfiguration getSslConfiguration(); @Override final boolean equals(Object o); @Override final int hashCode(); static final String ELASTICSEARCH_HOSTS; static final String ELASTICSEARCH_MASTER_HOST; static final String ELASTICSEARCH_PORT; static final String ELASTICSEARCH_HOST_SCHEME; static final String ELASTICSEARCH_HTTPS_SSL_VALIDATION_STRATEGY; static final String ELASTICSEARCH_HTTPS_HOSTNAME_VERIFIER; static final String ELASTICSEARCH_HTTPS_TRUST_STORE_PATH; static final String ELASTICSEARCH_HTTPS_TRUST_STORE_PASSWORD; static final String ELASTICSEARCH_USER; static final String ELASTICSEARCH_PASSWORD; static final String ELASTICSEARCH_NB_REPLICA; static final String WAIT_FOR_ACTIVE_SHARDS; static final String ELASTICSEARCH_NB_SHARDS; static final String ELASTICSEARCH_RETRY_CONNECTION_MIN_DELAY; static final String ELASTICSEARCH_RETRY_CONNECTION_MAX_RETRIES; static final int DEFAULT_CONNECTION_MAX_RETRIES; static final int DEFAULT_CONNECTION_MIN_DELAY; static final Duration DEFAULT_REQUEST_TIMEOUT; static final int DEFAULT_NB_SHARDS; static final int DEFAULT_NB_REPLICA; static final int DEFAULT_WAIT_FOR_ACTIVE_SHARDS; static final int DEFAULT_PORT; static final String LOCALHOST; static final Optional<Integer> DEFAULT_PORT_AS_OPTIONAL; static final HostScheme DEFAULT_SCHEME; static final SSLConfiguration DEFAULT_SSL_TRUST_CONFIGURATION; static final ElasticSearchConfiguration DEFAULT_CONFIGURATION; }
@Test void fromPropertiesShouldThrowWhenOnlyUsername() throws Exception { PropertiesConfiguration configuration = new PropertiesConfiguration(); configuration.addProperty("elasticsearch.hosts", "127.0.0.1"); configuration.addProperty("elasticsearch.user", "username"); assertThatThrownBy(() -> ElasticSearchConfiguration.fromProperties(configuration)) .isInstanceOf(NullPointerException.class) .hasMessage("password cannot be null when username is specified"); }
@Test void fromPropertiesShouldThrowWhenOnlyPassword() throws Exception { PropertiesConfiguration configuration = new PropertiesConfiguration(); configuration.addProperty("elasticsearch.hosts", "127.0.0.1"); configuration.addProperty("elasticsearch.password", "password"); assertThatThrownBy(() -> ElasticSearchConfiguration.fromProperties(configuration)) .isInstanceOf(NullPointerException.class) .hasMessage("username cannot be null when password is specified"); }
@Test void fromPropertiesShouldThrowWhenInvalidValue() throws Exception { PropertiesConfiguration configuration = new PropertiesConfiguration(); configuration.addProperty("elasticsearch.hosts", "127.0.0.1"); configuration.addProperty("elasticsearch.hostScheme", "invalid-protocol"); assertThatThrownBy(() -> ElasticSearchConfiguration.fromProperties(configuration)) .isInstanceOf(IllegalArgumentException.class) .hasMessage("Unknown HostScheme 'invalid-protocol'"); }