From 41e7da5a3447bdbb30d8019f186fe612fe317b94 Mon Sep 17 00:00:00 2001 From: Paul Hawke Date: Fri, 29 Nov 2024 10:54:34 -0600 Subject: [PATCH] Convert LeaderboardResponse and related test to kotlin --- .../leaderboard/LeaderboardResponse.java | 237 ------------------ .../leaderboard/LeaderboardResponse.kt | 19 ++ .../leaderboard/LeaderboardApiTest.java | 116 --------- .../commons/leaderboard/LeaderboardApiTest.kt | 121 +++++++++ 4 files changed, 140 insertions(+), 353 deletions(-) delete mode 100644 app/src/main/java/fr/free/nrw/commons/profile/leaderboard/LeaderboardResponse.java create mode 100644 app/src/main/java/fr/free/nrw/commons/profile/leaderboard/LeaderboardResponse.kt delete mode 100644 app/src/test/kotlin/fr/free/nrw/commons/leaderboard/LeaderboardApiTest.java create mode 100644 app/src/test/kotlin/fr/free/nrw/commons/leaderboard/LeaderboardApiTest.kt diff --git a/app/src/main/java/fr/free/nrw/commons/profile/leaderboard/LeaderboardResponse.java b/app/src/main/java/fr/free/nrw/commons/profile/leaderboard/LeaderboardResponse.java deleted file mode 100644 index 34294fca9..000000000 --- a/app/src/main/java/fr/free/nrw/commons/profile/leaderboard/LeaderboardResponse.java +++ /dev/null @@ -1,237 +0,0 @@ -package fr.free.nrw.commons.profile.leaderboard; - -import java.util.List; -import com.google.gson.annotations.Expose; -import com.google.gson.annotations.SerializedName; - -/** - * GSON Response Class for Leaderboard API response - */ -public class LeaderboardResponse { - - /** - * Status Code returned from the API - * Example value - 200 - */ - @SerializedName("status") - @Expose - private Integer status; - - /** - * Username returned from the API - * Example value - Syced - */ - @SerializedName("username") - @Expose - private String username; - - /** - * Category count returned from the API - * Example value - 10 - */ - @SerializedName("category_count") - @Expose - private Integer categoryCount; - - /** - * Limit returned from the API - * Example value - 10 - */ - @SerializedName("limit") - @Expose - private int limit; - - /** - * Avatar returned from the API - * Example value - https://upload.wikimedia.org/wikipedia/commons/thumb/0/0a/Gnome-stock_person.svg/200px-Gnome-stock_person.svg.png - */ - @SerializedName("avatar") - @Expose - private String avatar; - - /** - * Offset returned from the API - * Example value - 0 - */ - @SerializedName("offset") - @Expose - private int offset; - - /** - * Duration returned from the API - * Example value - yearly - */ - @SerializedName("duration") - @Expose - private String duration; - - /** - * Leaderboard list returned from the API - * Example value - [{ - * "username": "Fæ", - * "category_count": 107147, - * "avatar": "https://upload.wikimedia.org/wikipedia/commons/thumb/0/0a/Gnome-stock_person.svg/200px-Gnome-stock_person.svg.png", - * "rank": 1 - * }] - */ - @SerializedName("leaderboard_list") - @Expose - private List leaderboardList = null; - - /** - * Category returned from the API - * Example value - upload - */ - @SerializedName("category") - @Expose - private String category; - - /** - * Rank returned from the API - * Example value - 1 - */ - @SerializedName("rank") - @Expose - private Integer rank; - - /** - * @return the status code - */ - public Integer getStatus() { - return status; - } - - /** - * Sets the status code - */ - public void setStatus(Integer status) { - this.status = status; - } - - /** - * @return the username - */ - public String getUsername() { - return username; - } - - /** - * Sets the username - */ - public void setUsername(String username) { - this.username = username; - } - - /** - * @return the category count - */ - public Integer getCategoryCount() { - return categoryCount; - } - - /** - * Sets the category count - */ - public void setCategoryCount(Integer categoryCount) { - this.categoryCount = categoryCount; - } - - /** - * @return the limit - */ - public int getLimit() { - return limit; - } - - /** - * Sets the limit - */ - public void setLimit(int limit) { - this.limit = limit; - } - - /** - * @return the avatar - */ - public String getAvatar() { - return avatar; - } - - /** - * Sets the avatar - */ - public void setAvatar(String avatar) { - this.avatar = avatar; - } - - /** - * @return the offset - */ - public int getOffset() { - return offset; - } - - /** - * Sets the offset - */ - public void setOffset(int offset) { - this.offset = offset; - } - - /** - * @return the duration - */ - public String getDuration() { - return duration; - } - - /** - * Sets the duration - */ - public void setDuration(String duration) { - this.duration = duration; - } - - /** - * @return the leaderboard list - */ - public List getLeaderboardList() { - return leaderboardList; - } - - /** - * Sets the leaderboard list - */ - public void setLeaderboardList(List leaderboardList) { - this.leaderboardList = leaderboardList; - } - - /** - * @return the category - */ - public String getCategory() { - return category; - } - - /** - * Sets the category - */ - public void setCategory(String category) { - this.category = category; - } - - /** - * @return the rank - */ - public Integer getRank() { - return rank; - } - - /** - * Sets the rank - */ - public void setRank(Integer rank) { - this.rank = rank; - } - -} \ No newline at end of file diff --git a/app/src/main/java/fr/free/nrw/commons/profile/leaderboard/LeaderboardResponse.kt b/app/src/main/java/fr/free/nrw/commons/profile/leaderboard/LeaderboardResponse.kt new file mode 100644 index 000000000..8be342650 --- /dev/null +++ b/app/src/main/java/fr/free/nrw/commons/profile/leaderboard/LeaderboardResponse.kt @@ -0,0 +1,19 @@ +package fr.free.nrw.commons.profile.leaderboard + +import com.google.gson.annotations.SerializedName + +/** + * GSON Response Class for Leaderboard API response + */ +data class LeaderboardResponse( + @SerializedName("status") var status: Int? = null, + @SerializedName("username") var username: String? = null, + @SerializedName("category_count") var categoryCount: Int? = null, + @SerializedName("limit") var limit: Int = 0, + @SerializedName("avatar") var avatar: String? = null, + @SerializedName("offset") var offset: Int = 0, + @SerializedName("duration") var duration: String? = null, + @SerializedName("leaderboard_list") var leaderboardList: List? = null, + @SerializedName("category") var category: String? = null, + @SerializedName("rank") var rank: Int? = null +) \ No newline at end of file diff --git a/app/src/test/kotlin/fr/free/nrw/commons/leaderboard/LeaderboardApiTest.java b/app/src/test/kotlin/fr/free/nrw/commons/leaderboard/LeaderboardApiTest.java deleted file mode 100644 index 51d806a88..000000000 --- a/app/src/test/kotlin/fr/free/nrw/commons/leaderboard/LeaderboardApiTest.java +++ /dev/null @@ -1,116 +0,0 @@ -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; - -/** - * This class tests the Leaderboard API calls - */ -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"; - - /** - * This method initialises a Mock Server - */ - @Before - public void initTest() { - server = new MockWebServer(); - } - - /** - * This method will setup a Mock Server and load Test JSON Response File - * @throws Exception - */ - @Before - public void setUp() throws Exception { - - String testResponseBody = convertStreamToString(getClass().getClassLoader().getResourceAsStream(FILE_NAME)); - - server.enqueue(new MockResponse().setBody(testResponseBody)); - server.start(); - } - - /** - * This method converts a Input Stream to String - * @param is takes Input Stream of JSON File as Parameter - * @return a String with JSON data - * @throws Exception - */ - 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(); - } - - /** - * This method will call the Mock Server and Test it with sample values. - * It will test the Leaderboard API call functionality and check if the object is - * being created with the correct values - * @throws IOException - */ - @Test - 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()); - } - - /** - * This method will call the Mock API and returns the Leaderboard Response Object - * @param okHttpClient - * @param httpUrl - * @return Leaderboard Response Object - * @throws IOException - */ - 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; - } - - /** - * This method shuts down the Mock Server - * @throws IOException - */ - @After - public void shutdown() throws IOException { - server.shutdown(); - } -} diff --git a/app/src/test/kotlin/fr/free/nrw/commons/leaderboard/LeaderboardApiTest.kt b/app/src/test/kotlin/fr/free/nrw/commons/leaderboard/LeaderboardApiTest.kt new file mode 100644 index 000000000..ac0da42f3 --- /dev/null +++ b/app/src/test/kotlin/fr/free/nrw/commons/leaderboard/LeaderboardApiTest.kt @@ -0,0 +1,121 @@ +package fr.free.nrw.commons.leaderboard + +import com.google.gson.Gson +import fr.free.nrw.commons.profile.leaderboard.LeaderboardResponse +import okhttp3.HttpUrl +import okhttp3.OkHttpClient +import okhttp3.Request +import okhttp3.mockwebserver.MockResponse +import okhttp3.mockwebserver.MockWebServer +import org.junit.After +import org.junit.Assert +import org.junit.Before +import org.junit.Test +import java.io.BufferedReader +import java.io.IOException +import java.io.InputStream +import java.io.InputStreamReader + +/** + * This class tests the Leaderboard API calls + */ +class LeaderboardApiTest { + lateinit var server: MockWebServer + + /** + * This method initialises a Mock Server + */ + @Before + fun initTest() { + server = MockWebServer() + } + + /** + * This method will setup a Mock Server and load Test JSON Response File + * @throws Exception + */ + @Before + @Throws(Exception::class) + fun setUp() { + val testResponseBody = convertStreamToString( + javaClass.classLoader!!.getResourceAsStream(FILE_NAME) + ) + + server.enqueue(MockResponse().setBody(testResponseBody)) + server.start() + } + + /** + * This method will call the Mock Server and Test it with sample values. + * It will test the Leaderboard API call functionality and check if the object is + * being created with the correct values + * @throws IOException + */ + @Test + @Throws(IOException::class) + fun apiTest() { + val httpUrl = server.url(ENDPOINT) + val response = sendRequest(OkHttpClient(), httpUrl) + + Assert.assertEquals(TEST_AVATAR, response!!.avatar) + Assert.assertEquals(TEST_USERNAME, response.username) + Assert.assertEquals(TEST_USER_RANK, response.rank) + Assert.assertEquals(TEST_USER_COUNT, response.categoryCount) + } + + /** + * This method will call the Mock API and returns the Leaderboard Response Object + * @param okHttpClient + * @param httpUrl + * @return Leaderboard Response Object + * @throws IOException + */ + @Throws(IOException::class) + private fun sendRequest(okHttpClient: OkHttpClient, httpUrl: HttpUrl): LeaderboardResponse? { + val request: Request = Request.Builder().url(httpUrl).build() + val response = okHttpClient.newCall(request).execute() + if (response.isSuccessful) { + val gson = Gson() + return gson.fromJson(response.body!!.string(), LeaderboardResponse::class.java) + } + return null + } + + /** + * This method shuts down the Mock Server + * @throws IOException + */ + @After + @Throws(IOException::class) + fun shutdown() { + server.shutdown() + } + + companion object { + private const val TEST_USERNAME = "user" + private const val TEST_AVATAR = "avatar" + private const val TEST_USER_RANK = 1 + private const val TEST_USER_COUNT = 0 + + private const val FILE_NAME = "leaderboard_sample_response.json" + private const val ENDPOINT = "/leaderboard.py" + + /** + * This method converts a Input Stream to String + * @param is takes Input Stream of JSON File as Parameter + * @return a String with JSON data + * @throws Exception + */ + @Throws(Exception::class) + private fun convertStreamToString(`is`: InputStream): String { + val reader = BufferedReader(InputStreamReader(`is`)) + val sb = StringBuilder() + var line: String? + while ((reader.readLine().also { line = it }) != null) { + sb.append(line).append("\n") + } + reader.close() + return sb.toString() + } + } +}