SchedulerFactory { public List<Scheduler> parseSchedulers(Buffer buffer) throws ValidationException { List<Scheduler> result = new ArrayList<>(); String configString; try { configString = StringUtils.replaceWildcardConfigs(buffer.toString("UTF-8"), properties); } catch(Exception e){ throw new ValidationException(e); } ValidationResult validationResult = Validator.validateStatic(Buffer.buffer(configString), schedulersSchema, log); if(!validationResult.isSuccess()){ throw new ValidationException(validationResult); } JsonObject mainObject = new JsonObject(configString); for(Map.Entry<String,Object> entry: mainObject.getJsonObject(SCHEDULERS).getMap().entrySet()) { Map<String,Object> schedulerJson = (Map<String,Object>)entry.getValue(); boolean executeOnStartup = false; boolean executeOnReload = false; if ( schedulerJson.containsKey(EXECUTE_ON_STARTUP) ) { executeOnStartup = (boolean) schedulerJson.get(EXECUTE_ON_STARTUP); executeOnReload = executeOnStartup; } if ( schedulerJson.containsKey(EXECUTE_ON_RELOAD) ) { executeOnReload = (boolean) schedulerJson.get(EXECUTE_ON_RELOAD); } int maxRandomOffset = 0; if ( schedulerJson.containsKey(RANDOM_OFFSET) ) { try { maxRandomOffset = (Integer) schedulerJson.get(RANDOM_OFFSET); } catch(NumberFormatException e) { throw new ValidationException("Could not parse " + RANDOM_OFFSET + " of scheduler '"+entry.getKey()+"'", e); } } List<HttpRequest> requests = new ArrayList<>(); for(int i = 0; i< ((ArrayList<Object>)schedulerJson.get(REQUESTS)).size(); i++) { try { requests.add(new HttpRequest(prepare((JsonObject) mainObject.getJsonObject(SCHEDULERS).getJsonObject(entry.getKey()).getJsonArray(REQUESTS).getValue(i)))); } catch(Exception e) { throw new ValidationException("Could not parse request ["+i+"] of scheduler "+entry.getKey(), e); } } try { result.add(new Scheduler(vertx, redisquesAddress, redisClient, entry.getKey(), (String)schedulerJson.get("cronExpression"), requests, monitoringHandler, maxRandomOffset, executeOnStartup, executeOnReload)); } catch (ParseException e) { throw new ValidationException("Could not parse cron expression of scheduler '"+entry.getKey()+"'", e); } } return result; } SchedulerFactory(Map<String, Object> properties, Vertx vertx, RedisClient redisClient, MonitoringHandler monitoringHandler, String schedulersSchema, String redisquesAddress); List<Scheduler> parseSchedulers(Buffer buffer);  }
@Test public void testInvalidJson() throws ValidationException { thrown.expect( ValidationException.class ); thrown.expectMessage("Unable to parse json"); schedulerFactory.parseSchedulers(Buffer.buffer(INVALID_JSON)); }
@Test public void testValidSchedulerConfig(TestContext context) throws ValidationException { List<Scheduler> schedulers = schedulerFactory.parseSchedulers(Buffer.buffer(VALID_SCHEDULER_RESOURCE)); context.assertNotNull(schedulers); context.assertEquals(2, schedulers.size()); }
@Test public void testMissingSchedulersProperty(TestContext context) throws ValidationException { try { schedulerFactory.parseSchedulers(Buffer.buffer(MISSING_SCHEDULERS_PROPERTY)); context.fail("Should have thrown a ValidationException since 'schedulers' property is missing"); } catch(ValidationException ex){ context.assertNotNull(ex.getValidationDetails()); context.assertEquals(2, ex.getValidationDetails().size()); for(Object obj : ex.getValidationDetails()){ JsonObject jsonObject = (JsonObject) obj; if("additionalProperties".equalsIgnoreCase(jsonObject.getString("keyword"))){ context.assertEquals("listofschedulers", jsonObject.getJsonArray("unwanted").getString(0)); } else if("required".equalsIgnoreCase(jsonObject.getString("keyword"))){ context.assertEquals("schedulers", jsonObject.getJsonArray("missing").getString(0)); } } } }
@Test public void testMissingCronExpressionProperty(TestContext context) throws ValidationException { try { schedulerFactory.parseSchedulers(Buffer.buffer(MISSING_CRONEXPRESSION_PROPERTY)); context.fail("Should have thrown a ValidationException since 'cronExpression' property is missing"); } catch(ValidationException ex){ context.assertNotNull(ex.getValidationDetails()); context.assertEquals(2, ex.getValidationDetails().size()); for(Object obj : ex.getValidationDetails()){ JsonObject jsonObject = (JsonObject) obj; if("additionalProperties".equalsIgnoreCase(jsonObject.getString("keyword"))){ context.assertEquals("cron", jsonObject.getJsonArray("unwanted").getString(0)); } else if("required".equalsIgnoreCase(jsonObject.getString("keyword"))){ context.assertEquals("cronExpression", jsonObject.getJsonArray("missing").getString(0)); } } } }