SecurityContextUtil { public static boolean hasRole(String role) { SecurityContext sc = SecurityContextUtil.getSC(); return sc != null && sc.isUserInRole(role); }  static SecurityContext getSC(); static void assertSC(); static void assertLoggedIn(); static String getUser(); static IUser getUserObject(); static boolean hasRole(String role); static String requestId(); static boolean isLoggedIn();  }
@Test public void hasRoleWithNullContext() throws Exception { PowerMockito.spy(SecurityContextUtil.class); PowerMockito.doReturn(null).when(SecurityContextUtil.class, "getSC"); Assert.assertFalse(SecurityContextUtil.hasRole("foo")); }
@Test public void hasRoleWithRolePresent() throws Exception { SecurityContext securityContext = PowerMockito.mock(SecurityContext.class); PowerMockito.when(securityContext.isUserInRole("foo")).thenReturn(true); PowerMockito.spy(SecurityContextUtil.class); PowerMockito.doReturn(securityContext).when(SecurityContextUtil.class, "getSC"); Assert.assertTrue(SecurityContextUtil.hasRole("foo")); }
@Test public void hasRoleWithRoleAbsent() throws Exception { SecurityContext securityContext = PowerMockito.mock(SecurityContext.class); PowerMockito.when(securityContext.isUserInRole("foo")).thenReturn(false); PowerMockito.spy(SecurityContextUtil.class); PowerMockito.doReturn(securityContext).when(SecurityContextUtil.class, "getSC"); Assert.assertFalse(SecurityContextUtil.hasRole("foo")); }