|
17 | 17 |
|
18 | 18 | package de.spinscale.elasticsearch.ingest.opennlp;
|
19 | 19 |
|
| 20 | +import org.elasticsearch.action.ingest.SimulateProcessorResult; |
| 21 | +import org.elasticsearch.common.io.PathUtils; |
20 | 22 | import org.elasticsearch.common.settings.Settings;
|
| 23 | +import org.elasticsearch.common.xcontent.ToXContent; |
| 24 | +import org.elasticsearch.common.xcontent.XContentBuilder; |
| 25 | +import org.elasticsearch.common.xcontent.XContentFactory; |
21 | 26 | import org.elasticsearch.ingest.IngestDocument;
|
22 | 27 | import org.elasticsearch.ingest.Processor;
|
23 | 28 | import org.elasticsearch.ingest.RandomDocumentPicks;
|
24 | 29 | import org.elasticsearch.test.ESTestCase;
|
25 |
| -import org.junit.Before; |
| 30 | +import org.junit.BeforeClass; |
26 | 31 |
|
27 |
| -import java.io.IOException; |
| 32 | +import java.nio.file.Path; |
28 | 33 | import java.util.Arrays;
|
29 | 34 | import java.util.Collections;
|
30 | 35 | import java.util.HashMap;
|
31 | 36 | import java.util.HashSet;
|
| 37 | +import java.util.List; |
32 | 38 | import java.util.Map;
|
33 |
| -import java.util.Set; |
34 | 39 |
|
35 | 40 | import static org.hamcrest.Matchers.containsInAnyOrder;
|
36 | 41 | import static org.hamcrest.Matchers.hasKey;
|
| 42 | +import static org.hamcrest.Matchers.hasSize; |
37 | 43 | import static org.hamcrest.Matchers.instanceOf;
|
38 | 44 | import static org.hamcrest.Matchers.not;
|
39 | 45 |
|
40 | 46 | public class OpenNlpProcessorTests extends ESTestCase {
|
41 | 47 |
|
42 |
| - private OpenNlpService service; |
| 48 | + private static OpenNlpService service; |
43 | 49 |
|
44 |
| - @Before |
45 |
| - public void createOpenNlpService() throws IOException { |
| 50 | + @BeforeClass |
| 51 | + public static void createOpenNlpService() throws Exception { |
46 | 52 | Settings settings = Settings.builder()
|
47 | 53 | .put("ingest.opennlp.model.file.names", "en-ner-persons.bin")
|
48 | 54 | .put("ingest.opennlp.model.file.locations", "en-ner-locations.bin")
|
49 | 55 | .put("ingest.opennlp.model.file.dates", "en-ner-dates.bin")
|
50 | 56 | .build();
|
51 | 57 |
|
52 |
| - service = new OpenNlpService(getDataPath("/models/en-ner-persons.bin").getParent(), settings).start(); |
| 58 | + Path path = PathUtils.get(OpenNlpProcessorTests.class.getResource("/models/en-ner-persons.bin").toURI()); |
| 59 | + service = new OpenNlpService(path.getParent(), settings).start(); |
53 | 60 | }
|
54 | 61 |
|
55 | 62 | public void testThatExtractionsWork() throws Exception {
|
@@ -110,7 +117,20 @@ public void testConstructorNoFieldsSpecified() throws Exception {
|
110 | 117 | assertThatHasElements(entityData, "names", "Kobe Bryant", "Michael Jordan");
|
111 | 118 | assertThatHasElements(entityData, "dates", "Yesterday");
|
112 | 119 | assertThatHasElements(entityData, "locations", "Munich", "New York");
|
| 120 | + } |
| 121 | + |
| 122 | + public void testToXContent() throws Exception { |
| 123 | + OpenNlpProcessor processor = new OpenNlpProcessor(service, randomAlphaOfLength(10), "source_field", "target_field", |
| 124 | + new HashSet<>(Arrays.asList("names", "dates", "locations"))); |
| 125 | + |
| 126 | + IngestDocument ingestDocument = getIngestDocument(); |
| 127 | + processor.execute(ingestDocument); |
| 128 | + |
| 129 | + SimulateProcessorResult result = new SimulateProcessorResult("tag", ingestDocument); |
113 | 130 |
|
| 131 | + try (XContentBuilder builder = XContentFactory.jsonBuilder()) { |
| 132 | + result.toXContent(builder, ToXContent.EMPTY_PARAMS); |
| 133 | + } |
114 | 134 | }
|
115 | 135 |
|
116 | 136 | private Map<String, Object> getIngestDocumentData(OpenNlpProcessor processor) throws Exception {
|
@@ -138,15 +158,16 @@ private Map<String, Object> getIngestDocumentData(IngestDocument ingestDocument)
|
138 | 158 | }
|
139 | 159 |
|
140 | 160 | private void assertThatHasElements(Map<String, Object> entityData, String field, String ... items) {
|
141 |
| - Set<String> values = getValues(entityData, field); |
| 161 | + List<String> values = getValues(entityData, field); |
| 162 | + assertThat(values, hasSize(items.length)); |
142 | 163 | assertThat(values, containsInAnyOrder(items));
|
143 | 164 | }
|
144 | 165 |
|
145 |
| - private Set<String> getValues(Map<String, Object> entityData, String field) { |
| 166 | + private List<String> getValues(Map<String, Object> entityData, String field) { |
146 | 167 | assertThat(entityData, hasKey(field));
|
147 |
| - assertThat(entityData.get(field), instanceOf(Set.class)); |
| 168 | + assertThat(entityData.get(field), instanceOf(List.class)); |
148 | 169 | @SuppressWarnings("unchecked")
|
149 |
| - Set<String> values = (Set<String>) entityData.get(field); |
| 170 | + List<String> values = (List<String>) entityData.get(field); |
150 | 171 | return values;
|
151 | 172 | }
|
152 | 173 | }
|
0 commit comments