From 1a24f1ab4df584ad059a987ef086fbb07d02ff69 Mon Sep 17 00:00:00 2001 From: Marcos Gonzalez Mayedo Date: Mon, 10 Jul 2023 15:15:36 -0700 Subject: [PATCH] Added changes to tests for readability, fixed typos, and renamed files for CloudWatchLogs prefixing Signed-off-by: Marcos Gonzalez Mayedo --- ....java => CloudWatchLogsClientFactory.java} | 6 +- ...fig.java => CloudWatchLogsSinkConfig.java} | 2 +- .../sink/threshold/ThresholdCheck.java | 20 ++--- .../buffer/InMemoryBufferFactoryTest.java | 20 +++++ .../sink/buffer/InMemoryBufferTest.java | 20 +++-- ...a => CloudWatchLogsClientFactoryTest.java} | 13 ++-- .../AwsConfigTest.java | 2 +- .../config/CloudWatchLogsSinkConfigTest.java | 75 +++++++++++++++++++ .../ThresholdConfigTest.java | 2 +- .../sink/configuration/CwlSinkConfigTest.java | 75 ------------------- .../sink/threshold/ThresholdCheckTest.java | 49 +++++++----- 11 files changed, 161 insertions(+), 123 deletions(-) rename data-prepper-plugins/cloudwatch-logs/src/main/java/org/opensearch/dataprepper/plugins/sink/client/{CwlClientFactory.java => CloudWatchLogsClientFactory.java} (95%) rename data-prepper-plugins/cloudwatch-logs/src/main/java/org/opensearch/dataprepper/plugins/sink/config/{CwlSinkConfig.java => CloudWatchLogsSinkConfig.java} (96%) create mode 100644 data-prepper-plugins/cloudwatch-logs/src/test/java/org/opensearch/dataprepper/plugins/sink/buffer/InMemoryBufferFactoryTest.java rename data-prepper-plugins/cloudwatch-logs/src/test/java/org/opensearch/dataprepper/plugins/sink/client/{CwlClientFactoryTest.java => CloudWatchLogsClientFactoryTest.java} (89%) rename data-prepper-plugins/cloudwatch-logs/src/test/java/org/opensearch/dataprepper/plugins/sink/{configuration => config}/AwsConfigTest.java (98%) create mode 100644 data-prepper-plugins/cloudwatch-logs/src/test/java/org/opensearch/dataprepper/plugins/sink/config/CloudWatchLogsSinkConfigTest.java rename data-prepper-plugins/cloudwatch-logs/src/test/java/org/opensearch/dataprepper/plugins/sink/{configuration => config}/ThresholdConfigTest.java (98%) delete mode 100644 data-prepper-plugins/cloudwatch-logs/src/test/java/org/opensearch/dataprepper/plugins/sink/configuration/CwlSinkConfigTest.java diff --git a/data-prepper-plugins/cloudwatch-logs/src/main/java/org/opensearch/dataprepper/plugins/sink/client/CwlClientFactory.java b/data-prepper-plugins/cloudwatch-logs/src/main/java/org/opensearch/dataprepper/plugins/sink/client/CloudWatchLogsClientFactory.java similarity index 95% rename from data-prepper-plugins/cloudwatch-logs/src/main/java/org/opensearch/dataprepper/plugins/sink/client/CwlClientFactory.java rename to data-prepper-plugins/cloudwatch-logs/src/main/java/org/opensearch/dataprepper/plugins/sink/client/CloudWatchLogsClientFactory.java index c28fc94e77..18b9e9ebbb 100644 --- a/data-prepper-plugins/cloudwatch-logs/src/main/java/org/opensearch/dataprepper/plugins/sink/client/CwlClientFactory.java +++ b/data-prepper-plugins/cloudwatch-logs/src/main/java/org/opensearch/dataprepper/plugins/sink/client/CloudWatchLogsClientFactory.java @@ -19,7 +19,7 @@ * client for interfacing with * CloudWatchLogs services. */ -public final class CwlClientFactory { +public final class CloudWatchLogsClientFactory { /** * Generates a CloudWatchLogs Client based on STS role ARN system credentials. @@ -32,10 +32,10 @@ public static CloudWatchLogsClient createCwlClient(final AwsConfig awsConfig, fi return CloudWatchLogsClient.builder() .region(awsConfig.getAwsRegion()) .credentialsProvider(awsCredentialsProvider) - .overrideConfiguration(createOverrideConfiguration(awsConfig)).build(); + .overrideConfiguration(createOverrideConfiguration()).build(); } - private static ClientOverrideConfiguration createOverrideConfiguration(final AwsConfig awsConfig) { + private static ClientOverrideConfiguration createOverrideConfiguration() { final RetryPolicy retryPolicy = RetryPolicy.builder().numRetries(AwsConfig.DEFAULT_CONNECTION_ATTEMPTS).build(); return ClientOverrideConfiguration.builder() diff --git a/data-prepper-plugins/cloudwatch-logs/src/main/java/org/opensearch/dataprepper/plugins/sink/config/CwlSinkConfig.java b/data-prepper-plugins/cloudwatch-logs/src/main/java/org/opensearch/dataprepper/plugins/sink/config/CloudWatchLogsSinkConfig.java similarity index 96% rename from data-prepper-plugins/cloudwatch-logs/src/main/java/org/opensearch/dataprepper/plugins/sink/config/CwlSinkConfig.java rename to data-prepper-plugins/cloudwatch-logs/src/main/java/org/opensearch/dataprepper/plugins/sink/config/CloudWatchLogsSinkConfig.java index 80cafcf48c..6d9088d4e2 100644 --- a/data-prepper-plugins/cloudwatch-logs/src/main/java/org/opensearch/dataprepper/plugins/sink/config/CwlSinkConfig.java +++ b/data-prepper-plugins/cloudwatch-logs/src/main/java/org/opensearch/dataprepper/plugins/sink/config/CloudWatchLogsSinkConfig.java @@ -10,7 +10,7 @@ import jakarta.validation.constraints.NotEmpty; import jakarta.validation.constraints.NotNull; -public class CwlSinkConfig { +public class CloudWatchLogsSinkConfig { public static final String DEFAULT_BUFFER_TYPE = "in_memory"; @JsonProperty("aws") diff --git a/data-prepper-plugins/cloudwatch-logs/src/main/java/org/opensearch/dataprepper/plugins/sink/threshold/ThresholdCheck.java b/data-prepper-plugins/cloudwatch-logs/src/main/java/org/opensearch/dataprepper/plugins/sink/threshold/ThresholdCheck.java index a30be60c7f..7916efeca8 100644 --- a/data-prepper-plugins/cloudwatch-logs/src/main/java/org/opensearch/dataprepper/plugins/sink/threshold/ThresholdCheck.java +++ b/data-prepper-plugins/cloudwatch-logs/src/main/java/org/opensearch/dataprepper/plugins/sink/threshold/ThresholdCheck.java @@ -5,7 +5,7 @@ package org.opensearch.dataprepper.plugins.sink.threshold; /** - * ThresholdCheck receives paramaters for which to reference the + * ThresholdCheck receives parameters for which to reference the * limits of a buffer and CloudWatchLogsClient before making a * PutLogEvent request to AWS. */ @@ -30,8 +30,8 @@ public ThresholdCheck(final int batchSize, final int maxEventSizeBytes, final in * @return boolean - true if we exceed the threshold events or false otherwise. */ public boolean isGreaterThanThresholdReached(final long currentTime, final int currentRequestSize, final int batchSize) { - return ((checkGreaterThanBatchSize(batchSize) || checkLogSendInterval(currentTime) - || checkGreaterThanMaxRequestSize(currentRequestSize)) && (batchSize > 0)); + return ((isGreaterThanBatchSize(batchSize) || isGreaterEqualToLogSendInterval(currentTime) + || isGreaterThanMaxRequestSize(currentRequestSize)) && (batchSize > 0)); } /** @@ -41,7 +41,7 @@ public boolean isGreaterThanThresholdReached(final long currentTime, final int c * @return boolean - true if we equal the threshold events or false otherwise. */ public boolean isEqualToThresholdReached(final int currentRequestSize, final int batchSize) { - return ((checkEqualBatchSize(batchSize) || checkEqualMaxRequestSize(currentRequestSize)) && (batchSize > 0)); + return ((isEqualBatchSize(batchSize) || isEqualMaxRequestSize(currentRequestSize)) && (batchSize > 0)); } /** @@ -50,7 +50,7 @@ public boolean isEqualToThresholdReached(final int currentRequestSize, final int * @param currentTimeSeconds int denoting seconds. * @return boolean - true if greater than or equal to logInterval, false otherwise. */ - private boolean checkLogSendInterval(final long currentTimeSeconds) { + private boolean isGreaterEqualToLogSendInterval(final long currentTimeSeconds) { return currentTimeSeconds >= logSendInterval; } @@ -59,7 +59,7 @@ private boolean checkLogSendInterval(final long currentTimeSeconds) { * @param eventSize int denoting size of event. * @return boolean - true if greater than MaxEventSize, false otherwise. */ - public boolean checkGreaterThanMaxEventSize(final int eventSize) { + public boolean isGreaterThanMaxEventSize(final int eventSize) { return eventSize > maxEventSizeBytes; } @@ -68,7 +68,7 @@ public boolean checkGreaterThanMaxEventSize(final int eventSize) { * @param currentRequestSize int denoting size of request(Sum of PutLogEvent messages). * @return boolean - true if greater than Max request size, smaller otherwise. */ - private boolean checkGreaterThanMaxRequestSize(final int currentRequestSize) { + private boolean isGreaterThanMaxRequestSize(final int currentRequestSize) { return currentRequestSize > maxRequestSizeBytes; } @@ -78,7 +78,7 @@ private boolean checkGreaterThanMaxRequestSize(final int currentRequestSize) { * @param batchSize int denoting the size of the batch of PutLogEvents. * @return boolean - true if greater, false otherwise. */ - private boolean checkGreaterThanBatchSize(final int batchSize) { + private boolean isGreaterThanBatchSize(final int batchSize) { return batchSize > this.batchSize; } @@ -87,11 +87,11 @@ private boolean checkGreaterThanBatchSize(final int batchSize) { * @param currentRequestSize int denoting size of request(Sum of PutLogEvent messages). * @return boolean - true if equal Max request size, smaller otherwise. */ - private boolean checkEqualMaxRequestSize(final int currentRequestSize) { + private boolean isEqualMaxRequestSize(final int currentRequestSize) { return currentRequestSize == maxRequestSizeBytes; } - private boolean checkEqualBatchSize(final int batchSize) { + private boolean isEqualBatchSize(final int batchSize) { return batchSize == this.batchSize; } } diff --git a/data-prepper-plugins/cloudwatch-logs/src/test/java/org/opensearch/dataprepper/plugins/sink/buffer/InMemoryBufferFactoryTest.java b/data-prepper-plugins/cloudwatch-logs/src/test/java/org/opensearch/dataprepper/plugins/sink/buffer/InMemoryBufferFactoryTest.java new file mode 100644 index 0000000000..cac52a5525 --- /dev/null +++ b/data-prepper-plugins/cloudwatch-logs/src/test/java/org/opensearch/dataprepper/plugins/sink/buffer/InMemoryBufferFactoryTest.java @@ -0,0 +1,20 @@ +/* + * Copyright OpenSearch Contributors + * SPDX-License-Identifier: Apache-2.0 + */ + +package org.opensearch.dataprepper.plugins.sink.buffer; + +import org.junit.jupiter.api.Test; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.*; + +public class InMemoryBufferFactoryTest { + @Test + void check_buffer_not_null() { + Buffer buffer = new InMemoryBufferFactory().getBuffer(); + assertThat(buffer, notNullValue()); + assertThat(buffer.getClass(), typeCompatibleWith(Buffer.class)); + } +} diff --git a/data-prepper-plugins/cloudwatch-logs/src/test/java/org/opensearch/dataprepper/plugins/sink/buffer/InMemoryBufferTest.java b/data-prepper-plugins/cloudwatch-logs/src/test/java/org/opensearch/dataprepper/plugins/sink/buffer/InMemoryBufferTest.java index fcdbd2562b..1b14aab052 100644 --- a/data-prepper-plugins/cloudwatch-logs/src/test/java/org/opensearch/dataprepper/plugins/sink/buffer/InMemoryBufferTest.java +++ b/data-prepper-plugins/cloudwatch-logs/src/test/java/org/opensearch/dataprepper/plugins/sink/buffer/InMemoryBufferTest.java @@ -18,6 +18,8 @@ public class InMemoryBufferTest { private static InMemoryBuffer inMemoryBuffer; + public static final String TEST_EVENT_MESSAGE = "testing"; + public static final int TEST_COLLECTION_SIZE = 3; @BeforeEach void setUp() { @@ -27,15 +29,19 @@ void setUp() { ArrayList> getTestCollection() { ArrayList> testCollection = new ArrayList<>(); - for (int i = 0; i < 3; i++) { - testCollection.add(new Record<>(JacksonEvent.fromMessage("testing"))); + for (int i = 0; i < TEST_COLLECTION_SIZE; i++) { + testCollection.add(new Record<>(JacksonEvent.fromMessage(TEST_EVENT_MESSAGE))); } return testCollection; } String getStringJsonMessage() { - return JacksonEvent.fromMessage("testing").toJsonString(); + return JacksonEvent.fromMessage(TEST_EVENT_MESSAGE).toJsonString(); + } + + int getStringJsonMessageSize() { + return JacksonEvent.fromMessage(TEST_EVENT_MESSAGE).toJsonString().length(); } @Test @@ -50,7 +56,7 @@ void check_buffer_has_right_number_of_events_test() { inMemoryBuffer.writeEvent(eventToTest.getData().toJsonString().getBytes()); } - assertThat(inMemoryBuffer.getEventCount(), equalTo(3)); + assertThat(inMemoryBuffer.getEventCount(), equalTo(TEST_COLLECTION_SIZE)); } @Test @@ -61,7 +67,7 @@ void check_right_event_count_after_event_fetch_test() { inMemoryBuffer.popEvent(); - assertThat(inMemoryBuffer.getEventCount(), equalTo(2)); + assertThat(inMemoryBuffer.getEventCount(), equalTo(TEST_COLLECTION_SIZE - 1)); } @Test @@ -72,7 +78,7 @@ void check_right_buffer_size_after_event_fetch_test() { inMemoryBuffer.popEvent(); - assertThat(inMemoryBuffer.getBufferSize(), equalTo(42)); + assertThat(inMemoryBuffer.getBufferSize(), equalTo(getStringJsonMessageSize() * (TEST_COLLECTION_SIZE - 1))); } @Test @@ -81,7 +87,7 @@ void check_buffer_has_right_size_test() { inMemoryBuffer.writeEvent(eventToTest.getData().toJsonString().getBytes()); } - assertThat(inMemoryBuffer.getBufferSize(), equalTo(63)); + assertThat(inMemoryBuffer.getBufferSize(), equalTo(getStringJsonMessageSize() * TEST_COLLECTION_SIZE)); } //TODO: Add tests for getting events. diff --git a/data-prepper-plugins/cloudwatch-logs/src/test/java/org/opensearch/dataprepper/plugins/sink/client/CwlClientFactoryTest.java b/data-prepper-plugins/cloudwatch-logs/src/test/java/org/opensearch/dataprepper/plugins/sink/client/CloudWatchLogsClientFactoryTest.java similarity index 89% rename from data-prepper-plugins/cloudwatch-logs/src/test/java/org/opensearch/dataprepper/plugins/sink/client/CwlClientFactoryTest.java rename to data-prepper-plugins/cloudwatch-logs/src/test/java/org/opensearch/dataprepper/plugins/sink/client/CloudWatchLogsClientFactoryTest.java index 0ad15ada70..ae2d5e954a 100644 --- a/data-prepper-plugins/cloudwatch-logs/src/test/java/org/opensearch/dataprepper/plugins/sink/client/CwlClientFactoryTest.java +++ b/data-prepper-plugins/cloudwatch-logs/src/test/java/org/opensearch/dataprepper/plugins/sink/client/CloudWatchLogsClientFactoryTest.java @@ -25,12 +25,13 @@ import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.notNullValue; +import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; import static org.mockito.Mockito.mockStatic; import static org.mockito.Mockito.verify; -public class CwlClientFactoryTest { +public class CloudWatchLogsClientFactoryTest { private AwsConfig awsConfig; private AwsCredentialsSupplier awsCredentialsSupplier; private AwsCredentialsOptions awsCredentialsOptions; @@ -45,16 +46,16 @@ void setUp() { @Test void check_created_real_default_client_test() { - final CloudWatchLogsClient cloudWatchLogsClientToTest = CwlClientFactory.createCwlClient(awsConfig, awsCredentialsSupplier); + final CloudWatchLogsClient cloudWatchLogsClientToTest = CloudWatchLogsClientFactory.createCwlClient(awsConfig, awsCredentialsSupplier); - assertThat(cloudWatchLogsClientToTest, notNullValue()); + assertNotNull(cloudWatchLogsClientToTest); } @Test void check_created_client_with_no_params() { - final CloudWatchLogsClient cloudWatchLogsClient = CwlClientFactory.createCwlClient(awsConfig, awsCredentialsSupplier); + final CloudWatchLogsClient cloudWatchLogsClient = CloudWatchLogsClientFactory.createCwlClient(awsConfig, awsCredentialsSupplier); - assertThat(cloudWatchLogsClient, notNullValue()); + assertNotNull(cloudWatchLogsClient); } @Test @@ -76,7 +77,7 @@ void check_CwlClient_with_correct_inputs() { try(final MockedStatic cloudWatchLogsClientMockedStatic = mockStatic(CloudWatchLogsClient.class)) { cloudWatchLogsClientMockedStatic.when(CloudWatchLogsClient::builder) .thenReturn(cloudWatchLogsClientBuilder); - CwlClientFactory.createCwlClient(awsConfig, awsCredentialsSupplier); + CloudWatchLogsClientFactory.createCwlClient(awsConfig, awsCredentialsSupplier); } final ArgumentCaptor credentialsProviderArgumentCaptor = ArgumentCaptor.forClass(AwsCredentialsProvider.class); diff --git a/data-prepper-plugins/cloudwatch-logs/src/test/java/org/opensearch/dataprepper/plugins/sink/configuration/AwsConfigTest.java b/data-prepper-plugins/cloudwatch-logs/src/test/java/org/opensearch/dataprepper/plugins/sink/config/AwsConfigTest.java similarity index 98% rename from data-prepper-plugins/cloudwatch-logs/src/test/java/org/opensearch/dataprepper/plugins/sink/configuration/AwsConfigTest.java rename to data-prepper-plugins/cloudwatch-logs/src/test/java/org/opensearch/dataprepper/plugins/sink/config/AwsConfigTest.java index 7de34bce21..53f68100a4 100644 --- a/data-prepper-plugins/cloudwatch-logs/src/test/java/org/opensearch/dataprepper/plugins/sink/configuration/AwsConfigTest.java +++ b/data-prepper-plugins/cloudwatch-logs/src/test/java/org/opensearch/dataprepper/plugins/sink/config/AwsConfigTest.java @@ -3,7 +3,7 @@ * SPDX-License-Identifier: Apache-2.0 */ -package org.opensearch.dataprepper.plugins.sink.configuration; +package org.opensearch.dataprepper.plugins.sink.config; import com.fasterxml.jackson.databind.ObjectMapper; import org.junit.jupiter.api.BeforeEach; diff --git a/data-prepper-plugins/cloudwatch-logs/src/test/java/org/opensearch/dataprepper/plugins/sink/config/CloudWatchLogsSinkConfigTest.java b/data-prepper-plugins/cloudwatch-logs/src/test/java/org/opensearch/dataprepper/plugins/sink/config/CloudWatchLogsSinkConfigTest.java new file mode 100644 index 0000000000..956fb51e69 --- /dev/null +++ b/data-prepper-plugins/cloudwatch-logs/src/test/java/org/opensearch/dataprepper/plugins/sink/config/CloudWatchLogsSinkConfigTest.java @@ -0,0 +1,75 @@ +/* + * Copyright OpenSearch Contributors + * SPDX-License-Identifier: Apache-2.0 + */ + +package org.opensearch.dataprepper.plugins.sink.config; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.opensearch.dataprepper.plugins.sink.config.AwsConfig; +import org.opensearch.dataprepper.plugins.sink.config.CloudWatchLogsSinkConfig; +import org.opensearch.dataprepper.plugins.sink.config.ThresholdConfig; +import org.opensearch.dataprepper.test.helper.ReflectivelySetField; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.notNullValue; + +public class CloudWatchLogsSinkConfigTest { + private CloudWatchLogsSinkConfig cloudWatchLogsSinkConfig; + private AwsConfig awsConfig; + private ThresholdConfig thresholdConfig; + private final String LOG_GROUP = "testLogGroup"; + private final String LOG_STREAM = "testLogStream"; + + @BeforeEach + void setUp() { + cloudWatchLogsSinkConfig = new CloudWatchLogsSinkConfig(); + awsConfig = new AwsConfig(); + thresholdConfig = new ThresholdConfig(); + } + + @Test + void check_null_auth_config_test() { + assertThat(new CloudWatchLogsSinkConfig().getAwsConfig(), equalTo(null)); + } + + @Test + void check_null_threshold_config_test() { + assertThat(new CloudWatchLogsSinkConfig().getThresholdConfig(), notNullValue()); + } + + @Test + void check_default_buffer_type_test() { + assertThat(new CloudWatchLogsSinkConfig().getBufferType(), equalTo(CloudWatchLogsSinkConfig.DEFAULT_BUFFER_TYPE)); + } + + @Test + void check_null_log_group_test() { + assertThat(new CloudWatchLogsSinkConfig().getLogGroup(), equalTo(null)); + } + + @Test + void check_null_log_stream_test() { + assertThat(new CloudWatchLogsSinkConfig().getLogStream(), equalTo(null)); + } + + @Test + void check_valid_log_group_and_log_stream_test() throws NoSuchFieldException, IllegalAccessException { + ReflectivelySetField.setField(cloudWatchLogsSinkConfig.getClass(), cloudWatchLogsSinkConfig, "logGroup", LOG_GROUP); + ReflectivelySetField.setField(cloudWatchLogsSinkConfig.getClass(), cloudWatchLogsSinkConfig, "logStream", LOG_STREAM); + + assertThat(cloudWatchLogsSinkConfig.getLogGroup(), equalTo(LOG_GROUP)); + assertThat(cloudWatchLogsSinkConfig.getLogStream(), equalTo(LOG_STREAM)); + } + + @Test + void check_valid_sub_config_test() throws NoSuchFieldException, IllegalAccessException { + ReflectivelySetField.setField(cloudWatchLogsSinkConfig.getClass(), cloudWatchLogsSinkConfig, "thresholdConfig", thresholdConfig); + ReflectivelySetField.setField(cloudWatchLogsSinkConfig.getClass(), cloudWatchLogsSinkConfig, "awsConfig", awsConfig); + + assertThat(cloudWatchLogsSinkConfig.getAwsConfig(), equalTo(awsConfig)); + assertThat(cloudWatchLogsSinkConfig.getThresholdConfig(), equalTo(thresholdConfig)); + } +} \ No newline at end of file diff --git a/data-prepper-plugins/cloudwatch-logs/src/test/java/org/opensearch/dataprepper/plugins/sink/configuration/ThresholdConfigTest.java b/data-prepper-plugins/cloudwatch-logs/src/test/java/org/opensearch/dataprepper/plugins/sink/config/ThresholdConfigTest.java similarity index 98% rename from data-prepper-plugins/cloudwatch-logs/src/test/java/org/opensearch/dataprepper/plugins/sink/configuration/ThresholdConfigTest.java rename to data-prepper-plugins/cloudwatch-logs/src/test/java/org/opensearch/dataprepper/plugins/sink/config/ThresholdConfigTest.java index f32ad28e7f..9e74c04501 100644 --- a/data-prepper-plugins/cloudwatch-logs/src/test/java/org/opensearch/dataprepper/plugins/sink/configuration/ThresholdConfigTest.java +++ b/data-prepper-plugins/cloudwatch-logs/src/test/java/org/opensearch/dataprepper/plugins/sink/config/ThresholdConfigTest.java @@ -3,7 +3,7 @@ * SPDX-License-Identifier: Apache-2.0 */ -package org.opensearch.dataprepper.plugins.sink.configuration; +package org.opensearch.dataprepper.plugins.sink.config; import com.fasterxml.jackson.databind.ObjectMapper; import org.junit.jupiter.api.BeforeEach; diff --git a/data-prepper-plugins/cloudwatch-logs/src/test/java/org/opensearch/dataprepper/plugins/sink/configuration/CwlSinkConfigTest.java b/data-prepper-plugins/cloudwatch-logs/src/test/java/org/opensearch/dataprepper/plugins/sink/configuration/CwlSinkConfigTest.java deleted file mode 100644 index 99f8cb1ffb..0000000000 --- a/data-prepper-plugins/cloudwatch-logs/src/test/java/org/opensearch/dataprepper/plugins/sink/configuration/CwlSinkConfigTest.java +++ /dev/null @@ -1,75 +0,0 @@ -/* - * Copyright OpenSearch Contributors - * SPDX-License-Identifier: Apache-2.0 - */ - -package org.opensearch.dataprepper.plugins.sink.configuration; - -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; -import org.opensearch.dataprepper.plugins.sink.config.AwsConfig; -import org.opensearch.dataprepper.plugins.sink.config.CwlSinkConfig; -import org.opensearch.dataprepper.plugins.sink.config.ThresholdConfig; -import org.opensearch.dataprepper.test.helper.ReflectivelySetField; - -import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.Matchers.equalTo; -import static org.hamcrest.Matchers.notNullValue; - -public class CwlSinkConfigTest { - private CwlSinkConfig cwlSinkConfig; - private AwsConfig awsConfig; - private ThresholdConfig thresholdConfig; - private final String LOG_GROUP = "testLogGroup"; - private final String LOG_STREAM = "testLogStream"; - - @BeforeEach - void setUp() { - cwlSinkConfig = new CwlSinkConfig(); - awsConfig = new AwsConfig(); - thresholdConfig = new ThresholdConfig(); - } - - @Test - void check_null_auth_config_test() { - assertThat(new CwlSinkConfig().getAwsConfig(), equalTo(null)); - } - - @Test - void check_null_threshold_config_test() { - assertThat(new CwlSinkConfig().getThresholdConfig(), notNullValue()); - } - - @Test - void check_default_buffer_type_test() { - assertThat(new CwlSinkConfig().getBufferType(), equalTo(CwlSinkConfig.DEFAULT_BUFFER_TYPE)); - } - - @Test - void check_null_log_group_test() { - assertThat(new CwlSinkConfig().getLogGroup(), equalTo(null)); - } - - @Test - void check_null_log_stream_test() { - assertThat(new CwlSinkConfig().getLogStream(), equalTo(null)); - } - - @Test - void check_valid_log_group_and_log_stream_test() throws NoSuchFieldException, IllegalAccessException { - ReflectivelySetField.setField(cwlSinkConfig.getClass(), cwlSinkConfig, "logGroup", LOG_GROUP); - ReflectivelySetField.setField(cwlSinkConfig.getClass(), cwlSinkConfig, "logStream", LOG_STREAM); - - assertThat(cwlSinkConfig.getLogGroup(), equalTo(LOG_GROUP)); - assertThat(cwlSinkConfig.getLogStream(), equalTo(LOG_STREAM)); - } - - @Test - void check_valid_sub_config_test() throws NoSuchFieldException, IllegalAccessException { - ReflectivelySetField.setField(cwlSinkConfig.getClass(), cwlSinkConfig, "thresholdConfig", thresholdConfig); - ReflectivelySetField.setField(cwlSinkConfig.getClass(), cwlSinkConfig, "awsConfig", awsConfig); - - assertThat(cwlSinkConfig.getAwsConfig(), equalTo(awsConfig)); - assertThat(cwlSinkConfig.getThresholdConfig(), equalTo(thresholdConfig)); - } -} \ No newline at end of file diff --git a/data-prepper-plugins/cloudwatch-logs/src/test/java/org/opensearch/dataprepper/plugins/sink/threshold/ThresholdCheckTest.java b/data-prepper-plugins/cloudwatch-logs/src/test/java/org/opensearch/dataprepper/plugins/sink/threshold/ThresholdCheckTest.java index 3dc7def333..7afd69ade7 100644 --- a/data-prepper-plugins/cloudwatch-logs/src/test/java/org/opensearch/dataprepper/plugins/sink/threshold/ThresholdCheckTest.java +++ b/data-prepper-plugins/cloudwatch-logs/src/test/java/org/opensearch/dataprepper/plugins/sink/threshold/ThresholdCheckTest.java @@ -11,9 +11,8 @@ import org.junit.jupiter.params.provider.ValueSource; import org.opensearch.dataprepper.plugins.sink.config.ThresholdConfig; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.Matchers.equalTo; -import static org.hamcrest.Matchers.is; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; public class ThresholdCheckTest { private ThresholdCheck thresholdCheck; @@ -27,68 +26,80 @@ void setUp() { @ParameterizedTest @ValueSource(ints = {55, 80, 100}) void check_max_event_size_invalid(final int event_size) { - assertThat(thresholdCheck.checkGreaterThanMaxEventSize(event_size), is(true)); + boolean isEventGreater = thresholdCheck.isGreaterThanMaxEventSize(event_size); + assertTrue(isEventGreater); } @ParameterizedTest @ValueSource(ints = {10, 30, 50}) void check_max_event_size_valid(final int event_size) { - assertThat(thresholdCheck.checkGreaterThanMaxEventSize(event_size), is(false)); + boolean isEventGreater = thresholdCheck.isGreaterThanMaxEventSize(event_size); + assertFalse(isEventGreater); } @ParameterizedTest @ValueSource(ints = {60, 80, 100}) void check_greater_than_threshold_conditions_time_true(final int send_interval) { - assertThat(thresholdCheck.isGreaterThanThresholdReached(send_interval, ThresholdConfig.DEFAULT_SIZE_OF_REQUEST,ThresholdConfig.DEFAULT_BATCH_SIZE), equalTo(true)); + boolean thresholdMetTime = thresholdCheck.isGreaterThanThresholdReached(send_interval, ThresholdConfig.DEFAULT_SIZE_OF_REQUEST,ThresholdConfig.DEFAULT_BATCH_SIZE); + assertTrue(thresholdMetTime); } @ParameterizedTest @ValueSource(ints = {1, 3, 59}) void check_greater_than_threshold_conditions_time_false(final int send_interval) { - assertThat(thresholdCheck.isGreaterThanThresholdReached(send_interval, ThresholdConfig.DEFAULT_SIZE_OF_REQUEST,ThresholdConfig.DEFAULT_BATCH_SIZE), equalTo(false)); + boolean thresholdMetTime = thresholdCheck.isGreaterThanThresholdReached(send_interval, ThresholdConfig.DEFAULT_SIZE_OF_REQUEST,ThresholdConfig.DEFAULT_BATCH_SIZE); + assertFalse(thresholdMetTime); } @ParameterizedTest @ValueSource(ints = {550000, 750000, 1000000}) - void check_greater_than_threshold_conditions_requeset_size_true(final int request_size) { - assertThat(thresholdCheck.isGreaterThanThresholdReached(ThresholdConfig.DEFAULT_LOG_SEND_INTERVAL_TIME - 1, request_size, ThresholdConfig.DEFAULT_BATCH_SIZE), is(true)); + void check_greater_than_threshold_conditions_request_size_true(final int request_size) { + boolean thresholdMetRequestSize = thresholdCheck.isGreaterThanThresholdReached(ThresholdConfig.DEFAULT_LOG_SEND_INTERVAL_TIME - 1, request_size, ThresholdConfig.DEFAULT_BATCH_SIZE); + assertTrue(thresholdMetRequestSize); } @ParameterizedTest @ValueSource(ints = {10000, 250000, 500000}) - void check_greater_than_threshold_conditions_requeset_size_false(final int request_size) { - assertThat(thresholdCheck.isGreaterThanThresholdReached(ThresholdConfig.DEFAULT_LOG_SEND_INTERVAL_TIME - 1, request_size, ThresholdConfig.DEFAULT_BATCH_SIZE), is(false)); + void check_greater_than_threshold_conditions_request_size_false(final int request_size) { + boolean thresholdMetRequestSize = thresholdCheck.isGreaterThanThresholdReached(ThresholdConfig.DEFAULT_LOG_SEND_INTERVAL_TIME - 1, request_size, ThresholdConfig.DEFAULT_BATCH_SIZE); + assertFalse(thresholdMetRequestSize); } @ParameterizedTest @ValueSource(ints = {26, 50, 100}) void check_greater_than_threshold_conditions_batch_size_true(final int batch_size) { - assertThat(thresholdCheck.isGreaterThanThresholdReached(ThresholdConfig.DEFAULT_LOG_SEND_INTERVAL_TIME -1, ThresholdConfig.DEFAULT_SIZE_OF_REQUEST, batch_size), is(true)); + boolean thresholdMetBatchSize = thresholdCheck.isGreaterThanThresholdReached(ThresholdConfig.DEFAULT_LOG_SEND_INTERVAL_TIME -1, ThresholdConfig.DEFAULT_SIZE_OF_REQUEST, batch_size); + assertTrue(thresholdMetBatchSize); } @ParameterizedTest @ValueSource(ints = {1, 10, 25}) void check_greater_than_threshold_conditions_batch_size_false(final int batch_size) { - assertThat(thresholdCheck.isGreaterThanThresholdReached(ThresholdConfig.DEFAULT_LOG_SEND_INTERVAL_TIME -1, ThresholdConfig.DEFAULT_SIZE_OF_REQUEST, batch_size), is(false)); + boolean thresholdMetBatchSize = thresholdCheck.isGreaterThanThresholdReached(ThresholdConfig.DEFAULT_LOG_SEND_INTERVAL_TIME -1, ThresholdConfig.DEFAULT_SIZE_OF_REQUEST, batch_size); + assertFalse(thresholdMetBatchSize); } @Test - void check_equal_than_threshold_conditions_requeset_size_true() { - assertThat(thresholdCheck.isEqualToThresholdReached(ThresholdConfig.DEFAULT_SIZE_OF_REQUEST, ThresholdConfig.DEFAULT_BATCH_SIZE - 1), is(true)); + void check_equal_than_threshold_conditions_request_size_true() { + boolean thresholdMetRequestSize = thresholdCheck.isEqualToThresholdReached(ThresholdConfig.DEFAULT_SIZE_OF_REQUEST, ThresholdConfig.DEFAULT_BATCH_SIZE - 1); + assertTrue(thresholdMetRequestSize); } @Test - void check_equal_than_threshold_conditions_requeset_size_false() { - assertThat(thresholdCheck.isEqualToThresholdReached(ThresholdConfig.DEFAULT_SIZE_OF_REQUEST - 1, ThresholdConfig.DEFAULT_BATCH_SIZE - 1), is(false)); + void check_equal_than_threshold_conditions_request_size_false() { + boolean thresholdMetRequestSize = thresholdCheck.isEqualToThresholdReached(ThresholdConfig.DEFAULT_SIZE_OF_REQUEST - 1, ThresholdConfig.DEFAULT_BATCH_SIZE - 1); + assertFalse(thresholdMetRequestSize); } @Test void check_equal_than_threshold_conditions_batch_size_true() { - assertThat(thresholdCheck.isEqualToThresholdReached(ThresholdConfig.DEFAULT_SIZE_OF_REQUEST - 1, ThresholdConfig.DEFAULT_BATCH_SIZE), is(true)); + boolean thresholdMetBatchSize = thresholdCheck.isEqualToThresholdReached(ThresholdConfig.DEFAULT_SIZE_OF_REQUEST - 1, ThresholdConfig.DEFAULT_BATCH_SIZE); + assertTrue(thresholdMetBatchSize); } @Test void check_equal_than_threshold_conditions_batch_size_false() { - assertThat(thresholdCheck.isEqualToThresholdReached(ThresholdConfig.DEFAULT_SIZE_OF_REQUEST - 1, ThresholdConfig.DEFAULT_BATCH_SIZE - 1), is(false)); + boolean thresholdMetBatchSize = thresholdCheck.isEqualToThresholdReached(ThresholdConfig.DEFAULT_SIZE_OF_REQUEST - 1, ThresholdConfig.DEFAULT_BATCH_SIZE - 1); + assertFalse(thresholdMetBatchSize); } } \ No newline at end of file