Skip to content

Commit

Permalink
Include Guice
Browse files Browse the repository at this point in the history
  • Loading branch information
birelian committed Oct 4, 2019
1 parent 9212f98 commit 5624e22
Show file tree
Hide file tree
Showing 7 changed files with 179 additions and 76 deletions.
22 changes: 19 additions & 3 deletions pom.xml
@@ -1,24 +1,34 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>team.codium</groupId>
<artifactId>weather-katas-java</artifactId>
<version>0.0.1-SNAPSHOT</version>
<groupId>net.birelian</groupId>
<artifactId>weather-kata</artifactId>
<version>1.0.0</version>

<properties>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.source>1.8</maven.compiler.source>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>

<commons-lang3.version>3.9</commons-lang3.version>
<gson.version>2.8.5</gson.version>
<hamcrest-all.version>1.3</hamcrest-all.version>
<junit.version>4.12</junit.version>
<junit-jupiter-api.version>RELEASE</junit-jupiter-api.version>
<lombok.version>1.18.10</lombok.version>
<okhttp.version>4.2.1</okhttp.version>
<guice.version>4.2.2</guice.version>
</properties>

<dependencies>

<dependency>
<groupId>com.google.inject</groupId>
<artifactId>guice</artifactId>
<version>${guice.version}</version>
</dependency>

<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
Expand All @@ -31,6 +41,12 @@
<version>${gson.version}</version>
</dependency>

<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>${commons-lang3.version}</version>
</dependency>

<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
Expand Down
17 changes: 12 additions & 5 deletions src/main/java/net/birelian/forecast/Forecast.java
@@ -1,16 +1,23 @@
package net.birelian.forecast;

import java.time.LocalDate;
import javax.inject.Inject;
import net.birelian.forecast.model.weather.WeatherDay;
import net.birelian.forecast.service.CityService;
import net.birelian.forecast.service.WeatherService;
import net.birelian.forecast.service.impl.CityServiceImpl;
import net.birelian.forecast.service.impl.WeatherServiceImpl;

