mirror of
https://github.com/commons-app/apps-android-commons.git
synced 2025-10-27 21:03:54 +01:00
OkHttpJsonApi#getMediaList migrated to retrofit (#3054)
* Migrated OkHttpJsonApi#getMediaList partially to MediaClient#getCategoryImages * Migrated rest of OkHttpJsonApi#getMediaList functionality to MediaClient#getCategoryImages * Removed unused code and tests * Fixed small bug * Added tests * Removed unused CategoryImageController
This commit is contained in:
parent
d5198be3e3
commit
97122296dd
9 changed files with 153 additions and 331 deletions
|
|
@ -1,20 +1,24 @@
|
|||
package fr.free.nrw.commons.media
|
||||
|
||||
import fr.free.nrw.commons.Media
|
||||
import io.reactivex.Observable
|
||||
import junit.framework.Assert.assertFalse
|
||||
import junit.framework.Assert.assertTrue
|
||||
import junit.framework.Assert.*
|
||||
import org.junit.Before
|
||||
import org.junit.Test
|
||||
import org.mockito.ArgumentMatchers
|
||||
import org.mockito.InjectMocks
|
||||
import org.mockito.Mock
|
||||
import org.mockito.*
|
||||
import org.mockito.Mockito.`when`
|
||||
import org.mockito.Mockito.mock
|
||||
import org.mockito.MockitoAnnotations
|
||||
import org.wikipedia.dataclient.mwapi.ImageDetails
|
||||
import org.wikipedia.dataclient.mwapi.MwQueryPage
|
||||
import org.wikipedia.dataclient.mwapi.MwQueryResponse
|
||||
import org.wikipedia.dataclient.mwapi.MwQueryResult
|
||||
import org.wikipedia.gallery.ImageInfo
|
||||
import org.mockito.ArgumentCaptor
|
||||
import java.util.*
|
||||
import org.mockito.Captor
|
||||
|
||||
|
||||
|
||||
|
||||
class MediaClientTest {
|
||||
|
||||
|
|
@ -97,4 +101,36 @@ class MediaClientTest {
|
|||
val checkFileExistsUsingSha = mediaClient!!.checkFileExistsUsingSha("abcde").blockingGet()
|
||||
assertFalse(checkFileExistsUsingSha)
|
||||
}
|
||||
|
||||
@Captor
|
||||
private val continuationCaptor: ArgumentCaptor<Map<String, String>>? = null
|
||||
|
||||
@Test
|
||||
fun getMediaListFromCategoryTwice() {
|
||||
val mockContinuation= mapOf(Pair("gcmcontinue", "test"))
|
||||
val imageInfo = ImageInfo()
|
||||
|
||||
val mwQueryPage = mock(MwQueryPage::class.java)
|
||||
`when`(mwQueryPage.title()).thenReturn("Test")
|
||||
`when`(mwQueryPage.imageInfo()).thenReturn(imageInfo)
|
||||
|
||||
val mwQueryResult = mock(MwQueryResult::class.java)
|
||||
`when`(mwQueryResult.pages()).thenReturn(listOf(mwQueryPage))
|
||||
|
||||
val mockResponse = mock(MwQueryResponse::class.java)
|
||||
`when`(mockResponse.query()).thenReturn(mwQueryResult)
|
||||
`when`(mockResponse.continuation()).thenReturn(mockContinuation)
|
||||
|
||||
`when`(mediaInterface!!.getMediaListFromCategory(ArgumentMatchers.anyString(), ArgumentMatchers.anyInt(),
|
||||
continuationCaptor!!.capture()))
|
||||
.thenReturn(Observable.just(mockResponse))
|
||||
val media1 = mediaClient!!.getMediaListFromCategory("abcde").blockingGet().get(0)
|
||||
val media2 = mediaClient!!.getMediaListFromCategory("abcde").blockingGet().get(0)
|
||||
|
||||
assertEquals(continuationCaptor.allValues[0], emptyMap<String, String>())
|
||||
assertEquals(continuationCaptor.allValues[1], mockContinuation)
|
||||
|
||||
assertEquals(media1.filename, "Test")
|
||||
assertEquals(media2.filename, "Test")
|
||||
}
|
||||
}
|
||||
|
|
@ -4,9 +4,7 @@ import com.google.gson.Gson
|
|||
import fr.free.nrw.commons.Media
|
||||
import fr.free.nrw.commons.TestCommonsApplication
|
||||
import fr.free.nrw.commons.kvstore.JsonKvStore
|
||||
import fr.free.nrw.commons.mwapi.OkHttpJsonApiClient.mapType
|
||||
import fr.free.nrw.commons.utils.CommonsDateUtil
|
||||
import junit.framework.Assert.assertEquals
|
||||
import okhttp3.HttpUrl
|
||||
import okhttp3.OkHttpClient
|
||||
import okhttp3.mockwebserver.MockResponse
|
||||
|
|
@ -18,7 +16,6 @@ import org.junit.Before
|
|||
import org.junit.Test
|
||||
import org.junit.runner.RunWith
|
||||
import org.mockito.Mockito
|
||||
import org.mockito.Mockito.`when`
|
||||
import org.robolectric.RobolectricTestRunner
|
||||
import org.robolectric.annotation.Config
|
||||
import java.util.*
|
||||
|
|
@ -55,7 +52,7 @@ class OkHttpJsonApiClientTest {
|
|||
val sparqlUrl = "http://" + sparqlServer.hostName + ":" + sparqlServer.port + "/"
|
||||
val campaignsUrl = "http://" + campaignsServer.hostName + ":" + campaignsServer.port + "/"
|
||||
val serverUrl = "http://" + server.hostName + ":" + server.port + "/"
|
||||
testObject = OkHttpJsonApiClient(okHttpClient, HttpUrl.get(toolsForgeUrl), sparqlUrl, campaignsUrl, serverUrl, sharedPreferences, Gson())
|
||||
testObject = OkHttpJsonApiClient(okHttpClient, HttpUrl.get(toolsForgeUrl), sparqlUrl, campaignsUrl, serverUrl, Gson())
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -69,96 +66,6 @@ class OkHttpJsonApiClientTest {
|
|||
campaignsServer.shutdown()
|
||||
}
|
||||
|
||||
/**
|
||||
* Test response for category images
|
||||
*/
|
||||
@Test
|
||||
fun getCategoryImages() {
|
||||
server.enqueue(getFirstPageOfImages())
|
||||
testFirstPageQuery()
|
||||
}
|
||||
|
||||
/**
|
||||
* test paginated response for category images
|
||||
*/
|
||||
@Test
|
||||
fun getCategoryImagesWithContinue() {
|
||||
server.enqueue(getFirstPageOfImages())
|
||||
server.enqueue(getSecondPageOfImages())
|
||||
testFirstPageQuery()
|
||||
|
||||
`when`(sharedPreferences.getJson<HashMap<String, String>>("query_continue_Watercraft moored off shore", mapType))
|
||||
.thenReturn(hashMapOf(Pair("gcmcontinue", "testvalue"), Pair("continue", "gcmcontinue||")))
|
||||
|
||||
|
||||
val categoryImagesContinued = testObject.getMediaList("category", "Watercraft moored off shore")!!.blockingGet()
|
||||
|
||||
assertBasicRequestParameters(server, "GET").let { request ->
|
||||
parseQueryParams(request).let { body ->
|
||||
Assert.assertEquals("json", body["format"])
|
||||
Assert.assertEquals("2", body["formatversion"])
|
||||
Assert.assertEquals("query", body["action"])
|
||||
Assert.assertEquals("categorymembers", body["generator"])
|
||||
Assert.assertEquals("file", body["gcmtype"])
|
||||
Assert.assertEquals("Watercraft moored off shore", body["gcmtitle"])
|
||||
Assert.assertEquals("timestamp", body["gcmsort"])
|
||||
Assert.assertEquals("desc", body["gcmdir"])
|
||||
Assert.assertEquals("testvalue", body["gcmcontinue"])
|
||||
Assert.assertEquals("gcmcontinue||", body["continue"])
|
||||
Assert.assertEquals("imageinfo", body["prop"])
|
||||
Assert.assertEquals("url|extmetadata", body["iiprop"])
|
||||
Assert.assertEquals("DateTime|Categories|GPSLatitude|GPSLongitude|ImageDescription|DateTimeOriginal|Artist|LicenseShortName|LicenseUrl", body["iiextmetadatafilter"])
|
||||
}
|
||||
}
|
||||
|
||||
assertEquals(categoryImagesContinued.size, 2)
|
||||
}
|
||||
|
||||
/**
|
||||
* Test response for search images
|
||||
*/
|
||||
@Test
|
||||
fun getSearchImages() {
|
||||
server.enqueue(getFirstPageOfImages())
|
||||
testFirstPageSearchQuery()
|
||||
}
|
||||
|
||||
/**
|
||||
* Test response for paginated search
|
||||
*/
|
||||
@Test
|
||||
fun getSearchImagesWithContinue() {
|
||||
server.enqueue(getFirstPageOfSearchImages())
|
||||
server.enqueue(getSecondPageOfSearchImages())
|
||||
testFirstPageSearchQuery()
|
||||
|
||||
`when`(sharedPreferences.getJson<HashMap<String, String>>("query_continue_Watercraft moored off shore", mapType))
|
||||
.thenReturn(hashMapOf(Pair("gsroffset", "25"), Pair("continue", "gsroffset||")))
|
||||
|
||||
|
||||
val categoryImagesContinued = testObject.getMediaList("search", "Watercraft moored off shore")!!.blockingGet()
|
||||
|
||||
assertBasicRequestParameters(server, "GET").let { request ->
|
||||
parseQueryParams(request).let { body ->
|
||||
Assert.assertEquals("json", body["format"])
|
||||
Assert.assertEquals("2", body["formatversion"])
|
||||
Assert.assertEquals("query", body["action"])
|
||||
Assert.assertEquals("search", body["generator"])
|
||||
Assert.assertEquals("text", body["gsrwhat"])
|
||||
Assert.assertEquals("6", body["gsrnamespace"])
|
||||
Assert.assertEquals("25", body["gsrlimit"])
|
||||
Assert.assertEquals("Watercraft moored off shore", body["gsrsearch"])
|
||||
Assert.assertEquals("25", body["gsroffset"])
|
||||
Assert.assertEquals("gsroffset||", body["continue"])
|
||||
Assert.assertEquals("imageinfo", body["prop"])
|
||||
Assert.assertEquals("url|extmetadata", body["iiprop"])
|
||||
Assert.assertEquals("DateTime|Categories|GPSLatitude|GPSLongitude|ImageDescription|DateTimeOriginal|Artist|LicenseShortName|LicenseUrl", body["iiextmetadatafilter"])
|
||||
}
|
||||
}
|
||||
|
||||
assertEquals(categoryImagesContinued.size, 2)
|
||||
}
|
||||
|
||||
/**
|
||||
* Test response for getting media without generator
|
||||
*/
|
||||
|
|
@ -236,64 +143,6 @@ class OkHttpJsonApiClientTest {
|
|||
assert(media is Media)
|
||||
}
|
||||
|
||||
private fun testFirstPageSearchQuery() {
|
||||
val categoryImages = testObject.getMediaList("search", "Watercraft moored off shore")!!.blockingGet()
|
||||
|
||||
assertBasicRequestParameters(server, "GET").let { request ->
|
||||
parseQueryParams(request).let { body ->
|
||||
Assert.assertEquals("json", body["format"])
|
||||
Assert.assertEquals("2", body["formatversion"])
|
||||
Assert.assertEquals("query", body["action"])
|
||||
Assert.assertEquals("search", body["generator"])
|
||||
Assert.assertEquals("text", body["gsrwhat"])
|
||||
Assert.assertEquals("6", body["gsrnamespace"])
|
||||
Assert.assertEquals("25", body["gsrlimit"])
|
||||
Assert.assertEquals("Watercraft moored off shore", body["gsrsearch"])
|
||||
Assert.assertEquals("imageinfo", body["prop"])
|
||||
Assert.assertEquals("url|extmetadata", body["iiprop"])
|
||||
Assert.assertEquals("DateTime|Categories|GPSLatitude|GPSLongitude|ImageDescription|DateTimeOriginal|Artist|LicenseShortName|LicenseUrl", body["iiextmetadatafilter"])
|
||||
}
|
||||
}
|
||||
assertEquals(categoryImages.size, 2)
|
||||
}
|
||||
|
||||
private fun testFirstPageQuery() {
|
||||
val categoryImages = testObject.getMediaList("category", "Watercraft moored off shore")?.blockingGet()
|
||||
|
||||
assertBasicRequestParameters(server, "GET").let { request ->
|
||||
parseQueryParams(request).let { body ->
|
||||
Assert.assertEquals("json", body["format"])
|
||||
Assert.assertEquals("2", body["formatversion"])
|
||||
Assert.assertEquals("query", body["action"])
|
||||
Assert.assertEquals("categorymembers", body["generator"])
|
||||
Assert.assertEquals("file", body["gcmtype"])
|
||||
Assert.assertEquals("Watercraft moored off shore", body["gcmtitle"])
|
||||
Assert.assertEquals("timestamp", body["gcmsort"])
|
||||
Assert.assertEquals("desc", body["gcmdir"])
|
||||
Assert.assertEquals("imageinfo", body["prop"])
|
||||
Assert.assertEquals("url|extmetadata", body["iiprop"])
|
||||
Assert.assertEquals("DateTime|Categories|GPSLatitude|GPSLongitude|ImageDescription|DateTimeOriginal|Artist|LicenseShortName|LicenseUrl", body["iiextmetadatafilter"])
|
||||
}
|
||||
}
|
||||
assertEquals(categoryImages?.size, 2)
|
||||
}
|
||||
|
||||
private fun getFirstPageOfImages(): MockResponse {
|
||||
return getMediaList("gcmcontinue", "testvalue", "gcmcontinue||", 2)
|
||||
}
|
||||
|
||||
private fun getSecondPageOfImages(): MockResponse {
|
||||
return getMediaList("gcmcontinue", "testvalue2", "gcmcontinue||", 2)
|
||||
}
|
||||
|
||||
private fun getFirstPageOfSearchImages(): MockResponse {
|
||||
return getMediaList("gsroffset", "25", "gsroffset||", 2)
|
||||
}
|
||||
|
||||
private fun getSecondPageOfSearchImages(): MockResponse {
|
||||
return getMediaList("gsroffset", "25", "gsroffset||", 2)
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate a MockResponse object which contains a list of media pages
|
||||
*/
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue