### Question:
UserService { @GET @Path( "/{username}" ) public Response getUser( @PathParam( "username" ) String username, @Context HttpServletRequest req ) { Principal principal = req.getUserPrincipal(); if ( principal == null || !principal.getName().equals( username ) ) { return output.notFound(); } LoginContext loginContext = getLoginContextFromUserPrincipal( principal ); UserManager userManager = userManagerSupplier.getUserManager( loginContext.subject(), false ); try { User user = userManager.getUser( username ); return output.ok( new AuthorizationRepresentation( user ) ); } catch ( InvalidArgumentsException e ) { return output.notFound(); } } UserService( @Context UserManagerSupplier userManagerSupplier, @Context InputFormat input, @Context OutputFormat
            output ); @GET @Path( "/{username}" ) Response getUser( @PathParam( "username" ) String username, @Context HttpServletRequest req ); @POST @Path( "/{username}/password" ) Response setPassword( @PathParam( "username" ) String username, @Context HttpServletRequest req,
            String payload ); static final String PASSWORD; }### Answer:
@Test public void shouldReturnValidUserRepresentation() throws Exception { when( request.getUserPrincipal() ).thenReturn( neo4jPrinciple ); OutputFormat outputFormat = new EntityOutputFormat( new JsonFormat(), new URI( "http: UserService userService = new UserService( userManagerSupplier, new JsonFormat(), outputFormat ); Response response = userService.getUser( "neo4j", request ); assertThat( response.getStatus(), equalTo( 200 ) ); String json = new String( (byte[]) response.getEntity() ); assertNotNull( json ); assertThat( json, containsString( "\"username\" : \"neo4j\"" ) ); assertThat( json, containsString( "\"password_change\" : \"http: assertThat( json, containsString( "\"password_change_required\" : true" ) ); }
@Test public void shouldReturn404WhenRequestingUserIfNotAuthenticated() throws Exception { when( request.getUserPrincipal() ).thenReturn( null ); OutputFormat outputFormat = new EntityOutputFormat( new JsonFormat(), new URI( "http: UserService userService = new UserService( userManagerSupplier, new JsonFormat(), outputFormat ); Response response = userService.getUser( "neo4j", request ); assertThat( response.getStatus(), equalTo( 404 ) ); }
@Test public void shouldReturn404WhenRequestingUserIfDifferentUser() throws Exception { when( request.getUserPrincipal() ).thenReturn( neo4jPrinciple ); OutputFormat outputFormat = new EntityOutputFormat( new JsonFormat(), new URI( "http: UserService userService = new UserService( mock( BasicAuthManager.class ), new JsonFormat(), outputFormat ); Response response = userService.getUser( "fred", request ); assertThat( response.getStatus(), equalTo( 404 ) ); }
@Test public void shouldReturn404WhenRequestingUserIfUnknownUser() throws Exception { when( request.getUserPrincipal() ).thenReturn( neo4jPrinciple ); userManagerSupplier.getUserManager().deleteUser( "neo4j" ); OutputFormat outputFormat = new EntityOutputFormat( new JsonFormat(), new URI( "http: UserService userService = new UserService( userManagerSupplier, new JsonFormat(), outputFormat ); Response response = userService.getUser( "neo4j", request ); assertThat( response.getStatus(), equalTo( 404 ) ); }