PersonProvider implements Provider<Person> { @Override public Person provide() { HttpServletRequest request = RollbarRequestListener.getServletRequest(); if (request != null && request.getUserPrincipal() != null) { return new Person.Builder() .id(request.getUserPrincipal().getName()) .build(); } return null; }  @Override Person provide();  }
@Test public void shouldRetrieveThePersonIfPrincipalPresent() { Principal principal = mock(Principal.class); when(principal.getName()).thenReturn(PRINCIPAL_NAME); when(request.getUserPrincipal()).thenReturn(principal); Person result = sut.provide(); Person expected = new Person.Builder().id(PRINCIPAL_NAME).build(); assertThat(result, is(expected)); }
@Test public void shouldRetrieveNullIfPrincipalNotPresent() { when(request.getUserPrincipal()).thenReturn(null); Person result = sut.provide(); assertNull(result); }