DATAGRAPH-1260 - Align ID assertion logic in all variants of findById().

This commit is contained in:
Michael Simons
2020-08-20 14:46:10 +02:00
committed by GitHub
parent d1d6c80ba4
commit e792b315b2
2 changed files with 22 additions and 0 deletions

View File

@@ -43,6 +43,7 @@ import org.springframework.util.Assert;
* @author Mark Paluch
* @author Jens Schauder
* @author Gerrit Meier
* @author Michael J. Simons
*/
@Repository
@Transactional(readOnly = true)
@@ -140,6 +141,7 @@ public class SimpleNeo4jRepository<T, ID extends Serializable> implements Neo4jR
@Override
public Optional<T> findById(ID id, int depth) {
Assert.notNull(id, ID_MUST_NOT_BE_NULL);
return Optional.ofNullable(session.load(clazz, id, depth));
}

View File

@@ -28,6 +28,10 @@ import org.neo4j.ogm.session.Session;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
/**
* @author Gerrit Meier
* @author Michael J. Simons
*/
public class SimpleNeo4jRepositoryTests {
private final Session sessionMock = mock(Session.class);
@@ -136,6 +140,22 @@ public class SimpleNeo4jRepositoryTests {
assertThat(page.getTotalElements()).isEqualTo(amountOfElementsInDatabase);
}
@Test // DATAGRAPH-1260
public void idShouldBeAsserted() {
assertThatIllegalArgumentException()
.isThrownBy(() -> repository.findById(null))
.withMessage("The given id must not be null!");
}
@Test // DATAGRAPH-1260
public void idShouldBeAssertedWithDepth() {
assertThatIllegalArgumentException()
.isThrownBy(() -> repository.findById(null, 42))
.withMessage("The given id must not be null!");
}
private Page loadPage(PageRequest requestedPage, long amountOfElementsInDatabase) {
prepareSessionMock(requestedPage, amountOfElementsInDatabase);
return repository.findAll(requestedPage);