ObtainLineasCercanasAction { public Observable<Linea> obtainLineas(Location location) { return obtainCercanasAction.obtainCercanas(location) .map(ParadaCercana::getParada) .map(Parada::getNumero) .flatMap(lineaDataSource::getFromParada) .flatMap(Observable::from) .distinct() .toSortedList() .flatMap(Observable::from); } ObtainLineasCercanasAction(LineaDataSource lineaDataSource, ObtainCercanasAction obtainCercanasAction); Observable<Linea> obtainLineas(Location location);  }
@Test public void returns_distinct_stops_have_distinct_lines() throws Exception { when(lineaDataSource.getFromParada(P1_NUMBER)).thenReturn(lineas(LINEA_A)); when(lineaDataSource.getFromParada(P2_NUMBER)).thenReturn(lineas(LINEA_B)); when(obtainCercanasAction.obtainCercanas(any(Location.class))).thenReturn(Observable.just(PARADA_1, PARADA_2)); List<Linea> results = action.obtainLineas(SOME_LOCATION).toList().toBlocking().single(); assertThat(results).containsSequence(LINEA_A, LINEA_B); }
@Test public void returns_distinct_when_stops_have_common_lines() throws Exception { when(lineaDataSource.getFromParada(P1_NUMBER)).thenReturn(lineas(LINEA_A, LINEA_B)); when(lineaDataSource.getFromParada(P2_NUMBER)).thenReturn(lineas(LINEA_B)); when(obtainCercanasAction.obtainCercanas(any(Location.class))).thenReturn(Observable.just(PARADA_1, PARADA_2)); List<Linea> results = action.obtainLineas(SOME_LOCATION).toList().toBlocking().single(); assertThat(results).containsSequence(LINEA_A, LINEA_B); }
@Test public void returns_ordered_when_stops_have_unordered_lines() throws Exception { when(lineaDataSource.getFromParada(P1_NUMBER)).thenReturn(lineas(LINEA_B)); when(lineaDataSource.getFromParada(P2_NUMBER)).thenReturn(lineas(LINEA_A)); when(obtainCercanasAction.obtainCercanas(any(Location.class))).thenReturn(Observable.just(PARADA_1, PARADA_2)); List<Linea> results = action.obtainLineas(SOME_LOCATION).toList().toBlocking().single(); assertThat(results).containsSequence(LINEA_A, LINEA_B); }