From 27f46fb12417510f3300dccf4b72287aafd71305 Mon Sep 17 00:00:00 2001 From: Mark Paluch Date: Mon, 30 Jan 2017 11:06:37 +0100 Subject: [PATCH] DATALDAP-21 - Integrate Data Commons Java 8 upgrade branch. --- .../data/ldap/repository/LdapRepository.java | 7 +- .../support/LdapRepositoryFactory.java | 31 +- .../support/QueryDslLdapRepository.java | 15 +- .../support/SimpleLdapRepository.java | 99 ++----- .../repository/SimpleLdapRepositoryTests.java | 267 ++++++++++-------- 5 files changed, 195 insertions(+), 224 deletions(-) diff --git a/src/main/java/org/springframework/data/ldap/repository/LdapRepository.java b/src/main/java/org/springframework/data/ldap/repository/LdapRepository.java index d23e9ef..562b1f4 100644 --- a/src/main/java/org/springframework/data/ldap/repository/LdapRepository.java +++ b/src/main/java/org/springframework/data/ldap/repository/LdapRepository.java @@ -1,5 +1,5 @@ /* - * Copyright 2016 the original author or authors. + * Copyright 2016-2017 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -15,6 +15,8 @@ */ package org.springframework.data.ldap.repository; +import java.util.Optional; + import javax.naming.Name; import org.springframework.data.repository.CrudRepository; @@ -24,6 +26,7 @@ import org.springframework.ldap.query.LdapQuery; * Ldap specific extensions to CrudRepository. * * @author Mattias Hellborg Arthursson + * @author Mark Paluch */ public interface LdapRepository extends CrudRepository { @@ -34,7 +37,7 @@ public interface LdapRepository extends CrudRepository { * @return the found entry or null if no matching entry was found. * @throws org.springframework.dao.IncorrectResultSizeDataAccessException if more than one entry matches the query. */ - T findOne(LdapQuery ldapQuery); + Optional findOne(LdapQuery ldapQuery); /** * Find all entries matching the specified query. diff --git a/src/main/java/org/springframework/data/ldap/repository/support/LdapRepositoryFactory.java b/src/main/java/org/springframework/data/ldap/repository/support/LdapRepositoryFactory.java index 7dcdbc8..eff531a 100644 --- a/src/main/java/org/springframework/data/ldap/repository/support/LdapRepositoryFactory.java +++ b/src/main/java/org/springframework/data/ldap/repository/support/LdapRepositoryFactory.java @@ -1,5 +1,5 @@ /* - * Copyright 2016 the original author or authors. + * Copyright 2016-2017 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -15,16 +15,17 @@ */ package org.springframework.data.ldap.repository.support; -import static org.springframework.data.querydsl.QueryDslUtils.*; +import static org.springframework.data.querydsl.QuerydslUtils.*; import java.io.Serializable; import java.lang.reflect.Method; +import java.util.Optional; import org.springframework.data.ldap.repository.query.AnnotatedLdapRepositoryQuery; import org.springframework.data.ldap.repository.query.LdapQueryMethod; import org.springframework.data.ldap.repository.query.PartTreeLdapRepositoryQuery; import org.springframework.data.projection.ProjectionFactory; -import org.springframework.data.querydsl.QueryDslPredicateExecutor; +import org.springframework.data.querydsl.QuerydslPredicateExecutor; import org.springframework.data.repository.core.EntityInformation; import org.springframework.data.repository.core.NamedQueries; import org.springframework.data.repository.core.RepositoryInformation; @@ -39,7 +40,7 @@ import org.springframework.util.Assert; /** * Factory to create {@link org.springframework.data.ldap.repository.LdapRepository} instances. - * + * * @author Mattias Hellborg Arthursson * @author Eddu Melendez * @author Mark Paluch @@ -76,12 +77,11 @@ public class LdapRepositoryFactory extends RepositoryFactorySupport { */ @Override protected Class getRepositoryBaseClass(RepositoryMetadata metadata) { - return isQueryDslRepository(metadata.getRepositoryInterface()) ? QueryDslLdapRepository.class - : SimpleLdapRepository.class; - } - private static boolean isQueryDslRepository(Class repositoryInterface) { - return QUERY_DSL_PRESENT && QueryDslPredicateExecutor.class.isAssignableFrom(repositoryInterface); + boolean isQueryDslRepository = QUERY_DSL_PRESENT + && QuerydslPredicateExecutor.class.isAssignableFrom(metadata.getRepositoryInterface()); + + return isQueryDslRepository ? QueryDslLdapRepository.class : SimpleLdapRepository.class; } /* @@ -94,20 +94,13 @@ public class LdapRepositoryFactory extends RepositoryFactorySupport { information.getDomainType()); } - /* (non-Javadoc) - * @see org.springframework.data.repository.core.support.RepositoryFactorySupport#getQueryLookupStrategy(org.springframework.data.repository.query.QueryLookupStrategy.Key) - */ - @Override - protected QueryLookupStrategy getQueryLookupStrategy(QueryLookupStrategy.Key key) { - return queryLookupStrategy; - } - /* (non-Javadoc) * @see org.springframework.data.repository.core.support.RepositoryFactorySupport#getQueryLookupStrategy(org.springframework.data.repository.query.QueryLookupStrategy.Key, org.springframework.data.repository.query.EvaluationContextProvider) */ @Override - protected QueryLookupStrategy getQueryLookupStrategy(Key key, EvaluationContextProvider evaluationContextProvider) { - return queryLookupStrategy; + protected Optional getQueryLookupStrategy(Key key, + EvaluationContextProvider evaluationContextProvider) { + return Optional.of(queryLookupStrategy); } private static final class LdapQueryLookupStrategy implements QueryLookupStrategy { diff --git a/src/main/java/org/springframework/data/ldap/repository/support/QueryDslLdapRepository.java b/src/main/java/org/springframework/data/ldap/repository/support/QueryDslLdapRepository.java index 6b10543..22770c1 100644 --- a/src/main/java/org/springframework/data/ldap/repository/support/QueryDslLdapRepository.java +++ b/src/main/java/org/springframework/data/ldap/repository/support/QueryDslLdapRepository.java @@ -1,5 +1,5 @@ /* - * Copyright 2016 the original author or authors. + * Copyright 2016-2017 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -20,7 +20,7 @@ import java.util.List; import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; import org.springframework.data.domain.Sort; -import org.springframework.data.querydsl.QueryDslPredicateExecutor; +import org.springframework.data.querydsl.QuerydslPredicateExecutor; import org.springframework.ldap.core.LdapOperations; import org.springframework.ldap.odm.core.ObjectDirectoryMapper; @@ -32,8 +32,12 @@ import com.querydsl.core.types.Predicate; * * @author Mattias Hellborg Arthursson * @author Eddu Melendez + * @author Mark Paluch */ -public class QueryDslLdapRepository extends SimpleLdapRepository implements QueryDslPredicateExecutor { +public class QueryDslLdapRepository extends SimpleLdapRepository implements QuerydslPredicateExecutor { + + private final LdapOperations ldapOperations; + private final Class entityType; /** * Creates a new {@link QueryDslLdapRepository}. @@ -44,6 +48,9 @@ public class QueryDslLdapRepository extends SimpleLdapRepository implement */ public QueryDslLdapRepository(LdapOperations ldapOperations, ObjectDirectoryMapper odm, Class entityType) { super(ldapOperations, odm, entityType); + + this.ldapOperations = ldapOperations; + this.entityType = entityType; } /* (non-Javadoc) @@ -85,7 +92,7 @@ public class QueryDslLdapRepository extends SimpleLdapRepository implement } private QueryDslLdapQuery queryFor(Predicate predicate) { - return new QueryDslLdapQuery(getLdapOperations(), getEntityType()).where(predicate); + return new QueryDslLdapQuery(ldapOperations, entityType).where(predicate); } /* (non-Javadoc) diff --git a/src/main/java/org/springframework/data/ldap/repository/support/SimpleLdapRepository.java b/src/main/java/org/springframework/data/ldap/repository/support/SimpleLdapRepository.java index 9b210b6..72f9643 100644 --- a/src/main/java/org/springframework/data/ldap/repository/support/SimpleLdapRepository.java +++ b/src/main/java/org/springframework/data/ldap/repository/support/SimpleLdapRepository.java @@ -1,5 +1,5 @@ /* - * Copyright 2016 the original author or authors. + * Copyright 2016-2017 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -17,15 +17,17 @@ package org.springframework.data.ldap.repository.support; import static org.springframework.ldap.query.LdapQueryBuilder.*; -import java.util.Iterator; -import java.util.LinkedList; import java.util.List; +import java.util.Optional; +import java.util.stream.Collectors; +import java.util.stream.StreamSupport; import javax.naming.Name; import org.springframework.dao.EmptyResultDataAccessException; import org.springframework.data.domain.Persistable; import org.springframework.data.ldap.repository.LdapRepository; +import org.springframework.data.util.Optionals; import org.springframework.ldap.NameNotFoundException; import org.springframework.ldap.core.LdapOperations; import org.springframework.ldap.core.support.CountNameClassPairCallbackHandler; @@ -66,14 +68,6 @@ public class SimpleLdapRepository implements LdapRepository { this.entityType = entityType; } - protected LdapOperations getLdapOperations() { - return ldapOperations; - } - - protected Class getEntityType() { - return entityType; - } - /* (non-Javadoc) * @see org.springframework.data.repository.CrudRepository#count() */ @@ -123,26 +117,23 @@ public class SimpleLdapRepository implements LdapRepository { @Override public Iterable save(Iterable entities) { - return new TransformingIterable(entities, new Function() { - @Override - public S transform(S entry) { - return save(entry); - } - }); + return StreamSupport.stream(entities.spliterator(), false) // + .map(this::save) // + .collect(Collectors.toList()); } /* (non-Javadoc) * @see org.springframework.data.repository.CrudRepository#findOne(java.io.Serializable) */ @Override - public T findOne(Name name) { + public Optional findOne(Name name) { Assert.notNull(name, "Id must not be null"); try { - return ldapOperations.findByDn(name, entityType); + return Optional.ofNullable(ldapOperations.findByDn(name, entityType)); } catch (NameNotFoundException e) { - return null; + return Optional.empty(); } } @@ -160,14 +151,14 @@ public class SimpleLdapRepository implements LdapRepository { * @see org.springframework.data.ldap.repository.LdapRepository#findOne(org.springframework.ldap.query.LdapQuery) */ @Override - public T findOne(LdapQuery ldapQuery) { + public Optional findOne(LdapQuery ldapQuery) { Assert.notNull(ldapQuery, "LdapQuery must not be null"); try { - return ldapOperations.findOne(ldapQuery, entityType); + return Optional.ofNullable(ldapOperations.findOne(ldapQuery, entityType)); } catch (EmptyResultDataAccessException e) { - return null; + return Optional.empty(); } } @@ -196,21 +187,10 @@ public class SimpleLdapRepository implements LdapRepository { @Override public List findAll(final Iterable names) { - Iterable found = new TransformingIterable(names, new Function() { - @Override - public T transform(Name name) { - return findOne(name); - } - }); - - LinkedList list = new LinkedList(); - for (T entry : found) { - if (entry != null) { - list.add(entry); - } - } - - return list; + return StreamSupport.stream(names.spliterator(), false) // + .map(this::findOne) // + .flatMap(Optionals::toStream) // + .collect(Collectors.toList()); } /* (non-Javadoc) @@ -240,10 +220,7 @@ public class SimpleLdapRepository implements LdapRepository { */ @Override public void delete(Iterable entities) { - - for (T entity : entities) { - delete(entity); - } + entities.forEach(this::delete); } /* (non-Javadoc) @@ -253,42 +230,4 @@ public class SimpleLdapRepository implements LdapRepository { public void deleteAll() { delete(findAll()); } - - private static final class TransformingIterable implements Iterable { - - private final Iterable target; - private final Function function; - - private TransformingIterable(Iterable target, Function function) { - this.target = target; - this.function = function; - } - - @Override - public Iterator iterator() { - - final Iterator targetIterator = target.iterator(); - return new Iterator() { - - @Override - public boolean hasNext() { - return targetIterator.hasNext(); - } - - @Override - public T next() { - return function.transform(targetIterator.next()); - } - - @Override - public void remove() { - throw new UnsupportedOperationException("Remove is not supported for this iterator"); - } - }; - } - } - - private interface Function { - T transform(F entry); - } } diff --git a/src/test/java/org/springframework/data/ldap/repository/SimpleLdapRepositoryTests.java b/src/test/java/org/springframework/data/ldap/repository/SimpleLdapRepositoryTests.java index 3857c79..4e5e7c1 100644 --- a/src/test/java/org/springframework/data/ldap/repository/SimpleLdapRepositoryTests.java +++ b/src/test/java/org/springframework/data/ldap/repository/SimpleLdapRepositoryTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2016 the original author or authors. + * Copyright 2016-2017 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -13,7 +13,6 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - package org.springframework.data.ldap.repository; import static org.assertj.core.api.Assertions.*; @@ -22,191 +21,221 @@ import static org.mockito.Mockito.*; import java.util.Arrays; import java.util.Iterator; +import java.util.Optional; import javax.naming.Name; import javax.naming.ldap.LdapName; import org.junit.Before; import org.junit.Test; +import org.junit.runner.RunWith; import org.mockito.ArgumentCaptor; +import org.mockito.Mock; +import org.mockito.runners.MockitoJUnitRunner; import org.springframework.data.domain.Persistable; +import org.springframework.data.ldap.repository.support.SimpleLdapRepository; import org.springframework.ldap.NameNotFoundException; import org.springframework.ldap.core.LdapOperations; import org.springframework.ldap.core.support.CountNameClassPairCallbackHandler; import org.springframework.ldap.filter.Filter; import org.springframework.ldap.odm.core.ObjectDirectoryMapper; import org.springframework.ldap.query.LdapQuery; -import org.springframework.data.ldap.repository.support.SimpleLdapRepository; import org.springframework.ldap.support.LdapUtils; /** + * Unit tests for {@link SimpleLdapRepository}. + * * @author Mattias Hellborg Arthursson + * @author Mark Paluch */ +@RunWith(MockitoJUnitRunner.class) public class SimpleLdapRepositoryTests { - private LdapOperations ldapOperationsMock; - private ObjectDirectoryMapper odmMock; - private SimpleLdapRepository tested; + @Mock LdapOperations ldapOperationsMock; + @Mock ObjectDirectoryMapper odmMock; - @Before - public void prepareTestedInstance() { - ldapOperationsMock = mock(LdapOperations.class); - odmMock = mock(ObjectDirectoryMapper.class); - tested = new SimpleLdapRepository(ldapOperationsMock, odmMock, Object.class); - } + SimpleLdapRepository tested; - @Test - public void testCount() { - Filter filterMock = mock(Filter.class); - when(odmMock.filterFor(Object.class, null)).thenReturn(filterMock); - ArgumentCaptor ldapQuery = ArgumentCaptor.forClass(LdapQuery.class); - doNothing().when(ldapOperationsMock).search(ldapQuery.capture(), any(CountNameClassPairCallbackHandler.class)); + @Before + public void prepareTestedInstance() { + tested = new SimpleLdapRepository<>(ldapOperationsMock, odmMock, Object.class); + } - long count = tested.count(); + @Test + public void testCount() { - assertThat(count).isEqualTo(0); - LdapQuery query = ldapQuery.getValue(); - assertThat(query.filter()).isEqualTo(filterMock); - assertThat(query.attributes()).isEqualTo(new String[]{"objectclass"}); - } + Filter filterMock = mock(Filter.class); + when(odmMock.filterFor(Object.class, null)).thenReturn(filterMock); + ArgumentCaptor ldapQuery = ArgumentCaptor.forClass(LdapQuery.class); + doNothing().when(ldapOperationsMock).search(ldapQuery.capture(), any(CountNameClassPairCallbackHandler.class)); - @Test - public void testSaveNonPersistableWithIdSet() { - Object expectedEntity = new Object(); + long count = tested.count(); - when(odmMock.getId(expectedEntity)).thenReturn(LdapUtils.emptyLdapName()); - when(odmMock.getCalculatedId(expectedEntity)).thenReturn(null); + assertThat(count).isEqualTo(0); + LdapQuery query = ldapQuery.getValue(); + assertThat(query.filter()).isEqualTo(filterMock); + assertThat(query.attributes()).isEqualTo(new String[] { "objectclass" }); + } - tested.save(expectedEntity); + @Test + public void testSaveNonPersistableWithIdSet() { - verify(ldapOperationsMock).update(expectedEntity); - } + Object expectedEntity = new Object(); - @Test - public void testSaveNonPersistableWithIdChanged() { - Object expectedEntity = new Object(); - LdapName expectedName = LdapUtils.newLdapName("ou=newlocation"); + when(odmMock.getId(expectedEntity)).thenReturn(LdapUtils.emptyLdapName()); + when(odmMock.getCalculatedId(expectedEntity)).thenReturn(null); - when(odmMock.getId(expectedEntity)).thenReturn(LdapUtils.emptyLdapName()); - when(odmMock.getCalculatedId(expectedEntity)).thenReturn(expectedName); + tested.save(expectedEntity); - tested.save(expectedEntity); + verify(ldapOperationsMock).update(expectedEntity); + } - verify(ldapOperationsMock).update(expectedEntity); - } + @Test + public void testSaveNonPersistableWithIdChanged() { - @Test - public void testSaveNonPersistableWithNoIdCalculatedId() { - Object expectedEntity = new Object(); - LdapName expectedName = LdapUtils.emptyLdapName(); + Object expectedEntity = new Object(); + LdapName expectedName = LdapUtils.newLdapName("ou=newlocation"); - when(odmMock.getId(expectedEntity)).thenReturn(null); - when(odmMock.getCalculatedId(expectedEntity)).thenReturn(expectedName); + when(odmMock.getId(expectedEntity)).thenReturn(LdapUtils.emptyLdapName()); + when(odmMock.getCalculatedId(expectedEntity)).thenReturn(expectedName); - tested.save(expectedEntity); + tested.save(expectedEntity); - verify(ldapOperationsMock).create(expectedEntity); - } + verify(ldapOperationsMock).update(expectedEntity); + } - @Test - public void testSavePersistableNewWithDeclaredId() { - Persistable expectedEntity = mock(Persistable.class); + @Test + public void testSaveNonPersistableWithNoIdCalculatedId() { - when(expectedEntity.isNew()).thenReturn(true); - when(odmMock.getId(expectedEntity)).thenReturn(LdapUtils.emptyLdapName()); - when(odmMock.getCalculatedId(expectedEntity)).thenReturn(null); + Object expectedEntity = new Object(); + LdapName expectedName = LdapUtils.emptyLdapName(); - tested.save(expectedEntity); + when(odmMock.getId(expectedEntity)).thenReturn(null); + when(odmMock.getCalculatedId(expectedEntity)).thenReturn(expectedName); - verify(ldapOperationsMock).create(expectedEntity); - } + tested.save(expectedEntity); - @Test - public void testSavePersistableNewWithCalculatedId() { - Persistable expectedEntity = mock(Persistable.class); - LdapName expectedName = LdapUtils.emptyLdapName(); + verify(ldapOperationsMock).create(expectedEntity); + } - when(expectedEntity.isNew()).thenReturn(true); - when(odmMock.getId(expectedEntity)).thenReturn(null); - when(odmMock.getCalculatedId(expectedEntity)).thenReturn(expectedName); + @Test + public void testSavePersistableNewWithDeclaredId() { - tested.save(expectedEntity); + Persistable expectedEntity = mock(Persistable.class); - verify(ldapOperationsMock).create(expectedEntity); - } + when(expectedEntity.isNew()).thenReturn(true); + when(odmMock.getId(expectedEntity)).thenReturn(LdapUtils.emptyLdapName()); + when(odmMock.getCalculatedId(expectedEntity)).thenReturn(null); - @Test - public void testSavePersistableNotNew() { - Persistable expectedEntity = mock(Persistable.class); + tested.save(expectedEntity); - when(expectedEntity.isNew()).thenReturn(false); - when(odmMock.getId(expectedEntity)).thenReturn(LdapUtils.emptyLdapName()); - when(odmMock.getCalculatedId(expectedEntity)).thenReturn(null); + verify(ldapOperationsMock).create(expectedEntity); + } - tested.save(expectedEntity); + @Test + public void testSavePersistableNewWithCalculatedId() { - verify(ldapOperationsMock).update(expectedEntity); - } + Persistable expectedEntity = mock(Persistable.class); + LdapName expectedName = LdapUtils.emptyLdapName(); - @Test - public void testFindOneWithName() { - LdapName expectedName = LdapUtils.emptyLdapName(); - Object expectedResult = new Object(); + when(expectedEntity.isNew()).thenReturn(true); + when(odmMock.getId(expectedEntity)).thenReturn(null); + when(odmMock.getCalculatedId(expectedEntity)).thenReturn(expectedName); - when(ldapOperationsMock.findByDn(expectedName, Object.class)).thenReturn(expectedResult); + tested.save(expectedEntity); - Object actualResult = tested.findOne(expectedName); + verify(ldapOperationsMock).create(expectedEntity); + } - assertThat(actualResult).isSameAs(expectedResult); - } + @Test + public void testSavePersistableNotNew() { - @Test - public void verifyThatNameNotFoundInFindOneWithNameReturnsNull() { - LdapName expectedName = LdapUtils.emptyLdapName(); + Persistable expectedEntity = mock(Persistable.class); - when(ldapOperationsMock.findByDn(expectedName, Object.class)).thenThrow(new NameNotFoundException("")); + when(expectedEntity.isNew()).thenReturn(false); + when(odmMock.getId(expectedEntity)).thenReturn(LdapUtils.emptyLdapName()); + when(odmMock.getCalculatedId(expectedEntity)).thenReturn(null); - Object actualResult = tested.findOne(expectedName); + tested.save(expectedEntity); - assertThat(actualResult).isNull(); - } + verify(ldapOperationsMock).update(expectedEntity); + } - @Test - public void testFindAll() { - Name expectedName1 = LdapUtils.newLdapName("ou=aa"); - Name expectedName2 = LdapUtils.newLdapName("ou=bb"); + @Test + public void testFindOneWithName() { - Object expectedResult1 = new Object(); - Object expectedResult2 = new Object(); + LdapName expectedName = LdapUtils.emptyLdapName(); + Object expectedResult = new Object(); - when(ldapOperationsMock.findByDn(expectedName1, Object.class)).thenReturn(expectedResult1); - when(ldapOperationsMock.findByDn(expectedName2, Object.class)).thenReturn(expectedResult2); + when(ldapOperationsMock.findByDn(expectedName, Object.class)).thenReturn(expectedResult); - Iterable actualResult = tested.findAll(Arrays.asList(expectedName1, expectedName2)); + Optional actualResult = tested.findOne(expectedName); - Iterator iterator = actualResult.iterator(); - assertThat(iterator.next()).isSameAs(expectedResult1); - assertThat(iterator.next()).isSameAs(expectedResult2); + assertThat(actualResult).contains(expectedResult); + } - assertThat(iterator.hasNext()).isFalse(); - } + @Test // DATALDAP-21 + public void verifyThatNameNotFoundInFindOneWithNameReturnsEmptyOptional() { - @Test - public void testFindAllWhereOneEntryIsNotFound() { - Name expectedName1 = LdapUtils.newLdapName("ou=aa"); - Name expectedName2 = LdapUtils.newLdapName("ou=bb"); + LdapName expectedName = LdapUtils.emptyLdapName(); - Object expectedResult2 = new Object(); + when(ldapOperationsMock.findByDn(expectedName, Object.class)).thenThrow(new NameNotFoundException("")); - when(ldapOperationsMock.findByDn(expectedName1, Object.class)).thenReturn(null); - when(ldapOperationsMock.findByDn(expectedName2, Object.class)).thenReturn(expectedResult2); + Optional actualResult = tested.findOne(expectedName); - Iterable actualResult = tested.findAll(Arrays.asList(expectedName1, expectedName2)); + assertThat(actualResult).isNotPresent(); + } - Iterator iterator = actualResult.iterator(); - assertThat(iterator.next()).isSameAs(expectedResult2); + @Test // DATALDAP-21 + public void verifyThatNoResultFoundInFindOneWithNameReturnsEmptyOptional() { - assertThat(iterator.hasNext()).isFalse(); - } + LdapName expectedName = LdapUtils.emptyLdapName(); + + when(ldapOperationsMock.findByDn(expectedName, Object.class)).thenReturn(null); + + Optional actualResult = tested.findOne(expectedName); + + assertThat(actualResult).isNotPresent(); + } + + @Test + public void testFindAll() { + + Name expectedName1 = LdapUtils.newLdapName("ou=aa"); + Name expectedName2 = LdapUtils.newLdapName("ou=bb"); + + Object expectedResult1 = new Object(); + Object expectedResult2 = new Object(); + + when(ldapOperationsMock.findByDn(expectedName1, Object.class)).thenReturn(expectedResult1); + when(ldapOperationsMock.findByDn(expectedName2, Object.class)).thenReturn(expectedResult2); + + Iterable actualResult = tested.findAll(Arrays.asList(expectedName1, expectedName2)); + + Iterator iterator = actualResult.iterator(); + assertThat(iterator.next()).isSameAs(expectedResult1); + assertThat(iterator.next()).isSameAs(expectedResult2); + + assertThat(iterator.hasNext()).isFalse(); + } + + @Test + public void testFindAllWhereOneEntryIsNotFound() { + + Name expectedName1 = LdapUtils.newLdapName("ou=aa"); + Name expectedName2 = LdapUtils.newLdapName("ou=bb"); + + Object expectedResult2 = new Object(); + + when(ldapOperationsMock.findByDn(expectedName1, Object.class)).thenReturn(null); + when(ldapOperationsMock.findByDn(expectedName2, Object.class)).thenReturn(expectedResult2); + + Iterable actualResult = tested.findAll(Arrays.asList(expectedName1, expectedName2)); + + Iterator iterator = actualResult.iterator(); + assertThat(iterator.next()).isSameAs(expectedResult2); + + assertThat(iterator.hasNext()).isFalse(); + } }