Search in sources :

Example 21 with AddRequest

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

the class AddRequestTest method testRequestWith1AttrWithoutValue.

/**
 * Test parsing of a request with an Attr elements with value
 */
@Test
public void testRequestWith1AttrWithoutValue() {
    Dsmlv2Parser parser = null;
    try {
        parser = newParser();
        parser.setInput(AddRequestTest.class.getResource("request_with_1_attr_without_value.xml").openStream(), "UTF-8");
        parser.parse();
    } catch (Exception e) {
        fail(e.getMessage());
    }
    AddRequest addRequest = (AddRequest) parser.getBatchRequest().getCurrentRequest();
    Entry entry = addRequest.getEntry();
    assertEquals(1, entry.size());
    // Getting the Attribute
    Iterator<Attribute> attributeIterator = entry.iterator();
    Attribute attribute = attributeIterator.next();
    assertEquals("objectclass", attribute.getUpId());
    // Getting the Value
    Iterator<Value> valueIterator = attribute.iterator();
    assertFalse(valueIterator.hasNext());
}
Also used : AddRequest(org.apache.directory.api.ldap.model.message.AddRequest) Entry(org.apache.directory.api.ldap.model.entry.Entry) Attribute(org.apache.directory.api.ldap.model.entry.Attribute) Value(org.apache.directory.api.ldap.model.entry.Value) Dsmlv2Parser(org.apache.directory.api.dsmlv2.Dsmlv2Parser) Test(org.junit.Test) AbstractTest(org.apache.directory.api.dsmlv2.AbstractTest)

Example 22 with AddRequest

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

the class AddRequestTest method testRequestWith1ControlEmptyValue.

/**
 * Test parsing of a request with a (optional) Control element with empty value
 */
@Test
public void testRequestWith1ControlEmptyValue() {
    Dsmlv2Parser parser = null;
    try {
        parser = newParser();
        parser.setInput(AddRequestTest.class.getResource("request_with_1_control_empty_value.xml").openStream(), "UTF-8");
        parser.parse();
    } catch (Exception e) {
        fail(e.getMessage());
    }
    AddRequest addRequest = (AddRequest) parser.getBatchRequest().getCurrentRequest();
    Map<String, Control> controls = addRequest.getControls();
    assertEquals(1, addRequest.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());
    assertFalse(((DsmlControl<?>) control).hasValue());
}
Also used : AddRequest(org.apache.directory.api.ldap.model.message.AddRequest) DsmlControl(org.apache.directory.api.dsmlv2.DsmlControl) Control(org.apache.directory.api.ldap.model.message.Control) Dsmlv2Parser(org.apache.directory.api.dsmlv2.Dsmlv2Parser) Test(org.junit.Test) AbstractTest(org.apache.directory.api.dsmlv2.AbstractTest)

Example 23 with AddRequest

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

the class AddRequestTest method testRequestWith2Controls.

/**
 * Test parsing of a request with 2 (optional) Control elements
 */
@Test
public void testRequestWith2Controls() {
    Dsmlv2Parser parser = null;
    try {
        parser = newParser();
        parser.setInput(AddRequestTest.class.getResource("request_with_2_controls.xml").openStream(), "UTF-8");
        parser.parse();
    } catch (Exception e) {
        fail(e.getMessage());
    }
    AddRequest addRequest = (AddRequest) parser.getBatchRequest().getCurrentRequest();
    Map<String, Control> controls = addRequest.getControls();
    assertEquals(2, addRequest.getControls().size());
    Control control = controls.get("1.2.840.113556.1.4.789");
    assertNotNull(control);
    assertFalse(control.isCritical());
    assertEquals("1.2.840.113556.1.4.789", control.getOid());
    assertEquals("Some other text", Strings.utf8ToString(((DsmlControl<?>) control).getValue()));
}
Also used : AddRequest(org.apache.directory.api.ldap.model.message.AddRequest) 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) Test(org.junit.Test) AbstractTest(org.apache.directory.api.dsmlv2.AbstractTest)

Example 24 with AddRequest

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

the class AddRequestTest method testRequestWith1AttrEmptyValue.

