@Test public final void testSequenceNumberValidator() { IKinesisProxy proxy = Mockito.mock(IKinesisProxy.class); SequenceNumberValidator validator = new SequenceNumberValidator(proxy, shardId, validateWithGetIterator); String goodSequence = "456"; String iterator = "happyiterator"; String badSequence = "789"; Mockito.doReturn(iterator) .when(proxy) .getIterator(shardId, ShardIteratorType.AFTER_SEQUENCE_NUMBER.toString(), goodSequence); Mockito.doThrow(new InvalidArgumentException("")) .when(proxy) .getIterator(shardId, ShardIteratorType.AFTER_SEQUENCE_NUMBER.toString(), badSequence); validator.validateSequenceNumber(goodSequence); Mockito.verify(proxy, Mockito.times(1)) .getIterator(shardId, ShardIteratorType.AFTER_SEQUENCE_NUMBER.toString(), goodSequence); try { validator.validateSequenceNumber(badSequence); fail("Bad sequence number did not cause the validator to throw an exception"); } catch (IllegalArgumentException e) { Mockito.verify(proxy, Mockito.times(1)) .getIterator(shardId, ShardIteratorType.AFTER_SEQUENCE_NUMBER.toString(), badSequence); } nonNumericValueValidationTest(validator, proxy, validateWithGetIterator); }
private void nonNumericValueValidationTest( SequenceNumberValidator validator, IKinesisProxy proxy, boolean validateWithGetIterator) { String[] nonNumericStrings = { null, "bogus-sequence-number", SentinelCheckpoint.LATEST.toString(), SentinelCheckpoint.SHARD_END.toString(), SentinelCheckpoint.TRIM_HORIZON.toString() }; for (String nonNumericString : nonNumericStrings) { try { validator.validateSequenceNumber(nonNumericString); fail("Validator should not consider " + nonNumericString + " a valid sequence number"); } catch (IllegalArgumentException e) { // Non-numeric strings should always be rejected by the validator before the proxy can be // called so we // check that the proxy was not called at all Mockito.verify(proxy, Mockito.times(0)) .getIterator( shardId, ShardIteratorType.AFTER_SEQUENCE_NUMBER.toString(), nonNumericString); } } }
@Test public final void testNoValidation() { IKinesisProxy proxy = Mockito.mock(IKinesisProxy.class); String shardId = "shardid-123"; SequenceNumberValidator validator = new SequenceNumberValidator(proxy, shardId, !validateWithGetIterator); String goodSequence = "456"; // Just checking that the false flag for validating against getIterator is honored validator.validateSequenceNumber(goodSequence); Mockito.verify(proxy, Mockito.times(0)) .getIterator(shardId, ShardIteratorType.AFTER_SEQUENCE_NUMBER.toString(), goodSequence); // Validator should still validate sentinel values nonNumericValueValidationTest(validator, proxy, !validateWithGetIterator); }
/** * Get the records from the particular shard * * @param streamName Name of the stream from where the records to be accessed * @param recordsLimit Number of records to return from shard * @param shId Shard Id of the shard * @param iteratorType Shard iterator type * @param seqNo Record sequence number * @return the list of records from the given shard * @throws AmazonClientException */ public List<Record> getRecords( String streamName, Integer recordsLimit, String shId, ShardIteratorType iteratorType, String seqNo) throws AmazonClientException { assert client != null : "Illegal client"; try { // Create the GetShardIteratorRequest instance and sets streamName, shardId and iteratorType // to it GetShardIteratorRequest iteratorRequest = new GetShardIteratorRequest(); iteratorRequest.setStreamName(streamName); iteratorRequest.setShardId(shId); iteratorRequest.setShardIteratorType(iteratorType); // If the iteratorType is AFTER_SEQUENCE_NUMBER, set the sequence No to the iteratorRequest if (ShardIteratorType.AFTER_SEQUENCE_NUMBER.equals(iteratorType) || ShardIteratorType.AT_SEQUENCE_NUMBER.equals(iteratorType)) iteratorRequest.setStartingSequenceNumber(seqNo); // Get the Response from the getShardIterator service method & get the shardIterator from that // response GetShardIteratorResult iteratorResponse = client.getShardIterator(iteratorRequest); // getShardIterator() specifies the position in the shard String iterator = iteratorResponse.getShardIterator(); // Create the GetRecordsRequest instance and set the recordsLimit and iterator GetRecordsRequest getRequest = new GetRecordsRequest(); getRequest.setLimit(recordsLimit); getRequest.setShardIterator(iterator); // Get the Response from the getRecords service method and get the data records from that // response. GetRecordsResult getResponse = client.getRecords(getRequest); return getResponse.getRecords(); } catch (AmazonClientException e) { throw new RuntimeException(e); } }