SchedulerResource implements ISchedulerResource { @Override public Response saveJob( String jobId, String jobName, String appName, String cmdName, String cron ) { this.logger.fine( "Request: save a new scheduled job as " + jobName + "." ); Response result; try { jobId = this.scheduler.saveJob( jobId, jobName, cmdName, cron, appName ); result = Response.ok( new StringWrapper( jobId )).build(); } catch( IllegalArgumentException e ) { result = handleError( Status.NOT_FOUND, new RestError( ErrorCode.REST_SAVE_ERROR, e, ErrorDetails.name( jobName )), lang( this.manager )).build(); } catch( IOException e ) { result = handleError( Status.BAD_REQUEST, new RestError( ErrorCode.REST_SAVE_ERROR, e, ErrorDetails.name( jobName )), lang( this.manager )).build(); } catch( NullPointerException e ) { result = handleError( Status.FORBIDDEN, new RestError( ErrorCode.REST_SCHEDULER_IS_UNAVAILABLE, e, ErrorDetails.name( jobName )), lang( this.manager )).build(); } return result; } SchedulerResource( Manager manager ); @Override Response saveJob( String jobId, String jobName, String appName, String cmdName, String cron ); @Override Response deleteJob( String jobName ); @Override Response findJobProperties( String jobName ); @Override List<ScheduledJob> listJobs( String appName, String cmdName ); void setScheduler( IScheduler scheduler );  }
@Test public void testSaveJob_inexistingApplicationOrCommand() throws Exception { Mockito .doThrow( new IllegalArgumentException( "For test" )) .when( this.scheduler ).saveJob( Mockito.anyString(), Mockito.anyString(), Mockito.anyString(), Mockito.anyString(), Mockito.anyString()); Response resp = this.resource.saveJob( null, "job", "app", "cmd", "cron" ); Assert.assertEquals( Status.NOT_FOUND.getStatusCode(), resp.getStatus()); }
@Test public void testSaveJob_ok() throws Exception { Mockito .when( this.scheduler.saveJob( Mockito.anyString(), Mockito.anyString(), Mockito.anyString(), Mockito.anyString(), Mockito.anyString())) .thenReturn( "some-id" ); Response resp = this.resource.saveJob( null, "job", "app", "cmd", "cron" ); Assert.assertEquals( Status.OK.getStatusCode(), resp.getStatus()); Assert.assertTrue( resp.getEntity() instanceof StringWrapper ); Assert.assertEquals( "some-id", ((StringWrapper) resp.getEntity()).toString()); }
@Test public void testSaveJob_error() throws Exception { Mockito .doThrow( new IOException( "For test" )) .when( this.scheduler ).saveJob( Mockito.anyString(), Mockito.anyString(), Mockito.anyString(), Mockito.anyString(), Mockito.anyString()); Response resp = this.resource.saveJob( null, "job", "app", "cmd", "cron" ); Assert.assertEquals( Status.BAD_REQUEST.getStatusCode(), resp.getStatus()); }
@Test public void testSaveJob_noScheduler() { this.resource.scheduler = null; Response resp = this.resource.saveJob( null, "job", "app", "cmd", "cron" ); Assert.assertEquals( Status.FORBIDDEN.getStatusCode(), resp.getStatus()); }