Implement WeatherComForecaster adpater for the weather.com Forecast client.
Co-Authored-By: os222
This commit is contained in:
parent
8d6c6415ac
commit
68b7b9cf5f
|
@ -0,0 +1,47 @@
|
|||
package ic.doc;
|
||||
|
||||
import com.weather.Day;
|
||||
import com.weather.Forecast;
|
||||
import com.weather.Forecaster;
|
||||
import com.weather.Region;
|
||||
|
||||
// Adapter for weather.com's Forecaster API.
|
||||
public class WeatherComForecaster implements WeatherForecaster {
|
||||
private final Forecaster forecaster;
|
||||
|
||||
WeatherComForecaster(Forecaster forecaster) {
|
||||
this.forecaster = forecaster;
|
||||
}
|
||||
|
||||
@Override
|
||||
public WeatherForecast forecastFor(WeatherRegion region, Weekday day) {
|
||||
Forecast forecast = forecaster.forecastFor(convertWeatherRegion(region), convertWeekday(day));
|
||||
return new WeatherForecast(forecast.summary(), forecast.temperature());
|
||||
}
|
||||
|
||||
public static Region convertWeatherRegion(WeatherRegion region) {
|
||||
return switch (region) {
|
||||
case BIRMINGHAM -> Region.BIRMINGHAM;
|
||||
case EDINBURGH -> Region.EDINBURGH;
|
||||
case GLASGOW -> Region.GLASGOW;
|
||||
case LONDON -> Region.LONDON;
|
||||
case MANCHESTER -> Region.MANCHESTER;
|
||||
case NORTH_ENGLAND -> Region.NORTH_ENGLAND;
|
||||
case SOUTH_WEST_ENGLAND -> Region.SOUTH_WEST_ENGLAND;
|
||||
case SOUTH_EAST_ENGLAND -> Region.SOUTH_EAST_ENGLAND;
|
||||
case WALES -> Region.WALES;
|
||||
};
|
||||
}
|
||||
|
||||
public static Day convertWeekday(Weekday day) {
|
||||
return switch (day) {
|
||||
case MONDAY -> Day.MONDAY;
|
||||
case TUESDAY -> Day.TUESDAY;
|
||||
case WEDNESDAY -> Day.WEDNESDAY;
|
||||
case THURSDAY -> Day.THURSDAY;
|
||||
case FRIDAY -> Day.FRIDAY;
|
||||
case SATURDAY -> Day.SATURDAY;
|
||||
case SUNDAY -> Day.SUNDAY;
|
||||
};
|
||||
}
|
||||
}
|
|
@ -1,7 +1,11 @@
|
|||
package ic.doc;
|
||||
|
||||
public interface WeatherForecast {
|
||||
String summary();
|
||||
public record WeatherForecast(String summary, int temperature) {
|
||||
public String summary() {
|
||||
return summary;
|
||||
}
|
||||
|
||||
int temperature();
|
||||
public int temperature() {
|
||||
return temperature;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,52 @@
|
|||
package ic.doc;
|
||||
|
||||
import com.weather.Forecast;
|
||||
import com.weather.Forecaster;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.junit.runners.Parameterized;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
// Integration test for WeatherComForecaster adapter
|
||||
@RunWith(Parameterized.class)
|
||||
public class WeatherComForecasterTest {
|
||||
private final WeatherRegion region;
|
||||
private final Weekday day;
|
||||
private final Forecaster forecaster = new Forecaster();
|
||||
private final WeatherComForecaster weatherComForecaster = new WeatherComForecaster(forecaster);
|
||||
|
||||
public WeatherComForecasterTest(WeatherRegion region, Weekday day) {
|
||||
this.region = region;
|
||||
this.day = day;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testForecastFor() {
|
||||
Forecast forecastExp = forecaster.forecastFor(
|
||||
WeatherComForecaster.convertWeatherRegion(region),
|
||||
WeatherComForecaster.convertWeekday(day)
|
||||
);
|
||||
WeatherForecast forecastAct = weatherComForecaster.forecastFor(region, day);
|
||||
assertEquals(forecastExp.summary(), forecastAct.summary());
|
||||
assertEquals(forecastExp.temperature(), forecastAct.temperature());
|
||||
}
|
||||
|
||||
@Parameterized.Parameters
|
||||
public static Collection<Object[]> data() {
|
||||
return Arrays.asList(new Object[][] {
|
||||
{ WeatherRegion.BIRMINGHAM, Weekday.MONDAY },
|
||||
{ WeatherRegion.EDINBURGH, Weekday.TUESDAY },
|
||||
{ WeatherRegion.GLASGOW, Weekday.WEDNESDAY },
|
||||
{ WeatherRegion.LONDON, Weekday.THURSDAY },
|
||||
{ WeatherRegion.MANCHESTER, Weekday.FRIDAY },
|
||||
{ WeatherRegion.NORTH_ENGLAND, Weekday.SATURDAY },
|
||||
{ WeatherRegion.SOUTH_WEST_ENGLAND, Weekday.SUNDAY },
|
||||
{ WeatherRegion.SOUTH_EAST_ENGLAND, Weekday.MONDAY },
|
||||
{ WeatherRegion.WALES, Weekday.TUESDAY }
|
||||
});
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue