AccountResource extends ResourceBase { @GET @Path("{username}") public Account getAccount(@PathParam("username") String username) { if (username == null) { logservice.log(LogService.LOG_WARNING, USERNAME_MISSING_ERROR); throw new BadRequestException(USERNAME_MISSING_ERROR); } if (!isCurrentUserOrAdmin(username, logservice)) { logservice.log(LogService.LOG_WARNING, String.format("REST endpoint /ukelonn/api/account logged in user not allowed to fetch account for username %s", username)); throw new ForbiddenException(); } try { return ukelonn.getAccount(username); } catch (UkelonnException e) { logservice.log(LogService.LOG_ERROR, String.format("Internal Server Error in REST endpoint /ukelonn/api/account: %s", e.getMessage()), e); throw new InternalServerErrorException("Failed to get account from database. See log for details."); } }  @GET @Path("{username}") Account getAccount(@PathParam("username") String username);  }
@Test public void testGetAccount() throws Exception { HttpServletRequest request = mock(HttpServletRequest.class); HttpSession session = mock(HttpSession.class); when(request.getSession()).thenReturn(session); HttpServletResponse response = mock(HttpServletResponse.class); AccountResource resource = new AccountResource(); loginUser(request, response, "jad", "1ad"); MockLogService logservice = new MockLogService(); resource.logservice = logservice; UkelonnService ukelonn = mock(UkelonnService.class); when(ukelonn.getAccount(anyString())).thenReturn(getJadAccount()); resource.ukelonn = ukelonn; Account result = resource.getAccount("jad"); assertEquals("jad", result.getUsername()); assertEquals(673.0, result.getBalance(), 0.0); }
@Test public void testGetAccountOtherUsername() throws Exception { HttpServletRequest request = mock(HttpServletRequest.class); HttpSession session = mock(HttpSession.class); when(request.getSession()).thenReturn(session); HttpServletResponse response = mock(HttpServletResponse.class); loginUser(request, response, "jad", "1ad"); AccountResource resource = new AccountResource(); MockLogService logservice = new MockLogService(); resource.logservice = logservice; UkelonnService ukelonn = mock(UkelonnService.class); resource.ukelonn = ukelonn; assertThrows(ForbiddenException.class, () -> { resource.getAccount("jod"); }); }
@Test public void testGetAccountWhenLoggedInAsAdministrator() throws Exception { HttpServletRequest request = mock(HttpServletRequest.class); HttpSession session = mock(HttpSession.class); when(request.getSession()).thenReturn(session); HttpServletResponse response = mock(HttpServletResponse.class); loginUser(request, response, "admin", "admin"); AccountResource resource = new AccountResource(); MockLogService logservice = new MockLogService(); resource.logservice = logservice; UkelonnService ukelonn = mock(UkelonnService.class); when(ukelonn.getAccount(anyString())).thenReturn(getJadAccount()); resource.ukelonn = ukelonn; Account result = resource.getAccount("jad"); assertEquals("jad", result.getUsername()); assertEquals(673.0, result.getBalance(), 0.0); }
@Test public void testGetAccountNoUsername() throws Exception { HttpServletRequest request = mock(HttpServletRequest.class); HttpSession session = mock(HttpSession.class); when(request.getSession()).thenReturn(session); AccountResource resource = new AccountResource(); MockLogService logservice = new MockLogService(); resource.logservice = logservice; UkelonnService ukelonn = mock(UkelonnService.class); resource.ukelonn = ukelonn; assertThrows(BadRequestException.class, () -> { resource.getAccount(null); }); }
@SuppressWarnings("unchecked") @Test public void testGetAccountUsernameNotPresentInDatabase() throws Exception { HttpServletRequest request = mock(HttpServletRequest.class); HttpSession session = mock(HttpSession.class); when(request.getSession()).thenReturn(session); HttpServletResponse response = mock(HttpServletResponse.class); loginUser(request, response, "admin", "admin"); AccountResource resource = new AccountResource(); MockLogService logservice = new MockLogService(); resource.logservice = logservice; UkelonnService ukelonn = mock(UkelonnService.class); when(ukelonn.getAccount(anyString())).thenThrow(UkelonnException.class); resource.ukelonn = ukelonn; assertThrows(InternalServerErrorException.class, () -> { resource.getAccount("on"); }); }
@Test public void testGetAccountWhenSubjectHasNullPrincipal() { createSubjectWithNullPrincipalAndBindItToThread(); AccountResource resource = new AccountResource(); MockLogService logservice = new MockLogService(); resource.logservice = logservice; UkelonnService ukelonn = mock(UkelonnService.class); resource.ukelonn = ukelonn; assertThrows(InternalServerErrorException.class, () -> { resource.getAccount("on"); }); }