### Question:
LogsearchExternalServerAuthenticationProvider extends LogsearchAbstractAuthenticationProvider { @Override public Authentication authenticate(Authentication authentication) throws AuthenticationException { if (!authPropsConfig.isAuthExternalEnabled()) { LOG.debug("external server auth is disabled."); return authentication; } String username = authentication.getName(); String password = (String) authentication.getCredentials(); if (StringUtils.isBlank(username)) { throw new BadCredentialsException("Username can't be null or empty."); } if (StringUtils.isBlank(password)) { throw new BadCredentialsException("Password can't be null or empty."); } password = StringEscapeUtils.unescapeHtml(password); username = StringEscapeUtils.unescapeHtml(username); try { String finalLoginUrl = authPropsConfig.getExternalAuthLoginUrl().replace("$USERNAME", username); String responseObj = (String) externalServerClient.sendGETRequest(finalLoginUrl, String.class, username, password); if (!isAllowedRole(responseObj)) { LOG.error(username + " doesn't have permission"); throw new BadCredentialsException("Invalid User"); } } catch (Exception e) { LOG.error("Login failed for username :" + username + " Error :" + e.getLocalizedMessage()); throw new BadCredentialsException("Bad credentials"); } authentication = new UsernamePasswordAuthenticationToken(username, password, getAuthorities()); return authentication; }  @Override Authentication authenticate(Authentication authentication);  }### Answer:
@Test public void testAuthenticationUnsuccessful() throws Exception { expect(mockAuthPropsConfig.isAuthExternalEnabled()).andReturn(true); expect(mockAuthPropsConfig.getExternalAuthLoginUrl()).andReturn("http: expect(mockAuthPropsConfig.getAllowedRoles()).andReturn(Arrays.asList("AMBARI.ADMINISTRATOR")); expect(mockExternalServerClient.sendGETRequest("http: .andReturn("{\"permission_name\": \"NOT.AMBARI.ADMINISTRATOR\" }"); replay(mockAuthPropsConfig, mockExternalServerClient); Authentication authentication = new TestingAuthenticationToken("principal", "credentials"); try { provider.authenticate(authentication); assertTrue("Should have thrown BadCredentialsException", false); } catch (BadCredentialsException e) { assertEquals("Bad credentials", e.getMessage()); } verify(mockAuthPropsConfig, mockExternalServerClient); }
@Test public void testAuthenticationSuccessful() throws Exception { expect(mockAuthPropsConfig.isAuthExternalEnabled()).andReturn(true); expect(mockAuthPropsConfig.getExternalAuthLoginUrl()).andReturn("http: expect(mockAuthPropsConfig.getAllowedRoles()).andReturn(Arrays.asList("AMBARI.ADMINISTRATOR")); expect(mockExternalServerClient.sendGETRequest("http: .andReturn("{\"permission_name\": \"AMBARI.ADMINISTRATOR\" }"); replay(mockAuthPropsConfig, mockExternalServerClient); Authentication authentication = new TestingAuthenticationToken("principal", "credentials"); Authentication authenticationResult = provider.authenticate(authentication); assertEquals("principal", authenticationResult.getName()); assertEquals("credentials", authenticationResult.getCredentials()); assertEquals(1, authenticationResult.getAuthorities().size()); assertEquals(new SimpleGrantedAuthority("ROLE_USER"), authenticationResult.getAuthorities().iterator().next()); verify(mockAuthPropsConfig, mockExternalServerClient); }