Search in sources :

Example 36 with ModifyRequest

use of org.apache.directory.api.ldap.model.message.ModifyRequest in project directory-ldap-api by apache.

the class ModifyRequestTest method testRequestWith1Control.

/**
 * Test parsing of a request with a (optional) Control element
 */
@Test
public void testRequestWith1Control() {
    Dsmlv2Parser parser = null;
    try {
        parser = newParser();
        parser.setInput(ModifyRequestTest.class.getResource("request_with_1_control.xml").openStream(), "UTF-8");
        parser.parse();
    } catch (Exception e) {
        fail(e.getMessage());
    }
    ModifyRequest modifyRequest = (ModifyRequest) parser.getBatchRequest().getCurrentRequest();
    Map<String, Control> controls = modifyRequest.getControls();
    assertEquals(1, modifyRequest.getControls().size());
    Control control = controls.get("1.2.840.113556.1.4.643");
    assertNotNull(control);
    assertTrue(control.isCritical());
    assertEquals("1.2.840.113556.1.4.643", control.getOid());
    assertEquals("Some text", Strings.utf8ToString(((DsmlControl<?>) control).getValue()));
}
Also used : DsmlControl(org.apache.directory.api.dsmlv2.DsmlControl) DsmlControl(org.apache.directory.api.dsmlv2.DsmlControl) Control(org.apache.directory.api.ldap.model.message.Control) Dsmlv2Parser(org.apache.directory.api.dsmlv2.Dsmlv2Parser) ModifyRequest(org.apache.directory.api.ldap.model.message.ModifyRequest) LdapException(org.apache.directory.api.ldap.model.exception.LdapException) Test(org.junit.Test) AbstractTest(org.apache.directory.api.dsmlv2.AbstractTest)

Example 37 with ModifyRequest

use of org.apache.directory.api.ldap.model.message.ModifyRequest in project directory-ldap-api by apache.

the class ModifyRequestTest method testRequestWith1ControlBase64Value.

/**
 * Test parsing of a request with a (optional) Control element with Base64 Value
 */
@Test
public void testRequestWith1ControlBase64Value() {
    Dsmlv2Parser parser = null;
    try {
        parser = newParser();
        parser.setInput(ModifyRequestTest.class.getResource("request_with_1_control_base64_value.xml").openStream(), "UTF-8");
        parser.parse();
    } catch (Exception e) {
        fail(e.getMessage());
    }
    ModifyRequest modifyRequest = (ModifyRequest) parser.getBatchRequest().getCurrentRequest();
    Map<String, Control> controls = modifyRequest.getControls();
    assertEquals(1, modifyRequest.getControls().size());
    Control control = controls.get("1.2.840.113556.1.4.643");
    assertNotNull(control);
    assertTrue(control.isCritical());
    assertEquals("1.2.840.113556.1.4.643", control.getOid());
    assertEquals("DSMLv2.0 rocks!!", Strings.utf8ToString(((DsmlControl<?>) control).getValue()));
}
Also used : DsmlControl(org.apache.directory.api.dsmlv2.DsmlControl) DsmlControl(org.apache.directory.api.dsmlv2.DsmlControl) Control(org.apache.directory.api.ldap.model.message.Control) Dsmlv2Parser(org.apache.directory.api.dsmlv2.Dsmlv2Parser) ModifyRequest(org.apache.directory.api.ldap.model.message.ModifyRequest) LdapException(org.apache.directory.api.ldap.model.exception.LdapException) Test(org.junit.Test) AbstractTest(org.apache.directory.api.dsmlv2.AbstractTest)

Example 38 with ModifyRequest

use of org.apache.directory.api.ldap.model.message.ModifyRequest in project directory-ldap-api by apache.

the class ModifyRequestTest method testDecodeModifyRequestAddOperationModificationType2Vals.

/**
 * Test the decoding of a ModifyRequest with an add operation, and a
 * modification with a type and two vals
 */
