From 68b7b9cf5fac1b72099e271c2a8cabb87c19372d Mon Sep 17 00:00:00 2001 From: Gleb Koval Date: Mon, 18 Nov 2024 17:13:46 +0000 Subject: [PATCH] Implement WeatherComForecaster adpater for the weather.com Forecast client. Co-Authored-By: os222 --- .../java/ic/doc/WeatherComForecaster.java | 47 +++++++++++++++++ src/main/java/ic/doc/WeatherForecast.java | 10 ++-- .../java/ic/doc/WeatherComForecasterTest.java | 52 +++++++++++++++++++ 3 files changed, 106 insertions(+), 3 deletions(-) create mode 100644 src/main/java/ic/doc/WeatherComForecaster.java create mode 100644 src/test/java/ic/doc/WeatherComForecasterTest.java diff --git a/src/main/java/ic/doc/WeatherComForecaster.java b/src/main/java/ic/doc/WeatherComForecaster.java new file mode 100644 index 0000000..8aeac87 --- /dev/null +++ b/src/main/java/ic/doc/WeatherComForecaster.java @@ -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; + }; + } +} diff --git a/src/main/java/ic/doc/WeatherForecast.java b/src/main/java/ic/doc/WeatherForecast.java index 6b6256a..190efc9 100644 --- a/src/main/java/ic/doc/WeatherForecast.java +++ b/src/main/java/ic/doc/WeatherForecast.java @@ -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; + } } diff --git a/src/test/java/ic/doc/WeatherComForecasterTest.java b/src/test/java/ic/doc/WeatherComForecasterTest.java new file mode 100644 index 0000000..1aef599 --- /dev/null +++ b/src/test/java/ic/doc/WeatherComForecasterTest.java @@ -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 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 } + }); + } +} \ No newline at end of file