diff --git a/acceptance-tests/src/test/java/flyinghigh/services/acceptancetests/stepdefs/FrequentFlyerStepDefinitions.java b/acceptance-tests/src/test/java/flyinghigh/services/acceptancetests/stepdefs/FrequentFlyerStepDefinitions.java index e1c74d2..8ba17b9 100644 --- a/acceptance-tests/src/test/java/flyinghigh/services/acceptancetests/stepdefs/FrequentFlyerStepDefinitions.java +++ b/acceptance-tests/src/test/java/flyinghigh/services/acceptancetests/stepdefs/FrequentFlyerStepDefinitions.java @@ -29,11 +29,13 @@ public void giveSarahSomePoints(FrequentFlyer frequentFlyer, int points) throws restClient.updatePointsFor(frequentFlyer.getNumber(), points); } -// @When("Sarah views her account details") -// public void viewAccountDetails() { -// } -// -// @Then("she should see an account balance of $expectedPoints points") -// public void shouldSeePointBalanceOf(int expectedPoints) { -// } + @When("Sarah views her account details") + public void viewAccountDetails() { + sarah.openAccountPage(); + } + + @Then("she should see an account balance of $expectedPoints points") + public void shouldSeePointBalanceOf(int expectedPoints) { + sarah.shouldSeeAccountBalanceOf(expectedPoints); + } } diff --git a/acceptance-tests/src/test/resources/stories/spending_points/calculating_required_points.story b/acceptance-tests/src/test/resources/stories/spending_points/calculating_required_points.story index c53f52c..b0f3177 100644 --- a/acceptance-tests/src/test/resources/stories/spending_points/calculating_required_points.story +++ b/acceptance-tests/src/test/resources/stories/spending_points/calculating_required_points.story @@ -21,6 +21,7 @@ Examples: |Sydney |Melbourne |1700 | |Melbourne |Wellington |4400 | + Scenario: Required points between different destinations Meta: @tag layer:webservice @@ -38,3 +39,4 @@ Examples: |BNE |SYD |1700 | |BNE |LAX |12400 | |LAX |BNE |12400 | + diff --git a/flights-web-service/src/main/java/flyinghigh/services/flights/repositories/RouteRepository.java b/flights-web-service/src/main/java/flyinghigh/services/flights/repositories/RouteRepository.java index 4606c39..bc8549a 100644 --- a/flights-web-service/src/main/java/flyinghigh/services/flights/repositories/RouteRepository.java +++ b/flights-web-service/src/main/java/flyinghigh/services/flights/repositories/RouteRepository.java @@ -10,6 +10,7 @@ @RepositoryRestResource(collectionResourceRel = "routes", path = "routes") public interface RouteRepository extends MongoRepository { List findByDepartureCode(@Param("departureCode") String departureCode); + List findByDestinationCode(@Param("destinationCode") String destinationCode); List findByDepartureCodeAndDestinationCode(@Param("departureCode") String departureCode, @Param("destinationCode") String destinationCode); } \ No newline at end of file diff --git a/flights-web-service/src/main/java/flyinghigh/services/flights/web/RoutesController.java b/flights-web-service/src/main/java/flyinghigh/services/flights/web/RoutesController.java index e573cef..772bcb7 100644 --- a/flights-web-service/src/main/java/flyinghigh/services/flights/web/RoutesController.java +++ b/flights-web-service/src/main/java/flyinghigh/services/flights/web/RoutesController.java @@ -1,6 +1,8 @@ package flyinghigh.services.flights.web; +import com.google.common.collect.Lists; import flyinghigh.services.flights.domain.Airport; +import flyinghigh.services.flights.domain.Route; import flyinghigh.services.flights.repositories.RouteRepository; import flyinghigh.services.flights.services.database.DatabaseSetup; import flyinghigh.services.flights.services.points.NoSuchRouteException; @@ -11,6 +13,11 @@ import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; +import java.util.Collections; +import java.util.List; + +import static java.util.stream.Collectors.toList; + @RestController public class RoutesController { @@ -33,7 +40,47 @@ public int calculateRequiredPoints(@RequestParam("departureCode") String departu @RequestParam("destinationCode") String destinationCode) throws NoSuchRouteException { - return (departureCode.equals(destinationCode)) ? - 0 : pointsCalculator.calculatePointsRequiredBetween(departureCode, destinationCode); + return (departureCode.equals(destinationCode)) ? + 0 : pointsCalculator.calculatePointsRequiredBetween(departureCode, destinationCode); + } + + @RequestMapping(method = RequestMethod.GET, value = "/rest/api/routes/from") + public List airportsWithFlightsFrom(@RequestParam("departureCode") String departureCode) + throws NoSuchRouteException { + List departs = routeRepository.findByDepartureCode(departureCode) + .stream() + .map(Route::getDestination) + .collect(toList()); + + List returns = routeRepository.findByDestinationCode(departureCode) + .stream() + .map(Route::getDeparture) + .collect(toList()); + + return combinationOf(departs, returns); + } + + @RequestMapping(method = RequestMethod.GET, value = "/rest/api/routes/to") + public List airportsWithFlightsTo(@RequestParam("destinationCode") String destinationCode) + throws NoSuchRouteException { + List departs = routeRepository.findByDestinationCode(destinationCode) + .stream() + .map(Route::getDeparture) + .collect(toList()); + + List returns = routeRepository.findByDepartureCode(destinationCode) + .stream() + .map(Route::getDestination) + .collect(toList()); + + return combinationOf(departs, returns); + + } + + private List combinationOf(List departs, List returns) { + List allAirports = Lists.newArrayList(departs); + allAirports.addAll(returns); + return allAirports.stream().distinct().collect(toList()); } + } diff --git a/flights-web-service/src/test/java/flyinghigh/services/flights/FindingDestinationAndDepartureAirports.java b/flights-web-service/src/test/java/flyinghigh/services/flights/FindingDestinationAndDepartureAirports.java new file mode 100644 index 0000000..be7850d --- /dev/null +++ b/flights-web-service/src/test/java/flyinghigh/services/flights/FindingDestinationAndDepartureAirports.java @@ -0,0 +1,76 @@ +package flyinghigh.services.flights; + +import com.mongodb.util.Hash; +import flyinghigh.services.flights.domain.Airport; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.boot.context.embedded.EmbeddedWebApplicationContext; +import org.springframework.boot.test.IntegrationTest; +import org.springframework.boot.test.SpringApplicationConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.context.web.WebAppConfiguration; +import org.springframework.web.client.RestTemplate; + +import java.util.HashMap; +import java.util.List; + +import static org.fest.assertions.api.Assertions.assertThat; + +@RunWith(SpringJUnit4ClassRunner.class) +@SpringApplicationConfiguration(classes = FlightsApp.class) +@WebAppConfiguration +@IntegrationTest({"server.port=0", "management.port=0"}) +public class FindingDestinationAndDepartureAirports { + + @Autowired + private EmbeddedWebApplicationContext server; + + @Value("${local.server.port}") + private int port; + + private RestTemplate restTemplate = new RestTemplate(); + + private String baseUrl; + + @Before + public void configureBaseUrl() { + baseUrl = "http://localhost:" + port; + restTemplate = new RestTemplate(); + } + + @Test + public void should_find_airports_with_flights_to_a_given_destination() { + List destinations = restTemplate.getForObject(baseUrl + "/rest/api/routes/to?destinationCode={destinationCode}", List.class, "SYD"); + + assertThat(destinations).isNotEmpty(); + destinations.stream().forEach( + destination -> assertThat(destination.get("code")).isNotEqualTo("SYD") + ); + } + + @Test + public void should_find_airports_with_flights_from_a_given_destination() { + List destinations = restTemplate.getForObject(baseUrl + "/rest/api/routes/from?departureCode={departureCode}", List.class, "SYD"); + + assertThat(destinations).isNotEmpty(); + destinations.stream().forEach( + destination -> assertThat(destination.get("code")).isNotEqualTo("SYD") + ); + } + + @Test + public void should_find_airports_based_on_return_routes() { + List destinations = restTemplate.getForObject(baseUrl + "/rest/api/routes/from?departureCode={departureCode}", List.class, "WLG"); + + assertThat(destinations).isNotEmpty(); + destinations.stream().forEach( + destination -> assertThat(destination.get("code")).isNotEqualTo("WEL") + ); + } + + + +}