DashboardSecureProvider implements SecureProvider { @Override public void checkAccess(InvocationContext ctx, String[] roles) throws IllegalAccessException { boolean allowed = rcm.getRoles().isEmpty(); for (String role : rcm.getRoles()) { allowed |= context.isUserInRole(role); if(allowed) { break; } } if(!allowed) { throw new IllegalAccessException(context.getPrincipal().getName()+" no access to "+ctx); } }  @Override void checkAccess(InvocationContext ctx, String[] roles);  }
@Test(expected = IllegalAccessException.class) public void testCheckAccessFailed() throws IllegalAccessException { System.out.println("checkAccess"); InvocationContext ctx = mock(InvocationContext.class); Principal principal = mock(Principal.class); String[] roles = null; when(context.isUserInRole(eq("ROLE"))).thenReturn(Boolean.FALSE); when(context.getPrincipal()).thenReturn(principal); when(rcm.getRoles()).thenReturn(Arrays.asList("ROLE")); instance.checkAccess(ctx, roles); }
@Test public void checkAccessTest() throws IllegalAccessException { System.out.println("checkAccess"); InvocationContext ctx = mock(InvocationContext.class); Principal principal = mock(Principal.class); String[] roles = null; when(context.isUserInRole(eq("ROLE"))).thenReturn(Boolean.TRUE); when(context.getPrincipal()).thenReturn(principal); when(rcm.getRoles()).thenReturn(Arrays.asList("ROLE")); instance.checkAccess(ctx, roles); }