diff --git a/AndroidAnnotations/functional-test-1-5-tests/src/test/java/org/androidannotations/test15/rest/HttpMethodServiceTest.java b/AndroidAnnotations/functional-test-1-5-tests/src/test/java/org/androidannotations/test15/rest/HttpMethodServiceTest.java new file mode 100644 index 0000000000..06380f3607 --- /dev/null +++ b/AndroidAnnotations/functional-test-1-5-tests/src/test/java/org/androidannotations/test15/rest/HttpMethodServiceTest.java @@ -0,0 +1,117 @@ +/** + * Copyright (C) 2010-2012 eBusiness Information, Excilys Group + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed To in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ +package org.androidannotations.test15.rest; + +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import org.androidannotations.test15.AndroidAnnotationsTestRunner; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mockito; +import org.springframework.http.HttpEntity; +import org.springframework.http.HttpHeaders; +import org.springframework.http.HttpMethod; +import org.springframework.http.ResponseEntity; +import org.springframework.web.client.RestTemplate; + +@RunWith(AndroidAnnotationsTestRunner.class) +public class HttpMethodServiceTest { + + @Test + public void use_delete_http_method() { + HttpMethodsService_ service = new HttpMethodsService_(); + + RestTemplate restTemplate = mock(RestTemplate.class); + service.setRestTemplate(restTemplate); + + service.delete(); + + verify(restTemplate).exchange(Mockito.anyString(), Mockito. eq(HttpMethod.DELETE), Mockito.> any(), Mockito.> any()); + } + + @Test + public void use_get_http_method() { + HttpMethodsService_ service = new HttpMethodsService_(); + + RestTemplate restTemplate = mock(RestTemplate.class); + service.setRestTemplate(restTemplate); + + service.get(); + + verify(restTemplate).exchange(Mockito.anyString(), Mockito. eq(HttpMethod.GET), Mockito.> any(), Mockito.> any()); + } + + @Test + @SuppressWarnings("unchecked") + public void use_head_http_method() { + HttpMethodsService_ service = new HttpMethodsService_(); + + RestTemplate restTemplate = mock(RestTemplate.class); + ResponseEntity response = mock(ResponseEntity.class); + when(restTemplate.exchange("http://company.com/ajax/services/head/", HttpMethod.HEAD, null, null)).thenReturn(response); + + service.setRestTemplate(restTemplate); + + service.head(); + + verify(restTemplate).exchange(Mockito.anyString(), Mockito. eq(HttpMethod.HEAD), Mockito.> any(), Mockito.> any()); + } + + @Test + @SuppressWarnings("unchecked") + public void use_options_http_method() { + HttpMethodsService_ service = new HttpMethodsService_(); + + RestTemplate restTemplate = mock(RestTemplate.class); + ResponseEntity response = mock(ResponseEntity.class); + when(restTemplate.exchange("http://company.com/ajax/services/options/", HttpMethod.OPTIONS, null, null)).thenReturn(response); + HttpHeaders headers = mock(HttpHeaders.class); + when(response.getHeaders()).thenReturn(headers); + + service.setRestTemplate(restTemplate); + + service.options(); + + verify(restTemplate).exchange(Mockito.anyString(), Mockito. eq(HttpMethod.OPTIONS), Mockito.> any(), Mockito.> any()); + } + + @Test + public void use_post_http_method() { + HttpMethodsService_ service = new HttpMethodsService_(); + + RestTemplate restTemplate = mock(RestTemplate.class); + service.setRestTemplate(restTemplate); + + service.post(); + + verify(restTemplate).exchange(Mockito.anyString(), Mockito. eq(HttpMethod.POST), Mockito.> any(), Mockito.> any()); + } + + @Test + public void use_put_http_method() { + HttpMethodsService_ service = new HttpMethodsService_(); + + RestTemplate restTemplate = mock(RestTemplate.class); + service.setRestTemplate(restTemplate); + + service.put(); + + verify(restTemplate).exchange(Mockito.anyString(), Mockito. eq(HttpMethod.PUT), Mockito.> any(), Mockito.> any()); + } + +} diff --git a/AndroidAnnotations/functional-test-1-5-tests/src/test/java/org/androidannotations/test15/rest/MyServiceTest.java b/AndroidAnnotations/functional-test-1-5-tests/src/test/java/org/androidannotations/test15/rest/MyServiceTest.java index ccd3d9dbce..dbe2df2776 100644 --- a/AndroidAnnotations/functional-test-1-5-tests/src/test/java/org/androidannotations/test15/rest/MyServiceTest.java +++ b/AndroidAnnotations/functional-test-1-5-tests/src/test/java/org/androidannotations/test15/rest/MyServiceTest.java @@ -143,4 +143,24 @@ public void getEventsGenericsMap() { assertEquals(event2, eventsMap.get("event2")); } + @Test + public void urlWithAParameterDeclaredTwiceTest() { + addPendingResponse("[[{'id':1,'name':'event1'},{'id':2,'name':'event2'}],[{'id':1,'name':'event1'},{'id':2,'name':'event2'}]]"); + Event[][] results = myService.urlWithAParameterDeclaredTwice(1985); + + Event event1 = new Event(1, "event1"); + Event event2 = new Event(2, "event2"); + Event[][] events = new Event[][] {{event1, event2}, {event1, event2}}; + + for (int i = 0 ; i < events.length ; i++) { + + assertEquals(results[i].length, events[i].length); + + for (int j = 0 ; j < events[i].length ; j++) { + assertEquals(events[i][j].getName(), results[i][j].getName()); + assertEquals(events[i][j].getId(), results[i][j].getId()); + } + } + } + } diff --git a/AndroidAnnotations/functional-test-1-5/src/main/java/org/androidannotations/test15/rest/HttpMethodsService.java b/AndroidAnnotations/functional-test-1-5/src/main/java/org/androidannotations/test15/rest/HttpMethodsService.java new file mode 100644 index 0000000000..d716badc67 --- /dev/null +++ b/AndroidAnnotations/functional-test-1-5/src/main/java/org/androidannotations/test15/rest/HttpMethodsService.java @@ -0,0 +1,56 @@ +/** + * Copyright (C) 2010-2012 eBusiness Information, Excilys Group + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed To in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ +package org.androidannotations.test15.rest; + +import java.util.Set; + +import org.androidannotations.annotations.rest.Delete; +import org.androidannotations.annotations.rest.Get; +import org.androidannotations.annotations.rest.Head; +import org.androidannotations.annotations.rest.Options; +import org.androidannotations.annotations.rest.Post; +import org.androidannotations.annotations.rest.Put; +import org.androidannotations.annotations.rest.Rest; +import org.springframework.http.HttpHeaders; +import org.springframework.http.HttpMethod; +import org.springframework.http.converter.json.MappingJacksonHttpMessageConverter; +import org.springframework.web.client.RestTemplate; + +// if defined, the rootUrl will be added as a prefix to every request +@Rest(rootUrl = "http://company.com/ajax/services", converters = { MappingJacksonHttpMessageConverter.class }, interceptors = { RequestInterceptor.class }) +public interface HttpMethodsService { + + @Delete("/delete/") + void delete(); + + @Get("/get/") + void get(); + + @Head("/head/") + HttpHeaders head(); + + @Options("/options/") + Set options(); + + @Post("/post/") + void post(); + + @Put("/put/") + void put(); + + void setRestTemplate(RestTemplate restTemplate); + +}