@Test
public void testDecodeModifyRequestAddOperationModificationType2Vals() throws LdapException {
    Asn1Decoder ldapDecoder = new Asn1Decoder();
    ByteBuffer stream = ByteBuffer.allocate(0x3D);
    stream.put(new byte[] { 0x30, // LdapMessage
    0x3B, 0x02, 0x01, // Message ID : 49
    0x31, 0x66, // ModifyRequest
    0x36, 0x04, 0x20, 'c', 'n', '=', 't', 'e', 's', 't', 'M', 'o', 'd', 'i', 'f', 'y', ',', 'o', 'u', '=', 'u', 's', 'e', 'r', 's', ',', 'o', 'u', '=', 's', 'y', 's', 't', 'e', 'm', 0x30, 0x12, 0x30, 0x10, 0x0A, 0x01, 0x00, 0x30, 0x0B, 0x04, 0x01, 'l', 0x31, 0x06, 0x04, 0x01, 'a', 0x04, 0x01, 'b' });
    String decodedPdu = Strings.dumpBytes(stream.array());
    stream.flip();
    // Allocate a LdapMessage Container
    LdapMessageContainer<ModifyRequestDecorator> ldapMessageContainer = new LdapMessageContainer<ModifyRequestDecorator>(codec);
    // Decode a ModifyRequest PDU
    try {
        ldapDecoder.decode(stream, ldapMessageContainer);
    } catch (DecoderException de) {
        de.printStackTrace();
        fail(de.getMessage());
    }
    // Check the decoded PDU
    ModifyRequest modifyRequest = ldapMessageContainer.getMessage();
    assertEquals(49, modifyRequest.getMessageId());
    assertEquals("cn=testModify,ou=users,ou=system", modifyRequest.getName().toString());
    Object[] modifications = modifyRequest.getModifications().toArray();
    assertEquals(1, modifications.length);
    Modification modification = (Modification) modifications[0];
    Attribute attributeValue = modification.getAttribute();
    assertEquals("l", Strings.toLowerCaseAscii(attributeValue.getUpId()));
    assertEquals(2, attributeValue.size());
    assertTrue(attributeValue.contains("a"));
    assertTrue(attributeValue.contains("b"));
    // Check the encoding
    try {
        ByteBuffer bb = encoder.encodeMessage(modifyRequest);
        // Check the length
        assertEquals(0x3D, bb.limit());
        String encodedPdu = Strings.dumpBytes(bb.array());
        assertEquals(encodedPdu, decodedPdu);
    } catch (EncoderException ee) {
        ee.printStackTrace();
        fail(ee.getMessage());
    }
}
Also used : ModifyRequestDecorator(org.apache.directory.api.ldap.codec.decorators.ModifyRequestDecorator) LdapMessageContainer(org.apache.directory.api.ldap.codec.api.LdapMessageContainer) Modification(org.apache.directory.api.ldap.model.entry.Modification) Attribute(org.apache.directory.api.ldap.model.entry.Attribute) ModifyRequest(org.apache.directory.api.ldap.model.message.ModifyRequest) ByteBuffer(java.nio.ByteBuffer) DecoderException(org.apache.directory.api.asn1.DecoderException) EncoderException(org.apache.directory.api.asn1.EncoderException) Asn1Decoder(org.apache.directory.api.asn1.ber.Asn1Decoder) Test(org.junit.Test) AbstractCodecServiceTest(org.apache.directory.api.ldap.codec.osgi.AbstractCodecServiceTest)

Example 39 with ModifyRequest

use of org.apache.directory.api.ldap.model.message.ModifyRequest in project directory-ldap-api by apache.

the class ModifyRequestTest method testDecodeModifyRequest2Attrs3valsSuccess.

/**
 * Test the decoding of a ModifyRequest
 */
