Skip to content

Commit 2b23be0

Browse files
committed
Add info method to High Level Rest client
This commit adds support for an info() method to the High Level Rest client that returns the cluster information usually obtained by performing a `GET hostname:9200` request.
1 parent 9270f8a commit 2b23be0

File tree

6 files changed

+69
-6
lines changed

6 files changed

+69
-6
lines changed

client/rest-high-level/src/main/java/org/elasticsearch/client/Request.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,10 @@ static Request delete(DeleteRequest deleteRequest) {
9797
return new Request(HttpDelete.METHOD_NAME, endpoint, parameters.getParams(), null);
9898
}
9999

100+
static Request info() {
101+
return new Request("GET", "/", Collections.emptyMap(), null);
102+
}
103+
100104
static Request bulk(BulkRequest bulkRequest) throws IOException {
101105
Params parameters = Params.builder();
102106
parameters.withTimeout(bulkRequest.timeout());

client/rest-high-level/src/main/java/org/elasticsearch/client/RestHighLevelClient.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import org.elasticsearch.action.index.IndexRequest;
3636
import org.elasticsearch.action.index.IndexResponse;
3737
import org.elasticsearch.action.main.MainRequest;
38+
import org.elasticsearch.action.main.MainResponse;
3839
import org.elasticsearch.action.update.UpdateRequest;
3940
import org.elasticsearch.action.update.UpdateResponse;
4041
import org.elasticsearch.common.CheckedFunction;
@@ -113,6 +114,14 @@ public boolean ping(Header... headers) throws IOException {
113114
emptySet(), headers);
114115
}
115116

