diff --git a/app/src/main/java/gq/kirmanak/mealient/data/recipes/impl/RecipeImageLoaderImpl.kt b/app/src/main/java/gq/kirmanak/mealient/data/recipes/impl/RecipeImageLoaderImpl.kt index 1c15bd7..0f33b44 100644 --- a/app/src/main/java/gq/kirmanak/mealient/data/recipes/impl/RecipeImageLoaderImpl.kt +++ b/app/src/main/java/gq/kirmanak/mealient/data/recipes/impl/RecipeImageLoaderImpl.kt @@ -1,21 +1,37 @@ package gq.kirmanak.mealient.data.recipes.impl import android.widget.ImageView +import androidx.annotation.VisibleForTesting import gq.kirmanak.mealient.R import gq.kirmanak.mealient.data.auth.AuthRepo import gq.kirmanak.mealient.data.recipes.RecipeImageLoader import gq.kirmanak.mealient.ui.ImageLoader +import okhttp3.HttpUrl.Companion.toHttpUrlOrNull +import timber.log.Timber import javax.inject.Inject class RecipeImageLoaderImpl @Inject constructor( private val imageLoader: ImageLoader, private val authRepo: AuthRepo ): RecipeImageLoader { + override suspend fun loadRecipeImage(view: ImageView, slug: String?) { - val baseUrl = authRepo.getBaseUrl() - val recipeImageUrl = - if (baseUrl.isNullOrBlank() || slug.isNullOrBlank()) null - else "$baseUrl/api/media/recipes/$slug/images/original.webp" - imageLoader.loadImage(recipeImageUrl, R.drawable.placeholder_recipe, view) + Timber.v("loadRecipeImage() called with: view = $view, slug = $slug") + imageLoader.loadImage(generateImageUrl(slug), R.drawable.placeholder_recipe, view) + } + + @VisibleForTesting + suspend fun generateImageUrl(slug: String?): String? { + Timber.v("generateImageUrl() called with: slug = $slug") + val result = authRepo.getBaseUrl() + ?.takeIf { it.isNotBlank() } + ?.takeUnless { slug.isNullOrBlank() } + ?.toHttpUrlOrNull() + ?.newBuilder() + ?.addPathSegments("api/media/recipes/$slug/images/original.webp") + ?.build() + ?.toString() + Timber.v("generateImageUrl() returned: $result") + return result } } \ No newline at end of file diff --git a/app/src/test/java/gq/kirmanak/mealient/data/recipes/impl/RecipeImageLoaderImplTest.kt b/app/src/test/java/gq/kirmanak/mealient/data/recipes/impl/RecipeImageLoaderImplTest.kt new file mode 100644 index 0000000..8a0ada1 --- /dev/null +++ b/app/src/test/java/gq/kirmanak/mealient/data/recipes/impl/RecipeImageLoaderImplTest.kt @@ -0,0 +1,75 @@ +package gq.kirmanak.mealient.data.recipes.impl + +import com.google.common.truth.Truth.assertThat +import dagger.hilt.android.testing.HiltAndroidTest +import gq.kirmanak.mealient.data.auth.AuthStorage +import gq.kirmanak.mealient.test.AuthImplTestData.TEST_TOKEN +import gq.kirmanak.mealient.test.AuthImplTestData.TEST_URL +import gq.kirmanak.mealient.test.HiltRobolectricTest +import kotlinx.coroutines.runBlocking +import org.junit.Test +import javax.inject.Inject + +@HiltAndroidTest +class RecipeImageLoaderImplTest : HiltRobolectricTest() { + @Inject + lateinit var subject: RecipeImageLoaderImpl + + @Inject + lateinit var authStorage: AuthStorage + + @Test + fun `when url has slash then generated doesn't add new`() = runBlocking { + authStorage.storeAuthData(TEST_TOKEN, "https://google.com/") + val actual = subject.generateImageUrl("cake") + assertThat(actual).isEqualTo("https://google.com/api/media/recipes/cake/images/original.webp") + } + + @Test + fun `when url doesn't have slash then generated adds new`() = runBlocking { + authStorage.storeAuthData(TEST_TOKEN, "https://google.com") + val actual = subject.generateImageUrl("cake") + assertThat(actual).isEqualTo("https://google.com/api/media/recipes/cake/images/original.webp") + } + + @Test + fun `when url is null then generated is null`() = runBlocking { + val actual = subject.generateImageUrl("cake") + assertThat(actual).isNull() + } + + @Test + fun `when url is blank then generated is null`() = runBlocking { + authStorage.storeAuthData(TEST_TOKEN, " ") + val actual = subject.generateImageUrl("cake") + assertThat(actual).isNull() + } + + @Test + fun `when url is empty then generated is null`() = runBlocking { + authStorage.storeAuthData(TEST_TOKEN, "") + val actual = subject.generateImageUrl("cake") + assertThat(actual).isNull() + } + + @Test + fun `when slug is empty then generated is null`() = runBlocking { + authStorage.storeAuthData(TEST_TOKEN, TEST_URL) + val actual = subject.generateImageUrl("") + assertThat(actual).isNull() + } + + @Test + fun `when slug is blank then generated is null`() = runBlocking { + authStorage.storeAuthData(TEST_TOKEN, TEST_URL) + val actual = subject.generateImageUrl(" ") + assertThat(actual).isNull() + } + + @Test + fun `when slug is null then generated is null`() = runBlocking { + authStorage.storeAuthData(TEST_TOKEN, TEST_URL) + val actual = subject.generateImageUrl(null) + assertThat(actual).isNull() + } +} \ No newline at end of file