Skip to content

Commit

Permalink
Fix ingest pipeline _simulate api with empty docs never returns a res… (
Browse files Browse the repository at this point in the history
  • Loading branch information
gaobinlong authored Mar 11, 2020
1 parent 5494675 commit 7ba94d8
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,13 @@ public void execute(SimulatePipelineRequest.Parsed request, ActionListener<Simul
final AtomicInteger counter = new AtomicInteger();
final List<SimulateDocumentResult> responses =
new CopyOnWriteArrayList<>(new SimulateDocumentBaseResult[request.getDocuments().size()]);

if (request.getDocuments().isEmpty()) {
l.onResponse(new SimulatePipelineResponse(request.getPipeline().getId(),
request.isVerbose(), responses));
return;
}

int iter = 0;
for (IngestDocument ingestDocument : request.getDocuments()) {
final int index = iter;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,8 +173,15 @@ static Parsed parse(Map<String, Object> config, boolean verbose, IngestService i
private static List<IngestDocument> parseDocs(Map<String, Object> config) {
List<Map<String, Object>> docs =
ConfigurationUtils.readList(null, null, config, Fields.DOCS);
if (docs.isEmpty()) {
throw new IllegalArgumentException("must specify at least one document in [docs]");
}
List<IngestDocument> ingestDocumentList = new ArrayList<>();
for (Map<String, Object> dataMap : docs) {
for (Object object : docs) {
if ((object instanceof Map) == false) {
throw new IllegalArgumentException("malformed [docs] section, should include an inner object");
}
Map<String, Object> dataMap = (Map<String, Object>) object;
Map<String, Object> document = ConfigurationUtils.readMap(null, null,
dataMap, Fields.SOURCE);
String index = ConfigurationUtils.readStringOrIntProperty(null, null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

package org.elasticsearch.action.ingest;

import org.elasticsearch.ElasticsearchParseException;
import org.elasticsearch.index.VersionType;
import org.elasticsearch.ingest.CompoundProcessor;
import org.elasticsearch.ingest.IngestDocument;
Expand All @@ -45,6 +46,7 @@
import static org.elasticsearch.ingest.IngestDocument.MetaData.ROUTING;
import static org.elasticsearch.ingest.IngestDocument.MetaData.VERSION;
import static org.elasticsearch.ingest.IngestDocument.MetaData.VERSION_TYPE;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.nullValue;
import static org.mockito.Mockito.mock;
Expand Down Expand Up @@ -216,4 +218,33 @@ public void testNonExistentPipelineId() {
() -> SimulatePipelineRequest.parseWithPipelineId(pipelineId, requestContent, false, ingestService));
assertThat(e.getMessage(), equalTo("pipeline [" + pipelineId + "] does not exist"));
}

public void testNotValidDocs() {
Map<String, Object> requestContent = new HashMap<>();
List<Map<String, Object>> docs = new ArrayList<>();
Map<String, Object> pipelineConfig = new HashMap<>();
List<Map<String, Object>> processors = new ArrayList<>();
pipelineConfig.put("processors", processors);
requestContent.put(Fields.DOCS, docs);
requestContent.put(Fields.PIPELINE, pipelineConfig);
Exception e1 = expectThrows(IllegalArgumentException.class,
() -> SimulatePipelineRequest.parse(requestContent, false, ingestService));
assertThat(e1.getMessage(), equalTo("must specify at least one document in [docs]"));

List<String> stringList = new ArrayList<>();
stringList.add("test");
pipelineConfig.put("processors", processors);
requestContent.put(Fields.DOCS, stringList);
requestContent.put(Fields.PIPELINE, pipelineConfig);
Exception e2 = expectThrows(IllegalArgumentException.class,
() -> SimulatePipelineRequest.parse(requestContent, false, ingestService));
assertThat(e2.getMessage(), equalTo("malformed [docs] section, should include an inner object"));

docs.add(new HashMap<>());
requestContent.put(Fields.DOCS, docs);
requestContent.put(Fields.PIPELINE, pipelineConfig);
Exception e3 = expectThrows(ElasticsearchParseException.class,
() -> SimulatePipelineRequest.parse(requestContent, false, ingestService));
assertThat(e3.getMessage(), containsString("required property is missing"));
}
}

0 comments on commit 7ba94d8

Please sign in to comment.