Improved test coverage and added support to search in ES
diff --git a/org.eclipse.opencert.elastic.test/src/org/eclipse/opencert/elastic/AllTests.java b/org.eclipse.opencert.elastic.test/src/org/eclipse/opencert/elastic/AllTests.java
index e4f9671..6d23e7f 100644
--- a/org.eclipse.opencert.elastic.test/src/org/eclipse/opencert/elastic/AllTests.java
+++ b/org.eclipse.opencert.elastic.test/src/org/eclipse/opencert/elastic/AllTests.java
@@ -16,9 +16,8 @@
import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses;
-@SuppressWarnings("javadoc")
@RunWith(Suite.class)
-@SuiteClasses({ ElasticClientTest.class, EObjectToJsonTest.class })
+@SuiteClasses({ ElasticClientTest.class, ElasticFinderTest.class, EObjectToJsonTest.class, HitResolutionTest.class })
public class AllTests {
// empty by intention
}
diff --git a/org.eclipse.opencert.elastic.test/src/org/eclipse/opencert/elastic/EObjectToJsonTest.java b/org.eclipse.opencert.elastic.test/src/org/eclipse/opencert/elastic/EObjectToJsonTest.java
index 3417dc2..9e5ff6a 100644
--- a/org.eclipse.opencert.elastic.test/src/org/eclipse/opencert/elastic/EObjectToJsonTest.java
+++ b/org.eclipse.opencert.elastic.test/src/org/eclipse/opencert/elastic/EObjectToJsonTest.java
@@ -17,7 +17,6 @@
import com.google.gson.JsonObject;
-@SuppressWarnings({ "javadoc" })
public class EObjectToJsonTest {
@Test
diff --git a/org.eclipse.opencert.elastic.test/src/org/eclipse/opencert/elastic/ElasticClientTest.java b/org.eclipse.opencert.elastic.test/src/org/eclipse/opencert/elastic/ElasticClientTest.java
index 4ecf8ae..520506f 100644
--- a/org.eclipse.opencert.elastic.test/src/org/eclipse/opencert/elastic/ElasticClientTest.java
+++ b/org.eclipse.opencert.elastic.test/src/org/eclipse/opencert/elastic/ElasticClientTest.java
@@ -12,16 +12,18 @@
******************************************************************************/
package org.eclipse.opencert.elastic;
+import java.util.Set;
+
import org.eclipse.uml2.uml.Component;
+import org.junit.Assert;
import org.junit.Test;
-@SuppressWarnings({ "nls", "javadoc" })
public class ElasticClientTest {
@Test
public void testPing() throws Exception {
System.out.println("Send a ping to local Elastic server...");
- ElasticClientImpl.on("localhost", 9200, "http").ping();
+ ElasticClientImpl.on("localhost", 9200, "http").ping().close();
System.out.println("Done. Server seems to be alive.");
}
@@ -29,6 +31,16 @@
public void testSend() throws Exception {
Component object = TestData.createComponent();
ElasticDocument document = EObjectToDocument.INSTANCE.convert(object, "amass-test");
- ElasticClientImpl.on("localhost", 9200, "http").store(document);
+ ElasticClientImpl.on("localhost", 9200, "http").store(document).close();
+ }
+
+ @Test
+ public void testSendAndSearch() throws Exception {
+ Component object = TestData.createComponent();
+ ElasticDocument document = EObjectToDocument.INSTANCE.convert(object, "amass-test");
+ ElasticClient client = ElasticClientImpl.on("localhost", 9200, "http")/*.delete("amass-test")*/.store(document);
+ Set<ElasticDocument> hits = client.search("amass-test");
+ Assert.assertEquals(1, hits.size());
+ client.close();
}
}
diff --git a/org.eclipse.opencert.elastic.test/src/org/eclipse/opencert/elastic/ElasticFinderTest.java b/org.eclipse.opencert.elastic.test/src/org/eclipse/opencert/elastic/ElasticFinderTest.java
index 919cfc2..7d2062a 100644
--- a/org.eclipse.opencert.elastic.test/src/org/eclipse/opencert/elastic/ElasticFinderTest.java
+++ b/org.eclipse.opencert.elastic.test/src/org/eclipse/opencert/elastic/ElasticFinderTest.java
@@ -19,14 +19,11 @@
import org.eclipse.opencert.elastic.search.ElasticFinderImpl;
import org.eclipse.opencert.elastic.search.Hit;
import org.junit.Assert;
-import org.junit.Ignore;
import org.junit.Test;
-@SuppressWarnings({ "nls", "javadoc" })
public class ElasticFinderTest {
@Test
- @Ignore
public void testSearch() throws Exception {
ElasticClient client = ElasticClientImpl.on("localhost", 9200, "http");
ElasticFinderImpl.onClient(client).search("this AND that OR thus", null);
diff --git a/org.eclipse.opencert.elastic.test/src/org/eclipse/opencert/elastic/HitResolutionTest.java b/org.eclipse.opencert.elastic.test/src/org/eclipse/opencert/elastic/HitResolutionTest.java
index fce76b8..45f078f 100644
--- a/org.eclipse.opencert.elastic.test/src/org/eclipse/opencert/elastic/HitResolutionTest.java
+++ b/org.eclipse.opencert.elastic.test/src/org/eclipse/opencert/elastic/HitResolutionTest.java
@@ -25,7 +25,6 @@
import org.junit.Assert;
import org.junit.Test;
-@SuppressWarnings({ "nls", "javadoc" })
public class HitResolutionTest {
@Test
diff --git a/org.eclipse.opencert.elastic.test/src/org/eclipse/opencert/elastic/TestData.java b/org.eclipse.opencert.elastic.test/src/org/eclipse/opencert/elastic/TestData.java
index e0e80d7..165d3d7 100644
--- a/org.eclipse.opencert.elastic.test/src/org/eclipse/opencert/elastic/TestData.java
+++ b/org.eclipse.opencert.elastic.test/src/org/eclipse/opencert/elastic/TestData.java
@@ -27,7 +27,6 @@
*
* @author mauersberger
*/
-@SuppressWarnings({ "nls" })
public class TestData {
static Component createComponent() {
diff --git a/org.eclipse.opencert.elastic/src/org/eclipse/opencert/elastic/ElasticClient.java b/org.eclipse.opencert.elastic/src/org/eclipse/opencert/elastic/ElasticClient.java
index 345c1a1..2f36b41 100644
--- a/org.eclipse.opencert.elastic/src/org/eclipse/opencert/elastic/ElasticClient.java
+++ b/org.eclipse.opencert.elastic/src/org/eclipse/opencert/elastic/ElasticClient.java
@@ -12,12 +12,15 @@
******************************************************************************/
package org.eclipse.opencert.elastic;
+import java.io.Closeable;
+import java.util.Set;
+
/**
* Simple client side interface to talk to an Elastic server via REST.
*
* @author mauersberger
*/
-public interface ElasticClient {
+public interface ElasticClient extends Closeable {
/**
* Store the given {@link de.ikv.analyze.census.elasticsearch.ElasticDocument}
@@ -49,6 +52,17 @@
ElasticClient delete(String indexName) throws Exception;
/**
+ * Searches the given index. <strong>This is preliminary API and not closed yet!<strong>
+ *
+ * @param indexName
+ * the name of the index
+ * @return a set of {@link ElasticDocument}s
+ * @throws Exception
+ * on failure
+ */
+ Set<ElasticDocument> search(String indexName) throws Exception;
+
+ /**
* Retrieves the server version.
*
* @return the version string
diff --git a/org.eclipse.opencert.elastic/src/org/eclipse/opencert/elastic/ElasticClientImpl.java b/org.eclipse.opencert.elastic/src/org/eclipse/opencert/elastic/ElasticClientImpl.java
index ebe45cd..4749caa 100644
--- a/org.eclipse.opencert.elastic/src/org/eclipse/opencert/elastic/ElasticClientImpl.java
+++ b/org.eclipse.opencert.elastic/src/org/eclipse/opencert/elastic/ElasticClientImpl.java
@@ -17,14 +17,23 @@
import java.io.InputStreamReader;
import java.util.Collections;
import java.util.Date;
+import java.util.HashSet;
+import java.util.Set;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.entity.ContentType;
import org.apache.http.impl.nio.client.DefaultAsyncUserTokenHandler;
import org.apache.http.nio.entity.NStringEntity;
+import org.elasticsearch.action.search.SearchRequest;
+import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.Response;
import org.elasticsearch.client.RestClient;
+import org.elasticsearch.client.RestHighLevelClient;
+import org.elasticsearch.index.query.QueryBuilders;
+import org.elasticsearch.search.SearchHit;
+import org.elasticsearch.search.SearchHits;
+import org.elasticsearch.search.builder.SearchSourceBuilder;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
@@ -34,11 +43,14 @@
*
* @author mauersberger
*/
-public class ElasticClientImpl implements ElasticClient {
+public class ElasticClientImpl implements ElasticClient, AutoCloseable {
// the "real" client
private RestClient restClient;
+ @SuppressWarnings("unused")
+ private int lastStatus;
+
/**
* Factory method to create a new {@link ElasticClient}.
*
@@ -66,25 +78,54 @@
@Override
public ElasticClient store(ElasticDocument document) throws IOException {
HttpEntity entity = new NStringEntity(document.source.toString(), ContentType.APPLICATION_JSON);
- this.restClient.performRequest("PUT", document.getEndPoint(), Collections.emptyMap(), entity); //$NON-NLS-1$
+ Response response = this.restClient.performRequest("PUT", document.getEndPoint(), Collections.emptyMap(), entity); //$NON-NLS-1$
+ lastStatus = response.getStatusLine().getStatusCode();
return this;
}
@Override
+ public Set<ElasticDocument> search(String indexName) throws Exception {
+ RestHighLevelClient highLevel = new RestHighLevelClient(this.restClient);
+ SearchRequest searchRequest = new SearchRequest(indexName);
+ // see example at https://dzone.com/articles/java-high-level-rest-client-elasticsearch
+ SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
+ sourceBuilder.query(QueryBuilders.matchAllQuery());
+ searchRequest.source(sourceBuilder);
+
+ SearchResponse response = highLevel.search(searchRequest);
+ lastStatus = response.status().getStatus();
+
+ Set<ElasticDocument> result = new HashSet<>();
+ SearchHits hits = response.getHits();
+ for (int i=0; i< hits.totalHits; i++) {
+ SearchHit hit = hits.getAt(i);
+ // TODO Convert Hit to Json
+ JsonObject json = new JsonObject();
+ ElasticDocument doc = new ElasticDocument(json, hit.getIndex(), hit.getType(), hit.getId());
+ result.add(doc);
+ }
+
+ return result;
+ }
+
+ @Override
public ElasticClient ping() throws Exception {
- this.restClient.performRequest("GET", "/"); //$NON-NLS-1$ //$NON-NLS-2$
+ Response response = this.restClient.performRequest("GET", "/"); //$NON-NLS-1$ //$NON-NLS-2$
+ lastStatus = response.getStatusLine().getStatusCode();
return this;
}
@Override
public ElasticClient delete(String indexName) throws Exception {
- this.restClient.performRequest("DELETE", "/" + indexName, Collections.emptyMap()); //$NON-NLS-1$ //$NON-NLS-2$
+ Response response = this.restClient.performRequest("DELETE", "/" + indexName, Collections.emptyMap()); //$NON-NLS-1$ //$NON-NLS-2$
+ lastStatus = response.getStatusLine().getStatusCode();
return this;
}
@Override
public String version() throws IOException {
Response response = this.restClient.performRequest("GET", "/"); //$NON-NLS-1$ //$NON-NLS-2$
+ lastStatus = response.getStatusLine().getStatusCode();
// we assume its a JSON response
try (InputStream content = response.getEntity().getContent()) {
@@ -99,6 +140,7 @@
@Override
public String indexStatus(String indexName) throws Exception {
Response response = this.restClient.performRequest("GET", "/" + indexName); //$NON-NLS-1$ //$NON-NLS-2$
+ lastStatus = response.getStatusLine().getStatusCode();
// we assume its a JSON response
try (InputStream content = response.getEntity().getContent()) {
@@ -114,4 +156,12 @@
throw new IOException("Unable to ping index", exception); //$NON-NLS-1$
}
}
+
+ @Override
+ public void close() throws IOException {
+ if (restClient != null) {
+ restClient.close();
+ restClient = null;
+ }
+ }
}
diff --git a/org.eclipse.opencert.elastic/src/org/eclipse/opencert/elastic/search/ElasticFinderImpl.java b/org.eclipse.opencert.elastic/src/org/eclipse/opencert/elastic/search/ElasticFinderImpl.java
index 817e209..8af3a79 100644
--- a/org.eclipse.opencert.elastic/src/org/eclipse/opencert/elastic/search/ElasticFinderImpl.java
+++ b/org.eclipse.opencert.elastic/src/org/eclipse/opencert/elastic/search/ElasticFinderImpl.java
@@ -13,6 +13,7 @@
package org.eclipse.opencert.elastic.search;
import java.util.Collection;
+import java.util.Collections;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
@@ -29,6 +30,8 @@
// XXX hack to get some API tests running
private Collection<Object> dummyData = null;
+ private ElasticClient client;
+
/**
* Factory method to create a new {@link ElasticFinder}. XXX This is an
* intermediate feature.
@@ -48,8 +51,9 @@
}
public static ElasticFinder onClient(ElasticClient client) {
- // TODO Create a finder that uses the given elastic client
- throw new RuntimeException("not implemented");
+ ElasticFinderImpl impl = new ElasticFinderImpl();
+ impl.client = client;
+ return impl;
}
/*
@@ -64,8 +68,15 @@
if (dummyData != null) {
return searchInDummyData(query, filters);
}
- // TODO Implement
- throw new RuntimeException("not impleemented");
+
+ try {
+ this.client.search("_all");
+ } catch (Exception e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+
+ return Collections.emptySet();
}
/*