CronTaskController extends BaseController { @ApiOperation(value = "Used to upload groovy script for groovy cron task") @ApiResponses(value = { @ApiResponse(code = 200, message = SUCCESSFUL_UPLOAD_GROOVY_SCRIPT), @ApiResponse(code = 400, message = FAILED_UPLOAD_GROOVY_SCRIPT) }) @PutMapping(value = "/cron/groovy/{UUID}", produces = { MediaType.TEXT_PLAIN_VALUE, MediaType.APPLICATION_JSON_VALUE }) public ResponseEntity uploadGroovyScript(@PathVariable("UUID") UUID uuid, HttpServletRequest request, @RequestHeader(HttpHeaders.ACCEPT) String acceptHeader) { final String fileName = request.getHeader(CRON_CONFIG_FILE_NAME_KEY); if (!fileName.endsWith(".groovy")) { return getBadRequestResponseEntity("The uploaded file must be a Groovy one!", acceptHeader); } CronTaskConfigurationDto cronTaskConfiguration = cronTaskConfigurationService.getTaskConfigurationDto(uuid); if (cronTaskConfiguration == null) { return getNotFoundResponseEntity(NOT_FOUND_CONFIGURATION, acceptHeader); } logger.info(">> CRON UUID: {}", cronTaskConfiguration.getUuid()); logger.info(">> CRON NAME: {}", cronTaskConfiguration.getName()); logger.info(">> Properties: {}", cronTaskConfiguration.getProperties()); String path = propertiesBooter.getVaultDirectory() + "/etc/conf/cron/groovy"; cronTaskConfiguration.addProperty(CRON_CONFIG_FILE_NAME_KEY, fileName); cronTaskConfiguration.setJobClass(GroovyCronJob.class.getName()); cronTaskConfiguration.addProperty(CRON_CONFIG_SCRIPT_PATH_KEY, path + "/" + fileName); try { storeGroovyCronTask(request.getInputStream(), path, fileName); cronTaskConfigurationService.saveConfiguration(cronTaskConfiguration); } catch (Exception e) { return getExceptionResponseEntity(HttpStatus.INTERNAL_SERVER_ERROR, FAILED_UPLOAD_GROOVY_SCRIPT, e, acceptHeader); } return getSuccessfulResponseEntity(SUCCESSFUL_UPLOAD_GROOVY_SCRIPT, acceptHeader); }  @ApiOperation(value = "Used to save a new cron task job") @ApiResponses(value = { @ApiResponse(code = 200, message = SUCCESSFUL_SAVE_CONFIGURATION), @ApiResponse(code = 400, message = FAILED_SAVE_CONFIGURATION) }) @PutMapping(consumes = MediaType.APPLICATION_JSON_VALUE, produces = { MediaType.TEXT_PLAIN_VALUE, MediaType.APPLICATION_JSON_VALUE }) ResponseEntity create(@RequestBody @Validated CronTaskConfigurationForm cronTaskConfigurationForm,
                                 BindingResult bindingResult,
                                 @RequestHeader(HttpHeaders.ACCEPT) String acceptHeader); @ApiOperation(value = "Used to update an existing configuration") @ApiResponses(value = { @ApiResponse(code = 200, message = SUCCESSFUL_UPDATE_CONFIGURATION), @ApiResponse(code = 400, message = FAILED_UPDATE_CONFIGURATION) }) @PutMapping(value = "/{UUID}", consumes = MediaType.APPLICATION_JSON_VALUE, produces = { MediaType.TEXT_PLAIN_VALUE, MediaType.APPLICATION_JSON_VALUE }) ResponseEntity updateConfiguration(@PathVariable("UUID") UUID uuid,
                                              @RequestBody @Validated CronTaskConfigurationForm cronTaskConfigurationForm,
                                              BindingResult bindingResult,
                                              @RequestHeader(HttpHeaders.ACCEPT) String acceptHeader); @ApiOperation(value = "Used to delete the configuration") @ApiResponses(value = { @ApiResponse(code = 200, message = SUCCESSFUL_DELETE_CONFIGURATION), @ApiResponse(code = 400, message = FAILED_DELETE_CONFIGURATION) }) @DeleteMapping(value = "{UUID}", produces = { MediaType.TEXT_PLAIN_VALUE, MediaType.APPLICATION_JSON_VALUE }) ResponseEntity deleteConfiguration(@PathVariable("UUID") UUID uuid,
                                              @RequestHeader(HttpHeaders.ACCEPT) String acceptHeader); @ApiOperation(value = "Lists all of the cron task types and the field types of those tasks.") @ApiResponses(value = { @ApiResponse(code = 200, message = SUCCESSFUL_GET_CONFIGURATION), @ApiResponse(code = 404, message = NOT_FOUND_CONFIGURATION) }) @GetMapping(value = "/types/list", produces = { MediaType.APPLICATION_JSON_VALUE }) ResponseEntity listCronJobs(); @ApiOperation(value = "Used to get the configuration on given cron task UUID") @ApiResponses(value = { @ApiResponse(code = 200, message = SUCCESSFUL_GET_CONFIGURATION), @ApiResponse(code = 404, message = NOT_FOUND_CONFIGURATION) }) @GetMapping(value = "/{UUID}", produces = { MediaType.APPLICATION_JSON_VALUE, APPLICATION_YAML_VALUE }) ResponseEntity getConfiguration(@PathVariable("UUID") UUID uuid,
                                           @RequestHeader(HttpHeaders.ACCEPT) String acceptHeader); @ApiOperation(value = "Used to get list of all the configurations") @ApiResponses(value = { @ApiResponse(code = 200, message = SUCCESSFUL_GET_CONFIGURATIONS), @ApiResponse(code = 404, message = NOT_FOUND_CONFIGURATIONS) }) @GetMapping(produces = { MediaType.APPLICATION_JSON_VALUE, APPLICATION_YAML_VALUE }) ResponseEntity getConfigurations(@RequestHeader(HttpHeaders.ACCEPT) String acceptHeader); @ApiOperation(value = "Used to upload groovy script for groovy cron task") @ApiResponses(value = { @ApiResponse(code = 200, message = SUCCESSFUL_UPLOAD_GROOVY_SCRIPT), @ApiResponse(code = 400, message = FAILED_UPLOAD_GROOVY_SCRIPT) }) @PutMapping(value = "/cron/groovy/{UUID}", produces = { MediaType.TEXT_PLAIN_VALUE, MediaType.APPLICATION_JSON_VALUE }) ResponseEntity uploadGroovyScript(@PathVariable("UUID") UUID uuid,
                                             HttpServletRequest request,
                                             @RequestHeader(HttpHeaders.ACCEPT) String acceptHeader); @ApiOperation(value = "Used to get all groovy scripts names") @ApiResponses(value = { @ApiResponse(code = 200, message = SUCCESSFUL_GET_GROOVY_SCRIPTS) }) @GetMapping(value = "/groovy/names", produces = MediaType.APPLICATION_JSON_VALUE) ResponseEntity getGroovyScriptsName();  }
@Test public void testGroovyCronTaskConfiguration() throws Exception { CronTaskConfigurationForm cronTaskConfigurationForm = new CronTaskConfigurationForm(); cronTaskConfigurationForm.setJobClass(GroovyCronJob.class.getName()); cronTaskConfigurationForm.setCronExpression("0 11 11 11 11 ? 2100"); Headers headers = mockMvc.contentType(MediaType.APPLICATION_JSON_VALUE) .accept(MediaType.APPLICATION_JSON_VALUE) .body(cronTaskConfigurationForm) .when() .put(getContextBaseUrl()) .peek() .then() .statusCode(HttpStatus.OK.value()) .and() .extract() .headers(); UUID cronUuid = UUID.fromString(headers.getValue(HEADER_NAME_CRON_TASK_ID)); assertThat(cronUuid).isNotNull(); uploadGroovyScript(cronUuid); deleteConfig(cronUuid); }