Search in sources :

Example 21 with ZMutableEntry

use of com.zimbra.cs.ldap.ZMutableEntry in project zm-mailbox by Zimbra.

the class TestLdapZMutableEntry method mapToAttrsBinarySingle.

@Test
public void mapToAttrsBinarySingle() throws Exception {
    int NUM_BYTES_IN_BINARY_DATA = 64;
    String KEY_SINGLE_BINARY = Provisioning.A_userSMIMECertificate;
    String KEY_SINGLE_BINARY_TRANSFER = Provisioning.A_userCertificate;
    BinaryLdapData.Content VALUE_BINARY = BinaryLdapData.Content.generateContent(NUM_BYTES_IN_BINARY_DATA);
    BinaryLdapData.Content VALUE_BINARY_TRANSFER = BinaryLdapData.Content.generateContent(NUM_BYTES_IN_BINARY_DATA);
    Map<String, Object> attrMap = new HashMap<String, Object>();
    attrMap.put(KEY_SINGLE_BINARY, VALUE_BINARY.getString());
    attrMap.put(KEY_SINGLE_BINARY_TRANSFER, VALUE_BINARY_TRANSFER.getString());
    ZMutableEntry entry = LdapClient.createMutableEntry();
    entry.mapToAttrs(attrMap);
    /*
         * this will fail.  We do not handle binary data in this path,
         * nor is there a use case for it.
         * 
         * Note: ZMutableEntry and ZAttributes encapsulate a 
         * SDK(UBID or JNDI) native Entry/Attributes.  Binary data
         * are always stored in byte[] in those.  When the SDK object 
         * is "mapped" to a Map<String, Object>, used in Provisioning,
         * it is then base64 encoded, and appear as a String.
         */
    // String valueGot = entry.getAttrString(KEY_SINGLE_BINARY);
    // assertEquals(VALUE1.getString(), valueGot); 
    // this will work, because the ZAttribute.getAttrs() base64 encode 
    // the binary data when putting them in the Map<String, Object>.
    // contains binary data
    Map<String, Object> attrMapGot = entry.getAttributes().getAttrs();
    Object valueGot = attrMapGot.get(KEY_SINGLE_BINARY);
    assertTrue(valueGot instanceof String);
    assertTrue(((String) valueGot).equals(VALUE_BINARY.getString()));
    // is binary transfer
    attrMapGot = entry.getAttributes().getAttrs();
    valueGot = attrMapGot.get(KEY_SINGLE_BINARY_TRANSFER);
    assertTrue(valueGot instanceof String);
    assertTrue(((String) valueGot).equals(VALUE_BINARY_TRANSFER.getString()));
}
Also used : ZMutableEntry(com.zimbra.cs.ldap.ZMutableEntry) HashMap(java.util.HashMap) BinaryLdapData(com.zimbra.qa.unittest.prov.BinaryLdapData)

Example 22 with ZMutableEntry

use of com.zimbra.cs.ldap.ZMutableEntry in project zm-mailbox by Zimbra.

the class TestLdapZMutableEntry method setAttr.

@Test
public void setAttr() throws Exception {
    String KEY = "key";
    String VALUE1 = "value1";
    String VALUE2 = "value2";
    ZMutableEntry entry = LdapClient.createMutableEntry();
    entry.setAttr(KEY, VALUE1);
    // should REPLACE
    entry.setAttr(KEY, VALUE2);
    ZAttributes zattrs = entry.getAttributes();
    Map<String, Object> attrs = zattrs.getAttrs();
    Object valueGot = attrs.get(KEY);
    assertTrue(valueGot instanceof String);
}
Also used : ZMutableEntry(com.zimbra.cs.ldap.ZMutableEntry) ZAttributes(com.zimbra.cs.ldap.ZAttributes)

Example 23 with ZMutableEntry

use of com.zimbra.cs.ldap.ZMutableEntry in project zm-mailbox by Zimbra.

the class TestLdapZMutableEntry method getAttrString.

@Test
public void getAttrString() throws Exception {
    String KEY = "key";
    String VALUE1 = "value1";
    String VALUE2 = "value2";
    ZMutableEntry entry = LdapClient.createMutableEntry();
    entry.setAttr(KEY, VALUE1);
    // single value
    String valueGot = entry.getAttrString(KEY);
    assertEquals(VALUE1, valueGot);
    // multi value - should just return the first one
    Set<String> values = new HashSet<String>();
    values.add(VALUE2);
    entry.addAttr(KEY, values);
    valueGot = entry.getAttrString(KEY);
    // order is not guaranteed
    assertTrue(VALUE1.equals(valueGot) || VALUE2.equals(valueGot));
}
Also used : ZMutableEntry(com.zimbra.cs.ldap.ZMutableEntry) HashSet(java.util.HashSet)

Example 24 with ZMutableEntry

use of com.zimbra.cs.ldap.ZMutableEntry in project zm-mailbox by Zimbra.

the class TestLdapZMutableEntry method addAttr.

