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 -> {
FileUtils.readFromResource("/queries/radius_query_for_item_count.rq")
.replace("\${LAT}", String.format(Locale.ROOT, "%.4f", queryParams.center.latitude))
.replace("\${LONG}", String.format(Locale.ROOT, "%.4f", queryParams.center.longitude))
.replace(
"\${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))
}
}
@ -378,34 +384,72 @@ class OkHttpJsonApiClient @Inject constructor(
@Throws(Exception::class)
fun getNearbyPlaces(
screenTopRight: LatLng,
screenBottomLeft: LatLng, language: String,
queryParams: NearbyQueryParams, language: String,
shouldQueryForMonuments: Boolean, customQuery: String?
): List<Place>? {
Timber.d("CUSTOM_SPARQL: %s", (customQuery != null).toString())
val locale = Locale.ROOT;
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
} 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")
} else {
FileUtils.readFromResource("/queries/rectangle_query_for_nearby_monuments.rq")
}
val westCornerLat = screenTopRight.latitude
val westCornerLong = screenTopRight.longitude
val eastCornerLat = screenBottomLeft.latitude
val eastCornerLong = screenBottomLeft.longitude
val query = wikidataQuery
.replace("\${LAT_WEST}", String.format(Locale.ROOT, "%.4f", westCornerLat))
.replace("\${LONG_WEST}", String.format(Locale.ROOT, "%.4f", westCornerLong))
.replace("\${LAT_EAST}", String.format(Locale.ROOT, "%.4f", eastCornerLat))
.replace("\${LONG_EAST}", String.format(Locale.ROOT, "%.4f", eastCornerLong))
val westCornerLat = queryParams.screenTopRight.latitude
val westCornerLong = queryParams.screenTopRight.longitude
val eastCornerLat = queryParams.screenBottomLeft.latitude
val eastCornerLong = queryParams.screenBottomLeft.longitude
placeHolderQuery
.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)
}
}
val urlBuilder: HttpUrl.Builder = sparqlQueryUrl.toHttpUrlOrNull()!!
.newBuilder()
.addQueryParameter("query", query)
.addQueryParameter("query", wikidataQuery)
.addQueryParameter("format", "json")
val request: Request = Request.Builder()

View file

@ -121,7 +121,9 @@ public class NearbyPlaces {
@Nullable final String customQuery) throws Exception {
if (customQuery != null) {
return okHttpJsonApiClient
.getNearbyPlaces(screenTopRight, screenBottomLeft, lang, shouldQueryForMonuments,
.getNearbyPlaces(
new NearbyQueryParams.Rectangular(screenTopRight, screenBottomLeft), lang,
shouldQueryForMonuments,
customQuery);
}
@ -139,7 +141,8 @@ public class NearbyPlaces {
final int itemCount = okHttpJsonApiClient.getNearbyItemCount(
new NearbyQueryParams.Rectangular(screenTopRight, screenBottomLeft));
if (itemCount < upperLimit) {
return okHttpJsonApiClient.getNearbyPlaces(screenTopRight, screenBottomLeft, lang,
return okHttpJsonApiClient.getNearbyPlaces(
new NearbyQueryParams.Rectangular(screenTopRight, screenBottomLeft), lang,
shouldQueryForMonuments, null);
}
}
@ -163,7 +166,9 @@ public class NearbyPlaces {
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.location.LatLng
import fr.free.nrw.commons.mwapi.OkHttpJsonApiClient
import fr.free.nrw.commons.nearby.model.NearbyQueryParams
import okhttp3.Call
import okhttp3.HttpUrl
import okhttp3.OkHttpClient
@ -14,6 +15,7 @@ import org.junit.Before
import org.junit.Test
import org.mockito.Mock
import org.mockito.Mockito
import org.mockito.Mockito.times
import org.mockito.MockitoAnnotations
import java.lang.Exception
@ -64,12 +66,16 @@ class OkHttpJsonApiClientTests {
Mockito.`when`(response.message).thenReturn("test")
try {
okHttpJsonApiClient.getNearbyPlaces(latLng, "test", 10.0, "test")
okHttpJsonApiClient.getNearbyPlaces(latLng, latLng, "test", true, "test")
} catch (e: Exception) {
assert(e.message.equals("test"))
}
verify(okhttpClient).newCall(any())
verify(call).execute()
try {
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
@ -77,11 +83,48 @@ class OkHttpJsonApiClientTests {
Mockito.`when`(response.message).thenReturn("test")
try {
okHttpJsonApiClient.getNearbyPlaces(latLng, "test", 10.0, null)
okHttpJsonApiClient.getNearbyPlaces(latLng, latLng, "test", true, null)
} catch (e: Exception) {
assert(e.message.equals("test"))
}
verify(okhttpClient).newCall(any())
verify(call).execute()
try {
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()
}
}