Skip to content

Commit 2b704eb

Browse files
committed
Add UT for factory
Signed-off-by: Liyun Xiu <[email protected]>
1 parent 3a9145c commit 2b704eb

File tree

2 files changed

+44
-6
lines changed

2 files changed

+44
-6
lines changed

server/src/main/java/org/opensearch/ingest/AbstractBatchingProcessor.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -112,12 +112,7 @@ public AbstractBatchingProcessor create(
112112
) throws Exception {
113113
int batchSize = ConfigurationUtils.readIntProperty(this.processorType, tag, config, BATCH_SIZE_FIELD, DEFAULT_BATCH_SIZE);
114114
if (batchSize < 1) {
115-
throw newConfigurationException(
116-
this.processorType,
117-
tag,
118-
BATCH_SIZE_FIELD,
119-
BATCH_SIZE_FIELD + " must be a positive integer"
120-
);
115+
throw newConfigurationException(this.processorType, tag, BATCH_SIZE_FIELD, "batch size must be a positive integer");
121116
}
122117
return newProcessor(tag, description, batchSize, config);
123118
}

server/src/test/java/org/opensearch/ingest/AbstractBatchingProcessorTests.java

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,15 @@
88

99
package org.opensearch.ingest;
1010

11+
import org.opensearch.OpenSearchParseException;
1112
import org.opensearch.test.OpenSearchTestCase;
1213

1314
import java.util.ArrayList;
1415
import java.util.Arrays;
1516
import java.util.Collections;
17+
import java.util.HashMap;
1618
import java.util.List;
19+
import java.util.Map;
1720
import java.util.function.Consumer;
1821

1922
public class AbstractBatchingProcessorTests extends OpenSearchTestCase {
@@ -87,6 +90,29 @@ public void testBatchExecute_defaultBatchSize() {
8790
assertEquals(wrapperList.subList(2, 3), processor.getSubBatches().get(2));
8891
}
8992

93+
public void testFactory_invalidBatchSize() {
94+
Map<String, Object> config = new HashMap<>();
95+
config.put("batch_size", 0);
96+
DummyProcessor.DummyProcessorFactory factory = new DummyProcessor.DummyProcessorFactory("DummyProcessor");
97+
OpenSearchParseException exception = assertThrows(OpenSearchParseException.class, () -> factory.create(config));
98+
assertEquals("[batch_size] batch size must be a positive integer", exception.getMessage());
99+
}
100+
101+
public void testFactory_defaultBatchSize() throws Exception {
102+
Map<String, Object> config = new HashMap<>();
103+
DummyProcessor.DummyProcessorFactory factory = new DummyProcessor.DummyProcessorFactory("DummyProcessor");
104+
DummyProcessor processor = (DummyProcessor) factory.create(config);
105+
assertEquals(1, processor.batchSize);
106+
}
107+
108+
public void testFactory_callNewProcessor() throws Exception {
109+
Map<String, Object> config = new HashMap<>();
110+
config.put("batch_size", 3);
111+
DummyProcessor.DummyProcessorFactory factory = new DummyProcessor.DummyProcessorFactory("DummyProcessor");
112+
DummyProcessor processor = (DummyProcessor) factory.create(config);
113+
assertEquals(3, processor.batchSize);
114+
}
115+
90116
static class DummyProcessor extends AbstractBatchingProcessor {
91117
private List<List<IngestDocumentWrapper>> subBatches = new ArrayList<>();
92118

@@ -113,5 +139,22 @@ public IngestDocument execute(IngestDocument ingestDocument) throws Exception {
113139
public String getType() {
114140
return null;
115141
}
142+
143+
public static class DummyProcessorFactory extends Factory {
144+
145+
protected DummyProcessorFactory(String processorType) {
146+
super(processorType);
147+
}
148+
149+
public AbstractBatchingProcessor create(Map<String, Object> config) throws Exception {
150+
final Map<String, org.opensearch.ingest.Processor.Factory> processorFactories = new HashMap<>();
151+
return super.create(processorFactories, "tag", "description", config);
152+
}
153+
154+
@Override
155+
protected AbstractBatchingProcessor newProcessor(String tag, String description, int batchSize, Map<String, Object> config) {
156+
return new DummyProcessor(batchSize);
157+
}
158+
}
116159
}
117160
}

0 commit comments

Comments
 (0)