117+
/**
118+
* Get the cluster info otherwise provided when calling GET `localhost:9200`
119+
*/
120+
public MainResponse info(Header... headers) throws IOException {
121+
return performRequestAndParseEntity(new MainRequest(), (request) -> Request.info(), MainResponse::fromXContent, emptySet(),
122+
headers);
123+
}
124+
116125
/**
117126
* Retrieves a document by id using the Get API
118127
*

client/rest-high-level/src/test/java/org/elasticsearch/client/ESRestHighLevelClientTestCase.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public void initHighLevelClient() throws IOException {
4141
}
4242

4343
@AfterClass
44-
public static void cleanupClient() throws IOException {
44+
public static void cleanupClient() {
4545
restHighLevelClient = null;
4646
}
4747

client/rest-high-level/src/test/java/org/elasticsearch/client/PingAndInfoIT.java

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,33 @@
1919

2020
package org.elasticsearch.client;
2121

22+
import org.elasticsearch.action.main.MainResponse;
23+
2224
import java.io.IOException;
25+
import java.util.Map;
2326

2427
public class PingAndInfoIT extends ESRestHighLevelClientTestCase {
2528

2629
public void testPing() throws IOException {
2730
assertTrue(highLevelClient().ping());
2831
}
2932

30-
//TODO add here integ tests for info api: "GET /" once we have parsing code for MainResponse
33+
@SuppressWarnings("unchecked")
34+
public void testInfo() throws IOException {
35+
MainResponse info = highLevelClient().info();
36+
// compare with what the low level client outputs
37+
Map<String, Object> infoAsMap = entityAsMap(adminClient().performRequest("GET", "/"));
38+
assertEquals(infoAsMap.get("cluster_name"), info.getClusterName().value());
39+
assertEquals(infoAsMap.get("cluster_uuid"), info.getClusterUuid());
40+
41+
// only check node name existence, might be a different one from what was hit by low level client in multi-node cluster
42+
assertNotNull(info.getNodeName());
43+
Map<String, Object> versionMap = (Map<String, Object>) infoAsMap.get("version");
44+
assertEquals(versionMap.get("build_hash"), info.getBuild().shortHash());
45+
assertEquals(versionMap.get("build_date"), info.getBuild().date());
46+
assertEquals(versionMap.get("build_snapshot"), info.getBuild().isSnapshot());
47+
assertEquals(versionMap.get("number"), info.getVersion().toString());
48+
assertEquals(versionMap.get("lucene_version"), info.getVersion().luceneVersion.toString());
49+
}
50+
3151
}

client/rest-high-level/src/test/java/org/elasticsearch/client/RequestTests.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,14 @@ public void testPing() {
6666
assertEquals("HEAD", request.method);
6767
}
6868

69+
public void testInfo() {
70+
Request request = Request.info();
71+
assertEquals("/", request.endpoint);
72+
assertEquals(0, request.params.size());
73+
assertNull(request.entity);
74+
assertEquals("GET", request.method);
75+
}
76+
6977
public void testGet() {
7078
getAndExistsTest(Request::get, "GET");
7179
}

client/rest-high-level/src/test/java/org/elasticsearch/client/RestHighLevelClientTests.java

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
package org.elasticsearch.client;
2121

2222
import com.fasterxml.jackson.core.JsonParseException;
23+
2324
import org.apache.http.Header;
2425
import org.apache.http.HttpEntity;
2526
import org.apache.http.HttpHost;
@@ -33,15 +34,20 @@
3334
import org.apache.http.message.BasicHttpResponse;
3435
import org.apache.http.message.BasicRequestLine;
3536
import org.apache.http.message.BasicStatusLine;
37+
import org.elasticsearch.Build;
3638
import org.elasticsearch.ElasticsearchException;
39+
import org.elasticsearch.Version;
3740
import org.elasticsearch.action.ActionListener;
3841
import org.elasticsearch.action.ActionRequest;
3942
import org.elasticsearch.action.ActionRequestValidationException;
4043
import org.elasticsearch.action.main.MainRequest;
44+
import org.elasticsearch.action.main.MainResponse;
45+
import org.elasticsearch.cluster.ClusterName;
4146
import org.elasticsearch.common.CheckedFunction;
4247
import org.elasticsearch.common.xcontent.NamedXContentRegistry;
4348
import org.elasticsearch.common.xcontent.XContentBuilder;
4449
import org.elasticsearch.common.xcontent.XContentParser;
50+
import org.elasticsearch.common.xcontent.XContentType;
4551
import org.elasticsearch.common.xcontent.cbor.CborXContent;
4652
import org.elasticsearch.common.xcontent.smile.SmileXContent;
4753
import org.elasticsearch.rest.RestStatus;
@@ -59,6 +65,7 @@
5965
import java.util.concurrent.atomic.AtomicInteger;
6066
import java.util.concurrent.atomic.AtomicReference;
6167

68+
import static org.elasticsearch.common.xcontent.XContentHelper.toXContent;
6269
import static org.hamcrest.CoreMatchers.instanceOf;
6370
import static org.mockito.Matchers.anyMapOf;
6471
import static org.mockito.Matchers.anyObject;
@@ -79,7 +86,7 @@ public class RestHighLevelClientTests extends ESTestCase {
7986
private RestHighLevelClient restHighLevelClient;
8087

8188
@Before
82-
public void initClient() throws IOException {
89+
public void initClient() {
8390
restClient = mock(RestClient.class);
8491
restHighLevelClient = new RestHighLevelClient(restClient);
8592
}
@@ -115,6 +122,21 @@ public void testPingSocketTimeout() throws IOException {
115122
Matchers.isNull(HttpEntity.class), argThat(new HeadersVarargMatcher(headers)));
116123
}
117124

125+
public void testInfo() throws IOException {
126+
Header[] headers = RestClientTestUtil.randomHeaders(random(), "Header");
127+
Response response = mock(Response.class);
128+
MainResponse testInfo = new MainResponse("nodeName", Version.CURRENT, new ClusterName("clusterName"), "clusterUuid",
129+
Build.CURRENT, true);
130+
when(response.getEntity()).thenReturn(
131+
new StringEntity(toXContent(testInfo, XContentType.JSON, false).utf8ToString(), ContentType.APPLICATION_JSON));
132+
when(restClient.performRequest(anyString(), anyString(), anyMapOf(String.class, String.class),
133+
anyObject(), anyVararg())).thenReturn(response);
134+
MainResponse receivedInfo = restHighLevelClient.info(headers);
135+
assertEquals(testInfo, receivedInfo);
136+
verify(restClient).performRequest(eq("GET"), eq("/"), eq(Collections.emptyMap()),
137+
Matchers.isNull(HttpEntity.class), argThat(new HeadersVarargMatcher(headers)));
138+
}
139+
118140
public void testRequestValidation() {
119141
ActionRequestValidationException validationException = new ActionRequestValidationException();
120142
validationException.addValidationError("validation error");
@@ -388,7 +410,7 @@ public void testPerformRequestOnResponseExceptionWithIgnoresErrorValidBody() thr
388410
assertEquals("Elasticsearch exception [type=exception, reason=test error message]", elasticsearchException.getMessage());
389411
}
390412

391-
public void testWrapResponseListenerOnSuccess() throws IOException {
413+
public void testWrapResponseListenerOnSuccess() {
392414
{
393415
TrackingActionListener trackingActionListener = new TrackingActionListener();
394416
ResponseListener responseListener = restHighLevelClient.wrapResponseListener(
@@ -414,7 +436,7 @@ public void testWrapResponseListenerOnSuccess() throws IOException {
414436
}
415437
}
416438

417-
public void testWrapResponseListenerOnException() throws IOException {
439+
public void testWrapResponseListenerOnException() {
418440
TrackingActionListener trackingActionListener = new TrackingActionListener();
419441
ResponseListener responseListener = restHighLevelClient.wrapResponseListener(
420442
response -> response.getStatusLine().getStatusCode(), trackingActionListener, Collections.emptySet());
@@ -543,7 +565,7 @@ public void testWrapResponseListenerOnResponseExceptionWithIgnoresErrorValidBody
543565
assertEquals("Elasticsearch exception [type=exception, reason=test error message]", elasticsearchException.getMessage());
544566
}
545567

546-
public void testNamedXContents() throws IOException {
568+
public void testNamedXContents() {
547569
List<NamedXContentRegistry.Entry> namedXContents = RestHighLevelClient.getNamedXContents();
548570
assertEquals(0, namedXContents.size());
549571
}

0 commit comments

Comments
 (0)