ResourceContext { public void authorize(String action, String resource, String trustedDomain) { principal = authenticate(); Http.authorize(authorizer, principal, action, resource, trustedDomain); } ResourceContext(HttpServletRequest request, HttpServletResponse response,
            Http.AuthorityList authorities, Authorizer authorizer); static void setSendMultipleWwwAuthenticateHeaders(boolean bSendMultipleHeaders); HttpServletRequest request(); HttpServletResponse response(); Principal principal(); Principal authenticate(); Principal authenticate(boolean optionalAuth); void authorize(String action, String resource, String trustedDomain); void sendAuthenticateChallenges(ResourceException exc); String getRequestDomain(); void setRequestDomain(String requestDomain);  }
@Test public void testAuthorizeFailure() { HttpServletRequest httpServletRequest = Mockito.mock(HttpServletRequest.class); HttpServletResponse httpServletResponse = Mockito.mock(HttpServletResponse.class); Authorizer authorizer = Mockito.mock(Authorizer.class); Http.AuthorityList authorities = new Http.AuthorityList(); ResourceContext context = new ResourceContext(httpServletRequest, httpServletResponse, authorities, authorizer); try { context.authorize("action", "resource", "domain"); } catch (ResourceException expected) { assertEquals(expected.getCode(), 401); } }
@Test public void testAuthorize() { HttpServletRequest httpServletRequest = Mockito.mock(HttpServletRequest.class); HttpServletResponse httpServletResponse = Mockito.mock(HttpServletResponse.class); Principal principal = Mockito.mock(Principal.class); Authorizer authorizer = Mockito.mock(Authorizer.class); Mockito.when(authorizer.access(ArgumentMatchers.anyString(), ArgumentMatchers.anyString(), ArgumentMatchers.any(Principal.class), ArgumentMatchers.anyString())).thenReturn(true); Authority authority = Mockito.mock(Authority.class); Mockito.when(authority.getCredSource()).thenReturn(Authority.CredSource.HEADER); Mockito.when(authority.getHeader()).thenReturn("Athenz-Principal-Auth"); Mockito.when(httpServletRequest.getHeader("Athenz-Principal-Auth")).thenReturn("Creds"); Mockito.when(authority.authenticate(ArgumentMatchers.any(), ArgumentMatchers.any(), ArgumentMatchers.any(), ArgumentMatchers.any())).thenReturn(principal); Http.AuthorityList authorities = new Http.AuthorityList(); authorities.add(authority); ResourceContext context = new ResourceContext(httpServletRequest, httpServletResponse, authorities, authorizer); context.authorize("action", "resource", "domain"); assertTrue(true); }