Updated arn import to use software.amzon instead of com.amazonaws, also updated unit tests to be more explicit with the expected exceptions

This commit is contained in:
Ryan Pelaez 2023-06-14 17:26:27 -07:00
parent d2fe79112e
commit a3d10bf702
2 changed files with 38 additions and 34 deletions

View file

@ -19,13 +19,12 @@ import java.io.InputStream;
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.InvocationTargetException;
import java.util.Properties; import java.util.Properties;
import com.amazonaws.arn.Arn;
import org.apache.commons.beanutils.BeanUtilsBean; import org.apache.commons.beanutils.BeanUtilsBean;
import org.apache.commons.beanutils.ConvertUtilsBean; import org.apache.commons.beanutils.ConvertUtilsBean;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.Validate; import org.apache.commons.lang3.Validate;
import software.amazon.awssdk.arns.Arn;
import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.regions.Region;
/** /**
@ -74,20 +73,20 @@ public class KinesisClientLibConfigurator {
if (configuration.getStreamArn() != null && !configuration.getStreamArn().trim().isEmpty()) { if (configuration.getStreamArn() != null && !configuration.getStreamArn().trim().isEmpty()) {
Arn streamArnObj = Arn.fromString(configuration.getStreamArn()); Arn streamArnObj = Arn.fromString(configuration.getStreamArn());
if(!streamArnObj.getResource().getResourceType().equalsIgnoreCase("stream")){ if(!streamArnObj.resource().resourceType().get().equalsIgnoreCase("stream")){
throw new IllegalArgumentException(String.format("StreamArn has unsupported resource type of '%s'. Expected: stream", throw new IllegalArgumentException(String.format("StreamArn has unsupported resource type of '%s'. Expected: stream",
streamArnObj.getResource().getResourceType())); streamArnObj.resource().resourceType().get()));
} }
if(!streamArnObj.getService().equalsIgnoreCase("kinesis")){ if(!streamArnObj.service().equalsIgnoreCase("kinesis")){
throw new IllegalArgumentException(String.format("StreamArn has unsupported service type of '%s'. Expected: kinesis", throw new IllegalArgumentException(String.format("StreamArn has unsupported service type of '%s'. Expected: kinesis",
streamArnObj.getResource().getResourceType())); streamArnObj.service()));
} }
//Parse out the stream Name from the Arn (and/or override existing value for Stream Name) //Parse out the stream Name from the Arn (and/or override existing value for Stream Name)
String streamNameFromArn = streamArnObj.getResource().getResource(); String streamNameFromArn = streamArnObj.resource().resource();
configuration.setStreamName(streamNameFromArn); configuration.setStreamName(streamNameFromArn);
//Parse out the region from the Arn and set (and/or override existing value for region) //Parse out the region from the Arn and set (and/or override existing value for region)
Region regionObj = Region.of(streamArnObj.getRegion()); Region regionObj = Region.of(streamArnObj.region().get());
if(Region.regions().stream().filter(x -> x.id().equalsIgnoreCase(regionObj.id())).count() == 0){ if(Region.regions().stream().filter(x -> x.id().equalsIgnoreCase(regionObj.id())).count() == 0){
throw new IllegalArgumentException(String.format("%s is not a valid region", regionObj.id())); throw new IllegalArgumentException(String.format("%s is not a valid region", regionObj.id()));
} }

View file

@ -15,14 +15,15 @@
package software.amazon.kinesis.multilang; package software.amazon.kinesis.multilang;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThrows;
import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNotNull;
import static org.mockito.Mockito.when; import static org.mockito.Mockito.when;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.IOException; import java.io.IOException;
import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.mockito.Mock; import org.mockito.Mock;
import org.mockito.Mockito; import org.mockito.Mockito;
@ -31,7 +32,6 @@ import org.mockito.runners.MockitoJUnitRunner;
import junit.framework.Assert; import junit.framework.Assert;
import software.amazon.awssdk.auth.credentials.AwsCredentials; import software.amazon.awssdk.auth.credentials.AwsCredentials;
import software.amazon.awssdk.auth.credentials.AwsCredentialsProvider; import software.amazon.awssdk.auth.credentials.AwsCredentialsProvider;
import software.amazon.awssdk.services.kinesis.model.InvalidArgumentException;
import software.amazon.kinesis.multilang.config.KinesisClientLibConfigurator; import software.amazon.kinesis.multilang.config.KinesisClientLibConfigurator;
@RunWith(MockitoJUnitRunner.class) @RunWith(MockitoJUnitRunner.class)
@ -58,6 +58,9 @@ public class MultiLangDaemonConfigTest {
@Mock @Mock
private KinesisClientLibConfigurator configurator; private KinesisClientLibConfigurator configurator;
@Rule
public final ExpectedException thrown = ExpectedException.none();
public void setup(String streamName, String streamArn) { public void setup(String streamName, String streamArn) {
String PROPERTIES = String.format("executableName = %s\n" String PROPERTIES = String.format("executableName = %s\n"
@ -87,59 +90,51 @@ public class MultiLangDaemonConfigTest {
} }
@Test @Test
public void testConstructorFailsBecauseStreamArnIsInvalid() throws IOException { public void testConstructorFailsBecauseStreamArnIsInvalid() throws Exception {
setup("", "this_is_not_a_valid_arn"); setup("", "this_is_not_a_valid_arn");
assertConstructorThrowsException(IllegalArgumentException.class, "Malformed ARN - doesn't start with 'arn:");
assertThrows(Exception.class, () -> new MultiLangDaemonConfig(FILENAME, classLoader, configurator));
} }
@Test @Test
public void testConstructorFailsBecauseStreamArnHasInvalidRegion() throws IOException { public void testConstructorFailsBecauseStreamArnHasInvalidRegion() throws Exception {
setup("", "arn:aws:kinesis:us-east-1000:ACCOUNT_ID:stream/streamName"); setup("", "arn:aws:kinesis:us-east-1000:ACCOUNT_ID:stream/streamName");
assertConstructorThrowsException(IllegalArgumentException.class, "us-east-1000 is not a valid region");
assertThrows(IllegalArgumentException.class, () -> new MultiLangDaemonConfig(FILENAME, classLoader, configurator));
} }
@Test @Test
public void testConstructorFailsBecauseStreamArnHasInvalidResourceType() throws IOException { public void testConstructorFailsBecauseStreamArnHasInvalidResourceType() throws Exception {
setup("", "arn:aws:kinesis:us-EAST-1:ACCOUNT_ID:dynamodb/streamName"); setup("", "arn:aws:kinesis:us-EAST-1:ACCOUNT_ID:dynamodb/streamName");
assertConstructorThrowsException(IllegalArgumentException.class, "StreamArn has unsupported resource type of 'dynamodb'. Expected: stream");
assertThrows(IllegalArgumentException.class, () -> new MultiLangDaemonConfig(FILENAME, classLoader, configurator));
} }
@Test @Test
public void testConstructorFailsBecauseStreamArnHasInvalidService() throws IOException { public void testConstructorFailsBecauseStreamArnHasInvalidService() throws Exception {
setup("", "arn:aws:kinesisFakeService:us-east-1:ACCOUNT_ID:stream/streamName"); setup("", "arn:aws:kinesisFakeService:us-east-1:ACCOUNT_ID:stream/streamName");
assertConstructorThrowsException(IllegalArgumentException.class, "StreamArn has unsupported service type of 'kinesisFakeService'. Expected: kinesis");
assertThrows(IllegalArgumentException.class, () -> new MultiLangDaemonConfig(FILENAME, classLoader, configurator));
} }
@Test @Test
public void testConstructorFailsBecauseStreamNameAndArnAreEmpty() throws IOException { public void testConstructorFailsBecauseStreamNameAndArnAreEmpty() throws Exception {
setup("", ""); setup("", "");
assertConstructorThrowsException(IllegalArgumentException.class, "Stream name or Stream Arn is required. Stream Arn takes precedence if both are passed in.");
assertThrows(Exception.class, () -> new MultiLangDaemonConfig(FILENAME, classLoader, configurator));
} }
@Test @Test
public void testConstructorFailsBecauseStreamNameAndArnAreNull() { public void testConstructorFailsBecauseStreamNameAndArnAreNull() throws Exception {
setup(null, null); setup(null, null);
assertConstructorThrowsException(NullPointerException.class, "Stream name or Stream Arn is required. Stream Arn takes precedence if both are passed in.");
assertThrows(Exception.class, () -> new MultiLangDaemonConfig(FILENAME, classLoader, configurator));
} }
@Test @Test
public void testConstructorFailsBecauseStreamNameIsNullAndArnIsEmpty() { public void testConstructorFailsBecauseStreamNameIsNullAndArnIsEmpty() throws Exception {
setup(null, ""); setup(null, "");
assertConstructorThrowsException(NullPointerException.class, "Stream name or Stream Arn is required. Stream Arn takes precedence if both are passed in.");
assertThrows(Exception.class, () -> new MultiLangDaemonConfig(FILENAME, classLoader, configurator));
} }
@Test @Test
public void testConstructorFailsBecauseStreamNameIsEmptyAndArnIsNull() { public void testConstructorFailsBecauseStreamNameIsEmptyAndArnIsNull() throws Exception {
setup("", null); setup("", null);
assertConstructorThrowsException(IllegalArgumentException.class, "Stream name or Stream Arn is required. Stream Arn takes precedence if both are passed in.");
assertThrows(Exception.class, () -> new MultiLangDaemonConfig(FILENAME, classLoader, configurator));
} }
@Test @Test
@ -220,6 +215,16 @@ public class MultiLangDaemonConfigTest {
assertEquals(expectedStreamArn, deamonConfig.getMultiLangDaemonConfiguration().getStreamArn()); assertEquals(expectedStreamArn, deamonConfig.getMultiLangDaemonConfiguration().getStreamArn());
} }
private void assertConstructorThrowsException(Class<? extends Exception> exceptionClass, String exceptionMessage) throws Exception{
thrown.expect(exceptionClass);
if(exceptionMessage != null) {
thrown.expectMessage(exceptionMessage);
}
new MultiLangDaemonConfig(FILENAME, classLoader, configurator);
}
@Test @Test
public void testPropertyValidation() { public void testPropertyValidation() {
String PROPERTIES_NO_EXECUTABLE_NAME = "applicationName = testApp \n" + "streamName = fakeStream \n" String PROPERTIES_NO_EXECUTABLE_NAME = "applicationName = testApp \n" + "streamName = fakeStream \n"