AlertRouter { public Route addRoute(Matcher scheduleMatcher, Matcher milestoneMatcher, Matcher windowMatcher, Action action) { Route route = new Route(scheduleMatcher, milestoneMatcher, windowMatcher, action); routes.add(route); return route; } AlertRouter(); Route addRoute(Matcher scheduleMatcher, Matcher milestoneMatcher, Matcher windowMatcher, Action action); @MotechListener(subjects = {EventSubjects.MILESTONE_ALERT}) void handle(MotechEvent realEvent);  }
@Test public void shouldBeAbleToSetACatchAllRoute() { router.addRoute(any(), any(), any(), firstAction); assertRouteMatches("someSchedule", "someMilestone", "someWindow", firstAction, new HashMap<String, String>()); assertRouteMatches("someOtherSchedule", "someOtherMilestone", "someOtherWindow", firstAction, new HashMap<String, String>()); }
@Test public void shouldBeAbleToSetARouteWhichMatchesBasedOnScheduleNameOnly() { router.addRoute(eq("Schedule X"), any(), any(), firstAction); assertNoRoutesMatch("someSchedule", "someMilestone", "someWindow"); assertRouteMatches("Schedule X", "someMilestone", "someWindow", firstAction, new HashMap<String, String>()); }
@Test public void shouldBeAbleToSetARouteWhichMatchesBasedOnMilestoneNameOnly() { router.addRoute(any(), eq("Milestone X"), any(), firstAction); assertNoRoutesMatch("someSchedule", "someMilestone", "someWindow"); assertRouteMatches("someOtherSchedule", "Milestone X", "someWindow", firstAction, new HashMap<String, String>()); }
@Test public void shouldBeAbleToSetARouteWhichMatchesBasedOnWindowNameOnly() { router.addRoute(any(), any(), eq("Window X"), firstAction); assertNoRoutesMatch("someSchedule", "someMilestone", "someWindow"); assertRouteMatches("someOtherSchedule", "someOtherMilestone", "Window X", firstAction, new HashMap<String, String>()); }
@Test public void shouldBeAbleToSetARouteWhichMatchesBasedOnACombinationOfMatchers() { router.addRoute(eq("Milestone X"), any(), eq("Window X"), firstAction); assertNoRoutesMatch("someSchedule", "someMilestone", "someWindow"); assertNoRoutesMatch("Milestone X", "someMilestone", "someWindow"); assertNoRoutesMatch("someSchedule", "someMilestone", "Window X"); assertRouteMatches("Milestone X", "someOtherMilestone", "Window X", firstAction, new HashMap<String, String>()); }
@Test public void shouldBeAbleToSetARouteWithExtraData() { router.addRoute(eq("Milestone X"), any(), eq("Window X"), firstAction).addExtraData("Unicorns", "AreFun"); assertNoRoutesMatch("someSchedule", "someMilestone", "someWindow"); assertNoRoutesMatch("Milestone X", "someMilestone", "someWindow"); assertNoRoutesMatch("someSchedule", "someMilestone", "Window X"); HashMap<String, String> extraData = new HashMap<>(); extraData.put("Unicorns", "AreFun"); assertRouteMatches("Milestone X", "someOtherMilestone", "Window X", firstAction, extraData); }
@Test public void shouldInvokeTheActionOfOnlyTheFirstRouteWhichMatchesInTheOrderInWhichTheyWereAdded() { router.addRoute(any(), any(), eq("Window X"), firstAction); router.addRoute(any(), any(), any(), secondAction); assertRouteMatches("someSchedule", "someMilestone", "Window X", firstAction, new HashMap<String, String>()); verifyZeroInteractions(secondAction); assertRouteMatches("someSchedule", "someMilestone", "someOtherWindow", secondAction, new HashMap<String, String>()); verifyZeroInteractions(firstAction); }
@Test public void shouldNotTryAndLookAtTheBestMatchButJustTheFirstProperMatch() { router.addRoute(any(), any(), eq("Window X"), firstAction); router.addRoute(eq("Milestone X"), any(), eq("Window X"), secondAction); assertRouteMatches("Milestone X", "someMilestone", "Window X", firstAction, new HashMap<String, String>()); verifyZeroInteractions(secondAction); }