use of org.apache.directory.api.ldap.model.message.ModifyRequest in project directory-ldap-api by apache.
the class ModifyRequestTest method testRequestWithModificationWithEmptyValue.
/**
* Test parsing of a request with a Modification element with an empty value
* @throws NamingException
*/
@Test
public void testRequestWithModificationWithEmptyValue() throws LdapException {
Dsmlv2Parser parser = null;
try {
parser = newParser();
parser.setInput(ModifyRequestTest.class.getResource("request_with_modification_with_empty_value.xml").openStream(), "UTF-8");
parser.parse();
} catch (Exception e) {
fail(e.getMessage());
}
ModifyRequest modifyRequest = (ModifyRequest) parser.getBatchRequest().getCurrentRequest();
Collection<Modification> modifications = modifyRequest.getModifications();
assertEquals(1, modifications.size());
Modification modification = modifications.iterator().next();
assertEquals(ModificationOperation.ADD_ATTRIBUTE, modification.getOperation());
Attribute attribute = modification.getAttribute();
assertEquals("directreport", attribute.getId());
assertEquals(1, attribute.size());
assertEquals("", attribute.get().getValue());
}
use of org.apache.directory.api.ldap.model.message.ModifyRequest in project directory-ldap-api by apache.
the class ModifyRequestTest method testRequestWith2Controls.
/**
* Test parsing of a request with 2 (optional) Control elements
*/
@Test
public void testRequestWith2Controls() {
Dsmlv2Parser parser = null;
try {
parser = newParser();
parser.setInput(ModifyRequestTest.class.getResource("request_with_2_controls.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(2, modifyRequest.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()));
}
use of org.apache.directory.api.ldap.model.message.ModifyRequest in project directory-ldap-api by apache.
the class ModifyRequestTest method testRequestWithRequestId.
/**
* Test parsing of a request with the (optional) requestID attribute
*/
@Test
public void testRequestWithRequestId() {
Dsmlv2Parser parser = null;
try {
parser = newParser();
parser.setInput(ModifyRequestTest.class.getResource("request_with_requestID_attribute.xml").openStream(), "UTF-8");
parser.parse();
} catch (Exception e) {
fail(e.getMessage());
}
ModifyRequest modifyRequest = (ModifyRequest) parser.getBatchRequest().getCurrentRequest();
assertEquals(456, modifyRequest.getMessageId());
}
use of org.apache.directory.api.ldap.model.message.ModifyRequest in project directory-ldap-api by apache.
the class BatchRequestTest method testResponseWith2ModifyRequest.
/**
* Test parsing of a Request with 2 ModifyRequest
*/
@Test
public void testResponseWith2ModifyRequest() {
Dsmlv2Parser parser = null;
try {
parser = newParser();
parser.setInput(BatchRequestTest.class.getResource("request_with_2_ModifyRequest.xml").openStream(), "UTF-8");
parser.parse();
} catch (Exception e) {
fail(e.getMessage());
}
BatchRequestDsml batchRequest = parser.getBatchRequest();
assertEquals(2, batchRequest.getRequests().size());
if (batchRequest.getCurrentRequest() instanceof ModifyRequest) {
assertTrue(true);
} else {
fail();
}
}
use of org.apache.directory.api.ldap.model.message.ModifyRequest in project directory-ldap-api by apache.
the class ModifyRequestDsml method toDsml.
/**
* {@inheritDoc}
*/
@Override
public Element toDsml(Element root) {
Element element = super.toDsml(root);
ModifyRequest request = getDecorated();
// Dn
if (request.getName() != null) {
element.addAttribute("dn", request.getName().getName());
}
// Modifications
Collection<Modification> modifications = request.getModifications();
for (Modification modification : modifications) {
Element modElement = element.addElement("modification");
if (modification.getAttribute() != null) {
modElement.addAttribute("name", modification.getAttribute().getId());
for (Value value : modification.getAttribute()) {
if (value.getValue() != null) {
if (ParserUtils.needsBase64Encoding(value.getValue())) {
Namespace xsdNamespace = new Namespace("xsd", ParserUtils.XML_SCHEMA_URI);
Namespace xsiNamespace = new Namespace("xsi", ParserUtils.XML_SCHEMA_INSTANCE_URI);
element.getDocument().getRootElement().add(xsdNamespace);
element.getDocument().getRootElement().add(xsiNamespace);
Element valueElement = modElement.addElement("value").addText(ParserUtils.base64Encode(value.getValue()));
valueElement.addAttribute(new QName("type", xsiNamespace), "xsd:" + ParserUtils.BASE64BINARY);
} else {
modElement.addElement("value").setText(value.getValue());
}
}
}
}
ModificationOperation operation = modification.getOperation();
if (operation == ModificationOperation.ADD_ATTRIBUTE) {
modElement.addAttribute("operation", "add");
} else if (operation == ModificationOperation.REPLACE_ATTRIBUTE) {
modElement.addAttribute("operation", "replace");
} else if (operation == ModificationOperation.REMOVE_ATTRIBUTE) {
modElement.addAttribute("operation", "delete");
}
}
return element;
}
Aggregations