Skip to content

Commit 130c54f

Browse files
authored
Merge pull request #110 from derjust/issue_109
Issue #109: `deleteById` used the `Optional` from `findById`
2 parents 5c40b6e + 1809762 commit 130c54f

File tree

3 files changed

+32
-5
lines changed

3 files changed

+32
-5
lines changed

src/changes/changes.xml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,12 @@
1616
<action dev="derjust" type="add" date="2017-12-17" >
1717
Update Mockito and resolve dependency clashes
1818
</action>
19-
<action dev="Gaurav Rawat" type="fix" issue="91">>
19+
<action dev="Gaurav Rawat" type="fix" issue="91">
2020
Fixed false assertion introduced implementing #91
2121
</action>
22+
<action dev="derjust" type="fix" issue="109">
23+
Fix incorrect passing of an Optional into the delete method
24+
</action>
2225
</release>
2326
<release version="5.0.0" date="2017-11-27" description="Spring 5 release" >
2427
<action dev="derjust" type="add">

src/main/java/org/socialsignin/spring/data/dynamodb/repository/support/SimpleDynamoDBCrudRepository.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,11 +168,14 @@ public void deleteById(ID id) {
168168
Assert.notNull(id, "The given id must not be null!");
169169

170170
Optional<T> entity = findById(id);
171-
if (!entity.isPresent()) {
171+
172+
if (entity.isPresent()) {
173+
dynamoDBOperations.delete(entity.get());
174+
175+
} else {
172176
throw new EmptyResultDataAccessException(String.format(
173177
"No %s entity with id %s exists!", domainType, id), 1);
174178
}
175-
dynamoDBOperations.delete(entity);
176179
}
177180

178181
@Override

src/test/java/org/socialsignin/spring/data/dynamodb/repository/support/SimpleDynamoDBCrudRepositoryUnitTest.java

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,11 @@
1515
*/
1616
package org.socialsignin.spring.data.dynamodb.repository.support;
1717

18+
import org.junit.Assert;
1819
import org.junit.Before;
1920
import org.junit.Test;
2021
import org.junit.runner.RunWith;
22+
import org.mockito.ArgumentCaptor;
2123
import org.mockito.Mock;
2224
import org.mockito.Mockito;
2325
import org.mockito.junit.MockitoJUnitRunner;
@@ -28,8 +30,11 @@
2830
import org.springframework.dao.EmptyResultDataAccessException;
2931

3032
import java.util.Optional;
33+
import java.util.Random;
3134

3235
import static org.junit.Assert.assertEquals;
36+
import static org.mockito.ArgumentMatchers.anyLong;
37+
import static org.mockito.Mockito.verify;
3338
import static org.mockito.Mockito.when;
3439

3540

@@ -87,16 +92,32 @@ public void setUp() {
8792
when(entityWithCompositeIdInformation.getRangeKey(testPlaylistId)).thenReturn("playlist1");
8893
when(entityWithCompositeIdInformation.isRangeKeyAware()).thenReturn(true);
8994

90-
repoForEntityWithOnlyHashKey = new SimpleDynamoDBCrudRepository<User, Long>(entityWithSimpleIdInformation,
95+
repoForEntityWithOnlyHashKey = new SimpleDynamoDBCrudRepository<>(entityWithSimpleIdInformation,
9196
dynamoDBOperations,mockEnableScanPermissions);
92-
repoForEntityWithHashAndRangeKey = new SimpleDynamoDBCrudRepository<Playlist, PlaylistId>(
97+
repoForEntityWithHashAndRangeKey = new SimpleDynamoDBCrudRepository<>(
9398
entityWithCompositeIdInformation, dynamoDBOperations,mockEnableScanPermissions);
9499

95100
when(dynamoDBOperations.load(User.class, 1l)).thenReturn(testUser);
96101
when(dynamoDBOperations.load(Playlist.class, "michael", "playlist1")).thenReturn(testPlaylist);
97102

98103
}
99104

105+
@Test
106+
public void deleteById() {
107+
final long id = new Random().nextLong();
108+
User testResult = new User();
109+
testResult.setId(Long.toString(id));
110+
111+
when(entityWithSimpleIdInformation.getHashKey(id)).thenReturn(id);
112+
when(dynamoDBOperations.load(User.class, id)).thenReturn(testResult);
113+
114+
repoForEntityWithOnlyHashKey.deleteById(id);
115+
116+
ArgumentCaptor<User> captor = ArgumentCaptor.forClass(User.class);
117+
Mockito.verify(dynamoDBOperations).delete(captor.capture());
118+
Assert.assertEquals(Long.toString(id), captor.getValue().getId());
119+
}
120+
100121
/**
101122
* @see DATAJPA-177
102123
*/

0 commit comments

Comments
 (0)