LDAP-266: Finishing up spring-data repository support.

This commit is contained in:
Mattias Hellborg Arthursson
2013-10-14 16:09:28 +02:00
parent bad3a071ce
commit 983d183bb1
25 changed files with 778 additions and 9 deletions

View File

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

View File

@@ -17,10 +17,18 @@
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);
}

View File

@@ -0,0 +1,61 @@
/*
* 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.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.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
/**
* 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, c=Sweden");
@Autowired
private PersonRepository tested;
@Test
public void testExists() {
assertTrue(tested.exists(PERSON3_DN));
}
@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());
}
}

View File

@@ -19,7 +19,6 @@ 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.core.LdapTemplate;
import org.springframework.ldap.itest.AbstractLdapTemplateIntegrationTest;
import org.springframework.ldap.itest.odm.Person;
import org.springframework.ldap.itest.repositories.PersonRepository;
@@ -236,4 +235,31 @@ public class RepositoryScanITest extends AbstractLdapTemplateIntegrationTest {
assertEquals(3, tested.count());
}
@Test
public void testFindByLastName() {
Iterable<Person> found = tested.findByLastName("Person3");
assertEquals(1, countIterable(found));
}
@Test
public void testFindByUid() {
Person person = tested.findByUid("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());
}
@Test
public void testFindByPhoneNumber() {
Person person = tested.findByTelephoneNumber("+46 555-123654");
assertEquals("Some Person3", person.getCommonName());
assertEquals("Person3", person.getSurname());
assertEquals("Sweden, Company1, Some Person3", person.getDesc().get(0));
assertEquals("+46 555-123654", person.getTelephoneNumber());
}
}

View File

@@ -0,0 +1,26 @@
<?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"
xmlns:context="http://www.springframework.org/schema/context"
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 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.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 />
<context:component-scan base-package="org.springframework.ldap.itest.config" />
<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>