forked from opensearch-project/data-prepper
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
GitHub-issue#2822: Define null characters in convert processor (opens…
…earch-project#2844) GitHub-issue#2822: Define null characters in convert processor Signed-off-by: Aidar Shaidullin <[email protected]> Signed-off-by: saydar31 <[email protected]> --------- Signed-off-by: saydar31 <[email protected]> Co-authored-by: saydar31 <[email protected]> Signed-off-by: Marcos_Gonzalez_Mayedo <[email protected]>
- Loading branch information
1 parent
b741aaa
commit 2383e57
Showing
3 changed files
with
137 additions
and
1 deletion.
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
120 changes: 120 additions & 0 deletions
120
...h/dataprepper/plugins/processor/mutateevent/ConvertEntryTypeProcessor_NullValueTests.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,120 @@ | ||
package org.opensearch.dataprepper.plugins.processor.mutateevent; | ||
|
||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.mockito.Mock; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
import org.opensearch.dataprepper.expression.ExpressionEvaluator; | ||
import org.opensearch.dataprepper.metrics.PluginMetrics; | ||
import org.opensearch.dataprepper.model.event.Event; | ||
import org.opensearch.dataprepper.model.event.JacksonEvent; | ||
import org.opensearch.dataprepper.model.record.Record; | ||
|
||
import java.util.Collections; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
import java.util.UUID; | ||
|
||
import static org.hamcrest.CoreMatchers.equalTo; | ||
import static org.hamcrest.CoreMatchers.notNullValue; | ||
import static org.hamcrest.CoreMatchers.nullValue; | ||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.mockito.Mockito.when; | ||
|
||
@ExtendWith(MockitoExtension.class) | ||
public class ConvertEntryTypeProcessor_NullValueTests { | ||
|
||
static final String TEST_KEY = UUID.randomUUID().toString(); | ||
|
||
@Mock | ||
private PluginMetrics pluginMetrics; | ||
|
||
@Mock | ||
private ConvertEntryTypeProcessorConfig mockConfig; | ||
|
||
@Mock | ||
private ExpressionEvaluator expressionEvaluator; | ||
|
||
private ConvertEntryTypeProcessor nullValuesProcessor; | ||
|
||
@BeforeEach | ||
private void setup() { | ||
when(mockConfig.getKey()).thenReturn(TEST_KEY); | ||
when(mockConfig.getType()).thenReturn(TargetType.fromOptionValue("integer")); | ||
when(mockConfig.getConvertWhen()).thenReturn(null); | ||
} | ||
|
||
private Event executeAndGetProcessedEvent(final Object testValue) { | ||
final Record<Event> record = getMessage(UUID.randomUUID().toString(), TEST_KEY, testValue); | ||
final List<Record<Event>> processedRecords = (List<Record<Event>>) nullValuesProcessor.doExecute(Collections.singletonList(record)); | ||
assertThat(processedRecords.size(), equalTo(1)); | ||
assertThat(processedRecords.get(0), notNullValue()); | ||
Event event = processedRecords.get(0).getData(); | ||
assertThat(event, notNullValue()); | ||
return event; | ||
} | ||
|
||
private Record<Event> getMessage(String message, String key, Object value) { | ||
final Map<String, Object> testData = new HashMap(); | ||
testData.put("message", message); | ||
testData.put(key, value); | ||
return buildRecordWithEvent(testData); | ||
} | ||
|
||
static Record<Event> buildRecordWithEvent(final Map<String, Object> data) { | ||
return new Record<>(JacksonEvent.builder() | ||
.withData(data) | ||
.withEventType("event") | ||
.build()); | ||
} | ||
|
||
@Test | ||
void testNoNullValues() { | ||
int testValue = 5432; | ||
when(mockConfig.getNullValues()).thenReturn(Optional.empty()); | ||
nullValuesProcessor = new ConvertEntryTypeProcessor(pluginMetrics, mockConfig, expressionEvaluator); | ||
Event event = executeAndGetProcessedEvent(testValue); | ||
assertThat(event.get(TEST_KEY, Integer.class), equalTo(testValue)); | ||
} | ||
|
||
@Test | ||
void testEmptyListNullValues() { | ||
int testValue = 5432; | ||
when(mockConfig.getNullValues()).thenReturn(Optional.of(List.of())); | ||
nullValuesProcessor = new ConvertEntryTypeProcessor(pluginMetrics, mockConfig, expressionEvaluator); | ||
Event event = executeAndGetProcessedEvent(testValue); | ||
assertThat(event.get(TEST_KEY, Integer.class), equalTo(testValue)); | ||
} | ||
|
||
@Test | ||
void testOneElementNullValues() { | ||
String testValue = "-"; | ||
when(mockConfig.getNullValues()).thenReturn(Optional.of(List.of("-"))); | ||
nullValuesProcessor = new ConvertEntryTypeProcessor(pluginMetrics, mockConfig, expressionEvaluator); | ||
Event event = executeAndGetProcessedEvent(testValue); | ||
Object keyValue = event.get(TEST_KEY, Object.class); | ||
assertThat(keyValue, nullValue()); | ||
} | ||
|
||
@Test | ||
void testMultipleElementNullValues() { | ||
String testValue = "-"; | ||
when(mockConfig.getNullValues()).thenReturn(Optional.of(List.of("-", "null"))); | ||
|
||
nullValuesProcessor = new ConvertEntryTypeProcessor(pluginMetrics, mockConfig, expressionEvaluator); | ||
Event event = executeAndGetProcessedEvent(testValue); | ||
assertThat(event.get(TEST_KEY, Integer.class), nullValue()); | ||
|
||
testValue = "null"; | ||
event = executeAndGetProcessedEvent(testValue); | ||
assertThat(event.get(TEST_KEY, Integer.class), nullValue()); | ||
|
||
int testNumber = 5432; | ||
event = executeAndGetProcessedEvent(testNumber); | ||
assertThat(event.get(TEST_KEY, Integer.class), equalTo(testNumber)); | ||
} | ||
|
||
} |