Skip to content

Commit

Permalink
Added changes to tests for readability, fixed typos, and renamed file…
Browse files Browse the repository at this point in the history
…s for CloudWatchLogs prefixing

Signed-off-by: Marcos Gonzalez Mayedo <[email protected]>
  • Loading branch information
Marcos Gonzalez Mayedo committed Jul 10, 2023
1 parent 976f06a commit 1a24f1a
Show file tree
Hide file tree
Showing 11 changed files with 161 additions and 123 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand All @@ -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));
}

/**
Expand All @@ -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));
}

/**
Expand All @@ -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;
}

Expand All @@ -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;
}

Expand All @@ -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;
}

Expand All @@ -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;
}

Expand All @@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -27,15 +29,19 @@ void setUp() {
ArrayList<Record<Event>> getTestCollection() {
ArrayList<Record<Event>> 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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand All @@ -76,7 +77,7 @@ void check_CwlClient_with_correct_inputs() {
try(final MockedStatic<CloudWatchLogsClient> cloudWatchLogsClientMockedStatic = mockStatic(CloudWatchLogsClient.class)) {
cloudWatchLogsClientMockedStatic.when(CloudWatchLogsClient::builder)
.thenReturn(cloudWatchLogsClientBuilder);
CwlClientFactory.createCwlClient(awsConfig, awsCredentialsSupplier);
CloudWatchLogsClientFactory.createCwlClient(awsConfig, awsCredentialsSupplier);
}

final ArgumentCaptor<AwsCredentialsProvider> credentialsProviderArgumentCaptor = ArgumentCaptor.forClass(AwsCredentialsProvider.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Loading

0 comments on commit 1a24f1a

Please sign in to comment.