mirror of
				https://github.com/commons-app/apps-android-commons.git
				synced 2025-10-31 14:53:59 +01:00 
			
		
		
		
	Convert LeaderboardResponse and related test to kotlin
This commit is contained in:
		
							parent
							
								
									61e306f75a
								
							
						
					
					
						commit
						41e7da5a34
					
				
					 4 changed files with 140 additions and 353 deletions
				
			
		|  | @ -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> 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<LeaderboardList> getLeaderboardList() { | ||||
|         return leaderboardList; | ||||
|     } | ||||
| 
 | ||||
|     /** | ||||
|      * Sets the leaderboard list | ||||
|      */ | ||||
|     public void setLeaderboardList(List<LeaderboardList> 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; | ||||
|     } | ||||
| 
 | ||||
| } | ||||
|  | @ -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<LeaderboardList>? = null, | ||||
|     @SerializedName("category") var category: String? = null, | ||||
|     @SerializedName("rank") var rank: Int? = 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(); | ||||
|     } | ||||
| } | ||||
|  | @ -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() | ||||
|         } | ||||
|     } | ||||
| } | ||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Paul Hawke
						Paul Hawke