Skip to content

Commit bc7bb5b

Browse files
Merge pull request androidannotations#495 from excilys/RestAPITests
Added some tests around rest API
2 parents 8dc0d65 + 0d85d6c commit bc7bb5b

3 files changed

Lines changed: 193 additions & 0 deletions

File tree

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
/**
2+
* Copyright (C) 2010-2012 eBusiness Information, Excilys Group
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
5+
* use this file except in compliance with the License. You may obtain a copy of
6+
* the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed To in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations under
14+
* the License.
15+
*/
16+
package org.androidannotations.test15.rest;
17+
18+
import static org.mockito.Mockito.mock;
19+
import static org.mockito.Mockito.verify;
20+
import static org.mockito.Mockito.when;
21+
22+
import org.androidannotations.test15.AndroidAnnotationsTestRunner;
23+
import org.junit.Test;
24+
import org.junit.runner.RunWith;
25+
import org.mockito.Mockito;
26+
import org.springframework.http.HttpEntity;
27+
import org.springframework.http.HttpHeaders;
28+
import org.springframework.http.HttpMethod;
29+
import org.springframework.http.ResponseEntity;
30+
import org.springframework.web.client.RestTemplate;
31+
32+
@RunWith(AndroidAnnotationsTestRunner.class)
33+
public class HttpMethodServiceTest {
34+
35+
@Test
36+
public void use_delete_http_method() {
37+
HttpMethodsService_ service = new HttpMethodsService_();
38+
39+
RestTemplate restTemplate = mock(RestTemplate.class);
40+
service.setRestTemplate(restTemplate);
41+
42+
service.delete();
43+
44+
verify(restTemplate).exchange(Mockito.anyString(), Mockito.<HttpMethod> eq(HttpMethod.DELETE), Mockito.<HttpEntity<?>> any(), Mockito.<Class<Object>> any());
45+
}
46+
47+
@Test
48+
public void use_get_http_method() {
49+
HttpMethodsService_ service = new HttpMethodsService_();
50+
51+
RestTemplate restTemplate = mock(RestTemplate.class);
52+
service.setRestTemplate(restTemplate);
53+
54+
service.get();
55+
56+
verify(restTemplate).exchange(Mockito.anyString(), Mockito.<HttpMethod> eq(HttpMethod.GET), Mockito.<HttpEntity<?>> any(), Mockito.<Class<Object>> any());
57+
}
58+
59+
@Test
60+
@SuppressWarnings("unchecked")
61+
public void use_head_http_method() {
62+
HttpMethodsService_ service = new HttpMethodsService_();
63+
64+
RestTemplate restTemplate = mock(RestTemplate.class);
65+
ResponseEntity<Object> response = mock(ResponseEntity.class);
66+
when(restTemplate.exchange("http://company.com/ajax/services/head/", HttpMethod.HEAD, null, null)).thenReturn(response);
67+
68+
service.setRestTemplate(restTemplate);
69+
70+
service.head();
71+
72+
verify(restTemplate).exchange(Mockito.anyString(), Mockito.<HttpMethod> eq(HttpMethod.HEAD), Mockito.<HttpEntity<?>> any(), Mockito.<Class<Object>> any());
73+
}
74+
75+
@Test
76+
@SuppressWarnings("unchecked")
77+
public void use_options_http_method() {
78+
HttpMethodsService_ service = new HttpMethodsService_();
79+
80+
RestTemplate restTemplate = mock(RestTemplate.class);
81+
ResponseEntity<Object> response = mock(ResponseEntity.class);
82+
when(restTemplate.exchange("http://company.com/ajax/services/options/", HttpMethod.OPTIONS, null, null)).thenReturn(response);
83+
HttpHeaders headers = mock(HttpHeaders.class);
84+
when(response.getHeaders()).thenReturn(headers);
85+
86+
service.setRestTemplate(restTemplate);
87+
88+
service.options();
89+
90+
verify(restTemplate).exchange(Mockito.anyString(), Mockito.<HttpMethod> eq(HttpMethod.OPTIONS), Mockito.<HttpEntity<?>> any(), Mockito.<Class<Object>> any());
91+
}
92+
93+
@Test
94+
public void use_post_http_method() {
95+
HttpMethodsService_ service = new HttpMethodsService_();
96+
97+
RestTemplate restTemplate = mock(RestTemplate.class);
98+
service.setRestTemplate(restTemplate);
99+
100+
service.post();
101+
102+
verify(restTemplate).exchange(Mockito.anyString(), Mockito.<HttpMethod> eq(HttpMethod.POST), Mockito.<HttpEntity<?>> any(), Mockito.<Class<Object>> any());
103+
}
104+
105+
@Test
106+
public void use_put_http_method() {
107+
HttpMethodsService_ service = new HttpMethodsService_();
108+
109+
RestTemplate restTemplate = mock(RestTemplate.class);
110+
service.setRestTemplate(restTemplate);
111+
112+
service.put();
113+
114+
verify(restTemplate).exchange(Mockito.anyString(), Mockito.<HttpMethod> eq(HttpMethod.PUT), Mockito.<HttpEntity<?>> any(), Mockito.<Class<Object>> any());
115+
}
116+
117+
}

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,4 +143,24 @@ public void getEventsGenericsMap() {
143143
assertEquals(event2, eventsMap.get("event2"));
144144
}
145145

