DATALDAP-21 - Integrate Data Commons Java 8 upgrade branch.
This commit is contained in:
@@ -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<T> extends CrudRepository<T, Name> {
|
||||
|
||||
@@ -34,7 +37,7 @@ public interface LdapRepository<T> extends CrudRepository<T, Name> {
|
||||
* @return the found entry or <code>null</code> if no matching entry was found.
|
||||
* @throws org.springframework.dao.IncorrectResultSizeDataAccessException if more than one entry matches the query.
|
||||
*/
|
||||
T findOne(LdapQuery ldapQuery);
|
||||
Optional<T> findOne(LdapQuery ldapQuery);
|
||||
|
||||
/**
|
||||
* Find all entries matching the specified query.
|
||||
|
||||
@@ -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<QueryLookupStrategy> getQueryLookupStrategy(Key key,
|
||||
EvaluationContextProvider evaluationContextProvider) {
|
||||
return Optional.of(queryLookupStrategy);
|
||||
}
|
||||
|
||||
private static final class LdapQueryLookupStrategy implements QueryLookupStrategy {
|
||||
|
||||
@@ -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<T> extends SimpleLdapRepository<T> implements QueryDslPredicateExecutor<T> {
|
||||
public class QueryDslLdapRepository<T> extends SimpleLdapRepository<T> implements QuerydslPredicateExecutor<T> {
|
||||
|
||||
private final LdapOperations ldapOperations;
|
||||
private final Class<T> entityType;
|
||||
|
||||
/**
|
||||
* Creates a new {@link QueryDslLdapRepository}.
|
||||
@@ -44,6 +48,9 @@ public class QueryDslLdapRepository<T> extends SimpleLdapRepository<T> implement
|
||||
*/
|
||||
public QueryDslLdapRepository(LdapOperations ldapOperations, ObjectDirectoryMapper odm, Class<T> entityType) {
|
||||
super(ldapOperations, odm, entityType);
|
||||
|
||||
this.ldapOperations = ldapOperations;
|
||||
this.entityType = entityType;
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
@@ -85,7 +92,7 @@ public class QueryDslLdapRepository<T> extends SimpleLdapRepository<T> implement
|
||||
}
|
||||
|
||||
private QueryDslLdapQuery<T> queryFor(Predicate predicate) {
|
||||
return new QueryDslLdapQuery<T>(getLdapOperations(), getEntityType()).where(predicate);
|
||||
return new QueryDslLdapQuery<T>(ldapOperations, entityType).where(predicate);
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
|
||||
@@ -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<T> implements LdapRepository<T> {
|
||||
this.entityType = entityType;
|
||||
}
|
||||
|
||||
protected LdapOperations getLdapOperations() {
|
||||
return ldapOperations;
|
||||
}
|
||||
|
||||
protected Class<T> getEntityType() {
|
||||
return entityType;
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.springframework.data.repository.CrudRepository#count()
|
||||
*/
|
||||
@@ -123,26 +117,23 @@ public class SimpleLdapRepository<T> implements LdapRepository<T> {
|
||||
@Override
|
||||
public <S extends T> Iterable<S> save(Iterable<S> entities) {
|
||||
|
||||
return new TransformingIterable<S, S>(entities, new Function<S, S>() {
|
||||
@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<T> 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<T> implements LdapRepository<T> {
|
||||
* @see org.springframework.data.ldap.repository.LdapRepository#findOne(org.springframework.ldap.query.LdapQuery)
|
||||
*/
|
||||
@Override
|
||||
public T findOne(LdapQuery ldapQuery) {
|
||||
public Optional<T> 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<T> implements LdapRepository<T> {
|
||||
@Override
|
||||
public List<T> findAll(final Iterable<Name> names) {
|
||||
|
||||
Iterable<T> found = new TransformingIterable<Name, T>(names, new Function<Name, T>() {
|
||||
@Override
|
||||
public T transform(Name name) {
|
||||
return findOne(name);
|
||||
}
|
||||
});
|
||||
|
||||
LinkedList<T> list = new LinkedList<T>();
|
||||
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<T> implements LdapRepository<T> {
|
||||
*/
|
||||
@Override
|
||||
public void delete(Iterable<? extends T> entities) {
|
||||
|
||||
for (T entity : entities) {
|
||||
delete(entity);
|
||||
}
|
||||
entities.forEach(this::delete);
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
@@ -253,42 +230,4 @@ public class SimpleLdapRepository<T> implements LdapRepository<T> {
|
||||
public void deleteAll() {
|
||||
delete(findAll());
|
||||
}
|
||||
|
||||
private static final class TransformingIterable<F, T> implements Iterable<T> {
|
||||
|
||||
private final Iterable<F> target;
|
||||
private final Function<F, T> function;
|
||||
|
||||
private TransformingIterable(Iterable<F> target, Function<F, T> function) {
|
||||
this.target = target;
|
||||
this.function = function;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Iterator<T> iterator() {
|
||||
|
||||
final Iterator<F> targetIterator = target.iterator();
|
||||
return new Iterator<T>() {
|
||||
|
||||
@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<F, T> {
|
||||
T transform(F entry);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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<Object> tested;
|
||||
@Mock LdapOperations ldapOperationsMock;
|
||||
@Mock ObjectDirectoryMapper odmMock;
|
||||
|
||||
@Before
|
||||
public void prepareTestedInstance() {
|
||||
ldapOperationsMock = mock(LdapOperations.class);
|
||||
odmMock = mock(ObjectDirectoryMapper.class);
|
||||
tested = new SimpleLdapRepository<Object>(ldapOperationsMock, odmMock, Object.class);
|
||||
}
|
||||
SimpleLdapRepository<Object> tested;
|
||||
|
||||
@Test
|
||||
public void testCount() {
|
||||
Filter filterMock = mock(Filter.class);
|
||||
when(odmMock.filterFor(Object.class, null)).thenReturn(filterMock);
|
||||
ArgumentCaptor<LdapQuery> 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> 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<Object> actualResult = tested.findAll(Arrays.asList(expectedName1, expectedName2));
|
||||
Optional<Object> actualResult = tested.findOne(expectedName);
|
||||
|
||||
Iterator<Object> 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<Object> actualResult = tested.findOne(expectedName);
|
||||
|
||||
Iterable<Object> actualResult = tested.findAll(Arrays.asList(expectedName1, expectedName2));
|
||||
assertThat(actualResult).isNotPresent();
|
||||
}
|
||||
|
||||
Iterator<Object> 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<Object> 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<Object> actualResult = tested.findAll(Arrays.asList(expectedName1, expectedName2));
|
||||
|
||||
Iterator<Object> 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<Object> actualResult = tested.findAll(Arrays.asList(expectedName1, expectedName2));
|
||||
|
||||
Iterator<Object> iterator = actualResult.iterator();
|
||||
assertThat(iterator.next()).isSameAs(expectedResult2);
|
||||
|
||||
assertThat(iterator.hasNext()).isFalse();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user