Convert profile package to kotlin (#5979)

* Convert ViewModelFactory to kotlin

* Convert UpdateAvatarResponse and related test to Kotlin

* Convert LeaderboardResponse and related test to kotlin

* Convert LeaderboardListAdapter to kotlin

* Convert UserDetailAdapter to kotlin

* Convert LeaderboardListViewModel to kotlin

* Convert DataSourceClass to kotlin

* Convert the LeaderboardFragment to kotlin

* Converted AchievementsFragment to kotlin

* Revert "Converted AchievementsFragment to kotlin"

This reverts commit 4fcbb81e5d.

---------

Co-authored-by: Nicolas Raoul <nicolas.raoul@gmail.com>
This commit is contained in:
Paul Hawke 2024-12-03 00:47:25 -06:00 committed by GitHub
parent 8265cc6306
commit 33548fa57d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
26 changed files with 1042 additions and 1694 deletions

View file

@ -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();
}
}

View file

@ -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()
}
}
}

View file

@ -1,117 +0,0 @@
package fr.free.nrw.commons.leaderboard;
import com.google.gson.Gson;
import fr.free.nrw.commons.profile.leaderboard.UpdateAvatarResponse;
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 UpdateAvatarApiTest {
private static final String TEST_USERNAME = "user";
private static final String TEST_STATUS = "200";
private static final String TEST_MESSAGE = "Avatar Updated";
private static final String FILE_NAME = "update_leaderboard_avatar_sample_response.json";
private static final String ENDPOINT = "/update_avatar.py";
MockWebServer server;
/**
* 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(final InputStream is) throws Exception {
final BufferedReader reader = new BufferedReader(new InputStreamReader(is));
final StringBuilder sb = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
sb.append(line).append("\n");
}
reader.close();
return sb.toString();
}
/**
* 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 {
final String testResponseBody = convertStreamToString(
getClass().getClassLoader().getResourceAsStream(FILE_NAME));
server.enqueue(new MockResponse().setBody(testResponseBody));
server.start();
}
/**
* This method will call the Mock Server and Test it with sample values. It will test the Update
* Avatar API call functionality and check if the object is being created with the correct
* values
*
* @throws IOException
*/
@Test
public void apiTest() throws IOException {
final HttpUrl httpUrl = server.url(ENDPOINT);
final UpdateAvatarResponse response = sendRequest(new OkHttpClient(), httpUrl);
Assert.assertEquals(TEST_USERNAME, response.getUser());
Assert.assertEquals(TEST_STATUS, response.getStatus());
Assert.assertEquals(TEST_MESSAGE, response.getMessage());
}
/**
* This method will call the Mock API and returns the Update Avatar Response Object
*
* @param okHttpClient
* @param httpUrl
* @return Update Avatar Response Object
* @throws IOException
*/
private UpdateAvatarResponse sendRequest(final OkHttpClient okHttpClient, final HttpUrl httpUrl)
throws IOException {
final Request request = new Builder().url(httpUrl).build();
final Response response = okHttpClient.newCall(request).execute();
if (response.isSuccessful()) {
final Gson gson = new Gson();
return gson.fromJson(response.body().string(), UpdateAvatarResponse.class);
}
return null;
}
/**
* This method shuts down the Mock Server
*
* @throws IOException
*/
@After
public void shutdown() throws IOException {
server.shutdown();
}
}

View file

@ -0,0 +1,127 @@
package fr.free.nrw.commons.leaderboard
import com.google.gson.Gson
import fr.free.nrw.commons.profile.leaderboard.UpdateAvatarResponse
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
class UpdateAvatarApiTest {
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 Update
* Avatar 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.assertNotNull(response)
with(response!!) {
Assert.assertEquals(TEST_USERNAME, user)
Assert.assertEquals(TEST_STATUS, status)
Assert.assertEquals(TEST_MESSAGE, message)
}
}
/**
* This method will call the Mock API and returns the Update Avatar Response Object
*
* @param okHttpClient
* @param httpUrl
* @return Update Avatar Response Object
* @throws IOException
*/
@Throws(IOException::class)
private fun sendRequest(okHttpClient: OkHttpClient, httpUrl: HttpUrl): UpdateAvatarResponse? {
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(),
UpdateAvatarResponse::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_STATUS = "200"
private const val TEST_MESSAGE = "Avatar Updated"
private const val FILE_NAME = "update_leaderboard_avatar_sample_response.json"
private const val ENDPOINT = "/update_avatar.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()
}
}
}