Skip to content

Commit 4920ca6

Browse files
committed
Merge branch 'dhleong-475_Add_Missing_Rest_Authentication' into develop
2 parents 9a6c878 + 2c87f82 commit 4920ca6

10 files changed

Lines changed: 270 additions & 69 deletions

File tree

‎AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/processing/rest/DeleteProcessor.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
import org.androidannotations.annotations.rest.Delete;
2222

23-
public class DeleteProcessor extends GetPostProcessor {
23+
public class DeleteProcessor extends MethodCrudProcessor {
2424

2525
public DeleteProcessor(ProcessingEnvironment processingEnv, RestImplementationsHolder restImplementationsHolder) {
2626
super(processingEnv, restImplementationsHolder);

‎AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/processing/rest/GetProcessor.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
import com.sun.codemodel.JInvocation;
3131
import com.sun.codemodel.JVar;
3232

33-
public class GetProcessor extends GetPostProcessor {
33+
public class GetProcessor extends MethodCrudProcessor {
3434

3535
public GetProcessor(ProcessingEnvironment processingEnv, RestImplementationsHolder restImplementationHolder) {
3636
super(processingEnv, restImplementationHolder);

AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/processing/rest/GetPostProcessor.java renamed to AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/processing/rest/MethodCrudProcessor.java

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
import com.sun.codemodel.JInvocation;
4747
import com.sun.codemodel.JPackage;
4848

49-
public abstract class GetPostProcessor extends MethodProcessor {
49+
public abstract class MethodCrudProcessor extends MethodProcessor {
5050

5151
protected EBeanHolder holder;
5252

@@ -57,7 +57,7 @@ public abstract class GetPostProcessor extends MethodProcessor {
5757

5858
protected APTCodeModelHelper helper;
5959

60-
public GetPostProcessor(ProcessingEnvironment processingEnv, RestImplementationsHolder restImplementationsHolder) {
60+
public MethodCrudProcessor(ProcessingEnvironment processingEnv, RestImplementationsHolder restImplementationsHolder) {
6161
super(processingEnv, restImplementationsHolder);
6262
helper = new APTCodeModelHelper();
6363
}
@@ -251,11 +251,6 @@ protected String plainName(JClass jClass) {
251251
return plainName;
252252
}
253253

254-
@Override
255-
protected JInvocation addHttpEntityVar(JInvocation restCall, MethodProcessorHolder methodHolder) {
256-
return restCall.arg(generateHttpEntityVar(methodHolder));
257-
}
258-
259254
@Override
260255
protected JInvocation addResponseEntityArg(JInvocation restCall, MethodProcessorHolder methodHolder) {
261256
JClass expectedClass = methodHolder.getExpectedClass();

‎AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/processing/rest/MethodProcessor.java‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -182,12 +182,10 @@ protected void generateRestTemplateCallBlock(MethodProcessorHolder methodHolder)
182182
}
183183

184184
/**
185-
* Add the HttpEntity attribute to restTemplate.exchange() method. By
186-
* default, the value will be <code>null</code> (for DELETE, HEAD and
187-
* OPTIONS method type)
185+
* Add the HttpEntity attribute to restTemplate.exchange() method.
188186
*/
189187
protected JInvocation addHttpEntityVar(JInvocation restCall, MethodProcessorHolder methodHolder) {
190-
return restCall.arg(JExpr._null());
188+
return restCall.arg(generateHttpEntityVar(methodHolder));
191189
}
192190

193191
/**

‎AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/processing/rest/PostProcessor.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
import org.androidannotations.annotations.rest.Post;
2222

23-
public class PostProcessor extends GetPostProcessor {
23+
public class PostProcessor extends MethodCrudProcessor {
2424

2525
public PostProcessor(ProcessingEnvironment processingEnv, RestImplementationsHolder restImplementationHolder) {
2626
super(processingEnv, restImplementationHolder);

‎AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/processing/rest/PutProcessor.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
import org.androidannotations.annotations.rest.Put;
2222

23-
public class PutProcessor extends GetPostProcessor {
23+
public class PutProcessor extends MethodCrudProcessor {
2424

2525
public PutProcessor(ProcessingEnvironment processingEnv, RestImplementationsHolder restImplementationsHolder) {
2626
super(processingEnv, restImplementationsHolder);

‎AndroidAnnotations/functional-test-1-5-tests/src/test/java/org/androidannotations/test15/rest/HttpMethodServiceTest.java‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public void use_head_http_method() {
6363

6464
RestTemplate restTemplate = mock(RestTemplate.class);
6565
ResponseEntity<Object> response = mock(ResponseEntity.class);
66-
when(restTemplate.exchange("http://company.com/ajax/services/head/", HttpMethod.HEAD, null, null)).thenReturn(response);
66+
when(restTemplate.exchange(Mockito.anyString(), Mockito.<HttpMethod> eq(HttpMethod.HEAD), Mockito.<HttpEntity<?>> any(), Mockito.<Class<Object>> any())).thenReturn(response);
6767

6868
service.setRestTemplate(restTemplate);
6969

@@ -79,7 +79,7 @@ public void use_options_http_method() {
7979

8080
RestTemplate restTemplate = mock(RestTemplate.class);
8181
ResponseEntity<Object> response = mock(ResponseEntity.class);
82-
when(restTemplate.exchange("http://company.com/ajax/services/options/", HttpMethod.OPTIONS, null, null)).thenReturn(response);
82+
when(restTemplate.exchange(Mockito.anyString(), Mockito.<HttpMethod> eq(HttpMethod.OPTIONS), Mockito.<HttpEntity<?>> any(), Mockito.<Class<Object>> any())).thenReturn(response);
8383
HttpHeaders headers = mock(HttpHeaders.class);
8484
when(response.getHeaders()).thenReturn(headers);
8585

‎AndroidAnnotations/functional-test-1-5-tests/src/test/java/org/androidannotations/test15/rest/MyServiceTest.java‎

Lines changed: 56 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -27,20 +27,19 @@
2727
import java.util.Map;
2828

2929
import org.androidannotations.test15.AndroidAnnotationsTestRunner;
30+
import org.androidannotations.test15.rest.RequestTestBuilder.RequestTestBuilderExecutor;
3031
import org.apache.http.Header;
3132
import org.apache.http.message.BasicHeader;
3233
import org.junit.Test;
3334
import org.junit.runner.RunWith;
3435
import org.mockito.ArgumentMatcher;
3536
import org.mockito.Mockito;
3637
import org.springframework.http.HttpEntity;
37-
import org.springframework.http.HttpHeaders;
3838
import org.springframework.http.HttpMethod;
3939
import org.springframework.http.HttpStatus;
4040
import org.springframework.http.ResponseEntity;
4141
import org.springframework.web.client.RestTemplate;
4242

43-
import com.google.inject.matcher.Matchers;
4443
import com.xtremelabs.robolectric.Robolectric;
4544

4645
@RunWith(AndroidAnnotationsTestRunner.class)
@@ -51,16 +50,16 @@ public class MyServiceTest {
5150
private void addPendingResponse(String jsonResponse) {
5251
addPendingResponse(jsonResponse, "_=_");
5352
}
53+
5454
private void addPendingResponse(String jsonResponse, String... cookies) {
55-
Header[] headers = new Header[1 + cookies.length/2];
55+
Header[] headers = new Header[1 + cookies.length / 2];
5656
headers[0] = new BasicHeader("content-type", "application/json");
57-
for (int i=0, j=1; i < cookies.length-1; i += 2, j++) {
58-
headers[j] = new BasicHeader("set-cookie", cookies[i] + "=" + cookies[i+1]);
57+
for (int i = 0, j = 1; i < cookies.length - 1; i += 2, j++) {
58+
headers[j] = new BasicHeader("set-cookie", cookies[i] + "=" + cookies[i + 1]);
5959
}
6060
Robolectric.addPendingHttpResponse(HttpStatus.OK.value(), jsonResponse.replaceAll("'", "\""), headers);
6161
}
6262

63-
6463
@Test
6564
public void can_override_root_url() {
6665
MyService_ myService = new MyService_();
@@ -166,46 +165,45 @@ public void urlWithAParameterDeclaredTwiceTest() {
166165

167166
Event event1 = new Event(1, "event1");
168167
Event event2 = new Event(2, "event2");
169-
Event[][] events = new Event[][] {{event1, event2}, {event1, event2}};
168+
Event[][] events = new Event[][] { { event1, event2 }, { event1, event2 } };
170169

171-
for (int i = 0 ; i < events.length ; i++) {
170+
for (int i = 0; i < events.length; i++) {
172171

173172
assertEquals(results[i].length, events[i].length);
174173

175-
for (int j = 0 ; j < events[i].length ; j++) {
174+
for (int j = 0; j < events[i].length; j++) {
176175
assertEquals(events[i][j].getName(), results[i][j].getName());
177176
assertEquals(events[i][j].getId(), results[i][j].getId());
178177
}
179178
}
180179
}
181-
180+
182181
@Test
183182
public void manualFullUrl() {
184-
183+
185184
MyService_ myService = new MyService_();
186185

187186
RestTemplate restTemplate = mock(RestTemplate.class);
188187
myService.setRestTemplate(restTemplate);
189188

190189
// make sure we used the full custom url.
191190
// this may be used like in Google's APIs
192-
// to fetch an oauth token; Mockito doesn't
193-
// return a response with the mock'd template,
194-
// so we just use this weird "ping" endpoint
191+
// to fetch an oauth token; Mockito doesn't
192+
// return a response with the mock'd template,
193+
// so we just use this weird "ping" endpoint
195194
addPendingResponse("fancyHeaderToken");
196195
myService.setHttpBasicAuth("fancyUser", "fancierPassword");
197196
myService.ping();
198197
verify(restTemplate).exchange(eq("http://company.com/client/ping"), Mockito.<HttpMethod> any(), Mockito.<HttpEntity<?>> any(), Mockito.<Class<Object>> any());
199198
}
200-
199+
201200
@Test
202201
public void cookieInUrl() {
203-
204202
final String xtValue = "1234";
205203
final String sjsaidValue = "7890";
206204
final String locationValue = "somePlace";
207205
final int yearValue = 2013;
208-
206+
209207
MyService_ myService = new MyService_();
210208

211209
RestTemplate restTemplate = mock(RestTemplate.class);
@@ -214,13 +212,13 @@ public void cookieInUrl() {
214212
addPendingResponse("{'id':1,'name':'event1'}");
215213

216214
// normally this is set by a call like authenticate()
217-
// which is annotated with @SetsCookie
215+
// which is annotated with @SetsCookie
218216
myService.setCookie("xt", xtValue);
219217
myService.setCookie("sjsaid", sjsaidValue);
220218
myService.setHttpBasicAuth("fancyUser", "fancierPassword");
221219
myService.getEventsVoid(locationValue, yearValue);
222220

223-
ArgumentMatcher<HttpEntity<Void>> matcher = new ArgumentMatcher<HttpEntity<Void>>() {
221+
ArgumentMatcher<HttpEntity<Void>> matcher = new ArgumentMatcher<HttpEntity<Void>>() {
224222

225223
@Override
226224
public boolean matches(Object argument) {
@@ -237,18 +235,46 @@ public boolean matches(Object argument) {
237235
}
238236

239237
@Test
240-
public void authenticatedRequests() {
241-
242-
String xtValue = "1234";
243-
String sjsaidValue = "5678";
244-
245-
myService.setHeader("SomeFancyHeader", "fancyHeaderToken");
246-
247-
addPendingResponse("[]", "xt", xtValue, "sjsaid", sjsaidValue);
248-
myService.authenticate();
238+
public void authenticate() {
239+
RequestTestBuilder.build() //
240+
.requestHeader("SomeFancyHeader", "aFancyHeader") //
241+
.responseCookie("xt", "1234") //
242+
.responseCookie("sjsaid", "5678") //
243+
.asserts(new RequestTestBuilderExecutor() {
244+
@Override
245+
public void execute(MyService myService) {
246+
myService.authenticate();
247+
}
248+
});
249+
}
249250

250-
assertEquals(xtValue, myService.getCookie("xt"));
251-
assertEquals(sjsaidValue, myService.getCookie("sjsaid"));
251+
@Test
252+
public void removeEventWithRequires() {
253+
RequestTestBuilder.build() //
254+
.requestCookie("myCookie", "myCookieValue") //
255+
.requestHeader("SomeFancyHeader", "aFancyHeader") //
256+
.hasUrlVariables(true) //
257+
.asserts(new RequestTestBuilderExecutor() {
258+
@Override
259+
public void execute(MyService myService) {
260+
myService.removeEventWithRequires(0);
261+
}
262+
});
263+
}
252264

265+
@Test
266+
public void updateEventWithRequires() {
267+
RequestTestBuilder.build() //
268+
.requestCookie("myCookie", "myCookieValue") //
269+
.requestHeader("SomeFancyHeader", "aFancyHeader") //
270+
.responseContent("{'id':1,'name':'event1'}") //
271+
.hasUrlVariables(true) //
272+
.asserts(new RequestTestBuilderExecutor() {
273+
@Override
274+
public void execute(MyService myService) {
275+
myService.updateEventWithRequires(0);
276+
}
277+
});
253278
}
279+
254280
}

0 commit comments

Comments
 (0)