146+
@Test
147+
public void urlWithAParameterDeclaredTwiceTest() {
148+
addPendingResponse("[[{'id':1,'name':'event1'},{'id':2,'name':'event2'}],[{'id':1,'name':'event1'},{'id':2,'name':'event2'}]]");
149+
Event[][] results = myService.urlWithAParameterDeclaredTwice(1985);
150+
151+
Event event1 = new Event(1, "event1");
152+
Event event2 = new Event(2, "event2");
153+
Event[][] events = new Event[][] {{event1, event2}, {event1, event2}};
154+
155+
for (int i = 0 ; i < events.length ; i++) {
156+
157+
assertEquals(results[i].length, events[i].length);
158+
159+
for (int j = 0 ; j < events[i].length ; j++) {
160+
assertEquals(events[i][j].getName(), results[i][j].getName());
161+
assertEquals(events[i][j].getId(), results[i][j].getId());
162+
}
163+
}
164+
}
165+
146166
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/**
2+
* Copyright (C) 2010-2012 eBusiness Information, Excilys Group
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
5+
* use this file except in compliance with the License. You may obtain a copy of
6+
* the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed To in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations under
14+
* the License.
15+
*/
16+
package org.androidannotations.test15.rest;
17+
18+
import java.util.Set;
19+
20+
import org.androidannotations.annotations.rest.Delete;
21+
import org.androidannotations.annotations.rest.Get;
22+
import org.androidannotations.annotations.rest.Head;
23+
import org.androidannotations.annotations.rest.Options;
24+
import org.androidannotations.annotations.rest.Post;
25+
import org.androidannotations.annotations.rest.Put;
26+
import org.androidannotations.annotations.rest.Rest;
27+
import org.springframework.http.HttpHeaders;
28+
import org.springframework.http.HttpMethod;
29+
import org.springframework.http.converter.json.MappingJacksonHttpMessageConverter;
30+
import org.springframework.web.client.RestTemplate;
31+
32+
// if defined, the rootUrl will be added as a prefix to every request
33+
@Rest(rootUrl = "http://company.com/ajax/services", converters = { MappingJacksonHttpMessageConverter.class }, interceptors = { RequestInterceptor.class })
34+
public interface HttpMethodsService {
35+
36+
@Delete("/delete/")
37+
void delete();
38+
39+
@Get("/get/")
40+
void get();
41+
42+
@Head("/head/")
43+
HttpHeaders head();
44+
45+
@Options("/options/")
46+
Set<HttpMethod> options();
47+
48+
@Post("/post/")
49+
void post();
50+
51+
@Put("/put/")
52+
void put();
53+
54+
void setRestTemplate(RestTemplate restTemplate);
55+
56+
}

0 commit comments

Comments
 (0)