Java Code Examples for org.apache.directory.api.ldap.model.entry.ModificationOperation#ADD_ATTRIBUTE

The following examples show how to use org.apache.directory.api.ldap.model.entry.ModificationOperation#ADD_ATTRIBUTE . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SchemaAwareModificationSerializationTest.java    From directory-ldap-api with Apache License 2.0 6 votes vote down vote up
@Test
public void testCreateServerModification() throws LdapException
{
    Attribute attribute = new DefaultAttribute( "cn", cnAT );
    attribute.add( "test1", "test2" );

    Modification mod = new DefaultModification( ModificationOperation.ADD_ATTRIBUTE, attribute );
    Modification clone = mod.clone();

    attribute.remove( "test2" );

    Attribute clonedAttribute = clone.getAttribute();

    assertEquals( 1, mod.getAttribute().size() );
    assertTrue( mod.getAttribute().contains( "TEST1" ) );

    assertEquals( 2, clonedAttribute.size() );
    assertTrue( clone.getAttribute().contains( "test1" ) );
    assertTrue( clone.getAttribute().contains( "test2" ) );
}
 
Example 2
Source File: ModificationTest.java    From directory-ldap-api with Apache License 2.0 6 votes vote down vote up
@Test
public void testCreateServerModification() throws LdapException
{
    Attribute attribute = new DefaultAttribute( "cn" );
    attribute.add( "test1", "test2" );

    Modification mod = new DefaultModification( ModificationOperation.ADD_ATTRIBUTE, attribute );
    Modification clone = mod.clone();

    attribute.remove( "test2" );

    Attribute clonedAttribute = clone.getAttribute();

    assertEquals( 1, mod.getAttribute().size() );
    assertTrue( mod.getAttribute().contains( "test1" ) );

    assertEquals( 2, clonedAttribute.size() );
    assertTrue( clone.getAttribute().contains( "test1" ) );
    assertTrue( clone.getAttribute().contains( "test2" ) );
}
 
Example 3
Source File: AttributeUtilsTest.java    From directory-ldap-api with Apache License 2.0 6 votes vote down vote up
/**
 * Test a addModification applied to an entry 
 */
@Test
public void testApplyAddModificationToEntry() throws LdapException
{
    Entry entry = new DefaultEntry();
    entry.add( "dc", "apache" );
    assertEquals( 1, entry.size() );

    Attribute attr = new DefaultAttribute( "cn", "test" );
    Modification modification = new DefaultModification( ModificationOperation.ADD_ATTRIBUTE, attr );

    AttributeUtils.applyModification( entry, modification );
    assertNotNull( entry.get( "cn" ) );
    assertEquals( 2, entry.size() );
    assertEquals( attr, entry.get( "cn" ) );
}
 
Example 4
Source File: ModifyRequestImplTest.java    From directory-ldap-api with Apache License 2.0 6 votes vote down vote up
/**
 * Test for inequality when only the number of mods are different.
 */
@Test
public void testNotEqualDiffModCount() throws LdapException
{
    ModifyRequestImpl req0 = getRequest();
    Attribute attr = new DefaultAttribute( "attr3" );
    attr.add( "val0" );
    attr.add( "val1" );
    attr.add( "val2" );
    Modification item = new DefaultModification( ModificationOperation.ADD_ATTRIBUTE, attr );
    req0.addModification( item );

    ModifyRequestImpl req1 = getRequest();

    assertFalse( req0.equals( req1 ) );
    assertFalse( req1.equals( req0 ) );
}
 
Example 5
Source File: SchemaAwareModificationSerializationTest.java    From directory-ldap-api with Apache License 2.0 5 votes vote down vote up
@Test
public void testSerializationModificationADD() throws ClassNotFoundException, IOException,
    LdapInvalidAttributeValueException
{
    Attribute attribute = new DefaultAttribute( "cn", cnAT );
    attribute.add( "test1", "test2" );

    DefaultModification mod = new DefaultModification( ModificationOperation.ADD_ATTRIBUTE, attribute );

    Modification modSer = deserializeValue( serializeValue( mod ) );

    assertEquals( mod, modSer );
}
 
