mirror of
				https://github.com/commons-app/apps-android-commons.git
				synced 2025-10-31 14:53:59 +01:00 
			
		
		
		
	Convert UpdateAvatarResponse and related test to Kotlin
This commit is contained in:
		
							parent
							
								
									4ddb36ac63
								
							
						
					
					
						commit
						61e306f75a
					
				
					 4 changed files with 137 additions and 194 deletions
				
			
		|  | @ -1,77 +0,0 @@ | ||||||
| package fr.free.nrw.commons.profile.leaderboard; |  | ||||||
| 
 |  | ||||||
| import com.google.gson.annotations.Expose; |  | ||||||
| import com.google.gson.annotations.SerializedName; |  | ||||||
| 
 |  | ||||||
| /** |  | ||||||
|  * GSON Response Class for Update Avatar API response |  | ||||||
|  */ |  | ||||||
| public class UpdateAvatarResponse { |  | ||||||
| 
 |  | ||||||
|   /** |  | ||||||
|    * Status Code returned from the API |  | ||||||
|    * Example value - 200 |  | ||||||
|    */ |  | ||||||
|   @SerializedName("status") |  | ||||||
|   @Expose |  | ||||||
|   private String status; |  | ||||||
| 
 |  | ||||||
|   /** |  | ||||||
|    * Message returned from the API |  | ||||||
|    * Example value - Avatar Updated |  | ||||||
|    */ |  | ||||||
|   @SerializedName("message") |  | ||||||
|   @Expose |  | ||||||
|   private String message; |  | ||||||
| 
 |  | ||||||
|   /** |  | ||||||
|    * Username returned from the API |  | ||||||
|    * Example value - Syced |  | ||||||
|    */ |  | ||||||
|   @SerializedName("user") |  | ||||||
|   @Expose |  | ||||||
|   private String user; |  | ||||||
| 
 |  | ||||||
|   /** |  | ||||||
|    * @return the status code |  | ||||||
|    */ |  | ||||||
|   public String getStatus() { |  | ||||||
|     return status; |  | ||||||
|   } |  | ||||||
| 
 |  | ||||||
|   /** |  | ||||||
|    * Sets the status code |  | ||||||
|    */ |  | ||||||
|   public void setStatus(String status) { |  | ||||||
|     this.status = status; |  | ||||||
|   } |  | ||||||
| 
 |  | ||||||
|   /** |  | ||||||
|    * @return the message |  | ||||||
|    */ |  | ||||||
|   public String getMessage() { |  | ||||||
|     return message; |  | ||||||
|   } |  | ||||||
| 
 |  | ||||||
|   /** |  | ||||||
|    * Sets the message |  | ||||||
|    */ |  | ||||||
|   public void setMessage(String message) { |  | ||||||
|     this.message = message; |  | ||||||
|   } |  | ||||||
| 
 |  | ||||||
|   /** |  | ||||||
|    * @return the username |  | ||||||
|    */ |  | ||||||
|   public String getUser() { |  | ||||||
|     return user; |  | ||||||
|   } |  | ||||||
| 
 |  | ||||||
|   /** |  | ||||||
|    * Sets the username |  | ||||||
|    */ |  | ||||||
|   public void setUser(String user) { |  | ||||||
|     this.user = user; |  | ||||||
|   } |  | ||||||
| 
 |  | ||||||
| } |  | ||||||
|  | @ -0,0 +1,10 @@ | ||||||
|  | package fr.free.nrw.commons.profile.leaderboard | ||||||
|  | 
 | ||||||
|  | /** | ||||||
|  |  * GSON Response Class for Update Avatar API response | ||||||
|  |  */ | ||||||
|  | data class UpdateAvatarResponse( | ||||||
|  |     var status: String? = null, | ||||||
|  |     var message: String? = null, | ||||||
|  |     var user: String? = null | ||||||
|  | ) | ||||||
|  | @ -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(); |  | ||||||
|     } |  | ||||||
| } |  | ||||||
| 
 |  | ||||||
|  | @ -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() | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  | } | ||||||
|  | 
 | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Paul Hawke
						Paul Hawke