DefaultMessageRouter implements MessageRouter, Serializable { @Override public List<MessageRoute> route(JSONObject message, ProfilerConfig config, Context context) { List<MessageRoute> routes = new ArrayList<>(); for (ProfileConfig profile: config.getProfiles()) { Clock clock = clockFactory.createClock(config); Optional<MessageRoute> route = routeToProfile(message, profile, clock); route.ifPresent(routes::add); } return routes; } DefaultMessageRouter(Context context); @Override List<MessageRoute> route(JSONObject message, ProfilerConfig config, Context context); void setExecutor(StellarStatefulExecutor executor); void setClockFactory(ClockFactory clockFactory);  }
@Test public void testWithOneRoute() throws Exception { List<MessageRoute> routes = router.route(messageOne, createConfig(oneProfile), context); assertEquals(1, routes.size()); MessageRoute route1 = routes.get(0); assertEquals(messageOne.get("ip_src_addr"), route1.getEntity()); assertEquals("profile-one", route1.getProfileDefinition().getProfile()); }
@Test public void testWithNoRoutes() throws Exception { List<MessageRoute> routes = router.route(messageOne, createConfig(noProfiles), context); assertEquals(0, routes.size()); }
@Test public void testWithTwoRoutes() throws Exception { List<MessageRoute> routes = router.route(messageOne, createConfig(twoProfiles), context); assertEquals(2, routes.size()); { MessageRoute route1 = routes.get(0); assertEquals(messageOne.get("ip_src_addr"), route1.getEntity()); assertEquals("profile-one", route1.getProfileDefinition().getProfile()); } { MessageRoute route2 = routes.get(1); assertEquals(messageOne.get("ip_src_addr"), route2.getEntity()); assertEquals("profile-two", route2.getProfileDefinition().getProfile()); } }
@Test public void testExclusiveProfile() throws Exception { List<MessageRoute> routes = router.route(messageOne, createConfig(exclusiveProfile), context); assertEquals(0, routes.size()); }
@Test public void testWithBadForeachExpression() throws Exception { List<MessageRoute> routes = router.route(messageOne, createConfig(badForeach), context); assertEquals(0, routes.size()); }
@Test public void testWithBadOnlyifExpression() throws Exception { List<MessageRoute> routes = router.route(messageOne, createConfig(badForeach), context); assertEquals(0, routes.size()); }
@Test public void testWithGoodAndBad() throws Exception { List<MessageRoute> routes = router.route(messageOne, createConfig(goodAndBad), context); assertEquals(1, routes.size()); MessageRoute route1 = routes.get(0); assertEquals("good-profile", route1.getProfileDefinition().getProfile()); assertEquals(messageOne.get("ip_src_addr"), route1.getEntity()); }
@Test public void testMessageWithTimestamp() throws Exception { List<MessageRoute> routes = router.route(messageWithTimestamp, createConfig(profileWithEventTime), context); assertEquals(1, routes.size()); MessageRoute route1 = routes.get(0); assertEquals("profile-one", route1.getProfileDefinition().getProfile()); assertEquals(messageWithTimestamp.get("ip_src_addr"), route1.getEntity()); assertEquals(messageWithTimestamp.get("timestamp"), route1.getTimestamp()); }
@Test public void testMessageWithMissingTimestamp() throws Exception { List<MessageRoute> routes = router.route(messageOne, createConfig(profileWithEventTime), context); assertEquals(0, routes.size()); }