20
20
import com .ibm .cloud .sdk .core .service .model .GenericModel ;
21
21
import com .ibm .cloud .sdk .core .test .BaseServiceUnitTest ;
22
22
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 ;
23
26
import okhttp3 .Headers ;
24
27
import okhttp3 .HttpUrl ;
25
28
import okhttp3 .mockwebserver .MockResponse ;
26
29
import org .junit .Before ;
27
30
import org .junit .Test ;
28
31
29
- import java .util .concurrent .ExecutionException ;
30
-
31
32
import static org .junit .Assert .assertEquals ;
32
33
import static org .junit .Assert .assertNotNull ;
33
34
import static org .junit .Assert .assertNull ;
34
35
35
36
public class ResponseTest extends BaseServiceUnitTest {
37
+ private class TestModel extends GenericModel {
38
+ String city ;
36
39
37
- private class TestModel extends GenericModel { }
40
+ String getKey () {
41
+ return city ;
42
+ }
43
+ }
38
44
39
45
public class TestService extends BaseService {
40
46
41
47
private static final String SERVICE_NAME = "test" ;
42
48
43
- public TestService () {
49
+ TestService () {
44
50
super (SERVICE_NAME );
45
51
}
46
52
47
- public ServiceCall <TestModel > testMethod () {
53
+ ServiceCall <TestModel > testMethod () {
48
54
RequestBuilder builder = RequestBuilder .get (HttpUrl .parse (getEndPoint () + "/v1/test" ));
49
55
return createServiceCall (builder .build (), ResponseConverterUtils .getObject (TestModel .class ));
50
56
}
51
57
52
- public ServiceCall <Void > testHeadMethod () {
58
+ ServiceCall <Void > testHeadMethod () {
53
59
RequestBuilder builder = RequestBuilder .head (HttpUrl .parse (getEndPoint () + "/v1/test" ));
54
60
return createServiceCall (builder .build (), ResponseConverterUtils .getVoid ());
55
61
}
56
62
}
57
63
58
64
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 ;
59
71
60
72
/*
61
73
* (non-Javadoc)
@@ -78,10 +90,11 @@ public void setUp() throws Exception {
78
90
*/
79
91
@ Test
80
92
public void testExecuteWithDetails () throws InterruptedException {
81
- server .enqueue (new MockResponse ().setBody ("{ \" test_key \" : \" test_value \" }" ));
93
+ server .enqueue (new MockResponse ().setBody (testResponseBody ));
82
94
83
95
Response <TestModel > response = service .testMethod ().executeWithDetails ();
84
96
assertNotNull (response .getResult ());
97
+ assertEquals (testResponseValue , response .getResult ().getKey ());
85
98
assertNotNull (response .getHeaders ());
86
99
}
87
100
@@ -92,18 +105,67 @@ public void testExecuteWithDetails() throws InterruptedException {
92
105
*/
93
106
@ Test
94
107
public void testEnqueueWithDetails () throws InterruptedException {
95
- server .enqueue (new MockResponse ().setBody ("{ \" test_key \" : \" test_value \" }" ));
108
+ server .enqueue (new MockResponse ().setBody (testResponseBody ));
96
109
97
110
service .testMethod ().enqueueWithDetails (new ServiceCallbackWithDetails <TestModel >() {
98
111
@ Override
99
112
public void onResponse (Response <TestModel > response ) {
100
113
assertNotNull (response .getResult ());
114
+ assertEquals (testResponseValue , response .getResult ().getKey ());
101
115
assertNotNull (response .getHeaders ());
102
116
}
103
117
104
118
@ Override
105
119
public void onFailure (Exception e ) { }
106
120
});
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 ());
107
169
}
108
170
109
171
/**
0 commit comments