Skip to content

Commit

Permalink
Fine tuned calculator
Browse files Browse the repository at this point in the history
  • Loading branch information
wakaleo committed Sep 30, 2014
1 parent 604ff0c commit 922dadc
Show file tree
Hide file tree
Showing 5 changed files with 137 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ Examples:
|Sydney |Melbourne |1700 |
|Melbourne |Wellington |4400 |


Scenario: Required points between different destinations
Meta:
@tag layer:webservice
Expand All @@ -38,3 +39,4 @@ Examples:
|BNE |SYD |1700 |
|BNE |LAX |12400 |
|LAX |BNE |12400 |

Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
@RepositoryRestResource(collectionResourceRel = "routes", path = "routes")
public interface RouteRepository extends MongoRepository<Route, String> {
List<Route> findByDepartureCode(@Param("departureCode") String departureCode);
List<Route> findByDestinationCode(@Param("destinationCode") String destinationCode);
List<Route> findByDepartureCodeAndDestinationCode(@Param("departureCode") String departureCode,
@Param("destinationCode") String destinationCode);
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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 {

Expand All @@ -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<Airport> airportsWithFlightsFrom(@RequestParam("departureCode") String departureCode)
throws NoSuchRouteException {
List<Airport> departs = routeRepository.findByDepartureCode(departureCode)
.stream()
.map(Route::getDestination)
.collect(toList());

List<Airport> 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<Airport> airportsWithFlightsTo(@RequestParam("destinationCode") String destinationCode)
throws NoSuchRouteException {
List<Airport> departs = routeRepository.findByDestinationCode(destinationCode)
.stream()
.map(Route::getDeparture)
.collect(toList());

List<Airport> returns = routeRepository.findByDepartureCode(destinationCode)
.stream()
.map(Route::getDestination)
.collect(toList());

return combinationOf(departs, returns);

}

private List<Airport> combinationOf(List<Airport> departs, List<Airport> returns) {
List<Airport> allAirports = Lists.newArrayList(departs);
allAirports.addAll(returns);
return allAirports.stream().distinct().collect(toList());
}

}
Original file line number Diff line number Diff line change
@@ -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<HashMap> 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<HashMap> 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<HashMap> 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")
);
}



}

0 comments on commit 922dadc

Please sign in to comment.