class Forecast {

private final CityService cityService = new CityServiceImpl();
private final WeatherService weatherService = new WeatherServiceImpl();
// Injected dependencies
private final CityService cityService;
private final WeatherService weatherService;

@Inject
Forecast(final CityService cityService, final WeatherService weatherService) {

this.cityService = cityService;
this.weatherService = weatherService;
}

/**
* Get the weather prediction for today
Expand All @@ -34,7 +41,7 @@ String predict(final String cityName, final boolean wind) {
*
* @return The weather prediction for today
*/
String predict(final String cityName, LocalDate date, final boolean wind) {
String predict(final String cityName, final LocalDate date, final boolean wind) {

weatherService.validateDate(date);

Expand Down
@@ -0,0 +1,26 @@
package net.birelian.forecast.config;

import com.google.inject.AbstractModule;
import net.birelian.forecast.service.CityService;
import net.birelian.forecast.service.HttpService;
import net.birelian.forecast.service.WeatherService;
import net.birelian.forecast.service.impl.CityServiceImpl;
import net.birelian.forecast.service.impl.HttpServiceImpl;
import net.birelian.forecast.service.impl.WeatherServiceImpl;

/**
* Guice dependency injection configuration module
*/
public class GuiceConfigurationModule extends AbstractModule {

@Override
protected void configure() {

// Bindings
bind(HttpService.class).to(HttpServiceImpl.class);
bind(CityService.class).to(CityServiceImpl.class);
bind(WeatherService.class).to(WeatherServiceImpl.class);

}
}

@@ -1,5 +1,6 @@
package net.birelian.forecast.service.impl;

import javax.inject.Inject;
import net.birelian.forecast.model.City;
import net.birelian.forecast.service.CityService;
import net.birelian.forecast.service.HttpService;
Expand All @@ -12,7 +13,14 @@ public class CityServiceImpl implements CityService {

private static final String SERVICE_URL = "https://www.metaweather.com/api/location/search/?query=";

private final HttpService httpService = new HttpServiceImpl();
// Injected dependencies
private final HttpService httpService;

@Inject
public CityServiceImpl(final HttpService httpService) {

this.httpService = httpService;
}

@Override
public City getCity(final String cityName) {
Expand Down
Expand Up @@ -3,6 +3,7 @@
import static org.junit.Assert.assertNotNull;

import java.time.LocalDate;
import javax.inject.Inject;
import net.birelian.forecast.model.weather.Weather;
import net.birelian.forecast.model.weather.WeatherDay;
import net.birelian.forecast.service.HttpService;
Expand All @@ -11,10 +12,17 @@

public class WeatherServiceImpl implements WeatherService {

public static final int MAX_FUTURE_DAYS = 5;
private static final String SERVICE_URL = "https://www.metaweather.com/api/location/";
private static final int MAX_FUTURE_DAYS = 5;

private final HttpService httpService = new HttpServiceImpl();
// Injected dependencies
private final HttpService httpService;

@Inject
public WeatherServiceImpl(final HttpService httpService) {

this.httpService = httpService;
}

@Override
public WeatherDay getForecast(final String woeid, final LocalDate date, final boolean wind) {
Expand Down
103 changes: 103 additions & 0 deletions src/test/java/net/birelian/forecast/ForecastTest.java
@@ -0,0 +1,103 @@
package net.birelian.forecast;

import static junit.framework.TestCase.assertTrue;
import static net.birelian.forecast.service.impl.WeatherServiceImpl.MAX_FUTURE_DAYS;
import static org.junit.Assert.assertFalse;

import com.google.inject.Guice;
import java.time.LocalDate;
import net.birelian.forecast.config.GuiceConfigurationModule;
import net.birelian.forecast.service.exception.ServiceException;
import org.apache.commons.lang3.math.NumberUtils;
import org.junit.Test;
import org.junit.platform.commons.util.StringUtils;

// https://www.metaweather.com/api/location/766273/
public class ForecastTest {

private static final String CITY_NAME = "Madrid";

private static final boolean NO_WIND = false;
private static final boolean WIND = true;

private static final LocalDate YESTERDAY = LocalDate.now().minusDays(1);
private static final LocalDate NON_VALID_FUTURE_DATE = LocalDate.now().plusDays(MAX_FUTURE_DAYS + 1);
private static final LocalDate VALID_FUTURE_DATE = LocalDate.now().plusDays(MAX_FUTURE_DAYS);

@Test(expected = AssertionError.class)
public void findTheWeatherWithANullDateShouldThrowException() {

getForecastInstance().predict(CITY_NAME, null, NO_WIND);
}

@Test(expected = AssertionError.class)
public void findTheWindWithANullDateShouldThrowException() {

getForecastInstance().predict(CITY_NAME, null, WIND);
}

@Test
public void findTheWeatherOfTodayShouldWork() {

String prediction = getForecastInstance().predict(CITY_NAME, NO_WIND);

assertTrue("Forecast should return a String when it works. Exception otherwise", StringUtils.isNotBlank(prediction));
assertFalse("Forecast should not be a number when wind parameter is false", NumberUtils.isParsable(prediction));
}

@Test
public void findTheWindOfTodayShouldWork() {

String prediction = getForecastInstance().predict(CITY_NAME, WIND);

assertTrue("Forecast should return a String when it works. Exception otherwise", StringUtils.isNotBlank(prediction));
assertTrue("Forecast wind should be parsable as Double", NumberUtils.isParsable(prediction));
}

@Test(expected = ServiceException.class)
public void findTheWeatherForAPastDateShouldThrowException() {

getForecastInstance().predict(CITY_NAME, YESTERDAY, NO_WIND);
}

@Test(expected = ServiceException.class)
public void findTheWindForAPastDateShouldThrowException() {

getForecastInstance().predict(CITY_NAME, YESTERDAY, WIND);
}

@Test
public void findTheWeatherForAValidFutureDateShouldReturnTheWeather() {

String prediction = getForecastInstance().predict(CITY_NAME, VALID_FUTURE_DATE, NO_WIND);

assertTrue("Forecast should return a String when it works. Exception otherwise", StringUtils.isNotBlank(prediction));
assertFalse("Forecast should not be a number when wind parameter is false", NumberUtils.isParsable(prediction));
}

@Test
public void findTheWindForAValidFutureDateShouldReturnTheWeather() {

String prediction = getForecastInstance().predict(CITY_NAME, VALID_FUTURE_DATE, WIND);

assertTrue("Forecast should return a String when it works. Exception otherwise", StringUtils.isNotBlank(prediction));
assertTrue("Forecast wind should be parsable as Double", NumberUtils.isParsable(prediction));
}

@Test(expected = ServiceException.class)
public void findTheWeatherForANonValidFutureDateShouldThrowException() {

getForecastInstance().predict(CITY_NAME, NON_VALID_FUTURE_DATE, NO_WIND);
}

@Test(expected = ServiceException.class)
public void findTheWindForANonValidFutureDateShouldThrowException() {

getForecastInstance().predict(CITY_NAME, NON_VALID_FUTURE_DATE, WIND);
}

private Forecast getForecastInstance() {

return Guice.createInjector(new GuiceConfigurationModule()).getInstance(Forecast.class);
}
}
65 changes: 0 additions & 65 deletions src/test/java/net/birelian/forecast/WeatherKataTest.java

This file was deleted.

0 comments on commit 5624e22

Please sign in to comment.