O2AuthorizationFilter implements ContainerRequestFilter { @Override public void filter(final ContainerRequestContext requestContext) { SecurityContext context = requestContext.getSecurityContext(); O2Principal principal = ObjectUtil .safeCast(context.getUserPrincipal(), O2Principal.class) .orElse(null); if (principal == null) { throw new AccessDeniedException(); } if (principal.getContext() == null) { throw new AccessDeniedException(); } } O2AuthorizationFilter(); @Override void filter(final ContainerRequestContext requestContext);  }
@Test(expected = AccessDeniedException.class) public void assertFailWithWrongPrincipalType() { O2AuthorizationFilter filter = new O2AuthorizationFilter(); Principal principal = () -> "wrong type"; doReturn(principal) .when(securityContext) .getUserPrincipal(); filter.filter(requestContext); }
@Test(expected = AccessDeniedException.class) public void assertFailWithNoPrincipal() { O2AuthorizationFilter filter = new O2AuthorizationFilter(); doReturn(null) .when(securityContext) .getUserPrincipal(); filter.filter(requestContext); }
@Test(expected = AccessDeniedException.class) public void assertFailWithNoClient() { O2AuthorizationFilter filter = new O2AuthorizationFilter(); O2Principal p = new O2Principal(); doReturn(p) .when(securityContext) .getUserPrincipal(); filter.filter(requestContext); }
@Test public void assertPassWithPublicClient() { O2AuthorizationFilter filter = new O2AuthorizationFilter(); Client c = new Client(); c.setId(IdUtil.next()); O2Principal p = new O2Principal(c); doReturn(p) .when(securityContext) .getUserPrincipal(); filter.filter(requestContext); }