Search in sources :

Example 26 with LdapInvalidAttributeValueException

use of org.apache.directory.api.ldap.model.exception.LdapInvalidAttributeValueException in project directory-ldap-api by apache.

the class StringValueAttributeTypeTest method testConstrainedString.

/**
 * Presumes an attribute which constrains it's values to some constant
 * strings: LOW, MEDIUM, HIGH.  Normalization does nothing. MatchingRules
 * are exact case matching.
 *
 * @throws Exception on errors
 */
@Test
public void testConstrainedString() throws LdapInvalidAttributeValueException {
    s.setSyntaxChecker(new SyntaxChecker("1.1.1.1") {

        public static final long serialVersionUID = 1L;

        public boolean isValidSyntax(Object value) {
            if (value instanceof String) {
                String strval = (String) value;
                return strval.equals("HIGH") || strval.equals("LOW") || strval.equals("MEDIUM");
            }
            return false;
        }
    });
    mr.setSyntax(s);
    mr.setNormalizer(new NoOpNormalizer(mr.getOid()));
    at.setEquality(mr);
    at.setSyntax(s);
    // check that normalization and syntax checks work as expected
    Value value = new Value(at, "HIGH");
    assertEquals(value.getValue(), value.getValue());
    try {
        new Value(at, "high");
        fail();
    } catch (LdapInvalidAttributeValueException liave) {
    // expected
    }
    // create a bunch to best tested for equals and in containers
    Value v0 = new Value(at, "LOW");
    Value v1 = new Value(at, "LOW");
    Value v2 = new Value(at, "MEDIUM");
    Value v3 = new Value(at, "HIGH");
    // check equals
    assertTrue(v0.equals(v1));
    assertTrue(v1.equals(v0));
    assertEquals(0, v0.compareTo(v1));
    assertFalse(v2.equals(v3));
    assertFalse(v3.equals(v2));
    assertTrue(v2.compareTo(v3) > 0);
    assertTrue(v3.compareTo(v2) < 0);
    // add all except v1 and v5 to a set
    HashSet<Value> set = new HashSet<Value>();
    set.add(v0);
    set.add(v2);
    set.add(v3);
    // check contains method
    assertTrue("since v1.equals( v0 ) and v0 was added then this should be true", set.contains(v1));
    // check ordering based on the comparator
    List<Value> list = new ArrayList<Value>();
    list.add(v1);
    list.add(v3);
    list.add(v0);
    list.add(v2);
    Collections.sort(list);
    // High, low, low, medium
    assertTrue("since v0 equals v1 either could be at index 0 & 1", list.get(0).equals(v3));
    assertTrue("since v0 equals v1 either could be at index 0 & 1", list.get(1).equals(v0));
    assertTrue("since v2 \"MEDIUM\" should be at index 2", list.get(2).equals(v1));
    assertTrue("since v3 \"HIGH\" should be at index 3", list.get(3).equals(v2));
    assertEquals(4, list.size());
}
Also used : NoOpNormalizer(org.apache.directory.api.ldap.model.schema.normalizers.NoOpNormalizer) OctetStringSyntaxChecker(org.apache.directory.api.ldap.model.schema.syntaxCheckers.OctetStringSyntaxChecker) SyntaxChecker(org.apache.directory.api.ldap.model.schema.SyntaxChecker) ArrayList(java.util.ArrayList) LdapInvalidAttributeValueException(org.apache.directory.api.ldap.model.exception.LdapInvalidAttributeValueException) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 27 with LdapInvalidAttributeValueException

use of org.apache.directory.api.ldap.model.exception.LdapInvalidAttributeValueException in project directory-ldap-api by apache.

the class LdifUtilsTest method testCreateAttributesVarargs.

@Test
public void testCreateAttributesVarargs() throws LdapException, LdapLdifException, NamingException {
    String mOid = "m-oid: 1.2.3.4";
    String description = "description";
    Attributes attrs = LdifUtils.createJndiAttributes("objectClass: top", "objectClass: metaTop", "objectClass: metaSyntax", mOid, "m-description", description);
    assertEquals("top", attrs.get("objectClass").get(0));
    assertEquals("metaTop", attrs.get("objectClass").get(1));
    assertEquals("metaSyntax", attrs.get("objectClass").get(2));
    assertEquals("1.2.3.4", attrs.get("m-oid").get());
    assertEquals("description", attrs.get("m-description").get());
    try {
        LdifUtils.createJndiAttributes("objectClass", "top", "objectClass");
        fail();
    } catch (LdapInvalidAttributeValueException iave) {
        assertTrue(true);
    }
}
Also used : BasicAttributes(javax.naming.directory.BasicAttributes) Attributes(javax.naming.directory.Attributes) LdapInvalidAttributeValueException(org.apache.directory.api.ldap.model.exception.LdapInvalidAttributeValueException) Test(org.junit.Test)

Example 28 with LdapInvalidAttributeValueException

use of org.apache.directory.api.ldap.model.exception.LdapInvalidAttributeValueException in project directory-ldap-api by apache.

the class ModifyRequestImplTest method testEqualsDiffImpl.

/**
 * Tests for equality even when another BindRequest implementation is used.
 */
