|
36 | 36 | import static org.hamcrest.Matchers.containsString; |
37 | 37 | import static org.hamcrest.Matchers.equalTo; |
38 | 38 | import static org.hamcrest.Matchers.sameInstance; |
| 39 | +import static org.hamcrest.Matchers.not; |
39 | 40 |
|
40 | 41 | public class ConvertProcessorTests extends ESTestCase { |
41 | 42 |
|
@@ -79,6 +80,92 @@ public void testConvertIntError() throws Exception { |
79 | 80 | } |
80 | 81 | } |
81 | 82 |
|
| 83 | + public void testConvertLong() throws Exception { |
| 84 | + IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random()); |
| 85 | + Map<String, Long> expectedResult = new HashMap<>(); |
| 86 | + long randomLong = randomLong(); |
| 87 | + String fieldName = RandomDocumentPicks.addRandomField(random(), ingestDocument, randomLong); |
| 88 | + expectedResult.put(fieldName, randomLong); |
| 89 | + |
| 90 | + Processor processor = new ConvertProcessor(randomAlphaOfLength(10), fieldName, fieldName, Type.LONG, false); |
| 91 | + processor.execute(ingestDocument); |
| 92 | + assertThat(ingestDocument.getFieldValue(fieldName, Long.class), equalTo(randomLong)); |
| 93 | + } |
| 94 | + |
| 95 | + public void testConvertLongList() throws Exception { |
| 96 | + IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random()); |
| 97 | + int numItems = randomIntBetween(1, 10); |
| 98 | + List<String> fieldValue = new ArrayList<>(); |
| 99 | + List<Long> expectedList = new ArrayList<>(); |
| 100 | + for (int j = 0; j < numItems; j++) { |
| 101 | + long randomLong = randomLong(); |
| 102 | + fieldValue.add(Long.toString(randomLong)); |
| 103 | + expectedList.add(randomLong); |
| 104 | + } |
| 105 | + String fieldName = RandomDocumentPicks.addRandomField(random(), ingestDocument, fieldValue); |
| 106 | + Processor processor = new ConvertProcessor(randomAlphaOfLength(10), fieldName, fieldName, Type.LONG, false); |
| 107 | + processor.execute(ingestDocument); |
| 108 | + assertThat(ingestDocument.getFieldValue(fieldName, List.class), equalTo(expectedList)); |
| 109 | + } |
| 110 | + |
| 111 | + public void testConvertLongError() throws Exception { |
| 112 | + IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random(), new HashMap<>()); |
| 113 | + String fieldName = RandomDocumentPicks.randomFieldName(random()); |
| 114 | + String value = "string-" + randomAlphaOfLengthBetween(1, 10); |
| 115 | + ingestDocument.setFieldValue(fieldName, value); |
| 116 | + |
| 117 | + Processor processor = new ConvertProcessor(randomAlphaOfLength(10), fieldName, fieldName, Type.LONG, false); |
| 118 | + try { |
| 119 | + processor.execute(ingestDocument); |
| 120 | + fail("processor execute should have failed"); |
| 121 | + } catch(IllegalArgumentException e) { |
| 122 | + assertThat(e.getMessage(), equalTo("unable to convert [" + value + "] to long")); |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + public void testConvertDouble() throws Exception { |
| 127 | + IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random()); |
| 128 | + Map<String, Double> expectedResult = new HashMap<>(); |
| 129 | + double randomDouble = randomDouble(); |
| 130 | + String fieldName = RandomDocumentPicks.addRandomField(random(), ingestDocument, randomDouble); |
| 131 | + expectedResult.put(fieldName, randomDouble); |
| 132 | + |
| 133 | + Processor processor = new ConvertProcessor(randomAlphaOfLength(10), fieldName, fieldName, Type.DOUBLE, false); |
| 134 | + processor.execute(ingestDocument); |
| 135 | + assertThat(ingestDocument.getFieldValue(fieldName, Double.class), equalTo(randomDouble)); |
| 136 | + } |
| 137 | + |
| 138 | + public void testConvertDoubleList() throws Exception { |
| 139 | + IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random()); |
| 140 | + int numItems = randomIntBetween(1, 10); |
| 141 | + List<String> fieldValue = new ArrayList<>(); |
| 142 | + List<Double> expectedList = new ArrayList<>(); |
| 143 | + for (int j = 0; j < numItems; j++) { |
| 144 | + double randomDouble = randomDouble(); |
| 145 | + fieldValue.add(Double.toString(randomDouble)); |
| 146 | + expectedList.add(randomDouble); |
| 147 | + } |
| 148 | + String fieldName = RandomDocumentPicks.addRandomField(random(), ingestDocument, fieldValue); |
| 149 | + Processor processor = new ConvertProcessor(randomAlphaOfLength(10), fieldName, fieldName, Type.DOUBLE, false); |
| 150 | + processor.execute(ingestDocument); |
| 151 | + assertThat(ingestDocument.getFieldValue(fieldName, List.class), equalTo(expectedList)); |
| 152 | + } |
| 153 | + |
| 154 | + public void testConvertDoubleError() throws Exception { |
| 155 | + IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random(), new HashMap<>()); |
| 156 | + String fieldName = RandomDocumentPicks.randomFieldName(random()); |
| 157 | + String value = "string-" + randomAlphaOfLengthBetween(1, 10); |
| 158 | + ingestDocument.setFieldValue(fieldName, value); |
| 159 | + |
| 160 | + Processor processor = new ConvertProcessor(randomAlphaOfLength(10), fieldName, fieldName, Type.DOUBLE, false); |
| 161 | + try { |
| 162 | + processor.execute(ingestDocument); |
| 163 | + fail("processor execute should have failed"); |
| 164 | + } catch(IllegalArgumentException e) { |
| 165 | + assertThat(e.getMessage(), equalTo("unable to convert [" + value + "] to double")); |
| 166 | + } |
| 167 | + } |
| 168 | + |
82 | 169 | public void testConvertFloat() throws Exception { |
83 | 170 | IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random()); |
84 | 171 | Map<String, Float> expectedResult = new HashMap<>(); |
@@ -231,6 +318,16 @@ public void testConvertStringList() throws Exception { |
231 | 318 | randomValue = randomBoolean; |
232 | 319 | randomValueString = Boolean.toString(randomBoolean); |
233 | 320 | break; |
| 321 | + case 3: |
| 322 | + long randomLong = randomLong(); |
| 323 | + randomValue = randomLong; |
| 324 | + randomValueString = Long.toString(randomLong); |
| 325 | + break; |
| 326 | + case 4: |
| 327 | + double randomDouble = randomDouble(); |
| 328 | + randomValue = randomDouble; |
| 329 | + randomValueString = Double.toString(randomDouble); |
| 330 | + break; |
234 | 331 | default: |
235 | 332 | throw new UnsupportedOperationException(); |
236 | 333 | } |
@@ -342,6 +439,28 @@ public void testAutoConvertMatchInteger() throws Exception { |
342 | 439 | assertThat(convertedValue, equalTo(randomInt)); |
343 | 440 | } |
344 | 441 |
|
| 442 | + public void testAutoConvertMatchLong() throws Exception { |
| 443 | + long randomLong = randomLong(); |
| 444 | + String randomString = Long.toString(randomLong); |
| 445 | + IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random(), Collections.singletonMap("field", randomString)); |
| 446 | + Processor processor = new ConvertProcessor(randomAlphaOfLength(10), "field", "field", Type.AUTO, false); |
| 447 | + processor.execute(ingestDocument); |
| 448 | + Object convertedValue = ingestDocument.getFieldValue("field", Object.class); |
| 449 | + assertThat(convertedValue, equalTo(randomLong)); |
| 450 | + } |
| 451 | + |
| 452 | + public void testAutoConvertDoubleNotMatched() throws Exception { |
| 453 | + double randomDouble = randomDouble(); |
| 454 | + String randomString = Double.toString(randomDouble); |
| 455 | + float randomFloat = Float.parseFloat(randomString); |
| 456 | + IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random(), Collections.singletonMap("field", randomString)); |
| 457 | + Processor processor = new ConvertProcessor(randomAlphaOfLength(10), "field", "field", Type.AUTO, false); |
| 458 | + processor.execute(ingestDocument); |
| 459 | + Object convertedValue = ingestDocument.getFieldValue("field", Object.class); |
| 460 | + assertThat(convertedValue, not(randomDouble)); |
| 461 | + assertThat(convertedValue, equalTo(randomFloat)); |
| 462 | + } |
| 463 | + |
345 | 464 | public void testAutoConvertMatchFloat() throws Exception { |
346 | 465 | float randomFloat = randomFloat(); |
347 | 466 | String randomString = Float.toString(randomFloat); |
|
0 commit comments