mirror of
https://github.com/commons-app/apps-android-commons.git
synced 2025-10-27 04:43:54 +01:00
Fix implementation of getUploadCount() and its test
This commit is contained in:
parent
d1e53db73a
commit
946f4f2ac2
2 changed files with 19 additions and 4 deletions
|
|
@ -3,6 +3,7 @@ package fr.free.nrw.commons.mwapi;
|
||||||
import android.os.Build;
|
import android.os.Build;
|
||||||
import android.support.annotation.NonNull;
|
import android.support.annotation.NonNull;
|
||||||
import android.support.annotation.Nullable;
|
import android.support.annotation.Nullable;
|
||||||
|
import android.support.annotation.VisibleForTesting;
|
||||||
import android.text.TextUtils;
|
import android.text.TextUtils;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
|
|
||||||
|
|
@ -41,6 +42,8 @@ import timber.log.Timber;
|
||||||
* @author Addshore
|
* @author Addshore
|
||||||
*/
|
*/
|
||||||
public class ApacheHttpClientMediaWikiApi implements MediaWikiApi {
|
public class ApacheHttpClientMediaWikiApi implements MediaWikiApi {
|
||||||
|
private String wikiMediaToolforgeUrl = "https://tools.wmflabs.org/";
|
||||||
|
|
||||||
private static final String THUMB_SIZE = "640";
|
private static final String THUMB_SIZE = "640";
|
||||||
private AbstractHttpClient httpClient;
|
private AbstractHttpClient httpClient;
|
||||||
private MWApi api;
|
private MWApi api;
|
||||||
|
|
@ -57,6 +60,11 @@ public class ApacheHttpClientMediaWikiApi implements MediaWikiApi {
|
||||||
api = new MWApi(apiURL, httpClient);
|
api = new MWApi(apiURL, httpClient);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@VisibleForTesting
|
||||||
|
public void setWikiMediaToolforgeUrl(String wikiMediaToolforgeUrl) {
|
||||||
|
this.wikiMediaToolforgeUrl = wikiMediaToolforgeUrl;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param username String
|
* @param username String
|
||||||
* @param password String
|
* @param password String
|
||||||
|
|
@ -376,14 +384,16 @@ public class ApacheHttpClientMediaWikiApi implements MediaWikiApi {
|
||||||
@NonNull
|
@NonNull
|
||||||
public Single<Integer> getUploadCount(String userName) {
|
public Single<Integer> getUploadCount(String userName) {
|
||||||
final String uploadCountUrlTemplate =
|
final String uploadCountUrlTemplate =
|
||||||
"https://tools.wmflabs.org/urbanecmbot/uploadsbyuser/uploadsbyuser.py?user=%s";
|
wikiMediaToolforgeUrl + "urbanecmbot/uploadsbyuser/uploadsbyuser.py";
|
||||||
|
|
||||||
return Single.fromCallable(() -> {
|
return Single.fromCallable(() -> {
|
||||||
String url = String.format(
|
String url = String.format(
|
||||||
Locale.ENGLISH,
|
Locale.ENGLISH,
|
||||||
uploadCountUrlTemplate,
|
uploadCountUrlTemplate,
|
||||||
new PageTitle(userName).getText());
|
new PageTitle(userName).getText());
|
||||||
HttpResponse response = Http.get(url).use(httpClient).asResponse();
|
HttpResponse response = Http.get(url).use(httpClient)
|
||||||
|
.data("user", userName)
|
||||||
|
.asResponse();
|
||||||
String uploadCount = EntityUtils.toString(response.getEntity()).trim();
|
String uploadCount = EntityUtils.toString(response.getEntity()).trim();
|
||||||
return Integer.parseInt(uploadCount);
|
return Integer.parseInt(uploadCount);
|
||||||
});
|
});
|
||||||
|
|
|
||||||
|
|
@ -41,6 +41,7 @@ public class ApacheHttpClientMediaWikiApiTest {
|
||||||
public void setUp() throws Exception {
|
public void setUp() throws Exception {
|
||||||
server = new MockWebServer();
|
server = new MockWebServer();
|
||||||
testObject = new ApacheHttpClientMediaWikiApi("http://" + server.getHostName() + ":" + server.getPort() + "/");
|
testObject = new ApacheHttpClientMediaWikiApi("http://" + server.getHostName() + ":" + server.getPort() + "/");
|
||||||
|
testObject.setWikiMediaToolforgeUrl("http://" + server.getHostName() + ":" + server.getPort() + "/");
|
||||||
}
|
}
|
||||||
|
|
||||||
@After
|
@After
|
||||||
|
|
@ -198,10 +199,14 @@ public class ApacheHttpClientMediaWikiApiTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void getUploadCount() {
|
public void getUploadCount() throws InterruptedException {
|
||||||
server.enqueue(new MockResponse().setBody("23\n"));
|
server.enqueue(new MockResponse().setBody("23\n"));
|
||||||
|
|
||||||
TestObserver<Integer> testObserver = testObject.getUploadCount("username").test();
|
TestObserver<Integer> testObserver = testObject.getUploadCount("testUsername").test();
|
||||||
|
|
||||||
|
RecordedRequest request = server.takeRequest();
|
||||||
|
Map<String, String> params = parseQueryParams(request);
|
||||||
|
assertEquals("testUsername", params.get("user"));
|
||||||
|
|
||||||
assertEquals(1, testObserver.valueCount());
|
assertEquals(1, testObserver.valueCount());
|
||||||
assertEquals(23, (int)testObserver.values().get(0));
|
assertEquals(23, (int)testObserver.values().get(0));
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue