Search in sources :

Example 6 with MutableMatchingRule

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

the class TestEntryUtils method getBytesAttributeType.

/* No protection */
static AttributeType getBytesAttributeType() {
    MutableAttributeType attributeType = new MutableAttributeType("1.2");
    LdapSyntax syntax = new LdapSyntax("1.2.1", "", true);
    syntax.setSyntaxChecker(new SyntaxChecker("1.2.1") {

        /**
         * The mandatory serialVersionUID field
         */
        public static final long serialVersionUID = 1L;

        public boolean isValidSyntax(Object value) {
            return (value == null) || (((byte[]) value).length < 5);
        }
    });
    MutableMatchingRule matchingRule = new MutableMatchingRule("1.2.2");
    matchingRule.setSyntax(syntax);
    matchingRule.setLdapComparator(new ByteArrayComparator("1.2.2"));
    matchingRule.setNormalizer(new Normalizer("1.1.1") {

        /**
         * The mandatory serialVersionUID field
         */
        public static final long serialVersionUID = 1L;

        public String normalize(String value) throws LdapException {
            return normalize(value, AssertionType.ATTRIBUTE_VALUE);
        }

        public String normalize(String value, PrepareString.AssertionType assertionType) throws LdapException {
            byte[] val = Strings.getBytesUtf8(value);
            // each byte will be changed to be > 0, and spaces will be trimmed
            byte[] newVal = new byte[val.length];
            int i = 0;
            for (byte b : val) {
                newVal[i++] = (byte) (b & 0x007F);
            }
            return Strings.utf8ToString(Strings.trim(newVal));
        }
    });
    attributeType.setEquality(matchingRule);
    attributeType.setSyntax(syntax);
    return attributeType;
}
Also used : MutableMatchingRule(org.apache.directory.api.ldap.model.schema.MutableMatchingRule) SyntaxChecker(org.apache.directory.api.ldap.model.schema.SyntaxChecker) PrepareString(org.apache.directory.api.ldap.model.schema.PrepareString) DeepTrimToLowerNormalizer(org.apache.directory.api.ldap.model.schema.normalizers.DeepTrimToLowerNormalizer) Normalizer(org.apache.directory.api.ldap.model.schema.Normalizer) AssertionType(org.apache.directory.api.ldap.model.schema.PrepareString.AssertionType) LdapSyntax(org.apache.directory.api.ldap.model.schema.LdapSyntax) PrepareString(org.apache.directory.api.ldap.model.schema.PrepareString) MutableAttributeType(org.apache.directory.api.ldap.model.schema.MutableAttributeType) ByteArrayComparator(org.apache.directory.api.ldap.model.schema.comparators.ByteArrayComparator) LdapException(org.apache.directory.api.ldap.model.exception.LdapException)

Example 7 with MutableMatchingRule

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

the class TestEntryUtils method getIA5StringAttributeType.

/* no protection*/
static AttributeType getIA5StringAttributeType() {
    MutableAttributeType attributeType = new MutableAttributeType("1.1");
    attributeType.addName("1.1");
    LdapSyntax syntax = new LdapSyntax("1.1.1", "", true);
    syntax.setSyntaxChecker(new SyntaxChecker("1.1.2") {

        /**
         * The mandatory serialVersionUID field
         */
        public static final long serialVersionUID = 1L;

        public boolean isValidSyntax(Object value) {
            String trimmedValue = Strings.deepTrim((String) value);
            return (trimmedValue == null) || (trimmedValue.length() < 7);
        }
    });
    MutableMatchingRule matchingRule = new MutableMatchingRule("1.1.2");
    matchingRule.setSyntax(syntax);
    matchingRule.setLdapComparator(new LdapComparator<String>(matchingRule.getOid()) {

        /**
         * The mandatory serialVersionUID field
         */
        public static final long serialVersionUID = 1L;

        public int compare(String o1, String o2) {
            return ((o1 == null) ? (o2 == null ? 0 : -1) : (o2 == null ? 1 : o1.compareTo(o2)));
        }
    });
    matchingRule.setNormalizer(new DeepTrimToLowerNormalizer(matchingRule.getOid()));
    attributeType.setEquality(matchingRule);
    attributeType.setSyntax(syntax);
    return attributeType;
}
Also used : MutableMatchingRule(org.apache.directory.api.ldap.model.schema.MutableMatchingRule) SyntaxChecker(org.apache.directory.api.ldap.model.schema.SyntaxChecker) DeepTrimToLowerNormalizer(org.apache.directory.api.ldap.model.schema.normalizers.DeepTrimToLowerNormalizer) LdapSyntax(org.apache.directory.api.ldap.model.schema.LdapSyntax) PrepareString(org.apache.directory.api.ldap.model.schema.PrepareString) MutableAttributeType(org.apache.directory.api.ldap.model.schema.MutableAttributeType)

Example 8 with MutableMatchingRule

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

the class SchemaManagerAddTest method testAddMatchingRuleExistingName.

/**
 * Try to inject a new MatchingRule with an existing name
 */
@Test
public void testAddMatchingRuleExistingName() throws Exception {
    SchemaManager schemaManager = loadSystem();
    int mrrSize = schemaManager.getMatchingRuleRegistry().size();
    int goidSize = schemaManager.getGlobalOidRegistry().size();
    MutableMatchingRule matchingRule = new MutableMatchingRule("1.1.0");
    matchingRule.setNames("Test", "objectIdentifierMatch");
    matchingRule.setSyntaxOid("1.3.6.1.4.1.1466.115.121.1.26");
    // It should fail (name already registered)
    assertFalse(schemaManager.add(matchingRule));
    List<Throwable> errors = schemaManager.getErrors();
    assertEquals(1, errors.size());
    Throwable error = errors.get(0);
    assertTrue(error instanceof LdapSchemaException);
    assertEquals(mrrSize, schemaManager.getMatchingRuleRegistry().size());
    assertEquals(goidSize, schemaManager.getGlobalOidRegistry().size());
}
Also used : MutableMatchingRule(org.apache.directory.api.ldap.model.schema.MutableMatchingRule) LdapSchemaException(org.apache.directory.api.ldap.model.exception.LdapSchemaException) SchemaManager(org.apache.directory.api.ldap.model.schema.SchemaManager) DefaultSchemaManager(org.apache.directory.api.ldap.schema.manager.impl.DefaultSchemaManager) Test(org.junit.Test)

Example 9 with MutableMatchingRule

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

the class SchemaManagerAddTest method testAddMatchingRuleNotExistingSchema.

/**
 * Try to inject a new MatchingRule with an existing AT name
 */
@Test
public void testAddMatchingRuleNotExistingSchema() throws Exception {
    SchemaManager schemaManager = loadSystem();
    int mrrSize = schemaManager.getMatchingRuleRegistry().size();
    int goidSize = schemaManager.getGlobalOidRegistry().size();
    MutableMatchingRule matchingRule = new MutableMatchingRule("1.1.0");
    matchingRule.setNames("Test");
    matchingRule.setSyntaxOid("1.3.6.1.4.1.1466.115.121.1.26");
    matchingRule.setSchemaName("bad");
    // It should fail
    assertFalse(schemaManager.add(matchingRule));
    List<Throwable> errors = schemaManager.getErrors();
    assertEquals(1, errors.size());
    Throwable error = errors.get(0);
    assertTrue(error instanceof LdapSchemaException);
    // Check that the new MR has been injected
    assertFalse(isMRPresent(schemaManager, "1.1.0"));
    assertEquals(mrrSize, schemaManager.getMatchingRuleRegistry().size());
    assertEquals(goidSize, schemaManager.getGlobalOidRegistry().size());
}
Also used : MutableMatchingRule(org.apache.directory.api.ldap.model.schema.MutableMatchingRule) LdapSchemaException(org.apache.directory.api.ldap.model.exception.LdapSchemaException) SchemaManager(org.apache.directory.api.ldap.model.schema.SchemaManager) DefaultSchemaManager(org.apache.directory.api.ldap.schema.manager.impl.DefaultSchemaManager) Test(org.junit.Test)

Example 10 with MutableMatchingRule

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

the class SchemaManagerAddTest method testAddValidMatchingRule.

