PrincipalTools { public Principal getPrincipal(HandshakeRequest handshakeRequest) { if (null != handshakeRequest && handshakeRequest.getUserPrincipal() != null) { return handshakeRequest.getUserPrincipal(); } else { return ANONYMOUS; } }  Principal getPrincipal(HandshakeRequest handshakeRequest); Principal getPrincipal(Session session);  }
@Test public void testGetPrincipal_HandshakeRequest() { System.out.println("getPrincipal"); HandshakeRequest handshakeRequest = mock(HandshakeRequest.class); Principal principal = mock(Principal.class); when(principal.getName()).thenReturn("FOO"); when(handshakeRequest.getUserPrincipal()).thenReturn(null).thenReturn(principal); Principal result = instance.getPrincipal(handshakeRequest); assertThat(result).isNotNull(); assertThat(result.getName()).isEqualTo(Constants.ANONYMOUS); result = instance.getPrincipal(handshakeRequest); assertThat(result).isEqualTo(principal); assertThat(result.getName()).isEqualTo("FOO"); handshakeRequest = null; result = instance.getPrincipal(handshakeRequest); assertThat(result).isNotNull(); assertThat(result.getName()).isEqualTo(Constants.ANONYMOUS); }
@Test public void testGetPrincipal_Session() { System.out.println("getPrincipal"); Session session = mock(Session.class); Principal principal = mock(Principal.class); when(principal.getName()).thenReturn("FOO"); when(session.getUserPrincipal()).thenReturn(null).thenReturn(principal); Principal result = instance.getPrincipal(session); assertThat(result).isNotNull(); assertThat(result.getName()).isEqualTo(Constants.ANONYMOUS); result = instance.getPrincipal(session); assertThat(result).isEqualTo(principal); assertThat(result.getName()).isEqualTo("FOO"); session = null; result = instance.getPrincipal(session); assertThat(result).isNotNull(); assertThat(result.getName()).isEqualTo(Constants.ANONYMOUS); }