Example 6
Source File: ModificationTest.java    From directory-ldap-api with Apache License 2.0 5 votes vote down vote up
@Test
public void testSerializationModificationADD() throws ClassNotFoundException, IOException, LdapException
{
    Attribute attribute = new DefaultAttribute( "cn" );
    attribute.add( "test1", "test2" );

    DefaultModification mod = new DefaultModification( ModificationOperation.ADD_ATTRIBUTE, attribute );

    Modification modSer = deserializeValue( serializeValue( mod ) );

    assertEquals( mod, modSer );
}
 
Example 7
Source File: AttributeUtilsTest.java    From directory-ldap-api with Apache License 2.0 5 votes vote down vote up
/**
 * Test a addModification applied to an empty entry
 */
@Test
public void testApplyAddModificationToEmptyEntry() throws LdapException
{
    Entry entry = new DefaultEntry();
    Attribute attr = new DefaultAttribute( "cn", "test" );
    Modification modification = new DefaultModification( ModificationOperation.ADD_ATTRIBUTE, attr );
    AttributeUtils.applyModification( entry, modification );
    assertNotNull( entry.get( "cn" ) );
    assertEquals( 1, entry.size() );
    assertEquals( attr, entry.get( "cn" ) );
}
 
Example 8
Source File: AttributeUtilsTest.java    From directory-ldap-api with Apache License 2.0 5 votes vote down vote up
/**
 * Test a addModification applied to an entry with the same attribute
 * but with another value 
 */
@Test
public void testApplyAddModificationToEntryWithValues() throws LdapException
{
    Entry entry = new DefaultEntry();
    entry.put( "cn", "apache" );
    assertEquals( 1, entry.size() );

    Attribute attr = new DefaultAttribute( "cn", "test" );
    Modification modification = new DefaultModification( ModificationOperation.ADD_ATTRIBUTE, attr );
    AttributeUtils.applyModification( entry, modification );
    assertNotNull( entry.get( "cn" ) );
    assertEquals( 1, entry.size() );

    Attribute attribute = entry.get( "cn" );

    assertTrue( attribute.size() != 0 );

    Set<String> expectedValues = new HashSet<String>();
    expectedValues.add( "apache" );
    expectedValues.add( "test" );

    for ( Value value : attribute )
    {
        String valueStr = value.getString();

        assertTrue( expectedValues.contains( valueStr ) );

        expectedValues.remove( valueStr );
    }

    assertEquals( 0, expectedValues.size() );
}
 
Example 9
Source File: AttributeUtilsTest.java    From directory-ldap-api with Apache License 2.0 5 votes vote down vote up
/**
 * Test a addModification applied to an entry with the same attribute
 * and the same value 
 */
@Test
public void testApplyAddModificationToEntryWithSameValue() throws LdapException
{
    Entry entry = new DefaultEntry();
    entry.put( "cn", "test", "apache" );
    assertEquals( 1, entry.size() );

    Attribute attr = new DefaultAttribute( "cn", "test" );
    Modification modification = new DefaultModification( ModificationOperation.ADD_ATTRIBUTE, attr );
    AttributeUtils.applyModification( entry, modification );
    assertNotNull( entry.get( "cn" ) );
    assertEquals( 1, entry.size() );

    Attribute cnAttr = entry.get( "cn" );

    assertTrue( cnAttr.size() != 0 );

    Set<String> expectedValues = new HashSet<String>();
    expectedValues.add( "apache" );
    expectedValues.add( "test" );

    for ( Value value : cnAttr )
    {
        String valueStr = value.getString();

        assertTrue( expectedValues.contains( valueStr ) );

        expectedValues.remove( valueStr );
    }

    assertEquals( 0, expectedValues.size() );
}
 
Example 10
Source File: ServerEntryUtils.java    From MyVirtualDirectory with Apache License 2.0 5 votes vote down vote up
/**
 * Convert a ModificationItem to an instance of a ServerModification object
 *
 * @param modificationImpl the modification instance to convert
 * @param attributeType the associated attributeType
 * @return a instance of a ServerModification object
 */
