From ad3ec4e440f0b0bf57272609897f18bbae20d304 Mon Sep 17 00:00:00 2001 From: Sebastian J Date: Tue, 23 Jan 2018 02:00:07 -0500 Subject: [PATCH] Issue #96: NullPointerException for findAllByOrderByProperty queries --- .../domain/sample/RepositoryFindTest.java | 58 +++++++++++++++++++ .../domain/sample/UserRepository.java | 3 + .../dynamodb/utils/DynamoDBLocalResource.java | 26 ++++++--- 3 files changed, 78 insertions(+), 9 deletions(-) create mode 100644 src/test/java/org/socialsignin/spring/data/dynamodb/domain/sample/RepositoryFindTest.java diff --git a/src/test/java/org/socialsignin/spring/data/dynamodb/domain/sample/RepositoryFindTest.java b/src/test/java/org/socialsignin/spring/data/dynamodb/domain/sample/RepositoryFindTest.java new file mode 100644 index 00000000..a45e689c --- /dev/null +++ b/src/test/java/org/socialsignin/spring/data/dynamodb/domain/sample/RepositoryFindTest.java @@ -0,0 +1,58 @@ +/** + * Copyright © 2013 spring-data-dynamodb (https://github.com/derjust/spring-data-dynamodb) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License 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 org.socialsignin.spring.data.dynamodb.domain.sample; + + +import com.amazonaws.services.dynamodbv2.AmazonDynamoDB; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.socialsignin.spring.data.dynamodb.repository.config.EnableDynamoDBRepositories; +import org.socialsignin.spring.data.dynamodb.utils.DynamoDBLocalResource; +import org.socialsignin.spring.data.dynamodb.utils.DynamoDBResource; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Configuration; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +import java.util.List; + +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration(classes = {DynamoDBLocalResource.class, RepositoryFindTest.TestAppConfig.class}) +public class RepositoryFindTest { + + @Configuration + @EnableDynamoDBRepositories(basePackages = "org.socialsignin.spring.data.dynamodb.domain.sample") + public static class TestAppConfig { + } + + @Autowired + private AmazonDynamoDB ddb; + @Autowired + private UserRepository userRepository; + + @Before + public void setUp() { + DynamoDBLocalResource.createTable(ddb, User.class); + } + + @Test + public void testFindAll() { + List actual = userRepository.findAllByOrderByName(); + } +} + + diff --git a/src/test/java/org/socialsignin/spring/data/dynamodb/domain/sample/UserRepository.java b/src/test/java/org/socialsignin/spring/data/dynamodb/domain/sample/UserRepository.java index 63cdbc13..b787cb28 100644 --- a/src/test/java/org/socialsignin/spring/data/dynamodb/domain/sample/UserRepository.java +++ b/src/test/java/org/socialsignin/spring/data/dynamodb/domain/sample/UserRepository.java @@ -30,6 +30,9 @@ public interface UserRepository extends Repository { @EnableScan List findByLeaveDate(Instant leaveDate); + @EnableScan + List findAllByOrderByName(); + @EnableScan Optional findByName(String name); diff --git a/src/test/java/org/socialsignin/spring/data/dynamodb/utils/DynamoDBLocalResource.java b/src/test/java/org/socialsignin/spring/data/dynamodb/utils/DynamoDBLocalResource.java index f704c15b..7a2f207f 100644 --- a/src/test/java/org/socialsignin/spring/data/dynamodb/utils/DynamoDBLocalResource.java +++ b/src/test/java/org/socialsignin/spring/data/dynamodb/utils/DynamoDBLocalResource.java @@ -25,12 +25,16 @@ import com.amazonaws.services.dynamodbv2.model.ProvisionedThroughput; import com.amazonaws.services.dynamodbv2.model.ScalarAttributeType; import org.junit.rules.ExternalResource; +import org.socialsignin.spring.data.dynamodb.repository.support.DynamoDBEntityInformation; import org.socialsignin.spring.data.dynamodb.repository.support.DynamoDBEntityMetadataSupport; +import org.socialsignin.spring.data.dynamodb.repository.support.DynamoDBIdIsHashAndRangeKeyEntityInformation; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; +import javax.swing.text.html.Option; import java.util.ArrayList; import java.util.List; +import java.util.Optional; @Configuration public class DynamoDBLocalResource extends ExternalResource { @@ -43,27 +47,31 @@ public AmazonDynamoDB amazonDynamoDB() { return ddb; } - public CreateTableResult createTable(Class domainType) { + public static CreateTableResult createTable(AmazonDynamoDB ddb, Class domainType) { DynamoDBEntityMetadataSupport support = new DynamoDBEntityMetadataSupport(domainType); + DynamoDBEntityInformation entityInfo = support.getEntityInformation(); - String tableName = support.getDynamoDBTableName(); - String hashKey = support.getHashKeyPropertyName(); - String rangeKey = support.getHashKeyPropertyName(); + String tableName = entityInfo.getDynamoDBTableName(); + String hashKey = entityInfo.getHashKeyPropertyName(); + Optional rangeKey = Optional.empty(); + if (entityInfo instanceof DynamoDBIdIsHashAndRangeKeyEntityInformation) { + rangeKey = Optional.of(((DynamoDBIdIsHashAndRangeKeyEntityInformation)entityInfo).getRangeKeyPropertyName()); + } - return createTable(tableName, hashKey, rangeKey); + return createTable(ddb, tableName, hashKey, rangeKey); } - private CreateTableResult createTable(String tableName, String hashKeyName, String rangeKeyName) { + private static CreateTableResult createTable(AmazonDynamoDB ddb, String tableName, String hashKeyName, Optional rangeKeyName) { List attributeDefinitions = new ArrayList<>(); attributeDefinitions.add(new AttributeDefinition(hashKeyName, ScalarAttributeType.S)); List ks = new ArrayList<>(); ks.add(new KeySchemaElement(hashKeyName, KeyType.HASH)); - if (rangeKeyName != null) { - attributeDefinitions.add(new AttributeDefinition(rangeKeyName, ScalarAttributeType.S)); + if (rangeKeyName.isPresent()) { + attributeDefinitions.add(new AttributeDefinition(rangeKeyName.get(), ScalarAttributeType.S)); - ks.add(new KeySchemaElement(rangeKeyName, KeyType.RANGE)); + ks.add(new KeySchemaElement(rangeKeyName.get(), KeyType.RANGE)); } ProvisionedThroughput provisionedthroughput = new ProvisionedThroughput(10L, 10L);