-
Notifications
You must be signed in to change notification settings - Fork 206
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Krishna Kondaka <[email protected]>
- Loading branch information
Krishna Kondaka
committed
Oct 4, 2023
1 parent
7260d58
commit 71fc541
Showing
7 changed files
with
206 additions
and
61 deletions.
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
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
49 changes: 49 additions & 0 deletions
49
...main/java/org/opensearch/dataprepper/plugins/sink/opensearch/bulk/SerializedJsonNode.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,49 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.opensearch.dataprepper.plugins.sink.opensearch.bulk; | ||
|
||
import com.fasterxml.jackson.databind.JsonNode; | ||
import java.io.Serializable; | ||
import java.util.Optional; | ||
|
||
class SerializedJsonNode implements SerializedJson, Serializable { | ||
private byte[] document; | ||
private JsonNode jsonNode; | ||
private String documentId = null; | ||
private String routingField = null; | ||
|
||
public SerializedJsonNode(final JsonNode jsonNode, SerializedJson doc) { | ||
this.jsonNode = jsonNode; | ||
this.documentId = doc.getDocumentId().get(); | ||
this.routingField = doc.getRoutingField().get(); | ||
this.document = jsonNode.toString().getBytes(); | ||
} | ||
|
||
public SerializedJsonNode(final JsonNode jsonNode) { | ||
this.jsonNode = jsonNode; | ||
this.document = jsonNode.toString().getBytes(); | ||
} | ||
|
||
@Override | ||
public long getDocumentSize() { | ||
return document.length; | ||
} | ||
|
||
@Override | ||
public byte[] getSerializedJson() { | ||
return document; | ||
} | ||
|
||
@Override | ||
public Optional<String> getDocumentId() { | ||
return Optional.ofNullable(documentId); | ||
} | ||
|
||
@Override | ||
public Optional<String> getRoutingField() { | ||
return Optional.ofNullable(routingField); | ||
} | ||
} |
62 changes: 62 additions & 0 deletions
62
.../java/org/opensearch/dataprepper/plugins/sink/opensearch/bulk/SerializedJsonNodeTest.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,62 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.opensearch.dataprepper.plugins.sink.opensearch.bulk; | ||
|
||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.util.Random; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.fasterxml.jackson.databind.JsonNode; | ||
import org.apache.commons.lang3.RandomStringUtils; | ||
import static org.hamcrest.CoreMatchers.equalTo; | ||
import static org.hamcrest.CoreMatchers.sameInstance; | ||
import static org.hamcrest.MatcherAssert.assertThat; | ||
|
||
class SerializedJsonNodeTest { | ||
private int documentSize; | ||
private byte[] documentBytes; | ||
private String documentId; | ||
private String routingField; | ||
private JsonNode jsonNode; | ||
private SerializedJson document; | ||
private String jsonString; | ||
|
||
@BeforeEach | ||
void setUp() { | ||
Random random = new Random(); | ||
jsonString = "{\"key\":\"value\"}"; | ||
documentSize = jsonString.length(); | ||
|
||
ObjectMapper objectMapper = new ObjectMapper(); | ||
try { | ||
jsonNode = objectMapper.readTree(jsonString); | ||
} catch (Exception e) { | ||
jsonNode = null; | ||
} | ||
documentId = RandomStringUtils.randomAlphabetic(10); | ||
routingField = RandomStringUtils.randomAlphabetic(10); | ||
document = SerializedJson.fromStringAndOptionals(jsonString, documentId, routingField); | ||
} | ||
|
||
private SerializedJsonNode createObjectUnderTest() { | ||
return new SerializedJsonNode(jsonNode, document); | ||
} | ||
|
||
@Test | ||
void getDocumentSize_returns_size_of_the_document_byte_array() { | ||
assertThat(createObjectUnderTest().getDocumentSize(), equalTo((long) documentSize)); | ||
} | ||
|
||
@Test | ||
void getSerializedJson_returns_the_document_byte_array_and_fields() { | ||
assertThat(createObjectUnderTest().getSerializedJson(), equalTo(jsonString.getBytes())); | ||
assertThat(createObjectUnderTest().getDocumentId().get(), equalTo(documentId)); | ||
assertThat(createObjectUnderTest().getRoutingField().get(), equalTo(routingField)); | ||
} | ||
} | ||
|
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