private static Modification toServerModification( ModificationItem modificationImpl, AttributeType attributeType )
    throws LdapException
{
    ModificationOperation operation;

    switch ( modificationImpl.getModificationOp() )
    {
        case DirContext.REMOVE_ATTRIBUTE:
            operation = ModificationOperation.REMOVE_ATTRIBUTE;
            break;

        case DirContext.REPLACE_ATTRIBUTE:
            operation = ModificationOperation.REPLACE_ATTRIBUTE;
            break;

        case DirContext.ADD_ATTRIBUTE:
        default:
            operation = ModificationOperation.ADD_ATTRIBUTE;
            break;

    }

    Modification modification = new DefaultModification(
        operation,
        ServerEntryUtils.toServerAttribute( modificationImpl.getAttribute(), attributeType ) );

    return modification;

}
 
Example 11
Source File: ServerEntryUtils.java    From MyVirtualDirectory with Apache License 2.0 5 votes vote down vote up
/**
 * Convert a ModificationItem to an instance of a ServerModification object
 *
 * @param modificationImpl the modification instance to convert
 * @param attributeType the associated attributeType
 * @return a instance of a ServerModification object
 */
private static Modification toServerModification( ModificationItem modificationImpl, AttributeType attributeType )
    throws LdapException
{
    ModificationOperation operation;

    switch ( modificationImpl.getModificationOp() )
    {
        case DirContext.REMOVE_ATTRIBUTE:
            operation = ModificationOperation.REMOVE_ATTRIBUTE;
            break;

        case DirContext.REPLACE_ATTRIBUTE:
            operation = ModificationOperation.REPLACE_ATTRIBUTE;
            break;

        case DirContext.ADD_ATTRIBUTE:
        default:
            operation = ModificationOperation.ADD_ATTRIBUTE;
            break;

    }

    Modification modification = new DefaultModification(
        operation,
        ServerEntryUtils.toServerAttribute( modificationImpl.getAttribute(), attributeType ) );

    return modification;

}
 
Example 12
Source File: ModifyRequestDsml.java    From directory-ldap-api with Apache License 2.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public Element toDsml( Element root )
{
    Element element = super.toDsml( root );

    ModifyRequest request = getDecorated();

    // Dn
    if ( request.getName() != null )
    {
        element.addAttribute( "dn", request.getName().getName() );
    }

    // Modifications
    Collection<Modification> modifications = request.getModifications();

    for ( Modification modification : modifications )
    {
        Element modElement = element.addElement( "modification" );

        if ( modification.getAttribute() != null )
        {
            modElement.addAttribute( "name", modification.getAttribute().getId() );

            for ( Value value : modification.getAttribute() )
            {
                if ( value.getString() != null )
                {
                    if ( ParserUtils.needsBase64Encoding( value.getString() ) )
                    {
                        Namespace xsdNamespace = new Namespace( "xsd", ParserUtils.XML_SCHEMA_URI );
                        Namespace xsiNamespace = new Namespace( "xsi", ParserUtils.XML_SCHEMA_INSTANCE_URI );
                        element.getDocument().getRootElement().add( xsdNamespace );
                        element.getDocument().getRootElement().add( xsiNamespace );

                        Element valueElement = modElement.addElement( "value" ).addText(
                            ParserUtils.base64Encode( value.getString() ) );
                        valueElement.addAttribute( new QName( "type", xsiNamespace ), "xsd:"
                            + ParserUtils.BASE64BINARY );
                    }
                    else
                    {
                        modElement.addElement( "value" ).setText( value.getString() );
                    }
                }
            }
        }

        ModificationOperation operation = modification.getOperation();

        if ( operation == ModificationOperation.ADD_ATTRIBUTE )
        {
            modElement.addAttribute( "operation", "add" );
        }
        else if ( operation == ModificationOperation.REPLACE_ATTRIBUTE )
        {
            modElement.addAttribute( "operation", "replace" );
        }
        else if ( operation == ModificationOperation.REMOVE_ATTRIBUTE )
        {
            modElement.addAttribute( "operation", "delete" );
        }
        else if ( operation == ModificationOperation.INCREMENT_ATTRIBUTE )
        {
            modElement.addAttribute( "operation", "increment" );
        }
    }

    return element;
}