Add unit tests and complete implementation

This commit is contained in:
savsch 2024-12-24 02:07:43 +05:30
parent 24f4d90892
commit 42a9c7d9bc
3 changed files with 135 additions and 43 deletions

View file

@ -351,8 +351,14 @@ class OkHttpJsonApiClient @Inject constructor(
is NearbyQueryParams.Radial -> { is NearbyQueryParams.Radial -> {
FileUtils.readFromResource("/queries/radius_query_for_item_count.rq") FileUtils.readFromResource("/queries/radius_query_for_item_count.rq")
.replace("\${LAT}", String.format(Locale.ROOT, "%.4f", queryParams.center.latitude)) .replace(
.replace("\${LONG}", String.format(Locale.ROOT, "%.4f", queryParams.center.longitude)) "\${LAT}",
String.format(Locale.ROOT, "%.4f", queryParams.center.latitude)
)
.replace(
"\${LONG}",
String.format(Locale.ROOT, "%.4f", queryParams.center.longitude)
)
.replace("\${RAD}", String.format(Locale.ROOT, "%.2f", queryParams.radius)) .replace("\${RAD}", String.format(Locale.ROOT, "%.2f", queryParams.radius))
} }
} }
@ -378,34 +384,72 @@ class OkHttpJsonApiClient @Inject constructor(
@Throws(Exception::class) @Throws(Exception::class)
fun getNearbyPlaces( fun getNearbyPlaces(
screenTopRight: LatLng, queryParams: NearbyQueryParams, language: String,
screenBottomLeft: LatLng, language: String,
shouldQueryForMonuments: Boolean, customQuery: String? shouldQueryForMonuments: Boolean, customQuery: String?
): List<Place>? { ): List<Place>? {
Timber.d("CUSTOM_SPARQL: %s", (customQuery != null).toString()) Timber.d("CUSTOM_SPARQL: %s", (customQuery != null).toString())
val locale = Locale.ROOT;
val wikidataQuery: String = if (customQuery != null) { val wikidataQuery: String = if (customQuery != null) {
when (queryParams) {
is NearbyQueryParams.Rectangular -> {
val westCornerLat = queryParams.screenTopRight.latitude
val westCornerLong = queryParams.screenTopRight.longitude
val eastCornerLat = queryParams.screenBottomLeft.latitude
val eastCornerLong = queryParams.screenBottomLeft.longitude
customQuery customQuery
} else if (!shouldQueryForMonuments) { .replace("\${LAT_WEST}", String.format(locale, "%.4f", westCornerLat))
.replace("\${LONG_WEST}", String.format(locale, "%.4f", westCornerLong))
.replace("\${LAT_EAST}", String.format(locale, "%.4f", eastCornerLat))
.replace("\${LONG_EAST}", String.format(locale, "%.4f", eastCornerLong))
.replace("\${LANG}", language)
}
is NearbyQueryParams.Radial -> {
Timber.e(
"%s%s",
"okHttpJsonApiClient.getNearbyPlaces invoked with custom query",
"and radial coordinates. This is currently not supported."
)
""
}
}
} else when (queryParams) {
is NearbyQueryParams.Radial -> {
val placeHolderQuery: String = if (!shouldQueryForMonuments) {
FileUtils.readFromResource("/queries/radius_query_for_nearby.rq")
} else {
FileUtils.readFromResource("/queries/radius_query_for_nearby_monuments.rq")
}
placeHolderQuery.replace(
"\${LAT}", String.format(locale, "%.4f", queryParams.center.latitude)
).replace(
"\${LONG}", String.format(locale, "%.4f", queryParams.center.longitude)
)
.replace("\${RAD}", String.format(locale, "%.2f", queryParams.radius))
}
is NearbyQueryParams.Rectangular -> {
val placeHolderQuery: String = if (!shouldQueryForMonuments) {
FileUtils.readFromResource("/queries/rectangle_query_for_nearby.rq") FileUtils.readFromResource("/queries/rectangle_query_for_nearby.rq")
} else { } else {
FileUtils.readFromResource("/queries/rectangle_query_for_nearby_monuments.rq") FileUtils.readFromResource("/queries/rectangle_query_for_nearby_monuments.rq")
} }
val westCornerLat = queryParams.screenTopRight.latitude
val westCornerLat = screenTopRight.latitude val westCornerLong = queryParams.screenTopRight.longitude
val westCornerLong = screenTopRight.longitude val eastCornerLat = queryParams.screenBottomLeft.latitude
val eastCornerLat = screenBottomLeft.latitude val eastCornerLong = queryParams.screenBottomLeft.longitude
val eastCornerLong = screenBottomLeft.longitude placeHolderQuery
.replace("\${LAT_WEST}", String.format(locale, "%.4f", westCornerLat))
val query = wikidataQuery .replace("\${LONG_WEST}", String.format(locale, "%.4f", westCornerLong))
.replace("\${LAT_WEST}", String.format(Locale.ROOT, "%.4f", westCornerLat)) .replace("\${LAT_EAST}", String.format(locale, "%.4f", eastCornerLat))
.replace("\${LONG_WEST}", String.format(Locale.ROOT, "%.4f", westCornerLong)) .replace("\${LONG_EAST}", String.format(locale, "%.4f", eastCornerLong))
.replace("\${LAT_EAST}", String.format(Locale.ROOT, "%.4f", eastCornerLat))
.replace("\${LONG_EAST}", String.format(Locale.ROOT, "%.4f", eastCornerLong))
.replace("\${LANG}", language) .replace("\${LANG}", language)
}
}
val urlBuilder: HttpUrl.Builder = sparqlQueryUrl.toHttpUrlOrNull()!! val urlBuilder: HttpUrl.Builder = sparqlQueryUrl.toHttpUrlOrNull()!!
.newBuilder() .newBuilder()
.addQueryParameter("query", query) .addQueryParameter("query", wikidataQuery)
.addQueryParameter("format", "json") .addQueryParameter("format", "json")
val request: Request = Request.Builder() val request: Request = Request.Builder()

View file

@ -121,7 +121,9 @@ public class NearbyPlaces {
@Nullable final String customQuery) throws Exception { @Nullable final String customQuery) throws Exception {
if (customQuery != null) { if (customQuery != null) {
return okHttpJsonApiClient return okHttpJsonApiClient
.getNearbyPlaces(screenTopRight, screenBottomLeft, lang, shouldQueryForMonuments, .getNearbyPlaces(
new NearbyQueryParams.Rectangular(screenTopRight, screenBottomLeft), lang,
shouldQueryForMonuments,
customQuery); customQuery);
} }
@ -139,7 +141,8 @@ public class NearbyPlaces {
final int itemCount = okHttpJsonApiClient.getNearbyItemCount( final int itemCount = okHttpJsonApiClient.getNearbyItemCount(
new NearbyQueryParams.Rectangular(screenTopRight, screenBottomLeft)); new NearbyQueryParams.Rectangular(screenTopRight, screenBottomLeft));
if (itemCount < upperLimit) { if (itemCount < upperLimit) {
return okHttpJsonApiClient.getNearbyPlaces(screenTopRight, screenBottomLeft, lang, return okHttpJsonApiClient.getNearbyPlaces(
new NearbyQueryParams.Rectangular(screenTopRight, screenBottomLeft), lang,
shouldQueryForMonuments, null); shouldQueryForMonuments, null);
} }
} }
@ -163,7 +166,9 @@ public class NearbyPlaces {
maxRadius = targetRadius - 1; maxRadius = targetRadius - 1;
} }
} }
return new java.util.ArrayList<Place>(); return okHttpJsonApiClient.getNearbyPlaces(
new NearbyQueryParams.Radial(centerPoint, targetRadius / 100f), lang, shouldQueryForMonuments,
null);
} }
/** /**

View file

@ -6,6 +6,7 @@ import com.nhaarman.mockitokotlin2.verify
import fr.free.nrw.commons.explore.depictions.DepictsClient import fr.free.nrw.commons.explore.depictions.DepictsClient
import fr.free.nrw.commons.location.LatLng import fr.free.nrw.commons.location.LatLng
import fr.free.nrw.commons.mwapi.OkHttpJsonApiClient import fr.free.nrw.commons.mwapi.OkHttpJsonApiClient
import fr.free.nrw.commons.nearby.model.NearbyQueryParams
import okhttp3.Call import okhttp3.Call
import okhttp3.HttpUrl import okhttp3.HttpUrl
import okhttp3.OkHttpClient import okhttp3.OkHttpClient
@ -14,6 +15,7 @@ import org.junit.Before
import org.junit.Test import org.junit.Test
import org.mockito.Mock import org.mockito.Mock
import org.mockito.Mockito import org.mockito.Mockito
import org.mockito.Mockito.times
import org.mockito.MockitoAnnotations import org.mockito.MockitoAnnotations
import java.lang.Exception import java.lang.Exception
@ -64,12 +66,16 @@ class OkHttpJsonApiClientTests {
Mockito.`when`(response.message).thenReturn("test") Mockito.`when`(response.message).thenReturn("test")
try { try {
okHttpJsonApiClient.getNearbyPlaces(latLng, "test", 10.0, "test") okHttpJsonApiClient.getNearbyPlaces(latLng, "test", 10.0, "test")
okHttpJsonApiClient.getNearbyPlaces(latLng, latLng, "test", true, "test")
} catch (e: Exception) { } catch (e: Exception) {
assert(e.message.equals("test")) assert(e.message.equals("test"))
} }
verify(okhttpClient).newCall(any()) try {
verify(call).execute() okHttpJsonApiClient.getNearbyPlaces(NearbyQueryParams.Rectangular(latLng, latLng), "test", true, "test")
} catch (e: Exception) {
assert(e.message.equals("test"))
}
verify(okhttpClient, times(2)).newCall(any())
verify(call, times(2)).execute()
} }
@Test @Test
@ -77,11 +83,48 @@ class OkHttpJsonApiClientTests {
Mockito.`when`(response.message).thenReturn("test") Mockito.`when`(response.message).thenReturn("test")
try { try {
okHttpJsonApiClient.getNearbyPlaces(latLng, "test", 10.0, null) okHttpJsonApiClient.getNearbyPlaces(latLng, "test", 10.0, null)
okHttpJsonApiClient.getNearbyPlaces(latLng, latLng, "test", true, null)
} catch (e: Exception) { } catch (e: Exception) {
assert(e.message.equals("test")) assert(e.message.equals("test"))
} }
verify(okhttpClient).newCall(any()) try {
verify(call).execute() okHttpJsonApiClient.getNearbyPlaces(
NearbyQueryParams.Rectangular(latLng, latLng),
"test",
true,
null
)
} catch (e: Exception) {
assert(e.message.equals("test"))
}
try {
okHttpJsonApiClient.getNearbyPlaces(
NearbyQueryParams.Radial(latLng, 10f),
"test",
true,
null
)
} catch (e: Exception) {
assert(e.message.equals("test"))
}
verify(okhttpClient, times(3)).newCall(any())
verify(call, times(3)).execute()
}
@Test
fun testGetNearbyItemCount() {
Mockito.`when`(response.message).thenReturn("test")
try {
okHttpJsonApiClient.getNearbyItemCount(NearbyQueryParams.Radial(latLng, 10f))
} catch (e: Exception) {
assert(e.message.equals("test"))
}
try {
okHttpJsonApiClient.getNearbyItemCount(NearbyQueryParams.Rectangular(latLng, latLng))
} catch (e: Exception) {
assert(e.message.equals("test"))
}
verify(okhttpClient, times(2)).newCall(any())
verify(call, times(2)).execute()
} }
} }