@Override public boolean hasFailed() { return !hasSucceeded(); } }
@Override public boolean hasSucceeded() { return isSuccess(); }
return new SimpleResponse(http.getStatusCode(), new ResponseInputStream(http), httpInfo);
Mockito.when(mock.execute(Mockito.eq(request))).thenReturn(new SimpleResponse(201, new FastByteArrayInputStream(new BytesArray(response)), "localhost:9200"));
Mockito.when(mock.execute(Mockito.eq(request))).thenReturn(new SimpleResponse(201, new FastByteArrayInputStream(new BytesArray(response)), "localhost:9200"));
@Override public boolean hasFailed() { return !hasSucceeded(); } }
@Override public boolean hasSucceeded() { return isSuccess(); }
Mockito.when(mock.execute(Mockito.eq(request))).thenReturn(new SimpleResponse(201, new FastByteArrayInputStream(new BytesArray(response)), "localhost:9200"));
@Override public boolean hasFailed() { return !hasSucceeded(); } }
@Override public boolean hasSucceeded() { return isSuccess(); }
.thenReturn(new SimpleResponse(201, new FastByteArrayInputStream(new BytesArray(response)), "localhost:9200")) .thenReturn(new SimpleResponse(201, new FastByteArrayInputStream(new BytesArray(response)), "localhost:9200"));
@Override public boolean hasFailed() { return !hasSucceeded(); } }
@Override public boolean hasSucceeded() { return isSuccess(); }
@Test public void testPostTypelessDocumentSuccess() throws Exception { String index = "index"; Settings settings = new TestSettings(); settings.setInternalVersion(EsMajorVersion.V_7_X); settings.setResourceWrite(index); Resource writeResource = new Resource(settings, false); BytesArray document = new BytesArray("{\"field\":\"value\"}"); SimpleRequest request = new SimpleRequest(Request.Method.POST, null, index + "/_doc", null, document); String response = "{\n" + " \"_index\": \"index\",\n" + " \"_type\": \"_doc\",\n" + " \"_id\": \"AbcDefGhiJklMnoPqrS_\",\n" + " \"_version\": 1,\n" + " \"result\": \"created\",\n" + " \"_shards\": {\n" + " \"total\": 2,\n" + " \"successful\": 1,\n" + " \"failed\": 0\n" + " },\n" + " \"_seq_no\": 0,\n" + " \"_primary_term\": 1\n" + "}"; NetworkClient mock = Mockito.mock(NetworkClient.class); Mockito.when(mock.execute(Mockito.eq(request))).thenReturn(new SimpleResponse(201, new FastByteArrayInputStream(new BytesArray(response)), "localhost:9200")); RestClient client = new RestClient(new TestSettings(), mock); String id = client.postDocument(writeResource, document); assertEquals("AbcDefGhiJklMnoPqrS_", id); }
@Override public boolean hasFailed() { return !hasSucceeded(); } }
@Override public boolean hasSucceeded() { return isSuccess(); }
@Test(expected = EsHadoopInvalidRequest.class) public void testPostTypelessDocumentWeirdness() throws Exception { String index = "index"; Settings settings = new TestSettings(); settings.setInternalVersion(EsMajorVersion.V_7_X); settings.setResourceWrite(index); Resource writeResource = new Resource(settings, false); BytesArray document = new BytesArray("{\"field\":\"value\"}"); SimpleRequest request = new SimpleRequest(Request.Method.POST, null, index + "/_doc", null, document); String response = "{\n" + " \"_index\": \"index\",\n" + " \"_type\": \"_doc\",\n" + " \"definitely_not_an_id\": \"AbcDefGhiJklMnoPqrS_\",\n" + // Make the ID go away " \"_version\": 1,\n" + " \"result\": \"created\",\n" + " \"_shards\": {\n" + " \"total\": 2,\n" + " \"successful\": 1,\n" + " \"failed\": 0\n" + " },\n" + " \"_seq_no\": 0,\n" + " \"_primary_term\": 1\n" + "}"; NetworkClient mock = Mockito.mock(NetworkClient.class); Mockito.when(mock.execute(Mockito.eq(request))).thenReturn(new SimpleResponse(201, new FastByteArrayInputStream(new BytesArray(response)), "localhost:9200")); RestClient client = new RestClient(new TestSettings(), mock); String id = client.postDocument(writeResource, document); assertEquals("AbcDefGhiJklMnoPqrS_", id); }
@Test public void testPostDocumentSuccess() throws Exception { String index = "index/type"; Settings settings = new TestSettings(); settings.setInternalVersion(EsMajorVersion.V_7_X); settings.setResourceWrite(index); Resource writeResource = new Resource(settings, false); BytesArray document = new BytesArray("{\"field\":\"value\"}"); SimpleRequest request = new SimpleRequest(Request.Method.POST, null, index, null, document); String response = "{\n" + " \"_index\": \"index\",\n" + " \"_type\": \"type\",\n" + " \"_id\": \"AbcDefGhiJklMnoPqrS_\",\n" + " \"_version\": 1,\n" + " \"result\": \"created\",\n" + " \"_shards\": {\n" + " \"total\": 2,\n" + " \"successful\": 1,\n" + " \"failed\": 0\n" + " },\n" + " \"_seq_no\": 0,\n" + " \"_primary_term\": 1\n" + "}"; NetworkClient mock = Mockito.mock(NetworkClient.class); Mockito.when(mock.execute(Mockito.eq(request))).thenReturn(new SimpleResponse(201, new FastByteArrayInputStream(new BytesArray(response)), "localhost:9200")); RestClient client = new RestClient(new TestSettings(), mock); String id = client.postDocument(writeResource, document); assertEquals("AbcDefGhiJklMnoPqrS_", id); }
@Test(expected = EsHadoopInvalidRequest.class) public void testPostDocumentWeirdness() throws Exception { String index = "index/type"; Settings settings = new TestSettings(); settings.setInternalVersion(EsMajorVersion.V_6_X); settings.setResourceWrite(index); Resource writeResource = new Resource(settings, false); BytesArray document = new BytesArray("{\"field\":\"value\"}"); SimpleRequest request = new SimpleRequest(Request.Method.POST, null, index, null, document); String response = "{\n" + " \"_index\": \"index\",\n" + " \"_type\": \"type\",\n" + " \"definitely_not_an_id\": \"AbcDefGhiJklMnoPqrS_\",\n" + // Make the ID go away " \"_version\": 1,\n" + " \"result\": \"created\",\n" + " \"_shards\": {\n" + " \"total\": 2,\n" + " \"successful\": 1,\n" + " \"failed\": 0\n" + " },\n" + " \"_seq_no\": 0,\n" + " \"_primary_term\": 1\n" + "}"; NetworkClient mock = Mockito.mock(NetworkClient.class); Mockito.when(mock.execute(Mockito.eq(request))).thenReturn(new SimpleResponse(201, new FastByteArrayInputStream(new BytesArray(response)), "localhost:9200")); RestClient client = new RestClient(new TestSettings(), mock); String id = client.postDocument(writeResource, document); assertEquals("AbcDefGhiJklMnoPqrS_", id); }
Mockito.when(mock.execute(Mockito.eq(request))).thenReturn(new SimpleResponse(400, new FastByteArrayInputStream(new BytesArray(response)), "localhost:9200"));