@SuppressWarnings("unchecked") protected <V> Object getPropertyAttributeValue(String propertyName, Object value) { DynamoDBMarshaller<V> marshaller = (DynamoDBMarshaller<V>) entityInformation.getMarshallerForProperty(propertyName); if (marshaller != null) { return marshaller.marshall((V) value); } else { return value; } }
private List<String> getDateListAsStringList(List<Date> dateList) { DynamoDBMarshaller<Date> marshaller = new DefaultDynamoDBDateMarshaller(); List<String> list = new ArrayList<String>(); for (Date date : dateList) { if (date != null) { list.add(marshaller.marshall(date)); } else { list.add(null); } } return list; }
@Test public void testGetMarshallerForProperty_DelegatesToEntityMetadata_IrrespectiveOfEntityInformationSetup() { DynamoDBMarshaller<?> marshaller1 = dynamoDBPlaylistEntityInformation.getMarshallerForProperty("marshalledProperty"); Assert.assertEquals(mockPropertyMarshaller, marshaller1); DynamoDBMarshaller<?> marshaller2 = dynamoDBUserEntityInformation.getMarshallerForProperty("marshalledProperty"); Assert.assertEquals(mockPropertyMarshaller, marshaller2); }
@Test public void testGetMarshallerForProperty_DelegatesToEntityMetadata_IrrespectiveOfEntityInformationSetup() { DynamoDBMarshaller<?> marshaller1 = dynamoDBPlaylistEntityInformation.getMarshallerForProperty("marshalledProperty"); Assert.assertEquals(mockPropertyMarshaller, marshaller1); }
@Override public DynamoDBMarshaller<?> getMarshallerForProperty(String propertyName) { return metadata.getMarshallerForProperty(propertyName); }
@SuppressWarnings({ "unchecked", "rawtypes" }) @Test public void testExecute_WhenFinderMethodIsFindingEntityList_WithSingleDateParameter_WithCustomMarshaller_WhenNotFindingByHashKey() throws ParseException { String joinYearString = "2013"; DateFormat dateFormat = new SimpleDateFormat("yyyy"); Date joinYear = dateFormat.parse(joinYearString); setupCommonMocksForThisRepositoryMethod(mockUserEntityMetadata, mockDynamoDBUserQueryMethod, User.class, "findByJoinYear", 1, "id", null); Mockito.when(mockDynamoDBUserQueryMethod.isCollectionQuery()).thenReturn(true); DynamoDBMarshaller marshaller = new DynamoDBYearMarshaller(); Mockito.when(mockUserEntityMetadata.getMarshallerForProperty("joinYear")).thenReturn(marshaller); // 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[] { joinYear }; 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("joinYear"); 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(joinYearString, 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()); }
@SuppressWarnings({ "unchecked", "rawtypes" }) @Test public void testExecute_WhenFinderMethodIsFindingEntityList_WithSingleDateParameter_WithCustomMarshaller_WhenFindingByGlobalSecondaryHashIndexHashKey() throws ParseException { String joinYearString = "2013"; DateFormat dateFormat = new SimpleDateFormat("yyyy"); Date joinYear = dateFormat.parse(joinYearString); setupCommonMocksForThisRepositoryMethod(mockUserEntityMetadata, mockDynamoDBUserQueryMethod, User.class, "findByJoinYear", 1, "id", null); Mockito.when(mockDynamoDBUserQueryMethod.isCollectionQuery()).thenReturn(true); DynamoDBMarshaller marshaller = new DynamoDBYearMarshaller(); Mockito.when(mockUserEntityMetadata.isGlobalIndexHashKeyProperty("joinYear")).thenReturn(true); Mockito.when(mockUserEntityMetadata.getMarshallerForProperty("joinYear")).thenReturn(marshaller); Map<String, String[]> indexRangeKeySecondaryIndexNames = new HashMap<String,String[]>(); indexRangeKeySecondaryIndexNames.put("joinYear", new String[] {"JoinYear-index"}); Mockito.when(mockUserEntityMetadata.getGlobalSecondaryIndexNamesByPropertyName()).thenReturn(indexRangeKeySecondaryIndexNames); Mockito.when(mockUserEntityMetadata.getDynamoDBTableName()).thenReturn("user"); // Mock out specific QueryRequestMapper behavior expected by this method ArgumentCaptor<QueryRequest> queryCaptor = ArgumentCaptor.forClass(QueryRequest.class); ArgumentCaptor<Class> classCaptor = ArgumentCaptor.forClass(Class.class); Mockito.when(mockUserQueryResults.get(0)).thenReturn(mockUser); Mockito.when(mockUserQueryResults.size()).thenReturn(1); Mockito.when(mockDynamoDBOperations.query(classCaptor.capture(), queryCaptor.capture())) .thenReturn(mockUserQueryResults); Mockito.when(mockDynamoDBOperations.getOverriddenTableName("user")).thenReturn("user"); // Execute the query Object[] parameters = new Object[] { joinYear}; Object o = partTreeDynamoDBQuery.execute(parameters); // Assert that we obtain the expected results assertEquals(mockUserQueryResults, o); assertEquals(1, mockUserQueryResults.size()); assertEquals(mockUser, mockUserQueryResults.get(0)); // Assert that we scanned DynamoDB for the correct class assertEquals(classCaptor.getValue(), User.class); String indexName = queryCaptor.getValue().getIndexName(); assertNotNull(indexName); assertEquals("JoinYear-index",indexName); assertEquals("user",queryCaptor.getValue().getTableName()); // Assert that we have only one range condition for the global secondary index hash key assertEquals(1,queryCaptor.getValue().getKeyConditions().size()); Condition condition = queryCaptor.getValue().getKeyConditions().get("joinYear"); assertEquals(ComparisonOperator.EQ.name(),condition.getComparisonOperator()); assertEquals(1,condition.getAttributeValueList().size()); assertEquals(joinYearString,condition.getAttributeValueList().get(0).getS()); // Assert that all other attribute value types other than String type // are null assertNull(condition.getAttributeValueList().get(0).getSS()); assertNull(condition.getAttributeValueList().get(0).getN()); assertNull(condition.getAttributeValueList().get(0).getNS()); assertNull(condition.getAttributeValueList().get(0).getB()); assertNull(condition.getAttributeValueList().get(0).getBS()); // Verify that the expected DynamoDBOperations method was called Mockito.verify(mockDynamoDBOperations).query(classCaptor.getValue(), queryCaptor.getValue()); }
@SuppressWarnings({ "unchecked", "rawtypes" }) @Test public void testExecute_WhenFinderMethodIsFindingEntityList_WithSingleStringParameter_WithCustomMarshaller_WhenNotFindingByHashKey() throws ParseException { String postcode = "N1"; setupCommonMocksForThisRepositoryMethod(mockUserEntityMetadata, mockDynamoDBUserQueryMethod, User.class, "findByPostCode", 1, "id", null); Mockito.when(mockDynamoDBUserQueryMethod.isCollectionQuery()).thenReturn(true); DynamoDBMarshaller marshaller = new CaseChangingMarshaller(); Mockito.when(mockUserEntityMetadata.getMarshallerForProperty("postCode")).thenReturn(marshaller); // 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[] { postcode }; 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("postCode"); 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("n1", 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()); }
public DynamoDBMarshaller<?> getMarshallerForProperty(String propertyName);