### Question:
SimpleLdapClient implements LdapClient { @Override public String authenticate(final Auth auth){ for(String template : templates){ String userNameTemplate = convertTemplate(template, auth.getUserName()); LdapConfig config = new LdapConfig(url, userNameTemplate, auth.getPassword()); if(tryAuthenticate(config)){ return auth.getUserName(); } } throw new NotAuthorizedException(); } SimpleLdapClient(final List<String> templates, final String url); @Override String authenticate(final Auth auth);  }### Answer:
@Test(expected = WebApplicationException.class) public void when_wrong_password_then_throw_401() throws LdapException{ client = client("cn=${login},ou=system"); client.authenticate(new Auth(USER_NAME, "wrong_password".toCharArray())); }
@Test(expected = WebApplicationException.class) public void when_wrong_user_name_then_throw_401() throws LdapException{ client = client("cn=${login},ou=system"); client.authenticate(new Auth("wrong_user", USER_PASSWORD.toCharArray())); }
@Test(expected = WebApplicationException.class) public void when_multiple_templates_and_none_is_valid_then_throw_401() throws LdapException{ client = client("cn=${login},ou=notexisting", "cn=${login},ou=nothing, dc=example, dc=com"); String username = client.authenticate(new Auth(USER_NAME, USER_PASSWORD.toCharArray())); assertThat(username).isEqualTo(USER_NAME); }
@Test public void when_single_template_and_valid_credentials_then_authenticate_and_return_correct_user_name() throws LdapException{ client = client("cn=${login},ou=system"); String username = client.authenticate(new Auth(USER_NAME, USER_PASSWORD.toCharArray())); assertThat(username).isEqualTo(USER_NAME); }
@Test public void when_multiple_templates_and_last_one_is_valid_then_authenticate() throws LdapException{ client = client("cn=${login},ou=notexisting", "cn=${login},ou=nothing, dc=example, dc=com", "cn=${login},ou=system"); String username = client.authenticate(new Auth(USER_NAME, USER_PASSWORD.toCharArray())); assertThat(username).isEqualTo(USER_NAME); }
@Test public void when_multiple_templates_and_first_one_is_valid_then_authenticate() throws LdapException{ client = client("cn=${login},ou=system", "cn=${login},ou=notexisting", "cn=${login},ou=nothing, dc=example, dc=com"); String username = client.authenticate(new Auth(USER_NAME, USER_PASSWORD.toCharArray())); assertThat(username).isEqualTo(USER_NAME); }