|
| 1 | +package com.codingblocks.onlineandroidapi; |
| 2 | + |
| 3 | +import com.codingblocks.onlineandroidapi.api.QbountyApi; |
| 4 | +import com.codingblocks.onlineandroidapi.models.qbounty.Claim; |
| 5 | +import com.codingblocks.onlineandroidapi.models.qbounty.Task; |
| 6 | +import com.codingblocks.onlineandroidapi.models.qbounty.User; |
| 7 | +import com.fasterxml.jackson.core.JsonFactory; |
| 8 | +import com.fasterxml.jackson.databind.DeserializationFeature; |
| 9 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 10 | +import com.github.jasminb.jsonapi.RelationshipResolver; |
| 11 | +import com.github.jasminb.jsonapi.ResourceConverter; |
| 12 | +import com.github.jasminb.jsonapi.retrofit.JSONAPIConverterFactory; |
| 13 | + |
| 14 | +import org.junit.Before; |
| 15 | +import org.junit.BeforeClass; |
| 16 | +import org.junit.Rule; |
| 17 | +import org.junit.Test; |
| 18 | + |
| 19 | +import java.io.IOException; |
| 20 | +import java.util.ArrayList; |
| 21 | +import java.util.Arrays; |
| 22 | + |
| 23 | +import okhttp3.OkHttpClient; |
| 24 | +import okhttp3.Request; |
| 25 | +import okhttp3.ResponseBody; |
| 26 | +import retrofit2.Call; |
| 27 | +import retrofit2.Retrofit; |
| 28 | +import retrofit2.http.GET; |
| 29 | + |
| 30 | +import static org.junit.Assert.assertEquals; |
| 31 | + |
| 32 | +public class QbountyTest { |
| 33 | + |
| 34 | + public ObjectMapper om; |
| 35 | + public QbountyApi api; |
| 36 | + public ResourceConverter rc; |
| 37 | + |
| 38 | + |
| 39 | + @Before |
| 40 | + public void setUp () { |
| 41 | + om = new ObjectMapper(); |
| 42 | + rc = new ResourceConverter(om, |
| 43 | + Claim.class, Task.class, User.class); |
| 44 | + |
| 45 | + rc.setGlobalResolver(new RelationshipResolver() { |
| 46 | + @Override |
| 47 | + public byte[] resolve(String relationshipURL) { |
| 48 | + OkHttpClient client = new OkHttpClient(); |
| 49 | + String[] parts = relationshipURL.split(":[0-9]{4}"); |
| 50 | + String actualUrl = parts[0] + parts[1]; |
| 51 | + try { |
| 52 | + okhttp3.Call c = client.newCall(new Request.Builder().url(actualUrl).build()); |
| 53 | + ResponseBody b = c.execute().body(); |
| 54 | + return b.bytes(); |
| 55 | + } catch (IOException e) { |
| 56 | + e.printStackTrace(); |
| 57 | + return new byte[0]; |
| 58 | + } |
| 59 | + } |
| 60 | + }); |
| 61 | + |
| 62 | + api = new Retrofit.Builder() |
| 63 | + .baseUrl("https://cb-qbounty.herokuapp.com/api/") |
| 64 | + .addConverterFactory(new JSONAPIConverterFactory(rc)) |
| 65 | + .build() |
| 66 | + .create(QbountyApi.class); |
| 67 | + } |
| 68 | + |
| 69 | + |
| 70 | + @Test |
| 71 | + public void testApi() throws IOException { |
| 72 | + |
| 73 | + ArrayList<Claim> claims = api.getClaims().execute().body(); |
| 74 | + int l = claims.size(); |
| 75 | + assertEquals(2, l); |
| 76 | + |
| 77 | + } |
| 78 | + @Test |
| 79 | + public void testTasks() throws IOException { |
| 80 | + |
| 81 | + |
| 82 | + ArrayList<Task> tasks = api.getTasks().execute().body(); |
| 83 | + int l = tasks.size(); |
| 84 | + assertEquals(3, l); |
| 85 | + |
| 86 | + } |
| 87 | +} |
0 commit comments