feature: added unit tests
This commit is contained in:
parent
929e686698
commit
e794c29eff
@ -12,6 +12,7 @@ repositories {
|
|||||||
dependencies {
|
dependencies {
|
||||||
testImplementation platform('org.junit:junit-bom:5.10.0')
|
testImplementation platform('org.junit:junit-bom:5.10.0')
|
||||||
testImplementation 'org.junit.jupiter:junit-jupiter'
|
testImplementation 'org.junit.jupiter:junit-jupiter'
|
||||||
|
testImplementation 'org.mockito:mockito-core:5.18.0'
|
||||||
implementation 'org.json:json:20250517'
|
implementation 'org.json:json:20250517'
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -5,12 +5,8 @@ import ru.dima.weather.cli.CLIInputReader;
|
|||||||
import ru.dima.weather.cli.WeatherProviderCLISelector;
|
import ru.dima.weather.cli.WeatherProviderCLISelector;
|
||||||
import ru.dima.weather.http.HttpRequester;
|
import ru.dima.weather.http.HttpRequester;
|
||||||
import ru.dima.weather.provider.OpenWeatherMapWeatherProvider;
|
import ru.dima.weather.provider.OpenWeatherMapWeatherProvider;
|
||||||
import ru.dima.weather.provider.WeatherProvider;
|
|
||||||
import ru.dima.weather.provider.WeatherProviderRegistry;
|
import ru.dima.weather.provider.WeatherProviderRegistry;
|
||||||
|
|
||||||
import java.util.Scanner;
|
|
||||||
import java.util.stream.Collectors;
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
1. Брать текущий IP
|
1. Брать текущий IP
|
||||||
2. Определять город по текущему IP
|
2. Определять город по текущему IP
|
||||||
|
@ -13,6 +13,8 @@ public class CityByIpResolver {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public String getCurrentCityViaIP() {
|
public String getCurrentCityViaIP() {
|
||||||
return httpRequester.getString(IP_INFO_URL).replace("\n", "").replace("\r", "");
|
return httpRequester.getString(IP_INFO_URL)
|
||||||
|
.replace("\n", "")
|
||||||
|
.replace("\r", "");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -6,6 +6,7 @@ import ru.dima.weather.http.HttpRequester;
|
|||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
|
||||||
public class OpenWeatherMapWeatherProvider implements WeatherProvider {
|
public class OpenWeatherMapWeatherProvider implements WeatherProvider {
|
||||||
|
|
||||||
private HttpRequester httpRequester;
|
private HttpRequester httpRequester;
|
||||||
|
24
src/test/java/ru/dima/weather/city/CityByIpResolverTest.java
Normal file
24
src/test/java/ru/dima/weather/city/CityByIpResolverTest.java
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
package ru.dima.weather.city;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.mockito.Mockito;
|
||||||
|
import ru.dima.weather.http.HttpRequester;
|
||||||
|
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
|
||||||
|
class CityByIpResolverTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void mustReturnPlaintextCityFromIp() {
|
||||||
|
var mockHttpRequester = Mockito.mock(HttpRequester.class);
|
||||||
|
var cityByIpResolver = new CityByIpResolver(mockHttpRequester);
|
||||||
|
var randomCityName = UUID.randomUUID().toString();
|
||||||
|
|
||||||
|
Mockito.when(mockHttpRequester.getString(Mockito.any())).thenReturn(randomCityName);
|
||||||
|
|
||||||
|
var currentCity = cityByIpResolver.getCurrentCityViaIP();
|
||||||
|
assertEquals(randomCityName, currentCity);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user