Skip to content

Commit f704635

Browse files
Add unit tests for DefaultHomeRemoteDataSource
1 parent a82cdd8 commit f704635

File tree

1 file changed

+116
-0
lines changed

1 file changed

+116
-0
lines changed
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
package com.d4rk.androidtutorials.java.data.source;
2+
3+
import static org.junit.Assert.assertEquals;
4+
import static org.junit.Assert.assertTrue;
5+
import static org.mockito.ArgumentMatchers.any;
6+
import static org.mockito.Mockito.mock;
7+
import static org.mockito.Mockito.verify;
8+
import static org.mockito.Mockito.when;
9+
10+
import android.os.Handler;
11+
12+
import com.android.volley.RequestQueue;
13+
import com.android.volley.VolleyError;
14+
import com.android.volley.toolbox.JsonObjectRequest;
15+
import com.d4rk.androidtutorials.java.data.model.PromotedApp;
16+
17+
import org.json.JSONArray;
18+
import org.json.JSONObject;
19+
import org.junit.Test;
20+
import org.mockito.ArgumentCaptor;
21+
22+
import java.lang.reflect.Field;
23+
import java.lang.reflect.Method;
24+
import java.util.Arrays;
25+
import java.util.List;
26+
import java.util.concurrent.Executor;
27+
28+
public class DefaultHomeRemoteDataSourceTest {
29+
30+
@Test
31+
@SuppressWarnings("unchecked")
32+
public void fetchPromotedApps_filtersInternalPackages() throws Exception {
33+
RequestQueue requestQueue = mock(RequestQueue.class);
34+
DefaultHomeRemoteDataSource dataSource = new DefaultHomeRemoteDataSource(requestQueue, "https://example.com");
35+
makeSynchronous(dataSource);
36+
HomeRemoteDataSource.PromotedAppsCallback callback = mock(HomeRemoteDataSource.PromotedAppsCallback.class);
37+
38+
dataSource.fetchPromotedApps(callback);
39+
40+
ArgumentCaptor<JsonObjectRequest> requestCaptor = ArgumentCaptor.forClass(JsonObjectRequest.class);
41+
verify(requestQueue).add(requestCaptor.capture());
42+
JsonObjectRequest capturedRequest = requestCaptor.getValue();
43+
44+
JSONObject response = new JSONObject()
45+
.put("data", new JSONObject()
46+
.put("apps", new JSONArray()
47+
.put(new JSONObject()
48+
.put("name", "Android Tutorials")
49+
.put("packageName", "com.d4rk.androidtutorials.sample")
50+
.put("iconLogo", "internal.png"))
51+
.put(new JSONObject()
52+
.put("name", "Cool App")
53+
.put("packageName", "com.example.cool")
54+
.put("iconLogo", "cool.png"))
55+
.put(new JSONObject()
56+
.put("name", "Another App")
57+
.put("packageName", "org.sample.another")
58+
.put("iconLogo", "another.png"))));
59+
60+
invokeDeliverResponse(capturedRequest, response);
61+
62+
ArgumentCaptor<List> appsCaptor = ArgumentCaptor.forClass(List.class);
63+
verify(callback).onResult(appsCaptor.capture());
64+
List<PromotedApp> apps = appsCaptor.getValue();
65+
List<PromotedApp> expected = Arrays.asList(
66+
new PromotedApp("Cool App", "com.example.cool", "cool.png"),
67+
new PromotedApp("Another App", "org.sample.another", "another.png"));
68+
assertEquals(expected, apps);
69+
}
70+
71+
@Test
72+
@SuppressWarnings("unchecked")
73+
public void fetchPromotedApps_errorReturnsEmptyList() throws Exception {
74+
RequestQueue requestQueue = mock(RequestQueue.class);
75+
DefaultHomeRemoteDataSource dataSource = new DefaultHomeRemoteDataSource(requestQueue, "https://example.com");
76+
makeSynchronous(dataSource);
77+
HomeRemoteDataSource.PromotedAppsCallback callback = mock(HomeRemoteDataSource.PromotedAppsCallback.class);
78+
79+
dataSource.fetchPromotedApps(callback);
80+
81+
ArgumentCaptor<JsonObjectRequest> requestCaptor = ArgumentCaptor.forClass(JsonObjectRequest.class);
82+
verify(requestQueue).add(requestCaptor.capture());
83+
JsonObjectRequest capturedRequest = requestCaptor.getValue();
84+
85+
capturedRequest.deliverError(new VolleyError("network"));
86+
87+
ArgumentCaptor<List> appsCaptor = ArgumentCaptor.forClass(List.class);
88+
verify(callback).onResult(appsCaptor.capture());
89+
List<PromotedApp> apps = appsCaptor.getValue();
90+
assertTrue(apps.isEmpty());
91+
}
92+
93+
private static void makeSynchronous(DefaultHomeRemoteDataSource dataSource) throws Exception {
94+
Handler handler = mock(Handler.class);
95+
when(handler.post(any())).thenAnswer(invocation -> {
96+
Runnable runnable = invocation.getArgument(0);
97+
runnable.run();
98+
return true;
99+
});
100+
101+
setField(dataSource, "executor", (Executor) Runnable::run);
102+
setField(dataSource, "mainHandler", handler);
103+
}
104+
105+
private static void setField(DefaultHomeRemoteDataSource dataSource, String name, Object value) throws Exception {
106+
Field field = DefaultHomeRemoteDataSource.class.getDeclaredField(name);
107+
field.setAccessible(true);
108+
field.set(dataSource, value);
109+
}
110+
111+
private static void invokeDeliverResponse(JsonObjectRequest request, JSONObject response) throws Exception {
112+
Method deliverResponse = JsonObjectRequest.class.getDeclaredMethod("deliverResponse", JSONObject.class);
113+
deliverResponse.setAccessible(true);
114+
deliverResponse.invoke(request, response);
115+
}
116+
}

0 commit comments

Comments
 (0)