Skip to content

Commit b901335

Browse files
MrCitronjbaiera
authored andcommitted
Fix NPE with old ES version (#1304)
1 parent 0315d8e commit b901335

File tree

2 files changed

+49
-1
lines changed

2 files changed

+49
-1
lines changed

mr/src/main/java/org/elasticsearch/hadoop/rest/RestClient.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -694,7 +694,7 @@ public ClusterInfo mainInfo() {
694694
throw new EsHadoopIllegalStateException("Unable to retrieve elasticsearch main cluster info.");
695695
}
696696
String clusterName = result.get("cluster_name").toString();
697-
String clusterUUID = result.get("cluster_uuid").toString();
697+
String clusterUUID = (String)result.get("cluster_uuid");
698698
@SuppressWarnings("unchecked")
699699
Map<String, String> versionBody = (Map<String, String>) result.get("version");
700700
if (versionBody == null || !StringUtils.hasText(versionBody.get("number"))) {

mr/src/test/java/org/elasticsearch/hadoop/rest/RestClientTest.java

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
import org.mockito.Mockito;
2929

3030
import static org.junit.Assert.assertEquals;
31+
import static org.junit.Assert.assertNotNull;
32+
import static org.junit.Assert.assertNull;
3133
import static org.junit.Assert.fail;
3234

3335
public class RestClientTest {
@@ -241,4 +243,50 @@ public void testCount7xBadRelation() throws Exception {
241243

242244
assertEquals(5L, count);
243245
}
246+
247+
@Test
248+
public void testMainInfoWithClusterNotProvidingUUID() {
249+
String response = "{\n" +
250+
"\"name\": \"node\",\n" +
251+
"\"cluster_name\": \"cluster\",\n" +
252+
"\"version\": {\n" +
253+
" \"number\": \"2.0.1\"\n" +
254+
"},\n" +
255+
"\"tagline\": \"You Know, for Search\"\n" +
256+
"}";
257+
258+
NetworkClient mock = Mockito.mock(NetworkClient.class);
259+
Mockito.when(mock.execute(Mockito.any(SimpleRequest.class))).thenReturn(new SimpleResponse(201, new FastByteArrayInputStream(new BytesArray(response)), "localhost:9200"));
260+
261+
RestClient client = new RestClient(new TestSettings(), mock);
262+
263+
ClusterInfo clusterInfo = client.mainInfo();
264+
265+
assertNotNull(clusterInfo.getClusterName());
266+
assertNull(clusterInfo.getClusterName().getUUID());
267+
}
268+
269+
@Test
270+
public void testMainInfoWithClusterProvidingUUID() {
271+
String response = "{\n" +
272+
"\"name\": \"node\",\n" +
273+
"\"cluster_name\": \"cluster\",\n" +
274+
"\"cluster_uuid\": \"uuid\",\n" +
275+
"\"version\": {\n" +
276+
" \"number\": \"6.7.0\"\n" +
277+
"},\n" +
278+
"\"tagline\": \"You Know, for Search\"\n" +
279+
"}";
280+
281+
NetworkClient mock = Mockito.mock(NetworkClient.class);
282+
Mockito.when(mock.execute(Mockito.any(SimpleRequest.class))).thenReturn(new SimpleResponse(201, new FastByteArrayInputStream(new BytesArray(response)), "localhost:9200"));
283+
284+
RestClient client = new RestClient(new TestSettings(), mock);
285+
286+
ClusterInfo clusterInfo = client.mainInfo();
287+
288+
assertNotNull(clusterInfo.getClusterName());
289+
assertEquals("uuid", clusterInfo.getClusterName().getUUID());
290+
}
291+
244292
}

0 commit comments

Comments
 (0)