// =========================================================================
// DitContentRule addition tests
// -------------------------------------------------------------------------
// TODO
// =========================================================================
// DitStructureRule addition tests
// -------------------------------------------------------------------------
// TODO
// =========================================================================
// MatchingRule addition tests
// -------------------------------------------------------------------------
/**
 * Try to inject a new MatchingRule
 */
@Test
public void testAddValidMatchingRule() throws Exception {
    SchemaManager schemaManager = loadSystem();
    int mrrSize = schemaManager.getMatchingRuleRegistry().size();
    int goidSize = schemaManager.getGlobalOidRegistry().size();
    MutableMatchingRule matchingRule = new MutableMatchingRule("1.1.0");
    matchingRule.setSyntaxOid("1.3.6.1.4.1.1466.115.121.1.26");
    // It should not fail
    assertTrue(schemaManager.add(matchingRule));
    assertTrue(isMRPresent(schemaManager, "1.1.0"));
    // The C and N must have default values
    MatchingRule added = schemaManager.lookupMatchingRuleRegistry("1.1.0");
    assertEquals(NoOpNormalizer.class.getName(), added.getNormalizer().getClass().getName());
    assertEquals(ComparableComparator.class.getName(), added.getLdapComparator().getClass().getName());
    assertEquals(mrrSize + 1, schemaManager.getMatchingRuleRegistry().size());
    assertEquals(goidSize + 1, schemaManager.getGlobalOidRegistry().size());
}
Also used : MutableMatchingRule(org.apache.directory.api.ldap.model.schema.MutableMatchingRule) NoOpNormalizer(org.apache.directory.api.ldap.model.schema.normalizers.NoOpNormalizer) SchemaManager(org.apache.directory.api.ldap.model.schema.SchemaManager) DefaultSchemaManager(org.apache.directory.api.ldap.schema.manager.impl.DefaultSchemaManager) ComparableComparator(org.apache.directory.api.ldap.model.schema.comparators.ComparableComparator) MatchingRule(org.apache.directory.api.ldap.model.schema.MatchingRule) MutableMatchingRule(org.apache.directory.api.ldap.model.schema.MutableMatchingRule) Test(org.junit.Test)

Aggregations

MutableMatchingRule (org.apache.directory.api.ldap.model.schema.MutableMatchingRule)14 LdapSyntax (org.apache.directory.api.ldap.model.schema.LdapSyntax)6 MutableAttributeType (org.apache.directory.api.ldap.model.schema.MutableAttributeType)6 SchemaManager (org.apache.directory.api.ldap.model.schema.SchemaManager)6 SyntaxChecker (org.apache.directory.api.ldap.model.schema.SyntaxChecker)6 DefaultSchemaManager (org.apache.directory.api.ldap.schema.manager.impl.DefaultSchemaManager)6 Test (org.junit.Test)6 PrepareString (org.apache.directory.api.ldap.model.schema.PrepareString)5 DeepTrimToLowerNormalizer (org.apache.directory.api.ldap.model.schema.normalizers.DeepTrimToLowerNormalizer)5 LdapSchemaException (org.apache.directory.api.ldap.model.exception.LdapSchemaException)4 MatchingRule (org.apache.directory.api.ldap.model.schema.MatchingRule)3 Normalizer (org.apache.directory.api.ldap.model.schema.Normalizer)3 NoOpNormalizer (org.apache.directory.api.ldap.model.schema.normalizers.NoOpNormalizer)3 AssertionType (org.apache.directory.api.ldap.model.schema.PrepareString.AssertionType)2 ByteArrayComparator (org.apache.directory.api.ldap.model.schema.comparators.ByteArrayComparator)2 Attribute (org.apache.directory.api.ldap.model.entry.Attribute)1 DefaultAttribute (org.apache.directory.api.ldap.model.entry.DefaultAttribute)1 LdapException (org.apache.directory.api.ldap.model.exception.LdapException)1 LdapUnwillingToPerformException (org.apache.directory.api.ldap.model.exception.LdapUnwillingToPerformException)1 ComparableComparator (org.apache.directory.api.ldap.model.schema.comparators.ComparableComparator)1