Search in sources :

Example 11 with AttributeType

use of org.apache.directory.api.ldap.model.schema.AttributeType in project directory-ldap-api by apache.

the class SchemaAwareAttributeTest method testAddOneValue.

@Test
public void testAddOneValue() throws Exception {
    AttributeType at = TestEntryUtils.getIA5StringAttributeType();
    DefaultAttribute attr = new DefaultAttribute(at);
    // Add a String value
    attr.add("test");
    assertEquals(1, attr.size());
    assertTrue(attr.getAttributeType().getSyntax().isHumanReadable());
    Value value = attr.get();
    assertTrue(value instanceof Value);
    assertEquals("test", ((Value) value).getValue());
    // Add a binary value
    assertEquals(0, attr.add(new byte[] { 0x01 }));
    // Add a Value
    Value ssv = new Value(at, "test2");
    attr.add(ssv);
    assertEquals(2, attr.size());
    Set<String> expected = new HashSet<String>();
    expected.add("test");
    expected.add("test2");
    for (Value val : attr) {
        if (expected.contains(val.getValue())) {
            expected.remove(val.getValue());
        } else {
            fail();
        }
    }
    assertEquals(0, expected.size());
}
Also used : AttributeType(org.apache.directory.api.ldap.model.schema.AttributeType) Value(org.apache.directory.api.ldap.model.entry.Value) DefaultAttribute(org.apache.directory.api.ldap.model.entry.DefaultAttribute) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 12 with AttributeType

use of org.apache.directory.api.ldap.model.schema.AttributeType in project directory-ldap-api by apache.

the class SchemaAwareModificationSerializationTest method deserializeValue.

/**
 * Deserialize a DefaultModification
 */
private Modification deserializeValue(ByteArrayOutputStream out) throws IOException, ClassNotFoundException, LdapInvalidAttributeValueException {
    ObjectInputStream oIn = null;
    ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
    try {
        oIn = new ObjectInputStream(in);
        Modification modification = new DefaultModification();
        modification.readExternal(oIn);
        Attribute attribute = modification.getAttribute();
        if ((attribute != null) && (schemaManager != null)) {
            AttributeType attributeType = schemaManager.getAttributeType(attribute.getId());
            modification.apply(attributeType);
        }
        return modification;
    } catch (IOException ioe) {
        throw ioe;
    } finally {
        try {
            if (oIn != null) {
                oIn.close();
            }
        } catch (IOException ioe) {
            throw ioe;
        }
    }
}
Also used : DefaultModification(org.apache.directory.api.ldap.model.entry.DefaultModification) Modification(org.apache.directory.api.ldap.model.entry.Modification) ByteArrayInputStream(java.io.ByteArrayInputStream) DefaultModification(org.apache.directory.api.ldap.model.entry.DefaultModification) DefaultAttribute(org.apache.directory.api.ldap.model.entry.DefaultAttribute) Attribute(org.apache.directory.api.ldap.model.entry.Attribute) AttributeType(org.apache.directory.api.ldap.model.schema.AttributeType) IOException(java.io.IOException) ObjectInputStream(java.io.ObjectInputStream)

Example 13 with AttributeType

use of org.apache.directory.api.ldap.model.schema.AttributeType in project directory-ldap-api by apache.

the class DefaultSchemaLoader method loadAttributeTypes.

/**
 * {@inheritDoc}
 */
