WeatherService { public List<Weather> searchAirportWeather(String iataCode, double radiusKm) { Airport centralAirport = repository.getAirport(iataCode); if (radiusKm < 0) { throw new WeatherException("Radius cannot be negative"); } if (radiusKm < .0001) { return Collections.singletonList(centralAirport.getWeather()); } List<Weather> resultList = new ArrayList<>(); for (Airport otherAirport : repository.getAllAirports()){ if (centralAirport == otherAirport || calculateHaversineDistance(centralAirport, otherAirport) <= radiusKm){ Weather weather = otherAirport.getWeather(); if (!weather.isEmpty()) { resultList.add(weather); } } } return resultList; }  @PostConstruct void loadDefaultAirports(); void resetData(); void setDataPoint(String iataCode, DataPointType type, DataPoint dataPoint); Airport createAirport(String iataCode, double latitude, double longitude); List<Weather> searchAirportWeather(String iataCode, double radiusKm); Airport findAirport(String iataCode); double calculateHaversineDistance(Airport a1, Airport a2); Set<String> getAllIataCodes(); void deleteAirport(String iataCode); void createAirport(Airport airport);  }
@Test(expected = WeatherException.class) public void searchWithNegativeRadiusFails() { target.searchAirportWeather("code1", -1); }
@Test public void searchWithRadius0AlwaysReturnsThatAirportAWeather() { assertEquals(1, target.searchAirportWeather("code1", 0).size()); assertEquals(airport.getWeather(), target.searchAirportWeather("code1", 0).get(0)); }
@Test public void searchWithNon0RadiusReturnsWeatherWithData() { assertEquals(0, target.searchAirportWeather("code1", .01).size()); }
@Test(expected = WeatherException.class) public void searchWithNonNegativeRadiusForUnknownIataThrowsError() { when(repository.getAirport("code2")).thenThrow(new WeatherException("")); target.searchAirportWeather("code2", .01); }
@Test public void searchWithRadiusReturnsMultipleAirports() { Airport airport2 = new Airport("code2", 0.01, 0.01); airport.getWeather().setPrecipitation(new DataPoint.Builder().withMean(50).build());; airport.getWeather().setLastUpdateTime(System.currentTimeMillis()); airport2.getWeather().setPrecipitation(new DataPoint.Builder().withMean(50).build());; airport2.getWeather().setLastUpdateTime(System.currentTimeMillis()); when(repository.getAllAirports()).thenReturn(Arrays.asList( airport, airport2 )); List<Weather> list = target.searchAirportWeather("code1", 1000); assertEquals(2, list.size()); }
@Test public void searchWithRadiusReturnsOnlyKnownWeatherData() { airport.getWeather().setLastUpdateTime(System.currentTimeMillis()); when(repository.getAllAirports()).thenReturn(Arrays.asList(airport)); List<Weather> list = target.searchAirportWeather("code1", 1000); assertEquals(0, list.size()); }
@Test public void searchWithRadiusReturnsOldWeatherData() { airport.getWeather().setPrecipitation(new DataPoint.Builder().withMean(50).build());; when(repository.getAllAirports()).thenReturn(Arrays.asList(airport)); List<Weather> list = target.searchAirportWeather("code1", 1000); assertEquals(1, list.size()); }