Skip to content

Commit

Permalink
Applying review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
eyalkoren committed Sep 4, 2024
1 parent c0dbe39 commit 1f01aca
Showing 1 changed file with 40 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.network.NetworkAddress;
import org.elasticsearch.common.time.DateFormatter;
import org.elasticsearch.common.time.FormatNames;
import org.elasticsearch.core.Nullable;
import org.elasticsearch.core.SuppressForbidden;
import org.elasticsearch.test.cluster.ElasticsearchCluster;
Expand Down Expand Up @@ -206,6 +207,44 @@ public void testNumericMessage() throws IOException {
verifyEcsMappings(indexName);
}

public void testDateFieldsWithDifferentFormats() throws IOException {
Map<String, Object> dateFieldsMap = ecsFlatFieldDefinitions.entrySet()
.stream()
.filter(entry -> "date".equals(entry.getValue().get("type")))
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));

// test with iso8601 format
String indexName = "test-date-fields-as-is8601";
createTestIndex(indexName);
Map<String, Object> document = new HashMap<>();
DateFormatter formatter = DateFormatter.forPattern(FormatNames.ISO8601.getName());
for (String field : dateFieldsMap.keySet()) {
document.put(field, formatter.formatMillis(System.currentTimeMillis()));
}
verifyAllDateFields(indexName, document, dateFieldsMap);

// test with milliseconds since epoch format
indexName = "test-date-fields-as-millis";
createTestIndex(indexName);
document = new HashMap<>();
for (String field : dateFieldsMap.keySet()) {
document.put(field, System.currentTimeMillis());
}
verifyAllDateFields(indexName, document, dateFieldsMap);
}

private void verifyAllDateFields(String indexName, Map<String, Object> document, Map<String, Object> dateFieldsMap) throws IOException {
indexDocument(indexName, document);
final Map<String, Object> rawMappings = getMappings(indexName);
final Map<String, Map<String, Object>> flatFieldMappings = new HashMap<>();
processRawMappingsSubtree(rawMappings, flatFieldMappings, new HashMap<>(), "");
flatFieldMappings.forEach((fieldName, fieldMappings) -> {
if (dateFieldsMap.containsKey(fieldName)) {
assertType("date", fieldMappings);
}
});
}

private void assertType(String expectedType, Map<String, Object> actualMappings) {
assertNotNull("expected to get non-null mappings for field", actualMappings);
assertEquals(expectedType, actualMappings.get("type"));
Expand Down Expand Up @@ -350,7 +389,7 @@ private Object generateTestValue(String type) {
return "test";
}
case "date" -> {
return DateFormatter.forPattern("strict_date_optional_time").formatMillis(System.currentTimeMillis());
return DateFormatter.forPattern(FormatNames.STRICT_DATE_OPTIONAL_TIME.getName()).formatMillis(System.currentTimeMillis());
}
case "ip" -> {
return NetworkAddress.format(randomIp(true));
Expand Down

0 comments on commit 1f01aca

Please sign in to comment.