Skip to content

Commit

Permalink
Add OutputCodecContext for output codecs.
Browse files Browse the repository at this point in the history
Signed-off-by: Aiden Dai <[email protected]>
  • Loading branch information
daixba committed Aug 9, 2023
1 parent 3922e75 commit 4a2fa2a
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

package org.opensearch.dataprepper.model.sink;

import java.util.Collections;
import java.util.List;

/**
* Data Prepper Output Codec Context class.
* The context contains information that are shared and may be used among {@link org.opensearch.dataprepper.model.codec.OutputCodec}
*/
public class OutputCodecContext {

private final String tagsTargetKey;

private final List<String> includeKeys;
private final List<String> excludeKeys;

public OutputCodecContext() {
this(null, Collections.emptyList(), Collections.emptyList());
}


public OutputCodecContext(String tagsTargetKey, List<String> includeKeys, List<String> excludeKeys) {
this.tagsTargetKey = tagsTargetKey;
this.includeKeys = includeKeys;
this.excludeKeys = excludeKeys;
}


public static OutputCodecContext fromSinkContext(SinkContext sinkContext) {
if (sinkContext == null) {
return new OutputCodecContext();
}
return new OutputCodecContext(sinkContext.getTagsTargetKey(), sinkContext.getIncludeKeys(), sinkContext.getExcludeKeys());
}

public String getTagsTargetKey() {
return tagsTargetKey;
}

public List<String> getIncludeKeys() {
return includeKeys;
}

public List<String> getExcludeKeys() {
return excludeKeys;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

package org.opensearch.dataprepper.model.sink;

import org.apache.commons.lang3.RandomStringUtils;
import org.junit.jupiter.api.Test;

import java.util.Collections;
import java.util.List;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.junit.jupiter.api.Assertions.assertNull;

public class OutputCodecContextTest {


@Test
public void testOutputCodecContextBasic() {
final String testTagsTargetKey = RandomStringUtils.randomAlphabetic(6);
final List<String> testIncludeKeys = Collections.emptyList();
final List<String> testExcludeKeys = Collections.emptyList();
OutputCodecContext codecContext = new OutputCodecContext(testTagsTargetKey, testIncludeKeys, testExcludeKeys);
assertThat(codecContext.getTagsTargetKey(), equalTo(testTagsTargetKey));
assertThat(codecContext.getIncludeKeys(), equalTo(testIncludeKeys));
assertThat(codecContext.getExcludeKeys(), equalTo(testExcludeKeys));

OutputCodecContext emptyContext = new OutputCodecContext();
assertNull(emptyContext.getTagsTargetKey());
assertThat(emptyContext.getIncludeKeys(), equalTo(testIncludeKeys));
assertThat(emptyContext.getExcludeKeys(), equalTo(testExcludeKeys));


}

@Test
public void testOutputCodecContextAdapter() {
final String testTagsTargetKey = RandomStringUtils.randomAlphabetic(6);
final List<String> testIncludeKeys = Collections.emptyList();
final List<String> testExcludeKeys = Collections.emptyList();

SinkContext sinkContext = new SinkContext(testTagsTargetKey, null, testIncludeKeys, testExcludeKeys);

OutputCodecContext codecContext = OutputCodecContext.fromSinkContext(sinkContext);
assertThat(codecContext.getTagsTargetKey(), equalTo(testTagsTargetKey));
assertThat(codecContext.getIncludeKeys(), equalTo(testIncludeKeys));
assertThat(codecContext.getExcludeKeys(), equalTo(testExcludeKeys));

OutputCodecContext emptyContext = OutputCodecContext.fromSinkContext(null);
assertNull(emptyContext.getTagsTargetKey());
assertThat(emptyContext.getIncludeKeys(), equalTo(testIncludeKeys));
assertThat(emptyContext.getExcludeKeys(), equalTo(testExcludeKeys));


}
}

0 comments on commit 4a2fa2a

Please sign in to comment.