@Test
public void testDecodeModifyRequest2Attrs3valsSuccess() throws LdapException {
    Asn1Decoder ldapDecoder = new Asn1Decoder();
    ByteBuffer stream = ByteBuffer.allocate(0x5C);
    stream.put(new byte[] { 0x30, // LDAPMessage ::= SEQUENCE {
    0x5A, 0x02, 0x01, // messageID MessageID
    0x01, 0x66, // CHOICE { ..., modifyRequest ModifyRequest, ...
    0x55, // object LDAPDN,
    0x04, 0x20, 'c', 'n', '=', 't', 'e', 's', 't', 'M', 'o', 'd', 'i', 'f', 'y', ',', 'o', 'u', '=', 'u', 's', 'e', 'r', 's', ',', 'o', 'u', '=', 's', 'y', 's', 't', 'e', 'm', 0x30, // modification SEQUENCE OF SEQUENCE {
    0x31, 0x30, 0x19, 0x0A, 0x01, // operation ENUMERATED {
    0x02, // modification AttributeTypeAndValues } }
    0x30, // AttributeTypeAndValues ::= SEQUENCE {
    0x14, 0x04, 0x01, // type AttributeDescription,
    'l', 0x31, // vals SET OF AttributeValue }
    0x0F, 0x04, 0x05, 'P', 'a', 'r', 'i', 's', 0x04, 0x06, 'L', 'o', 'n', 'd', 'o', 'n', 0x30, // modification SEQUENCE OF *SEQUENCE*  {
    0x14, 0x0A, 0x01, // operation ENUMERATED {
    0x00, // modification AttributeTypeAndValues } }
    0x30, // AttributeTypeAndValues ::= SEQUENCE {
    0x0f, // type AttributeDescription,
    0x04, 0x05, 'a', 't', 't', 'r', 's', 0x31, // vals SET OF AttributeValue }
    0x06, 0x04, 0x04, 't', 'e', 's', 't' });
    String decodedPdu = Strings.dumpBytes(stream.array());
    stream.flip();
    // Allocate a LdapMessage Container
    LdapMessageContainer<ModifyRequestDecorator> ldapMessageContainer = new LdapMessageContainer<ModifyRequestDecorator>(codec);
    // Decode a ModifyRequest PDU
    try {
        ldapDecoder.decode(stream, ldapMessageContainer);
    } catch (DecoderException de) {
        de.printStackTrace();
        fail(de.getMessage());
    }
    // Check the decoded PDU
    ModifyRequest modifyRequest = ldapMessageContainer.getMessage();
    assertEquals(1, modifyRequest.getMessageId());
    assertEquals("cn=testModify,ou=users,ou=system", modifyRequest.getName().toString());
    Object[] modifications = modifyRequest.getModifications().toArray();
    assertEquals(2, modifications.length);
    Modification modification = (Modification) modifications[0];
    Attribute attributeValue = modification.getAttribute();
    assertEquals("l", Strings.toLowerCaseAscii(attributeValue.getUpId()));
    assertTrue(attributeValue.contains("Paris"));
    assertTrue(attributeValue.contains("London"));
    modification = (Modification) modifications[1];
    attributeValue = modification.getAttribute();
    assertEquals("attrs", Strings.toLowerCaseAscii(attributeValue.getUpId()));
    String attrValue = attributeValue.getString();
    assertEquals("test", attrValue);
    // Check the encoding
    try {
        ByteBuffer bb = encoder.encodeMessage(modifyRequest);
        // Check the length
        assertEquals(0x5C, bb.limit());
        String encodedPdu = Strings.dumpBytes(bb.array());
        assertEquals(encodedPdu, decodedPdu);
    } catch (EncoderException ee) {
        ee.printStackTrace();
        fail(ee.getMessage());
    }
}
Also used : ModifyRequestDecorator(org.apache.directory.api.ldap.codec.decorators.ModifyRequestDecorator) LdapMessageContainer(org.apache.directory.api.ldap.codec.api.LdapMessageContainer) Modification(org.apache.directory.api.ldap.model.entry.Modification) Attribute(org.apache.directory.api.ldap.model.entry.Attribute) ModifyRequest(org.apache.directory.api.ldap.model.message.ModifyRequest) ByteBuffer(java.nio.ByteBuffer) DecoderException(org.apache.directory.api.asn1.DecoderException) EncoderException(org.apache.directory.api.asn1.EncoderException) Asn1Decoder(org.apache.directory.api.asn1.ber.Asn1Decoder) Test(org.junit.Test) AbstractCodecServiceTest(org.apache.directory.api.ldap.codec.osgi.AbstractCodecServiceTest)

