PcapServiceImpl implements PcapService { @Override public PcapStatus getJobStatus(String username, String jobId) throws RestException { PcapStatus pcapStatus = null; try { Statusable<Path> statusable = jobManager.getJob(username, jobId); if (statusable != null) { pcapStatus = statusableToPcapStatus(statusable); } } catch (JobNotFoundException | InterruptedException e) { LOG.warn(String.format("Could not get job status. Job not found for user %s with job id %s", username, jobId)); } catch (JobException e) { throw new RestException(e); } return pcapStatus; } @Autowired  PcapServiceImpl(Environment environment, Configuration configuration, PcapJobSupplier pcapJobSupplier, JobManager<Path> jobManager, PcapToPdmlScriptWrapper pcapToPdmlScriptWrapper); @Override PcapStatus submit(String username, PcapRequest pcapRequest); @Override PcapStatus getJobStatus(String username, String jobId); @Override List<PcapStatus> getJobStatus(String username, JobStatus.State state); @Override PcapStatus killJob(String username, String jobId); @Override Path getPath(String username, String jobId, Integer page); @Override Pdml getPdml(String username, String jobId, Integer page); @Override InputStream getRawPcap(String username, String jobId, Integer page); @Override Map<String, Object> getConfiguration(String username, String jobId);  }
@Test public void getStatusShouldProperlyReturnStatus() throws Exception { MockPcapJob mockPcapJob = mock(MockPcapJob.class); JobManager jobManager = mock(JobManager.class); JobStatus actualJobStatus = new JobStatus() .withJobId("jobId") .withState(JobStatus.State.SUCCEEDED) .withDescription("description") .withPercentComplete(100.0); Pageable pageable = mock(Pageable.class); when(pageable.getSize()).thenReturn(2); when(mockPcapJob.getStatus()).thenReturn(actualJobStatus); when(mockPcapJob.isDone()).thenReturn(true); when(mockPcapJob.get()).thenReturn(pageable); when(jobManager.getJob("user", "jobId")).thenReturn(mockPcapJob); PcapServiceImpl pcapService = new PcapServiceImpl(environment, configuration, mockPcapJobSupplier, jobManager, pcapToPdmlScriptWrapper); PcapStatus expectedPcapStatus = new PcapStatus(); expectedPcapStatus.setJobId("jobId"); expectedPcapStatus.setJobStatus(JobStatus.State.SUCCEEDED.name()); expectedPcapStatus.setDescription("description"); expectedPcapStatus.setPercentComplete(100.0); expectedPcapStatus.setPageTotal(2); assertEquals(expectedPcapStatus, pcapService.getJobStatus("user", "jobId")); }
@Test public void getStatusShouldReturnNullOnMissingStatus() throws Exception { JobManager jobManager = new InMemoryJobManager(); PcapServiceImpl pcapService = new PcapServiceImpl(environment, configuration, new PcapJobSupplier(), jobManager, pcapToPdmlScriptWrapper); assertNull(pcapService.getJobStatus("user", "jobId")); }
@Test public void getStatusShouldThrowRestException() throws Exception { JobManager jobManager = mock(JobManager.class); when(jobManager.getJob("user", "jobId")).thenThrow(new JobException("some job exception")); PcapServiceImpl pcapService = new PcapServiceImpl(environment, configuration, new PcapJobSupplier(), jobManager, pcapToPdmlScriptWrapper); RestException e = assertThrows(RestException.class, () -> pcapService.getJobStatus("user", "jobId")); assertEquals("some job exception", e.getMessage()); }
@Test public void getStatusForStateShouldProperlyReturnJobs() throws Exception { MockPcapJob mockPcapJob = mock(MockPcapJob.class); JobManager jobManager = mock(JobManager.class); Statusable<Path> runningJob = mock(Statusable.class); JobStatus runningStatus = mock(JobStatus.class); when(runningStatus.getJobId()).thenReturn("runningJob"); when(runningStatus.getState()).thenReturn(JobStatus.State.RUNNING); when(runningJob.getStatus()).thenReturn(runningStatus); Statusable<Path> failedJob = mock(Statusable.class); when(failedJob.getStatus()).thenThrow(new JobException("job exception")); Statusable<Path> succeededJob = mock(Statusable.class); JobStatus succeededStatus = mock(JobStatus.class); when(succeededStatus.getJobId()).thenReturn("succeededJob"); when(succeededStatus.getState()).thenReturn(JobStatus.State.SUCCEEDED); when(succeededJob.isDone()).thenReturn(true); when(succeededJob.getStatus()).thenReturn(succeededStatus); Pageable<Path> succeededPageable = mock(Pageable.class); when(succeededPageable.getSize()).thenReturn(5); when(succeededJob.get()).thenReturn(succeededPageable); when(jobManager.getJobs("user")).thenReturn(Arrays.asList(runningJob, failedJob, succeededJob)); PcapServiceImpl pcapService = new PcapServiceImpl(environment, configuration, mockPcapJobSupplier, jobManager, pcapToPdmlScriptWrapper); PcapStatus expectedRunningPcapStatus = new PcapStatus(); expectedRunningPcapStatus.setJobId("runningJob"); expectedRunningPcapStatus.setJobStatus(JobStatus.State.RUNNING.name()); assertEquals(expectedRunningPcapStatus, pcapService.getJobStatus("user", JobStatus.State.RUNNING).get(0)); PcapStatus expectedFailedPcapStatus = new PcapStatus(); expectedFailedPcapStatus.setJobStatus(JobStatus.State.FAILED.name()); expectedFailedPcapStatus.setDescription("job exception"); assertEquals(expectedFailedPcapStatus, pcapService.getJobStatus("user", JobStatus.State.FAILED).get(0)); PcapStatus expectedSucceededPcapStatus = new PcapStatus(); expectedSucceededPcapStatus.setJobId("succeededJob"); expectedSucceededPcapStatus.setJobStatus(JobStatus.State.SUCCEEDED.name()); expectedSucceededPcapStatus.setPageTotal(5); assertEquals(expectedSucceededPcapStatus, pcapService.getJobStatus("user", JobStatus.State.SUCCEEDED).get(0)); }