-
Notifications
You must be signed in to change notification settings - Fork 1.3k
[GSoC] Added Unit Tests and Fixed Landscape Mode Bug #3872
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
maskaravivek
merged 6 commits into
commons-app:leaderboard
from
madhurgupta10:leaderboard
Jul 15, 2020
Merged
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
8b15863
Fixes #3861 Use the APIs to fetch leaderboard’s based on uploads via …
madhurgupta10 0104a76
Merge remote-tracking branch 'upstream/leaderboard' into leaderboard
madhurgupta10 089c153
Fixed Bug - missing data in landscape mode
madhurgupta10 ce30c8f
Added Unit Tests for Leaderboard
madhurgupta10 3fca9e9
Added JavaDocs
madhurgupta10 4b2b7ad
Updated JavaDocs
madhurgupta10 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Added Unit Tests for Leaderboard
- Loading branch information
commit ce30c8f1f3c94cd304de05ec578b4e64332d6995
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
83 changes: 83 additions & 0 deletions
83
app/src/test/kotlin/fr/free/nrw/commons/leaderboard/LeaderboardApiTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
package fr.free.nrw.commons.leaderboard; | ||
|
||
import com.google.gson.Gson; | ||
import fr.free.nrw.commons.profile.leaderboard.LeaderboardResponse; | ||
import java.io.BufferedReader; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.io.InputStreamReader; | ||
import okhttp3.HttpUrl; | ||
import okhttp3.OkHttpClient; | ||
import okhttp3.Request; | ||
import okhttp3.Request.Builder; | ||
import okhttp3.Response; | ||
import okhttp3.mockwebserver.MockResponse; | ||
import okhttp3.mockwebserver.MockWebServer; | ||
import org.junit.After; | ||
import org.junit.Assert; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
public class LeaderboardApiTest { | ||
|
||
MockWebServer server; | ||
private static final String TEST_USERNAME = "user"; | ||
private static final String TEST_AVATAR = "avatar"; | ||
private static final int TEST_USER_RANK = 1; | ||
private static final int TEST_USER_COUNT = 0; | ||
|
||
private static final String FILE_NAME = "leaderboard_sample_response.json"; | ||
private static final String ENDPOINT = "/leaderboard.py"; | ||
|
||
@Before | ||
public void initTest() { | ||
server = new MockWebServer(); | ||
} | ||
|
||
@Before | ||
public void setUp() throws Exception { | ||
|
||
String testResponseBody = convertStreamToString(getClass().getClassLoader().getResourceAsStream(FILE_NAME)); | ||
|
||
server.enqueue(new MockResponse().setBody(testResponseBody)); | ||
server.start(); | ||
} | ||
|
||
private static String convertStreamToString(InputStream is) throws Exception { | ||
BufferedReader reader = new BufferedReader(new InputStreamReader(is)); | ||
StringBuilder sb = new StringBuilder(); | ||
String line; | ||
while ((line = reader.readLine()) != null) { | ||
sb.append(line).append("\n"); | ||
} | ||
reader.close(); | ||
return sb.toString(); | ||
} | ||
|
||
@Test | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As a javadoc here, please say what class (or what method of what class) is being tested. Thanks! |
||
public void apiTest() throws IOException { | ||
HttpUrl httpUrl = server.url(ENDPOINT); | ||
LeaderboardResponse response = sendRequest(new OkHttpClient(), httpUrl); | ||
|
||
Assert.assertEquals(TEST_AVATAR, response.getAvatar()); | ||
Assert.assertEquals(TEST_USERNAME, response.getUsername()); | ||
Assert.assertEquals(Integer.valueOf(TEST_USER_RANK), response.getRank()); | ||
Assert.assertEquals(Integer.valueOf(TEST_USER_COUNT), response.getCategoryCount()); | ||
} | ||
|
||
private LeaderboardResponse sendRequest(OkHttpClient okHttpClient, HttpUrl httpUrl) | ||
throws IOException { | ||
Request request = new Builder().url(httpUrl).build(); | ||
Response response = okHttpClient.newCall(request).execute(); | ||
if (response.isSuccessful()) { | ||
Gson gson = new Gson(); | ||
return gson.fromJson(response.body().string(), LeaderboardResponse.class); | ||
} | ||
return null; | ||
} | ||
|
||
@After | ||
public void shutdown() throws IOException { | ||
server.shutdown(); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
{ | ||
"status": 200, | ||
"username": "user", | ||
"category_count": 0, | ||
"limit": null, | ||
"avatar": "avatar", | ||
"offset": null, | ||
"duration": "all_time", | ||
"leaderboard_list": [ | ||
{ | ||
"username": "user", | ||
"category_count": 0, | ||
"avatar": "avatar", | ||
"rank": 1 | ||
} | ||
], | ||
"category": "used", | ||
"rank": 1 | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.