Example 40 with ModifyRequest

use of org.apache.directory.api.ldap.model.message.ModifyRequest in project directory-ldap-api by apache.

the class ModifyRequestTest method testDecodeModifyRequestManyOperations.

/**
 * Test the decoding of a ModifyRequest, with different operations
 */
@Test
public void testDecodeModifyRequestManyOperations() throws LdapException {
    Asn1Decoder ldapDecoder = new Asn1Decoder();
    ByteBuffer stream = ByteBuffer.allocate(0x18C);
    stream.put(new byte[] { 0x30, (byte) 0x81, (byte) 0x89, 0x02, 0x01, 0x15, 0x66, 0x67, 0x04, // ModifyRequest object : cn=Tori Amos,ou=playground,dc=apache,dc=org
    0x2B, 'c', 'n', '=', 'T', 'o', 'r', 'i', ' ', 'A', 'm', 'o', 's', ',', 'o', 'u', '=', 'p', 'l', 'a', 'y', 'g', 'r', 'o', 'u', 'n', 'd', ',', 'd', 'c', '=', 'a', 'p', 'a', 'c', 'h', 'e', ',', 'd', 'c', '=', 'o', 'r', 'g', 0x30, // Modifications
    0x38, 0x30, // Modification
    0x24, 0x0A, 0x01, // Operation = ADD
    0x00, 0x30, // type : telephoneNumber
    0x1F, 0x04, 0x0F, 't', 'e', 'l', 'e', 'p', 'h', 'o', 'n', 'e', 'N', 'u', 'm', 'b', 'e', 'r', 0x31, // vals : 1234567890
    0x0C, 0x04, 0x0A, '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', 0x30, // Modification
    0x10, 0x0A, 0x01, // Operation = REPLACE
    0x02, 0x30, // type : cn
    0x0B, 0x04, 0x02, 'c', 'n', 0x31, // vals : XXX
    0x05, 0x04, 0x03, 'X', 'X', 'X', (byte) 0xA0, // Control : 2.16.840.1.113730.3.4.2
    0x1B, 0x30, 0x19, 0x04, 0x17, '2', '.', '1', '6', '.', '8', '4', '0', '.', '1', '.', '1', '1', '3', '7', '3', '0', '.', '3', '.', '4', '.', '2' });
    stream.flip();
    // Allocate a LdapMessage Container
    LdapMessageContainer<ModifyRequestDecorator> ldapMessageContainer = new LdapMessageContainer<ModifyRequestDecorator>(codec);
    // Decode a ModifyRequest PDU
    try {
        ldapDecoder.decode(stream, ldapMessageContainer);
    } catch (DecoderException de) {
        de.printStackTrace();
        fail(de.getMessage());
    }
    // Check the decoded PDU
    ModifyRequest modifyRequest = ldapMessageContainer.getMessage();
    assertEquals(21, modifyRequest.getMessageId());
    assertEquals("cn=Tori Amos,ou=playground,dc=apache,dc=org", modifyRequest.getName().toString());
    Object[] modifications = modifyRequest.getModifications().toArray();
    assertEquals(2, modifications.length);
    Modification modification = (Modification) modifications[0];
    Attribute attributeValue = modification.getAttribute();
    assertEquals("telephonenumber", Strings.toLowerCaseAscii(attributeValue.getId()));
    String attrValue = attributeValue.getString();
    assertEquals("1234567890", attrValue);
    modification = (Modification) modifications[1];
    attributeValue = modification.getAttribute();
    assertEquals("cn", Strings.toLowerCaseAscii(attributeValue.getUpId()));
    attrValue = attributeValue.getString();
    assertEquals("XXX", attrValue);
    // Check the encoding, by decoding and re-encoding the result
    try {
        ByteBuffer bb = encoder.encodeMessage(modifyRequest);
        // Check the length
        assertEquals(0x8C, bb.limit());
        String decodedPdu1 = Strings.dumpBytes(bb.array());
        try {
            ldapDecoder.decode(bb, ldapMessageContainer);
        } catch (DecoderException de) {
            de.printStackTrace();
            fail(de.getMessage());
        }
        ModifyRequest modifyRequest2 = ldapMessageContainer.getMessage();
        bb = encoder.encodeMessage(modifyRequest2);
        String decodedPdu2 = Strings.dumpBytes(bb.array());
        assertEquals(decodedPdu1, decodedPdu2);
    } catch (EncoderException ee) {
        ee.printStackTrace();
        fail(ee.getMessage());
    }
}
Also used : ModifyRequestDecorator(org.apache.directory.api.ldap.codec.decorators.ModifyRequestDecorator) LdapMessageContainer(org.apache.directory.api.ldap.codec.api.LdapMessageContainer) Modification(org.apache.directory.api.ldap.model.entry.Modification) Attribute(org.apache.directory.api.ldap.model.entry.Attribute) ModifyRequest(org.apache.directory.api.ldap.model.message.ModifyRequest) ByteBuffer(java.nio.ByteBuffer) DecoderException(org.apache.directory.api.asn1.DecoderException) EncoderException(org.apache.directory.api.asn1.EncoderException) Asn1Decoder(org.apache.directory.api.asn1.ber.Asn1Decoder) Test(org.junit.Test) AbstractCodecServiceTest(org.apache.directory.api.ldap.codec.osgi.AbstractCodecServiceTest)

