Skip to content

Commit e9aada0

Browse files
MrCitronjbaiera
authored andcommitted
Fix NPE with old ES version (#1304)
1 parent d1596ac commit e9aada0

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
@@ -737,7 +737,7 @@ public ClusterInfo mainInfo() {
737737
throw new EsHadoopIllegalStateException("Unable to retrieve elasticsearch main cluster info.");
738738
}
739739
String clusterName = result.get("cluster_name").toString();
740-
String clusterUUID = result.get("cluster_uuid").toString();
740+
String clusterUUID = (String)result.get("cluster_uuid");
741741
@SuppressWarnings("unchecked")
742742
Map<String, String> versionBody = (Map<String, String>) result.get("version");
743743
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
@@ -30,6 +30,8 @@
3030
import org.mockito.Mockito;
3131

3232
import static org.junit.Assert.assertEquals;
33+
import static org.junit.Assert.assertNotNull;
34+
import static org.junit.Assert.assertNull;
3335
import static org.junit.Assert.fail;
3436

3537
public class RestClientTest {
@@ -406,4 +408,50 @@ public void testCount7xBadRelation() throws Exception {
406408

407409
assertEquals(5L, count);
408410
}
411+
412+
@Test
413+
public void testMainInfoWithClusterNotProvidingUUID() {
414+
String response = "{\n" +
415+
"\"name\": \"node\",\n" +
416+
"\"cluster_name\": \"cluster\",\n" +
417+
"\"version\": {\n" +
418+
" \"number\": \"2.0.1\"\n" +
419+
"},\n" +
420+
"\"tagline\": \"You Know, for Search\"\n" +
421+
"}";
422+
423+
NetworkClient mock = Mockito.mock(NetworkClient.class);
424+
Mockito.when(mock.execute(Mockito.any(SimpleRequest.class))).thenReturn(new SimpleResponse(201, new FastByteArrayInputStream(new BytesArray(response)), "localhost:9200"));
425+
426+
RestClient client = new RestClient(new TestSettings(), mock);
427+
428+
ClusterInfo clusterInfo = client.mainInfo();
429+
430+
assertNotNull(clusterInfo.getClusterName());
431+
assertNull(clusterInfo.getClusterName().getUUID());
432+
}
433+
434+
@Test
435+
public void testMainInfoWithClusterProvidingUUID() {
436+
String response = "{\n" +
437+
"\"name\": \"node\",\n" +
438+
"\"cluster_name\": \"cluster\",\n" +
439+
"\"cluster_uuid\": \"uuid\",\n" +
440+
"\"version\": {\n" +
441+
" \"number\": \"6.7.0\"\n" +
442+
"},\n" +
443+
"\"tagline\": \"You Know, for Search\"\n" +
444+
"}";
445+
446+
NetworkClient mock = Mockito.mock(NetworkClient.class);
447+
Mockito.when(mock.execute(Mockito.any(SimpleRequest.class))).thenReturn(new SimpleResponse(201, new FastByteArrayInputStream(new BytesArray(response)), "localhost:9200"));
448+
449+
RestClient client = new RestClient(new TestSettings(), mock);
450+
451+
ClusterInfo clusterInfo = client.mainInfo();
452+
453+
assertNotNull(clusterInfo.getClusterName());
454+
assertEquals("uuid", clusterInfo.getClusterName().getUUID());
455+
}
456+
409457
}

0 commit comments

Comments
 (0)