Skip to content

Commit

Permalink
fixup! Enables property for setting AT_TIMESTAMP shard iterator initi…
Browse files Browse the repository at this point in the history
…al timestamp (awslabs#341)
  • Loading branch information
marcinc committed May 25, 2018
1 parent 46d9d7e commit 1455f95
Show file tree
Hide file tree
Showing 3 changed files with 132 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2014 Amazon.com, Inc. or its affiliates. All Rights Reserved.
* Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Amazon Software License (the "License").
* You may not use this file except in compliance with the License.
Expand Down Expand Up @@ -35,7 +35,11 @@ class DatePropertyValueDecoder implements IPropertyValueDecoder<Date> {
*/
@Override
public Date decodeValue(String value) {
return new Date(Long.parseLong(value) * 1000L);
try {
return new Date(Long.parseLong(value) * 1000L);
} catch (NumberFormatException e) {
throw new IllegalArgumentException("Date property value must be numeric.");
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Amazon Software License (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://aws.amazon.com/asl/
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/
package com.amazonaws.services.kinesis.clientlibrary.config;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;

import java.util.Date;

import org.junit.Test;

import com.amazonaws.services.kinesis.clientlibrary.config.DatePropertyValueDecoder;

public class DatePropertyValueDecoderTest {

private DatePropertyValueDecoder decoder = new DatePropertyValueDecoder();

private static final String TEST_VALUE = "1527267472";

@Test
public void testNumericValue() {
Date timestamp = decoder.decodeValue(TEST_VALUE);
assertEquals(timestamp.getClass(), Date.class);
assertEquals(timestamp, new Date(Long.parseLong(TEST_VALUE) * 1000L));
}

@Test
public void testEmptyValue() {
try {
Date timestamp = decoder.decodeValue("");
fail("Expect IllegalArgumentException on empty value");
} catch (IllegalArgumentException e) {
// success
}
}

@Test
public void testNullValue() {
try {
Date timestamp = decoder.decodeValue(null);
fail("Expect IllegalArgumentException on null value");
} catch (IllegalArgumentException e) {
// success
}
}

@Test
public void testNonNumericValue() {
try {
Date timestamp = decoder.decodeValue("123abc");
fail("Expect IllegalArgumentException on non numeric value");
} catch (IllegalArgumentException e) {
// success
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.Date;
import java.util.Optional;
import java.util.Set;

Expand Down Expand Up @@ -143,6 +144,20 @@ public void testWithBooleanVariables() {
assertTrue(config.shouldValidateSequenceNumberBeforeCheckpointing());
}

@Test
public void testWithDateVariables() {
KinesisClientLibConfiguration config =
getConfiguration(StringUtils.join(new String[] {
"streamName = a",
"applicationName = b",
"AWSCredentialsProvider = ABCD, " + credentialName1,
"timestampAtInitialPositionInStream = 1527267472"
}, '\n'));

assertEquals(config.getTimestampAtInitialPositionInStream(),
new Date(1527267472 * 1000L));
}

@Test
public void testWithStringVariables() {
KinesisClientLibConfiguration config =
Expand Down Expand Up @@ -189,6 +204,49 @@ public void testWithInitialPositionInStreamVariables() {
}, '\n'));

assertEquals(config.getInitialPositionInStream(), InitialPositionInStream.TRIM_HORIZON);
}

@Test
public void testWithTimestampAtInitialPositionInStreamVariables() {
KinesisClientLibConfiguration config =
getConfiguration(StringUtils.join(new String[] {
"streamName = a",
"applicationName = b",
"AWSCredentialsProvider = ABCD," + credentialName1,
"timestampAtInitialPositionInStream = 1527267472"
}, '\n'));

assertEquals(config.getInitialPositionInStream(), InitialPositionInStream.AT_TIMESTAMP);
assertEquals(config.getTimestampAtInitialPositionInStream(),
new Date(1527267472 * 1000L));
}

@Test
public void testWithEmptyTimestampAtInitialPositionInStreamVariables() {
KinesisClientLibConfiguration config =
getConfiguration(StringUtils.join(new String[] {
"streamName = a",
"applicationName = b",
"AWSCredentialsProvider = ABCD," + credentialName1,
"timestampAtInitialPositionInStream = "
}, '\n'));

assertEquals(config.getInitialPositionInStream(), InitialPositionInStream.LATEST);
assertEquals(config.getTimestampAtInitialPositionInStream(), null);
}

@Test
public void testWithNonNumericTimestampAtInitialPositionInStreamVariables() {
KinesisClientLibConfiguration config =
getConfiguration(StringUtils.join(new String[] {
"streamName = a",
"applicationName = b",
"AWSCredentialsProvider = ABCD," + credentialName1,
"timestampAtInitialPositionInStream = 123abc"
}, '\n'));

assertEquals(config.getInitialPositionInStream(), InitialPositionInStream.LATEST);
assertEquals(config.getTimestampAtInitialPositionInStream(), null);
}

@Test
Expand Down

0 comments on commit 1455f95

Please sign in to comment.