Search in sources :

Example 31 with IndexRequest

use of org.elasticsearch.action.index.IndexRequest in project elasticsearch by elastic.

the class PipelineExecutionServiceTests method testBulkRequestExecution.

public void testBulkRequestExecution() throws Exception {
    BulkRequest bulkRequest = new BulkRequest();
    String pipelineId = "_id";
    int numRequest = scaledRandomIntBetween(8, 64);
    for (int i = 0; i < numRequest; i++) {
        IndexRequest indexRequest = new IndexRequest("_index", "_type", "_id").setPipeline(pipelineId);
        indexRequest.source(Requests.INDEX_CONTENT_TYPE, "field1", "value1");
        bulkRequest.add(indexRequest);
    }
    when(store.get(pipelineId)).thenReturn(new Pipeline(pipelineId, null, version, new CompoundProcessor()));
    @SuppressWarnings("unchecked") BiConsumer<IndexRequest, Exception> requestItemErrorHandler = mock(BiConsumer.class);
    @SuppressWarnings("unchecked") Consumer<Exception> completionHandler = mock(Consumer.class);
    executionService.executeBulkRequest(bulkRequest.requests(), requestItemErrorHandler, completionHandler);
    verify(requestItemErrorHandler, never()).accept(any(), any());
    verify(completionHandler, times(1)).accept(null);
}
Also used : BulkRequest(org.elasticsearch.action.bulk.BulkRequest) Matchers.anyString(org.mockito.Matchers.anyString) IndexRequest(org.elasticsearch.action.index.IndexRequest) ElasticsearchException(org.elasticsearch.ElasticsearchException)

Example 32 with IndexRequest

use of org.elasticsearch.action.index.IndexRequest in project elasticsearch by elastic.

the class PipelineExecutionServiceTests method testExecuteIndexPipelineDoesNotExist.

public void testExecuteIndexPipelineDoesNotExist() {
    IndexRequest indexRequest = new IndexRequest("_index", "_type", "_id").source(Collections.emptyMap()).setPipeline("_id");
    @SuppressWarnings("unchecked") Consumer<Exception> failureHandler = mock(Consumer.class);
    @SuppressWarnings("unchecked") Consumer<Boolean> completionHandler = mock(Consumer.class);
    try {
        executionService.executeIndexRequest(indexRequest, failureHandler, completionHandler);
        fail("IllegalArgumentException expected");
    } catch (IllegalArgumentException e) {
        assertThat(e.getMessage(), equalTo("pipeline with id [_id] does not exist"));
    }
    verify(failureHandler, never()).accept(any(Exception.class));
    verify(completionHandler, never()).accept(anyBoolean());
}
Also used : IndexRequest(org.elasticsearch.action.index.IndexRequest) Matchers.anyBoolean(org.mockito.Matchers.anyBoolean) ElasticsearchException(org.elasticsearch.ElasticsearchException)

Example 33 with IndexRequest

use of org.elasticsearch.action.index.IndexRequest in project elasticsearch by elastic.

the class PipelineExecutionServiceTests method testExecuteSuccess.

public void testExecuteSuccess() throws Exception {
    CompoundProcessor processor = mock(CompoundProcessor.class);
    when(store.get("_id")).thenReturn(new Pipeline("_id", "_description", version, processor));
    IndexRequest indexRequest = new IndexRequest("_index", "_type", "_id").source(Collections.emptyMap()).setPipeline("_id");
    @SuppressWarnings("unchecked") Consumer<Exception> failureHandler = mock(Consumer.class);
    @SuppressWarnings("unchecked") Consumer<Boolean> completionHandler = mock(Consumer.class);
    executionService.executeIndexRequest(indexRequest, failureHandler, completionHandler);
    verify(failureHandler, never()).accept(any());
    verify(completionHandler, times(1)).accept(true);
}
Also used : IndexRequest(org.elasticsearch.action.index.IndexRequest) Matchers.anyBoolean(org.mockito.Matchers.anyBoolean) ElasticsearchException(org.elasticsearch.ElasticsearchException)

Example 34 with IndexRequest

use of org.elasticsearch.action.index.IndexRequest in project elasticsearch by elastic.

the class PipelineExecutionServiceTests method testExecuteFailureWithOnFailure.

