JobResource extends ResourceBase { @Path("/register") @POST @Consumes(MediaType.APPLICATION_JSON) public Account doRegisterJob(PerformedTransaction performedJob) { String username = performedJob.getAccount().getUsername(); 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(); } return ukelonn.registerPerformedJob(performedJob); }  @Path("/register") @POST @Consumes(MediaType.APPLICATION_JSON) Account doRegisterJob(PerformedTransaction performedJob); @Path("/update") @POST @Consumes(MediaType.APPLICATION_JSON) List<Transaction> doUpdateJob(UpdatedTransaction editedJob);  }
@Test public void testRegisterJob() throws Exception { Account account = getJadAccount(); double originalBalance = account.getBalance(); List<TransactionType> jobTypes = getJobtypes(); PerformedTransaction job = new PerformedTransaction(account, jobTypes.get(0).getId(), jobTypes.get(0).getTransactionAmount(), new Date()); HttpServletRequest request = mock(HttpServletRequest.class); HttpSession session = mock(HttpSession.class); when(request.getSession()).thenReturn(session); HttpServletResponse response = mock(HttpServletResponse.class); JobResource resource = new JobResource(); loginUser(request, response, "jad", "1ad"); MockLogService logservice = new MockLogService(); resource.logservice = logservice; UkelonnService ukelonn = mock(UkelonnService.class); Account accountWithUpdatedBalance = copyAccount(account); accountWithUpdatedBalance.setBalance(account.getBalance() + job.getTransactionAmount()); when(ukelonn.registerPerformedJob(any())).thenReturn(accountWithUpdatedBalance); resource.ukelonn = ukelonn; Account result = resource.doRegisterJob(job); assertEquals("jad", result.getUsername()); assertThat(result.getBalance()).isGreaterThan(originalBalance); }
@Test(expected=ForbiddenException.class) public void testRegisterJobOtherUsername() throws Exception { Account account = getJodAccount(); List<TransactionType> jobTypes = getJobtypes(); PerformedTransaction job = new PerformedTransaction(account, jobTypes.get(0).getId(), jobTypes.get(0).getTransactionAmount(), new Date()); 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"); JobResource resource = new JobResource(); MockLogService logservice = new MockLogService(); resource.logservice = logservice; UkelonnService ukelonn = mock(UkelonnService.class); resource.ukelonn = ukelonn; resource.doRegisterJob(job); }
@Test public void testRegisterJobtWhenLoggedInAsAdministrator() throws Exception { Account account = getJadAccount(); double originalBalance = account.getBalance(); List<TransactionType> jobTypes = getJobtypes(); PerformedTransaction job = new PerformedTransaction(account, jobTypes.get(0).getId(), jobTypes.get(0).getTransactionAmount(), new Date()); 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"); JobResource resource = new JobResource(); MockLogService logservice = new MockLogService(); resource.logservice = logservice; UkelonnService ukelonn = mock(UkelonnService.class); Account accountWithUpdatedBalance = copyAccount(account); accountWithUpdatedBalance.setBalance(account.getBalance() + job.getTransactionAmount()); when(ukelonn.registerPerformedJob(any())).thenReturn(accountWithUpdatedBalance); resource.ukelonn = ukelonn; Account result = resource.doRegisterJob(job); assertEquals("jad", result.getUsername()); assertThat(result.getBalance()).isGreaterThan(originalBalance); }
@Test(expected=ForbiddenException.class) public void testRegisterJobNoUsername() throws Exception { Account account = new Account(); List<TransactionType> jobTypes = getJobtypes(); PerformedTransaction job = new PerformedTransaction(account, jobTypes.get(0).getId(), jobTypes.get(0).getTransactionAmount(), new Date()); 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"); JobResource resource = new JobResource(); MockLogService logservice = new MockLogService(); resource.logservice = logservice; UkelonnService ukelonn = mock(UkelonnService.class); resource.ukelonn = ukelonn; resource.doRegisterJob(job); }
@Test(expected=InternalServerErrorException.class) public void testRegisterJobInternalServerError() throws Exception { Account account = new Account(); List<TransactionType> jobTypes = getJobtypes(); PerformedTransaction job = new PerformedTransaction(account, jobTypes.get(0).getId(), jobTypes.get(0).getTransactionAmount(), new Date()); JobResource resource = new JobResource(); MockLogService logservice = new MockLogService(); resource.logservice = logservice; UkelonnService ukelonn = mock(UkelonnService.class); resource.ukelonn = ukelonn; ThreadContext.remove(); resource.doRegisterJob(job); }