1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.directory.server.core.entry;
21
22
23 import java.util.HashMap;
24 import java.util.HashSet;
25 import java.util.Map;
26 import java.util.Set;
27
28 import org.apache.directory.server.schema.bootstrap.ApacheSchema;
29 import org.apache.directory.server.schema.bootstrap.ApachemetaSchema;
30 import org.apache.directory.server.schema.bootstrap.BootstrapSchemaLoader;
31 import org.apache.directory.server.schema.bootstrap.CoreSchema;
32 import org.apache.directory.server.schema.bootstrap.CosineSchema;
33 import org.apache.directory.server.schema.bootstrap.InetorgpersonSchema;
34 import org.apache.directory.server.schema.bootstrap.Schema;
35 import org.apache.directory.server.schema.bootstrap.SystemSchema;
36 import org.apache.directory.server.schema.registries.DefaultOidRegistry;
37 import org.apache.directory.server.schema.registries.DefaultRegistries;
38 import org.apache.directory.server.schema.registries.OidRegistry;
39 import org.apache.directory.server.schema.registries.Registries;
40 import org.apache.directory.shared.ldap.entry.EntryAttribute;
41 import org.apache.directory.shared.ldap.name.LdapDN;
42 import org.apache.directory.shared.ldap.schema.DeepTrimToLowerNormalizer;
43 import org.apache.directory.shared.ldap.schema.OidNormalizer;
44 import org.apache.directory.shared.ldap.util.StringTools;
45
46 import static org.junit.Assert.assertEquals;
47 import org.junit.BeforeClass;
48 import org.junit.Test;
49
50
51
52
53
54
55
56
57 public class ServerEntrySerializerTest
58 {
59 private static BootstrapSchemaLoader loader;
60 private static Registries registries;
61 private static OidRegistry oidRegistry;
62 private static Map<String, OidNormalizer> oids;
63 private static Map<String, OidNormalizer> oidOids;
64
65
66
67
68 @BeforeClass
69 public static void setup() throws Exception
70 {
71 loader = new BootstrapSchemaLoader();
72 oidRegistry = new DefaultOidRegistry();
73 registries = new DefaultRegistries( "bootstrap", loader, oidRegistry );
74
75
76 Set<Schema> bootstrapSchemas = new HashSet<Schema>();
77 bootstrapSchemas.add( new ApachemetaSchema() );
78 bootstrapSchemas.add( new ApacheSchema() );
79 bootstrapSchemas.add( new CoreSchema() );
80 bootstrapSchemas.add( new SystemSchema() );
81 bootstrapSchemas.add( new InetorgpersonSchema() );
82 bootstrapSchemas.add( new CosineSchema() );
83 loader.loadWithDependencies( bootstrapSchemas, registries );
84
85 oids = new HashMap<String, OidNormalizer>();
86
87 oids.put( "dc", new OidNormalizer( "dc", new DeepTrimToLowerNormalizer() ) );
88 oids.put( "domaincomponent", new OidNormalizer( "dc", new DeepTrimToLowerNormalizer() ) );
89 oids.put( "0.9.2342.19200300.100.1.25", new OidNormalizer( "dc", new DeepTrimToLowerNormalizer() ) );
90 oids.put( "ou", new OidNormalizer( "ou", new DeepTrimToLowerNormalizer() ) );
91 oids.put( "organizationalUnitName", new OidNormalizer( "ou", new DeepTrimToLowerNormalizer() ) );
92 oids.put( "2.5.4.11", new OidNormalizer( "ou", new DeepTrimToLowerNormalizer() ) );
93
94
95
96 oidOids = new HashMap<String, OidNormalizer>();
97
98 oidOids.put( "dc", new OidNormalizer( "0.9.2342.19200300.100.1.25", new DeepTrimToLowerNormalizer() ) );
99 oidOids.put( "domaincomponent", new OidNormalizer( "0.9.2342.19200300.100.1.25", new DeepTrimToLowerNormalizer() ) );
100 oidOids.put( "0.9.2342.19200300.100.1.25",
101 new OidNormalizer( "0.9.2342.19200300.100.1.25", new DeepTrimToLowerNormalizer() ) );
102 oidOids.put( "ou", new OidNormalizer( "2.5.4.11", new DeepTrimToLowerNormalizer() ) );
103 oidOids.put( "organizationalUnitName", new OidNormalizer( "2.5.4.11", new DeepTrimToLowerNormalizer() ) );
104 oidOids.put( "2.5.4.11", new OidNormalizer( "2.5.4.11", new DeepTrimToLowerNormalizer() ) );
105 }
106
107
108 @Test public void testSerializeEmtpyServerEntry() throws Exception
109 {
110 LdapDN dn = LdapDN.EMPTY_LDAPDN;
111 ServerEntry entry = new DefaultServerEntry( registries, dn );
112
113 ServerEntrySerializer ses = new ServerEntrySerializer( registries );
114
115 byte[] data = ses.serialize( entry );
116
117 ServerEntry result = (ServerEntry)ses.deserialize( data );
118
119 assertEquals( entry, result );
120 }
121
122
123 @Test public void testSerializeDNServerEntry() throws Exception
124 {
125 LdapDN dn = new LdapDN( "cn=text, dc=example, dc=com" );
126 dn.normalize( oids );
127
128 ServerEntry entry = new DefaultServerEntry( registries, dn );
129
130 ServerEntrySerializer ses = new ServerEntrySerializer( registries );
131
132 byte[] data = ses.serialize( entry );
133
134 ServerEntry result = (ServerEntry)ses.deserialize( data );
135
136 assertEquals( entry, result );
137 }
138
139
140 @Test public void testSerializeServerEntryOC() throws Exception
141 {
142 LdapDN dn = new LdapDN( "cn=text, dc=example, dc=com" );
143 dn.normalize( oids );
144
145 ServerEntry entry = new DefaultServerEntry( registries, dn );
146 entry.add( "objectClass", "top", "person", "inetOrgPerson", "organizationalPerson" );
147
148 ServerEntrySerializer ses = new ServerEntrySerializer( registries );
149
150 byte[] data = ses.serialize( entry );
151
152 ServerEntry result = (ServerEntry)ses.deserialize( data );
153
154 assertEquals( entry, result );
155 }
156
157
158 @Test public void testSerializeServerEntry() throws Exception
159 {
160 LdapDN dn = new LdapDN( "cn=text, dc=example, dc=com" );
161 dn.normalize( oids );
162
163 ServerEntry entry = new DefaultServerEntry( registries, dn );
164 entry.add( "objectClass", "top", "person", "inetOrgPerson", "organizationalPerson" );
165 entry.add( "cn", "text", "test" );
166 entry.add( "SN", (String)null );
167 entry.add( "userPassword", StringTools.getBytesUtf8( "password" ) );
168
169 ServerEntrySerializer ses = new ServerEntrySerializer( registries );
170
171 byte[] data = ses.serialize( entry );
172
173 ServerEntry result = (ServerEntry)ses.deserialize( data );
174
175 assertEquals( entry, result );
176 }
177
178
179 @Test public void testSerializeServerEntryWithEmptyDN() throws Exception
180 {
181 LdapDN dn = new LdapDN( "" );
182 dn.normalize( oids );
183
184 ServerEntry entry = new DefaultServerEntry( registries, dn );
185 entry.add( "objectClass", "top", "person", "inetOrgPerson", "organizationalPerson" );
186 entry.add( "cn", "text", "test" );
187 entry.add( "SN", (String)null );
188 entry.add( "userPassword", StringTools.getBytesUtf8( "password" ) );
189
190 ServerEntrySerializer ses = new ServerEntrySerializer( registries );
191
192 byte[] data = ses.serialize( entry );
193
194 ServerEntry result = (ServerEntry)ses.deserialize( data );
195
196 assertEquals( entry, result );
197 }
198
199
200 @Test public void testSerializeServerEntryWithNoAttributes() throws Exception
201 {
202 LdapDN dn = new LdapDN( "" );
203 dn.normalize( oids );
204
205 ServerEntry entry = new DefaultServerEntry( registries, dn );
206
207 ServerEntrySerializer ses = new ServerEntrySerializer( registries );
208
209 byte[] data = ses.serialize( entry );
210
211 ServerEntry result = (ServerEntry)ses.deserialize( data );
212
213 assertEquals( entry, result );
214 }
215
216
217 @Test public void testSerializeServerEntryWithAttributeNoValue() throws Exception
218 {
219 LdapDN dn = new LdapDN( "" );
220 dn.normalize( oids );
221
222 ServerEntry entry = new DefaultServerEntry( registries, dn );
223
224 ServerEntrySerializer ses = new ServerEntrySerializer( registries );
225 EntryAttribute oc = new DefaultServerAttribute( "ObjectClass", registries.getAttributeTypeRegistry().lookup( "objectclass" ) );
226 entry.add( oc );
227
228 byte[] data = ses.serialize( entry );
229
230 ServerEntry result = (ServerEntry)ses.deserialize( data );
231
232 assertEquals( entry, result );
233 }
234
235
236 @Test public void testSerializeServerEntryWithAttributeStringValue() throws Exception
237 {
238 LdapDN dn = new LdapDN( "" );
239 dn.normalize( oids );
240
241 ServerEntry entry = new DefaultServerEntry( registries, dn );
242
243 ServerEntrySerializer ses = new ServerEntrySerializer( registries );
244 entry.add( "ObjectClass", "top", "person" );
245
246 byte[] data = ses.serialize( entry );
247
248 ServerEntry result = (ServerEntry)ses.deserialize( data );
249
250 assertEquals( entry, result );
251 }
252
253
254 @Test public void testSerializeServerEntryWithAttributeBinaryValue() throws Exception
255 {
256 LdapDN dn = new LdapDN( "" );
257 dn.normalize( oids );
258
259 ServerEntry entry = new DefaultServerEntry( registries, dn );
260
261 ServerEntrySerializer ses = new ServerEntrySerializer( registries );
262 entry.add( "userPassword", StringTools.getBytesUtf8( "secret" ) );
263
264 byte[] data = ses.serialize( entry );
265
266 ServerEntry result = (ServerEntry)ses.deserialize( data );
267
268 assertEquals( entry, result );
269 }
270 }