Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Creating test case for #77 #102

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package org.socialsignin.spring.data.dynamodb.issue77;

import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBHashKey;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBTable;

@DynamoDBTable(tableName = "foo")
public class Foo {

private long id;

@DynamoDBHashKey(attributeName = "Id")
public long getId() {
return id;
}

public void setId(long id) {
this.id = id;
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package org.socialsignin.spring.data.dynamodb.issue77;

import org.springframework.data.repository.CrudRepository;

public interface FooRepository extends CrudRepository<Foo, Long>, FooRepositoryCustom {

// Workaround as per DATACMNS-1008
//<X extends Foo> X save(X foo);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package org.socialsignin.spring.data.dynamodb.issue77;

public interface FooRepositoryCustom {
Foo doNonstandardThing(Foo foo);

// just here for testing...
boolean isDoNonstandardThingCalled();
boolean isSaveCalled();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package org.socialsignin.spring.data.dynamodb.issue77;

public class FooRepositoryImpl implements FooRepositoryCustom {

private ThreadLocal<Boolean> saveCalled = new ThreadLocal<>();
private ThreadLocal<Boolean> doNonstandardThingCalled = new ThreadLocal<>();

public FooRepositoryImpl() {
saveCalled.set(false);
doNonstandardThingCalled.set(false);
}

public Foo save(Foo foo) {
// Do things
saveCalled.set(true);
return foo;
}

public Foo doNonstandardThing(Foo foo) {
doNonstandardThingCalled.set(true);
return foo;
}

public boolean isDoNonstandardThingCalled() {
return doNonstandardThingCalled.get();
}

public boolean isSaveCalled() {
return saveCalled.get();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package org.socialsignin.spring.data.dynamodb.issue77;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class FooService {
private final FooRepository fooRepo;

@Autowired
public FooService(FooRepository fooRepo) {
this.fooRepo = fooRepo;
}

public void processFoo(Foo foo) {
fooRepo.doNonstandardThing(foo);
fooRepo.save(foo);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package org.socialsignin.spring.data.dynamodb.issue77;

import static org.junit.Assert.*;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.socialsignin.spring.data.dynamodb.repository.config.EnableDynamoDBRepositories;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.amazonaws.services.dynamodbv2.AmazonDynamoDB;
import com.amazonaws.services.dynamodbv2.model.UpdateItemResult;

import static org.mockito.Mockito.*;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes={Issue77Test.TestAppConfig.class})
public class Issue77Test {

@Configuration
@ComponentScan(basePackageClasses = FooService.class)
@EnableDynamoDBRepositories(basePackages = "org.socialsignin.spring.data.dynamodb.issue77")
public static class TestAppConfig {

@Bean
public AmazonDynamoDB amazonDynamoDB() {
AmazonDynamoDB amazonDynamoDB = Mockito.mock(AmazonDynamoDB.class);
UpdateItemResult result = new UpdateItemResult();
when(amazonDynamoDB.updateItem(any())).thenReturn(result);

return amazonDynamoDB;
}
}

@Autowired
FooService fooService;
@Autowired
FooRepository fooRepository;

@Test
public void testIssue77() {
Foo foo = new Foo();

fooService.processFoo(foo);

assertTrue(fooRepository.isDoNonstandardThingCalled());
assertTrue(fooRepository.isSaveCalled());
}
}