From 37d5fadf4d0d2c0a76c1ad33aef007019d741e14 Mon Sep 17 00:00:00 2001 From: Mark Paluch Date: Fri, 27 Jan 2017 15:04:15 +0100 Subject: [PATCH] #251 - Add example for Spring Data LDAP. --- ldap/example/README.md | 15 +++ ldap/example/pom.xml | 15 +++ .../ApplicationConfiguration.java | 27 +++++ .../java/example.springdata.ldap/Person.java | 45 ++++++++ .../PersonRepository.java | 46 ++++++++ .../PersonRepositoryIntegrationTests.java | 101 ++++++++++++++++++ .../example/springdata/ldap/package-info.java | 4 + .../src/test/resources/application.properties | 3 + .../src/test/resources/test-server.ldif | 48 +++++++++ ldap/pom.xml | 43 ++++++++ pom.xml | 1 + 11 files changed, 348 insertions(+) create mode 100644 ldap/example/README.md create mode 100644 ldap/example/pom.xml create mode 100644 ldap/example/src/main/java/example.springdata.ldap/ApplicationConfiguration.java create mode 100644 ldap/example/src/main/java/example.springdata.ldap/Person.java create mode 100644 ldap/example/src/main/java/example.springdata.ldap/PersonRepository.java create mode 100644 ldap/example/src/test/java/example/springdata/ldap/PersonRepositoryIntegrationTests.java create mode 100644 ldap/example/src/test/java/example/springdata/ldap/package-info.java create mode 100644 ldap/example/src/test/resources/application.properties create mode 100644 ldap/example/src/test/resources/test-server.ldif create mode 100644 ldap/pom.xml diff --git a/ldap/example/README.md b/ldap/example/README.md new file mode 100644 index 00000000..b61983f8 --- /dev/null +++ b/ldap/example/README.md @@ -0,0 +1,15 @@ +# Spring Data LDAP - Example + +This project contains samples of Spring Data (LDAP). + + +```java +public interface PersonRepository extends CrudRepository { + + List findByUid(String uid); + + List findByLastnameStartsWith(String prefix); +} +``` + +The test cases in `PersonRepositoryIntegrationTests` show basic interaction to search, create and modify objects stored in a LDAP repository. diff --git a/ldap/example/pom.xml b/ldap/example/pom.xml new file mode 100644 index 00000000..d3975f66 --- /dev/null +++ b/ldap/example/pom.xml @@ -0,0 +1,15 @@ + + 4.0.0 + + spring-data-ldap-example + + Spring Data LDAP - Example + + + org.springframework.data.examples + spring-data-ldap-examples + 1.0.0.BUILD-SNAPSHOT + + + diff --git a/ldap/example/src/main/java/example.springdata.ldap/ApplicationConfiguration.java b/ldap/example/src/main/java/example.springdata.ldap/ApplicationConfiguration.java new file mode 100644 index 00000000..27ff26d0 --- /dev/null +++ b/ldap/example/src/main/java/example.springdata.ldap/ApplicationConfiguration.java @@ -0,0 +1,27 @@ +/* + * Copyright 2017 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 example.springdata.ldap; + +import org.springframework.boot.autoconfigure.SpringBootApplication; + +/** + * Test configuration to spin up a in-memory LDAP server (see {@code application.properties}). Also enables Spring Data + * repositories for LDAP. + * + * @author Mark Paluch + */ +@SpringBootApplication +class ApplicationConfiguration {} diff --git a/ldap/example/src/main/java/example.springdata.ldap/Person.java b/ldap/example/src/main/java/example.springdata.ldap/Person.java new file mode 100644 index 00000000..9f0f0156 --- /dev/null +++ b/ldap/example/src/main/java/example.springdata.ldap/Person.java @@ -0,0 +1,45 @@ +/* + * Copyright 2017 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 example.springdata.ldap; + +import lombok.Data; + +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; + +/** + * {@link Person} object stored inside LDAP. + * + * @author Mark Paluch + */ +@Entry(base = "ou=people,dc=springframework,dc=org", objectClasses = "inetOrgPerson") +@Data +public class Person { + + @Id private Name id; + + @DnAttribute(value = "uid", index = 3) private String uid; + + @Attribute(name = "cn") private String fullName; + + @Attribute(name = "sn") private String lastname; + + private String userPassword; +} diff --git a/ldap/example/src/main/java/example.springdata.ldap/PersonRepository.java b/ldap/example/src/main/java/example.springdata.ldap/PersonRepository.java new file mode 100644 index 00000000..083a04b9 --- /dev/null +++ b/ldap/example/src/main/java/example.springdata.ldap/PersonRepository.java @@ -0,0 +1,46 @@ +/* + * Copyright 2017 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 example.springdata.ldap; + +import java.util.List; + +import javax.naming.Name; + +import org.springframework.data.repository.CrudRepository; + +/** + * Repository interface to manage {@link Person} instances. + * + * @author Mark Paluch + */ +public interface PersonRepository extends CrudRepository { + + /** + * Find by UserId. + * + * @param uid + * @return + */ + List findByUid(String uid); + + /** + * Prefix search on {@link Person#getLastname()}. + * + * @param prefix + * @return + */ + List findByLastnameStartsWith(String prefix); +} diff --git a/ldap/example/src/test/java/example/springdata/ldap/PersonRepositoryIntegrationTests.java b/ldap/example/src/test/java/example/springdata/ldap/PersonRepositoryIntegrationTests.java new file mode 100644 index 00000000..c2cf2778 --- /dev/null +++ b/ldap/example/src/test/java/example/springdata/ldap/PersonRepositoryIntegrationTests.java @@ -0,0 +1,101 @@ +/* + * Copyright 2017 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 example.springdata.ldap; + +import static org.assertj.core.api.Assertions.*; + +import java.util.List; + +import javax.naming.InvalidNameException; +import javax.naming.ldap.LdapName; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +/** + * Integration tests for {@link PersonRepository}. + * + * @author Mark Paluch + */ +@RunWith(SpringJUnit4ClassRunner.class) +@SpringBootTest +public class PersonRepositoryIntegrationTests { + + @Autowired PersonRepository personRepository; + + /** + * Find a {@link Person} by its Id that is a full DN. + * + * @throws InvalidNameException + */ + @Test + public void findOneByName() throws InvalidNameException { + + Person person = personRepository.findOne(new LdapName("uid=bob,ou=people,dc=springframework,dc=org")); + + assertThat(person).isNotNull(); + assertThat(person.getFullName()).isEqualTo("Bob Hamilton"); + assertThat(person.getLastname()).isEqualTo("Hamilton"); + assertThat(person.getUid()).isEqualTo("bob"); + } + + /** + * Find all entries in the base path. + */ + @Test + public void findAll() { + + Iterable people = personRepository.findAll(); + + assertThat(people).hasSize(3).extracting("uid").contains("bob", "joe", "ben"); + } + + /** + * Find all {@link Person} objects starting with {@code Ham} in the field {@code lastname}. + */ + @Test + public void findByLastname() { + + List people = personRepository.findByLastnameStartsWith("Ham"); + + assertThat(people).hasSize(1).extracting("uid").contains("bob"); + } + + /** + * Add and remove a user to the LDAP repository. + * + * @throws InvalidNameException + */ + @Test + public void addUser() throws InvalidNameException { + + Person walter = new Person(); + walter.setFullName("Walter White"); + walter.setUid("heisenberg"); + walter.setLastname("White"); + + personRepository.save(walter); + + List people = personRepository.findByUid("heisenberg"); + + assertThat(people).hasSize(1).extracting("fullName").contains("Walter White"); + + personRepository.delete(people.get(0)); + } +} diff --git a/ldap/example/src/test/java/example/springdata/ldap/package-info.java b/ldap/example/src/test/java/example/springdata/ldap/package-info.java new file mode 100644 index 00000000..2135f34e --- /dev/null +++ b/ldap/example/src/test/java/example/springdata/ldap/package-info.java @@ -0,0 +1,4 @@ +/** + * Package showing usage of Spring Data LDAP Repositories. + */ +package example.springdata.ldap; diff --git a/ldap/example/src/test/resources/application.properties b/ldap/example/src/test/resources/application.properties new file mode 100644 index 00000000..7a45a01f --- /dev/null +++ b/ldap/example/src/test/resources/application.properties @@ -0,0 +1,3 @@ +spring.ldap.embedded.ldif=test-server.ldif +spring.ldap.embedded.base-dn=dc=springframework,dc=org +spring.ldap.base=dc=springframework,dc=org diff --git a/ldap/example/src/test/resources/test-server.ldif b/ldap/example/src/test/resources/test-server.ldif new file mode 100644 index 00000000..25009b5e --- /dev/null +++ b/ldap/example/src/test/resources/test-server.ldif @@ -0,0 +1,48 @@ +dn: dc=springframework,dc=org +objectClass: top +objectClass: domain + +dn: ou=groups,dc=springframework,dc=org +objectclass: top +objectclass: organizationalUnit +ou: groups + +dn: ou=people,dc=springframework,dc=org +objectclass: top +objectclass: organizationalUnit +ou: people + +dn: ou=otherpeople,dc=springframework,dc=org +objectclass: top +objectclass: organizationalUnit +ou: otherpeople + +dn: uid=ben,ou=people,dc=springframework,dc=org +objectclass: top +objectclass: person +objectclass: organizationalPerson +objectclass: inetOrgPerson +cn: Ben Alex +sn: Alex +uid: ben +userPassword: {SHA}nFCebWjxfaLbHHG1Qk5UU4trbvQ= + +dn: uid=bob,ou=people,dc=springframework,dc=org +objectclass: top +objectclass: person +objectclass: organizationalPerson +objectclass: inetOrgPerson +cn: Bob Hamilton +sn: Hamilton +uid: bob +userPassword: bobspassword + +dn: uid=joe,ou=otherpeople,dc=springframework,dc=org +objectclass: top +objectclass: person +objectclass: organizationalPerson +objectclass: inetOrgPerson +cn: Joe Smeth +sn: Smeth +uid: joe +userPassword: joespassword diff --git a/ldap/pom.xml b/ldap/pom.xml new file mode 100644 index 00000000..f32f2fc6 --- /dev/null +++ b/ldap/pom.xml @@ -0,0 +1,43 @@ + + 4.0.0 + + spring-data-ldap-examples + pom + + + org.springframework.data.examples + spring-data-examples + 1.0.0.BUILD-SNAPSHOT + + + Spring Data LDAP - Examples + Sample projects for Spring Data LDAP + http://projects.spring.io/spring-data-ldap + 2016-2017 + + + example + + + + + + org.springframework.boot + spring-boot-starter-data-ldap + + + + org.springframework.boot + spring-boot-starter + + + + com.unboundid + unboundid-ldapsdk + runtime + + + + + \ No newline at end of file diff --git a/pom.xml b/pom.xml index ff32daeb..8738d223 100644 --- a/pom.xml +++ b/pom.xml @@ -28,6 +28,7 @@ web bom neo4j + ldap