Skip to content

Commit 0079e07

Browse files
committed
Creating test case for #77
Sadly the fix as per https://jira.spring.io/browse/DATACMNS-1008 doesn't work. Only the workaround does - Therefore this test fails right now :(
1 parent 0de29eb commit 0079e07

File tree

6 files changed

+143
-0
lines changed

6 files changed

+143
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package org.socialsignin.spring.data.dynamodb.issue77;
2+
3+
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBHashKey;
4+
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBTable;
5+
6+
@DynamoDBTable(tableName = "foo")
7+
public class Foo {
8+
9+
private long id;
10+
11+
@DynamoDBHashKey(attributeName = "Id")
12+
public long getId() {
13+
return id;
14+
}
15+
16+
public void setId(long id) {
17+
this.id = id;
18+
}
19+
20+
21+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package org.socialsignin.spring.data.dynamodb.issue77;
2+
3+
import org.springframework.data.repository.CrudRepository;
4+
5+
public interface FooRepository extends CrudRepository<Foo, Long>, FooRepositoryCustom {
6+
7+
// Workaround as per DATACMNS-1008
8+
//<X extends Foo> X save(X foo);
9+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package org.socialsignin.spring.data.dynamodb.issue77;
2+
3+
public interface FooRepositoryCustom {
4+
Foo doNonstandardThing(Foo foo);
5+
6+
// just here for testing...
7+
boolean isDoNonstandardThingCalled();
8+
boolean isSaveCalled();
9+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package org.socialsignin.spring.data.dynamodb.issue77;
2+
3+
public class FooRepositoryImpl implements FooRepositoryCustom {
4+
5+
private ThreadLocal<Boolean> saveCalled = new ThreadLocal<>();
6+
private ThreadLocal<Boolean> doNonstandardThingCalled = new ThreadLocal<>();
7+
8+
public FooRepositoryImpl() {
9+
saveCalled.set(false);
10+
doNonstandardThingCalled.set(false);
11+
}
12+
13+
public Foo save(Foo foo) {
14+
// Do things
15+
saveCalled.set(true);
16+
return foo;
17+
}
18+
19+
public Foo doNonstandardThing(Foo foo) {
20+
doNonstandardThingCalled.set(true);
21+
return foo;
22+
}
23+
24+
public boolean isDoNonstandardThingCalled() {
25+
return doNonstandardThingCalled.get();
26+
}
27+
28+
public boolean isSaveCalled() {
29+
return saveCalled.get();
30+
}
31+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package org.socialsignin.spring.data.dynamodb.issue77;
2+
3+
import org.springframework.beans.factory.annotation.Autowired;
4+
import org.springframework.stereotype.Service;
5+
6+
@Service
7+
public class FooService {
8+
private final FooRepository fooRepo;
9+
10+
@Autowired
11+
public FooService(FooRepository fooRepo) {
12+
this.fooRepo = fooRepo;
13+
}
14+
15+
public void processFoo(Foo foo) {
16+
fooRepo.doNonstandardThing(foo);
17+
fooRepo.save(foo);
18+
}
19+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package org.socialsignin.spring.data.dynamodb.issue77;
2+
3+
import static org.junit.Assert.*;
4+
5+
import org.junit.Test;
6+
import org.junit.runner.RunWith;
7+
import org.mockito.Mockito;
8+
import org.socialsignin.spring.data.dynamodb.repository.config.EnableDynamoDBRepositories;
9+
import org.springframework.beans.factory.annotation.Autowired;
10+
import org.springframework.context.annotation.Bean;
11+
import org.springframework.context.annotation.ComponentScan;
12+
import org.springframework.context.annotation.Configuration;
13+
import org.springframework.test.context.ContextConfiguration;
14+
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
15+
16+
import com.amazonaws.services.dynamodbv2.AmazonDynamoDB;
17+
import com.amazonaws.services.dynamodbv2.model.UpdateItemResult;
18+
19+
import static org.mockito.Mockito.*;
20+
21+
@RunWith(SpringJUnit4ClassRunner.class)
22+
@ContextConfiguration(classes={Issue77Test.TestAppConfig.class})
23+
public class Issue77Test {
24+
25+
@Configuration
26+
@ComponentScan(basePackageClasses = FooService.class)
27+
@EnableDynamoDBRepositories(basePackages = "org.socialsignin.spring.data.dynamodb.issue77")
28+
public static class TestAppConfig {
29+
30+
@Bean
31+
public AmazonDynamoDB amazonDynamoDB() {
32+
AmazonDynamoDB amazonDynamoDB = Mockito.mock(AmazonDynamoDB.class);
33+
UpdateItemResult result = new UpdateItemResult();
34+
when(amazonDynamoDB.updateItem(any())).thenReturn(result);
35+
36+
return amazonDynamoDB;
37+
}
38+
}
39+
40+
@Autowired
41+
FooService fooService;
42+
@Autowired
43+
FooRepository fooRepository;
44+
45+
@Test
46+
public void testIssue77() {
47+
Foo foo = new Foo();
48+
49+
fooService.processFoo(foo);
50+
51+
assertTrue(fooRepository.isDoNonstandardThingCalled());
52+
assertTrue(fooRepository.isSaveCalled());
53+
}
54+
}

0 commit comments

Comments
 (0)