@Test
public void addAttr() throws Exception {
    String KEY = "key";
    String VALUE1 = "value1";
    String VALUE2 = "value2";
    String VALUE3 = "value3";
    ZMutableEntry entry = LdapClient.createMutableEntry();
    Set<String> values = new HashSet<String>();
    values.add(VALUE1);
    values.add(VALUE2);
    entry.addAttr(KEY, values);
    values.clear();
    values.add(VALUE1);
    values.add(VALUE3);
    // should MERGE
    entry.addAttr(KEY, values);
    ZAttributes zattrs = entry.getAttributes();
    Map<String, Object> attrs = zattrs.getAttrs();
    Object valueGot = attrs.get(KEY);
    assertTrue(valueGot instanceof String[]);
    List<String> valuesGot = Arrays.asList((String[]) valueGot);
    assertTrue(valuesGot.contains(VALUE1));
    assertTrue(valuesGot.contains(VALUE2));
    assertTrue(valuesGot.contains(VALUE3));
}
Also used : ZMutableEntry(com.zimbra.cs.ldap.ZMutableEntry) ZAttributes(com.zimbra.cs.ldap.ZAttributes) HashSet(java.util.HashSet)

Example 25 with ZMutableEntry

use of com.zimbra.cs.ldap.ZMutableEntry in project zm-mailbox by Zimbra.

the class TestLdapZMutableEntry method mapToAttrsBinaryMulti.

@Test
public void mapToAttrsBinaryMulti() throws Exception {
    int NUM_BYTES_IN_BINARY_DATA = 64;
    String KEY_MULTI_BINARY = Provisioning.A_userSMIMECertificate;
    String KEY_MULTI_BINARY_TRANSFER = Provisioning.A_userCertificate;
    BinaryLdapData.Content VALUE_BINARY_1 = BinaryLdapData.Content.generateContent(NUM_BYTES_IN_BINARY_DATA);
    BinaryLdapData.Content VALUE_BINARY_2 = BinaryLdapData.Content.generateContent(NUM_BYTES_IN_BINARY_DATA);
    BinaryLdapData.Content VALUE_BINARY_TRANSFER_1 = BinaryLdapData.Content.generateContent(NUM_BYTES_IN_BINARY_DATA);
    BinaryLdapData.Content VALUE_BINARY_TRANSFER_2 = BinaryLdapData.Content.generateContent(NUM_BYTES_IN_BINARY_DATA);
    Map<String, Object> attrMap = new HashMap<String, Object>();
    attrMap.put(KEY_MULTI_BINARY, new String[] { VALUE_BINARY_1.getString(), VALUE_BINARY_2.getString() });
    attrMap.put(KEY_MULTI_BINARY_TRANSFER, new String[] { VALUE_BINARY_TRANSFER_1.getString(), VALUE_BINARY_TRANSFER_2.getString() });
    ZMutableEntry entry = LdapClient.createMutableEntry();
    entry.mapToAttrs(attrMap);
    // contains binary data
    Map<String, Object> attrMapGot = entry.getAttributes().getAttrs();
    Object valueGot = attrMapGot.get(KEY_MULTI_BINARY);
    assertTrue(valueGot instanceof String[]);
    List<String> valuesGot = Arrays.asList((String[]) valueGot);
    assertTrue(valuesGot.contains(VALUE_BINARY_1.getString()));
    assertTrue(valuesGot.contains(VALUE_BINARY_2.getString()));
    // is binary transfer
    attrMapGot = entry.getAttributes().getAttrs();
    valueGot = attrMapGot.get(KEY_MULTI_BINARY_TRANSFER);
    assertTrue(valueGot instanceof String[]);
    valuesGot = Arrays.asList((String[]) valueGot);
    assertTrue(valuesGot.contains(VALUE_BINARY_TRANSFER_1.getString()));
    assertTrue(valuesGot.contains(VALUE_BINARY_TRANSFER_2.getString()));
}
Also used : ZMutableEntry(com.zimbra.cs.ldap.ZMutableEntry) HashMap(java.util.HashMap) BinaryLdapData(com.zimbra.qa.unittest.prov.BinaryLdapData)

Aggregations

ZMutableEntry (com.zimbra.cs.ldap.ZMutableEntry)26 ZLdapContext (com.zimbra.cs.ldap.ZLdapContext)18 LdapEntryAlreadyExistException (com.zimbra.cs.ldap.LdapException.LdapEntryAlreadyExistException)15 AccountServiceException (com.zimbra.cs.account.AccountServiceException)14 CallbackContext (com.zimbra.cs.account.callback.CallbackContext)14 LdapException (com.zimbra.cs.ldap.LdapException)14 ServiceException (com.zimbra.common.service.ServiceException)13 AuthFailedServiceException (com.zimbra.cs.account.AccountServiceException.AuthFailedServiceException)13 Date (java.util.Date)13 Domain (com.zimbra.cs.account.Domain)6 LdapDomain (com.zimbra.cs.account.ldap.entry.LdapDomain)6 HashMap (java.util.HashMap)6 LdapEntry (com.zimbra.cs.account.ldap.entry.LdapEntry)4 ZAttributes (com.zimbra.cs.ldap.ZAttributes)3 Cos (com.zimbra.cs.account.Cos)2 LdapCos (com.zimbra.cs.account.ldap.entry.LdapCos)2 LdapDynamicGroup (com.zimbra.cs.account.ldap.entry.LdapDynamicGroup)2 GenericLdapConfig (com.zimbra.cs.ldap.LdapServerConfig.GenericLdapConfig)2 BinaryLdapData (com.zimbra.qa.unittest.prov.BinaryLdapData)2 HashSet (java.util.HashSet)2