RegexRouter implements Router { @Override public Agent[] route(Request request) { String text = request.getText(); Set<Agent> agents = new HashSet<Agent>(); for (Map.Entry<Pattern, Agent> entry : agentMap.entrySet()) { if (entry.getKey().matcher(text).matches()) { agents.add(entry.getValue()); } } return agents.toArray(new Agent[agents.size()]); } RegexRouter(RouterAgentConfig agentsConfig); @Override Agent[] route(Request request); @Override Agent[] getAgents(); static final String REGEX; }
@Test public void testRouting() { Agent agent1 = mock(Agent.class); MultiValuedMap params1 = new MultiValuedMap(); params1.addString(RegexRouter.REGEX, ".*time.*"); params1.addString(RegexRouter.REGEX, ".*date.*"); Agent agent2 = mock(Agent.class); MultiValuedMap params2 = new MultiValuedMap(); params2.addString(RegexRouter.REGEX, "^this is a test$"); RouterAgentConfig config = RouterAgentConfig.create(); config.add(agent1, params1); config.add(agent2, params2); Router router = new RegexRouter(config); Agent agents[] = router.route(new Request("This is a test")); Agent expected[] = new Agent[]{agent2}; assertArrayEquals(expected, agents); Agent agents2[] = router.route(new Request("What time is it?")); Agent expected2[] = new Agent[]{agent1}; assertArrayEquals(expected2, agents2); }