/**
 * Test parsing of a request with an Attr elements with empty value
 */
@Test
public void testRequestWith1AttrEmptyValue() {
    Dsmlv2Parser parser = null;
    try {
        parser = newParser();
        parser.setInput(AddRequestTest.class.getResource("request_with_1_attr_empty_value.xml").openStream(), "UTF-8");
        parser.parse();
    } catch (Exception e) {
        fail(e.getMessage());
    }
    AddRequest addRequest = (AddRequest) parser.getBatchRequest().getCurrentRequest();
    Entry entry = addRequest.getEntry();
    assertEquals(1, entry.size());
    // Getting the Attribute
    Iterator<Attribute> attributeIterator = entry.iterator();
    Attribute attribute = attributeIterator.next();
    assertEquals("objectclass", attribute.getUpId());
    // Getting the Value
    Iterator<Value> valueIterator = attribute.iterator();
    assertFalse(valueIterator.hasNext());
}
Also used : AddRequest(org.apache.directory.api.ldap.model.message.AddRequest) Entry(org.apache.directory.api.ldap.model.entry.Entry) Attribute(org.apache.directory.api.ldap.model.entry.Attribute) Value(org.apache.directory.api.ldap.model.entry.Value) Dsmlv2Parser(org.apache.directory.api.dsmlv2.Dsmlv2Parser) Test(org.junit.Test) AbstractTest(org.apache.directory.api.dsmlv2.AbstractTest)

Example 25 with AddRequest

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

the class AddRequestTest method testDecodeAddRequestSuccess.

/**
 * Test the decoding of a AddRequest
 */
@Test
public void testDecodeAddRequestSuccess() throws NamingException {
    Asn1Decoder ldapDecoder = new Asn1Decoder();
    ByteBuffer stream = ByteBuffer.allocate(0x59);
    stream.put(new byte[] { 0x30, // LDAPMessage ::= SEQUENCE {
    0x57, 0x02, 0x01, // messageID MessageID
    0x01, 0x68, // CHOICE { ..., addRequest AddRequest, ...
    0x52, // entry 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', // attributes AttributeList }
    0x30, // AttributeList ::= SEQUENCE OF SEQUENCE {
    0x2E, 0x30, // attribute 1
    0x0c, 0x04, 0x01, // type AttributeDescription,
    'l', 0x31, // vals SET OF AttributeValue }
    0x07, 0x04, 0x05, 'P', 'a', 'r', 'i', 's', 0x30, // attribute 2
    0x1E, // type AttributeDescription,
    0x04, 0x05, 'a', 't', 't', 'r', 's', 0x31, // vals SET
    0x15, // }
    0x04, 0x05, 't', 'e', 's', 't', '1', 0x04, 0x05, 't', 'e', 's', 't', '2', 0x04, 0x05, 't', 'e', 's', 't', '3' });
    Strings.dumpBytes(stream.array());
    stream.flip();
    // Allocate a LdapMessage Container
    LdapMessageContainer<AddRequestDecorator> container = new LdapMessageContainer<AddRequestDecorator>(codec);
    // Decode a AddRequest message
    try {
        ldapDecoder.decode(stream, container);
    } catch (DecoderException de) {
        de.printStackTrace();
        fail(de.getMessage());
    }
    AddRequest addRequest = container.getMessage();
    // Check the decoded message
    assertEquals(1, addRequest.getMessageId());
    assertEquals("cn=testModify,ou=users,ou=system", addRequest.getEntryDn().toString());
    Entry entry = addRequest.getEntry();
    assertEquals(2, entry.size());
    Set<String> expectedTypes = new HashSet<String>();
    expectedTypes.add("l");
    expectedTypes.add("attrs");
    Map<String, Set<String>> typesVals = new HashMap<String, Set<String>>();
    Set<String> lVal1 = new HashSet<String>();
    lVal1.add("Paris");
    typesVals.put("l", lVal1);
    Set<String> lVal2 = new HashSet<String>();
    lVal2.add("test1");
    lVal2.add("test2");
    lVal2.add("test3");
    typesVals.put("attrs", lVal2);
    Attribute attribute = entry.get("l");
    assertTrue(expectedTypes.contains(Strings.toLowerCaseAscii(attribute.getId())));
    Set<String> vals = (Set<String>) typesVals.get(Strings.toLowerCaseAscii(attribute.getId()));
    for (Value value : attribute) {
        assertTrue(vals.contains(value.getValue()));
        vals.remove(value.getValue());
    }
    attribute = entry.get("attrs");
    assertTrue(expectedTypes.contains(Strings.toLowerCaseAscii(attribute.getId())));
    vals = (Set<String>) typesVals.get(Strings.toLowerCaseAscii(attribute.getId()));
    for (Value value : attribute) {
        assertTrue(vals.contains(value.getValue()));
        vals.remove(value.getValue());
    }
    // Check the encoding
    try {
        ByteBuffer bb = encoder.encodeMessage(addRequest);
        // Check the length
        assertEquals(0x59, bb.limit());
        // kept. Let's decode again and compare the resulting AddRequest
        try {
            ldapDecoder.decode(bb, container);
        } catch (DecoderException de) {
            de.printStackTrace();
            fail(de.getMessage());
        }
        AddRequest addRequest2 = container.getMessage();
        assertEquals(addRequest, addRequest2);
    } catch (EncoderException ee) {
        ee.printStackTrace();
        fail(ee.getMessage());
    }
}
Also used : LdapMessageContainer(org.apache.directory.api.ldap.codec.api.LdapMessageContainer) HashSet(java.util.HashSet) Set(java.util.Set) HashMap(java.util.HashMap) Attribute(org.apache.directory.api.ldap.model.entry.Attribute) ByteBuffer(java.nio.ByteBuffer) AddRequest(org.apache.directory.api.ldap.model.message.AddRequest) DecoderException(org.apache.directory.api.asn1.DecoderException) EncoderException(org.apache.directory.api.asn1.EncoderException) Entry(org.apache.directory.api.ldap.model.entry.Entry) Asn1Decoder(org.apache.directory.api.asn1.ber.Asn1Decoder) Value(org.apache.directory.api.ldap.model.entry.Value) AddRequestDecorator(org.apache.directory.api.ldap.codec.decorators.AddRequestDecorator) HashSet(java.util.HashSet) Test(org.junit.Test) AbstractCodecServiceTest(org.apache.directory.api.ldap.codec.osgi.AbstractCodecServiceTest)

