Skip to content

Commit

Permalink
Issue #96: NullPointerException for findAllByOrderByProperty queries
Browse files Browse the repository at this point in the history
  • Loading branch information
derjust committed Jan 26, 2018
1 parent 3c5d51d commit b47008f
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 9 deletions.
@@ -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<User> actual = userRepository.findAllByOrderByName();
}
}


Expand Up @@ -30,6 +30,9 @@ public interface UserRepository extends Repository<User, String> {
@EnableScan
List<User> findByLeaveDate(Instant leaveDate);

@EnableScan
List<User> findAllByOrderByName();

@EnableScan
Optional<User> findByName(String name);

Expand Down
Expand Up @@ -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 {
Expand All @@ -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<String> 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<String> rangeKeyName) {
List<AttributeDefinition> attributeDefinitions = new ArrayList<>();
attributeDefinitions.add(new AttributeDefinition(hashKeyName, ScalarAttributeType.S));

List<KeySchemaElement> 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);
Expand Down

0 comments on commit b47008f

Please sign in to comment.