changeset 23:99472565ba0b

Extract the code to create a new user to the User object
author Dirk Olmes <dirk.olmes@codedo.de>
date Tue, 25 Oct 2016 15:39:52 +0000
parents 886a28c5898c
children f6e07a3d95a2
files jndi/src/test/java/de/codedo/jndi/JndiTestCase.java jndi/src/test/java/de/codedo/jndi/User.java
diffstat 2 files changed, 58 insertions(+), 29 deletions(-) [+]
line wrap: on
line diff
--- a/jndi/src/test/java/de/codedo/jndi/JndiTestCase.java	Tue Oct 25 15:21:13 2016 +0000
+++ b/jndi/src/test/java/de/codedo/jndi/JndiTestCase.java	Tue Oct 25 15:39:52 2016 +0000
@@ -13,35 +13,6 @@
 
 	@Ignore
 	@Test
-	public void connectToJndi() throws Exception
-	{
-		DirContext context = createContext();
-
-		NamingEnumeration<NameClassPair> enumeration = context.list(USER_BASE);
-		while (enumeration.hasMore())
-		{
-			NameClassPair pair = enumeration.next();
-
-			String dn = pair.getName() + "," + USER_BASE;
-			LdapContext ldapContext = (LdapContext) context.lookup(dn);
-			System.out.println(ldapContext);
-
-			/*
-			Attributes attributes = context.getAttributes(dn);
-			NamingEnumeration attributesEnum = attributes.getAll();
-			while (attributesEnum.hasMore())
-			{
-				Attribute attribute = (Attribute)attributesEnum.next();
-				System.out.println(attribute.getID());				
-			}
-			*/
-		}
-
-		context.close();
-	}
-
-	@Ignore
-	@Test
 	public void createInLdap() throws Exception
 	{
 		DirContext context = createContext();
@@ -69,6 +40,16 @@
 	}
 
 	@Test
+	public void createInLdap2() throws Exception
+	{
+		DirContext context = createContext();
+		User user = new User("psb", "Fernand", "Pirschelbär");
+		user.create(context);
+		context.close();	
+	}
+
+	@Ignore
+	@Test
 	public void findHighestGroupNumber() throws Exception
 	{
 		DirContext context = createContext();
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jndi/src/test/java/de/codedo/jndi/User.java	Tue Oct 25 15:39:52 2016 +0000
@@ -0,0 +1,48 @@
+package de.codedo.jndi;
+
+import javax.naming.*;
+import javax.naming.directory.*;
+
+class User extends Object
+{
+	private static final String USER_BASE = "ou=users,dc=ldap,dc=example,dc=org";
+
+	private	Attribute _cn;
+	private Attribute _uid;
+	private Attribute _sn;
+	private Attribute _givenName;
+
+	User(String login, String givenName, String surname)
+	{
+		super();
+		_uid = new BasicAttribute("uid", login);
+		_cn = new BasicAttribute("cn", login);
+		_sn = new BasicAttribute("sn", surname);
+		_givenName = new BasicAttribute("givenName", givenName);
+	}
+
+	public void create(DirContext context) throws NamingException
+	{
+		Attribute objectClass = createObjectClass();
+
+		Attributes attributes = new BasicAttributes();
+		attributes.put(objectClass);
+		attributes.put(_cn);
+		attributes.put(_uid);
+		attributes.put(_sn);
+		attributes.put(_givenName);
+
+		String dn = "uid=" + _uid.get() + "," + USER_BASE; 
+		context.createSubcontext(dn, attributes);
+	}
+
+	private Attribute createObjectClass()
+	{
+		Attribute objectClass = new BasicAttribute("objectClass"); 
+		objectClass.add("inetOrgPerson");
+		objectClass.add("organizationalPerson");
+		objectClass.add("person");
+		objectClass.add("top");
+		return objectClass;
+	}
+}