Initial import.

This commit is contained in:
Mark Paluch
2016-11-21 15:53:45 +01:00
commit 9bbf0389e2
47 changed files with 3624 additions and 0 deletions

View File

@@ -0,0 +1,22 @@
/*
* Copyright 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.data.ldap.config;
/**
* @author Mattias Hellborg Arthursson
*/
public class DummyEntity {
}

View File

@@ -0,0 +1,27 @@
/*
* Copyright 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.data.ldap.config;
import javax.naming.Name;
import org.springframework.data.repository.CrudRepository;
/**
* @author Mattias Hellborg Arthursson
*/
public interface DummyLdapRepository extends CrudRepository<DummyEntity, Name> {
}

View File

@@ -0,0 +1,212 @@
/*
* Copyright 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.data.ldap.repository;
import static org.assertj.core.api.Assertions.*;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.*;
import java.util.Arrays;
import java.util.Iterator;
import javax.naming.Name;
import javax.naming.ldap.LdapName;
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.data.ldap.repository.support.SimpleLdapRepository;
import org.springframework.ldap.support.LdapUtils;
/**
* @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();
}
}

View File

@@ -0,0 +1,52 @@
/*
* Copyright 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.data.ldap.repository.config;
import static org.assertj.core.api.Assertions.*;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;
import org.springframework.data.ldap.config.DummyLdapRepository;
import org.springframework.data.ldap.repository.config.AnnotationConfigTests.Config;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;
/**
* @author Mattias Hellborg Arthursson
* @author Mark Paluch
*/
@RunWith(SpringRunner.class)
@ContextConfiguration(classes = Config.class)
public class AnnotationConfigTests {
@Autowired ApplicationContext context;
@Test
public void testAnnotationConfig() {
DummyLdapRepository repository = context.getBean(DummyLdapRepository.class);
assertThat(repository).isNotNull();
}
@Configuration
@ImportResource("classpath:/ldap-annotation-config.xml")
@EnableLdapRepositories(basePackageClasses = DummyLdapRepository.class)
static class Config {}
}

View File

@@ -0,0 +1,25 @@
/*
* Copyright 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.data.ldap.repository.config;
import org.springframework.context.annotation.Configuration;
/**
* @author Mattias Hellborg Arthursson
*/
@Configuration
@EnableLdapRepositories(basePackages = "org.springframework.ldap.config")
public class SpringLdapConfiguration {}

View File

@@ -0,0 +1,30 @@
/*
* Copyright 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.data.ldap.repository.query;
import java.util.List;
import org.springframework.data.ldap.repository.LdapRepository;
import org.springframework.data.ldap.repository.support.BaseUnitTestPerson;
import org.springframework.data.ldap.repository.support.UnitTestPerson;
/**
* @author Rob Winch
*/
public interface BaseTestPersonRepository extends LdapRepository<UnitTestPerson> {
List<BaseUnitTestPerson> findByFullName(String name);
}

View File

@@ -0,0 +1,147 @@
/*
* Copyright 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.data.ldap.repository.query;
import static org.assertj.core.api.Assertions.*;
import java.lang.reflect.Method;
import org.junit.Before;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.ldap.repository.support.BaseUnitTestPerson;
import org.springframework.data.ldap.repository.support.UnitTestPerson;
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.query.LdapQuery;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;
/**
* @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);
}
}

View File

@@ -0,0 +1,54 @@
/*
* Copyright 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.data.ldap.repository.query;
import java.util.List;
import org.springframework.data.ldap.repository.LdapRepository;
import org.springframework.data.ldap.repository.support.UnitTestPerson;
/**
* @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);
}

View File

@@ -0,0 +1,49 @@
/*
* Copyright 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.data.ldap.repository.support;
import java.util.List;
import javax.naming.Name;
import org.springframework.ldap.odm.annotations.Attribute;
import org.springframework.ldap.odm.annotations.DnAttribute;
import org.springframework.ldap.odm.annotations.Entry;
import org.springframework.ldap.odm.annotations.Id;
import org.springframework.ldap.odm.annotations.Transient;
/**
* @author Rob Winch
*/
@Entry(base = "ou=someOu", objectClasses = { "inetOrgPerson", "organizationalPerson", "person", "top" })
public class BaseUnitTestPerson {
@Id private Name dn;
@Attribute(name = "cn") @DnAttribute("cn") private String fullName;
@Attribute(name = "sn") private String lastName;
@Attribute(name = "description") private List<String> description;
@Transient @DnAttribute("c") private String country;
@Transient @DnAttribute("ou") private String company;
// This should be automatically found
private String telephoneNumber;
}

View File

@@ -0,0 +1,58 @@
/*
* Copyright 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.data.ldap.repository.support;
import static com.querydsl.core.types.PathMetadataFactory.*;
import javax.annotation.Generated;
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;
/**
* 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);
}
}

View File

@@ -0,0 +1,122 @@
/*
* Copyright 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.data.ldap.repository.support;
import static org.assertj.core.api.Assertions.*;
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 com.querydsl.core.types.Expression;
/**
* @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=*))");
}
}

View File

@@ -0,0 +1,58 @@
/*
* Copyright 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.data.ldap.repository.support;
import java.util.List;
import javax.naming.Name;
import org.springframework.ldap.odm.annotations.Attribute;
import org.springframework.ldap.odm.annotations.DnAttribute;
import org.springframework.ldap.odm.annotations.Entry;
import org.springframework.ldap.odm.annotations.Id;
import org.springframework.ldap.odm.annotations.Transient;
/**
* @author Mattias Hellborg Arthursson
*/
@Entry(objectClasses = {"inetOrgPerson", "organizationalPerson", "person", "top"})
public class UnitTestPerson {
@Id
private Name dn;
@Attribute(name = "cn")
@DnAttribute("cn")
private String fullName;
@Attribute(name = "sn")
private String lastName;
@Attribute(name = "description")
private List<String> description;
@Transient
@DnAttribute("c")
private String country;
@Transient
@DnAttribute("ou")
private String company;
// This should be automatically found
private String telephoneNumber;
}