@Override
public List<Entry> loadAttributeTypes(Schema... schemas) throws LdapException, IOException {
    List<Entry> attributeTypeEntries = new ArrayList<>();
    if (schemas == null) {
        return attributeTypeEntries;
    }
    AttributesFactory factory = new AttributesFactory();
    for (Schema schema : schemas) {
        Set<SchemaObjectWrapper> schemaObjectWrappers = schema.getContent();
        for (SchemaObjectWrapper schemaObjectWrapper : schemaObjectWrappers) {
            SchemaObject schemaObject = schemaObjectWrapper.get();
            if (schemaObject instanceof AttributeType) {
                AttributeType attributeType = (AttributeType) schemaObject;
                Entry attributeTypeEntry = factory.convert(attributeType, schema, null);
                attributeTypeEntries.add(attributeTypeEntry);
            }
        }
    }
    return attributeTypeEntries;
}
Also used : SchemaObject(org.apache.directory.api.ldap.model.schema.SchemaObject) DefaultEntry(org.apache.directory.api.ldap.model.entry.DefaultEntry) Entry(org.apache.directory.api.ldap.model.entry.Entry) AttributesFactory(org.apache.directory.api.ldap.model.schema.AttributesFactory) AttributeType(org.apache.directory.api.ldap.model.schema.AttributeType) DefaultSchema(org.apache.directory.api.ldap.model.schema.registries.DefaultSchema) Schema(org.apache.directory.api.ldap.model.schema.registries.Schema) ArrayList(java.util.ArrayList) SchemaObjectWrapper(org.apache.directory.api.ldap.model.schema.SchemaObjectWrapper)

Example 14 with AttributeType

use of org.apache.directory.api.ldap.model.schema.AttributeType in project directory-ldap-api by apache.

the class BinaryValueAttributeTypeTest method testgetAttributeType.

/**
 * Test the getAttributeType method
 * @throws LdapInvalidAttributeValueException
 */
@Test
public void testgetAttributeType() throws LdapInvalidAttributeValueException {
    AttributeType attribute = EntryUtils.getBytesAttributeType();
    Value sbv = Value.createValue(attribute);
    assertEquals(attribute, sbv.getAttributeType());
}
Also used : AttributeType(org.apache.directory.api.ldap.model.schema.AttributeType) MutableAttributeType(org.apache.directory.api.ldap.model.schema.MutableAttributeType) Test(org.junit.Test)

Example 15 with AttributeType

use of org.apache.directory.api.ldap.model.schema.AttributeType in project directory-ldap-api by apache.

the class BinaryValueAttributeTypeTest method testInstanceOf.

/**
 * Test the instanceOf method
 */
@Test
public void testInstanceOf() throws LdapException {
    AttributeType attribute = EntryUtils.getBytesAttributeType();
    Value sbv = Value.createValue(attribute);
    assertTrue(sbv.isInstanceOf(attribute));
    attribute = EntryUtils.getIA5StringAttributeType();
    assertFalse(sbv.isInstanceOf(attribute));
}
Also used : AttributeType(org.apache.directory.api.ldap.model.schema.AttributeType) MutableAttributeType(org.apache.directory.api.ldap.model.schema.MutableAttributeType) Test(org.junit.Test)

Aggregations

AttributeType (org.apache.directory.api.ldap.model.schema.AttributeType)135 Test (org.junit.Test)68 MutableAttributeType (org.apache.directory.api.ldap.model.schema.MutableAttributeType)42 LdapException (org.apache.directory.api.ldap.model.exception.LdapException)25 ParseException (java.text.ParseException)15 DefaultAttribute (org.apache.directory.api.ldap.model.entry.DefaultAttribute)15 Value (org.apache.directory.api.ldap.model.entry.Value)15 Attribute (org.apache.directory.api.ldap.model.entry.Attribute)11 LdapInvalidAttributeValueException (org.apache.directory.api.ldap.model.exception.LdapInvalidAttributeValueException)11 HashSet (java.util.HashSet)10 LdapSchemaException (org.apache.directory.api.ldap.model.exception.LdapSchemaException)10 ObjectClass (org.apache.directory.api.ldap.model.schema.ObjectClass)10 MatchingRule (org.apache.directory.api.ldap.model.schema.MatchingRule)9 IOException (java.io.IOException)7 LdapInvalidDnException (org.apache.directory.api.ldap.model.exception.LdapInvalidDnException)7 Before (org.junit.Before)7 SchemaManager (org.apache.directory.api.ldap.model.schema.SchemaManager)6 DefaultSchemaManager (org.apache.directory.api.ldap.schema.manager.impl.DefaultSchemaManager)6 ArrayList (java.util.ArrayList)5 Dn (org.apache.directory.api.ldap.model.name.Dn)4