### Question:
RsrcCtxWrapper implements ResourceContext { @Override public void authorize(String action, String resource, String trustedDomain) { try { ctx.authorize(action, resource, trustedDomain); } catch (com.yahoo.athenz.common.server.rest.ResourceException restExc) { logPrincipal(ctx.principal()); throwZtsException(restExc); } } RsrcCtxWrapper(HttpServletRequest request, HttpServletResponse response,
            Http.AuthorityList authList,  boolean optionalAuth, Authorizer authorizer,
            Metric metric, Object timerMetric, String apiName); com.yahoo.athenz.common.server.rest.ResourceContext context(); Principal principal(); String getRequestDomain(); Object getTimerMetric(); void setRequestDomain(String requestDomain); @Override HttpServletRequest request(); @Override HttpServletResponse response(); @Override String getApiName(); @Override String getHttpMethod(); @Override void authenticate(); @Override void authorize(String action, String resource, String trustedDomain); void logPrincipal(final Principal principal); void logPrincipal(final String principal); void logAuthorityId(Authority authority); void throwZtsException(com.yahoo.athenz.common.server.rest.ResourceException restExc);  }### Answer:
@Test public void testAuthorize() { HttpServletRequest reqMock = Mockito.mock(HttpServletRequest.class); HttpServletResponse resMock = Mockito.mock(HttpServletResponse.class); AuthorityList authListMock = new AuthorityList(); Authorizer authorizerMock = Mockito.mock(Authorizer.class); Authority authMock = Mockito.mock(Authority.class); Metric metricMock = Mockito.mock(Metric.class); Object timerMetricMock = Mockito.mock(Object.class); Principal prin = Mockito.mock(Principal.class); Mockito.when(authMock.getHeader()).thenReturn("testheader"); Mockito.when(reqMock.getHeader("testheader")).thenReturn("testcred"); Mockito.when(authMock.getCredSource()).thenReturn(com.yahoo.athenz.auth.Authority.CredSource.HEADER); Mockito.when(authMock.authenticate(Mockito.any(), Mockito.any(), Mockito.any(), Mockito.any())) .thenReturn(prin); Mockito.when(reqMock.getRemoteAddr()).thenReturn("1.1.1.1"); Mockito.when(reqMock.getMethod()).thenReturn("POST"); authListMock.add(authMock); Mockito.when(authorizerMock.access(Mockito.any(), Mockito.any(), Mockito.any(), Mockito.any())) .thenReturn(true); RsrcCtxWrapper wrapper = new RsrcCtxWrapper( reqMock, resMock, authListMock, false, authorizerMock, metricMock, timerMetricMock, "apiName"); wrapper.authorize("add-domain", "test", "test"); assertEquals(wrapper.principal(), prin); }
@Test(expectedExceptions = { ResourceException.class }) public void testAuthorizeInvalid() { HttpServletRequest reqMock = Mockito.mock(HttpServletRequest.class); HttpServletResponse resMock = Mockito.mock(HttpServletResponse.class); AuthorityList authListMock = new AuthorityList(); Authorizer authorizerMock = Mockito.mock(Authorizer.class); Metric metricMock = Mockito.mock(Metric.class); Object timerMetricMock = Mockito.mock(Object.class); Mockito.when(reqMock.getHeader("testheader")).thenReturn("testcred"); Mockito.when(reqMock.getRemoteAddr()).thenReturn("1.1.1.1"); Mockito.when(reqMock.getMethod()).thenReturn("POST"); Mockito.when(authorizerMock.access(Mockito.any(), Mockito.any(), Mockito.any(), Mockito.any())) .thenReturn(true); RsrcCtxWrapper wrapper = new RsrcCtxWrapper( reqMock, resMock, authListMock, false, authorizerMock, metricMock, timerMetricMock, "apiName"); wrapper.authorize("add-domain", "test", "test"); }