1 package org.jellyfish.implementation;
2
3 import java.util.ArrayList;
4 import java.util.List;
5 import java.util.SortedSet;
6
7 import javax.naming.NamingException;
8 import javax.naming.directory.Attribute;
9 import javax.naming.directory.Attributes;
10 import javax.naming.directory.BasicAttribute;
11 import javax.naming.directory.BasicAttributes;
12
13 import junit.framework.TestCase;
14 import mock.org.apache.avalon.framework.logger.LoggerMock;
15 import mock
16 .org
17 .apache
18 .avalon
19 .framework
20 .service
21 .ServiceManagerMock
22 .ServiceManagerMock;
23
24 import org.jellyfish.ConfigurationManager;
25 import org.jellyfish.ConfigurationManagerMock;
26 import org.jellyfish.ConnectionManager;
27 import org.jellyfish.ConnectionManagerMock;
28 import org.jellyfish.MappingManager;
29 import org.jellyfish.ObjectMapNotFoundException;
30 import org.jellyfish.config.AttributeMapping;
31 import org.jellyfish.config.Config;
32 import org.jellyfish.config.ObjectMapping;
33 import org.jellyfish.model.Person;
34
35 public class MappingManagerDefaultTest extends TestCase {
36 private MappingManagerDefault mappingManager;
37 private Attributes attributes;
38 private ServiceManagerMock serviceManagerMock;
39 private ConfigurationManagerMock configurationManagerMock;
40 private ConnectionManagerMock connectionManagerMock;
41 private LoggerMock loggerMock;
42
43 public MappingManagerDefaultTest(String arg0) {
44 super(arg0);
45 }
46
47 protected void setUp() throws Exception {
48 configurationManagerMock = new ConfigurationManagerMock();
49 configurationManagerMock.getConfigurationReturn = createConfig();
50 connectionManagerMock = new ConnectionManagerMock();
51 attributes = createAttributes();
52 serviceManagerMock = new ServiceManagerMock();
53 serviceManagerMock.lookupMap.put(
54 ConfigurationManager.ROLE,
55 configurationManagerMock);
56 serviceManagerMock.lookupMap.put(
57 ConnectionManager.ROLE,
58 connectionManagerMock);
59 mappingManager = new MappingManagerDefault();
60 mappingManager.service(serviceManagerMock);
61 loggerMock = new LoggerMock();
62 mappingManager.enableLogging(loggerMock);
63 }
64
65 private Attributes createAttributes() {
66 Attributes attributes = new BasicAttributes();
67 attributes.put(MappingManager.OBJECTCLASS, "person");
68 attributes.put("cn", "Joe");
69 attributes.put("sn", "Smith");
70 Attribute telephoneNumbersAttribute =
71 new BasicAttribute("telephoneNumber");
72 telephoneNumbersAttribute.add("1111111111");
73 telephoneNumbersAttribute.add("9999999999");
74 attributes.put(telephoneNumbersAttribute);
75 return attributes;
76 }
77
78 private Config createConfig() {
79 Config config = new Config();
80 config.setBaseDn("dc=jellyfish,dc=org");
81 config.setUserDn("cn=${username},ou=jellyfish");
82 List objectMappings = new ArrayList();
83 ObjectMapping objectMapping = new ObjectMapping();
84 objectMapping.setJavaName(Person.class.getName());
85 objectMapping.setLdapName("person");
86 objectMapping.setDn("cn=${firstName},ou=people");
87 objectMapping.getAttributeMappings().add(
88 new AttributeMapping("firstName", "cn"));
89 objectMapping.getAttributeMappings().add(
90 new AttributeMapping("lastName", "sn"));
91 objectMapping.getAttributeMappings().add(
92 new AttributeMapping("manager", "mgr"));
93 objectMapping.getAttributeMappings().add(
94 new AttributeMapping(
95 "telephoneNumbers",
96 "telephoneNumber",
97 "java.util.TreeSet"));
98 objectMappings.add(objectMapping);
99 config.setObjectMappings(objectMappings);
100 return config;
101 }
102
103 private Person createPerson() {
104 Person person = new Person("Joe", "Smith");
105 person.setManager(new Person("Mike", "Jones"));
106 return person;
107 }
108
109 public void testMapToAttributes() throws NamingException {
110 Person person = createPerson();
111
112 Attributes attributes = mappingManager.mapToAttributes(person);
113
114 assertTrue(configurationManagerMock.getConfigurationCalled);
115
116 assertNotNull(attributes);
117 Attribute cnAttribute = attributes.get("cn");
118 assertNotNull(cnAttribute);
119 assertEquals(person.getFirstName(), cnAttribute.get());
120
121 Attribute snAttribute = attributes.get("sn");
122 assertNotNull(snAttribute);
123 assertEquals(person.getLastName(), snAttribute.get());
124 }
125
126 public void testMapToAttributesNullProperty() throws NamingException {
127 Person person = createPerson();
128 person.setLastName(null);
129
130 Attributes attributes = mappingManager.mapToAttributes(person);
131
132 assertTrue(configurationManagerMock.getConfigurationCalled);
133
134 assertNotNull(attributes);
135 Attribute cnAttribute = attributes.get("cn");
136 assertNotNull(cnAttribute);
137 assertEquals(person.getFirstName(), cnAttribute.get());
138
139 assertNull(attributes.get("sn"));
140 }
141
142 public void testMapNestedObjectToAttributes() throws NamingException {
143 Person person = createPerson();
144 Attributes attributes = mappingManager.mapToAttributes(person);
145 assertEquals(
146 "cn="
147 + person.getManager().getFirstName()
148 + ",ou=people,dc=jellyfish,dc=org",
149 attributes.get("mgr").get());
150 }
151
152 public void testGetObjectDn() {
153 Person person = createPerson();
154 String objectDn = mappingManager.getObjectDn(person);
155 assertEquals(
156 "cn=" + person.getFirstName() + ",ou=people,dc=jellyfish,dc=org",
157 objectDn);
158 }
159
160 public void testGetUserDn() {
161 String userDn = mappingManager.getUserDn("jellyfish");
162 assertEquals("cn=jellyfish,ou=jellyfish,dc=jellyfish,dc=org", userDn);
163 }
164
165 public void testGetObjectMappingFromAttributes()
166 throws ObjectMapNotFoundException {
167 ObjectMapping objectMapping =
168 mappingManager.getObjectMapping(attributes);
169 assertEquals(
170 configurationManagerMock
171 .getConfigurationReturn
172 .getObjectMappings()
173 .get(
174 0),
175 objectMapping);
176 }
177
178 public void testMapFromAttributes() throws NamingException {
179 Person mappedPerson =
180 (Person) mappingManager.mapFromAttributes(attributes);
181 assertEquals(attributes.get("cn").get(), mappedPerson.getFirstName());
182 assertEquals(attributes.get("sn").get(), mappedPerson.getLastName());
183 assertTrue(mappedPerson.getTelephoneNumbers() instanceof SortedSet);
184 assertTrue(mappedPerson.getTelephoneNumbers().contains("1111111111"));
185 assertTrue(mappedPerson.getTelephoneNumbers().contains("9999999999"));
186 }
187
188 public void testMapFromAttributesNullAttribute() throws NamingException {
189 attributes.remove("sn");
190 try {
191 Person mappedPerson =
192 (Person) mappingManager.mapFromAttributes(attributes);
193 assertEquals(
194 attributes.get("cn").get(),
195 mappedPerson.getFirstName());
196 } catch (NullPointerException nullPointerException) {
197 fail("NullPointerException should be avoided");
198 }
199 }
200
201 public void testMapFromAttributesNestedObject() throws NamingException {
202 Person mappedPerson =
203 (Person) mappingManager.mapFromAttributes(attributes);
204 assertEquals(attributes.get("cn").get(), mappedPerson.getFirstName());
205 assertEquals(attributes.get("sn").get(), mappedPerson.getLastName());
206 }
207
208 public void testMapFromAttributesNoObjectclass() {
209 attributes.remove(MappingManager.OBJECTCLASS);
210 Person mappedPerson =
211 (Person) mappingManager.mapFromAttributes(attributes);
212 }
213 }
This page was automatically generated by Maven