com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBScanExpression Java Examples
The following examples show how to use
com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBScanExpression.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: DBUtil.java From chatbot with Apache License 2.0 | 7 votes |
public static StarWarsCharacter getCharacter(String name) { System.out.println("Name: " + name); DynamoDBMapper mapper = new DynamoDBMapper(getClient()); Map<String, AttributeValue> eav = new HashMap<>(); eav.put(":name", new AttributeValue().withS(name.toLowerCase())); DynamoDBScanExpression scanExpression = new DynamoDBScanExpression() .withFilterExpression("whoami = :name") .withExpressionAttributeValues(eav); List<StarWarsCharacter> list = mapper.scan(StarWarsCharacter.class, scanExpression); if (!list.isEmpty()) { return list.get(0); } return null; }
Example #2
Source File: DynamoDBEntityWithHashAndRangeKeyCriteria.java From spring-data-dynamodb with Apache License 2.0 | 6 votes |
public DynamoDBScanExpression buildScanExpression() { if (sort != null) { throw new UnsupportedOperationException("Sort not supported for scan expressions"); } DynamoDBScanExpression scanExpression = new DynamoDBScanExpression(); if (isHashKeySpecified()) { scanExpression.addFilterCondition( getHashKeyAttributeName(), createSingleValueCondition(getHashKeyPropertyName(), ComparisonOperator.EQ, getHashKeyAttributeValue(), getHashKeyAttributeValue().getClass(), true)); } if (isRangeKeySpecified()) { scanExpression.addFilterCondition( getRangeKeyAttributeName(), createSingleValueCondition(getRangeKeyPropertyName(), ComparisonOperator.EQ, getRangeKeyAttributeValue(), getRangeKeyAttributeValue().getClass(), true)); } for (Map.Entry<String, List<Condition>> conditionEntry : attributeConditions.entrySet()) { for (Condition condition : conditionEntry.getValue()) { scanExpression.addFilterCondition(conditionEntry.getKey(), condition); } } return scanExpression; }
Example #3
Source File: DynamoDBEntityWithHashKeyOnlyCriteria.java From spring-data-dynamodb with Apache License 2.0 | 6 votes |
public DynamoDBScanExpression buildScanExpression() { if (sort != null) { throw new UnsupportedOperationException("Sort not supported for scan expressions"); } DynamoDBScanExpression scanExpression = new DynamoDBScanExpression(); if (isHashKeySpecified()) { scanExpression.addFilterCondition( getHashKeyAttributeName(), createSingleValueCondition(getHashKeyPropertyName(), ComparisonOperator.EQ, getHashKeyAttributeValue(), getHashKeyAttributeValue().getClass(), true)); } for (Map.Entry<String, List<Condition>> conditionEntry : attributeConditions.entrySet()) { for (Condition condition : conditionEntry.getValue()) { scanExpression.addFilterCondition(conditionEntry.getKey(), condition); } } return scanExpression; }
Example #4
Source File: DynamoDBTemplate.java From spring-data-dynamodb with Apache License 2.0 | 6 votes |
@Override public <T> PaginatedScanList<T> scan(Class<T> domainClass, DynamoDBScanExpression scanExpression) { PaginatedScanList<T> results = dynamoDBMapper.scan(domainClass, scanExpression); maybeEmitEvent(new AfterScanEvent<T>(results)); return results; }
Example #5
Source File: MapperLoadingStrategyConfigITCase.java From aws-dynamodb-encryption-java with Apache License 2.0 | 6 votes |
private static PaginatedList<RangeKeyTestClass> getTestPaginatedParallelScanList(PaginationLoadingStrategy paginationLoadingStrategy) { DynamoDBMapperConfig mapperConfig = new DynamoDBMapperConfig(ConsistentReads.CONSISTENT); DynamoDBMapper mapper = new DynamoDBMapper(dynamo, mapperConfig); // Construct the scan expression with the exact same conditions DynamoDBScanExpression scanExpression = new DynamoDBScanExpression(); scanExpression.addFilterCondition("key", new Condition().withComparisonOperator(ComparisonOperator.EQ).withAttributeValueList( new AttributeValue().withN(Long.toString(hashKey)))); scanExpression.addFilterCondition("rangeKey", new Condition().withComparisonOperator(ComparisonOperator.GT).withAttributeValueList( new AttributeValue().withN("1.0"))); scanExpression.setLimit(PAGE_SIZE); return mapper.parallelScan(RangeKeyTestClass.class, scanExpression, PARALLEL_SEGMENT, new DynamoDBMapperConfig(paginationLoadingStrategy)); }
Example #6
Source File: MapperLoadingStrategyConfigITCase.java From aws-dynamodb-encryption-java with Apache License 2.0 | 6 votes |
private static PaginatedList<RangeKeyTestClass> getTestPaginatedScanList(PaginationLoadingStrategy paginationLoadingStrategy) { DynamoDBMapperConfig mapperConfig = new DynamoDBMapperConfig(ConsistentReads.CONSISTENT); DynamoDBMapper mapper = new DynamoDBMapper(dynamo, mapperConfig); // Construct the scan expression with the exact same conditions DynamoDBScanExpression scanExpression = new DynamoDBScanExpression(); scanExpression.addFilterCondition("key", new Condition().withComparisonOperator(ComparisonOperator.EQ).withAttributeValueList( new AttributeValue().withN(Long.toString(hashKey)))); scanExpression.addFilterCondition("rangeKey", new Condition().withComparisonOperator(ComparisonOperator.GT).withAttributeValueList( new AttributeValue().withN("1.0"))); scanExpression.setLimit(PAGE_SIZE); return mapper.scan(RangeKeyTestClass.class, scanExpression, new DynamoDBMapperConfig(paginationLoadingStrategy)); }
Example #7
Source File: ScanITCase.java From aws-dynamodb-encryption-java with Apache License 2.0 | 6 votes |
@Test(enabled = false) public void testParallelScanExceptionHandling() { DynamoDBMapper util = TestDynamoDBMapperFactory.createDynamoDBMapper(dynamo); int INVALID_LIMIT = 0; DynamoDBScanExpression scanExpression = new DynamoDBScanExpression().withLimit(INVALID_LIMIT); try { PaginatedParallelScanList<SimpleClass> parallelScanList = util.parallelScan(SimpleClass.class, scanExpression, PARALLEL_SCAN_SEGMENTS); fail("Should have seen the AmazonServiceException"); } catch (AmazonServiceException ase) { assertNotNull(ase.getErrorCode()); assertNotNull(ase.getErrorType()); assertNotNull(ase.getMessage()); } catch (Exception e) { fail("Should have seen the AmazonServiceException"); } }
Example #8
Source File: ScanITCase.java From aws-dynamodb-encryption-java with Apache License 2.0 | 6 votes |
@Test(enabled = false) public void testParallelScanPerformance() throws Exception{ DynamoDBMapper util = TestDynamoDBMapperFactory.createDynamoDBMapper(dynamo); DynamoDBScanExpression scanExpression = new DynamoDBScanExpression().withLimit(SCAN_LIMIT); scanExpression.addFilterCondition("value", new Condition().withComparisonOperator(ComparisonOperator.NOT_NULL.toString())); scanExpression.addFilterCondition("extraData", new Condition().withComparisonOperator(ComparisonOperator.NOT_NULL.toString())); long startTime = System.currentTimeMillis(); PaginatedScanList<SimpleClass> scanList = util.scan(SimpleClass.class, scanExpression); scanList.loadAllResults(); long fullTableScanTime = System.currentTimeMillis() - startTime; startTime = System.currentTimeMillis(); PaginatedParallelScanList<SimpleClass> parallelScanList = util.parallelScan(SimpleClass.class, scanExpression, PARALLEL_SCAN_SEGMENTS); parallelScanList.loadAllResults(); long parallelScanTime = System.currentTimeMillis() - startTime; assertTrue(scanList.size() == parallelScanList.size()); assertTrue(fullTableScanTime > parallelScanTime); System.out.println("fullTableScanTime : " + fullTableScanTime + "(ms), parallelScanTime : " + parallelScanTime + "(ms)."); }
Example #9
Source File: ScanITCase.java From aws-dynamodb-encryption-java with Apache License 2.0 | 6 votes |
/** * Tests scanning the table with AND/OR logic operator. */ @Test public void testScanWithConditionalOperator() { DynamoDBMapper mapper = TestDynamoDBMapperFactory.createDynamoDBMapper(dynamo); DynamoDBScanExpression scanExpression = new DynamoDBScanExpression() .withLimit(SCAN_LIMIT) .withScanFilter(ImmutableMapParameter.of( "value", new Condition().withComparisonOperator(ComparisonOperator.NOT_NULL), "non-existent-field", new Condition().withComparisonOperator(ComparisonOperator.NOT_NULL) )) .withConditionalOperator(ConditionalOperator.AND); List<SimpleClass> andConditionResult = mapper.scan(SimpleClass.class, scanExpression); assertTrue(andConditionResult.isEmpty()); List<SimpleClass> orConditionResult = mapper.scan(SimpleClass.class, scanExpression.withConditionalOperator(ConditionalOperator.OR)); assertFalse(orConditionResult.isEmpty()); }
Example #10
Source File: PartTreeDynamoDBQueryUnitTest.java From spring-data-dynamodb with Apache License 2.0 | 6 votes |
@SuppressWarnings("unchecked") @Test(expected=IllegalArgumentException.class) public void testExecute_WhenFinderMethodIsCountingEntityWithCompositeIdList_WhenFindingByRangeKeyOnly_ScanCountDisabled() { setupCommonMocksForThisRepositoryMethod(mockPlaylistEntityMetadata, mockDynamoDBPlaylistQueryMethod, Playlist.class, "countByPlaylistName", 1, "userName", "playlistName"); Mockito.when(mockDynamoDBPlaylistQueryMethod.isCollectionQuery()).thenReturn(false); Mockito.when(mockDynamoDBPlaylistQueryMethod.isScanCountEnabled()).thenReturn(false); // Mock out specific DynamoDBOperations behavior expected by this method ArgumentCaptor<DynamoDBScanExpression> scanCaptor = ArgumentCaptor.forClass(DynamoDBScanExpression.class); @SuppressWarnings("rawtypes") ArgumentCaptor<Class> classCaptor = ArgumentCaptor.forClass(Class.class); Mockito.when(mockDynamoDBOperations.count(classCaptor.capture(), scanCaptor.capture())).thenReturn( 100); // Execute the query Object[] parameters = new Object[] { "somePlaylistName" }; partTreeDynamoDBQuery.execute(parameters); }
Example #11
Source File: PartTreeDynamoDBQueryUnitTest.java From spring-data-dynamodb with Apache License 2.0 | 6 votes |
@SuppressWarnings({ "unchecked", "rawtypes" }) @Test(expected = UnsupportedOperationException.class) // Not yet supported public void testExecute_WhenFinderMethodIsFindingEntityList_WithSingleStringParameterIgnoringCase_WhenNotFindingByHashKey() { setupCommonMocksForThisRepositoryMethod(mockUserEntityMetadata, mockDynamoDBUserQueryMethod, User.class, "findByNameIgnoringCase", 1, "id", null); Mockito.when(mockDynamoDBUserQueryMethod.isCollectionQuery()).thenReturn(true); // Mock out specific DynamoDBOperations behavior expected by this method ArgumentCaptor<DynamoDBScanExpression> scanCaptor = ArgumentCaptor.forClass(DynamoDBScanExpression.class); ArgumentCaptor<Class> classCaptor = ArgumentCaptor.forClass(Class.class); Mockito.when(mockUserScanResults.get(0)).thenReturn(mockUser); Mockito.when(mockUserScanResults.size()).thenReturn(1); Mockito.when(mockDynamoDBOperations.scan(classCaptor.capture(), scanCaptor.capture())).thenReturn( mockUserScanResults); // Execute the query Object[] parameters = new Object[] { "someName" }; partTreeDynamoDBQuery.execute(parameters); }
Example #12
Source File: AbstractDAO.java From nfscan with MIT License | 6 votes |
/** * Find all rows in table given the entity object * * @return a list of entities found * @throws DataAccessException */ public List<T> findAll() throws DataAccessException { DynamoDBScanExpression dynamoDBScanExpression = new DynamoDBScanExpression(); DynamoDBMapperConfig config = new DynamoDBMapperConfig(DynamoDBMapperConfig.PaginationLoadingStrategy.EAGER_LOADING); PaginatedScanList<T> paginatedScanList = dynamoDBMapper.scan(getType(), dynamoDBScanExpression, config); paginatedScanList.loadAllResults(); List<T> list = new ArrayList<T>(paginatedScanList.size()); Iterator<T> iterator = paginatedScanList.iterator(); while (iterator.hasNext()) { T element = iterator.next(); list.add(element); } return list; }
Example #13
Source File: DynamoDBMapperQueryScanExample.java From aws-doc-sdk-examples with Apache License 2.0 | 6 votes |
private static void FindBicyclesOfSpecificTypeWithMultipleThreads(DynamoDBMapper mapper, int numberOfThreads, String bicycleType) throws Exception { System.out.println("FindBicyclesOfSpecificTypeWithMultipleThreads: Scan ProductCatalog With Multiple Threads."); Map<String, AttributeValue> eav = new HashMap<String, AttributeValue>(); eav.put(":val1", new AttributeValue().withS("Bicycle")); eav.put(":val2", new AttributeValue().withS(bicycleType)); DynamoDBScanExpression scanExpression = new DynamoDBScanExpression() .withFilterExpression("ProductCategory = :val1 and BicycleType = :val2").withExpressionAttributeValues(eav); List<Bicycle> scanResult = mapper.parallelScan(Bicycle.class, scanExpression, numberOfThreads); for (Bicycle bicycle : scanResult) { System.out.println(bicycle); } }
Example #14
Source File: DynamoDBMapperQueryScanExample.java From aws-doc-sdk-examples with Apache License 2.0 | 6 votes |
private static void FindBooksPricedLessThanSpecifiedValue(DynamoDBMapper mapper, String value) throws Exception { System.out.println("FindBooksPricedLessThanSpecifiedValue: Scan ProductCatalog."); Map<String, AttributeValue> eav = new HashMap<String, AttributeValue>(); eav.put(":val1", new AttributeValue().withN(value)); eav.put(":val2", new AttributeValue().withS("Book")); DynamoDBScanExpression scanExpression = new DynamoDBScanExpression() .withFilterExpression("Price < :val1 and ProductCategory = :val2").withExpressionAttributeValues(eav); List<Book> scanResult = mapper.scan(Book.class, scanExpression); for (Book book : scanResult) { System.out.println(book); } }
Example #15
Source File: PartTreeDynamoDBQueryUnitTest.java From spring-data-dynamodb with Apache License 2.0 | 6 votes |
@SuppressWarnings({ "unchecked", "rawtypes" }) @Test(expected = UnsupportedOperationException.class) // Not yet supported public void testExecute_WhenFinderMethodIsFindingEntityList_WithSingleStringParameter_WithSort_WhenNotFindingByHashKey() { setupCommonMocksForThisRepositoryMethod(mockUserEntityMetadata, mockDynamoDBUserQueryMethod, User.class, "findByNameOrderByNameAsc", 1, "id", null); Mockito.when(mockDynamoDBUserQueryMethod.isCollectionQuery()).thenReturn(true); // Mock out specific DynamoDBOperations behavior expected by this method ArgumentCaptor<DynamoDBScanExpression> scanCaptor = ArgumentCaptor.forClass(DynamoDBScanExpression.class); ArgumentCaptor<Class> classCaptor = ArgumentCaptor.forClass(Class.class); Mockito.when(mockUserScanResults.get(0)).thenReturn(mockUser); Mockito.when(mockUserScanResults.size()).thenReturn(1); Mockito.when(mockDynamoDBOperations.scan(classCaptor.capture(), scanCaptor.capture())).thenReturn( mockUserScanResults); // Execute the query Object[] parameters = new Object[] { "someName" }; partTreeDynamoDBQuery.execute(parameters); }
Example #16
Source File: ObjectPersistenceQueryScanExample.java From aws-dynamodb-examples with Apache License 2.0 | 6 votes |
private static void FindBooksPricedLessThanSpecifiedValue( DynamoDBMapper mapper, String value) throws Exception { System.out.println("FindBooksPricedLessThanSpecifiedValue: Scan ProductCatalog."); DynamoDBScanExpression scanExpression = new DynamoDBScanExpression(); scanExpression.addFilterCondition("Price", new Condition() .withComparisonOperator(ComparisonOperator.LT) .withAttributeValueList(new AttributeValue().withN(value))); scanExpression.addFilterCondition("ProductCategory", new Condition() .withComparisonOperator(ComparisonOperator.EQ) .withAttributeValueList(new AttributeValue().withS("Book"))); List<Book> scanResult = mapper.scan(Book.class, scanExpression); for (Book book : scanResult) { System.out.println(book); } }
Example #17
Source File: ObjectPersistenceQueryScanExample.java From aws-dynamodb-examples with Apache License 2.0 | 6 votes |
private static void FindBicyclesOfSpecificTypeWithMultipleThreads( DynamoDBMapper mapper, int numberOfThreads, String bicycleType) throws Exception { System.out.println("FindBicyclesOfSpecificTypeWithMultipleThreads: Scan ProductCatalog With Multiple Threads."); DynamoDBScanExpression scanExpression = new DynamoDBScanExpression(); scanExpression.addFilterCondition("ProductCategory", new Condition() .withComparisonOperator(ComparisonOperator.EQ) .withAttributeValueList(new AttributeValue().withS("Bicycle"))); scanExpression.addFilterCondition("BicycleType", new Condition() .withComparisonOperator(ComparisonOperator.EQ) .withAttributeValueList(new AttributeValue().withS(bicycleType))); List<Bicycle> scanResult = mapper.parallelScan(Bicycle.class, scanExpression, numberOfThreads); for (Bicycle bicycle : scanResult) { System.out.println(bicycle); } }
Example #18
Source File: SimpleDynamoDBCrudRepository.java From spring-data-dynamodb with Apache License 2.0 | 5 votes |
@Override public long count() { assertScanEnabled( enableScanPermissions.isCountUnpaginatedScanEnabled(), "count"); final DynamoDBScanExpression scanExpression = new DynamoDBScanExpression(); return dynamoDBOperations.count(domainType, scanExpression); }
Example #19
Source File: SimpleDynamoDBCrudRepository.java From spring-data-dynamodb with Apache License 2.0 | 5 votes |
@Override public List<T> findAll() { assertScanEnabled( enableScanPermissions.isFindAllUnpaginatedScanEnabled(), "findAll"); DynamoDBScanExpression scanExpression = new DynamoDBScanExpression(); return dynamoDBOperations.scan(domainType, scanExpression); }
Example #20
Source File: PartTreeDynamoDBQueryUnitTest.java From spring-data-dynamodb with Apache License 2.0 | 5 votes |
@SuppressWarnings({ "unchecked", "rawtypes" }) @Test(expected=UnsupportedOperationException.class) public void testExecute_WhenFinderMethodIsFindingEntityWithCompositeIdList_WhenFindingByNotCompositeId() { PlaylistId playlistId = new PlaylistId(); playlistId.setUserName("someUserName"); playlistId.setPlaylistName("somePlaylistName"); setupCommonMocksForThisRepositoryMethod(mockPlaylistEntityMetadata, mockDynamoDBPlaylistQueryMethod, Playlist.class, "findByPlaylistIdNot", 1, "userName", "playlistName"); Mockito.when(mockDynamoDBPlaylistQueryMethod.isCollectionQuery()).thenReturn(true); Mockito.when(mockPlaylistEntityMetadata.getHashKey(playlistId)).thenReturn("someUserName"); Mockito.when(mockPlaylistEntityMetadata.getRangeKey(playlistId)).thenReturn("somePlaylistName"); // Mock out specific DynamoDBOperations behavior expected by this method ArgumentCaptor<DynamoDBScanExpression> scanCaptor = ArgumentCaptor.forClass(DynamoDBScanExpression.class); ArgumentCaptor<Class> classCaptor = ArgumentCaptor.forClass(Class.class); Mockito.when(mockPlaylistScanResults.get(0)).thenReturn(mockPlaylist); Mockito.when(mockPlaylistScanResults.size()).thenReturn(1); Mockito.when(mockDynamoDBOperations.scan(classCaptor.capture(), scanCaptor.capture())).thenReturn( mockPlaylistScanResults); // Execute the query Object[] parameters = new Object[] { playlistId }; partTreeDynamoDBQuery.execute(parameters); }
Example #21
Source File: SimpleDynamoDBPagingAndSortingRepository.java From spring-data-dynamodb with Apache License 2.0 | 5 votes |
@Override public Page<T> findAll(Pageable pageable) { if (pageable.getSort() != null) { throw new UnsupportedOperationException("Sorting not supported for find all scan operations"); } DynamoDBScanExpression scanExpression = new DynamoDBScanExpression(); // Scan to the end of the page after the requested page int scanTo = pageable.getOffset() + (2 * pageable.getPageSize()); scanExpression.setLimit(scanTo); PaginatedScanList<T> paginatedScanList = dynamoDBOperations.scan(domainType, scanExpression); Iterator<T> iterator = paginatedScanList.iterator(); int processedCount = 0; if (pageable.getOffset() > 0) { processedCount = scanThroughResults(iterator, pageable.getOffset()); if (processedCount < pageable.getOffset()) return new PageImpl<T>(new ArrayList<T>()); } // Scan ahead to retrieve the next page count List<T> results = readPageOfResults(iterator, pageable.getPageSize()); assertScanEnabled(enableScanPermissions.isFindAllPaginatedScanEnabled(), "findAll(Pageable pageable)"); assertScanCountEnabled(enableScanPermissions.isFindAllUnpaginatedScanCountEnabled(), "findAll(Pageable pageable)"); int totalCount = dynamoDBOperations.count(domainType, scanExpression); return new PageImpl<T>(results, pageable, totalCount); }
Example #22
Source File: ScanITCase.java From aws-dynamodb-encryption-java with Apache License 2.0 | 5 votes |
@Test public void testParallelScan() throws Exception { DynamoDBMapper util = TestDynamoDBMapperFactory.createDynamoDBMapper(dynamo); DynamoDBScanExpression scanExpression = new DynamoDBScanExpression().withLimit(SCAN_LIMIT); scanExpression.addFilterCondition("value", new Condition().withComparisonOperator(ComparisonOperator.NOT_NULL.toString())); scanExpression.addFilterCondition("extraData", new Condition().withComparisonOperator(ComparisonOperator.NOT_NULL.toString())); PaginatedParallelScanList<SimpleClass> parallelScanList = util.parallelScan(SimpleClass.class, scanExpression, PARALLEL_SCAN_SEGMENTS); int count = 0; Iterator<SimpleClass> iterator = parallelScanList.iterator(); HashMap<String, Boolean> allDataAppearance = new HashMap<String, Boolean>(); for (int i=0;i<500;i++) { allDataAppearance.put("" + i, false); } while (iterator.hasNext()) { count++; SimpleClass next = iterator.next(); assertNotNull(next.getExtraData()); assertNotNull(next.getValue()); allDataAppearance.put(next.getId(), true); } assertFalse(allDataAppearance.values().contains(false)); int totalCount = util.count(SimpleClass.class, scanExpression); assertNotNull(parallelScanList.get(totalCount / 2)); assertTrue(totalCount == count); assertTrue(totalCount == parallelScanList.size()); assertTrue(parallelScanList.contains(parallelScanList.get(parallelScanList.size() / 2))); assertTrue(count == parallelScanList.toArray().length); }
Example #23
Source File: ScanITCase.java From aws-dynamodb-encryption-java with Apache License 2.0 | 5 votes |
@Test public void testScan() throws Exception { DynamoDBMapper util = TestDynamoDBMapperFactory.createDynamoDBMapper(dynamo); DynamoDBScanExpression scanExpression = new DynamoDBScanExpression().withLimit(SCAN_LIMIT); scanExpression.addFilterCondition("value", new Condition().withComparisonOperator(ComparisonOperator.NOT_NULL.toString())); scanExpression.addFilterCondition("extraData", new Condition().withComparisonOperator(ComparisonOperator.NOT_NULL.toString())); List<SimpleClass> list = util.scan(SimpleClass.class, scanExpression); int count = 0; Iterator<SimpleClass> iterator = list.iterator(); while (iterator.hasNext()) { count++; SimpleClass next = iterator.next(); assertNotNull(next.getExtraData()); assertNotNull(next.getValue()); } int totalCount = util.count(SimpleClass.class, scanExpression); assertNotNull(list.get(totalCount / 2)); assertTrue(totalCount == count); assertTrue(totalCount == list.size()); assertTrue(list.contains(list.get(list.size() / 2))); assertTrue(count == list.toArray().length); }
Example #24
Source File: PartTreeDynamoDBQueryUnitTest.java From spring-data-dynamodb with Apache License 2.0 | 4 votes |
@SuppressWarnings({ "unchecked", "rawtypes" }) @Test public void testExecute_WhenFinderMethodIsFindingEntityList_WithSingleDateParameter_WhenNotFindingByHashKey() throws ParseException { String joinDateString = "2013-09-12T14:04:03.123Z"; DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"); dateFormat.setTimeZone(TimeZone.getTimeZone("UTC")); Date joinDate = dateFormat.parse(joinDateString); setupCommonMocksForThisRepositoryMethod(mockUserEntityMetadata, mockDynamoDBUserQueryMethod, User.class, "findByJoinDate", 1, "id", null); Mockito.when(mockDynamoDBUserQueryMethod.isCollectionQuery()).thenReturn(true); // Mock out specific DynamoDBOperations behavior expected by this method ArgumentCaptor<DynamoDBScanExpression> scanCaptor = ArgumentCaptor.forClass(DynamoDBScanExpression.class); ArgumentCaptor<Class> classCaptor = ArgumentCaptor.forClass(Class.class); Mockito.when(mockUserScanResults.get(0)).thenReturn(mockUser); Mockito.when(mockUserScanResults.size()).thenReturn(1); Mockito.when(mockDynamoDBOperations.scan(classCaptor.capture(), scanCaptor.capture())).thenReturn( mockUserScanResults); // Execute the query Object[] parameters = new Object[] { joinDate }; Object o = partTreeDynamoDBQuery.execute(parameters); // Assert that we obtain the expected list of results assertEquals(o, mockUserScanResults); // Assert that the list of results contains the correct elements assertEquals(1, mockUserScanResults.size()); assertEquals(mockUser, mockUserScanResults.get(0)); // Assert that we scanned DynamoDB for the correct class assertEquals(classCaptor.getValue(), User.class); // Assert that we have only one filter condition, for the name of the // property Map<String, Condition> filterConditions = scanCaptor.getValue().getScanFilter(); assertEquals(1, filterConditions.size()); Condition filterCondition = filterConditions.get("joinDate"); assertNotNull(filterCondition); assertEquals(ComparisonOperator.EQ.name(), filterCondition.getComparisonOperator()); // Assert we only have one attribute value for this filter condition assertEquals(1, filterCondition.getAttributeValueList().size()); // Assert that there the attribute value type for this attribute value // is String, // and its value is the parameter expected assertEquals(joinDateString, filterCondition.getAttributeValueList().get(0).getS()); // Assert that all other attribute value types other than String type // are null assertNull(filterCondition.getAttributeValueList().get(0).getSS()); assertNull(filterCondition.getAttributeValueList().get(0).getN()); assertNull(filterCondition.getAttributeValueList().get(0).getNS()); assertNull(filterCondition.getAttributeValueList().get(0).getB()); assertNull(filterCondition.getAttributeValueList().get(0).getBS()); // Verify that the expected DynamoDBOperations method was called Mockito.verify(mockDynamoDBOperations).scan(classCaptor.getValue(), scanCaptor.getValue()); }
Example #25
Source File: PartTreeDynamoDBQueryUnitTest.java From spring-data-dynamodb with Apache License 2.0 | 4 votes |
@SuppressWarnings({ "unchecked", "rawtypes" }) @Test public void testExecute_WhenFinderMethodIsFindingSingleEntity_WithMultipleStringParameters_WhenFindingByHashKeyAndANonHashOrRangeProperty() { setupCommonMocksForThisRepositoryMethod(mockUserEntityMetadata, mockDynamoDBUserQueryMethod, User.class, "findByIdAndName", 2, "id", null); // Mock out specific DynamoDBOperations behavior expected by this method ArgumentCaptor<DynamoDBScanExpression> scanCaptor = ArgumentCaptor.forClass(DynamoDBScanExpression.class); ArgumentCaptor<Class> classCaptor = ArgumentCaptor.forClass(Class.class); Mockito.when(mockUserScanResults.get(0)).thenReturn(mockUser); Mockito.when(mockUserScanResults.size()).thenReturn(1); Mockito.when(mockDynamoDBOperations.scan(classCaptor.capture(), scanCaptor.capture())).thenReturn( mockUserScanResults); // Execute the query Object[] parameters = new Object[] { "someId", "someName" }; Object o = partTreeDynamoDBQuery.execute(parameters); // Assert that we obtain the expected single result assertEquals(o, mockUser); // Assert that we scanned DynamoDB for the correct class assertEquals(classCaptor.getValue(), User.class); // Assert that we have two filter conditions, for the id and name Map<String, Condition> filterConditions = scanCaptor.getValue().getScanFilter(); assertEquals(2, filterConditions.size()); Condition nameFilterCondition = filterConditions.get("name"); assertNotNull(nameFilterCondition); Condition idFilterCondition = filterConditions.get("id"); assertNotNull(idFilterCondition); assertEquals(ComparisonOperator.EQ.name(), nameFilterCondition.getComparisonOperator()); assertEquals(ComparisonOperator.EQ.name(), idFilterCondition.getComparisonOperator()); // Assert we only have one attribute value for each filter condition assertEquals(1, nameFilterCondition.getAttributeValueList().size()); assertEquals(1, idFilterCondition.getAttributeValueList().size()); // Assert that there the attribute value type for this attribute value // is String, // and its value is the parameter expected assertEquals("someName", nameFilterCondition.getAttributeValueList().get(0).getS()); assertEquals("someId", idFilterCondition.getAttributeValueList().get(0).getS()); // Assert that all other attribute value types other than String type // are null assertNull(nameFilterCondition.getAttributeValueList().get(0).getSS()); assertNull(nameFilterCondition.getAttributeValueList().get(0).getN()); assertNull(nameFilterCondition.getAttributeValueList().get(0).getNS()); assertNull(nameFilterCondition.getAttributeValueList().get(0).getB()); assertNull(nameFilterCondition.getAttributeValueList().get(0).getBS()); assertNull(idFilterCondition.getAttributeValueList().get(0).getSS()); assertNull(idFilterCondition.getAttributeValueList().get(0).getN()); assertNull(idFilterCondition.getAttributeValueList().get(0).getNS()); assertNull(idFilterCondition.getAttributeValueList().get(0).getB()); assertNull(idFilterCondition.getAttributeValueList().get(0).getBS()); // Verify that the expected DynamoDBOperations method was called Mockito.verify(mockDynamoDBOperations).scan(classCaptor.getValue(), scanCaptor.getValue()); }
Example #26
Source File: PartTreeDynamoDBQueryUnitTest.java From spring-data-dynamodb with Apache License 2.0 | 4 votes |
@SuppressWarnings({ "unchecked", "rawtypes" }) @Test public void testExecute_WhenFinderMethodIsFindingSingleEntity_WithMultipleStringParameters_WhenFindingByHashKeyAndACollectionProperty() { setupCommonMocksForThisRepositoryMethod(mockUserEntityMetadata, mockDynamoDBUserQueryMethod, User.class, "findByTestSet", 1, "id", null); Set<String> testSet = new HashSet<String>(); testSet.add("testData"); // Mock out specific DynamoDBOperations behavior expected by this method ArgumentCaptor<DynamoDBScanExpression> scanCaptor = ArgumentCaptor.forClass(DynamoDBScanExpression.class); ArgumentCaptor<Class> classCaptor = ArgumentCaptor.forClass(Class.class); Mockito.when(mockUserScanResults.get(0)).thenReturn(mockUser); Mockito.when(mockUserScanResults.size()).thenReturn(1); Mockito.when(mockDynamoDBOperations.scan(classCaptor.capture(), scanCaptor.capture())).thenReturn( mockUserScanResults); // Execute the query Object[] parameters = new Object[] { testSet }; Object o = partTreeDynamoDBQuery.execute(parameters); // Assert that we obtain the expected single result assertEquals(o, mockUser); // Assert that we scanned DynamoDB for the correct class assertEquals(classCaptor.getValue(), User.class); // Assert that we have one filter condition Map<String, Condition> filterConditions = scanCaptor.getValue().getScanFilter(); assertEquals(1, filterConditions.size()); Condition testSetFilterCondition = filterConditions.get("testSet"); assertNotNull(testSetFilterCondition); assertEquals(ComparisonOperator.EQ.name(), testSetFilterCondition.getComparisonOperator()); // Assert we only have one attribute value for each filter condition assertEquals(1, testSetFilterCondition.getAttributeValueList().size()); // Assert that there the attribute value type for this attribute value // is String, // and its value is the parameter expected assertNotNull(testSetFilterCondition.getAttributeValueList().get(0).getSS()); assertTrue(ClassUtils.isAssignable(Iterable.class, testSetFilterCondition.getAttributeValueList().get(0).getSS().getClass())); Iterable iterable = testSetFilterCondition.getAttributeValueList().get(0).getSS(); List<Object> returnObjects = new ArrayList<Object>(); for (Object object : iterable) { returnObjects.add(object); } assertEquals(1,returnObjects.size()); assertEquals("testData",returnObjects.get(0)); // Assert that all other attribute value types other than String type // are null assertNull(testSetFilterCondition.getAttributeValueList().get(0).getS()); assertNull(testSetFilterCondition.getAttributeValueList().get(0).getN()); assertNull(testSetFilterCondition.getAttributeValueList().get(0).getNS()); assertNull(testSetFilterCondition.getAttributeValueList().get(0).getB()); assertNull(testSetFilterCondition.getAttributeValueList().get(0).getBS()); // Verify that the expected DynamoDBOperations method was called Mockito.verify(mockDynamoDBOperations).scan(classCaptor.getValue(), scanCaptor.getValue()); }
Example #27
Source File: PartTreeDynamoDBQueryUnitTest.java From spring-data-dynamodb with Apache License 2.0 | 4 votes |
@SuppressWarnings({ "unchecked", "rawtypes" }) @Test public void testExecute_WhenFinderMethodIsFindingSingleEntity_WithMultipleStringParameters_WhenFindingByHashKeyAndANonHashOrRangeProperty_WhenDynamoDBAttributeNamesOveridden() { setupCommonMocksForThisRepositoryMethod(mockUserEntityMetadata, mockDynamoDBUserQueryMethod, User.class, "findByIdAndName", 2, "id", null); Mockito.when(mockUserEntityMetadata.getOverriddenAttributeName("name")).thenReturn("Name"); Mockito.when(mockUserEntityMetadata.getOverriddenAttributeName("id")).thenReturn("Id"); // Mock out specific DynamoDBOperations behavior expected by this method ArgumentCaptor<DynamoDBScanExpression> scanCaptor = ArgumentCaptor.forClass(DynamoDBScanExpression.class); ArgumentCaptor<Class> classCaptor = ArgumentCaptor.forClass(Class.class); Mockito.when(mockUserScanResults.get(0)).thenReturn(mockUser); Mockito.when(mockUserScanResults.size()).thenReturn(1); Mockito.when(mockDynamoDBOperations.scan(classCaptor.capture(), scanCaptor.capture())).thenReturn( mockUserScanResults); // Execute the query Object[] parameters = new Object[] { "someId", "someName" }; Object o = partTreeDynamoDBQuery.execute(parameters); // Assert that we obtain the expected single result assertEquals(o, mockUser); // Assert that we scanned DynamoDB for the correct class assertEquals(classCaptor.getValue(), User.class); // Assert that we have two filter conditions, for the id and name Map<String, Condition> filterConditions = scanCaptor.getValue().getScanFilter(); assertEquals(2, filterConditions.size()); Condition nameFilterCondition = filterConditions.get("Name"); assertNotNull(nameFilterCondition); Condition idFilterCondition = filterConditions.get("Id"); assertNotNull(idFilterCondition); assertEquals(ComparisonOperator.EQ.name(), nameFilterCondition.getComparisonOperator()); assertEquals(ComparisonOperator.EQ.name(), idFilterCondition.getComparisonOperator()); // Assert we only have one attribute value for each filter condition assertEquals(1, nameFilterCondition.getAttributeValueList().size()); assertEquals(1, idFilterCondition.getAttributeValueList().size()); // Assert that there the attribute value type for this attribute value // is String, // and its value is the parameter expected assertEquals("someName", nameFilterCondition.getAttributeValueList().get(0).getS()); assertEquals("someId", idFilterCondition.getAttributeValueList().get(0).getS()); // Assert that all other attribute value types other than String type // are null assertNull(nameFilterCondition.getAttributeValueList().get(0).getSS()); assertNull(nameFilterCondition.getAttributeValueList().get(0).getN()); assertNull(nameFilterCondition.getAttributeValueList().get(0).getNS()); assertNull(nameFilterCondition.getAttributeValueList().get(0).getB()); assertNull(nameFilterCondition.getAttributeValueList().get(0).getBS()); assertNull(idFilterCondition.getAttributeValueList().get(0).getSS()); assertNull(idFilterCondition.getAttributeValueList().get(0).getN()); assertNull(idFilterCondition.getAttributeValueList().get(0).getNS()); assertNull(idFilterCondition.getAttributeValueList().get(0).getB()); assertNull(idFilterCondition.getAttributeValueList().get(0).getBS()); // Verify that the expected DynamoDBOperations method was called Mockito.verify(mockDynamoDBOperations).scan(classCaptor.getValue(), scanCaptor.getValue()); }
Example #28
Source File: PartTreeDynamoDBQueryUnitTest.java From spring-data-dynamodb with Apache License 2.0 | 4 votes |
@SuppressWarnings({ "unchecked", "rawtypes" }) @Test public void testExecute_WhenFinderMethodIsFindingEntityList_WithSingleStringParameter_WhenNotFindingByHashKey() { setupCommonMocksForThisRepositoryMethod(mockUserEntityMetadata, mockDynamoDBUserQueryMethod, User.class, "findByName", 1, "id", null); Mockito.when(mockDynamoDBUserQueryMethod.isCollectionQuery()).thenReturn(true); // Mock out specific DynamoDBOperations behavior expected by this method ArgumentCaptor<DynamoDBScanExpression> scanCaptor = ArgumentCaptor.forClass(DynamoDBScanExpression.class); ArgumentCaptor<Class> classCaptor = ArgumentCaptor.forClass(Class.class); Mockito.when(mockUserScanResults.get(0)).thenReturn(mockUser); Mockito.when(mockUserScanResults.size()).thenReturn(1); Mockito.when(mockDynamoDBOperations.scan(classCaptor.capture(), scanCaptor.capture())).thenReturn( mockUserScanResults); // Execute the query Object[] parameters = new Object[] { "someName" }; Object o = partTreeDynamoDBQuery.execute(parameters); // Assert that we obtain the expected list of results assertEquals(o, mockUserScanResults); // Assert that the list of results contains the correct elements assertEquals(1, mockUserScanResults.size()); assertEquals(mockUser, mockUserScanResults.get(0)); // Assert that we scanned DynamoDB for the correct class assertEquals(classCaptor.getValue(), User.class); // Assert that we have only one filter condition, for the name of the // property Map<String, Condition> filterConditions = scanCaptor.getValue().getScanFilter(); assertEquals(1, filterConditions.size()); Condition filterCondition = filterConditions.get("name"); assertNotNull(filterCondition); assertEquals(ComparisonOperator.EQ.name(), filterCondition.getComparisonOperator()); // Assert we only have one attribute value for this filter condition assertEquals(1, filterCondition.getAttributeValueList().size()); // Assert that there the attribute value type for this attribute value // is String, // and its value is the parameter expected assertEquals("someName", filterCondition.getAttributeValueList().get(0).getS()); // Assert that all other attribute value types other than String type // are null assertNull(filterCondition.getAttributeValueList().get(0).getSS()); assertNull(filterCondition.getAttributeValueList().get(0).getN()); assertNull(filterCondition.getAttributeValueList().get(0).getNS()); assertNull(filterCondition.getAttributeValueList().get(0).getB()); assertNull(filterCondition.getAttributeValueList().get(0).getBS()); // Verify that the expected DynamoDBOperations method was called Mockito.verify(mockDynamoDBOperations).scan(classCaptor.getValue(), scanCaptor.getValue()); }
Example #29
Source File: DynamoDBEventDao.java From lambda-java8-dynamodb with Apache License 2.0 | 4 votes |
@Override public List<Event> findAllEvents() { return mapper.scan(Event.class, new DynamoDBScanExpression()); }
Example #30
Source File: PartTreeDynamoDBQueryUnitTest.java From spring-data-dynamodb with Apache License 2.0 | 4 votes |
@SuppressWarnings({ "unchecked", "rawtypes" }) @Test public void testExecute_WhenFinderMethodIsFindingEntityList_WithSingleStringArrayParameter_WithIn_WhenNotFindingByHashKey() { setupCommonMocksForThisRepositoryMethod(mockUserEntityMetadata, mockDynamoDBUserQueryMethod, User.class, "findByNameIn", 1, "id", null); Mockito.when(mockDynamoDBUserQueryMethod.isCollectionQuery()).thenReturn(true); String[] names = new String[] { "someName", "someOtherName" }; // Mock out specific DynamoDBOperations behavior expected by this method ArgumentCaptor<DynamoDBScanExpression> scanCaptor = ArgumentCaptor.forClass(DynamoDBScanExpression.class); ArgumentCaptor<Class> classCaptor = ArgumentCaptor.forClass(Class.class); Mockito.when(mockUserScanResults.get(0)).thenReturn(mockUser); Mockito.when(mockUserScanResults.size()).thenReturn(1); Mockito.when(mockDynamoDBOperations.scan(classCaptor.capture(), scanCaptor.capture())).thenReturn(mockUserScanResults); // Execute the query Object[] parameters = new Object[] { names }; Object o = partTreeDynamoDBQuery.execute(parameters); // Assert that we obtain the expected list of results assertEquals(o, mockUserScanResults); // Assert that the list of results contains the correct elements assertEquals(1, mockUserScanResults.size()); assertEquals(mockUser, mockUserScanResults.get(0)); // Assert that we scanned DynamoDB for the correct class assertEquals(classCaptor.getValue(), User.class); // Assert that we have only one filter condition, for the name of the // property Map<String, Condition> filterConditions = scanCaptor.getValue().getScanFilter(); assertEquals(1, filterConditions.size()); Condition filterCondition = filterConditions.get("name"); assertNotNull(filterCondition); assertEquals(ComparisonOperator.IN.name(), filterCondition.getComparisonOperator()); // Assert we only have an attribute value for each element of the IN array assertEquals(2, filterCondition.getAttributeValueList().size()); // Assert that there the attribute value type for this attribute value // is String, // and its value is the parameter expected assertEquals(names[0], filterCondition.getAttributeValueList().get(0).getS()); assertEquals(names[1], filterCondition.getAttributeValueList().get(1).getS()); // Assert that all other attribute value types other than String type // are null assertNull(filterCondition.getAttributeValueList().get(0).getSS()); assertNull(filterCondition.getAttributeValueList().get(0).getN()); assertNull(filterCondition.getAttributeValueList().get(0).getNS()); assertNull(filterCondition.getAttributeValueList().get(0).getB()); assertNull(filterCondition.getAttributeValueList().get(0).getBS()); // Verify that the expected DynamoDBOperations method was called Mockito.verify(mockDynamoDBOperations).scan(classCaptor.getValue(), scanCaptor.getValue()); }