Skip to content

Commit 7a9bebc

Browse files
committed
Validate the aws config via the @AssertTrue pattern
Signed-off-by: huy pham <huyp@amazon.com>
1 parent 84f193d commit 7a9bebc

4 files changed

Lines changed: 9 additions & 34 deletions

File tree

data-prepper-plugins/otlp-sink/src/main/java/org/opensearch/dataprepper/plugins/sink/otlp/OtlpSink.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,6 @@ public class OtlpSink extends AbstractSink<Record<Span>> {
4949
public OtlpSink(@Nonnull final AwsCredentialsSupplier awsCredentialsSupplier, @Nonnull final OtlpSinkConfig config, @Nonnull final PluginMetrics pluginMetrics, @Nonnull final PluginSetting pluginSetting) {
5050
super(pluginSetting);
5151

52-
config.validate();
53-
5452
this.sinkMetrics = new OtlpSinkMetrics(pluginMetrics, pluginSetting);
5553
this.buffer = new OtlpSinkBuffer(awsCredentialsSupplier, config, sinkMetrics);
5654
}

data-prepper-plugins/otlp-sink/src/main/java/org/opensearch/dataprepper/plugins/sink/otlp/configuration/OtlpSinkConfig.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
import com.fasterxml.jackson.annotation.JsonProperty;
88
import jakarta.validation.Valid;
9+
import jakarta.validation.constraints.AssertTrue;
910
import jakarta.validation.constraints.Min;
1011
import jakarta.validation.constraints.NotBlank;
1112
import lombok.Getter;
@@ -115,12 +116,9 @@ public String getStsExternalId() {
115116
/**
116117
* Validate the AWS configuration.
117118
* This method ensures breaking change in future release where non-AWS OTLP endpoints are supported.
118-
*
119-
* @throws IllegalArgumentException if the AWS configuration is invalid
120119
*/
121-
public void validate() {
122-
if (awsConfig == null) {
123-
throw new IllegalArgumentException("aws configuration is required");
124-
}
120+
@AssertTrue
121+
boolean isAwsConfigValid() {
122+
return awsConfig != null;
125123
}
126124
}

data-prepper-plugins/otlp-sink/src/test/java/org/opensearch/dataprepper/plugins/sink/otlp/OtlpSinkTest.java

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,8 @@
44
*/
55
package org.opensearch.dataprepper.plugins.sink.otlp;
66

7-
import org.junit.jupiter.api.Assertions;
87
import org.junit.jupiter.api.BeforeEach;
98
import org.junit.jupiter.api.Test;
10-
import org.junit.jupiter.api.function.Executable;
119
import org.opensearch.dataprepper.aws.api.AwsCredentialsSupplier;
1210
import org.opensearch.dataprepper.metrics.PluginMetrics;
1311
import org.opensearch.dataprepper.model.configuration.PluginSetting;
@@ -23,7 +21,6 @@
2321
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
2422
import static org.junit.jupiter.api.Assertions.assertFalse;
2523
import static org.junit.jupiter.api.Assertions.assertTrue;
26-
import static org.mockito.Mockito.doThrow;
2724
import static org.mockito.Mockito.mock;
2825
import static org.mockito.Mockito.verify;
2926
import static org.mockito.Mockito.verifyNoMoreInteractions;
@@ -70,23 +67,6 @@ void testInitialize_startsBuffer() {
7067
verify(mockBuffer).start();
7168
}
7269

73-
@Test
74-
void testConstructor_throwsWhenAwsConfigIsMissing() {
75-
doThrow(new IllegalArgumentException("aws configuration is required"))
76-
.when(mockConfig).validate();
77-
78-
// Act & Assert
79-
final Executable constructorCall = () ->
80-
new OtlpSink(mockAwsCredSupplier, mockConfig, mockMetrics, mockSetting);
81-
82-
final IllegalArgumentException thrown = Assertions.assertThrows(
83-
IllegalArgumentException.class,
84-
constructorCall
85-
);
86-
87-
Assertions.assertEquals("aws configuration is required", thrown.getMessage());
88-
}
89-
9070
@Test
9171
void testOutput_addsEveryRecordToBuffer() {
9272
// Arrange

data-prepper-plugins/otlp-sink/src/test/java/org/opensearch/dataprepper/plugins/sink/otlp/configuration/OtlpSinkConfigTest.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@
2121
import static org.hamcrest.MatcherAssert.assertThat;
2222
import static org.hamcrest.Matchers.equalTo;
2323
import static org.hamcrest.Matchers.nullValue;
24-
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
2524
import static org.junit.jupiter.api.Assertions.assertEquals;
25+
import static org.junit.jupiter.api.Assertions.assertFalse;
2626
import static org.junit.jupiter.api.Assertions.assertNull;
2727
import static org.junit.jupiter.api.Assertions.assertThrows;
2828
import static org.junit.jupiter.api.Assertions.assertTrue;
@@ -169,23 +169,22 @@ void testCustomThresholdAndRetries() throws Exception {
169169
}
170170

171171
@Test
172-
void testValidateAwsConfig_doesNotThrowWhenPresent() throws Exception {
172+
void testIsAwsConfigValid_returnsTrue_whenPresent() throws Exception {
173173
final String yaml = String.join("\n",
174174
"endpoint: \"" + EXPECTED_ENDPOINT + "\"",
175175
"aws: {}"
176176
);
177177
final OtlpSinkConfig config = mapper.readValue(yaml, OtlpSinkConfig.class);
178-
assertDoesNotThrow(config::validate);
178+
assertTrue(config.isAwsConfigValid());
179179
}
180180

181181
@Test
182-
void testValidateAwsConfig_throwsException() throws Exception {
182+
void testIsAwsConfigValid_returnsFalse_whenMissing() throws Exception {
183183
final String yaml = String.join("\n",
184184
"endpoint: \"" + EXPECTED_ENDPOINT + "\""
185185
);
186186
final OtlpSinkConfig config = mapper.readValue(yaml, OtlpSinkConfig.class);
187-
188-
assertThrows(IllegalArgumentException.class, config::validate);
187+
assertFalse(config.isAwsConfigValid());
189188
}
190189

191190
@Test

0 commit comments

Comments
 (0)