LDAP-266: Initial effort for automatic repository support.
This commit is contained in:
@@ -1,8 +1,10 @@
|
||||
package org.springframework.ldap.itest.odm;
|
||||
|
||||
import org.springframework.data.domain.Persistable;
|
||||
import org.springframework.ldap.odm.annotations.Attribute;
|
||||
import org.springframework.ldap.odm.annotations.Entry;
|
||||
import org.springframework.ldap.odm.annotations.Id;
|
||||
import org.springframework.ldap.odm.annotations.Transient;
|
||||
|
||||
import javax.naming.Name;
|
||||
import java.util.List;
|
||||
@@ -11,7 +13,7 @@ import java.util.List;
|
||||
* @author Mattias Hellborg Arthursson
|
||||
*/
|
||||
@Entry(objectClasses = { "inetOrgPerson", "organizationalPerson", "person", "top" })
|
||||
public class Person {
|
||||
public class Person implements Persistable<Name> {
|
||||
@Id
|
||||
private Name dn;
|
||||
|
||||
@@ -30,6 +32,19 @@ public class Person {
|
||||
@Attribute(name = "telephoneNumber")
|
||||
private String telephoneNumber;
|
||||
|
||||
@Transient
|
||||
private boolean isNew;
|
||||
|
||||
@Override
|
||||
public Name getId() {
|
||||
return getDn();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isNew() {
|
||||
return isNew;
|
||||
}
|
||||
|
||||
public Name getDn() {
|
||||
return dn;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
/*
|
||||
* 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.data.repository.CrudRepository;
|
||||
import org.springframework.ldap.itest.odm.Person;
|
||||
|
||||
import javax.naming.Name;
|
||||
|
||||
/**
|
||||
* @author Mattias Hellborg Arthursson
|
||||
*/
|
||||
public interface PersonRepository extends CrudRepository<Person, Name> {
|
||||
}
|
||||
@@ -0,0 +1,172 @@
|
||||
/*
|
||||
* 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.Assert;
|
||||
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 java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
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
|
||||
*/
|
||||
@ContextConfiguration(locations = {"/conf/repositoryScanTestContext.xml"})
|
||||
public class RepositoryScanITest extends AbstractLdapTemplateIntegrationTest {
|
||||
@Autowired
|
||||
private LdapTemplate ldapTemplate;
|
||||
|
||||
@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());
|
||||
}
|
||||
|
||||
// @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);
|
||||
// }
|
||||
// }
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
<?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-2.0.xsd http://www.springframework.org/schema/ldap http://www.springframework.org/schema/ldap/spring-ldap.xsd">
|
||||
<import resource="classpath:/conf/commonTestContext.xml" />
|
||||
|
||||
<ldap:context-source
|
||||
password="${password}"
|
||||
url="ldap://localhost:1888"
|
||||
username="${userDn}"
|
||||
base="dc=jayway,dc=se" />
|
||||
|
||||
<ldap:ldap-template />
|
||||
<ldap:repositories base-package="org.springframework.ldap.itest.repositories" />
|
||||
|
||||
<bean id="dummy" class="org.springframework.ldap.test.TestContextSourceFactoryBean">
|
||||
<property name="defaultPartitionSuffix" value="dc=jayway,dc=se" />
|
||||
<property name="defaultPartitionName" value="jayway" />
|
||||
<property name="ldifFile" value="classpath:/setup_data.ldif" />
|
||||
<property name="port" value="1888" />
|
||||
<property name="contextSource" ref="contextSource" />
|
||||
</bean>
|
||||
</beans>
|
||||
Reference in New Issue
Block a user