Skip to content

Commit 7060dc9

Browse files
authored
Merge pull request #129 from derjust/MoreUnitTesting
Improving Unit test coverage
2 parents aa0c0b1 + 64b5a6f commit 7060dc9

8 files changed

+183
-13
lines changed

src/main/java/org/socialsignin/spring/data/dynamodb/query/AbstractSingleEntityQuery.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
public abstract class AbstractSingleEntityQuery<T> extends AbstractQuery<T> implements Query<T> {
2424

25-
public AbstractSingleEntityQuery(DynamoDBOperations dynamoDBOperations,Class<T> clazz) {
25+
public AbstractSingleEntityQuery(DynamoDBOperations dynamoDBOperations, Class<T> clazz) {
2626
super(dynamoDBOperations,clazz);
2727
}
2828

src/main/java/org/socialsignin/spring/data/dynamodb/query/QueryExpressionCountQuery.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,8 @@
2323
*/
2424
public class QueryExpressionCountQuery<T> extends AbstractSingleEntityQuery<Long> {
2525

26-
private DynamoDBQueryExpression<T> queryExpression;
27-
28-
private Class<T> domainClass;
29-
26+
private final DynamoDBQueryExpression<T> queryExpression;
27+
private final Class<T> domainClass;
3028

3129
public QueryExpressionCountQuery(DynamoDBOperations dynamoDBOperations, Class<T> clazz,
3230
DynamoDBQueryExpression<T> queryExpression) {
@@ -37,7 +35,7 @@ public QueryExpressionCountQuery(DynamoDBOperations dynamoDBOperations, Class<T>
3735

3836
@Override
3937
public Long getSingleResult() {
40-
return new Long(dynamoDBOperations.count(domainClass, queryExpression));
38+
return Long.valueOf(dynamoDBOperations.count(domainClass, queryExpression));
4139
}
4240

4341
}

src/main/java/org/socialsignin/spring/data/dynamodb/query/QueryRequestCountQuery.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,12 @@
1818
import com.amazonaws.services.dynamodbv2.model.QueryRequest;
1919
import org.socialsignin.spring.data.dynamodb.core.DynamoDBOperations;
2020

21-
public class QueryRequestCountQuery<T> extends AbstractSingleEntityQuery<Long> {
21+
public class QueryRequestCountQuery extends AbstractSingleEntityQuery<Long> {
2222

23-
private DynamoDBOperations dynamoDBOperations;
24-
private QueryRequest queryRequest;
23+
private final DynamoDBOperations dynamoDBOperations;
24+
private final QueryRequest queryRequest;
2525

26-
public QueryRequestCountQuery(DynamoDBOperations dynamoDBOperations,Class<T> clazz,QueryRequest queryRequest) {
26+
public QueryRequestCountQuery(DynamoDBOperations dynamoDBOperations, QueryRequest queryRequest) {
2727
super(null, Long.class);
2828
this.queryRequest = queryRequest;
2929
this.dynamoDBOperations = dynamoDBOperations;
@@ -32,7 +32,7 @@ public QueryRequestCountQuery(DynamoDBOperations dynamoDBOperations,Class<T> cla
3232
@Override
3333
public Long getSingleResult() {
3434

35-
return new Long(dynamoDBOperations.count(clazz, queryRequest));
35+
return Long.valueOf(dynamoDBOperations.count(clazz, queryRequest));
3636
}
3737

3838
}

src/main/java/org/socialsignin/spring/data/dynamodb/repository/query/DynamoDBEntityWithHashAndRangeKeyCriteria.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ protected Query<Long> buildFinderCountQuery(DynamoDBOperations dynamoDBOperation
223223
QueryRequest queryRequest = buildQueryRequest(tableName, getGlobalSecondaryIndexName(),
224224
getHashKeyAttributeName(), getRangeKeyAttributeName(), this.getRangeKeyPropertyName(),
225225
getHashKeyConditions(), getRangeKeyConditions());
226-
return new QueryRequestCountQuery<T>(dynamoDBOperations,entityInformation.getJavaType(), queryRequest);
226+
return new QueryRequestCountQuery(dynamoDBOperations, queryRequest);
227227

228228
} else {
229229
DynamoDBQueryExpression<T> queryExpression = buildQueryExpression();

src/main/java/org/socialsignin/spring/data/dynamodb/repository/query/DynamoDBEntityWithHashKeyOnlyCriteria.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ protected Query<Long> buildFinderCountQuery(DynamoDBOperations dynamoDBOperation
7171
List<Condition> hashKeyConditions = getHashKeyConditions();
7272
QueryRequest queryRequest = buildQueryRequest(dynamoDBOperations.getOverriddenTableName(clazz, entityInformation.getDynamoDBTableName()),
7373
getGlobalSecondaryIndexName(), getHashKeyAttributeName(), null, null, hashKeyConditions, null);
74-
return new QueryRequestCountQuery<>(dynamoDBOperations, entityInformation.getJavaType(), queryRequest);
74+
return new QueryRequestCountQuery(dynamoDBOperations, queryRequest);
7575

7676
} else {
7777
return new ScanExpressionCountQuery<>(dynamoDBOperations, clazz, buildScanExpression(),pageQuery);
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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.query;
17+
18+
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBQueryExpression;
19+
import org.junit.Before;
20+
import org.junit.Test;
21+
import org.mockito.Mock;
22+
import org.socialsignin.spring.data.dynamodb.core.DynamoDBOperations;
23+
import org.socialsignin.spring.data.dynamodb.domain.sample.User;
24+
25+
import java.util.List;
26+
import java.util.Random;
27+
28+
import static org.junit.Assert.assertEquals;
29+
30+
public class AbstractSingleEntityQueryTest {
31+
32+
@Mock
33+
private DynamoDBOperations dynamoDBOperations;
34+
@Mock
35+
private User entity;
36+
37+
private AbstractSingleEntityQuery<User> underTest;
38+
39+
@Before
40+
public void setUp() {
41+
underTest = new AbstractSingleEntityQuery<User>(dynamoDBOperations, User.class) {
42+
@Override
43+
public User getSingleResult() {
44+
return entity;
45+
}
46+
};
47+
}
48+
49+
@Test
50+
public void testGetResultList() {
51+
List<User> actual = underTest.getResultList();
52+
53+
assertEquals(1, actual.size());
54+
assertEquals(entity, actual.get(0));
55+
}
56+
57+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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.query;
17+
18+
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBQueryExpression;
19+
import com.amazonaws.services.dynamodbv2.model.QueryRequest;
20+
import org.junit.Before;
21+
import org.junit.Test;
22+
import org.junit.runner.RunWith;
23+
import org.mockito.Mock;
24+
import org.mockito.junit.MockitoJUnitRunner;
25+
import org.socialsignin.spring.data.dynamodb.core.DynamoDBOperations;
26+
import org.socialsignin.spring.data.dynamodb.domain.sample.User;
27+
28+
import java.util.Random;
29+
30+
import static org.junit.Assert.assertEquals;
31+
import static org.mockito.Mockito.when;
32+
33+
@RunWith(MockitoJUnitRunner.class)
34+
public class QueryExpressionCountQueryTest {
35+
36+
private static final Random r = new Random();
37+
@Mock
38+
private DynamoDBOperations dynamoDBOperations;
39+
@Mock
40+
private DynamoDBQueryExpression<User> queryExpression;
41+
42+
private QueryExpressionCountQuery<User> underTest;
43+
44+
@Before
45+
public void setUp() {
46+
underTest = new QueryExpressionCountQuery(dynamoDBOperations, User.class, queryExpression);
47+
}
48+
49+
@Test
50+
public void testGetSingleResult() {
51+
int expected = r.nextInt();
52+
when(dynamoDBOperations.count(User.class, queryExpression)).thenReturn(expected);
53+
54+
Long actual = underTest.getSingleResult();
55+
56+
assertEquals(Long.valueOf(expected), actual);
57+
}
58+
59+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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.query;
17+
18+
import com.amazonaws.services.dynamodbv2.model.QueryRequest;
19+
import org.junit.Before;
20+
import org.junit.Test;
21+
import org.junit.runner.RunWith;
22+
import org.mockito.Mock;
23+
import org.mockito.junit.MockitoJUnitRunner;
24+
import org.socialsignin.spring.data.dynamodb.core.DynamoDBOperations;
25+
26+
import java.util.Random;
27+
28+
import static org.junit.Assert.assertEquals;
29+
import static org.mockito.Mockito.when;
30+
31+
@RunWith(MockitoJUnitRunner.class)
32+
public class QueryRequestCountQueryTest {
33+
34+
private static final Random r = new Random();
35+
@Mock
36+
private DynamoDBOperations dynamoDBOperations;
37+
@Mock
38+
private QueryRequest queryRequest;
39+
40+
private QueryRequestCountQuery underTest;
41+
42+
@Before
43+
public void setUp() {
44+
underTest = new QueryRequestCountQuery(dynamoDBOperations, queryRequest);
45+
}
46+
47+
@Test
48+
public void testGetSingleResult() {
49+
int expected = r.nextInt();
50+
when(dynamoDBOperations.count(Long.class, queryRequest)).thenReturn(expected);
51+
52+
Long actual = underTest.getSingleResult();
53+
54+
assertEquals(Long.valueOf(expected), actual);
55+
}
56+
}

0 commit comments

Comments
 (0)