Aggregations

ModifyRequest (org.apache.directory.api.ldap.model.message.ModifyRequest)40 Modification (org.apache.directory.api.ldap.model.entry.Modification)25 Test (org.junit.Test)24 LdapException (org.apache.directory.api.ldap.model.exception.LdapException)22 AbstractTest (org.apache.directory.api.dsmlv2.AbstractTest)17 Dsmlv2Parser (org.apache.directory.api.dsmlv2.Dsmlv2Parser)17 Attribute (org.apache.directory.api.ldap.model.entry.Attribute)14 ModifyRequestDecorator (org.apache.directory.api.ldap.codec.decorators.ModifyRequestDecorator)11 ModifyRequestImpl (org.apache.directory.api.ldap.model.message.ModifyRequestImpl)11 ModifyResponse (org.apache.directory.api.ldap.model.message.ModifyResponse)10 Dn (org.apache.directory.api.ldap.model.name.Dn)10 ByteBuffer (java.nio.ByteBuffer)7 DecoderException (org.apache.directory.api.asn1.DecoderException)7 EncoderException (org.apache.directory.api.asn1.EncoderException)7 Asn1Decoder (org.apache.directory.api.asn1.ber.Asn1Decoder)7 LdapMessageContainer (org.apache.directory.api.ldap.codec.api.LdapMessageContainer)7 AbstractCodecServiceTest (org.apache.directory.api.ldap.codec.osgi.AbstractCodecServiceTest)7 DefaultAttribute (org.apache.directory.api.ldap.model.entry.DefaultAttribute)7 DefaultModification (org.apache.directory.api.ldap.model.entry.DefaultModification)7 Control (org.apache.directory.api.ldap.model.message.Control)7