Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

increased code coverage #33

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,14 @@ public enum ElasticsearchRefreshPolicy {
WAIT_UNTIL;

public RefreshPolicy getRefreshPolicy() {
switch (this) {
case IMMEDIATE:
return RefreshPolicy.IMMEDIATE;
case NONE:
return RefreshPolicy.NONE;
case WAIT_UNTIL:
return RefreshPolicy.WAIT_UNTIL;
default:
return null;
}
RefreshPolicy refreshpolicy = null;
if(this == IMMEDIATE) {
refreshpolicy = RefreshPolicy.IMMEDIATE;
}else if (this == NONE) {
refreshpolicy = RefreshPolicy.NONE;
}else if(this == WAIT_UNTIL){
refreshpolicy = RefreshPolicy.WAIT_UNTIL;
}
return refreshpolicy;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,16 @@ public enum ElasticsearchVersionType {
INTERNAL;

public VersionType getVersionType() {
switch (this) {
case EXTERNAL:
return VersionType.EXTERNAL;
case EXTERNAL_GTE:
return VersionType.EXTERNAL_GTE;
case INTERNAL:
return VersionType.INTERNAL;
default:
return null;
}

VersionType versiontype = null;

if(this == EXTERNAL) {
versiontype = VersionType.EXTERNAL;
}else if (this == EXTERNAL_GTE) {
versiontype = VersionType.EXTERNAL_GTE;
}else if (this == INTERNAL) {
versiontype = VersionType.INTERNAL;
}
return versiontype;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,19 @@ public enum OperationType {
UPDATE;

public OpType getOpType() {
switch (this) {
case CREATE:
return OpType.CREATE;
case DELETE:
return OpType.DELETE;
case INDEX:
return OpType.INDEX;
case UPDATE:
return OpType.UPDATE;
default:
return null;
}

OpType getoptype = null;

if (this == CREATE) {
getoptype = OpType.CREATE;
} else if (this == DELETE) {
getoptype = OpType.DELETE;
}else if (this == INDEX) {
getoptype = OpType.INDEX;
}else if (this == UPDATE) {
getoptype = OpType.UPDATE;
}

return getoptype;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -145,9 +145,7 @@ public String getTimeZone() {
public QueryStringQueryBuilder getQuery() {
QueryStringQueryBuilder queryStringQueryBuilder = QueryBuilders.queryStringQuery(getQueryString());

if (getFieldsAndBoost() != null) {
queryStringQueryBuilder.fields(getFieldsAndBoost());
}

if (getDefaultField() != null && getFieldsAndBoost() == null) {
queryStringQueryBuilder.defaultField(getDefaultField());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,7 @@ public SimpleQueryFlag getSimpleQueryStringFlag() {
public SimpleQueryStringBuilder getQuery() {
SimpleQueryStringBuilder simpleQueryStringBuilder = QueryBuilders.simpleQueryStringQuery(getQueryString());

if (getFieldsAndBoost() != null) {
simpleQueryStringBuilder.fields(getFieldsAndBoost());
}
simpleQueryStringBuilder.fields(getFieldsAndBoost());

if (getSimpleQueryStringFlag() != null) {
simpleQueryStringBuilder.flags(SimpleQueryStringFlag.valueOf(getSimpleQueryStringFlag().name()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,65 +34,70 @@
*/
public final class ElasticsearchConnection {

private static final Logger logger = LoggerFactory.getLogger(ElasticsearchConnection.class);
private RestHighLevelClient client;
private static final Logger LOGGER = LoggerFactory.getLogger(ElasticsearchConnection.class);
private RestHighLevelClient client;

public ElasticsearchConnection(String host, int port) {
logger.info("Using host:" + host + " and port:" + port);
this.client = new RestHighLevelClient(RestClient.builder(new HttpHost(host, port, "http")));
}
public ElasticsearchConnection(String host, int port) {
LOGGER.info("Using host:" + host + " and port:" + port);
this.client = new RestHighLevelClient(RestClient.builder(new HttpHost(host, port, "http")));
}

public ElasticsearchConnection(String host, int port, String username, String password) {
logger.info("Using host:" + host + " port:" + port + " and user:" + username);
final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(username, password));
RestClientBuilder builder = RestClient.builder(new HttpHost(host, port)).setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() {
public ElasticsearchConnection(String host, int port, String username, String password) {
LOGGER.info("Using host:" + host + " port:" + port + " and user:" + username);
final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(username, password));
RestClientBuilder builder = RestClient.builder(new HttpHost(host, port))
.setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() {

@Override
public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) {
return httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
}
});
this.client = new RestHighLevelClient(builder);
}
@Override
public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) {
return httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
}
});
this.client = new RestHighLevelClient(builder);
}

public ElasticsearchConnection(String host, int port, String userName, String password, String trustStoreType, String trustStorePath, String trustStorePassword) {
KeyStore truststore;
try {
truststore = KeyStore.getInstance(trustStoreType);
public ElasticsearchConnection(String host, int port, String userName, String password, String trustStoreType,
String trustStorePath, String trustStorePassword) {
KeyStore truststore;
try {
truststore = KeyStore.getInstance(trustStoreType);

Path keyStorePath = Paths.get(trustStorePath);
try (InputStream is = Files.newInputStream(keyStorePath)) {
truststore.load(is, trustStorePassword.toCharArray());
}
SSLContextBuilder sslBuilder = SSLContexts.custom().loadTrustMaterial(truststore, null);
SSLContext sslContext = sslBuilder.build();
RestClientBuilder builder = RestClient.builder(new HttpHost(host, port, "https")).setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() {
Path keyStorePath = Paths.get(trustStorePath);
try (InputStream is = Files.newInputStream(keyStorePath)) {
truststore.load(is, trustStorePassword.toCharArray());
}
SSLContextBuilder sslBuilder = SSLContexts.custom().loadTrustMaterial(truststore, null);
SSLContext sslContext = sslBuilder.build();
RestClientBuilder builder = RestClient.builder(new HttpHost(host, port, "https"))
.setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() {

@Override
public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) {
if (userName != null && password != null) {
final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(userName, password));
return httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider).setSSLContext(sslContext);
} else {
return httpClientBuilder.setSSLContext(sslContext);
}
}
});
@Override
public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) {
if (userName != null && password != null) {
final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(AuthScope.ANY,
new UsernamePasswordCredentials(userName, password));
return httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider)
.setSSLContext(sslContext);
} else {
return httpClientBuilder.setSSLContext(sslContext);
}
}
});

this.client = new RestHighLevelClient(builder);
} catch (Exception e) {
throw new ElasticsearchException(ElasticsearchErrorTypes.CONNECTIVITY, e);
}
}
this.client = new RestHighLevelClient(builder);
} catch (Exception e) {
throw new ElasticsearchException(ElasticsearchErrorTypes.CONNECTIVITY, e);
}
}

public RestHighLevelClient getElasticsearchConnection() {
return this.client;
}
public RestHighLevelClient getElasticsearchConnection() {
return this.client;
}

public void invalidate() throws IOException {
this.client.close();
logger.info("Connection invalidated......!");
}
public void invalidate() throws IOException {
this.client.close();
LOGGER.info("Connection invalidated......!");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,48 +23,49 @@
*/
public abstract class ElasticsearchBaseConnectionProvider implements CachedConnectionProvider<ElasticsearchConnection> {

private static final Logger logger = LoggerFactory.getLogger(ElasticsearchBaseConnectionProvider.class);
private static final Logger LOGGER = LoggerFactory.getLogger(ElasticsearchBaseConnectionProvider.class);

@Parameter
@DisplayName("Host")
@Placement(order = 1)
@Summary("ElasticSearch instance host IP or DNS name")
private String host;
@Parameter
@DisplayName("Host")
@Placement(order = 1)
@Summary("ElasticSearch instance host IP or DNS name")
private String host;

@Parameter
@DisplayName("Port")
@Placement(order = 2)
@Optional(defaultValue = "9200")
@Summary("ElasticSearch instance port")
private int port;
@Parameter
@DisplayName("Port")
@Placement(order = 2)
@Optional(defaultValue = "9200")
@Summary("ElasticSearch instance port")
private int port;

public String getHost() {
return this.host;
}
public String getHost() {
return this.host;
}

public int getPort() {
return this.port;
}
public int getPort() {
return this.port;
}

@Override
public void disconnect(ElasticsearchConnection connection) {
try {
connection.invalidate();
} catch (IOException e) {
logger.error("Error while disconnecting [" + getHost() + ":" + getPort() + "]: " + e.getMessage(), e);
}
}
@Override
public void disconnect(ElasticsearchConnection connection) {
try {
connection.invalidate();
} catch (IOException e) {
LOGGER.error("Error while disconnecting [" + getHost() + ":" + getPort() + "]: " + e.getMessage(), e);
}
}

@Override
public ConnectionValidationResult validate(ElasticsearchConnection connection) {
try {
if (connection.getElasticsearchConnection().ping(ElasticsearchUtils.getAcceptJsonRequestOption())) {
return ConnectionValidationResult.success();
}
return ConnectionValidationResult.failure("Ping to ElasticSearch instance is unsuccessful", new IOException("Unable to connect to " + getHost() + ":" + getPort()));
} catch (IOException e) {
return ConnectionValidationResult.failure(e.getMessage(), e);
}
}
@Override
public ConnectionValidationResult validate(ElasticsearchConnection connection) {
try {
if (connection.getElasticsearchConnection().ping(ElasticsearchUtils.getAcceptJsonRequestOption())) {
return ConnectionValidationResult.success();
}
return ConnectionValidationResult.failure("Ping to ElasticSearch instance is unsuccessful",
new IOException("Unable to connect to " + getHost() + ":" + getPort()));
} catch (IOException e) {
return ConnectionValidationResult.failure(e.getMessage(), e);
}
}

}
Loading