/**
  * @return details for specified user, or null if such user doesn't exist
  * @throws SonarException if unable to retrieve details
  */
 public UserDetails doGetUserDetails(Context context) {
   // If there are no userMappings available, we can not retrieve user details.
   String username = context.getUsername();
   LOG.debug("Requesting details for user {}", username);
   if (userMappings.isEmpty()) {
     String errorMessage = "Unable to retrieve user details: No user mappings found.";
     LOG.debug(errorMessage);
     throw new SonarException(errorMessage);
   }
   UserDetails details = null;
   SonarException sonarException = null;
   for (String serverKey : userMappings.keySet()) {
     SearchResult searchResult = null;
     try {
       LdapUserMapping ldapUserMapping = userMappings.get(serverKey);
       searchResult =
           ldapUserMapping
               .createSearch(contextFactories.get(serverKey), username)
               .returns(
                   ldapUserMapping.getEmailAttribute(), ldapUserMapping.getRealNameAttribute())
               .findUnique();
     } catch (NamingException e) {
       // just in case if Sonar silently swallowed exception
       LOG.debug(e.getMessage(), e);
       sonarException =
           new SonarException(
               "Unable to retrieve details for user " + username + " in " + serverKey, e);
     }
     if (searchResult != null) {
       try {
         details = mapUserDetails(serverKey, searchResult);
         // if no exceptions occur, we found the user and mapped his details.
         break;
       } catch (NamingException e) {
         // just in case if Sonar silently swallowed exception
         LOG.debug(e.getMessage(), e);
         sonarException =
             new SonarException(
                 "Unable to retrieve details for user " + username + " in " + serverKey, e);
       }
     } else {
       // user not found
       LOG.debug("User {} not found in " + serverKey, username);
       continue;
     }
   }
   if (details == null && sonarException != null) {
     // No user found and there is an exception so there is a reason the user could not be found.
     throw sonarException;
   }
   return details;
 }
  @Test
  public void defaults() {
    LdapUserMapping userMapping = new LdapUserMapping(new Settings());
    assertThat(userMapping.getBaseDn(), equalTo(null));
    assertThat(userMapping.getObjectClass(), equalTo("inetOrgPerson"));
    assertThat(userMapping.getLoginAttribute(), equalTo("uid"));
    assertThat(userMapping.getRealNameAttribute(), equalTo("cn"));
    assertThat(userMapping.getEmailAttribute(), equalTo("mail"));

    assertThat(
        userMapping.toString(),
        equalTo(
            "LdapUserMapping{"
                + "baseDn=null,"
                + " objectClass=inetOrgPerson,"
                + " loginAttribute=uid,"
                + " realNameAttribute=cn,"
                + " emailAttribute=mail}"));
  }