Skip to content

Commit 4d8d654

Browse files
committed
Add async tests for TestService
1 parent d4d246e commit 4d8d654

File tree

2 files changed

+168
-1
lines changed

2 files changed

+168
-1
lines changed
Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
package servicestack.net.client.tests;
2+
3+
import android.app.Application;
4+
import android.test.ApplicationTestCase;
5+
6+
import net.servicestack.android.*;
7+
import net.servicestack.client.*;
8+
9+
import java.net.HttpURLConnection;
10+
import java.util.concurrent.CountDownLatch;
11+
import java.util.concurrent.TimeUnit;
12+
13+
import servicestack.net.client.tests.testdtos.*;
14+
15+
public class TestServiceTestsAsync extends ApplicationTestCase<Application> {
16+
public TestServiceTestsAsync() {
17+
super(Application.class);
18+
Log.Instance = new AndroidLogProvider("ZZZ");
19+
}
20+
21+
AndroidServiceClient client = new AndroidServiceClient("http://test.servicestack.net");
22+
23+
public void test_Can_POST_Test_HelloAllTypes_async() throws InterruptedException {
24+
final HelloAllTypes request = TestServiceTests.createHelloAllTypes();
25+
26+
final CountDownLatch signal = new CountDownLatch(1);
27+
28+
client.postAsync(request, new AsyncResult<HelloAllTypesResponse>() {
29+
@Override
30+
public void success(HelloAllTypesResponse response) {
31+
TestServiceTests.assertHelloAllTypesResponse(response, request);
32+
}
33+
34+
@Override
35+
public void complete() {
36+
signal.countDown();
37+
}
38+
});
39+
40+
assertTrue(signal.await(5, TimeUnit.SECONDS));
41+
}
42+
43+
public void test_Can_PUT_Test_HelloAllTypes_async() throws InterruptedException {
44+
final HelloAllTypes request = TestServiceTests.createHelloAllTypes();
45+
46+
final CountDownLatch signal = new CountDownLatch(1);
47+
48+
client.putAsync(request, new AsyncResult<HelloAllTypesResponse>() {
49+
@Override
50+
public void success(HelloAllTypesResponse response) {
51+
TestServiceTests.assertHelloAllTypesResponse(response, request);
52+
}
53+
54+
@Override
55+
public void complete() {
56+
signal.countDown();
57+
}
58+
});
59+
60+
assertTrue(signal.await(5, TimeUnit.SECONDS));
61+
}
62+
63+
64+
public void test_Does_handle_404_Error_Async() throws InterruptedException {
65+
AndroidServiceClient testClient = new AndroidServiceClient("http://test.servicestack.net");
66+
67+
final Exception[] globalError = new Exception[1]; //Wow Java, you suck.
68+
final Exception[] localError = new Exception[1];
69+
final WebServiceException[] thrownError = {null};
70+
71+
AndroidServiceClient.GlobalExceptionFilter = new ExceptionFilter() {
72+
@Override
73+
public void exec(HttpURLConnection res, Exception ex) {
74+
globalError[0] = ex;
75+
}
76+
};
77+
78+
testClient.ExceptionFilter = new ExceptionFilter() {
79+
@Override
80+
public void exec(HttpURLConnection res, Exception ex) {
81+
localError[0] = ex;
82+
}
83+
};
84+
85+
ThrowType request = new ThrowType()
86+
.setType("NotFound")
87+
.setMessage("not here");
88+
89+
90+
final CountDownLatch signal = new CountDownLatch(1);
91+
92+
testClient.putAsync(request, new AsyncResult<ThrowTypeResponse>() {
93+
@Override
94+
public void success(ThrowTypeResponse response) {
95+
fail("should not succeed");
96+
}
97+
98+
@Override
99+
public void error(Exception ex) {
100+
thrownError[0] = (WebServiceException) ex;
101+
}
102+
103+
@Override
104+
public void complete() {
105+
signal.countDown();
106+
}
107+
});
108+
109+
assertTrue(signal.await(5, TimeUnit.SECONDS));
110+
111+
assertNotNull(globalError[0]);
112+
assertNotNull(localError[0]);
113+
assertNotNull(thrownError[0]);
114+
115+
ResponseStatus status = thrownError[0].getResponseStatus();
116+
117+
assertEquals("not here", status.getErrorCode());
118+
assertEquals("not here", status.getMessage());
119+
assertNotNull(status.getStackTrace());
120+
}
121+
122+
public void test_Does_handle_ValidationException_Async(){
123+
ThrowValidation request = new ThrowValidation()
124+
.setEmail("invalidemail");
125+
126+
final CountDownLatch signal = new CountDownLatch(1);
127+
128+
client.postAsync(request, new AsyncResult<ThrowValidationResponse>() {
129+
@Override
130+
public void success(ThrowValidationResponse response) {
131+
fail("should not succeed");
132+
}
133+
134+
@Override
135+
public void error(Exception ex) {
136+
WebServiceException webEx = (WebServiceException)ex;
137+
ResponseStatus status = webEx.getResponseStatus();
138+
139+
assertNotNull(status);
140+
assertEquals(3, status.getErrors().size());
141+
142+
assertEquals(status.getErrors().get(0).getErrorCode(), status.getErrorCode());
143+
assertEquals(status.getErrors().get(0).getMessage(), status.getMessage());
144+
145+
assertEquals("InclusiveBetween", status.getErrors().get(0).errorCode);
146+
assertEquals("'Age' must be between 1 and 120. You entered 0.", status.getErrors().get(0).getMessage());
147+
assertEquals("Age", status.getErrors().get(0).getFieldName());
148+
149+
assertEquals("NotEmpty", status.getErrors().get(1).errorCode);
150+
assertEquals("'Required' should not be empty.", status.getErrors().get(1).getMessage());
151+
assertEquals("Required", status.getErrors().get(1).getFieldName());
152+
153+
assertEquals("Email", status.getErrors().get(2).errorCode);
154+
assertEquals("'Email' is not a valid email address.", status.getErrors().get(2).getMessage());
155+
assertEquals("Email", status.getErrors().get(2).getFieldName());
156+
}
157+
158+
@Override
159+
public void complete() {
160+
signal.countDown();
161+
}
162+
});
163+
}
164+
165+
166+
/* TEST HELPERS */
167+
}

src/AndroidClient/client/src/main/java/net/servicestack/client/AsyncResult.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public final void setError(Exception value) {
2121

2222
public final void completeResult(T value){
2323
try {
24-
if (ex != null){
24+
if (ex == null){
2525
setResult(value);
2626
success(value);
2727
}

0 commit comments

Comments
 (0)