LDAP-266: Integration tests of automatic repository support.

This commit is contained in:
Mattias Hellborg Arthursson
2013-10-14 09:21:24 +02:00
parent 1e2a060459
commit 2c3d1935dc
9 changed files with 381 additions and 141 deletions

View File

@@ -0,0 +1,47 @@
/*
* 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);
}

View File

@@ -24,6 +24,7 @@ import org.springframework.ldap.core.LdapOperations;
import java.io.Serializable;
/**
* Factory to create {@link LdapRepository} instances.
* @author Mattias Hellborg Arthursson
* @since 2.0
*/

View File

@@ -1,3 +1,19 @@
/*
* 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.Repository;
@@ -9,6 +25,8 @@ import org.springframework.util.Assert;
import javax.naming.Name;
/**
* {@link org.springframework.beans.factory.FactoryBean} to create {@link LdapRepository} instances.
*
* @author Mattias Hellborg Arthursson
* @since 2.0
*/

View File

@@ -16,8 +16,8 @@
package org.springframework.ldap.repository;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.data.domain.Persistable;
import org.springframework.data.repository.CrudRepository;
import org.springframework.ldap.NameNotFoundException;
import org.springframework.ldap.core.LdapOperations;
import org.springframework.ldap.core.support.CountNameClassPairCallbackHandler;
@@ -28,6 +28,7 @@ import org.springframework.util.Assert;
import javax.naming.Name;
import java.util.Iterator;
import java.util.LinkedList;
import static org.springframework.ldap.query.LdapQueryBuilder.query;
@@ -37,7 +38,7 @@ import static org.springframework.ldap.query.LdapQueryBuilder.query;
* @author Mattias Hellborg Arthursson
* @since 2.0
*/
public class SimpleLdapRepository<T> implements CrudRepository<T, Name> {
public class SimpleLdapRepository<T> implements LdapRepository<T> {
private static final String OBJECTCLASS_ATTRIBUTE = "objectclass";
private final LdapOperations ldapOperations;
private final ObjectDirectoryMapper odm;
@@ -64,7 +65,7 @@ public class SimpleLdapRepository<T> implements CrudRepository<T, Name> {
Persistable persistable = (Persistable) entity;
return persistable.isNew();
} else {
return id != null;
return id == null;
}
}
@@ -74,15 +75,21 @@ public class SimpleLdapRepository<T> implements CrudRepository<T, Name> {
Name declaredId = odm.getId(entity);
Name calculatedId = odm.getCalculatedId(entity);
if(isNew(entity, declaredId)) {
if(declaredId == null) {
odm.setId(entity, calculatedId);
if (isNew(entity, declaredId)) {
if (declaredId == null) {
if (calculatedId != null) {
odm.setId(entity, calculatedId);
} else {
throw new IllegalStateException(String.format("Unable to calculate id of entry of class %s - " +
"ID not set and unable to calculate new ID. Missing @DnAttribute annotations with index?",
entity.getClass()));
}
}
ldapOperations.create(entity);
} else {
ldapOperations.update(entity);
if(declaredId != calculatedId) {
if (declaredId != calculatedId) {
odm.setId(entity, calculatedId);
}
}
@@ -92,7 +99,7 @@ public class SimpleLdapRepository<T> implements CrudRepository<T, Name> {
@Override
public <S extends T> Iterable<S> save(Iterable<S> entities) {
return new DelegatingIterable<S, S>(entities, new Function<S, S>() {
return new TransformingIterable<S, S>(entities, new Function<S, S>() {
@Override
public S transform(S entry) {
return save(entry);
@@ -110,8 +117,25 @@ public class SimpleLdapRepository<T> implements CrudRepository<T, Name> {
}
}
@Override
public Iterable<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;
}
@@ -122,12 +146,21 @@ public class SimpleLdapRepository<T> implements CrudRepository<T, Name> {
@Override
public Iterable<T> findAll(final Iterable<Name> names) {
return new DelegatingIterable<Name, T>(names, new Function<Name, T>() {
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
@@ -154,11 +187,11 @@ public class SimpleLdapRepository<T> implements CrudRepository<T, Name> {
delete(findAll());
}
private static class DelegatingIterable<F, T> implements Iterable<T> {
private final static class TransformingIterable<F, T> implements Iterable<T> {
private final Iterable<F> target;
private final Function<F, T> function;
private DelegatingIterable(Iterable<F> target, Function<F, T> function) {
private TransformingIterable(Iterable<F> target, Function<F, T> function) {
this.target = target;
this.function = function;
}

View File

@@ -33,13 +33,17 @@ public class Person implements Persistable<Name> {
private String telephoneNumber;
@Transient
private boolean isNew;
private boolean isNew = false;
@Override
public Name getId() {
return getDn();
}
public void setNew() {
isNew = true;
}
@Override
public boolean isNew() {
return isNew;

View File

@@ -16,13 +16,11 @@
package org.springframework.ldap.itest.repositories;
import org.springframework.data.repository.CrudRepository;
import org.springframework.ldap.itest.odm.Person;
import javax.naming.Name;
import org.springframework.ldap.repository.LdapRepository;
/**
* @author Mattias Hellborg Arthursson
*/
public interface PersonRepository extends CrudRepository<Person, Name> {
public interface PersonRepository extends LdapRepository<Person> {
}

View File

@@ -0,0 +1,10 @@
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> {
}

View File

@@ -13,160 +13,227 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.ldap.itest.repository;
import org.junit.Assert;
import org.apache.commons.lang.StringUtils;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.ldap.core.LdapTemplate;
import org.springframework.ldap.itest.AbstractLdapTemplateIntegrationTest;
import org.springframework.ldap.itest.odm.Person;
import org.springframework.ldap.itest.repositories.PersonRepository;
import org.springframework.ldap.odm.core.OdmException;
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.List;
import java.util.Iterator;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.springframework.ldap.query.LdapQueryBuilder.query;
/**
* Tests for Spring LDAP automatic repository scan functionality.
*
* @author Ulrik Sandberg
* @author Mattias Hellborg Arthursson
*/
@ContextConfiguration(locations = {"/conf/repositoryScanTestContext.xml"})
public class RepositoryScanITest extends AbstractLdapTemplateIntegrationTest {
@Autowired
private LdapTemplate ldapTemplate;
private static final Name PERSON1_DN = LdapUtils.newLdapName("cn=Some Person, ou=Company1, c=Sweden");
private static final Name PERSON3_DN = LdapUtils.newLdapName("cn=Some Person3, ou=Company1, c=Sweden");
@Autowired
private PersonRepository tested;
@Test
public void testFindOne() {
// Person person = tested.findOne(LdapUtils.newLdapName("cn=Some Person3, ou=Company1, c=Sweden"));
//
// assertNotNull(person);
// Assert.assertEquals("Some Person3", person.getCommonName());
// Assert.assertEquals("Person3", person.getSurname());
// Assert.assertEquals("Sweden, Company1, Some Person3", person.getDesc().get(0));
// Assert.assertEquals("+46 555-123654", person.getTelephoneNumber());
public void testExists() {
assertTrue(tested.exists(PERSON3_DN));
}
// @Test
// public void testFindByDn() {
// Person person = tested.findByDn(LdapUtils.newLdapName("cn=Some Person3,ou=company1,c=Sweden"), Person.class);
//
// assertNotNull(person);
// Assert.assertEquals("Some Person3", person.getCommonName());
// Assert.assertEquals("Person3", person.getSurname());
// Assert.assertEquals("Sweden, Company1, Some Person3", person.getDesc().get(0));
// Assert.assertEquals("+46 555-123654", person.getTelephoneNumber());
// }
//
// @Test(expected = OdmException.class)
// public void testFindByDnThrowsExceptionOnInvalidEntry() {
// tested.findByDn(LdapUtils.newLdapName("ou=company1,c=Sweden"), Person.class);
// }
//
// @Test(expected = EmptyResultDataAccessException.class)
// public void testFindOneThrowsEmptyResultIfNotFound() {
// tested.findOne(query()
// .where("cn").is("This cn does not exist"), Person.class);
// }
//
// @Test
// public void testFind() {
// List<Person> persons = tested.find(query()
// .where("cn").is("Some Person3"), Person.class);
//
// Assert.assertEquals(1, persons.size());
// Person person = persons.get(0);
//
// assertNotNull(person);
// Assert.assertEquals("Some Person3", person.getCommonName());
// Assert.assertEquals("Person3", person.getSurname());
// Assert.assertEquals("Sweden, Company1, Some Person3", person.getDesc().get(0));
// Assert.assertEquals("+46 555-123654", person.getTelephoneNumber());
// }
//
// @Test
// public void testFindInCountry() {
// List<Person> persons = tested.find(query()
// .base("c=Sweden")
// .where("cn").isPresent(), Person.class);
//
// Assert.assertEquals(4, persons.size());
// Person person = persons.get(0);
//
// assertNotNull(person);
// }
//
// @Test
// public void testFindAll() {
// List<Person> result = tested.findAll(Person.class);
// Assert.assertEquals(5, result.size());
// }
//
// @Test
// public void testCreate() {
// Person person = new Person();
// person.setDn(LdapNameBuilder.newLdapName("ou=company1,c=Sweden")
// .add("cn", "New Person").build());
// person.setCommonName("New Person");
// person.setSurname("Person");
// person.setDesc(Arrays.asList("This is the description"));
// person.setTelephoneNumber("0123456");
//
// tested.create(person);
//
// Assert.assertEquals(6, tested.findAll(Person.class).size());
//
// person = tested.findOne(query()
// .where("cn").is("New Person"), Person.class);
//
// Assert.assertEquals("New Person", person.getCommonName());
// Assert.assertEquals("Person", person.getSurname());
// Assert.assertEquals("This is the description", person.getDesc().get(0));
// Assert.assertEquals("0123456", person.getTelephoneNumber());
// }
//
// @Test
// public void testUpdate() {
// Person person = tested.findOne(query()
// .where("cn").is("Some Person3"), Person.class);
//
// person.setDesc(Arrays.asList("New Description"));
// tested.update(person);
//
// person = tested.findOne(query()
// .where("cn").is("Some Person3"), Person.class);
//
// Assert.assertEquals("Some Person3", person.getCommonName());
// Assert.assertEquals("Person3", person.getSurname());
// Assert.assertEquals("New Description", person.getDesc().get(0));
// Assert.assertEquals("+46 555-123654", person.getTelephoneNumber());
// }
//
// @Test
// public void testDelete() {
// Person person = tested.findOne(query()
// .where("cn").is("Some Person3"), Person.class);
//
// tested.delete(person);
//
// try {
// tested.findOne(query().where("cn").is("Some Person3"), Person.class);
// fail("EmptyResultDataAccessException e");
// } catch (EmptyResultDataAccessException e) {
// Assert.assertTrue(true);
// }
// }
@Test
public void testFindOneWithDn() {
Person person = tested.findOne(PERSON3_DN);
assertNotNull(person);
assertEquals("Some Person3", person.getCommonName());
assertEquals("Person3", person.getSurname());
assertEquals("Sweden, Company1, Some Person3", person.getDesc().get(0));
assertEquals("+46 555-123654", person.getTelephoneNumber());
}
@Test
public void verifyThatFindOneWithNonexistingDnReturnsNull() {
Person person = tested.findOne(LdapUtils.newLdapName("dn=unknown"));
assertNull(person);
}
@Test
public void testFindOneWithQuery() {
Person person = tested.findOne(query().where("cn").is("Some Person3"));
assertNotNull(person);
assertEquals("Some Person3", person.getCommonName());
assertEquals("Person3", person.getSurname());
assertEquals("Sweden, Company1, Some Person3", person.getDesc().get(0));
assertEquals("+46 555-123654", person.getTelephoneNumber());
}
@Test
public void testFindAll() {
Iterable<Person> result = tested.findAll();
assertEquals(5, countIterable(result));
for (Person person : result) {
if(StringUtils.equals(person.getCommonName(), "Some Person3")) {
assertEquals("Some Person3", person.getCommonName());
assertEquals("Person3", person.getSurname());
assertEquals("Sweden, Company1, Some Person3", person.getDesc().get(0));
assertEquals("+46 555-123654", person.getTelephoneNumber());
// Done
return;
}
}
fail("Entry not found");
}
@Test
public void testFindAllWithQuery() {
Iterable<Person> persons = tested.findAll(query().where("cn").is("Some Person3"));
assertNotNull(persons);
Iterator<Person> iterator = persons.iterator();
Person person = iterator.next();
assertEquals("Some Person3", person.getCommonName());
assertEquals("Person3", person.getSurname());
assertEquals("Sweden, Company1, Some Person3", person.getDesc().get(0));
assertEquals("+46 555-123654", person.getTelephoneNumber());
assertFalse(iterator.hasNext());
}
@Test
public void testFindAllWithIterable() {
Iterable<Person> persons = tested.findAll(Arrays.asList(PERSON1_DN, PERSON3_DN));
Iterator<Person> iterator = persons.iterator();
Person person = iterator.next();
assertEquals("Some Person", person.getCommonName());
assertEquals("Person", person.getSurname());
assertEquals("Sweden, Company1, Some Person", person.getDesc().get(0));
assertEquals("+46 555-123456", person.getTelephoneNumber());
person = iterator.next();
assertEquals("Some Person3", person.getCommonName());
assertEquals("Person3", person.getSurname());
assertEquals("Sweden, Company1, Some Person3", person.getDesc().get(0));
assertEquals("+46 555-123654", person.getTelephoneNumber());
assertFalse(iterator.hasNext());
}
@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();
assertEquals("Some Person3", person.getCommonName());
assertEquals("Person3", person.getSurname());
assertEquals("Sweden, Company1, Some Person3", person.getDesc().get(0));
assertEquals("+46 555-123654", person.getTelephoneNumber());
assertFalse(iterator.hasNext());
}
@Test
public void testCount() {
assertEquals(5, tested.count());
}
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.newLdapName("ou=company1,c=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);
assertEquals(6, tested.count());
person = tested.findOne(dn);
assertEquals("New Person", person.getCommonName());
assertEquals("Person", person.getSurname());
assertEquals("This is the description", person.getDesc().get(0));
assertEquals("0123456", person.getTelephoneNumber());
}
@Test(expected = IllegalStateException.class)
public void verifyThatCreateWithNoIdSetAndNotAbleToCalculateThrowsIllegalState() {
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"));
assertEquals("Some Person3", person.getCommonName());
assertEquals("Person3", person.getSurname());
assertEquals("New Description", person.getDesc().get(0));
assertEquals("+46 555-123654", person.getTelephoneNumber());
}
@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"));
assertNull(found);
}
@Test
public void testDeleteWithIterable() {
Iterable<Person> found = tested.findAll(Arrays.asList(PERSON1_DN, PERSON3_DN));
tested.delete(found);
assertEquals(3, tested.count());
}
}

View File

@@ -0,0 +1,62 @@
/*
* 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.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.query.LdapQueryBuilder;
import org.springframework.test.context.ContextConfiguration;
import java.util.Arrays;
import static org.junit.Assert.assertEquals;
/**
* 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");
tested.save(person);
assertEquals(6, tested.count());
person = tested.findOne(LdapQueryBuilder.query().where("cn").is("New Person"));
assertEquals("New Person", person.getCommonName());
assertEquals("Person", person.getSurname());
assertEquals("This is the description", person.getDesc().get(0));
assertEquals("0123456", person.getTelephoneNumber());
}
}