ActionService { public FetchStatus fetchNewActions() { String previousFetchIndex = allSettings.fetchPreviousFetchIndex(); Response<List<Action>> response = drishtiService.fetchNewActions(allSharedPreference.fetchRegisteredANM(), previousFetchIndex); if (response.isFailure()) { return fetchedFailed; } if (response.payload().isEmpty()) { return nothingFetched; } handleActions(response); return FetchStatus.fetched; } ActionService(DrishtiService drishtiService, AllSettings allSettings, AllSharedPreferences allSharedPreferences, AllReports allReports);  ActionService(DrishtiService drishtiService, AllSettings allSettings, AllSharedPreferences allSharedPreferences, AllReports allReports, ActionRouter actionRouter); FetchStatus fetchNewActions();  }
@Test public void shouldFetchAlertActionsAndNotSaveAnythingIfThereIsNothingNewToSave() throws Exception { setupActions(success, new ArrayList<Action>()); assertEquals(nothingFetched, service.fetchNewActions()); verify(drishtiService).fetchNewActions("ANM X", "1234"); verifyNoMoreInteractions(drishtiService); verifyNoMoreInteractions(actionRouter); }
@Test public void shouldNotSaveAnythingIfTheDrishtiResponseStatusIsFailure() throws Exception { setupActions(failure, asList(actionForCloseAlert("Case X", "ANC 1", "2012-01-01", "0"))); assertEquals(fetchedFailed, service.fetchNewActions()); verify(drishtiService).fetchNewActions("ANM X", "1234"); verifyNoMoreInteractions(drishtiService); verifyNoMoreInteractions(actionRouter); }
@Test public void shouldFetchAlertActionsAndSaveThemToRepository() throws Exception { Action action = ActionBuilder.actionForCreateAlert("Case X", "normal", "mother", "Ante Natal Care - Normal", "ANC 1", "2012-01-01", null, "0"); setupActions(success, asList(action)); assertEquals(fetched, service.fetchNewActions()); verify(drishtiService).fetchNewActions("ANM X", "1234"); verify(actionRouter).directAlertAction(action); }
@Test public void shouldUpdatePreviousIndexWithIndexOfEachActionThatIsHandled() throws Exception { Action firstAction = actionForCreateAlert("Case X", "normal", "mother", "Ante Natal Care - Normal", "ANC 1", "2012-01-01", "2012-01-22", "11111"); Action secondAction = actionForCreateAlert("Case Y", "normal", "mother", "Ante Natal Care - Normal", "ANC 2", "2012-01-01", "2012-01-11", "12345"); setupActions(success, asList(firstAction, secondAction)); service.fetchNewActions(); InOrder inOrder = inOrder(actionRouter, allSettings); inOrder.verify(actionRouter).directAlertAction(firstAction); inOrder.verify(allSettings).savePreviousFetchIndex("11111"); inOrder.verify(actionRouter).directAlertAction(secondAction); inOrder.verify(allSettings).savePreviousFetchIndex("12345"); }