[TEST] rewrite testEnglishTextDocumentWithRandomFields

This commit is contained in:
javanna 2016-02-10 12:34:51 +01:00 committed by Luca Cavanna
parent fe7469dffb
commit 4e3fb69861
1 changed files with 22 additions and 16 deletions

View File

@ -27,18 +27,15 @@ import org.elasticsearch.ingest.core.IngestDocument;
import org.elasticsearch.test.ESTestCase; import org.elasticsearch.test.ESTestCase;
import org.junit.Before; import org.junit.Before;
import java.io.FileNotFoundException;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.EnumSet; import java.util.EnumSet;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.HashSet;
import java.util.Locale;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.containsInAnyOrder; import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.hasSize; import static org.hamcrest.Matchers.hasSize;
@ -68,22 +65,31 @@ public class AttachmentProcessorTests extends ESTestCase {
assertThat(attachmentData.get("content_length"), is(notNullValue())); assertThat(attachmentData.get("content_length"), is(notNullValue()));
} }
public void testEnglishTextDocumentWithRandomFields() throws Exception { public void testHtmlDocumentWithRandomFields() throws Exception {
Set<AttachmentProcessor.Field> fields = EnumSet.noneOf(AttachmentProcessor.Field.class); //date is not present in the html doc
List<String> fieldNames = new ArrayList<>(); ArrayList<AttachmentProcessor.Field> fieldsList = new ArrayList<>(EnumSet.complementOf(EnumSet.of
int numFields = scaledRandomIntBetween(1, AttachmentProcessor.Field.values().length); (AttachmentProcessor.Field.DATE)));
Set<AttachmentProcessor.Field> selectedFields = new HashSet<>();
int numFields = randomIntBetween(1, fieldsList.size());
String[] selectedFieldNames = new String[numFields];
for (int i = 0; i < numFields; i++) { for (int i = 0; i < numFields; i++) {
AttachmentProcessor.Field field = AttachmentProcessor.Field.values()[i]; AttachmentProcessor.Field field;
fields.add(field); do {
fieldNames.add(field.name().toLowerCase(Locale.ROOT)); field = randomFrom(fieldsList);
} while (selectedFields.add(field) == false);
selectedFieldNames[i] = field.toLowerCase();
}
if (randomBoolean()) {
selectedFields.add(AttachmentProcessor.Field.DATE);
} }
processor = new AttachmentProcessor(randomAsciiOfLength(10), "source_field", processor = new AttachmentProcessor(randomAsciiOfLength(10), "source_field",
"target_field", EnumSet.copyOf(fields), 10000); "target_field", selectedFields, 10000);
Map<String, Object> attachmentData = parseDocument("text-in-english.txt", processor); Map<String, Object> attachmentData = parseDocument("htmlWithEmptyDateMeta.html", processor);
assertThat(attachmentData.keySet(), hasSize(1)); assertThat(attachmentData.keySet(), hasSize(selectedFieldNames.length));
assertThat(attachmentData.keySet(), contains("content")); assertThat(attachmentData.keySet(), containsInAnyOrder(selectedFieldNames));
} }
public void testFrenchTextDocument() throws Exception { public void testFrenchTextDocument() throws Exception {