Skip to content

Commit ff1dc01

Browse files
committed
test(ResponseTest): Add reactive request tests and refactor others
1 parent 2acd268 commit ff1dc01

File tree

1 file changed

+70
-8
lines changed

1 file changed

+70
-8
lines changed

src/test/java/com/ibm/cloud/sdk/core/test/service/ResponseTest.java

Lines changed: 70 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,42 +20,54 @@
2020
import com.ibm.cloud.sdk.core.service.model.GenericModel;
2121
import com.ibm.cloud.sdk.core.test.BaseServiceUnitTest;
2222
import com.ibm.cloud.sdk.core.util.ResponseConverterUtils;
23+
import io.reactivex.Single;
24+
import io.reactivex.functions.Consumer;
25+
import io.reactivex.schedulers.Schedulers;
2326
import okhttp3.Headers;
2427
import okhttp3.HttpUrl;
2528
import okhttp3.mockwebserver.MockResponse;
2629
import org.junit.Before;
2730
import org.junit.Test;
2831

29-
import java.util.concurrent.ExecutionException;
30-
3132
import static org.junit.Assert.assertEquals;
3233
import static org.junit.Assert.assertNotNull;
3334
import static org.junit.Assert.assertNull;
3435

3536
public class ResponseTest extends BaseServiceUnitTest {
37+
private class TestModel extends GenericModel {
38+
String city;
3639

37-
private class TestModel extends GenericModel { }
40+
String getKey() {
41+
return city;
42+
}
43+
}
3844

3945
public class TestService extends BaseService {
4046

4147
private static final String SERVICE_NAME = "test";
4248

43-
public TestService() {
49+
TestService() {
4450
super(SERVICE_NAME);
4551
}
4652

47-
public ServiceCall<TestModel> testMethod() {
53+
ServiceCall<TestModel> testMethod() {
4854
RequestBuilder builder = RequestBuilder.get(HttpUrl.parse(getEndPoint() + "/v1/test"));
4955
return createServiceCall(builder.build(), ResponseConverterUtils.getObject(TestModel.class));
5056
}
5157

52-
public ServiceCall<Void> testHeadMethod() {
58+
ServiceCall<Void> testHeadMethod() {
5359
RequestBuilder builder = RequestBuilder.head(HttpUrl.parse(getEndPoint() + "/v1/test"));
5460
return createServiceCall(builder.build(), ResponseConverterUtils.getVoid());
5561
}
5662
}
5763

5864
private TestService service;
65+
private String testResponseKey = "city";
66+
private String testResponseValue = "Columbus";
67+
private String testResponseBody = "{\"" + testResponseKey + "\": \"" + testResponseValue + "\"}";
68+
69+
// used for a specific test so we don't run into any weirdness with final, one-element, generic arrays
70+
private Response<TestModel> testResponseModel = null;
5971

6072
/*
6173
* (non-Javadoc)
@@ -78,10 +90,11 @@ public void setUp() throws Exception {
7890
*/
7991
@Test
8092
public void testExecuteWithDetails() throws InterruptedException {
81-
server.enqueue(new MockResponse().setBody("{\"test_key\": \"test_value\"}"));
93+
server.enqueue(new MockResponse().setBody(testResponseBody));
8294

8395
Response<TestModel> response = service.testMethod().executeWithDetails();
8496
assertNotNull(response.getResult());
97+
assertEquals(testResponseValue, response.getResult().getKey());
8598
assertNotNull(response.getHeaders());
8699
}
87100

@@ -92,18 +105,67 @@ public void testExecuteWithDetails() throws InterruptedException {
92105
*/
93106
@Test
94107
public void testEnqueueWithDetails() throws InterruptedException {
95-
server.enqueue(new MockResponse().setBody("{\"test_key\": \"test_value\"}"));
108+
server.enqueue(new MockResponse().setBody(testResponseBody));
96109

97110
service.testMethod().enqueueWithDetails(new ServiceCallbackWithDetails<TestModel>() {
98111
@Override
99112
public void onResponse(Response<TestModel> response) {
100113
assertNotNull(response.getResult());
114+
assertEquals(testResponseValue, response.getResult().getKey());
101115
assertNotNull(response.getHeaders());
102116
}
103117

104118
@Override
105119
public void onFailure(Exception e) { }
106120
});
121+
122+
Thread.sleep(2000);
123+
}
124+
125+
@Test
126+
public void testReactiveRequest() throws InterruptedException {
127+
server.enqueue(new MockResponse().setBody(testResponseBody));
128+
129+
final TestModel[] responseValue = new TestModel[1];
130+
Single<TestModel> observableRequest = service.testMethod().reactiveRequest();
131+
132+
observableRequest
133+
.subscribeOn(Schedulers.single())
134+
.subscribe(new Consumer<TestModel>() {
135+
@Override
136+
public void accept(TestModel testModel) throws Exception {
137+
responseValue[0] = testModel;
138+
}
139+
});
140+
141+
// asynchronous, so test that we continued without a value yet
142+
assertNull(responseValue[0]);
143+
Thread.sleep(2000);
144+
assertNotNull(responseValue[0]);
145+
assertEquals(testResponseValue, responseValue[0].getKey());
146+
}
147+
148+
@Test
149+
public void testReactiveRequestWithDetails() throws InterruptedException {
150+
server.enqueue(new MockResponse().setBody(testResponseBody));
151+
152+
Single<Response<TestModel>> observableRequest = service.testMethod().reactiveRequestWithDetails();
153+
154+
observableRequest
155+
.subscribeOn(Schedulers.single())
156+
.subscribe(new Consumer<Response<TestModel>>() {
157+
@Override
158+
public void accept(Response<TestModel> testModel) throws Exception {
159+
testResponseModel = testModel;
160+
}
161+
});
162+
163+
// asynchronous, so test that we continued without a value yet
164+
assertNull(testResponseModel);
165+
Thread.sleep(2000);
166+
assertNotNull(testResponseModel);
167+
assertEquals(testResponseValue, testResponseModel.getResult().getKey());
168+
assertNotNull(testResponseModel.getHeaders());
107169
}
108170

109171
/**

0 commit comments

Comments
 (0)