@Test
public void testPreventRdnChangeOnModifyReplace() throws Exception
{
ModificationOperation mod = ModificationOperation.REPLACE_ATTRIBUTE;
DN name = new DN( "ou=user,dc=example,dc=com" );
ServerEntry attributes = new DefaultServerEntry( schemaManager, name );
attributes.put( "cn", "does not matter" );
// postive test which should pass
SchemaChecker.preventRdnChangeOnModifyReplace( name, mod, attributes, schemaManager );
// test should fail since we are removing the ou attribute
attributes.put( "ou", (String)null );
try
{
SchemaChecker.preventRdnChangeOnModifyReplace( name, mod, attributes, schemaManager );
fail( "should never get here due to a LdapSchemaViolationException being thrown" );
}
catch ( LdapSchemaViolationException e )
{
assertEquals( ResultCodeEnum.NOT_ALLOWED_ON_RDN, e.getResultCode() );
}
// test success using more than one attribute for the Rdn but not modifying rdn attribute
name = new DN( "ou=users+cn=system users,dc=example,dc=com" );
attributes = new DefaultServerEntry( schemaManager, name );
attributes.put( "sn", "does not matter" );
SchemaChecker.preventRdnChangeOnModifyReplace( name, mod, attributes, schemaManager );
// test for failure when modifying Rdn attribute in multi attribute Rdn
attributes.put("cn", (String)null );
try
{
SchemaChecker.preventRdnChangeOnModifyReplace( name, mod, attributes, schemaManager );
fail( "should never get here due to a LdapSchemaViolationException being thrown" );
}
catch ( LdapSchemaViolationException e )
{
assertEquals( ResultCodeEnum.NOT_ALLOWED_ON_RDN, e.getResultCode() );
}
// should succeed since the values being replaced from the rdn attribute is
// is includes the old Rdn attribute value
attributes = new DefaultServerEntry( schemaManager, name );
attributes.put( "ou", "container" );
attributes.put( "ou", "users" );
SchemaChecker.preventRdnChangeOnModifyReplace( name, mod, attributes, schemaManager );
// now let's make it fail by not including the old value for ou (users)
attributes = new DefaultServerEntry( schemaManager, name );
attributes.put( "ou", "container" );
try
{
SchemaChecker.preventRdnChangeOnModifyReplace( name, mod, attributes, schemaManager );
fail( "should never get here due to a LdapSchemaViolationException being thrown" );
}