Skip to content

Commit ad3ec4e

Browse files
committed
Issue #96: NullPointerException for findAllByOrderByProperty queries
1 parent 8a9e5c5 commit ad3ec4e

File tree

3 files changed

+78
-9
lines changed

3 files changed

+78
-9
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/**
2+
* Copyright © 2013 spring-data-dynamodb (https://github.com/derjust/spring-data-dynamodb)
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.socialsignin.spring.data.dynamodb.domain.sample;
17+
18+
19+
import com.amazonaws.services.dynamodbv2.AmazonDynamoDB;
20+
import org.junit.Before;
21+
import org.junit.Test;
22+
import org.junit.runner.RunWith;
23+
import org.socialsignin.spring.data.dynamodb.repository.config.EnableDynamoDBRepositories;
24+
import org.socialsignin.spring.data.dynamodb.utils.DynamoDBLocalResource;
25+
import org.socialsignin.spring.data.dynamodb.utils.DynamoDBResource;
26+
import org.springframework.beans.factory.annotation.Autowired;
27+
import org.springframework.context.annotation.Configuration;
28+
import org.springframework.test.context.ContextConfiguration;
29+
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
30+
31+
import java.util.List;
32+
33+
@RunWith(SpringJUnit4ClassRunner.class)
34+
@ContextConfiguration(classes = {DynamoDBLocalResource.class, RepositoryFindTest.TestAppConfig.class})
35+
public class RepositoryFindTest {
36+
37+
@Configuration
38+
@EnableDynamoDBRepositories(basePackages = "org.socialsignin.spring.data.dynamodb.domain.sample")
39+
public static class TestAppConfig {
40+
}
41+
42+
@Autowired
43+
private AmazonDynamoDB ddb;
44+
@Autowired
45+
private UserRepository userRepository;
46+
47+
@Before
48+
public void setUp() {
49+
DynamoDBLocalResource.createTable(ddb, User.class);
50+
}
51+
52+
@Test
53+
public void testFindAll() {
54+
List<User> actual = userRepository.findAllByOrderByName();
55+
}
56+
}
57+
58+

src/test/java/org/socialsignin/spring/data/dynamodb/domain/sample/UserRepository.java

+3
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ public interface UserRepository extends Repository<User, String> {
3030
@EnableScan
3131
List<User> findByLeaveDate(Instant leaveDate);
3232

33+
@EnableScan
34+
List<User> findAllByOrderByName();
35+
3336
@EnableScan
3437
Optional<User> findByName(String name);
3538

src/test/java/org/socialsignin/spring/data/dynamodb/utils/DynamoDBLocalResource.java

+17-9
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,16 @@
2525
import com.amazonaws.services.dynamodbv2.model.ProvisionedThroughput;
2626
import com.amazonaws.services.dynamodbv2.model.ScalarAttributeType;
2727
import org.junit.rules.ExternalResource;
28+
import org.socialsignin.spring.data.dynamodb.repository.support.DynamoDBEntityInformation;
2829
import org.socialsignin.spring.data.dynamodb.repository.support.DynamoDBEntityMetadataSupport;
30+
import org.socialsignin.spring.data.dynamodb.repository.support.DynamoDBIdIsHashAndRangeKeyEntityInformation;
2931
import org.springframework.context.annotation.Bean;
3032
import org.springframework.context.annotation.Configuration;
3133

34+
import javax.swing.text.html.Option;
3235
import java.util.ArrayList;
3336
import java.util.List;
37+
import java.util.Optional;
3438

3539
@Configuration
3640
public class DynamoDBLocalResource extends ExternalResource {
@@ -43,27 +47,31 @@ public AmazonDynamoDB amazonDynamoDB() {
4347
return ddb;
4448
}
4549

46-
public CreateTableResult createTable(Class<?> domainType) {
50+
public static CreateTableResult createTable(AmazonDynamoDB ddb, Class<?> domainType) {
4751
DynamoDBEntityMetadataSupport support = new DynamoDBEntityMetadataSupport(domainType);
52+
DynamoDBEntityInformation entityInfo = support.getEntityInformation();
4853

49-
String tableName = support.getDynamoDBTableName();
50-
String hashKey = support.getHashKeyPropertyName();
51-
String rangeKey = support.getHashKeyPropertyName();
54+
String tableName = entityInfo.getDynamoDBTableName();
55+
String hashKey = entityInfo.getHashKeyPropertyName();
56+
Optional<String> rangeKey = Optional.empty();
57+
if (entityInfo instanceof DynamoDBIdIsHashAndRangeKeyEntityInformation) {
58+
rangeKey = Optional.of(((DynamoDBIdIsHashAndRangeKeyEntityInformation)entityInfo).getRangeKeyPropertyName());
59+
}
5260

53-
return createTable(tableName, hashKey, rangeKey);
61+
return createTable(ddb, tableName, hashKey, rangeKey);
5462
}
5563

56-
private CreateTableResult createTable(String tableName, String hashKeyName, String rangeKeyName) {
64+
private static CreateTableResult createTable(AmazonDynamoDB ddb, String tableName, String hashKeyName, Optional<String> rangeKeyName) {
5765
List<AttributeDefinition> attributeDefinitions = new ArrayList<>();
5866
attributeDefinitions.add(new AttributeDefinition(hashKeyName, ScalarAttributeType.S));
5967

6068
List<KeySchemaElement> ks = new ArrayList<>();
6169
ks.add(new KeySchemaElement(hashKeyName, KeyType.HASH));
6270

63-
if (rangeKeyName != null) {
64-
attributeDefinitions.add(new AttributeDefinition(rangeKeyName, ScalarAttributeType.S));
71+
if (rangeKeyName.isPresent()) {
72+
attributeDefinitions.add(new AttributeDefinition(rangeKeyName.get(), ScalarAttributeType.S));
6573

66-
ks.add(new KeySchemaElement(rangeKeyName, KeyType.RANGE));
74+
ks.add(new KeySchemaElement(rangeKeyName.get(), KeyType.RANGE));
6775
}
6876

6977
ProvisionedThroughput provisionedthroughput = new ProvisionedThroughput(10L, 10L);

0 commit comments

Comments
 (0)