-
Notifications
You must be signed in to change notification settings - Fork 1
/
InjectionRobot.java
226 lines (161 loc) · 5.58 KB
/
InjectionRobot.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
package com.mytestedapp;
import android.support.annotation.NonNull;
import com.mytestedapp.rest.LoginRequest;
import com.mytestedapp.rest.LoginResponse;
import com.mytestedapp.rest.ProfileResponse;
import com.mytestedapp.rest.RestService;
import org.hamcrest.Matcher;
import org.mockito.ArgumentMatcher;
import org.mockito.Matchers;
import org.mockito.internal.matchers.Not;
import java.io.IOException;
import java.net.SocketException;
import okhttp3.MediaType;
import okhttp3.Request;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import static org.mockito.Matchers.argThat;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
public class InjectionRobot {
private Injection mInjection;
private RestService mRestService;
private static <T> Call<T> makeSuccessCall(final T body) {
return new Call<T>() {
@Override
public Response<T> execute() throws IOException {
return null;
}
@Override
public void enqueue(@NonNull Callback<T> callback) {
callback.onResponse(this, Response.success(body));
}
@Override
public boolean isExecuted() {
return false;
}
@Override
public void cancel() {
}
@Override
public boolean isCanceled() {
return false;
}
@Override
public Call<T> clone() {
return null;
}
@Override
public Request request() {
return null;
}
};
}
private static <T> Call<T> makeErrorCall(final int statusCode, final String body) {
return new Call<T>() {
@Override
public Response<T> execute() throws IOException {
return null;
}
@Override
public void enqueue(@NonNull Callback<T> callback) {
ResponseBody responseBody = ResponseBody.create(MediaType.parse("application/json"), body);
callback.onResponse(this, Response.<T>error(statusCode, responseBody));
}
@Override
public boolean isExecuted() {
return false;
}
@Override
public void cancel() {
}
@Override
public boolean isCanceled() {
return false;
}
@Override
public Call<T> clone() {
return null;
}
@Override
public Request request() {
return null;
}
};
}
private static <T> Call<T> makeFailureCall(final Throwable t) {
return new Call<T>() {
@Override
public Response<T> execute() throws IOException {
return null;
}
@Override
public void enqueue(@NonNull Callback<T> callback) {
callback.onFailure(this, t);
}
@Override
public boolean isExecuted() {
return false;
}
@Override
public void cancel() {
}
@Override
public boolean isCanceled() {
return false;
}
@Override
public Call<T> clone() {
return null;
}
@Override
public Request request() {
return null;
}
};
}
public InjectionRobot setup() {
mInjection = spy(new Injection());
Injection.setInstance(mInjection);
setupRestService();
return this;
}
public InjectionRobot setupRestService() {
if (mRestService != null) {
return this;
}
mRestService = mock(RestService.class);
doReturn(mRestService).when(mInjection).provideRestService();
return this;
}
public InjectionRobot setUsernamePassword(final String username, final String password) {
Matcher<LoginRequest> matcher = new ArgumentMatcher<LoginRequest>() {
@Override
public boolean matches(Object argument) {
LoginRequest loginRequest = (LoginRequest) argument;
return username.equals(loginRequest.getUsername()) && password.equals(loginRequest.getPassword());
}
};
LoginResponse loginResponse = new LoginResponse("ABC000000XYZ");
doReturn(makeSuccessCall(loginResponse)).when(mRestService).login(argThat(matcher));
Matcher<LoginRequest> notMatcher = new Not(matcher);
doReturn(makeErrorCall(400, "{error: \"invalid credentials\"}")).when(mRestService).login(argThat(notMatcher));
return this;
}
public InjectionRobot setUsernamePasswordNetworkError() {
doReturn(makeFailureCall(new SocketException("test bad network"))).when(mRestService).login(Matchers.<LoginRequest>any());
return this;
}
public InjectionRobot setProfile(String firstName, String lastName, String totalRides) {
ProfileResponse profileResponse = new ProfileResponse(firstName, lastName, totalRides);
doReturn(makeSuccessCall(profileResponse)).when(mRestService).profile();
return this;
}
public InjectionRobot setProfileNetworkError() {
doReturn(makeFailureCall(new SocketException())).when(mRestService).profile();
return this;
}
}