From 60072f861dd4b370ab1b4b3026494017dde3dd12 Mon Sep 17 00:00:00 2001 From: Luka Date: Wed, 31 Jul 2019 11:59:27 +0200 Subject: [PATCH] Update WeatherRepository tests --- .../ui/weather/presentation/WeatherViewModel.kt | 2 +- .../java/com/example/data/di/RepositoryModule.kt | 4 ++-- .../java/com/example/data/utils/Connectivity.kt | 11 ++--------- .../java/com/example/data/utils/ConnectivityImpl.kt | 13 +++++++++++++ .../java/com/example/data/WeatherRepositoryTest.kt | 6 ++++-- 5 files changed, 22 insertions(+), 14 deletions(-) create mode 100644 data/src/main/java/com/example/data/utils/ConnectivityImpl.kt diff --git a/app/src/main/java/com/cobeisfresh/template/ui/weather/presentation/WeatherViewModel.kt b/app/src/main/java/com/cobeisfresh/template/ui/weather/presentation/WeatherViewModel.kt index 2ce465c..8b04c69 100644 --- a/app/src/main/java/com/cobeisfresh/template/ui/weather/presentation/WeatherViewModel.kt +++ b/app/src/main/java/com/cobeisfresh/template/ui/weather/presentation/WeatherViewModel.kt @@ -15,7 +15,7 @@ import kotlinx.coroutines.launch import org.koin.core.KoinComponent class WeatherViewModel(private val getWeather: GetWeatherUseCase, - private val coroutineContextProvider: CoroutineContextProvider) : ViewModel(), KoinComponent { + private val coroutineContextProvider: CoroutineContextProvider) : ViewModel() { // we make this private and provide only immutable live data to observers so they can't change anything private val _weatherLiveData = MutableLiveData>() diff --git a/data/src/main/java/com/example/data/di/RepositoryModule.kt b/data/src/main/java/com/example/data/di/RepositoryModule.kt index 3f7d70a..652bff0 100644 --- a/data/src/main/java/com/example/data/di/RepositoryModule.kt +++ b/data/src/main/java/com/example/data/di/RepositoryModule.kt @@ -1,12 +1,12 @@ package com.example.data.di import com.example.data.repository.weather.WeatherRepositoryImpl -import com.example.data.utils.Connectivity +import com.example.data.utils.ConnectivityImpl import com.example.domain.repository.WeatherRepository import org.koin.android.ext.koin.androidContext import org.koin.dsl.module val repositoryModule = module { factory { WeatherRepositoryImpl(get(), get(), get()) } - factory { Connectivity(androidContext()) } + factory { ConnectivityImpl(androidContext()) } } \ No newline at end of file diff --git a/data/src/main/java/com/example/data/utils/Connectivity.kt b/data/src/main/java/com/example/data/utils/Connectivity.kt index 2b7762c..833b488 100644 --- a/data/src/main/java/com/example/data/utils/Connectivity.kt +++ b/data/src/main/java/com/example/data/utils/Connectivity.kt @@ -1,13 +1,6 @@ package com.example.data.utils -import android.content.Context -import android.net.ConnectivityManager - -class Connectivity(private val context: Context) { +interface Connectivity { - fun hasNetworkAccess(): Boolean { - val connectivityManager = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager - val info = connectivityManager.activeNetworkInfo - return info != null && info.isConnected - } + fun hasNetworkAccess(): Boolean } \ No newline at end of file diff --git a/data/src/main/java/com/example/data/utils/ConnectivityImpl.kt b/data/src/main/java/com/example/data/utils/ConnectivityImpl.kt new file mode 100644 index 0000000..92418a1 --- /dev/null +++ b/data/src/main/java/com/example/data/utils/ConnectivityImpl.kt @@ -0,0 +1,13 @@ +package com.example.data.utils + +import android.content.Context +import android.net.ConnectivityManager + +class ConnectivityImpl(private val context: Context) : Connectivity { + + override fun hasNetworkAccess(): Boolean { + val connectivityManager = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager + val info = connectivityManager.activeNetworkInfo + return info != null && info.isConnected + } +} \ No newline at end of file diff --git a/data/src/test/java/com/example/data/WeatherRepositoryTest.kt b/data/src/test/java/com/example/data/WeatherRepositoryTest.kt index fdef6f1..1a4b85c 100644 --- a/data/src/test/java/com/example/data/WeatherRepositoryTest.kt +++ b/data/src/test/java/com/example/data/WeatherRepositoryTest.kt @@ -13,11 +13,13 @@ class WeatherRepositoryTest { private val weatherTestApi: WeatherApi = mock() private val weatherDao: WeatherDao = mock() - private val weatherRepository = WeatherRepositoryImpl(weatherTestApi, weatherDao) + private val connectivity: Connectivity = mock() + private val weatherRepository = WeatherRepositoryImpl(weatherTestApi, weatherDao, connectivity) @Test fun `test getWeatherForLocation calls api and saves data to db upon success`() { runBlocking { + whenever(connectivity.hasNetworkAccess()).thenReturn(true) whenever(weatherTestApi.getWeatherForLocation(OSIJEK_CITY_NAME)).thenReturn(Response.success(successWeatherInfoResponse)) whenever(weatherDao.updateWeatherAndReturn(successWeatherInfoResponse.mapToRoomEntity())).thenReturn(fakeWeatherEntity) weatherRepository.getWeatherForLocation(OSIJEK_CITY_NAME) @@ -29,11 +31,11 @@ class WeatherRepositoryTest { @Test fun `test getWeatherForLocation calls api and returns cached data from db upon failure`() { runBlocking { + whenever(connectivity.hasNetworkAccess()).thenReturn(true) whenever(weatherTestApi.getWeatherForLocation(OSIJEK_CITY_NAME)) .thenReturn(Response.error(FAKE_FAILURE_ERROR_CODE, failureResponseBody)) weatherRepository.getWeatherForLocation(OSIJEK_CITY_NAME) verify(weatherTestApi, times(1)).getWeatherForLocation(OSIJEK_CITY_NAME) -// verify(weatherDao, times(1)).getWeatherInfoForCity(OSIJEK_CITY_NAME) verify(weatherDao, never()).updateWeatherAndReturn(fakeWeatherEntity) } }