public void testExecuteFailureWithOnFailure() throws Exception {
    Processor processor = mock(Processor.class);
    Processor onFailureProcessor = mock(Processor.class);
    CompoundProcessor compoundProcessor = new CompoundProcessor(false, Collections.singletonList(processor), Collections.singletonList(new CompoundProcessor(onFailureProcessor)));
    when(store.get("_id")).thenReturn(new Pipeline("_id", "_description", version, compoundProcessor));
    IndexRequest indexRequest = new IndexRequest("_index", "_type", "_id").source(Collections.emptyMap()).setPipeline("_id");
    doThrow(new RuntimeException()).when(processor).execute(eqID("_index", "_type", "_id", Collections.emptyMap()));
    doThrow(new RuntimeException()).when(onFailureProcessor).execute(eqID("_index", "_type", "_id", Collections.emptyMap()));
    @SuppressWarnings("unchecked") Consumer<Exception> failureHandler = mock(Consumer.class);
    @SuppressWarnings("unchecked") Consumer<Boolean> completionHandler = mock(Consumer.class);
    executionService.executeIndexRequest(indexRequest, failureHandler, completionHandler);
    verify(processor).execute(eqID("_index", "_type", "_id", Collections.emptyMap()));
    verify(failureHandler, times(1)).accept(any(RuntimeException.class));
    verify(completionHandler, never()).accept(anyBoolean());
}
Also used : IndexRequest(org.elasticsearch.action.index.IndexRequest) Matchers.anyBoolean(org.mockito.Matchers.anyBoolean) ElasticsearchException(org.elasticsearch.ElasticsearchException)

Example 35 with IndexRequest

use of org.elasticsearch.action.index.IndexRequest in project elasticsearch by elastic.

the class PipelineExecutionServiceTests method testBulkRequestExecutionWithFailures.

public void testBulkRequestExecutionWithFailures() throws Exception {
    BulkRequest bulkRequest = new BulkRequest();
    String pipelineId = "_id";
    int numRequest = scaledRandomIntBetween(8, 64);
    int numIndexRequests = 0;
    for (int i = 0; i < numRequest; i++) {
        DocWriteRequest request;
        if (randomBoolean()) {
            if (randomBoolean()) {
                request = new DeleteRequest("_index", "_type", "_id");
            } else {
                request = new UpdateRequest("_index", "_type", "_id");
            }
        } else {
            IndexRequest indexRequest = new IndexRequest("_index", "_type", "_id").setPipeline(pipelineId);
            indexRequest.source(Requests.INDEX_CONTENT_TYPE, "field1", "value1");
            request = indexRequest;
            numIndexRequests++;
        }
        bulkRequest.add(request);
    }
    CompoundProcessor processor = mock(CompoundProcessor.class);
    when(processor.getProcessors()).thenReturn(Collections.singletonList(mock(Processor.class)));
    Exception error = new RuntimeException();
    doThrow(error).when(processor).execute(any());
    when(store.get(pipelineId)).thenReturn(new Pipeline(pipelineId, null, version, processor));
    @SuppressWarnings("unchecked") BiConsumer<IndexRequest, Exception> requestItemErrorHandler = mock(BiConsumer.class);
    @SuppressWarnings("unchecked") Consumer<Exception> completionHandler = mock(Consumer.class);
    executionService.executeBulkRequest(bulkRequest.requests(), requestItemErrorHandler, completionHandler);
    verify(requestItemErrorHandler, times(numIndexRequests)).accept(any(IndexRequest.class), eq(error));
    verify(completionHandler, times(1)).accept(null);
}
Also used : UpdateRequest(org.elasticsearch.action.update.UpdateRequest) Matchers.anyString(org.mockito.Matchers.anyString) IndexRequest(org.elasticsearch.action.index.IndexRequest) ElasticsearchException(org.elasticsearch.ElasticsearchException) BulkRequest(org.elasticsearch.action.bulk.BulkRequest) DocWriteRequest(org.elasticsearch.action.DocWriteRequest) DeleteRequest(org.elasticsearch.action.delete.DeleteRequest)

Aggregations

IndexRequest (org.elasticsearch.action.index.IndexRequest)175 DeleteRequest (org.elasticsearch.action.delete.DeleteRequest)34 BulkRequest (org.elasticsearch.action.bulk.BulkRequest)32 UpdateRequest (org.elasticsearch.action.update.UpdateRequest)32 IOException (java.io.IOException)28 Test (org.junit.Test)27 DocWriteRequest (org.elasticsearch.action.DocWriteRequest)25 ElasticsearchException (org.elasticsearch.ElasticsearchException)20 IndexResponse (org.elasticsearch.action.index.IndexResponse)17 HashMap (java.util.HashMap)16 Map (java.util.Map)14 BulkResponse (org.elasticsearch.action.bulk.BulkResponse)14 XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)14 DeleteIndexRequest (org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest)13 GetRequest (org.elasticsearch.action.get.GetRequest)13 BytesReference (org.elasticsearch.common.bytes.BytesReference)11 ArrayList (java.util.ArrayList)10 Matchers.anyBoolean (org.mockito.Matchers.anyBoolean)9 CreateIndexRequest (org.elasticsearch.client.indices.CreateIndexRequest)8 Settings (org.elasticsearch.common.settings.Settings)8