Skip to content

Commit

Permalink
add parsing tests
Browse files Browse the repository at this point in the history
Signed-off-by: Joanne Wang <[email protected]>
  • Loading branch information
jowg-amazon committed Oct 29, 2024
1 parent ca9fa6b commit 0bb0651
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,15 @@
import org.opensearch.core.xcontent.XContentBuilder;
import org.opensearch.core.xcontent.XContentParser;
import org.opensearch.jobscheduler.spi.schedule.IntervalSchedule;
import org.opensearch.securityanalytics.threatIntel.common.SourceConfigType;
import org.opensearch.securityanalytics.threatIntel.model.S3Source;
import org.opensearch.securityanalytics.threatIntel.model.SATIFSourceConfigDto;
import org.opensearch.test.OpenSearchTestCase;

import java.io.IOException;
import java.time.Instant;
import java.time.temporal.ChronoUnit;
import java.util.List;

import static org.opensearch.securityanalytics.TestHelpers.randomSATIFSourceConfigDto;

Expand All @@ -36,6 +40,34 @@ public void testParseFunction() throws IOException {
assertEqualsSaTifSourceConfigDtos(saTifSourceConfigDto, newSaTifSourceConfigDto);
}

public void testParseFunctionWithNullValues() throws IOException {
// Source config with invalid name and format
SATIFSourceConfigDto saTifSourceConfigDto = new SATIFSourceConfigDto(
"randomId",
null,
null,
null,
SourceConfigType.S3_CUSTOM,
null,
null,
null,
new S3Source("bucket", "objectkey", "region", "rolearn"),
null,
null,
new org.opensearch.jobscheduler.spi.schedule.IntervalSchedule(Instant.now(), 1, ChronoUnit.DAYS),
null,
null,
null,
null,
true,
List.of("ip"),
true
);
String json = toJsonString(saTifSourceConfigDto);
SATIFSourceConfigDto newSaTifSourceConfigDto = SATIFSourceConfigDto.parse(getParser(json), saTifSourceConfigDto.getId(), null);
assertEqualsSaTifSourceConfigDtos(saTifSourceConfigDto, newSaTifSourceConfigDto);
}

public XContentParser getParser(String xc) throws IOException {
XContentParser parser = XContentType.JSON.xContent().createParser(xContentRegistry(), LoggingDeprecationHandler.INSTANCE, xc);
parser.nextToken();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,17 @@
import org.opensearch.core.xcontent.XContentBuilder;
import org.opensearch.core.xcontent.XContentParser;
import org.opensearch.jobscheduler.spi.schedule.IntervalSchedule;
import org.opensearch.securityanalytics.commons.model.IOCType;
import org.opensearch.securityanalytics.threatIntel.common.SourceConfigType;
import org.opensearch.securityanalytics.threatIntel.model.DefaultIocStoreConfig;
import org.opensearch.securityanalytics.threatIntel.model.S3Source;
import org.opensearch.securityanalytics.threatIntel.model.SATIFSourceConfig;
import org.opensearch.test.OpenSearchTestCase;

import java.io.IOException;
import java.time.Instant;
import java.time.temporal.ChronoUnit;
import java.util.List;

import static org.opensearch.securityanalytics.TestHelpers.randomSATIFSourceConfig;

Expand All @@ -37,6 +42,35 @@ public void testParseFunction() throws IOException {
assertEqualsSaTifSourceConfigs(saTifSourceConfig, newSaTifSourceConfig);
}

public void testParseFunctionWithNullValues() throws IOException {
// Source config with invalid name and format
SATIFSourceConfig saTifSourceConfig = new SATIFSourceConfig(
null,
null,
null,
null,
SourceConfigType.S3_CUSTOM,
null,
null,
null,
new S3Source("bucket", "objectkey", "region", "rolearn"),
null,
null,
new org.opensearch.jobscheduler.spi.schedule.IntervalSchedule(Instant.now(), 1, ChronoUnit.DAYS),
null,
null,
null,
null,
true,
new DefaultIocStoreConfig(List.of(new DefaultIocStoreConfig.IocToIndexDetails(new IOCType(IOCType.DOMAIN_NAME_TYPE), "indexPattern", "writeIndex"))),
List.of("ip"),
true
);
String json = toJsonString(saTifSourceConfig);
SATIFSourceConfig newSaTifSourceConfig = SATIFSourceConfig.parse(getParser(json), saTifSourceConfig.getId(), null);
assertEqualsSaTifSourceConfigs(saTifSourceConfig, newSaTifSourceConfig);
}

public XContentParser getParser(String xc) throws IOException {
XContentParser parser = XContentType.JSON.xContent().createParser(xContentRegistry(), LoggingDeprecationHandler.INSTANCE, xc);
parser.nextToken();
Expand Down

0 comments on commit 0bb0651

Please sign in to comment.