@Test
public void testEqualsDiffImpl() throws LdapException {
    ModifyRequest req0 = new ModifyRequest() {

        public Collection<Modification> getModifications() {
            List<Modification> list = new ArrayList<Modification>();
            try {
                Attribute attr = new DefaultAttribute("attr0");
                attr.add("val0");
                attr.add("val1");
                attr.add("val2");
                Modification item = new DefaultModification(ModificationOperation.ADD_ATTRIBUTE, attr);
                list.add(item);
                attr = new DefaultAttribute("attr1");
                attr.add("val3");
                item = new DefaultModification(ModificationOperation.REMOVE_ATTRIBUTE, attr);
                list.add(item);
                attr = new DefaultAttribute("attr2");
                attr.add("val4");
                attr.add("val5");
                item = new DefaultModification(ModificationOperation.REPLACE_ATTRIBUTE, attr);
                list.add(item);
            } catch (LdapInvalidAttributeValueException liave) {
            // Can't happen
            }
            return list;
        }

        public ModifyRequest addModification(Modification mod) {
            return this;
        }

        public ModifyRequest removeModification(Modification mod) {
            return this;
        }

        public Dn getName() {
            try {
                return new Dn("cn=admin,dc=apache,dc=org");
            } catch (Exception e) {
                // do nothing
                return null;
            }
        }

        public ModifyRequest setName(Dn name) {
            return this;
        }

        public MessageTypeEnum getResponseType() {
            return MessageTypeEnum.MODIFY_RESPONSE;
        }

        public boolean hasResponse() {
            return true;
        }

        public MessageTypeEnum getType() {
            return MessageTypeEnum.MODIFY_REQUEST;
        }

        public Map<String, Control> getControls() {
            return EMPTY_CONTROL_MAP;
        }

        public ModifyRequest addControl(Control a_control) {
            return this;
        }

        public ModifyRequest removeControl(Control a_control) {
            return this;
        }

        public int getMessageId() {
            return 45;
        }

        public Object get(Object a_key) {
            return null;
        }

        public Object put(Object a_key, Object a_value) {
            return null;
        }

        public void abandon() {
        }

        public boolean isAbandoned() {
            return false;
        }

        public ModifyRequest addAbandonListener(AbandonListener listener) {
            return this;
        }

        public ModifyResponse getResultResponse() {
            return null;
        }

        public ModifyRequest addAllControls(Control[] controls) {
            return this;
        }

        public boolean hasControl(String oid) {
            return false;
        }

        public Control getControl(String oid) {
            return null;
        }

        public ModifyRequest setMessageId(int messageId) {
            return this;
        }

        public ModifyRequest addModification(Attribute attr, ModificationOperation modOp) {
            return this;
        }

        public ModifyRequest replace(String attributeName) {
            return this;
        }

        public ModifyRequest replace(String attributeName, String... attributeValue) {
            return this;
        }

        public ModifyRequest replace(String attributeName, byte[]... attributeValue) {
            return this;
        }

        public ModifyRequest replace(Attribute attr) {
            return this;
        }

        public ModifyRequest add(String attributeName, String... attributeValue) {
            return this;
        }

        public ModifyRequest add(String attributeName, byte[]... attributeValue) {
            return this;
        }

        public ModifyRequest add(Attribute attr) {
            return this;
        }

        public ModifyRequest remove(String attributeName, String... attributeValue) {
            return this;
        }

        public ModifyRequest remove(String attributeName, byte[]... attributeValue) {
            return this;
        }

        public ModifyRequest remove(Attribute attr) {
            return this;
        }

        public ModifyRequest remove(String attributerName) {
            return this;
        }
    };
    ModifyRequestImpl req1 = getRequest();
    assertTrue(req1.equals(req0));
}
Also used : DefaultModification(org.apache.directory.api.ldap.model.entry.DefaultModification) Modification(org.apache.directory.api.ldap.model.entry.Modification) DefaultAttribute(org.apache.directory.api.ldap.model.entry.DefaultAttribute) Attribute(org.apache.directory.api.ldap.model.entry.Attribute) DefaultModification(org.apache.directory.api.ldap.model.entry.DefaultModification) ArrayList(java.util.ArrayList) Dn(org.apache.directory.api.ldap.model.name.Dn) LdapInvalidAttributeValueException(org.apache.directory.api.ldap.model.exception.LdapInvalidAttributeValueException) LdapInvalidAttributeValueException(org.apache.directory.api.ldap.model.exception.LdapInvalidAttributeValueException) LdapException(org.apache.directory.api.ldap.model.exception.LdapException) ModificationOperation(org.apache.directory.api.ldap.model.entry.ModificationOperation) DefaultAttribute(org.apache.directory.api.ldap.model.entry.DefaultAttribute) Test(org.junit.Test)

Aggregations

LdapInvalidAttributeValueException (org.apache.directory.api.ldap.model.exception.LdapInvalidAttributeValueException)28 Attribute (org.apache.directory.api.ldap.model.entry.Attribute)12 DefaultAttribute (org.apache.directory.api.ldap.model.entry.DefaultAttribute)9 AttributeType (org.apache.directory.api.ldap.model.schema.AttributeType)7 Test (org.junit.Test)7 LdapException (org.apache.directory.api.ldap.model.exception.LdapException)6 Value (org.apache.directory.api.ldap.model.entry.Value)5 IOException (java.io.IOException)4 ArrayList (java.util.ArrayList)3 DefaultModification (org.apache.directory.api.ldap.model.entry.DefaultModification)3 Modification (org.apache.directory.api.ldap.model.entry.Modification)3 PrepareString (org.apache.directory.api.ldap.model.schema.PrepareString)3 Nonnull (javax.annotation.Nonnull)2 ModificationOperation (org.apache.directory.api.ldap.model.entry.ModificationOperation)2 LdapInvalidDnException (org.apache.directory.api.ldap.model.exception.LdapInvalidDnException)2 LdapNoPermissionException (org.apache.directory.api.ldap.model.exception.LdapNoPermissionException)2 LdapNoSuchObjectException (org.apache.directory.api.ldap.model.exception.LdapNoSuchObjectException)2 LdapConnection (org.apache.directory.ldap.client.api.LdapConnection)2 ExternalIdentityRef (org.apache.jackrabbit.oak.spi.security.authentication.external.ExternalIdentityRef)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)1