|
| 1 | +package fr.free.nrw.commons.leaderboard; |
| 2 | + |
| 3 | +import com.google.gson.Gson; |
| 4 | +import fr.free.nrw.commons.profile.leaderboard.LeaderboardResponse; |
| 5 | +import java.io.BufferedReader; |
| 6 | +import java.io.IOException; |
| 7 | +import java.io.InputStream; |
| 8 | +import java.io.InputStreamReader; |
| 9 | +import okhttp3.HttpUrl; |
| 10 | +import okhttp3.OkHttpClient; |
| 11 | +import okhttp3.Request; |
| 12 | +import okhttp3.Request.Builder; |
| 13 | +import okhttp3.Response; |
| 14 | +import okhttp3.mockwebserver.MockResponse; |
| 15 | +import okhttp3.mockwebserver.MockWebServer; |
| 16 | +import org.junit.After; |
| 17 | +import org.junit.Assert; |
| 18 | +import org.junit.Before; |
| 19 | +import org.junit.Test; |
| 20 | + |
| 21 | +/** |
| 22 | + * This class tests the Leaderboard API calls |
| 23 | + */ |
| 24 | +public class LeaderboardApiTest { |
| 25 | + |
| 26 | + MockWebServer server; |
| 27 | + private static final String TEST_USERNAME = "user"; |
| 28 | + private static final String TEST_AVATAR = "avatar"; |
| 29 | + private static final int TEST_USER_RANK = 1; |
| 30 | + private static final int TEST_USER_COUNT = 0; |
| 31 | + |
| 32 | + private static final String FILE_NAME = "leaderboard_sample_response.json"; |
| 33 | + private static final String ENDPOINT = "/leaderboard.py"; |
| 34 | + |
| 35 | + /** |
| 36 | + * This method initialises a Mock Server |
| 37 | + */ |
| 38 | + @Before |
| 39 | + public void initTest() { |
| 40 | + server = new MockWebServer(); |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * This method will setup a Mock Server and load Test JSON Response File |
| 45 | + * @throws Exception |
| 46 | + */ |
| 47 | + @Before |
| 48 | + public void setUp() throws Exception { |
| 49 | + |
| 50 | + String testResponseBody = convertStreamToString(getClass().getClassLoader().getResourceAsStream(FILE_NAME)); |
| 51 | + |
| 52 | + server.enqueue(new MockResponse().setBody(testResponseBody)); |
| 53 | + server.start(); |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * This method converts a Input Stream to String |
| 58 | + * @param is takes Input Stream of JSON File as Parameter |
| 59 | + * @return a String with JSON data |
| 60 | + * @throws Exception |
| 61 | + */ |
| 62 | + private static String convertStreamToString(InputStream is) throws Exception { |
| 63 | + BufferedReader reader = new BufferedReader(new InputStreamReader(is)); |
| 64 | + StringBuilder sb = new StringBuilder(); |
| 65 | + String line; |
| 66 | + while ((line = reader.readLine()) != null) { |
| 67 | + sb.append(line).append("\n"); |
| 68 | + } |
| 69 | + reader.close(); |
| 70 | + return sb.toString(); |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * This method will call the Mock Server and Test it with sample values. |
| 75 | + * It will test the Leaderboard API call functionality and check if the object is |
| 76 | + * being created with the correct values |
| 77 | + * @throws IOException |
| 78 | + */ |
| 79 | + @Test |
| 80 | + public void apiTest() throws IOException { |
| 81 | + HttpUrl httpUrl = server.url(ENDPOINT); |
| 82 | + LeaderboardResponse response = sendRequest(new OkHttpClient(), httpUrl); |
| 83 | + |
| 84 | + Assert.assertEquals(TEST_AVATAR, response.getAvatar()); |
| 85 | + Assert.assertEquals(TEST_USERNAME, response.getUsername()); |
| 86 | + Assert.assertEquals(Integer.valueOf(TEST_USER_RANK), response.getRank()); |
| 87 | + Assert.assertEquals(Integer.valueOf(TEST_USER_COUNT), response.getCategoryCount()); |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * This method will call the Mock API and returns the Leaderboard Response Object |
| 92 | + * @param okHttpClient |
| 93 | + * @param httpUrl |
| 94 | + * @return Leaderboard Response Object |
| 95 | + * @throws IOException |
| 96 | + */ |
| 97 | + private LeaderboardResponse sendRequest(OkHttpClient okHttpClient, HttpUrl httpUrl) |
| 98 | + throws IOException { |
| 99 | + Request request = new Builder().url(httpUrl).build(); |
| 100 | + Response response = okHttpClient.newCall(request).execute(); |
| 101 | + if (response.isSuccessful()) { |
| 102 | + Gson gson = new Gson(); |
| 103 | + return gson.fromJson(response.body().string(), LeaderboardResponse.class); |
| 104 | + } |
| 105 | + return null; |
| 106 | + } |
| 107 | + |
| 108 | + /** |
| 109 | + * This method shuts down the Mock Server |
| 110 | + * @throws IOException |
| 111 | + */ |
| 112 | + @After |
| 113 | + public void shutdown() throws IOException { |
| 114 | + server.shutdown(); |
| 115 | + } |
| 116 | +} |
0 commit comments