From bab3830057bf6f80cc09719e2b653684883dacbb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Enrique=20Lo=CC=81pez=20Man=CC=83as?= Date: Mon, 31 Jul 2023 07:13:45 +0200 Subject: [PATCH] chore: comments from the Code Review --- README.md | 14 +++++++ library/build.gradle | 1 + .../com/google/maps/android/StreetViewUtil.kt | 6 +-- .../google/maps/android/StreetViewUtilTest.kt | 39 +++++++++++++++++++ 4 files changed, 56 insertions(+), 4 deletions(-) create mode 100644 library/src/test/java/com/google/maps/android/StreetViewUtilTest.kt diff --git a/README.md b/README.md index f6d310aad..817ec934b 100644 --- a/README.md +++ b/README.md @@ -290,6 +290,20 @@ _Old_ +## Usage guide + +The full documentation can be found here [Google Maps Platform documentation][devsite-guide]. + +For a quick snippet on the StreetViewUtil class, keep reading. + +The StreetViewUtil class provides functionality to check whether a location is supported in StreetView. To call it, use the following snippet: + +```kotlin +StreetViewUtils.fetchStreetViewData(LatLng(8.1425918, 11.5386121), BuildConfig.MAPS_API_KEY) +``` + +`fetchStreetViewData` will return `NOT_FOUND`, `OK` or `ZERO_RESULTS`, depending on the response. + ## Support Encounter an issue while using this library? diff --git a/library/build.gradle b/library/build.gradle index 69f30f94a..6c846ac37 100644 --- a/library/build.gradle +++ b/library/build.gradle @@ -64,6 +64,7 @@ dependencies { testImplementation 'junit:junit:4.13.2' testImplementation 'org.robolectric:robolectric:4.10.3' testImplementation 'net.sf.kxml:kxml2:2.3.0' + testImplementation "io.mockk:mockk:1.13.4" implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version" } diff --git a/library/src/main/java/com/google/maps/android/StreetViewUtil.kt b/library/src/main/java/com/google/maps/android/StreetViewUtil.kt index cbd4ba33f..945648aae 100644 --- a/library/src/main/java/com/google/maps/android/StreetViewUtil.kt +++ b/library/src/main/java/com/google/maps/android/StreetViewUtil.kt @@ -39,7 +39,7 @@ class StreetViewUtils { * @param apiKey Maps API Key * @return A boolean value specifying if the location is available on Street View or not. */ - suspend fun fetchStreetViewData(latLng: LatLng, apiKey: String): Boolean { + suspend fun fetchStreetViewData(latLng: LatLng, apiKey: String): Status { val urlString = "https://maps.googleapis.com/maps/api/streetview/metadata?location=${latLng.latitude},${latLng.longitude}&key=$apiKey" @@ -56,9 +56,7 @@ class StreetViewUtils { val responseString = bufferedReader.use { it.readText() } bufferedReader.close() inputStream.close() - - val response: ResponseStreetView = deserializeResponse(responseString) - response.status == Status.OK + deserializeResponse(responseString).status } else { throw IOException("HTTP Error: $responseCode") } diff --git a/library/src/test/java/com/google/maps/android/StreetViewUtilTest.kt b/library/src/test/java/com/google/maps/android/StreetViewUtilTest.kt new file mode 100644 index 000000000..0a150f768 --- /dev/null +++ b/library/src/test/java/com/google/maps/android/StreetViewUtilTest.kt @@ -0,0 +1,39 @@ +package com.google.maps.android + +import com.google.android.gms.maps.model.LatLng +import io.mockk.coEvery +import io.mockk.mockkObject +import kotlinx.coroutines.runBlocking +import org.junit.Assert.assertEquals +import org.junit.Before +import org.junit.Test + +class StreetViewUtilsTest { + + lateinit var latLng : LatLng + + val apiKey = "AN_API_KEY" + + @Before + fun setUp() { + latLng = LatLng(37.7749, -122.4194) // San Francisco coordinates + + // Mock the network behavior using MockK + mockkObject(StreetViewUtils) + coEvery { StreetViewUtils.fetchStreetViewData(any(), any()) } returns Status.NOT_FOUND + coEvery { StreetViewUtils.fetchStreetViewData(latLng, apiKey) } returns Status.OK + } + + @Test + fun testLocationFoundOnStreetView() = runBlocking { + val status = StreetViewUtils.fetchStreetViewData(latLng, apiKey) + assertEquals(Status.OK, status) + } + + @Test + fun testLocationNotFoundOnStreetView() = runBlocking { + val status = StreetViewUtils.fetchStreetViewData(LatLng(10.0, 20.0), apiKey) + assertEquals(Status.NOT_FOUND, status) + } +} +