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.util.Properties;
import com.amazonaws.arn.Arn;
import org.apache.commons.beanutils.BeanUtilsBean;
import org.apache.commons.beanutils.ConvertUtilsBean;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.Validate;
import software.amazon.awssdk.arns.Arn;
import software.amazon.awssdk.regions.Region;
/**
@ -74,20 +73,20 @@ public class KinesisClientLibConfigurator {
if (configuration.getStreamArn() != null && !configuration.getStreamArn().trim().isEmpty()) {
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",
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",
streamArnObj.getResource().getResourceType()));
streamArnObj.service()));
}
//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);
//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){
throw new IllegalArgumentException(String.format("%s is not a valid region", regionObj.id()));
}

View file

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