SecurityContextUtil { public static void assertSC() { SecurityContext sc = SecurityContextUtil.getSC(); if ((sc == null) || (sc.getUserPrincipal() == null)) { throw new NotAuthorizedException(Response.status(Status.UNAUTHORIZED).entity("Invalid credentials or session").build()); } }  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 assertSCWithNullContext() throws Exception { try { PowerMockito.spy(SecurityContextUtil.class); PowerMockito.doReturn(null).when(SecurityContextUtil.class, "getSC"); SecurityContextUtil.assertSC(); Assert.fail("Should not get past assertSC"); } catch (Exception e) { Assert.assertEquals(NotAuthorizedException.class, e.getClass()); } }
@Test public void assertSCWithNullPrincipal() throws Exception { try { SecurityContext securityContext = PowerMockito.mock(SecurityContext.class); PowerMockito.when(securityContext.getUserPrincipal()).thenReturn(null); PowerMockito.spy(SecurityContextUtil.class); PowerMockito.doReturn(securityContext).when(SecurityContextUtil.class, "getSC"); SecurityContextUtil.assertSC(); Assert.fail("Should not get past assertSC"); } catch (Exception e) { Assert.assertEquals(NotAuthorizedException.class, e.getClass()); } }
@Test public void assertSCWithSetPrincipal() throws Exception { try { SecurityContext securityContext = PowerMockito.mock(SecurityContext.class); PowerMockito.when(securityContext.getUserPrincipal()).thenReturn(new SimplePrincipal("foobar")); PowerMockito.spy(SecurityContextUtil.class); PowerMockito.doReturn(securityContext).when(SecurityContextUtil.class, "getSC"); SecurityContextUtil.assertSC(); } catch (Exception e) { Assert.fail("Should not fail on assertSC"); } }