Aggregations

AddRequest (org.apache.directory.api.ldap.model.message.AddRequest)26 Test (org.junit.Test)19 AbstractTest (org.apache.directory.api.dsmlv2.AbstractTest)16 Dsmlv2Parser (org.apache.directory.api.dsmlv2.Dsmlv2Parser)16 Entry (org.apache.directory.api.ldap.model.entry.Entry)12 Value (org.apache.directory.api.ldap.model.entry.Value)11 Attribute (org.apache.directory.api.ldap.model.entry.Attribute)10 Control (org.apache.directory.api.ldap.model.message.Control)7 DsmlControl (org.apache.directory.api.dsmlv2.DsmlControl)5 AddRequestDecorator (org.apache.directory.api.ldap.codec.decorators.AddRequestDecorator)5 DecoderException (org.apache.directory.api.asn1.DecoderException)4 AddRequestImpl (org.apache.directory.api.ldap.model.message.AddRequestImpl)4 ByteBuffer (java.nio.ByteBuffer)3 EncoderException (org.apache.directory.api.asn1.EncoderException)3 Asn1Decoder (org.apache.directory.api.asn1.ber.Asn1Decoder)3 BatchRequestDsml (org.apache.directory.api.dsmlv2.request.BatchRequestDsml)3 LdapMessageContainer (org.apache.directory.api.ldap.codec.api.LdapMessageContainer)3 AbstractCodecServiceTest (org.apache.directory.api.ldap.codec.osgi.AbstractCodecServiceTest)3 AbandonRequest (org.apache.directory.api.ldap.model.message.AbandonRequest)3 BindRequest (org.apache.directory.api.ldap.model.message.BindRequest)3