RuleProvider { public Future<List<Rule>> getRules(){ Future<List<Rule>> future = Future.future(); storage.get(rulesPath, buffer -> { if (buffer != null) { try { List<Rule> rules = new RuleFactory(properties, routingRulesSchema).parseRules(buffer); future.complete(rules); } catch (ValidationException e) { log.error("Could parse routing rules", e); future.fail(e); } } else { future.fail("Could not get URL '" + (rulesPath == null ? "<null>" : rulesPath) + "' (getting rules)."); } }); return future; } RuleProvider(Vertx vertx, String rulesPath, ResourceStorage storage, Map<String, Object> properties); void registerObserver(RuleChangesObserver observer); Future<List<Rule>> getRules();  }
@Test public void testGetRulesWithWrongRulesPath(TestContext context){ RuleProvider ruleProvider = new RuleProvider(vertx, "/some/wrong/path", storage, properties); Future<List<Rule>> rulesFuture = ruleProvider.getRules(); context.assertTrue(rulesFuture.failed(), "getRules() future should not have been successful"); context.assertNotNull(rulesFuture.cause()); context.assertTrue(rulesFuture.cause().getMessage().contains("Could not get URL")); }
@Test public void testGetRulesWithMissingPropertiesinRoutingRules(TestContext context){ ((MockResourceStorage)storage).putMockData(rulesPath, RULES_WITH_MISSING_PROPS); RuleProvider ruleProvider = new RuleProvider(vertx, rulesPath, storage, properties); Future<List<Rule>> rulesFuture = ruleProvider.getRules(); context.assertTrue(rulesFuture.failed(), "getRules() future should not have been successful"); System.out.println(rulesFuture.cause().getMessage()); context.assertNotNull(rulesFuture.cause()); context.assertTrue(rulesFuture.cause().getMessage().contains("Could not resolve gateleen.test.prop.1")); }
@Test public void testGetRulesAfterInitialization(TestContext context){ RuleProvider ruleProvider = new RuleProvider(vertx, rulesPath, storage, properties); Future<List<Rule>> rulesFuture = ruleProvider.getRules(); context.assertTrue(rulesFuture.succeeded(), "getRules() future should have been successful"); context.assertNotNull(rulesFuture.result(), "The list of rules should not be null"); context.assertEquals(3, rulesFuture.result().size(), "There should be exactly 3 rules"); }
@Test public void testUpdateRulesOnEventBusEvent(TestContext context) throws InterruptedException { RuleProvider ruleProvider = new RuleProvider(vertx, rulesPath, storage, properties); Future<List<Rule>> rulesFuture = ruleProvider.getRules(); context.assertTrue(rulesFuture.succeeded(), "getRules() future should have been successful"); context.assertNotNull(rulesFuture.result(), "The list of rules should not be null"); context.assertEquals(3, rulesFuture.result().size(), "There should be exactly 3 rules"); ((MockResourceStorage)storage).putMockData(rulesPath, RULES_STORAGE_UPDATED); vertx.eventBus().publish(Address.RULE_UPDATE_ADDRESS, true); Future<List<Rule>> rulesFuture2 = ruleProvider.getRules(); context.assertTrue(rulesFuture2.succeeded(), "getRules() future should have been successful"); context.assertNotNull(rulesFuture2.result(), "The list of rules should not be null"); context.assertEquals(2, rulesFuture2.result().size(), "There should be exactly 2 rules after update"); }