|
@@ -36,6 +36,7 @@ import static org.elasticsearch.ingest.common.ConvertProcessor.Type;
|
|
|
import static org.hamcrest.Matchers.containsString;
|
|
|
import static org.hamcrest.Matchers.equalTo;
|
|
|
import static org.hamcrest.Matchers.sameInstance;
|
|
|
+import static org.hamcrest.Matchers.not;
|
|
|
|
|
|
public class ConvertProcessorTests extends ESTestCase {
|
|
|
|
|
@@ -79,6 +80,92 @@ public class ConvertProcessorTests extends ESTestCase {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ public void testConvertLong() throws Exception {
|
|
|
+ IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random());
|
|
|
+ Map<String, Long> expectedResult = new HashMap<>();
|
|
|
+ long randomLong = randomLong();
|
|
|
+ String fieldName = RandomDocumentPicks.addRandomField(random(), ingestDocument, randomLong);
|
|
|
+ expectedResult.put(fieldName, randomLong);
|
|
|
+
|
|
|
+ Processor processor = new ConvertProcessor(randomAlphaOfLength(10), fieldName, fieldName, Type.LONG, false);
|
|
|
+ processor.execute(ingestDocument);
|
|
|
+ assertThat(ingestDocument.getFieldValue(fieldName, Long.class), equalTo(randomLong));
|
|
|
+ }
|
|
|
+
|
|
|
+ public void testConvertLongList() throws Exception {
|
|
|
+ IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random());
|
|
|
+ int numItems = randomIntBetween(1, 10);
|
|
|
+ List<String> fieldValue = new ArrayList<>();
|
|
|
+ List<Long> expectedList = new ArrayList<>();
|
|
|
+ for (int j = 0; j < numItems; j++) {
|
|
|
+ long randomLong = randomLong();
|
|
|
+ fieldValue.add(Long.toString(randomLong));
|
|
|
+ expectedList.add(randomLong);
|
|
|
+ }
|
|
|
+ String fieldName = RandomDocumentPicks.addRandomField(random(), ingestDocument, fieldValue);
|
|
|
+ Processor processor = new ConvertProcessor(randomAlphaOfLength(10), fieldName, fieldName, Type.LONG, false);
|
|
|
+ processor.execute(ingestDocument);
|
|
|
+ assertThat(ingestDocument.getFieldValue(fieldName, List.class), equalTo(expectedList));
|
|
|
+ }
|
|
|
+
|
|
|
+ public void testConvertLongError() throws Exception {
|
|
|
+ IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random(), new HashMap<>());
|
|
|
+ String fieldName = RandomDocumentPicks.randomFieldName(random());
|
|
|
+ String value = "string-" + randomAlphaOfLengthBetween(1, 10);
|
|
|
+ ingestDocument.setFieldValue(fieldName, value);
|
|
|
+
|
|
|
+ Processor processor = new ConvertProcessor(randomAlphaOfLength(10), fieldName, fieldName, Type.LONG, false);
|
|
|
+ try {
|
|
|
+ processor.execute(ingestDocument);
|
|
|
+ fail("processor execute should have failed");
|
|
|
+ } catch(IllegalArgumentException e) {
|
|
|
+ assertThat(e.getMessage(), equalTo("unable to convert [" + value + "] to long"));
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public void testConvertDouble() throws Exception {
|
|
|
+ IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random());
|
|
|
+ Map<String, Double> expectedResult = new HashMap<>();
|
|
|
+ double randomDouble = randomDouble();
|
|
|
+ String fieldName = RandomDocumentPicks.addRandomField(random(), ingestDocument, randomDouble);
|
|
|
+ expectedResult.put(fieldName, randomDouble);
|
|
|
+
|
|
|
+ Processor processor = new ConvertProcessor(randomAlphaOfLength(10), fieldName, fieldName, Type.DOUBLE, false);
|
|
|
+ processor.execute(ingestDocument);
|
|
|
+ assertThat(ingestDocument.getFieldValue(fieldName, Double.class), equalTo(randomDouble));
|
|
|
+ }
|
|
|
+
|
|
|
+ public void testConvertDoubleList() throws Exception {
|
|
|
+ IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random());
|
|
|
+ int numItems = randomIntBetween(1, 10);
|
|
|
+ List<String> fieldValue = new ArrayList<>();
|
|
|
+ List<Double> expectedList = new ArrayList<>();
|
|
|
+ for (int j = 0; j < numItems; j++) {
|
|
|
+ double randomDouble = randomDouble();
|
|
|
+ fieldValue.add(Double.toString(randomDouble));
|
|
|
+ expectedList.add(randomDouble);
|
|
|
+ }
|
|
|
+ String fieldName = RandomDocumentPicks.addRandomField(random(), ingestDocument, fieldValue);
|
|
|
+ Processor processor = new ConvertProcessor(randomAlphaOfLength(10), fieldName, fieldName, Type.DOUBLE, false);
|
|
|
+ processor.execute(ingestDocument);
|
|
|
+ assertThat(ingestDocument.getFieldValue(fieldName, List.class), equalTo(expectedList));
|
|
|
+ }
|
|
|
+
|
|
|
+ public void testConvertDoubleError() throws Exception {
|
|
|
+ IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random(), new HashMap<>());
|
|
|
+ String fieldName = RandomDocumentPicks.randomFieldName(random());
|
|
|
+ String value = "string-" + randomAlphaOfLengthBetween(1, 10);
|
|
|
+ ingestDocument.setFieldValue(fieldName, value);
|
|
|
+
|
|
|
+ Processor processor = new ConvertProcessor(randomAlphaOfLength(10), fieldName, fieldName, Type.DOUBLE, false);
|
|
|
+ try {
|
|
|
+ processor.execute(ingestDocument);
|
|
|
+ fail("processor execute should have failed");
|
|
|
+ } catch(IllegalArgumentException e) {
|
|
|
+ assertThat(e.getMessage(), equalTo("unable to convert [" + value + "] to double"));
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
public void testConvertFloat() throws Exception {
|
|
|
IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random());
|
|
|
Map<String, Float> expectedResult = new HashMap<>();
|
|
@@ -231,6 +318,16 @@ public class ConvertProcessorTests extends ESTestCase {
|
|
|
randomValue = randomBoolean;
|
|
|
randomValueString = Boolean.toString(randomBoolean);
|
|
|
break;
|
|
|
+ case 3:
|
|
|
+ long randomLong = randomLong();
|
|
|
+ randomValue = randomLong;
|
|
|
+ randomValueString = Long.toString(randomLong);
|
|
|
+ break;
|
|
|
+ case 4:
|
|
|
+ double randomDouble = randomDouble();
|
|
|
+ randomValue = randomDouble;
|
|
|
+ randomValueString = Double.toString(randomDouble);
|
|
|
+ break;
|
|
|
default:
|
|
|
throw new UnsupportedOperationException();
|
|
|
}
|
|
@@ -342,6 +439,28 @@ public class ConvertProcessorTests extends ESTestCase {
|
|
|
assertThat(convertedValue, equalTo(randomInt));
|
|
|
}
|
|
|
|
|
|
+ public void testAutoConvertMatchLong() throws Exception {
|
|
|
+ long randomLong = randomLong();
|
|
|
+ String randomString = Long.toString(randomLong);
|
|
|
+ IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random(), Collections.singletonMap("field", randomString));
|
|
|
+ Processor processor = new ConvertProcessor(randomAlphaOfLength(10), "field", "field", Type.AUTO, false);
|
|
|
+ processor.execute(ingestDocument);
|
|
|
+ Object convertedValue = ingestDocument.getFieldValue("field", Object.class);
|
|
|
+ assertThat(convertedValue, equalTo(randomLong));
|
|
|
+ }
|
|
|
+
|
|
|
+ public void testAutoConvertDoubleNotMatched() throws Exception {
|
|
|
+ double randomDouble = randomDouble();
|
|
|
+ String randomString = Double.toString(randomDouble);
|
|
|
+ float randomFloat = Float.parseFloat(randomString);
|
|
|
+ IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random(), Collections.singletonMap("field", randomString));
|
|
|
+ Processor processor = new ConvertProcessor(randomAlphaOfLength(10), "field", "field", Type.AUTO, false);
|
|
|
+ processor.execute(ingestDocument);
|
|
|
+ Object convertedValue = ingestDocument.getFieldValue("field", Object.class);
|
|
|
+ assertThat(convertedValue, not(randomDouble));
|
|
|
+ assertThat(convertedValue, equalTo(randomFloat));
|
|
|
+ }
|
|
|
+
|
|
|
public void testAutoConvertMatchFloat() throws Exception {
|
|
|
float randomFloat = randomFloat();
|
|
|
String randomString = Float.toString(randomFloat);
|