Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
wakaleo committed Sep 24, 2014
1 parent a886113 commit b645087
Show file tree
Hide file tree
Showing 15 changed files with 252 additions and 26 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package flyinghigh.services.acceptancetests;

import net.thucydides.jbehave.ThucydidesJUnitStory;

/**
* Created by john on 18/09/2014.
*/
public class ListingAffiliatedHotels extends ThucydidesJUnitStory {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package flyinghigh.services.acceptancetests.pages;

import flyinghigh.services.acceptancetests.domain.Airport;
import net.thucydides.core.pages.WebElementFacade;

import java.util.Map;

/**
* Created by john on 24/09/2014.
*/
public class DisplayedAirport {
public static Airport fromWebElement(final WebElementFacade airportElement) {
return new Airport(airportElement.findBy(".airport-name").getText(),
airportElement.findBy(".airport-code").getText(),
airportElement.findBy(".airport-country").getText());
}

public static Airport fromMapValues(final Map<String, String> airportFields) {
return new Airport(airportFields.get("name"),
airportFields.get("code"),
airportFields.get("country"));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package flyinghigh.services.acceptancetests.pages;

import flyinghigh.services.acceptancetests.domain.Airport;
import net.thucydides.core.annotations.DefaultUrl;
import net.thucydides.core.pages.PageObject;

import java.util.List;
import java.util.stream.Collectors;

@DefaultUrl("http://localhost:9001/#/home")
public class HomePage extends PageObject {
public List<Airport> getDisplayedAirports() {
return findAll(".airport")
.stream()
.map(DisplayedAirport::fromWebElement)
.collect(Collectors.toList());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package flyinghigh.services.acceptancetests.rest;

import com.google.common.collect.ImmutableList;
import flyinghigh.services.acceptancetests.domain.Airport;
import org.springframework.web.client.RestTemplate;

import java.util.List;

public class RestClient {

private RestTemplate restTemplate = new RestTemplate();

public String getBaseFlightUrl() {
String environment = System.getProperty("webservice.environment","local");
if (environment.equals("local")) {
return "http://localhost:8090/";
} else {
return "http://" + environment + "-" + "flights.cfapps.io";
}
}

public List<Airport> findAllAirports() {
return findAllAirports("/rest/api/airports");
}


public List<Airport> findAllAirports(String path) {
return ImmutableList.copyOf(restTemplate.getForEntity(getBaseFlightUrl() + path, Airport[].class).getBody());
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package flyinghigh.services.acceptancetests.stepdefs;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Lists;
import flyinghigh.services.acceptancetests.domain.Airport;
import flyinghigh.services.acceptancetests.pages.DisplayedAirport;
import flyinghigh.services.acceptancetests.steps.AirportClientSteps;
import net.thucydides.core.annotations.Steps;
import org.jbehave.core.annotations.Given;
Expand All @@ -11,7 +14,10 @@

import java.util.List;
import java.util.Map;
import java.util.stream.Collector;
import java.util.stream.Collectors;

import static ch.lambdaj.Lambda.collect;
import static org.fest.assertions.api.Assertions.assertThat;


Expand All @@ -31,20 +37,22 @@ public void givenINeedToKnowWhatCitiesICanFlyTo() {

@When("I ask for a list of airports")
public void whenIAskForAListOfAirports() {
retrievedAirports = airportClientSteps.listAllAirports("/reference/airports");
retrievedAirports = airportClientSteps.listAllAirports("/rest/api/airports");
}



@Then("I should obtain at least the following: $expectedAirports")
public void thenIShouldObtainAtLeastTheFollowing(ExamplesTable expectedAirports) {

List<Airport> expected = Lists.newArrayList();
for(Map<String, String> airportFields : expectedAirports.getRows()) {
expected.add(new Airport(airportFields.get("name"),
airportFields.get("code"),
airportFields.get("country")));
expected.add(DisplayedAirport.fromMapValues(airportFields));

}
assertThat(retrievedAirports).containsAll(expected);
}



}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package flyinghigh.services.acceptancetests.stepdefs;

import com.google.common.collect.Lists;
import flyinghigh.services.acceptancetests.domain.Airport;
import flyinghigh.services.acceptancetests.pages.HomePage;
import flyinghigh.services.acceptancetests.rest.RestClient;
import flyinghigh.services.acceptancetests.steps.AirportClientSteps;
import net.thucydides.core.annotations.Steps;
import org.jbehave.core.annotations.Given;
import org.jbehave.core.annotations.Then;
import org.jbehave.core.annotations.When;
import org.jbehave.core.model.ExamplesTable;

import java.util.List;
import java.util.Map;

import static org.fest.assertions.api.Assertions.assertThat;


/**
* Created by john on 17/09/2014.
*/
public class AirportStepUIDefinitions {

RestClient restClient = new RestClient();

@Steps
AirportClientSteps airportClientSteps;

HomePage homePage;

@When("I go to the home page")
public void openHomePage() {
homePage.open();
}

@Then("I should see the list of possibile destinations")
public void seeListOfPossibleDestinations() {
List<Airport> expectedAirports = restClient.findAllAirports();

List<Airport> displayedAirports = homePage.getDisplayedAirports();

assertThat(displayedAirports.size()).isEqualTo(expectedAirports.size());
assertThat(displayedAirports).containsAll(expectedAirports);
}



}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package flyinghigh.services.acceptancetests.stepdefs;

import org.jbehave.core.annotations.Given;

/**
* Created by john on 24/09/2014.
*/
public class FrequentFlyerStepDefinitions {

@Given("I am a frequent flyer")
public void givenAFrequentFlyer() {
// TODO
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.google.common.collect.ImmutableList;
import flyinghigh.services.acceptancetests.domain.Airport;
import flyinghigh.services.acceptancetests.rest.RestClient;
import net.thucydides.core.annotations.Step;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
Expand All @@ -14,23 +15,10 @@
*/
public class AirportClientSteps {

private RestTemplate restTemplate = new RestTemplate();
private static final HttpHeaders APPLICATION_JSON_HEADER = new HttpHeaders();
static {
APPLICATION_JSON_HEADER.setContentType(MediaType.APPLICATION_JSON);
}

public String getBaseFlightUrl() {
String environment = System.getProperty("webservice.environment","local");
if (environment.equals("local")) {
return "http://localhost:8090/";
} else {
return "http://" + environment + "-" + "flights.cfapps.io";
}
}
RestClient restClient = new RestClient();

@Step
public List<Airport> listAllAirports(String path) {
return ImmutableList.copyOf(restTemplate.getForEntity(getBaseFlightUrl() + path, Airport[].class).getBody());
return restClient.findAllAirports(path);
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
Meta:
@Versions Release 1, Iteration 1.0

Narrative:
In order to know where I can fly
As a traveller
I want to know what airports are served by Flying High flights


Scenario: List serviced airports
Given I need to know what cities I can fly to
When I ask for a list of airports
Expand All @@ -15,3 +17,8 @@ Then I should obtain at least the following:
| Australia | Brisbane | BNE |
| USA | San Francisco | SFO |
| USA | Los Angeles | LAX |

Scenario: Display available destinations on the home page
Given I need to know what cities I can fly to
When I go to the home page
Then I should see the list of possibile destinations
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
Meta:
@Versions Release 1, Iteration 1.1

Narrative:
In order to earn the most points possible
As a traveller
I want to know what hotels will let me earn poitns


Scenario: List partner hotels
Given I am a frequent flyer
When I ask to see what hotels I should go to to earn more points
Then I should see the list of partner hotels
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package flyinghigh.services.flights.filters;

import org.springframework.stereotype.Component;

import javax.servlet.*;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Component
public class SimpleCORSFilter implements Filter {

public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
HttpServletResponse response = (HttpServletResponse) res;
response.setHeader("Access-Control-Allow-Origin", "*");
response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");
response.setHeader("Access-Control-Max-Age", "3600");
response.setHeader("Access-Control-Allow-Headers", "x-requested-with");
chain.doFilter(req, res);
}

public void init(FilterConfig filterConfig) {}

public void destroy() {}

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@

public class Bootstrap implements ApplicationListener<ContextRefreshedEvent> {

private @Autowired
DatabaseSetup databaseSetup;
private @Autowired DatabaseSetup databaseSetup;

@Override
public void onApplicationEvent(ContextRefreshedEvent event) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,16 @@ public class DatabaseSetupImpl implements DatabaseSetup {

private final static List<Airport> DEFAULT_AIRPORTS = ImmutableList.of(
Airport.called("Sydney").inCountry("Australia").withCode("SYD"),
Airport.called("Sydney").inCountry("Melbourne").withCode("MLB"),
Airport.called("Melbourne").inCountry("Australia").withCode("MLB"),
Airport.called("Brisbane").inCountry("Australia").withCode("BNE"),
Airport.called("San Francisco").inCountry("USA").withCode("SFO"),
Airport.called("Los Angeles").inCountry("USA").withCode("LAX"),
Airport.called("Hong Kong").inCountry("Hong Kong").withCode("HKG"),
Airport.called("Singapore").inCountry("Singapore").withCode("SIN"),
Airport.called("Beijing").inCountry("China").withCode("PEK"),
Airport.called("Auckland").inCountry("New Zealand").withCode("AKL"),
Airport.called("Wellington").inCountry("New Zealand").withCode("WLG"),
Airport.called("Christchurch").inCountry("New Zealand").withCode("LHR"),
Airport.called("Christchurch").inCountry("New Zealand").withCode("LHR"),
Airport.called("Christchurch").inCountry("New Zealand").withCode("CHC"),
Airport.called("Paris").inCountry("France").withCode("CDG"),
Airport.called("Nice").inCountry("France").withCode("NIC"),
Airport.called("Rome").inCountry("Italy").withCode("FCO"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@
import java.util.List;

@RestController
public class ReferenceDataController {
public class AirportsController {

private @Autowired AirportRepository airportRepository;

@RequestMapping("/reference/airports")
@RequestMapping("/rest/api/airports")
public List<Airport> listAirports() {
return airportRepository.findAll(new Sort("name"));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package flyinghigh.services.flights;

import flyinghigh.services.flights.domain.Airport;
import flyinghigh.services.flights.repositories.AirportRepository;
import flyinghigh.services.flights.services.DatabaseSetup;
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.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 InitializingTheAirportsIT {

@Autowired
private DatabaseSetup databaseSetup;

@Autowired
private AirportRepository airportRepository;

@Test
public void should_instantiate_database_with_standard_airports() {
databaseSetup.initializeAirports();
List<Airport> airports = airportRepository.findAll();
assertThat(airports).isNotEmpty();
}

}

0 comments on commit b645087

Please sign in to comment.