@Test
public void testPreventRdnChangeOnModifyRemove() throws Exception
{
ModificationOperation mod = ModificationOperation.REMOVE_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.preventRdnChangeOnModifyRemove( name, mod, attributes, schemaManager );
// test should fail since we are removing the ou attribute
AttributeType OU_AT = schemaManager.lookupAttributeTypeRegistry( "ou" );
attributes.put( new DefaultServerAttribute( "ou", OU_AT ) );
try
{
SchemaChecker.preventRdnChangeOnModifyRemove( 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.preventRdnChangeOnModifyRemove( name, mod, attributes, schemaManager );
// test for failure when modifying Rdn attribute in multi attribute Rdn
AttributeType CN_AT = schemaManager.lookupAttributeTypeRegistry( "cn" );
attributes.put( new DefaultServerAttribute( "cn", CN_AT ) );
try
{
SchemaChecker.preventRdnChangeOnModifyRemove( 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 value being deleted from the rdn attribute is
// is not used when composing the Rdn
attributes = new DefaultServerEntry( schemaManager, name );
attributes.put( "ou", "container" );
SchemaChecker.preventRdnChangeOnModifyRemove( name, mod, attributes, schemaManager );
// now let's make it fail again just by providing the right value for ou (users)
attributes = new DefaultServerEntry( schemaManager, name );
attributes.put( "ou", "users" );
try
{
SchemaChecker.preventRdnChangeOnModifyRemove( name, mod, attributes, schemaManager );
fail( "should never get here due to a LdapSchemaViolationException being thrown" );
}