-
Notifications
You must be signed in to change notification settings - Fork 210
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Json codec changes with specific json input codec config #5054
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,30 @@ | ||
package org.opensearch.dataprepper.model.codec; | ||
|
||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import org.junit.jupiter.api.Nested; | ||
import org.junit.jupiter.api.Test; | ||
import org.opensearch.dataprepper.model.event.Event; | ||
import org.opensearch.dataprepper.model.event.DefaultEventHandle; | ||
import org.opensearch.dataprepper.model.record.Record; | ||
|
||
import java.io.ByteArrayInputStream; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.time.Instant; | ||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.LinkedHashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Random; | ||
import java.util.UUID; | ||
|
||
import static org.hamcrest.CoreMatchers.equalTo; | ||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertFalse; | ||
import static org.junit.jupiter.api.Assertions.assertNotEquals; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
import org.junit.jupiter.api.BeforeEach; | ||
|
||
|
@@ -26,7 +37,7 @@ private JsonDecoder createObjectUnderTest() { | |
return new JsonDecoder(); | ||
} | ||
|
||
@BeforeEach | ||
@BeforeEach | ||
void setup() { | ||
jsonDecoder = createObjectUnderTest(); | ||
receivedRecord = null; | ||
|
@@ -60,15 +71,146 @@ void test_basicJsonDecoder_withTimeReceived() { | |
try { | ||
jsonDecoder.parse(new ByteArrayInputStream(inputString.getBytes()), now, (record) -> { | ||
receivedRecord = record; | ||
receivedTime = ((DefaultEventHandle)(((Event)record.getData()).getEventHandle())).getInternalOriginationTime(); | ||
receivedTime = record.getData().getEventHandle().getInternalOriginationTime(); | ||
}); | ||
} catch (Exception e){} | ||
|
||
assertNotEquals(receivedRecord, null); | ||
Map<String, Object> map = receivedRecord.getData().toMap(); | ||
assertThat(map.get("key1"), equalTo(stringValue)); | ||
assertThat(map.get("key2"), equalTo(intValue)); | ||
assertThat(receivedTime, equalTo(now)); | ||
} | ||
|
||
@Nested | ||
class JsonDecoderWithInputConfig { | ||
private ObjectMapper objectMapper; | ||
private final List<String> includeKeys = new ArrayList<>(); | ||
private final List<String> includeMetadataKeys = new ArrayList<>(); | ||
private static final int numKeyRecords = 10; | ||
private static final int numKeyPerRecord = 3; | ||
private Map<String, Object> jsonObject; | ||
private final String key_name = "logEvents"; | ||
|
||
@BeforeEach | ||
void setup() { | ||
objectMapper = new ObjectMapper(); | ||
for (int i=0; i<10; i++) { | ||
includeKeys.add(UUID.randomUUID().toString()); | ||
includeMetadataKeys.add(UUID.randomUUID().toString()); | ||
} | ||
jsonObject = generateJsonWithSpecificKeys(includeKeys, includeMetadataKeys, key_name, numKeyRecords, numKeyPerRecord); | ||
} | ||
@Test | ||
void test_basicJsonDecoder_withInputConfig() throws IOException { | ||
final Instant now = Instant.now(); | ||
List<Record<Event>> records = new ArrayList<>(); | ||
jsonDecoder = new JsonDecoder(key_name, includeKeys, includeMetadataKeys); | ||
jsonDecoder.parse(createInputStream(jsonObject), now, (record) -> { | ||
records.add(record); | ||
receivedTime = record.getData().getEventHandle().getInternalOriginationTime(); | ||
}); | ||
|
||
assertFalse(records.isEmpty()); | ||
assertEquals(numKeyRecords, records.size()); | ||
|
||
records.forEach(record -> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Assert the size of |
||
Map<String, Object> dataMap = record.getData().toMap(); | ||
Map<String, Object> metadataMap = record.getData().getMetadata().getAttributes(); | ||
|
||
for (String includeKey: includeKeys) { | ||
assertThat(dataMap.get(includeKey), equalTo(jsonObject.get(includeKey))); | ||
} | ||
for (String includeMetadataKey: includeMetadataKeys) { | ||
assertThat(metadataMap.get(includeMetadataKey), equalTo(jsonObject.get(includeMetadataKey))); | ||
} | ||
}); | ||
|
||
assertThat(receivedTime, equalTo(now)); | ||
} | ||
|
||
@Test | ||
void test_basicJsonDecoder_withInputConfig_withoutEvents_empty_metadata_keys() throws IOException { | ||
final Instant now = Instant.now(); | ||
List<Record<Event>> records = new ArrayList<>(); | ||
jsonDecoder = new JsonDecoder("", includeKeys, Collections.emptyList()); | ||
jsonDecoder.parse(createInputStream(jsonObject), now, (record) -> { | ||
records.add(record); | ||
receivedTime = record.getData().getEventHandle().getInternalOriginationTime(); | ||
}); | ||
assertTrue(records.isEmpty()); | ||
} | ||
|
||
@Test | ||
void test_basicJsonDecoder_withInputConfig_withoutEvents_null_include_metadata_keys() throws IOException { | ||
final Instant now = Instant.now(); | ||
List<Record<Event>> records = new ArrayList<>(); | ||
jsonDecoder = new JsonDecoder("", includeKeys, null); | ||
jsonDecoder.parse(createInputStream(jsonObject), now, (record) -> { | ||
records.add(record); | ||
receivedTime = record.getData().getEventHandle().getInternalOriginationTime(); | ||
}); | ||
|
||
assertTrue(records.isEmpty()); | ||
} | ||
|
||
@Test | ||
void test_basicJsonDecoder_withInputConfig_withoutEvents_empty_include_keys() throws IOException { | ||
final Instant now = Instant.now(); | ||
List<Record<Event>> records = new ArrayList<>(); | ||
jsonDecoder = new JsonDecoder("", Collections.emptyList(), includeMetadataKeys); | ||
jsonDecoder.parse(createInputStream(jsonObject), now, (record) -> { | ||
records.add(record); | ||
receivedTime = record.getData().getEventHandle().getInternalOriginationTime(); | ||
}); | ||
assertTrue(records.isEmpty()); | ||
} | ||
|
||
@Test | ||
void test_basicJsonDecoder_withInputConfig_withoutEvents_null_include_keys() throws IOException { | ||
final Instant now = Instant.now(); | ||
List<Record<Event>> records = new ArrayList<>(); | ||
jsonDecoder = new JsonDecoder("", null, includeMetadataKeys); | ||
jsonDecoder.parse(createInputStream(jsonObject), now, (record) -> { | ||
records.add(record); | ||
receivedTime = record.getData().getEventHandle().getInternalOriginationTime(); | ||
}); | ||
|
||
assertTrue(records.isEmpty()); | ||
} | ||
|
||
private Map<String, Object> generateJsonWithSpecificKeys(final List<String> includeKeys, | ||
final List<String> includeMetadataKeys, | ||
final String key, | ||
final int numKeyRecords, | ||
final int numKeyPerRecord) { | ||
final Map<String, Object> jsonObject = new LinkedHashMap<>(); | ||
final List<Map<String, Object>> innerObjects = new ArrayList<>(); | ||
|
||
for (String includeKey: includeKeys) { | ||
jsonObject.put(includeKey, UUID.randomUUID().toString()); | ||
} | ||
|
||
for (String includeMetadataKey: includeMetadataKeys) { | ||
jsonObject.put(includeMetadataKey, UUID.randomUUID().toString()); | ||
} | ||
|
||
for (int i=0; i<numKeyRecords; i++) { | ||
final Map<String, Object> innerJsonMap = new LinkedHashMap<>(); | ||
for (int j=0; j<numKeyPerRecord; j++) { | ||
innerJsonMap.put(UUID.randomUUID().toString(), UUID.randomUUID().toString()); | ||
} | ||
innerObjects.add(innerJsonMap); | ||
} | ||
jsonObject.put(key, innerObjects); | ||
return jsonObject; | ||
} | ||
|
||
private InputStream createInputStream(final Map<String, ?> jsonRoot) throws JsonProcessingException { | ||
final byte[] jsonBytes = objectMapper.writeValueAsBytes(jsonRoot); | ||
|
||
return new ByteArrayInputStream(jsonBytes); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
...sor/src/main/java/org/opensearch/dataprepper/plugins/codec/json/JsonInputCodecConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
* | ||
*/ | ||
|
||
package org.opensearch.dataprepper.plugins.codec.json; | ||
|
||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import jakarta.validation.constraints.Size; | ||
|
||
import java.util.List; | ||
|
||
public class JsonInputCodecConfig { | ||
|
||
@JsonProperty("key_name") | ||
@Size(min = 1, max = 2048) | ||
private String keyName; | ||
|
||
public String getKeyName() { | ||
return keyName; | ||
} | ||
|
||
@JsonProperty("include_keys") | ||
private List<String> includeKeys; | ||
|
||
public List<String> getIncludeKeys() { | ||
return includeKeys; | ||
} | ||
|
||
@JsonProperty("include_keys_metadata") | ||
private List<String> includeKeysMetadata; | ||
|
||
public List<String> getIncludeKeysMetadata() { | ||
return includeKeysMetadata; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This only allows for first level of fields? Should we make it generic to allow nested fields, like