Remove Spring Data
Fixes gh-436
This commit is contained in:
@@ -14,8 +14,7 @@ dependencies {
|
||||
"org.springframework:spring-tx:$springVersion",
|
||||
"org.slf4j:slf4j-api:$slf4jVersion"
|
||||
|
||||
optional "org.springframework.data:spring-data-commons:$springDataVersion",
|
||||
"org.springframework:spring-context:$springVersion",
|
||||
optional "org.springframework:spring-context:$springVersion",
|
||||
"org.springframework:spring-jdbc:$springVersion",
|
||||
"org.springframework:spring-orm:$springVersion",
|
||||
"com.querydsl:querydsl-core:$queryDslVersion",
|
||||
|
||||
@@ -17,28 +17,16 @@
|
||||
package org.springframework.ldap.config;
|
||||
|
||||
import org.springframework.beans.factory.xml.NamespaceHandlerSupport;
|
||||
import org.springframework.data.repository.config.RepositoryBeanDefinitionParser;
|
||||
import org.springframework.ldap.repository.config.LdapRepositoryConfigurationExtension;
|
||||
import org.springframework.util.ClassUtils;
|
||||
|
||||
/**
|
||||
* @author Mattias Hellborg Arthursson
|
||||
* @author Rob Winch
|
||||
*/
|
||||
public class LdapNamespaceHandler extends NamespaceHandlerSupport {
|
||||
static final String REPOSITORY_CLASS_NAME = "org.springframework.data.repository.config.RepositoryConfigurationExtension";
|
||||
|
||||
@Override
|
||||
public void init() {
|
||||
LdapRepositoryConfigurationExtension extension = new LdapRepositoryConfigurationExtension();
|
||||
|
||||
registerBeanDefinitionParser(Elements.CONTEXT_SOURCE, new ContextSourceParser());
|
||||
registerBeanDefinitionParser(Elements.LDAP_TEMPLATE, new LdapTemplateParser());
|
||||
registerBeanDefinitionParser(Elements.TRANSACTION_MANAGER, new TransactionManagerParser());
|
||||
|
||||
if (ClassUtils.isPresent(REPOSITORY_CLASS_NAME, getClass().getClassLoader())) {
|
||||
RepositoryBeanDefinitionParser repositoryParser = new RepositoryBeanDefinitionParser(extension);
|
||||
registerBeanDefinitionParser(Elements.REPOSITORIES, repositoryParser);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,47 +0,0 @@
|
||||
/*
|
||||
* Copyright 2005-2013 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.ldap.repository;
|
||||
|
||||
import org.springframework.data.repository.CrudRepository;
|
||||
import org.springframework.ldap.query.LdapQuery;
|
||||
|
||||
import javax.naming.Name;
|
||||
|
||||
/**
|
||||
* Ldap specific extensions to CrudRepository.
|
||||
*
|
||||
* @author Mattias Hellborg Arthursson
|
||||
* @since 2.0
|
||||
*/
|
||||
public interface LdapRepository<T> extends CrudRepository<T, Name> {
|
||||
/**
|
||||
* Find one entry matching the specified query.
|
||||
*
|
||||
* @param ldapQuery the query specification.
|
||||
* @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);
|
||||
|
||||
/**
|
||||
* Find all entries matching the specified query.
|
||||
*
|
||||
* @param ldapQuery the query specification.
|
||||
* @return the entries matching the query.
|
||||
*/
|
||||
Iterable<T> findAll(LdapQuery ldapQuery);
|
||||
}
|
||||
@@ -1,68 +0,0 @@
|
||||
/*
|
||||
* Copyright 2005-2013 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.ldap.repository;
|
||||
|
||||
import org.springframework.ldap.query.SearchScope;
|
||||
|
||||
import java.lang.annotation.Documented;
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
/**
|
||||
* Annotation for use in {@link org.springframework.ldap.repository.LdapRepository} declarations
|
||||
* to create automatic query methods based on statically defined queries.
|
||||
*
|
||||
* @author Mattias Hellborg Arthursson
|
||||
* @since 2.0
|
||||
*/
|
||||
@Target(ElementType.METHOD)
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Documented
|
||||
public @interface Query {
|
||||
/**
|
||||
* Search base, to be used as input to {@link org.springframework.ldap.query.LdapQueryBuilder#base(javax.naming.Name)}.
|
||||
*
|
||||
* @return the search base, default is {@link org.springframework.ldap.support.LdapUtils#emptyLdapName()}
|
||||
*/
|
||||
String base() default "";
|
||||
/**
|
||||
* The filter format string, to be used as input to {@link org.springframework.ldap.query.LdapQueryBuilder#filter(String, Object...)}.
|
||||
*
|
||||
* @return search filter, must be specified.
|
||||
*/
|
||||
String value() default "";
|
||||
/**
|
||||
* Search scope, to be used as input to {@link org.springframework.ldap.query.LdapQueryBuilder#searchScope(org.springframework.ldap.query.SearchScope)}.
|
||||
*
|
||||
* @return the search scope.
|
||||
*/
|
||||
SearchScope searchScope() default SearchScope.SUBTREE;
|
||||
/**
|
||||
* Time limit, to be used as input to {@link org.springframework.ldap.query.LdapQueryBuilder#timeLimit(int)}.
|
||||
*
|
||||
* @return the time limit.
|
||||
*/
|
||||
int timeLimit() default 0;
|
||||
/**
|
||||
* Count limit, to be used as input to {@link org.springframework.ldap.query.LdapQueryBuilder#countLimit(int)}.
|
||||
*
|
||||
* @return the count limit.
|
||||
*/
|
||||
int countLimit() default 0;
|
||||
}
|
||||
@@ -1,113 +0,0 @@
|
||||
/*
|
||||
* Copyright 2005-2013 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.ldap.repository.config;
|
||||
|
||||
import org.springframework.context.annotation.ComponentScan.Filter;
|
||||
import org.springframework.context.annotation.Import;
|
||||
import org.springframework.data.repository.query.QueryLookupStrategy.Key;
|
||||
import org.springframework.ldap.repository.support.LdapRepositoryFactoryBean;
|
||||
|
||||
import java.lang.annotation.Documented;
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Inherited;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
/**
|
||||
* Annotation to activate Ldap repositories. If no base package is configured through either {@link #value()},
|
||||
* {@link #basePackages()} or {@link #basePackageClasses()} it will trigger scanning of the package of annotated class.
|
||||
*
|
||||
* @author Mattias Hellborg Arthursson
|
||||
* @since 2.0
|
||||
*/
|
||||
@Target(ElementType.TYPE)
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Documented
|
||||
@Inherited
|
||||
@Import(LdapRepositoriesRegistrar.class)
|
||||
public @interface EnableLdapRepositories {
|
||||
|
||||
/**
|
||||
* Alias for the {@link #basePackages()} attribute. Allows for more concise annotation declarations e.g.:
|
||||
* {@code @EnableLdapRepositories("org.my.pkg")} instead of {@code @EnableLdapRepositories(basePackages="org.my.pkg")}.
|
||||
*/
|
||||
String[] value() default {};
|
||||
|
||||
/**
|
||||
* Base packages to scan for annotated components. {@link #value()} is an alias for (and mutually exclusive with) this
|
||||
* attribute. Use {@link #basePackageClasses()} for a type-safe alternative to String-based package names.
|
||||
*/
|
||||
String[] basePackages() default {};
|
||||
|
||||
/**
|
||||
* Type-safe alternative to {@link #basePackages()} for specifying the packages to scan for annotated components. The
|
||||
* package of each class specified will be scanned. Consider creating a special no-op marker class or interface in
|
||||
* each package that serves no purpose other than being referenced by this attribute.
|
||||
*/
|
||||
Class<?>[] basePackageClasses() default {};
|
||||
|
||||
/**
|
||||
* Specifies which types are eligible for component scanning. Further narrows the set of candidate components from
|
||||
* everything in {@link #basePackages()} to everything in the base packages that matches the given filter or filters.
|
||||
*/
|
||||
Filter[] includeFilters() default {};
|
||||
|
||||
/**
|
||||
* Specifies which types are not eligible for component scanning.
|
||||
*/
|
||||
Filter[] excludeFilters() default {};
|
||||
|
||||
/**
|
||||
* Returns the postfix to be used when looking up custom repository implementations. Defaults to {@literal Impl}. So
|
||||
* for a repository named {@code PersonRepository} the corresponding implementation class will be looked up scanning
|
||||
* for {@code PersonRepositoryImpl}.
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
String repositoryImplementationPostfix() default "";
|
||||
|
||||
/**
|
||||
* Configures the location of where to find the Spring Data named queries properties file. Will default to
|
||||
* {@code META-INFO/mongo-named-queries.properties}.
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
String namedQueriesLocation() default "";
|
||||
|
||||
/**
|
||||
* Returns the key of the {@link org.springframework.data.repository.query.QueryLookupStrategy} to be used for lookup queries for query methods. Defaults to
|
||||
* {@link org.springframework.data.repository.query.QueryLookupStrategy.Key#CREATE_IF_NOT_FOUND}.
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
Key queryLookupStrategy() default Key.CREATE_IF_NOT_FOUND;
|
||||
|
||||
/**
|
||||
* Returns the {@link org.springframework.beans.factory.FactoryBean} class to be used for each repository instance. Defaults to
|
||||
* {@link org.springframework.ldap.repository.support.LdapRepositoryFactoryBean}.
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
Class<?> repositoryFactoryBeanClass() default LdapRepositoryFactoryBean.class;
|
||||
|
||||
/**
|
||||
* Configures the name of the {@link org.springframework.ldap.core.LdapTemplate} bean to be used with the repositories detected.
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
String ldapTemplateRef() default "ldapTemplate";
|
||||
}
|
||||
@@ -1,40 +0,0 @@
|
||||
/*
|
||||
* Copyright 2005-2013 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.ldap.repository.config;
|
||||
|
||||
import org.springframework.data.repository.config.RepositoryBeanDefinitionRegistrarSupport;
|
||||
import org.springframework.data.repository.config.RepositoryConfigurationExtension;
|
||||
|
||||
import java.lang.annotation.Annotation;
|
||||
|
||||
/**
|
||||
* LDAP-specific {@link org.springframework.context.annotation.ImportBeanDefinitionRegistrar}.
|
||||
*
|
||||
* @author Mattias Hellborg Arthursson
|
||||
* @since 2.0
|
||||
*/
|
||||
class LdapRepositoriesRegistrar extends RepositoryBeanDefinitionRegistrarSupport {
|
||||
|
||||
@Override
|
||||
protected Class<? extends Annotation> getAnnotation() {
|
||||
return EnableLdapRepositories.class;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected RepositoryConfigurationExtension getExtension() {
|
||||
return new LdapRepositoryConfigurationExtension();
|
||||
}
|
||||
}
|
||||
@@ -1,63 +0,0 @@
|
||||
/*
|
||||
* Copyright 2005-2013 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.ldap.repository.config;
|
||||
|
||||
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
|
||||
import org.springframework.core.annotation.AnnotationAttributes;
|
||||
import org.springframework.data.repository.config.AnnotationRepositoryConfigurationSource;
|
||||
import org.springframework.data.repository.config.RepositoryConfigurationExtensionSupport;
|
||||
import org.springframework.data.repository.config.XmlRepositoryConfigurationSource;
|
||||
import org.springframework.ldap.repository.support.LdapRepositoryFactoryBean;
|
||||
import org.springframework.util.StringUtils;
|
||||
import org.w3c.dom.Element;
|
||||
|
||||
/**
|
||||
* @author Mattias Hellborg Arthursson
|
||||
* @since 2.0
|
||||
*/
|
||||
public class LdapRepositoryConfigurationExtension extends RepositoryConfigurationExtensionSupport {
|
||||
|
||||
private static final String ATT_LDAP_TEMPLATE_REF = "ldap-template-ref";
|
||||
|
||||
@Override
|
||||
protected String getModulePrefix() {
|
||||
return "ldap";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getRepositoryFactoryClassName() {
|
||||
return LdapRepositoryFactoryBean.class.getName();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void postProcess(BeanDefinitionBuilder builder, XmlRepositoryConfigurationSource config) {
|
||||
Element element = config.getElement();
|
||||
String ldapTemplateRef = element.getAttribute(ATT_LDAP_TEMPLATE_REF);
|
||||
if(!StringUtils.hasText(ldapTemplateRef)) {
|
||||
ldapTemplateRef = "ldapTemplate";
|
||||
}
|
||||
|
||||
builder.addPropertyReference("ldapOperations", ldapTemplateRef);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void postProcess(BeanDefinitionBuilder builder, AnnotationRepositoryConfigurationSource config) {
|
||||
AnnotationAttributes attributes = config.getAttributes();
|
||||
|
||||
builder.addPropertyReference("ldapOperations", attributes.getString("ldapTemplateRef"));
|
||||
}
|
||||
}
|
||||
@@ -1,67 +0,0 @@
|
||||
/*
|
||||
* Copyright 2005-2013 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.ldap.repository.query;
|
||||
|
||||
import org.springframework.dao.EmptyResultDataAccessException;
|
||||
import org.springframework.data.repository.query.QueryMethod;
|
||||
import org.springframework.data.repository.query.RepositoryQuery;
|
||||
import org.springframework.ldap.core.LdapOperations;
|
||||
import org.springframework.ldap.query.LdapQuery;
|
||||
|
||||
/**
|
||||
* @author Mattias Hellborg Arthursson
|
||||
* @since 2.0
|
||||
*/
|
||||
abstract class AbstractLdapRepositoryQuery implements RepositoryQuery {
|
||||
private final LdapQueryMethod queryMethod;
|
||||
private final Class<?> clazz;
|
||||
private final LdapOperations ldapOperations;
|
||||
|
||||
public AbstractLdapRepositoryQuery(LdapQueryMethod queryMethod,
|
||||
Class<?> clazz,
|
||||
LdapOperations ldapOperations) {
|
||||
this.queryMethod = queryMethod;
|
||||
this.clazz = clazz;
|
||||
this.ldapOperations = ldapOperations;
|
||||
}
|
||||
|
||||
@Override
|
||||
public final Object execute(Object[] parameters) {
|
||||
LdapQuery query = createQuery(parameters);
|
||||
|
||||
if(queryMethod.isCollectionQuery()) {
|
||||
return ldapOperations.find(query, clazz);
|
||||
} else {
|
||||
try {
|
||||
return ldapOperations.findOne(query, clazz);
|
||||
} catch (EmptyResultDataAccessException e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected abstract LdapQuery createQuery(Object[] parameters);
|
||||
|
||||
Class<?> getClazz() {
|
||||
return clazz;
|
||||
}
|
||||
|
||||
@Override
|
||||
public final QueryMethod getQueryMethod() {
|
||||
return queryMethod;
|
||||
}
|
||||
}
|
||||
@@ -1,58 +0,0 @@
|
||||
/*
|
||||
* Copyright 2005-2013 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.ldap.repository.query;
|
||||
|
||||
import org.springframework.ldap.core.LdapOperations;
|
||||
import org.springframework.ldap.query.LdapQuery;
|
||||
import org.springframework.ldap.repository.Query;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
import static org.springframework.ldap.query.LdapQueryBuilder.query;
|
||||
|
||||
/**
|
||||
* Handles queries for repository methods annotated with {@link org.springframework.ldap.repository.Query}.
|
||||
*
|
||||
* @author Mattias Hellborg Arthursson
|
||||
* @since 2.0
|
||||
*/
|
||||
public class AnnotatedLdapRepositoryQuery extends AbstractLdapRepositoryQuery {
|
||||
private final Query queryAnnotation;
|
||||
|
||||
/**
|
||||
* Construct a new instance.
|
||||
* @param queryMethod the QueryMethod.
|
||||
* @param clazz the managed class.
|
||||
* @param ldapOperations the LdapOperations instance to use.
|
||||
*/
|
||||
public AnnotatedLdapRepositoryQuery(LdapQueryMethod queryMethod, Class<?> clazz, LdapOperations ldapOperations) {
|
||||
super(queryMethod, clazz, ldapOperations);
|
||||
queryAnnotation = queryMethod.getQueryAnnotation();
|
||||
|
||||
Assert.notNull(queryMethod, "Annotation must be present");
|
||||
Assert.hasLength(queryAnnotation.value(), "Query filter must be specified");
|
||||
}
|
||||
|
||||
@Override
|
||||
protected LdapQuery createQuery(Object[] parameters) {
|
||||
return query()
|
||||
.base(queryAnnotation.base())
|
||||
.searchScope(queryAnnotation.searchScope())
|
||||
.countLimit(queryAnnotation.countLimit())
|
||||
.timeLimit(queryAnnotation.timeLimit())
|
||||
.filter(queryAnnotation.value(), parameters);
|
||||
}
|
||||
}
|
||||
@@ -1,126 +0,0 @@
|
||||
/*
|
||||
* Copyright 2005-2013 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.ldap.repository.query;
|
||||
|
||||
import org.springframework.data.domain.Sort;
|
||||
import org.springframework.data.mapping.PropertyPath;
|
||||
import org.springframework.data.repository.query.Parameters;
|
||||
import org.springframework.data.repository.query.ParametersParameterAccessor;
|
||||
import org.springframework.data.repository.query.parser.AbstractQueryCreator;
|
||||
import org.springframework.data.repository.query.parser.Part;
|
||||
import org.springframework.data.repository.query.parser.PartTree;
|
||||
import org.springframework.ldap.odm.annotations.Entry;
|
||||
import org.springframework.ldap.odm.core.ObjectDirectoryMapper;
|
||||
import org.springframework.ldap.query.ConditionCriteria;
|
||||
import org.springframework.ldap.query.ContainerCriteria;
|
||||
import org.springframework.ldap.query.LdapQuery;
|
||||
|
||||
import java.util.Iterator;
|
||||
|
||||
import static org.springframework.ldap.query.LdapQueryBuilder.query;
|
||||
|
||||
/**
|
||||
* Creator of dynamic queries based on method names.
|
||||
* @author Mattias Hellborg Arthursson
|
||||
* @since 2.0
|
||||
*/
|
||||
public class LdapQueryCreator extends AbstractQueryCreator<LdapQuery, ContainerCriteria> {
|
||||
private final Class<?> clazz;
|
||||
private final ObjectDirectoryMapper mapper;
|
||||
|
||||
/**
|
||||
* Construct a new instance.
|
||||
*/
|
||||
public LdapQueryCreator(PartTree tree,
|
||||
Parameters<?, ?> parameters,
|
||||
Class<?> clazz,
|
||||
ObjectDirectoryMapper mapper,
|
||||
Object[] values) {
|
||||
super(tree, new ParametersParameterAccessor(parameters, values));
|
||||
this.clazz = clazz;
|
||||
this.mapper = mapper;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected ContainerCriteria create(Part part, Iterator<Object> iterator) {
|
||||
String base = clazz.getAnnotation(Entry.class).base();
|
||||
ConditionCriteria criteria = query().base(base).where(getAttribute(part));
|
||||
|
||||
return appendCondition(part, iterator, criteria);
|
||||
}
|
||||
|
||||
private ContainerCriteria appendCondition(Part part, Iterator<Object> iterator, ConditionCriteria criteria) {
|
||||
Part.Type type = part.getType();
|
||||
|
||||
String value = null;
|
||||
if(iterator.hasNext()){
|
||||
value = iterator.next().toString();
|
||||
}
|
||||
switch (type) {
|
||||
case NEGATING_SIMPLE_PROPERTY:
|
||||
return criteria.not().is(value);
|
||||
case SIMPLE_PROPERTY:
|
||||
return criteria.is(value);
|
||||
case STARTING_WITH:
|
||||
return criteria.like(value + "*");
|
||||
case ENDING_WITH:
|
||||
return criteria.like("*" + value);
|
||||
case CONTAINING:
|
||||
return criteria.like("*" + value + "*");
|
||||
case LIKE:
|
||||
return criteria.like(value);
|
||||
case NOT_LIKE:
|
||||
return criteria.not().like(value);
|
||||
case GREATER_THAN_EQUAL:
|
||||
return criteria.gte(value);
|
||||
case LESS_THAN_EQUAL:
|
||||
return criteria.lte(value);
|
||||
case IS_NOT_NULL:
|
||||
return criteria.isPresent();
|
||||
case IS_NULL:
|
||||
return criteria.not().isPresent();
|
||||
}
|
||||
|
||||
throw new IllegalArgumentException(String.format("%s queries are not supported for LDAP repositories", type));
|
||||
}
|
||||
|
||||
private String getAttribute(Part part) {
|
||||
PropertyPath path = part.getProperty();
|
||||
if(path.hasNext()) {
|
||||
throw new IllegalArgumentException("Nested properties are not supported");
|
||||
}
|
||||
|
||||
return mapper.attributeFor(clazz, path.getSegment());
|
||||
}
|
||||
|
||||
@Override
|
||||
protected ContainerCriteria and(Part part, ContainerCriteria base, Iterator<Object> iterator) {
|
||||
ConditionCriteria criteria = base.and(getAttribute(part));
|
||||
|
||||
return appendCondition(part, iterator, criteria);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected ContainerCriteria or(ContainerCriteria base, ContainerCriteria criteria) {
|
||||
return base.or(criteria);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected LdapQuery complete(ContainerCriteria criteria, Sort sort) {
|
||||
return criteria;
|
||||
}
|
||||
}
|
||||
@@ -1,65 +0,0 @@
|
||||
/*
|
||||
* Copyright 2005-2016 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.ldap.repository.query;
|
||||
|
||||
import org.springframework.core.annotation.AnnotationUtils;
|
||||
import org.springframework.data.projection.ProjectionFactory;
|
||||
import org.springframework.data.repository.core.RepositoryMetadata;
|
||||
import org.springframework.data.repository.query.QueryMethod;
|
||||
import org.springframework.ldap.repository.Query;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
/**
|
||||
* QueryMethod for Ldap Queries.
|
||||
*
|
||||
* @author Mattias Hellborg Arthursson
|
||||
* @author Eddu Melendez
|
||||
* @since 2.0
|
||||
*/
|
||||
public class LdapQueryMethod extends QueryMethod {
|
||||
private final Method method;
|
||||
|
||||
/**
|
||||
* Creates a new LdapQueryMethod from the given parameters.
|
||||
*
|
||||
* @param method must not be {@literal null}
|
||||
* @param metadata must not be {@literal null}
|
||||
*/
|
||||
public LdapQueryMethod(Method method, RepositoryMetadata metadata, ProjectionFactory factory) {
|
||||
super(method, metadata, factory);
|
||||
this.method = method;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check whether the target method is annotated with {@link org.springframework.ldap.repository.Query}.
|
||||
* @return <code>true</code> if the target method is annotated with {@link org.springframework.ldap.repository.Query}, <code>false</code>
|
||||
* otherwise.
|
||||
*/
|
||||
public boolean hasQueryAnnotation() {
|
||||
return getQueryAnnotation() != null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the {@link org.springframework.ldap.repository.Query} annotation of the target method (if any).
|
||||
* @return the {@link org.springframework.ldap.repository.Query} annotation of the target method if present, or <code>null</code>
|
||||
* otherwise.
|
||||
*/
|
||||
Query getQueryAnnotation() {
|
||||
return AnnotationUtils.getAnnotation(method, Query.class);
|
||||
}
|
||||
}
|
||||
@@ -1,35 +0,0 @@
|
||||
package org.springframework.ldap.repository.query;
|
||||
|
||||
import org.springframework.data.repository.query.Parameters;
|
||||
import org.springframework.data.repository.query.parser.PartTree;
|
||||
import org.springframework.ldap.core.LdapOperations;
|
||||
import org.springframework.ldap.odm.core.ObjectDirectoryMapper;
|
||||
import org.springframework.ldap.query.LdapQuery;
|
||||
|
||||
/**
|
||||
* @author Mattias Hellborg Arthursson
|
||||
*/
|
||||
public class PartTreeLdapRepositoryQuery extends AbstractLdapRepositoryQuery {
|
||||
private final PartTree partTree;
|
||||
private final Parameters<?,?> parameters;
|
||||
private final ObjectDirectoryMapper objectDirectoryMapper;
|
||||
|
||||
public PartTreeLdapRepositoryQuery(LdapQueryMethod queryMethod, Class<?> clazz, LdapOperations ldapOperations) {
|
||||
super(queryMethod, clazz, ldapOperations);
|
||||
partTree = new PartTree(queryMethod.getName(), clazz);
|
||||
parameters = queryMethod.getParameters();
|
||||
objectDirectoryMapper = ldapOperations.getObjectDirectoryMapper();
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
protected LdapQuery createQuery(Object[] actualParameters) {
|
||||
LdapQueryCreator queryCreator =
|
||||
new LdapQueryCreator(partTree,
|
||||
this.parameters,
|
||||
getClazz(),
|
||||
objectDirectoryMapper,
|
||||
actualParameters);
|
||||
return queryCreator.createQuery();
|
||||
}
|
||||
}
|
||||
@@ -1,57 +0,0 @@
|
||||
/*
|
||||
* Copyright 2005-2016 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.ldap.repository.support;
|
||||
|
||||
import com.querydsl.apt.DefaultConfiguration;
|
||||
import org.springframework.ldap.odm.annotations.Id;
|
||||
|
||||
import javax.annotation.processing.RoundEnvironment;
|
||||
import javax.lang.model.element.VariableElement;
|
||||
import java.lang.annotation.Annotation;
|
||||
import java.util.Collection;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @author Mattias Hellborg Arthursson
|
||||
* @author Eddu Melendez
|
||||
* @since 2.0
|
||||
*/
|
||||
class DefaultLdapAnnotationProcessorConfiguration extends DefaultConfiguration {
|
||||
public DefaultLdapAnnotationProcessorConfiguration(
|
||||
RoundEnvironment roundEnv,
|
||||
Map<String, String> options,
|
||||
Collection<String> keywords,
|
||||
Class<? extends Annotation> entitiesAnn,
|
||||
Class<? extends Annotation> entityAnn,
|
||||
Class<? extends Annotation> superTypeAnn,
|
||||
Class<? extends Annotation> embeddableAnn,
|
||||
Class<? extends Annotation> embeddedAnn,
|
||||
Class<? extends Annotation> skipAnn) {
|
||||
|
||||
super(roundEnv, options, keywords, entitiesAnn, entityAnn, superTypeAnn, embeddableAnn, embeddedAnn, skipAnn);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isBlockedField(VariableElement field) {
|
||||
return super.isBlockedField(field) || field.getAnnotation(Id.class) != null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isValidField(VariableElement field) {
|
||||
return super.isValidField(field) && field.getAnnotation(Id.class) == null;
|
||||
}
|
||||
}
|
||||
@@ -1,54 +0,0 @@
|
||||
/*
|
||||
* Copyright 2005-2016 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.ldap.repository.support;
|
||||
|
||||
import com.querydsl.apt.AbstractQuerydslProcessor;
|
||||
import com.querydsl.apt.Configuration;
|
||||
import com.querydsl.apt.DefaultConfiguration;
|
||||
import com.querydsl.core.annotations.QueryEntities;
|
||||
import org.springframework.ldap.odm.annotations.Entry;
|
||||
import org.springframework.ldap.odm.annotations.Transient;
|
||||
|
||||
import javax.annotation.processing.RoundEnvironment;
|
||||
import javax.annotation.processing.SupportedAnnotationTypes;
|
||||
import javax.annotation.processing.SupportedSourceVersion;
|
||||
import javax.lang.model.SourceVersion;
|
||||
import javax.tools.Diagnostic;
|
||||
import java.util.Collections;
|
||||
|
||||
/**
|
||||
* QueryDSL Annotation Processor to generate QueryDSL classes for entity classes annotated with {@link Entry}.
|
||||
*
|
||||
* @author Mattias Hellborg Arthursson
|
||||
* @author Eddu Melendez
|
||||
* @since 2.0
|
||||
*/
|
||||
@SupportedAnnotationTypes("org.springframework.ldap.odm.annotations.*")
|
||||
@SupportedSourceVersion(SourceVersion.RELEASE_6)
|
||||
public class LdapAnnotationProcessor extends AbstractQuerydslProcessor {
|
||||
@Override
|
||||
protected Configuration createConfiguration(RoundEnvironment roundEnv) {
|
||||
processingEnv.getMessager().printMessage(Diagnostic.Kind.NOTE, "Running " + getClass().getSimpleName());
|
||||
|
||||
DefaultConfiguration configuration = new DefaultLdapAnnotationProcessorConfiguration(roundEnv, processingEnv.getOptions(),
|
||||
Collections.<String>emptySet(), QueryEntities.class, Entry.class, null, null, null, Transient.class);
|
||||
configuration.setUseFields(true);
|
||||
configuration.setUseGetters(false);
|
||||
|
||||
return configuration;
|
||||
}
|
||||
}
|
||||
@@ -1,108 +0,0 @@
|
||||
/*
|
||||
* Copyright 2005-2016 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.ldap.repository.support;
|
||||
|
||||
import org.springframework.data.projection.ProjectionFactory;
|
||||
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;
|
||||
import org.springframework.data.repository.core.RepositoryMetadata;
|
||||
import org.springframework.data.repository.core.support.RepositoryFactorySupport;
|
||||
import org.springframework.data.repository.query.QueryLookupStrategy;
|
||||
import org.springframework.data.repository.query.RepositoryQuery;
|
||||
import org.springframework.ldap.core.LdapOperations;
|
||||
import org.springframework.ldap.repository.query.AnnotatedLdapRepositoryQuery;
|
||||
import org.springframework.ldap.repository.query.LdapQueryMethod;
|
||||
import org.springframework.ldap.repository.query.PartTreeLdapRepositoryQuery;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
import static org.springframework.data.querydsl.QueryDslUtils.QUERY_DSL_PRESENT;
|
||||
|
||||
/**
|
||||
* Factory to create {@link org.springframework.ldap.repository.LdapRepository} instances.
|
||||
* @author Mattias Hellborg Arthursson
|
||||
* @author Eddu Melendez
|
||||
* @since 2.0
|
||||
*/
|
||||
public class LdapRepositoryFactory extends RepositoryFactorySupport {
|
||||
private final LdapOperations ldapOperations;
|
||||
|
||||
public LdapRepositoryFactory(LdapOperations ldapOperations) {
|
||||
this.ldapOperations = ldapOperations;
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T, ID extends Serializable> EntityInformation<T, ID> getEntityInformation(Class<T> domainClass) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@SuppressWarnings({"unchecked", "rawtypes"})
|
||||
protected Object getTargetRepository(RepositoryMetadata metadata) {
|
||||
if(!isQueryDslRepository(metadata.getRepositoryInterface())) {
|
||||
return new SimpleLdapRepository(
|
||||
ldapOperations,
|
||||
ldapOperations.getObjectDirectoryMapper(),
|
||||
metadata.getDomainType());
|
||||
} else {
|
||||
return new QueryDslLdapRepository(
|
||||
ldapOperations,
|
||||
ldapOperations.getObjectDirectoryMapper(),
|
||||
metadata.getDomainType());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
protected Object getTargetRepository(RepositoryInformation metadata) {
|
||||
return getTargetRepository((RepositoryMetadata) metadata);
|
||||
}
|
||||
|
||||
private static boolean isQueryDslRepository(Class<?> repositoryInterface) {
|
||||
return QUERY_DSL_PRESENT && QueryDslPredicateExecutor.class.isAssignableFrom(repositoryInterface);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
protected Class<?> getRepositoryBaseClass(RepositoryMetadata metadata) {
|
||||
if(!isQueryDslRepository(metadata.getRepositoryInterface())) {
|
||||
return SimpleLdapRepository.class;
|
||||
} else {
|
||||
return QueryDslLdapRepository.class;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected QueryLookupStrategy getQueryLookupStrategy(QueryLookupStrategy.Key key) {
|
||||
return new LdapQueryLookupStrategy();
|
||||
}
|
||||
|
||||
private final class LdapQueryLookupStrategy implements QueryLookupStrategy {
|
||||
@Override
|
||||
public RepositoryQuery resolveQuery(Method method, RepositoryMetadata metadata, ProjectionFactory factory, NamedQueries namedQueries) {
|
||||
LdapQueryMethod queryMethod = new LdapQueryMethod(method, metadata, factory);
|
||||
Class<?> domainType = metadata.getDomainType();
|
||||
|
||||
if(queryMethod.hasQueryAnnotation()) {
|
||||
return new AnnotatedLdapRepositoryQuery(queryMethod, domainType, ldapOperations);
|
||||
} else {
|
||||
return new PartTreeLdapRepositoryQuery(queryMethod, domainType, ldapOperations);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,50 +0,0 @@
|
||||
/*
|
||||
* Copyright 2005-2013 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.ldap.repository.support;
|
||||
|
||||
import org.springframework.data.repository.Repository;
|
||||
import org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport;
|
||||
import org.springframework.data.repository.core.support.RepositoryFactorySupport;
|
||||
import org.springframework.ldap.core.LdapOperations;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
import javax.naming.Name;
|
||||
|
||||
/**
|
||||
* {@link org.springframework.beans.factory.FactoryBean} to create {@link org.springframework.ldap.repository.LdapRepository} instances.
|
||||
*
|
||||
* @author Mattias Hellborg Arthursson
|
||||
* @since 2.0
|
||||
*/
|
||||
public class LdapRepositoryFactoryBean<T extends Repository<S, Name>, S> extends RepositoryFactoryBeanSupport<T, S, Name> {
|
||||
private LdapOperations ldapOperations;
|
||||
|
||||
public void setLdapOperations(LdapOperations ldapOperations) {
|
||||
this.ldapOperations = ldapOperations;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected RepositoryFactorySupport createRepositoryFactory() {
|
||||
return new LdapRepositoryFactory(ldapOperations);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void afterPropertiesSet() {
|
||||
super.afterPropertiesSet();
|
||||
Assert.notNull(ldapOperations, "LdapOperations must be set");
|
||||
}
|
||||
}
|
||||
@@ -1,136 +0,0 @@
|
||||
/*
|
||||
* Copyright 2005-2016 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.ldap.repository.support;
|
||||
|
||||
import com.querydsl.core.types.Constant;
|
||||
import com.querydsl.core.types.Expression;
|
||||
import com.querydsl.core.types.FactoryExpression;
|
||||
import com.querydsl.core.types.Operation;
|
||||
import com.querydsl.core.types.Operator;
|
||||
import com.querydsl.core.types.Ops;
|
||||
import com.querydsl.core.types.ParamExpression;
|
||||
import com.querydsl.core.types.Path;
|
||||
import com.querydsl.core.types.SubQueryExpression;
|
||||
import com.querydsl.core.types.TemplateExpression;
|
||||
import com.querydsl.core.types.Visitor;
|
||||
import org.springframework.ldap.filter.AndFilter;
|
||||
import org.springframework.ldap.filter.EqualsFilter;
|
||||
import org.springframework.ldap.filter.Filter;
|
||||
import org.springframework.ldap.filter.GreaterThanOrEqualsFilter;
|
||||
import org.springframework.ldap.filter.LessThanOrEqualsFilter;
|
||||
import org.springframework.ldap.filter.LikeFilter;
|
||||
import org.springframework.ldap.filter.NotFilter;
|
||||
import org.springframework.ldap.filter.OrFilter;
|
||||
import org.springframework.ldap.filter.PresentFilter;
|
||||
import org.springframework.ldap.odm.core.ObjectDirectoryMapper;
|
||||
|
||||
/**
|
||||
* Helper class for generating LDAP filters from QueryDSL Expressions.
|
||||
*
|
||||
* @author Mattias Hellborg Arthursson
|
||||
* @author Eddu Melendez
|
||||
* @since 2.0
|
||||
*/
|
||||
class LdapSerializer implements Visitor<Object, Void> {
|
||||
|
||||
private final ObjectDirectoryMapper odm;
|
||||
private final Class<?> clazz;
|
||||
|
||||
public LdapSerializer(ObjectDirectoryMapper odm, Class<?> clazz) {
|
||||
this.odm = odm;
|
||||
this.clazz = clazz;
|
||||
}
|
||||
|
||||
public Filter handle(Expression<?> expression) {
|
||||
return (Filter) expression.accept(this, null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object visit(Constant<?> expr, Void context) {
|
||||
return expr.getConstant().toString();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object visit(FactoryExpression<?> expr, Void context) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object visit(Operation<?> expr, Void context) {
|
||||
Operator operator = expr.getOperator();
|
||||
if (operator == Ops.EQ) {
|
||||
return new EqualsFilter(attribute(expr), value(expr));
|
||||
} else if (operator == Ops.AND) {
|
||||
return new AndFilter()
|
||||
.and(handle(expr.getArg(0)))
|
||||
.and(handle(expr.getArg(1)));
|
||||
} else if (operator == Ops.OR) {
|
||||
return new OrFilter()
|
||||
.or(handle(expr.getArg(0)))
|
||||
.or(handle(expr.getArg(1)));
|
||||
} else if (operator == Ops.NOT) {
|
||||
return new NotFilter(handle(expr.getArg(0)));
|
||||
} else if (operator == Ops.LIKE) {
|
||||
return new LikeFilter(attribute(expr), value(expr));
|
||||
} else if (operator == Ops.STARTS_WITH || operator == Ops.STARTS_WITH_IC) {
|
||||
return new LikeFilter(attribute(expr), value(expr) + "*");
|
||||
} else if (operator == Ops.ENDS_WITH || operator == Ops.ENDS_WITH_IC) {
|
||||
return new LikeFilter(attribute(expr), "*" + value(expr));
|
||||
} else if (operator == Ops.STRING_CONTAINS || operator == Ops.STRING_CONTAINS_IC) {
|
||||
return new LikeFilter(attribute(expr), "*" + value(expr) + "*");
|
||||
} else if (operator == Ops.IS_NOT_NULL) {
|
||||
return new PresentFilter(attribute(expr));
|
||||
} else if (operator == Ops.IS_NULL) {
|
||||
return new NotFilter(new PresentFilter(attribute(expr)));
|
||||
} else if (operator == Ops.GOE) {
|
||||
return new GreaterThanOrEqualsFilter(attribute(expr), value(expr));
|
||||
} else if (operator == Ops.LOE) {
|
||||
return new LessThanOrEqualsFilter(attribute(expr), value(expr));
|
||||
}
|
||||
|
||||
|
||||
throw new UnsupportedOperationException("Unsupported operator " + operator.toString());
|
||||
}
|
||||
|
||||
private String value(Operation<?> expr) {
|
||||
return (String) expr.getArg(1).accept(this, null);
|
||||
}
|
||||
|
||||
private String attribute(Operation<?> expr) {
|
||||
return odm.attributeFor(clazz, (String) expr.getArg(0).accept(this, null));
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object visit(ParamExpression<?> expr, Void context) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object visit(Path<?> expr, Void context) {
|
||||
return expr.getMetadata().getName();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object visit(SubQueryExpression<?> expr, Void context) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object visit(TemplateExpression<?> expr, Void context) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
}
|
||||
@@ -1,76 +0,0 @@
|
||||
/*
|
||||
* Copyright 2005-2016 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.ldap.repository.support;
|
||||
|
||||
import com.querydsl.core.DefaultQueryMetadata;
|
||||
import com.querydsl.core.FilteredClause;
|
||||
import com.querydsl.core.support.QueryMixin;
|
||||
import com.querydsl.core.types.EntityPath;
|
||||
import com.querydsl.core.types.Predicate;
|
||||
import org.springframework.ldap.core.LdapOperations;
|
||||
import org.springframework.ldap.query.LdapQuery;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.springframework.ldap.query.LdapQueryBuilder.query;
|
||||
|
||||
/**
|
||||
* Spring LDAP specific {@link FilteredClause} implementation.
|
||||
*
|
||||
* @author Mattias Hellborg Arthursson
|
||||
* @author Eddu Melendez
|
||||
* @since 2.0
|
||||
*/
|
||||
public class QueryDslLdapQuery<K> implements FilteredClause<QueryDslLdapQuery<K>> {
|
||||
private final LdapOperations ldapOperations;
|
||||
private final Class<? extends K> clazz;
|
||||
|
||||
private QueryMixin<QueryDslLdapQuery<K>> queryMixin =
|
||||
new QueryMixin<QueryDslLdapQuery<K>>(this, new DefaultQueryMetadata().noValidate());
|
||||
|
||||
private final LdapSerializer filterGenerator;
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public QueryDslLdapQuery(LdapOperations ldapOperations, EntityPath<K> entityPath) {
|
||||
this(ldapOperations, (Class<K>) entityPath.getType());
|
||||
}
|
||||
|
||||
public QueryDslLdapQuery(LdapOperations ldapOperations, Class<K> clazz) {
|
||||
this.ldapOperations = ldapOperations;
|
||||
this.clazz = clazz;
|
||||
this.filterGenerator = new LdapSerializer(ldapOperations.getObjectDirectoryMapper(), clazz);
|
||||
}
|
||||
|
||||
@Override
|
||||
public QueryDslLdapQuery<K> where(Predicate... o) {
|
||||
return queryMixin.where(o);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public List<K> list() {
|
||||
return (List<K>) ldapOperations.find(buildQuery(), clazz);
|
||||
}
|
||||
|
||||
public K uniqueResult() {
|
||||
return ldapOperations.findOne(buildQuery(), clazz);
|
||||
}
|
||||
|
||||
LdapQuery buildQuery() {
|
||||
return query().filter(filterGenerator.handle(queryMixin.getMetadata().getWhere()));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,87 +0,0 @@
|
||||
/*
|
||||
* Copyright 2005-2016 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.ldap.repository.support;
|
||||
|
||||
import com.querydsl.core.types.OrderSpecifier;
|
||||
import com.querydsl.core.types.Predicate;
|
||||
|
||||
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.ldap.core.LdapOperations;
|
||||
import org.springframework.ldap.odm.core.ObjectDirectoryMapper;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Base repository implementation for QueryDSL support.
|
||||
*
|
||||
* @author Mattias Hellborg Arthursson
|
||||
* @author Eddu Melendez
|
||||
* @since 2.0
|
||||
*/
|
||||
public class QueryDslLdapRepository<T> extends SimpleLdapRepository<T> implements QueryDslPredicateExecutor<T> {
|
||||
|
||||
public QueryDslLdapRepository(LdapOperations ldapOperations,
|
||||
ObjectDirectoryMapper odm,
|
||||
Class<T> clazz) {
|
||||
super(ldapOperations, odm, clazz);
|
||||
}
|
||||
|
||||
@Override
|
||||
public T findOne(Predicate predicate) {
|
||||
return queryFor(predicate).uniqueResult();
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<T> findAll(Predicate predicate) {
|
||||
return queryFor(predicate).list();
|
||||
}
|
||||
|
||||
@Override
|
||||
public long count(Predicate predicate) {
|
||||
return findAll(predicate).size();
|
||||
}
|
||||
|
||||
public boolean exists(Predicate predicate) {
|
||||
return count(predicate) > 0;
|
||||
}
|
||||
|
||||
public Iterable<T> findAll(Predicate predicate, Sort sort) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
private QueryDslLdapQuery<T> queryFor(Predicate predicate) {
|
||||
return new QueryDslLdapQuery<T>(getLdapOperations(), getClazz())
|
||||
.where(predicate);
|
||||
}
|
||||
|
||||
public Iterable<T> findAll(OrderSpecifier<?>... orders) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Iterable<T> findAll(Predicate predicate, OrderSpecifier<?>... orders) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Page<T> findAll(Predicate predicate, Pageable pageable) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
}
|
||||
@@ -1,220 +0,0 @@
|
||||
/*
|
||||
* Copyright 2005-2013 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.ldap.repository.support;
|
||||
|
||||
import org.springframework.dao.EmptyResultDataAccessException;
|
||||
import org.springframework.data.domain.Persistable;
|
||||
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.ldap.repository.LdapRepository;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
import javax.naming.Name;
|
||||
import java.util.Iterator;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
import static org.springframework.ldap.query.LdapQueryBuilder.query;
|
||||
|
||||
/**
|
||||
* Base repository implementation for LDAP.
|
||||
*
|
||||
* @author Mattias Hellborg Arthursson
|
||||
* @since 2.0
|
||||
*/
|
||||
public class SimpleLdapRepository<T> implements LdapRepository<T> {
|
||||
private static final String OBJECTCLASS_ATTRIBUTE = "objectclass";
|
||||
private final LdapOperations ldapOperations;
|
||||
private final ObjectDirectoryMapper odm;
|
||||
private final Class<T> clazz;
|
||||
|
||||
public SimpleLdapRepository(LdapOperations ldapOperations, ObjectDirectoryMapper odm, Class<T> clazz) {
|
||||
this.ldapOperations = ldapOperations;
|
||||
this.odm = odm;
|
||||
this.clazz = clazz;
|
||||
}
|
||||
|
||||
protected LdapOperations getLdapOperations() {
|
||||
return ldapOperations;
|
||||
}
|
||||
|
||||
protected Class<T> getClazz() {
|
||||
return clazz;
|
||||
}
|
||||
|
||||
@Override
|
||||
public long count() {
|
||||
Filter filter = odm.filterFor(clazz, null);
|
||||
CountNameClassPairCallbackHandler callback = new CountNameClassPairCallbackHandler();
|
||||
LdapQuery query = query().attributes(OBJECTCLASS_ATTRIBUTE).filter(filter);
|
||||
ldapOperations.search(query, callback);
|
||||
|
||||
return callback.getNoOfRows();
|
||||
}
|
||||
|
||||
private <S extends T> boolean isNew(S entity, Name id) {
|
||||
if (entity instanceof Persistable) {
|
||||
Persistable<?> persistable = (Persistable<?>) entity;
|
||||
return persistable.isNew();
|
||||
} else {
|
||||
return id == null;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public <S extends T> S save(S entity) {
|
||||
Assert.notNull(entity, "Entity must not be null");
|
||||
Name declaredId = odm.getId(entity);
|
||||
|
||||
if (isNew(entity, declaredId)) {
|
||||
ldapOperations.create(entity);
|
||||
} else {
|
||||
ldapOperations.update(entity);
|
||||
}
|
||||
|
||||
return entity;
|
||||
}
|
||||
|
||||
@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);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public T findOne(Name name) {
|
||||
Assert.notNull(name, "Id must not be null");
|
||||
try {
|
||||
return ldapOperations.findByDn(name, clazz);
|
||||
} catch (NameNotFoundException e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<T> findAll(LdapQuery ldapQuery) {
|
||||
Assert.notNull(ldapQuery, "LdapQuery must not be null");
|
||||
return ldapOperations.find(ldapQuery, clazz);
|
||||
}
|
||||
|
||||
@Override
|
||||
public T findOne(LdapQuery ldapQuery) {
|
||||
Assert.notNull(ldapQuery, "LdapQuery must not be null");
|
||||
try {
|
||||
return ldapOperations.findOne(ldapQuery, clazz);
|
||||
} catch (EmptyResultDataAccessException e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean exists(Name name) {
|
||||
Assert.notNull(name, "Id must not be null");
|
||||
return findOne(name) != null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<T> findAll() {
|
||||
return ldapOperations.findAll(clazz);
|
||||
}
|
||||
|
||||
@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;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void delete(Name name) {
|
||||
Assert.notNull(name, "Id must not be null");
|
||||
ldapOperations.unbind(name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void delete(T entity) {
|
||||
Assert.notNull(entity, "Entity must not be null");
|
||||
ldapOperations.delete(entity);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void delete(Iterable<? extends T> entities) {
|
||||
for (T entity : entities) {
|
||||
delete(entity);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
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,7 +0,0 @@
|
||||
package org.springframework.ldap.config;
|
||||
|
||||
/**
|
||||
* @author Mattias Hellborg Arthursson
|
||||
*/
|
||||
public class DummyEntity {
|
||||
}
|
||||
@@ -1,27 +0,0 @@
|
||||
/*
|
||||
* Copyright 2005-2013 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.ldap.config;
|
||||
|
||||
import org.springframework.data.repository.CrudRepository;
|
||||
|
||||
import javax.naming.Name;
|
||||
|
||||
/**
|
||||
* @author Mattias Hellborg Arthursson
|
||||
*/
|
||||
public interface DummyLdapRepository extends CrudRepository<DummyEntity, Name> {
|
||||
}
|
||||
@@ -1,58 +0,0 @@
|
||||
/*
|
||||
* Copyright 2002-2015 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.ldap.config;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
import static org.mockito.Matchers.any;
|
||||
import static org.mockito.Matchers.eq;
|
||||
import static org.powermock.api.mockito.PowerMockito.spy;
|
||||
import static org.powermock.api.mockito.PowerMockito.when;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.powermock.core.classloader.annotations.PrepareForTest;
|
||||
import org.powermock.modules.junit4.PowerMockRunner;
|
||||
import org.powermock.reflect.internal.WhiteboxImpl;
|
||||
import org.springframework.beans.factory.xml.BeanDefinitionParser;
|
||||
import org.springframework.util.ClassUtils;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Rob Winch
|
||||
*
|
||||
*/
|
||||
@RunWith(PowerMockRunner.class)
|
||||
@PrepareForTest({ ClassUtils.class })
|
||||
public class LdapNamespaceHandlerTests {
|
||||
|
||||
// LDAP-335
|
||||
@Test
|
||||
public void repositoryClassNotLoadedIfNotOnClasspath() throws Exception {
|
||||
spy(ClassUtils.class);
|
||||
when(ClassUtils.class, "isPresent",
|
||||
eq(LdapNamespaceHandler.REPOSITORY_CLASS_NAME), any(ClassLoader.class))
|
||||
.thenReturn(false);
|
||||
|
||||
LdapNamespaceHandler handler = new LdapNamespaceHandler();
|
||||
|
||||
handler.init();
|
||||
|
||||
Map<String, BeanDefinitionParser> parsers = WhiteboxImpl.getInternalState(handler, "parsers");
|
||||
assertFalse(parsers.containsKey(Elements.REPOSITORIES));
|
||||
}
|
||||
}
|
||||
@@ -355,14 +355,6 @@ public class LdapTemplateNamespaceHandlerTest {
|
||||
new ClassPathXmlApplicationContext("/ldap-namespace-config-pooling-with-native.xml");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void verifyAutomaticRepositorySupport() {
|
||||
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("/ldap-namespace-config-with-repositories.xml");
|
||||
DummyLdapRepository repository = ctx.getBean(DummyLdapRepository.class);
|
||||
|
||||
assertThat(repository).isNotNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void verifyParsePooling2Defaults() {
|
||||
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("/ldap-namespace-config-pooling2-defaults.xml");
|
||||
|
||||
@@ -1,214 +0,0 @@
|
||||
/*
|
||||
* Copyright 2005-2016 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.ldap.repository;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
import org.springframework.data.domain.Persistable;
|
||||
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.ldap.repository.support.SimpleLdapRepository;
|
||||
import org.springframework.ldap.support.LdapUtils;
|
||||
|
||||
import javax.naming.Name;
|
||||
import javax.naming.ldap.LdapName;
|
||||
import java.util.Arrays;
|
||||
import java.util.Iterator;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.Matchers.any;
|
||||
import static org.mockito.Mockito.doNothing;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
/**
|
||||
* @author Mattias Hellborg Arthursson
|
||||
*/
|
||||
public class SimpleLdapRepositoryTest {
|
||||
|
||||
private LdapOperations ldapOperationsMock;
|
||||
private ObjectDirectoryMapper odmMock;
|
||||
private SimpleLdapRepository<Object> tested;
|
||||
|
||||
@Before
|
||||
public void prepareTestedInstance() {
|
||||
ldapOperationsMock = mock(LdapOperations.class);
|
||||
odmMock = mock(ObjectDirectoryMapper.class);
|
||||
tested = new SimpleLdapRepository<Object>(ldapOperationsMock, odmMock, Object.class);
|
||||
}
|
||||
|
||||
@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));
|
||||
|
||||
long count = tested.count();
|
||||
|
||||
assertThat(count).isEqualTo(0);
|
||||
LdapQuery query = ldapQuery.getValue();
|
||||
assertThat(query.filter()).isEqualTo(filterMock);
|
||||
assertThat(query.attributes()).isEqualTo(new String[]{"objectclass"});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSaveNonPersistableWithIdSet() {
|
||||
Object expectedEntity = new Object();
|
||||
|
||||
when(odmMock.getId(expectedEntity)).thenReturn(LdapUtils.emptyLdapName());
|
||||
when(odmMock.getCalculatedId(expectedEntity)).thenReturn(null);
|
||||
|
||||
tested.save(expectedEntity);
|
||||
|
||||
verify(ldapOperationsMock).update(expectedEntity);
|
||||
}
|
||||
|
||||
@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(expectedName);
|
||||
|
||||
tested.save(expectedEntity);
|
||||
|
||||
verify(ldapOperationsMock).update(expectedEntity);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSaveNonPersistableWithNoIdCalculatedId() {
|
||||
Object expectedEntity = new Object();
|
||||
LdapName expectedName = LdapUtils.emptyLdapName();
|
||||
|
||||
when(odmMock.getId(expectedEntity)).thenReturn(null);
|
||||
when(odmMock.getCalculatedId(expectedEntity)).thenReturn(expectedName);
|
||||
|
||||
tested.save(expectedEntity);
|
||||
|
||||
verify(ldapOperationsMock).create(expectedEntity);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSavePersistableNewWithDeclaredId() {
|
||||
Persistable expectedEntity = mock(Persistable.class);
|
||||
|
||||
when(expectedEntity.isNew()).thenReturn(true);
|
||||
when(odmMock.getId(expectedEntity)).thenReturn(LdapUtils.emptyLdapName());
|
||||
when(odmMock.getCalculatedId(expectedEntity)).thenReturn(null);
|
||||
|
||||
tested.save(expectedEntity);
|
||||
|
||||
verify(ldapOperationsMock).create(expectedEntity);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSavePersistableNewWithCalculatedId() {
|
||||
Persistable expectedEntity = mock(Persistable.class);
|
||||
LdapName expectedName = LdapUtils.emptyLdapName();
|
||||
|
||||
when(expectedEntity.isNew()).thenReturn(true);
|
||||
when(odmMock.getId(expectedEntity)).thenReturn(null);
|
||||
when(odmMock.getCalculatedId(expectedEntity)).thenReturn(expectedName);
|
||||
|
||||
tested.save(expectedEntity);
|
||||
|
||||
verify(ldapOperationsMock).create(expectedEntity);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSavePersistableNotNew() {
|
||||
Persistable expectedEntity = mock(Persistable.class);
|
||||
|
||||
when(expectedEntity.isNew()).thenReturn(false);
|
||||
when(odmMock.getId(expectedEntity)).thenReturn(LdapUtils.emptyLdapName());
|
||||
when(odmMock.getCalculatedId(expectedEntity)).thenReturn(null);
|
||||
|
||||
tested.save(expectedEntity);
|
||||
|
||||
verify(ldapOperationsMock).update(expectedEntity);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFindOneWithName() {
|
||||
LdapName expectedName = LdapUtils.emptyLdapName();
|
||||
Object expectedResult = new Object();
|
||||
|
||||
when(ldapOperationsMock.findByDn(expectedName, Object.class)).thenReturn(expectedResult);
|
||||
|
||||
Object actualResult = tested.findOne(expectedName);
|
||||
|
||||
assertThat(actualResult).isSameAs(expectedResult);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void verifyThatNameNotFoundInFindOneWithNameReturnsNull() {
|
||||
LdapName expectedName = LdapUtils.emptyLdapName();
|
||||
|
||||
when(ldapOperationsMock.findByDn(expectedName, Object.class)).thenThrow(new NameNotFoundException(""));
|
||||
|
||||
Object actualResult = tested.findOne(expectedName);
|
||||
|
||||
assertThat(actualResult).isNull();
|
||||
}
|
||||
|
||||
@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();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,21 +0,0 @@
|
||||
package org.springframework.ldap.repository.config;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
||||
import org.springframework.ldap.config.DummyLdapRepository;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* @author Mattias Hellborg Arthursson
|
||||
*/
|
||||
public class AnnotationConfigTest {
|
||||
|
||||
@Test
|
||||
public void testAnnotationConfig() {
|
||||
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("/ldap-annotation-config.xml");
|
||||
|
||||
DummyLdapRepository repository = ctx.getBean(DummyLdapRepository.class);
|
||||
assertThat(repository).isNotNull();
|
||||
}
|
||||
}
|
||||
@@ -1,11 +0,0 @@
|
||||
package org.springframework.ldap.repository.config;
|
||||
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
/**
|
||||
* @author Mattias Hellborg Arthursson
|
||||
*/
|
||||
@Configuration
|
||||
@EnableLdapRepositories(basePackages = "org.springframework.ldap.config")
|
||||
public class SpringLdapConfiguration {
|
||||
}
|
||||
@@ -1,30 +0,0 @@
|
||||
/*
|
||||
* Copyright 2005-2015 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.ldap.repository.query;
|
||||
|
||||
import org.springframework.ldap.odm.core.impl.BaseUnitTestPerson;
|
||||
import org.springframework.ldap.odm.core.impl.UnitTestPerson;
|
||||
import org.springframework.ldap.repository.LdapRepository;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author Rob Winch
|
||||
*/
|
||||
public interface BaseTestPersonRepository extends LdapRepository<UnitTestPerson> {
|
||||
List<BaseUnitTestPerson> findByFullName(String name);
|
||||
}
|
||||
@@ -1,187 +0,0 @@
|
||||
/*
|
||||
* Copyright 2005-2016 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.ldap.repository.query;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.projection.ProjectionFactory;
|
||||
import org.springframework.data.projection.SpelAwareProxyProjectionFactory;
|
||||
import org.springframework.data.repository.core.support.DefaultRepositoryMetadata;
|
||||
import org.springframework.ldap.core.LdapTemplate;
|
||||
import org.springframework.ldap.odm.core.impl.BaseUnitTestPerson;
|
||||
import org.springframework.ldap.odm.core.impl.UnitTestPerson;
|
||||
import org.springframework.ldap.query.LdapQuery;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* @author Mattias Hellborg Arthursson
|
||||
* @author Eddu Melendez
|
||||
*/
|
||||
@ContextConfiguration("classpath:/query-test.xml")
|
||||
public class PartTreeLdapRepositoryQueryTest extends AbstractJUnit4SpringContextTests {
|
||||
|
||||
@Autowired
|
||||
private LdapTemplate ldapTemplate;
|
||||
private Class<?> targetClass;
|
||||
private Class<?> entityClass;
|
||||
private DefaultRepositoryMetadata repositoryMetadata;
|
||||
private ProjectionFactory factory;
|
||||
|
||||
@Before
|
||||
public void prepareTest() {
|
||||
entityClass = UnitTestPerson.class;
|
||||
targetClass = UnitTestPersonRepository.class;
|
||||
repositoryMetadata = new DefaultRepositoryMetadata(targetClass);
|
||||
factory = new SpelAwareProxyProjectionFactory();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFindByFullName() throws NoSuchMethodException {
|
||||
assertFilterForMethod(
|
||||
targetClass.getMethod("findByFullName", String.class),
|
||||
"(cn=John Doe)",
|
||||
"John Doe");
|
||||
}
|
||||
|
||||
// LDAP-314
|
||||
@Test
|
||||
public void testFindByFullNameWithBase() throws NoSuchMethodException {
|
||||
entityClass = BaseUnitTestPerson.class;
|
||||
targetClass = BaseTestPersonRepository.class;
|
||||
repositoryMetadata = new DefaultRepositoryMetadata(targetClass);
|
||||
assertFilterAndBaseForMethod(
|
||||
targetClass.getMethod("findByFullName", String.class),
|
||||
"(cn=John Doe)",
|
||||
"ou=someOu",
|
||||
"John Doe");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFindByFullNameLike() throws NoSuchMethodException {
|
||||
assertFilterForMethod(
|
||||
targetClass.getMethod("findByFullNameLike", String.class),
|
||||
"(cn=*John*)",
|
||||
"*John*");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFindByFullNameStartsWith() throws NoSuchMethodException {
|
||||
assertFilterForMethod(
|
||||
targetClass.getMethod("findByFullNameStartsWith", String.class),
|
||||
"(cn=John*)",
|
||||
"John");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFindByFullNameEndsWith() throws NoSuchMethodException {
|
||||
assertFilterForMethod(
|
||||
targetClass.getMethod("findByFullNameEndsWith", String.class),
|
||||
"(cn=*John)",
|
||||
"John");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFindByFullNameContains() throws NoSuchMethodException {
|
||||
assertFilterForMethod(
|
||||
targetClass.getMethod("findByFullNameContains", String.class),
|
||||
"(cn=*John*)",
|
||||
"John");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFindByFullNameGreaterThanEqual() throws NoSuchMethodException {
|
||||
assertFilterForMethod(
|
||||
targetClass.getMethod("findByFullNameGreaterThanEqual", String.class),
|
||||
"(cn>=John)",
|
||||
"John");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFindByFullNameLessThanEqual() throws NoSuchMethodException {
|
||||
assertFilterForMethod(
|
||||
targetClass.getMethod("findByFullNameLessThanEqual", String.class),
|
||||
"(cn<=John)",
|
||||
"John");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFindByFullNameIsNotNull() throws NoSuchMethodException {
|
||||
assertFilterForMethod(
|
||||
targetClass.getMethod("findByFullNameIsNotNull"),
|
||||
"(cn=*)");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFindByFullNameIsNull() throws NoSuchMethodException {
|
||||
assertFilterForMethod(
|
||||
targetClass.getMethod("findByFullNameIsNull"),
|
||||
"(!(cn=*))");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFindByFullNameNot() throws NoSuchMethodException {
|
||||
assertFilterForMethod(
|
||||
targetClass.getMethod("findByFullNameNot", String.class),
|
||||
"(!(cn=John Doe))",
|
||||
"John Doe");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFindByFullNameNotLike() throws NoSuchMethodException {
|
||||
assertFilterForMethod(
|
||||
targetClass.getMethod("findByFullNameNotLike", String.class),
|
||||
"(!(cn=*John*))",
|
||||
"*John*");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFindByFullNameAndLastName() throws NoSuchMethodException {
|
||||
assertFilterForMethod(
|
||||
targetClass.getMethod("findByFullNameAndLastName", String.class, String.class),
|
||||
"(&(cn=John Doe)(sn=Doe))",
|
||||
"John Doe", "Doe");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFindByFullNameAndLastNameNot() throws NoSuchMethodException {
|
||||
assertFilterForMethod(
|
||||
targetClass.getMethod("findByFullNameAndLastNameNot", String.class, String.class),
|
||||
"(&(cn=John Doe)(!(sn=Doe)))",
|
||||
"John Doe", "Doe");
|
||||
}
|
||||
|
||||
private void assertFilterForMethod(Method targetMethod, String expectedFilter, Object... expectedParams) {
|
||||
assertFilterAndBaseForMethod(targetMethod, expectedFilter, "",
|
||||
expectedParams);
|
||||
}
|
||||
|
||||
private void assertFilterAndBaseForMethod(Method targetMethod, String expectedFilter, String expectedBase, Object... expectedParams) {
|
||||
LdapQueryMethod queryMethod = new LdapQueryMethod(targetMethod, repositoryMetadata, factory);
|
||||
PartTreeLdapRepositoryQuery tested = new PartTreeLdapRepositoryQuery(queryMethod, entityClass, ldapTemplate);
|
||||
|
||||
LdapQuery query = tested.createQuery(expectedParams);
|
||||
String base = query.base().toString();
|
||||
assertThat(base).isEqualTo(expectedBase);
|
||||
assertThat(query.filter().encode()).isEqualTo(expectedFilter);
|
||||
}
|
||||
}
|
||||
@@ -1,43 +0,0 @@
|
||||
/*
|
||||
* Copyright 2005-2013 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.ldap.repository.query;
|
||||
|
||||
import org.springframework.ldap.odm.core.impl.UnitTestPerson;
|
||||
import org.springframework.ldap.repository.LdapRepository;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author Mattias Hellborg Arthursson
|
||||
*/
|
||||
public interface UnitTestPersonRepository extends LdapRepository<UnitTestPerson> {
|
||||
List<UnitTestPerson> findByFullName(String name);
|
||||
List<UnitTestPerson> findByFullNameNot(String name);
|
||||
List<UnitTestPerson> findByFullNameLike(String name);
|
||||
List<UnitTestPerson> findByFullNameNotLike(String name);
|
||||
List<UnitTestPerson> findByFullNameStartsWith(String name);
|
||||
List<UnitTestPerson> findByFullNameEndsWith(String name);
|
||||
List<UnitTestPerson> findByFullNameContains(String name);
|
||||
List<UnitTestPerson> findByFullNameGreaterThanEqual(String name);
|
||||
List<UnitTestPerson> findByFullNameLessThanEqual(String name);
|
||||
List<UnitTestPerson> findByFullNameIsNotNull();
|
||||
List<UnitTestPerson> findByFullNameIsNull();
|
||||
|
||||
List<UnitTestPerson> findByFullNameAndLastName(String fullName, String lastName);
|
||||
List<UnitTestPerson> findByFullNameAndLastNameNot(String fullName, String lastName);
|
||||
|
||||
}
|
||||
@@ -1,45 +0,0 @@
|
||||
package org.springframework.ldap.repository.support;
|
||||
|
||||
import com.querydsl.core.types.Path;
|
||||
import com.querydsl.core.types.PathMetadata;
|
||||
import com.querydsl.core.types.dsl.EntityPathBase;
|
||||
import com.querydsl.core.types.dsl.ListPath;
|
||||
import com.querydsl.core.types.dsl.PathInits;
|
||||
import com.querydsl.core.types.dsl.StringPath;
|
||||
import org.springframework.ldap.odm.core.impl.UnitTestPerson;
|
||||
|
||||
import javax.annotation.Generated;
|
||||
|
||||
import static com.querydsl.core.types.PathMetadataFactory.forVariable;
|
||||
|
||||
|
||||
/**
|
||||
* QPerson is a Querydsl query type for Person
|
||||
*/
|
||||
@Generated("com.mysema.query.codegen.EntitySerializer")
|
||||
public class QPerson extends EntityPathBase<UnitTestPerson> {
|
||||
|
||||
private static final long serialVersionUID = -1526737794;
|
||||
|
||||
public static final QPerson person = new QPerson("person");
|
||||
|
||||
public final StringPath fullName = createString("fullName");
|
||||
|
||||
public final ListPath<String, StringPath> description = this.<String, StringPath>createList("description", String.class, StringPath.class, PathInits.DIRECT2);
|
||||
|
||||
public final StringPath lastName = createString("lastName");
|
||||
|
||||
public QPerson(String variable) {
|
||||
super(UnitTestPerson.class, forVariable(variable));
|
||||
}
|
||||
|
||||
public QPerson(Path<? extends UnitTestPerson> path) {
|
||||
super(path.getType(), path.getMetadata());
|
||||
}
|
||||
|
||||
public QPerson(PathMetadata metadata) {
|
||||
super(UnitTestPerson.class, metadata);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,123 +0,0 @@
|
||||
/*
|
||||
* Copyright 2005-2016 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.ldap.repository.support;
|
||||
|
||||
import com.querydsl.core.types.Expression;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.springframework.ldap.filter.Filter;
|
||||
import org.springframework.ldap.odm.core.ObjectDirectoryMapper;
|
||||
import org.springframework.ldap.odm.core.impl.DefaultObjectDirectoryMapper;
|
||||
import org.springframework.ldap.odm.core.impl.UnitTestPerson;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* @author Mattias Hellborg Arthursson
|
||||
* @author Eddu Melendez
|
||||
*/
|
||||
public class QueryDslFilterGeneratorTest {
|
||||
|
||||
private LdapSerializer tested;
|
||||
private QPerson person;
|
||||
|
||||
@Before
|
||||
public void prepareTestedInstance() {
|
||||
ObjectDirectoryMapper odm = new DefaultObjectDirectoryMapper();
|
||||
tested = new LdapSerializer(odm, UnitTestPerson.class);
|
||||
person = QPerson.person;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEqualsFilter() {
|
||||
Expression<?> expression = person.fullName.eq("John Doe");
|
||||
Filter result = tested.handle(expression);
|
||||
assertThat(result.toString()).isEqualTo("(cn=John Doe)");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAndFilter() {
|
||||
Expression<?> expression = person.fullName.eq("John Doe").and(person.lastName.eq("Doe"));
|
||||
Filter result = tested.handle(expression);
|
||||
assertThat(result.toString()).isEqualTo("(&(cn=John Doe)(sn=Doe))");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testOrFilter() {
|
||||
Expression<?> expression = person.fullName.eq("John Doe").or(person.lastName.eq("Doe"));
|
||||
Filter result = tested.handle(expression);
|
||||
assertThat(result.toString()).isEqualTo("(|(cn=John Doe)(sn=Doe))");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testOr() {
|
||||
Expression<?> expression = person.fullName.eq("John Doe")
|
||||
.and(person.lastName.eq("Doe").or(person.lastName.eq("Die")));
|
||||
|
||||
Filter result = tested.handle(expression);
|
||||
assertThat(result.toString()).isEqualTo("(&(cn=John Doe)(|(sn=Doe)(sn=Die)))");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNot() {
|
||||
Expression<?> expression = person.fullName.eq("John Doe").not();
|
||||
Filter result = tested.handle(expression);
|
||||
assertThat(result.toString()).isEqualTo("(!(cn=John Doe))");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIsLike() {
|
||||
Expression<?> expression = person.fullName.like("kalle*");
|
||||
Filter result = tested.handle(expression);
|
||||
assertThat(result.toString()).isEqualTo("(cn=kalle*)");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testStartsWith() {
|
||||
Expression<?> expression = person.fullName.startsWith("kalle");
|
||||
Filter result = tested.handle(expression);
|
||||
assertThat(result.toString()).isEqualTo("(cn=kalle*)");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEndsWith() {
|
||||
Expression<?> expression = person.fullName.endsWith("kalle");
|
||||
Filter result = tested.handle(expression);
|
||||
assertThat(result.toString()).isEqualTo("(cn=*kalle)");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testContains() {
|
||||
Expression<?> expression = person.fullName.contains("kalle");
|
||||
Filter result = tested.handle(expression);
|
||||
assertThat(result.toString()).isEqualTo("(cn=*kalle*)");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNotNull() {
|
||||
Expression<?> expression = person.fullName.isNotNull();
|
||||
Filter result = tested.handle(expression);
|
||||
assertThat(result.toString()).isEqualTo("(cn=*)");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNull() {
|
||||
Expression<?> expression = person.fullName.isNull();
|
||||
Filter result = tested.handle(expression);
|
||||
assertThat(result.toString()).isEqualTo("(!(cn=*))");
|
||||
}
|
||||
}
|
||||
@@ -1,12 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns:ldap="http://www.springframework.org/schema/ldap"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
|
||||
http://www.springframework.org/schema/ldap http://www.springframework.org/schema/ldap/spring-ldap.xsd">
|
||||
|
||||
<ldap:context-source password="apassword" url="ldap://localhost:389" username="uid=admin"/>
|
||||
<ldap:ldap-template />
|
||||
|
||||
<ldap:repositories base-package="org.springframework.ldap.config" />
|
||||
</beans>
|
||||
@@ -17,7 +17,6 @@ include 'test/integration-tests-sunone'
|
||||
include 'test/integration-tests-ad'
|
||||
include 'samples/plain'
|
||||
include 'samples/odm'
|
||||
include 'samples/user-admin'
|
||||
|
||||
|
||||
rootProject.children.each { p->
|
||||
|
||||
@@ -65,23 +65,7 @@ dependencies {
|
||||
}
|
||||
}
|
||||
|
||||
task generateQueryDSL(type: JavaCompile, group: 'build', description: 'Generates the QueryDSL query types') {
|
||||
source = sourceSets.main.java
|
||||
classpath = configurations.compile
|
||||
options.compilerArgs = [
|
||||
"-proc:only",
|
||||
"-processor", "org.springframework.ldap.repository.support.LdapAnnotationProcessor"
|
||||
]
|
||||
destinationDir = sourceSets.generated.java.srcDirs.iterator().next()
|
||||
}
|
||||
|
||||
compileJava {
|
||||
dependsOn generateQueryDSL
|
||||
source generateQueryDSL.destinationDir
|
||||
}
|
||||
|
||||
compileGeneratedJava {
|
||||
dependsOn generateQueryDSL
|
||||
options.warnings = false
|
||||
classpath += sourceSets.main.runtimeClasspath
|
||||
}
|
||||
@@ -90,8 +74,6 @@ clean {
|
||||
delete sourceSets.generated.java.srcDirs
|
||||
}
|
||||
|
||||
ideaModule.dependsOn generateQueryDSL
|
||||
|
||||
idea {
|
||||
module {
|
||||
sourceDirs += file('build/generated-src')
|
||||
|
||||
@@ -1,12 +0,0 @@
|
||||
package org.springframework.ldap.itest.config;
|
||||
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.ldap.repository.config.EnableLdapRepositories;
|
||||
|
||||
/**
|
||||
* @author Mattias Hellborg Arthursson
|
||||
*/
|
||||
@Configuration
|
||||
@EnableLdapRepositories(basePackages = "org.springframework.ldap.itest.repositories")
|
||||
public class SpringLdapConfiguration {
|
||||
}
|
||||
@@ -1,11 +0,0 @@
|
||||
package org.springframework.ldap.itest.repositories;
|
||||
|
||||
import org.springframework.data.querydsl.QueryDslPredicateExecutor;
|
||||
import org.springframework.ldap.itest.odm.Person;
|
||||
import org.springframework.ldap.repository.LdapRepository;
|
||||
|
||||
/**
|
||||
* @author Mattias Hellborg Arthursson
|
||||
*/
|
||||
public interface PersonQueryDslRepository extends LdapRepository<Person>, QueryDslPredicateExecutor<Person> {
|
||||
}
|
||||
@@ -1,34 +0,0 @@
|
||||
/*
|
||||
* Copyright 2005-2013 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.ldap.itest.repositories;
|
||||
|
||||
import org.springframework.ldap.itest.odm.Person;
|
||||
import org.springframework.ldap.repository.Query;
|
||||
import org.springframework.ldap.repository.LdapRepository;
|
||||
|
||||
/**
|
||||
* @author Mattias Hellborg Arthursson
|
||||
*/
|
||||
public interface PersonRepository extends LdapRepository<Person> {
|
||||
@Query("(sn={0})")
|
||||
Iterable<Person> findByLastName(String lastName);
|
||||
|
||||
@Query("(uid={0})")
|
||||
Person findByUid(String uid);
|
||||
|
||||
Person findByTelephoneNumber(String phoneNumber);
|
||||
}
|
||||
@@ -1,10 +0,0 @@
|
||||
package org.springframework.ldap.itest.repositories;
|
||||
|
||||
import org.springframework.ldap.itest.odm.PersonWithDnAnnotations;
|
||||
import org.springframework.ldap.repository.LdapRepository;
|
||||
|
||||
/**
|
||||
* @author Mattias Hellborg Arthursson
|
||||
*/
|
||||
public interface PersonWithDnAnnotationsRepository extends LdapRepository<PersonWithDnAnnotations> {
|
||||
}
|
||||
@@ -1,68 +0,0 @@
|
||||
/*
|
||||
* Copyright 2005-2016 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.ldap.itest.odm;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.ldap.core.LdapTemplate;
|
||||
import org.springframework.ldap.itest.AbstractLdapTemplateIntegrationTest;
|
||||
import org.springframework.ldap.repository.support.QueryDslLdapQuery;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* @author Mattias Hellborg Arthursson
|
||||
*/
|
||||
@ContextConfiguration(locations = {"/conf/ldapTemplateTestContext.xml"})
|
||||
public class LdapTemplateQueryDslLdapQueryITest extends AbstractLdapTemplateIntegrationTest {
|
||||
@Autowired
|
||||
private LdapTemplate tested;
|
||||
private QPerson qperson;
|
||||
private QueryDslLdapQuery<Person> query;
|
||||
|
||||
@Before
|
||||
public void prepareTestedInstance() {
|
||||
qperson = QPerson.person;
|
||||
query = new QueryDslLdapQuery<Person>(tested, qperson);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUniqueResult() {
|
||||
Person person = query.where(qperson.commonName.eq("Some Person3")).uniqueResult();
|
||||
|
||||
assertThat(person).isNotNull();
|
||||
assertThat(person.getCommonName()).isEqualTo("Some Person3");
|
||||
assertThat(person.getSurname()).isEqualTo("Person3");
|
||||
assertThat(person.getDesc().get(0)).isEqualTo("Sweden, Company1, Some Person3");
|
||||
assertThat(person.getTelephoneNumber()).isEqualTo("+46 555-123654");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testList() {
|
||||
List<Person> persons = query.where(qperson.commonName.eq("Some Person3")).list();
|
||||
|
||||
Person person = persons.get(0);
|
||||
assertThat(person.getCommonName()).isEqualTo("Some Person3");
|
||||
assertThat(person.getSurname()).isEqualTo("Person3");
|
||||
assertThat(person.getDesc().get(0)).isEqualTo("Sweden, Company1, Some Person3");
|
||||
assertThat(person.getTelephoneNumber()).isEqualTo("+46 555-123654");
|
||||
}
|
||||
}
|
||||
@@ -1,59 +0,0 @@
|
||||
/*
|
||||
* Copyright 2005-2016 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.ldap.itest.repository;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.ldap.itest.AbstractLdapTemplateIntegrationTest;
|
||||
import org.springframework.ldap.itest.odm.Person;
|
||||
import org.springframework.ldap.itest.repositories.PersonRepository;
|
||||
import org.springframework.ldap.support.LdapUtils;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
|
||||
import javax.naming.Name;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* Tests for Spring LDAP automatic repository scan functionality enabled with
|
||||
* {@link org.springframework.ldap.repository.config.EnableLdapRepositories}.
|
||||
*
|
||||
* @author Mattias Hellborg Arthursson
|
||||
*/
|
||||
@ContextConfiguration(locations = {"/conf/repositoryScanAnnotationTestContext.xml"})
|
||||
public class RepositoryScanAnnotationConfiguredITest extends AbstractLdapTemplateIntegrationTest {
|
||||
private static final Name PERSON3_DN = LdapUtils.newLdapName("cn=Some Person3, ou=Company1, ou=Sweden");
|
||||
|
||||
@Autowired
|
||||
private PersonRepository tested;
|
||||
|
||||
@Test
|
||||
public void testExists() {
|
||||
assertThat(tested.exists(PERSON3_DN)).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFindOneWithDn() {
|
||||
Person person = tested.findOne(PERSON3_DN);
|
||||
|
||||
assertThat(person).isNotNull();
|
||||
assertThat(person.getCommonName()).isEqualTo("Some Person3");
|
||||
assertThat(person.getSurname()).isEqualTo("Person3");
|
||||
assertThat(person.getDesc().get(0)).isEqualTo("Sweden, Company1, Some Person3");
|
||||
assertThat(person.getTelephoneNumber()).isEqualTo("+46 555-123654");
|
||||
}
|
||||
}
|
||||
@@ -1,261 +0,0 @@
|
||||
/*
|
||||
* Copyright 2005-2016 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.ldap.itest.repository;
|
||||
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
import org.junit.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.ldap.itest.AbstractLdapTemplateIntegrationTest;
|
||||
import org.springframework.ldap.itest.odm.Person;
|
||||
import org.springframework.ldap.itest.repositories.PersonRepository;
|
||||
import org.springframework.ldap.support.LdapNameBuilder;
|
||||
import org.springframework.ldap.support.LdapUtils;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
|
||||
import javax.naming.Name;
|
||||
import javax.naming.ldap.LdapName;
|
||||
import java.util.Arrays;
|
||||
import java.util.Iterator;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.fail;
|
||||
import static org.springframework.ldap.query.LdapQueryBuilder.query;
|
||||
|
||||
/**
|
||||
* Tests for Spring LDAP automatic repository scan functionality.
|
||||
*
|
||||
* @author Mattias Hellborg Arthursson
|
||||
*/
|
||||
@ContextConfiguration(locations = {"/conf/repositoryScanTestContext.xml"})
|
||||
public class RepositoryScanITest extends AbstractLdapTemplateIntegrationTest {
|
||||
private static final Name PERSON1_DN = LdapUtils.newLdapName("cn=Some Person, ou=Company1, ou=Sweden");
|
||||
private static final Name PERSON3_DN = LdapUtils.newLdapName("cn=Some Person3, ou=Company1, ou=Sweden");
|
||||
|
||||
@Autowired
|
||||
private PersonRepository tested;
|
||||
|
||||
@Test
|
||||
public void testExists() {
|
||||
assertThat(tested.exists(PERSON3_DN)).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFindOneWithDn() {
|
||||
Person person = tested.findOne(PERSON3_DN);
|
||||
|
||||
assertThat(person).isNotNull();
|
||||
assertThat(person.getCommonName()).isEqualTo("Some Person3");
|
||||
assertThat(person.getSurname()).isEqualTo("Person3");
|
||||
assertThat(person.getDesc().get(0)).isEqualTo("Sweden, Company1, Some Person3");
|
||||
assertThat(person.getTelephoneNumber()).isEqualTo("+46 555-123654");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void verifyThatFindOneWithNonexistingDnReturnsNull() {
|
||||
Person person = tested.findOne(LdapUtils.newLdapName("cn=unknown"));
|
||||
assertThat(person).isNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFindOneWithQuery() {
|
||||
Person person = tested.findOne(query().where("cn").is("Some Person3"));
|
||||
|
||||
assertThat(person).isNotNull();
|
||||
assertThat(person.getCommonName()).isEqualTo("Some Person3");
|
||||
assertThat(person.getSurname()).isEqualTo("Person3");
|
||||
assertThat(person.getDesc().get(0)).isEqualTo("Sweden, Company1, Some Person3");
|
||||
assertThat(person.getTelephoneNumber()).isEqualTo("+46 555-123654");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFindAll() {
|
||||
Iterable<Person> result = tested.findAll();
|
||||
assertThat(countIterable(result)).isEqualTo(5);
|
||||
|
||||
for (Person person : result) {
|
||||
if(StringUtils.equals(person.getCommonName(), "Some Person3")) {
|
||||
assertThat(person.getCommonName()).isEqualTo("Some Person3");
|
||||
assertThat(person.getSurname()).isEqualTo("Person3");
|
||||
assertThat(person.getDesc().get(0)).isEqualTo("Sweden, Company1, Some Person3");
|
||||
assertThat(person.getTelephoneNumber()).isEqualTo("+46 555-123654");
|
||||
|
||||
// Done
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
fail("Entry not found");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFindAllWithQuery() {
|
||||
Iterable<Person> persons = tested.findAll(query().where("cn").is("Some Person3"));
|
||||
|
||||
assertThat(persons).isNotNull();
|
||||
Iterator<Person> iterator = persons.iterator();
|
||||
Person person = iterator.next();
|
||||
|
||||
assertThat(person.getCommonName()).isEqualTo("Some Person3");
|
||||
assertThat(person.getSurname()).isEqualTo("Person3");
|
||||
assertThat(person.getDesc().get(0)).isEqualTo("Sweden, Company1, Some Person3");
|
||||
assertThat(person.getTelephoneNumber()).isEqualTo("+46 555-123654");
|
||||
|
||||
assertThat(iterator.hasNext()).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFindAllWithIterable() {
|
||||
Iterable<Person> persons = tested.findAll(Arrays.asList(PERSON1_DN, PERSON3_DN));
|
||||
Iterator<Person> iterator = persons.iterator();
|
||||
Person person = iterator.next();
|
||||
|
||||
assertThat(person.getCommonName()).isEqualTo("Some Person");
|
||||
assertThat(person.getSurname()).isEqualTo("Person");
|
||||
assertThat(person.getDesc().get(0)).isEqualTo("Sweden, Company1, Some Person");
|
||||
assertThat(person.getTelephoneNumber()).isEqualTo("+46 555-123456");
|
||||
|
||||
person = iterator.next();
|
||||
|
||||
assertThat(person.getCommonName()).isEqualTo("Some Person3");
|
||||
assertThat(person.getSurname()).isEqualTo("Person3");
|
||||
assertThat(person.getDesc().get(0)).isEqualTo("Sweden, Company1, Some Person3");
|
||||
assertThat(person.getTelephoneNumber()).isEqualTo("+46 555-123654");
|
||||
|
||||
assertThat(iterator.hasNext()).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void verifyThatFindAllWithIterableFiltersNotFoundEntries() {
|
||||
Iterable<Person> persons = tested.findAll(Arrays.asList(LdapUtils.newLdapName("cn=unknown"), PERSON3_DN));
|
||||
Iterator<Person> iterator = persons.iterator();
|
||||
Person person = iterator.next();
|
||||
|
||||
assertThat(person.getCommonName()).isEqualTo("Some Person3");
|
||||
assertThat(person.getSurname()).isEqualTo("Person3");
|
||||
assertThat(person.getDesc().get(0)).isEqualTo("Sweden, Company1, Some Person3");
|
||||
assertThat(person.getTelephoneNumber()).isEqualTo("+46 555-123654");
|
||||
|
||||
assertThat(iterator.hasNext()).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCount() {
|
||||
assertThat(tested.count()).isEqualTo(5);
|
||||
}
|
||||
|
||||
private int countIterable(Iterable<?> iterable) {
|
||||
int count = 0;
|
||||
|
||||
for (Object o : iterable) {
|
||||
count++;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCreate() {
|
||||
Person person = new Person();
|
||||
LdapName dn = LdapNameBuilder.newInstance("ou=company1,ou=Sweden")
|
||||
.add("cn", "New Person").build();
|
||||
person.setDn(dn);
|
||||
person.setCommonName("New Person");
|
||||
person.setSurname("Person");
|
||||
person.setDesc(Arrays.asList("This is the description"));
|
||||
person.setTelephoneNumber("0123456");
|
||||
person.setNew();
|
||||
tested.save(person);
|
||||
|
||||
assertThat(tested.count()).isEqualTo(6);
|
||||
|
||||
person = tested.findOne(dn);
|
||||
|
||||
assertThat(person.getCommonName()).isEqualTo("New Person");
|
||||
assertThat(person.getSurname()).isEqualTo("Person");
|
||||
assertThat(person.getDesc().get(0)).isEqualTo("This is the description");
|
||||
assertThat(person.getTelephoneNumber()).isEqualTo("0123456");
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void verifyThatCreateWithNoIdSetAndNotAbleToCalculateThrowsIllegalArgument() {
|
||||
Person person = new Person();
|
||||
person.setCommonName("New Person");
|
||||
person.setSurname("Person");
|
||||
person.setDesc(Arrays.asList("This is the description"));
|
||||
person.setTelephoneNumber("0123456");
|
||||
person.setNew();
|
||||
tested.save(person);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUpdate() {
|
||||
Person person = tested.findOne(query().where("cn").is("Some Person3"));
|
||||
|
||||
person.setDesc(Arrays.asList("New Description"));
|
||||
tested.save(person);
|
||||
|
||||
person = tested.findOne(query().where("cn").is("Some Person3"));
|
||||
|
||||
assertThat(person.getCommonName()).isEqualTo("Some Person3");
|
||||
assertThat(person.getSurname()).isEqualTo("Person3");
|
||||
assertThat(person.getDesc().get(0)).isEqualTo("New Description");
|
||||
assertThat(person.getTelephoneNumber()).isEqualTo("+46 555-123654");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDelete() {
|
||||
Person person = tested.findOne(query().where("cn").is("Some Person3"));
|
||||
tested.delete(person);
|
||||
|
||||
Person found = tested.findOne(query().where("cn").is("Some Person3"));
|
||||
assertThat(found).isNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDeleteWithIterable() {
|
||||
Iterable<Person> found = tested.findAll(Arrays.asList(PERSON1_DN, PERSON3_DN));
|
||||
tested.delete(found);
|
||||
|
||||
assertThat(tested.count()).isEqualTo(3);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFindByLastName() {
|
||||
Iterable<Person> found = tested.findByLastName("Person3");
|
||||
assertThat(countIterable(found)).isEqualTo(1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFindByUid() {
|
||||
Person person = tested.findByUid("some.person3");
|
||||
|
||||
assertThat(person.getCommonName()).isEqualTo("Some Person3");
|
||||
assertThat(person.getSurname()).isEqualTo("Person3");
|
||||
assertThat(person.getDesc().get(0)).isEqualTo("Sweden, Company1, Some Person3");
|
||||
assertThat(person.getTelephoneNumber()).isEqualTo("+46 555-123654");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFindByPhoneNumber() {
|
||||
Person person = tested.findByTelephoneNumber("+46 555-123654");
|
||||
|
||||
assertThat(person.getCommonName()).isEqualTo("Some Person3");
|
||||
assertThat(person.getSurname()).isEqualTo("Person3");
|
||||
assertThat(person.getDesc().get(0)).isEqualTo("Sweden, Company1, Some Person3");
|
||||
assertThat(person.getTelephoneNumber()).isEqualTo("+46 555-123654");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,73 +0,0 @@
|
||||
/*
|
||||
* Copyright 2005-2016 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.ldap.itest.repository;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.ldap.itest.AbstractLdapTemplateIntegrationTest;
|
||||
import org.springframework.ldap.itest.odm.Person;
|
||||
import org.springframework.ldap.itest.odm.QPerson;
|
||||
import org.springframework.ldap.itest.repositories.PersonQueryDslRepository;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* Tests for Spring LDAP automatic repository scan functionality.
|
||||
*
|
||||
* @author Mattias Hellborg Arthursson
|
||||
*/
|
||||
@ContextConfiguration(locations = {"/conf/repositoryScanTestContext.xml"})
|
||||
public class RepositoryScanQueryDslITest extends AbstractLdapTemplateIntegrationTest {
|
||||
|
||||
@Autowired
|
||||
private PersonQueryDslRepository tested;
|
||||
|
||||
@Test
|
||||
public void testFindOneWithPredicate() {
|
||||
QPerson person = QPerson.person;
|
||||
|
||||
Person found = tested.findOne(person.commonName.eq("Some Person3"));
|
||||
|
||||
assertThat(found).isNotNull();
|
||||
assertThat(found.getCommonName()).isEqualTo("Some Person3");
|
||||
assertThat(found.getSurname()).isEqualTo("Person3");
|
||||
assertThat(found.getDesc().get(0)).isEqualTo("Sweden, Company1, Some Person3");
|
||||
assertThat(found.getTelephoneNumber()).isEqualTo("+46 555-123654");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFindAllWithPredicate() {
|
||||
QPerson person = QPerson.person;
|
||||
|
||||
Iterable<Person> foundPersons = tested.findAll(person.commonName.eq("Some Person3"));
|
||||
|
||||
Person found = foundPersons.iterator().next();
|
||||
assertThat(found.getCommonName()).isEqualTo("Some Person3");
|
||||
assertThat(found.getSurname()).isEqualTo("Person3");
|
||||
assertThat(found.getDesc().get(0)).isEqualTo("Sweden, Company1, Some Person3");
|
||||
assertThat(found.getTelephoneNumber()).isEqualTo("+46 555-123654");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCountWithPredicate() {
|
||||
QPerson person = QPerson.person;
|
||||
|
||||
long count = tested.count(person.commonName.eq("Some Person3"));
|
||||
assertThat(count).isEqualTo(1);
|
||||
}
|
||||
}
|
||||
@@ -1,81 +0,0 @@
|
||||
/*
|
||||
* Copyright 2005-2016 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.ldap.itest.repository;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.ldap.itest.AbstractLdapTemplateIntegrationTest;
|
||||
import org.springframework.ldap.itest.odm.PersonWithDnAnnotations;
|
||||
import org.springframework.ldap.itest.repositories.PersonWithDnAnnotationsRepository;
|
||||
import org.springframework.ldap.support.LdapUtils;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.springframework.ldap.query.LdapQueryBuilder.query;
|
||||
|
||||
/**
|
||||
* Tests for Spring LDAP automatic repository scan functionality.
|
||||
*
|
||||
* @author Mattias Hellborg Arthursson
|
||||
*/
|
||||
@ContextConfiguration(locations = {"/conf/repositoryScanTestContext.xml"})
|
||||
public class RepositoryScanWithDnAnnotationsITest extends AbstractLdapTemplateIntegrationTest {
|
||||
@Autowired
|
||||
private PersonWithDnAnnotationsRepository tested;
|
||||
|
||||
@Test
|
||||
public void verifyThatCreateWithNoIdSetWillAutomaticallyCalculateDn() {
|
||||
PersonWithDnAnnotations person = new PersonWithDnAnnotations();
|
||||
person.setCommonName("New Person");
|
||||
person.setSurname("Person");
|
||||
person.setDesc(Arrays.asList("This is the description"));
|
||||
person.setTelephoneNumber("0123456");
|
||||
person.setCountry("Sweden");
|
||||
person.setCompany("company1");
|
||||
|
||||
assertThat(person.getDn()).isNull();
|
||||
|
||||
tested.save(person);
|
||||
|
||||
assertThat(person.getDn()).isNotNull();
|
||||
|
||||
assertThat(tested.count()).isEqualTo(6);
|
||||
|
||||
person = tested.findOne(query().where("cn").is("New Person"));
|
||||
|
||||
assertThat(person.getCommonName()).isEqualTo("New Person");
|
||||
assertThat(person.getSurname()).isEqualTo("Person");
|
||||
assertThat(person.getDesc().get(0)).isEqualTo("This is the description");
|
||||
assertThat(person.getTelephoneNumber()).isEqualTo("0123456");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void verifyThatMovedEntryGetsUpdatedId() {
|
||||
PersonWithDnAnnotations found = tested.findOne(query().where("cn").is("Some Person3"));
|
||||
assertThat(found).isNotNull();
|
||||
|
||||
assertThat(found.getDn()).isEqualTo(LdapUtils.newLdapName("cn=Some Person3,ou=company1,ou=Sweden"));
|
||||
|
||||
found.setCompany("company2");
|
||||
|
||||
tested.save(found);
|
||||
|
||||
assertThat(found.getDn()).isEqualTo(LdapUtils.newLdapName("cn=Some Person3,ou=company2,ou=Sweden"));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user